blob: 2cabced208c56c4d5eb8680b8b39853debf8eb38 [file] [log] [blame]
pbrook714fa302009-04-01 12:27:59 +00001/*
2 * Framebuffer device helper routines
3 *
4 * Copyright (c) 2009 CodeSourcery
5 * Written by Paul Brook <paul@codesourcery.com>
6 *
7 * This code is licensed under the GNU GPLv2.
Paolo Bonzini6b620ca2012-01-13 17:44:23 +01008 *
9 * Contributions after 2012-01-13 are licensed under the terms of the
10 * GNU GPL, version 2 or (at your option) any later version.
pbrook714fa302009-04-01 12:27:59 +000011 */
12
13/* TODO:
14 - Do something similar for framebuffers with local ram
15 - Handle rotation here instead of hacking dest_pitch
16 - Use common pixel conversion routines instead of per-device drawfn
17 - Remove all DisplayState knowledge from devices.
18 */
19
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010020#include "hw/hw.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010021#include "ui/console.h"
Paolo Bonzini47b43a12013-03-18 17:36:02 +010022#include "framebuffer.h"
pbrook714fa302009-04-01 12:27:59 +000023
24/* Render an image from a shared memory framebuffer. */
25
26void framebuffer_update_display(
Gerd Hoffmannc78f7132013-03-05 15:24:14 +010027 DisplaySurface *ds,
Avi Kivity75c9d6c2011-12-08 16:00:54 +020028 MemoryRegion *address_space,
Avi Kivitya8170e52012-10-23 12:30:10 +020029 hwaddr base,
pbrook714fa302009-04-01 12:27:59 +000030 int cols, /* Width in pixels. */
Stefan Weil9c6bb552012-08-11 21:32:02 +020031 int rows, /* Height in pixels. */
pbrook714fa302009-04-01 12:27:59 +000032 int src_width, /* Length of source line, in bytes. */
33 int dest_row_pitch, /* Bytes between adjacent horizontal output pixels. */
34 int dest_col_pitch, /* Bytes between adjacent vertical output pixels. */
35 int invalidate, /* nonzero to redraw the whole image. */
36 drawfn fn,
37 void *opaque,
38 int *first_row, /* Input and output. */
39 int *last_row /* Output only */)
40{
Avi Kivitya8170e52012-10-23 12:30:10 +020041 hwaddr src_len;
pbrook714fa302009-04-01 12:27:59 +000042 uint8_t *dest;
43 uint8_t *src;
44 uint8_t *src_base;
45 int first, last = 0;
46 int dirty;
47 int i;
Anthony Liguoric227f092009-10-01 16:12:16 -050048 ram_addr_t addr;
Avi Kivity75c9d6c2011-12-08 16:00:54 +020049 MemoryRegionSection mem_section;
50 MemoryRegion *mem;
pbrook714fa302009-04-01 12:27:59 +000051
52 i = *first_row;
53 *first_row = -1;
54 src_len = src_width * rows;
55
Avi Kivity75c9d6c2011-12-08 16:00:54 +020056 mem_section = memory_region_find(address_space, base, src_len);
Paolo Bonzinidfde4e62013-05-06 10:46:11 +020057 mem = mem_section.mr;
Paolo Bonzini052e87b2013-05-27 10:08:27 +020058 if (int128_get64(mem_section.size) != src_len ||
59 !memory_region_is_ram(mem_section.mr)) {
Paolo Bonzinidfde4e62013-05-06 10:46:11 +020060 goto out;
pbrook714fa302009-04-01 12:27:59 +000061 }
Avi Kivity75c9d6c2011-12-08 16:00:54 +020062 assert(mem);
63 assert(mem_section.offset_within_address_space == base);
pbrook714fa302009-04-01 12:27:59 +000064
Avi Kivityc1cd0b22011-12-15 16:07:48 +020065 memory_region_sync_dirty_bitmap(mem);
Paolo Bonzinid55d4202015-03-23 10:46:52 +010066 if (!memory_region_is_logging(mem, DIRTY_MEMORY_VGA)) {
67 invalidate = true;
68 }
69
pbrook714fa302009-04-01 12:27:59 +000070 src_base = cpu_physical_memory_map(base, &src_len, 0);
71 /* If we can't map the framebuffer then bail. We could try harder,
72 but it's not really worth it as dirty flag tracking will probably
73 already have failed above. */
74 if (!src_base)
Paolo Bonzinidfde4e62013-05-06 10:46:11 +020075 goto out;
pbrook714fa302009-04-01 12:27:59 +000076 if (src_len != src_width * rows) {
77 cpu_physical_memory_unmap(src_base, src_len, 0, 0);
Paolo Bonzinidfde4e62013-05-06 10:46:11 +020078 goto out;
pbrook714fa302009-04-01 12:27:59 +000079 }
80 src = src_base;
Gerd Hoffmannc78f7132013-03-05 15:24:14 +010081 dest = surface_data(ds);
pbrook714fa302009-04-01 12:27:59 +000082 if (dest_col_pitch < 0)
83 dest -= dest_col_pitch * (cols - 1);
Vasily Khoruzhick93128052011-06-17 13:04:36 +030084 if (dest_row_pitch < 0) {
85 dest -= dest_row_pitch * (rows - 1);
86 }
pbrook714fa302009-04-01 12:27:59 +000087 first = -1;
Avi Kivity75c9d6c2011-12-08 16:00:54 +020088 addr = mem_section.offset_within_region;
pbrook714fa302009-04-01 12:27:59 +000089
90 addr += i * src_width;
91 src += i * src_width;
92 dest += i * dest_row_pitch;
93
94 for (; i < rows; i++) {
Blue Swirld1f3dd32012-02-04 17:09:14 +000095 dirty = memory_region_get_dirty(mem, addr, src_width,
Avi Kivity75c9d6c2011-12-08 16:00:54 +020096 DIRTY_MEMORY_VGA);
pbrook714fa302009-04-01 12:27:59 +000097 if (dirty || invalidate) {
98 fn(opaque, dest, src, cols, dest_col_pitch);
99 if (first == -1)
100 first = i;
101 last = i;
102 }
103 addr += src_width;
104 src += src_width;
105 dest += dest_row_pitch;
106 }
107 cpu_physical_memory_unmap(src_base, src_len, 0, 0);
108 if (first < 0) {
Paolo Bonzinidfde4e62013-05-06 10:46:11 +0200109 goto out;
pbrook714fa302009-04-01 12:27:59 +0000110 }
Avi Kivity75c9d6c2011-12-08 16:00:54 +0200111 memory_region_reset_dirty(mem, mem_section.offset_within_region, src_len,
112 DIRTY_MEMORY_VGA);
pbrook714fa302009-04-01 12:27:59 +0000113 *first_row = first;
114 *last_row = last;
Paolo Bonzinidfde4e62013-05-06 10:46:11 +0200115out:
116 memory_region_unref(mem);
pbrook714fa302009-04-01 12:27:59 +0000117}