1086ba9d05fa15551e0ab2b2d142957d4c1569a5
[qemu] / hw / framebuffer.c
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
10 /* TODO:
11    - Do something similar for framebuffers with local ram
12    - Handle rotation here instead of hacking dest_pitch
13    - Use common pixel conversion routines instead of per-device drawfn
14    - Remove all DisplayState knowledge from devices.
15  */
16
17 #include "hw.h"
18 #include "console.h"
19 #include "framebuffer.h"
20 #include "kvm.h"
21
22 /* Render an image from a shared memory framebuffer.  */
23    
24 void framebuffer_update_display(
25     DisplayState *ds,
26     target_phys_addr_t base,
27     int cols, /* Width in pixels.  */
28     int rows, /* Leight in pixels.  */
29     int src_width, /* Length of source line, in bytes.  */
30     int dest_row_pitch, /* Bytes between adjacent horizontal output pixels.  */
31     int dest_col_pitch, /* Bytes between adjacent vertical output pixels.  */
32     int invalidate, /* nonzero to redraw the whole image.  */
33     drawfn fn,
34     void *opaque,
35     int *first_row, /* Input and output.  */
36     int *last_row /* Output only */)
37 {
38     target_phys_addr_t src_len;
39     uint8_t *dest;
40     uint8_t *src;
41     uint8_t *src_base;
42     int first, last = 0;
43     int dirty;
44     int i;
45     ram_addr_t addr;
46     ram_addr_t pd;
47     ram_addr_t pd2;
48
49     i = *first_row;
50     *first_row = -1;
51     src_len = src_width * rows;
52
53     if (kvm_enabled()) {
54         kvm_physical_sync_dirty_bitmap(base, src_len);
55     }
56     pd = cpu_get_physical_page_desc(base);
57     pd2 = cpu_get_physical_page_desc(base + src_len - 1);
58     /* We should reall check that this is a continuous ram region.
59        Instead we just check that the first and last pages are
60        both ram, and the right distance apart.  */
61     if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM
62         || (pd2 & ~TARGET_PAGE_MASK) > IO_MEM_ROM) {
63         return;
64     }
65     pd = (pd & TARGET_PAGE_MASK) + (base & ~TARGET_PAGE_MASK);
66     if (((pd + src_len - 1) & TARGET_PAGE_MASK) != (pd2 & TARGET_PAGE_MASK)) {
67         return;
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         return;
76     if (src_len != src_width * rows) {
77         cpu_physical_memory_unmap(src_base, src_len, 0, 0);
78         return;
79     }
80     src = src_base;
81     dest = ds_get_data(ds);
82     if (dest_col_pitch < 0)
83         dest -= dest_col_pitch * (cols - 1);
84     first = -1;
85     addr = pd;
86
87     addr += i * src_width;
88     src += i * src_width;
89     dest += i * dest_row_pitch;
90
91     for (; i < rows; i++) {
92         target_phys_addr_t dirty_offset;
93         dirty = 0;
94         dirty_offset = 0;
95         while (addr + dirty_offset < TARGET_PAGE_ALIGN(addr + src_width)) {
96             dirty |= cpu_physical_memory_get_dirty(addr + dirty_offset,
97                                                    VGA_DIRTY_FLAG);
98             dirty_offset += TARGET_PAGE_SIZE;
99         }
100
101         if (dirty || invalidate) {
102             fn(opaque, dest, src, cols, dest_col_pitch);
103             if (first == -1)
104                 first = i;
105             last = i;
106         }
107         addr += src_width;
108         src += src_width;
109         dest += dest_row_pitch;
110     }
111     cpu_physical_memory_unmap(src_base, src_len, 0, 0);
112     if (first < 0) {
113         return;
114     }
115     cpu_physical_memory_reset_dirty(pd, pd + src_len, VGA_DIRTY_FLAG);
116     *first_row = first;
117     *last_row = last;
118     return;
119 }