xref: /qemu/hw/display/framebuffer.c (revision bfa3ab61)
1 /*
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.
8  *
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.
11  */
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 
20 #include "hw/hw.h"
21 #include "ui/console.h"
22 #include "framebuffer.h"
23 
24 /* Render an image from a shared memory framebuffer.  */
25 
26 void framebuffer_update_display(
27     DisplaySurface *ds,
28     MemoryRegion *address_space,
29     hwaddr base,
30     int cols, /* Width in pixels.  */
31     int rows, /* Height in pixels.  */
32     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 {
41     hwaddr src_len;
42     uint8_t *dest;
43     uint8_t *src;
44     uint8_t *src_base;
45     int first, last = 0;
46     int dirty;
47     int i;
48     ram_addr_t addr;
49     MemoryRegionSection mem_section;
50     MemoryRegion *mem;
51 
52     i = *first_row;
53     *first_row = -1;
54     src_len = src_width * rows;
55 
56     mem_section = memory_region_find(address_space, base, src_len);
57     mem = mem_section.mr;
58     if (int128_get64(mem_section.size) != src_len ||
59             !memory_region_is_ram(mem_section.mr)) {
60         goto out;
61     }
62     assert(mem);
63     assert(mem_section.offset_within_address_space == base);
64 
65     memory_region_sync_dirty_bitmap(mem);
66     if (!memory_region_is_logging(mem, DIRTY_MEMORY_VGA)) {
67         invalidate = true;
68     }
69 
70     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)
75         goto out;
76     if (src_len != src_width * rows) {
77         cpu_physical_memory_unmap(src_base, src_len, 0, 0);
78         goto out;
79     }
80     src = src_base;
81     dest = surface_data(ds);
82     if (dest_col_pitch < 0)
83         dest -= dest_col_pitch * (cols - 1);
84     if (dest_row_pitch < 0) {
85         dest -= dest_row_pitch * (rows - 1);
86     }
87     first = -1;
88     addr = mem_section.offset_within_region;
89 
90     addr += i * src_width;
91     src += i * src_width;
92     dest += i * dest_row_pitch;
93 
94     for (; i < rows; i++) {
95         dirty = memory_region_get_dirty(mem, addr, src_width,
96                                              DIRTY_MEMORY_VGA);
97         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) {
109         goto out;
110     }
111     memory_region_reset_dirty(mem, mem_section.offset_within_region, src_len,
112                               DIRTY_MEMORY_VGA);
113     *first_row = first;
114     *last_row = last;
115 out:
116     memory_region_unref(mem);
117 }
118