Rewrite the framebuffer update display function.
[qemu/mini2440.git] / hw / s3c24xx_template.h
blobb171c0383f0da54d59adf0326aa551d2aec5177a
1 /*
2 * Samsung S3C2410A LCD controller emulation.
4 * Copyright (c) 2007 OpenMoko, Inc.
5 * Author: Andrzej Zaborowski <andrew@openedhand.com>
7 * This code is licensed under the GNU GPL v2.
9 * Framebuffer format conversion routines.
12 # define SKIP_PIXEL(to) to += deststep
13 #if BITS == 8
14 # define COPY_PIXEL(to, from) *to = from; SKIP_PIXEL(to)
15 #elif BITS == 15 || BITS == 16
16 # define COPY_PIXEL(to, from) *(uint16_t *) to = from; SKIP_PIXEL(to)
17 #elif BITS == 24
18 # define COPY_PIXEL(to, from) \
19 *(uint16_t *) to = from; *(to + 2) = (from) >> 16; SKIP_PIXEL(to)
20 #elif BITS == 32
21 # define COPY_PIXEL(to, from) *(uint32_t *) to = from; SKIP_PIXEL(to)
22 #else
23 # error unknown bit depth
24 #endif
26 #ifdef WORDS_BIGENDIAN
27 # define SWAP_WORDS 1
28 #endif
30 #define FN_2(x) FN(x + 1) FN(x)
31 #define FN_4(x) FN_2(x + 2) FN_2(x)
32 #define FN_8(x) FN_4(x + 4) FN_4(x)
34 static void glue(s3c_draw_line1_, BITS)(void *opaque,
35 uint8_t *dest, const uint8_t *src, int width, int deststep)
37 uint32_t *palette = opaque;
38 uint32_t data;
39 while (width > 0) {
40 data = *(uint32_t *) src;
41 #define FN(x) COPY_PIXEL(dest, palette[(data >> (x)) & 1]);
42 #ifdef SWAP_WORDS
43 FN_8(24)
44 FN_8(16)
45 FN_8(8)
46 FN_8(0)
47 #else
48 FN_8(0)
49 FN_8(8)
50 FN_8(16)
51 FN_8(24)
52 #endif
53 #undef FN
54 width -= 32;
55 src += 4;
59 static void glue(s3c_draw_line2_, BITS)(void *opaque,
60 uint8_t *dest, const uint8_t *src, int width, int deststep)
62 uint32_t *palette = opaque;
63 uint32_t data;
64 while (width > 0) {
65 data = *(uint32_t *) src;
66 #define FN(x) COPY_PIXEL(dest, palette[(data >> ((x) * 2)) & 3]);
67 #ifdef SWAP_WORDS
68 FN_4(12)
69 FN_4(8)
70 FN_4(4)
71 FN_4(0)
72 #else
73 FN_4(0)
74 FN_4(4)
75 FN_4(8)
76 FN_4(12)
77 #endif
78 #undef FN
79 width -= 16;
80 src += 4;
84 static void glue(s3c_draw_line4_, BITS)(void *opaque,
85 uint8_t *dest, const uint8_t *src, int width, int deststep)
87 uint32_t *palette = opaque;
88 uint32_t data;
89 while (width > 0) {
90 data = *(uint32_t *) src;
91 #define FN(x) COPY_PIXEL(dest, palette[(data >> ((x) * 4)) & 0xf]);
92 #ifdef SWAP_WORDS
93 FN_2(6)
94 FN_2(4)
95 FN_2(2)
96 FN_2(0)
97 #else
98 FN_2(0)
99 FN_2(2)
100 FN_2(4)
101 FN_2(6)
102 #endif
103 #undef FN
104 width -= 8;
105 src += 4;
109 static void glue(s3c_draw_line8_, BITS)(void *opaque,
110 uint8_t *dest, const uint8_t *src, int width, int deststep)
112 uint32_t *palette = opaque;
113 uint32_t data;
114 while (width > 0) {
115 data = *(uint32_t *) src;
116 #define FN(x) COPY_PIXEL(dest, palette[(data >> (x)) & 0xff]);
117 #ifdef SWAP_WORDS
118 FN(24)
119 FN(16)
120 FN(8)
121 FN(0)
122 #else
123 FN(0)
124 FN(8)
125 FN(16)
126 FN(24)
127 #endif
128 #undef FN
129 width -= 4;
130 src += 4;
134 static void glue(s3c_draw_line16a_, BITS)(void *opaque,
135 uint8_t *dest, const uint8_t *src, int width, int deststep)
137 uint32_t data;
138 unsigned int r, g, b;
139 while (width > 0) {
140 data = *(uint32_t *) src;
141 #ifdef SWAP_WORDS
142 data = bswap32(data);
143 #endif
144 b = (data & 0x1f) << 3;
145 data >>= 5;
146 g = (data & 0x3f) << 2;
147 data >>= 6;
148 r = (data & 0x1f) << 3;
149 data >>= 5;
150 COPY_PIXEL(dest, glue(s3c_rgb_to_pixel, BITS)(r, g, b));
151 b = (data & 0x1f) << 3;
152 data >>= 5;
153 g = (data & 0x3f) << 2;
154 data >>= 6;
155 r = (data & 0x1f) << 3;
156 COPY_PIXEL(dest, glue(s3c_rgb_to_pixel, BITS)(r, g, b));
157 width -= 2;
158 src += 4;
162 static void glue(s3c_draw_line16b_, BITS)(void *opaque,
163 uint8_t *dest, const uint8_t *src, int width, int deststep)
165 uint32_t data;
166 unsigned int r, g, b;
167 while (width > 0) {
168 data = *(uint32_t *) src;
169 #ifdef SWAP_WORDS
170 data = bswap32(data);
171 #endif
172 b = (data & 0x1f) << 3;
173 data >>= 5;
174 g = (data & 0x1f) << 3;
175 data >>= 5;
176 r = (data & 0x3f) << 2;
177 data >>= 5;
178 COPY_PIXEL(dest, glue(s3c_rgb_to_pixel, BITS)(r, g, b));
179 b = (data & 0x1f) << 3;
180 data >>= 5;
181 g = (data & 0x1f) << 3;
182 data >>= 5;
183 r = (data & 0x3f) << 2;
184 COPY_PIXEL(dest, glue(s3c_rgb_to_pixel, BITS)(r, g, b));
185 width -= 2;
186 src += 4;
190 static void glue(s3c_draw_line12_, BITS)(void *opaque,
191 uint8_t *dest, const uint8_t *src, int width, int deststep)
193 uint32_t data;
194 unsigned int r, g, b;
195 while (width > 0) {
196 data = *(uint32_t *) src;
197 src += 3;
198 #ifdef SWAP_WORDS
199 data = bswap32(data);
200 #endif
201 /* XXX should use (x & 0xf) << 4) | (x & 0xf) for natural
202 * colours. Otherwise the image may be a bit darkened. */
203 b = (data & 0xf00) >> 4;
204 g = (data & 0xf0) << 0;
205 r = (data & 0xf) << 4;
206 data >>= 12;
207 COPY_PIXEL(dest, glue(s3c_rgb_to_pixel, BITS)(r, g, b));
208 b = (data & 0xf00) >> 4;
209 g = (data & 0xf0) << 0;
210 r = (data & 0xf) << 4;
211 data >>= 12;
212 COPY_PIXEL(dest, glue(s3c_rgb_to_pixel, BITS)(r, g, b));
213 b = (data & 0xf00) >> 4;
214 g = (data & 0xf0) << 0;
215 r = (data & 0xf) << 4;
216 data >>= 12;
217 COPY_PIXEL(dest, glue(s3c_rgb_to_pixel, BITS)(r, g, b));
218 b = (data & 0xf00) >> 4;
219 g = (data & 0xf0) << 0;
220 r = (data & 0xf) << 4;
221 data >>= 12;
222 COPY_PIXEL(dest, glue(s3c_rgb_to_pixel, BITS)(r, g, b));
223 width -= 4;
227 static void glue(s3c_draw_line24_, BITS)(void *opaque,
228 uint8_t *dest, const uint8_t *src, int width, int deststep)
230 uint32_t data;
231 unsigned int r, g, b;
232 while (width > 0) {
233 data = *(uint32_t *) src;
234 #ifdef SWAP_WORDS
235 data = bswap32(data);
236 #endif
237 b = data & 0xff;
238 data >>= 8;
239 g = data & 0xff;
240 data >>= 8;
241 r = data & 0xff;
242 COPY_PIXEL(dest, glue(s3c_rgb_to_pixel, BITS)(r, g, b));
243 width -= 1;
244 src += 4;
248 static drawfn glue(s3c_draw_fn_, BITS)[] =
250 glue(s3c_draw_line1_, BITS),
251 glue(s3c_draw_line2_, BITS),
252 glue(s3c_draw_line4_, BITS),
253 glue(s3c_draw_line8_, BITS),
254 glue(s3c_draw_line12_, BITS),
255 glue(s3c_draw_line16a_, BITS),
256 glue(s3c_draw_line16b_, BITS),
257 glue(s3c_draw_line24_, BITS),
260 #undef BITS
261 #undef COPY_PIXEL
262 #undef SKIP_PIXEL
264 #ifdef SWAP_WORDS
265 # undef SWAP_WORDS
266 #endif