VirtualBox

source: vbox/trunk/src/VBox/Devices/Graphics/DevVGATmpl.h@ 55747

Last change on this file since 55747 was 49887, checked in by vboxsync, 11 years ago

Devices/Graphics/DevVGA: eliminate use of vl_vbox.h

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.4 KB
Line 
1/* $Id: DevVGATmpl.h 49887 2013-12-12 15:10:49Z vboxsync $ */
2/** @file
3 * DevVGA - VBox VGA/VESA device, code templates.
4 */
5
6/*
7 * Copyright (C) 2006-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 * --------------------------------------------------------------------
17 *
18 * This code is based on:
19 *
20 * QEMU VGA Emulator templates
21 *
22 * Copyright (c) 2003 Fabrice Bellard
23 *
24 * Permission is hereby granted, free of charge, to any person obtaining a copy
25 * of this software and associated documentation files (the "Software"), to deal
26 * in the Software without restriction, including without limitation the rights
27 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28 * copies of the Software, and to permit persons to whom the Software is
29 * furnished to do so, subject to the following conditions:
30 *
31 * The above copyright notice and this permission notice shall be included in
32 * all copies or substantial portions of the Software.
33 *
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
37 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
40 * THE SOFTWARE.
41 */
42
43#if DEPTH == 8
44#define BPP 1
45#define PIXEL_TYPE uint8_t
46#elif DEPTH == 15 || DEPTH == 16
47#define BPP 2
48#define PIXEL_TYPE uint16_t
49#elif DEPTH == 32
50#define BPP 4
51#define PIXEL_TYPE uint32_t
52#else
53#error unsupport depth
54#endif
55
56#if DEPTH != 15
57
58static inline void RT_CONCAT(vga_draw_glyph_line_, DEPTH)(uint8_t *d,
59 int font_data,
60 uint32_t xorcol,
61 uint32_t bgcol,
62 int dscan,
63 int linesize)
64{
65#if BPP == 1
66 ((uint32_t *)d)[0] = (dmask16[(font_data >> 4)] & xorcol) ^ bgcol;
67 ((uint32_t *)d)[1] = (dmask16[(font_data >> 0) & 0xf] & xorcol) ^ bgcol;
68 if (dscan) {
69 uint8_t *c = d + linesize;
70 ((uint32_t *)c)[0] = ((uint32_t *)d)[0];
71 ((uint32_t *)c)[1] = ((uint32_t *)d)[1];
72 }
73#elif BPP == 2
74 ((uint32_t *)d)[0] = (dmask4[(font_data >> 6)] & xorcol) ^ bgcol;
75 ((uint32_t *)d)[1] = (dmask4[(font_data >> 4) & 3] & xorcol) ^ bgcol;
76 ((uint32_t *)d)[2] = (dmask4[(font_data >> 2) & 3] & xorcol) ^ bgcol;
77 ((uint32_t *)d)[3] = (dmask4[(font_data >> 0) & 3] & xorcol) ^ bgcol;
78 if (dscan)
79 memcpy(d + linesize, d, 4 * sizeof(uint32_t));
80#else
81 ((uint32_t *)d)[0] = (-((font_data >> 7)) & xorcol) ^ bgcol;
82 ((uint32_t *)d)[1] = (-((font_data >> 6) & 1) & xorcol) ^ bgcol;
83 ((uint32_t *)d)[2] = (-((font_data >> 5) & 1) & xorcol) ^ bgcol;
84 ((uint32_t *)d)[3] = (-((font_data >> 4) & 1) & xorcol) ^ bgcol;
85 ((uint32_t *)d)[4] = (-((font_data >> 3) & 1) & xorcol) ^ bgcol;
86 ((uint32_t *)d)[5] = (-((font_data >> 2) & 1) & xorcol) ^ bgcol;
87 ((uint32_t *)d)[6] = (-((font_data >> 1) & 1) & xorcol) ^ bgcol;
88 ((uint32_t *)d)[7] = (-((font_data >> 0) & 1) & xorcol) ^ bgcol;
89 if (dscan)
90 memcpy(d + linesize, d, 8 * sizeof(uint32_t));
91#endif
92}
93
94static void RT_CONCAT(vga_draw_glyph8_, DEPTH)(uint8_t *d, int linesize,
95 const uint8_t *font_ptr, int h,
96 uint32_t fgcol, uint32_t bgcol, int dscan)
97{
98 uint32_t xorcol;
99 int font_data;
100
101 xorcol = bgcol ^ fgcol;
102 do {
103 font_data = font_ptr[0];
104 RT_CONCAT(vga_draw_glyph_line_, DEPTH)(d, font_data, xorcol, bgcol, dscan, linesize);
105 font_ptr += 4;
106 d += linesize << dscan;
107 } while (--h);
108}
109
110static void RT_CONCAT(vga_draw_glyph16_, DEPTH)(uint8_t *d, int linesize,
111 const uint8_t *font_ptr, int h,
112 uint32_t fgcol, uint32_t bgcol, int dscan)
113{
114 uint32_t xorcol;
115 int font_data;
116
117 xorcol = bgcol ^ fgcol;
118 do {
119 font_data = font_ptr[0];
120 RT_CONCAT(vga_draw_glyph_line_, DEPTH)(d,
121 expand4to8[font_data >> 4],
122 xorcol, bgcol, dscan, linesize);
123 RT_CONCAT(vga_draw_glyph_line_, DEPTH)(d + 8 * BPP,
124 expand4to8[font_data & 0x0f],
125 xorcol, bgcol, dscan, linesize);
126 font_ptr += 4;
127 d += linesize << dscan;
128 } while (--h);
129}
130
131static void RT_CONCAT(vga_draw_glyph9_, DEPTH)(uint8_t *d, int linesize,
132 const uint8_t *font_ptr, int h,
133 uint32_t fgcol, uint32_t bgcol, int dup9)
134{
135 uint32_t xorcol, v;
136 int font_data;
137
138 xorcol = bgcol ^ fgcol;
139 do {
140 font_data = font_ptr[0];
141#if BPP == 1
142 ((uint32_t *)d)[0] = RT_H2LE_U32((dmask16[(font_data >> 4)] & xorcol) ^ bgcol);
143 v = (dmask16[(font_data >> 0) & 0xf] & xorcol) ^ bgcol;
144 ((uint32_t *)d)[1] = RT_H2LE_U32(v);
145 if (dup9)
146 ((uint8_t *)d)[8] = v >> (24 * (1 - BIG));
147 else
148 ((uint8_t *)d)[8] = bgcol;
149
150#elif BPP == 2
151 ((uint32_t *)d)[0] = RT_H2LE_U32((dmask4[(font_data >> 6)] & xorcol) ^ bgcol);
152 ((uint32_t *)d)[1] = RT_H2LE_U32((dmask4[(font_data >> 4) & 3] & xorcol) ^ bgcol);
153 ((uint32_t *)d)[2] = RT_H2LE_U32((dmask4[(font_data >> 2) & 3] & xorcol) ^ bgcol);
154 v = (dmask4[(font_data >> 0) & 3] & xorcol) ^ bgcol;
155 ((uint32_t *)d)[3] = RT_H2LE_U32(v);
156 if (dup9)
157 ((uint16_t *)d)[8] = v >> (16 * (1 - BIG));
158 else
159 ((uint16_t *)d)[8] = bgcol;
160#else
161 ((uint32_t *)d)[0] = (-((font_data >> 7)) & xorcol) ^ bgcol;
162 ((uint32_t *)d)[1] = (-((font_data >> 6) & 1) & xorcol) ^ bgcol;
163 ((uint32_t *)d)[2] = (-((font_data >> 5) & 1) & xorcol) ^ bgcol;
164 ((uint32_t *)d)[3] = (-((font_data >> 4) & 1) & xorcol) ^ bgcol;
165 ((uint32_t *)d)[4] = (-((font_data >> 3) & 1) & xorcol) ^ bgcol;
166 ((uint32_t *)d)[5] = (-((font_data >> 2) & 1) & xorcol) ^ bgcol;
167 ((uint32_t *)d)[6] = (-((font_data >> 1) & 1) & xorcol) ^ bgcol;
168 v = (-((font_data >> 0) & 1) & xorcol) ^ bgcol;
169 ((uint32_t *)d)[7] = v;
170 if (dup9)
171 ((uint32_t *)d)[8] = v;
172 else
173 ((uint32_t *)d)[8] = bgcol;
174#endif
175 font_ptr += 4;
176 d += linesize;
177 } while (--h);
178}
179
180/*
181 * 4 color mode
182 */
183static void RT_CONCAT(vga_draw_line2_, DEPTH)(VGAState *s1, uint8_t *d,
184 const uint8_t *s, int width)
185{
186 uint32_t plane_mask, *palette, data, v, src_inc, dwb_mode;
187 int x;
188
189 palette = s1->last_palette;
190 plane_mask = mask16[s1->ar[0x12] & 0xf];
191 dwb_mode = (s1->cr[0x14] & 0x40) ? 2 : (s1->cr[0x17] & 0x40) ? 0 : 1;
192 src_inc = 4 << dwb_mode;
193 width >>= 3;
194 for(x = 0; x < width; x++) {
195 data = ((uint32_t *)s)[0];
196 data &= plane_mask;
197 v = expand2[GET_PLANE(data, 0)];
198 v |= expand2[GET_PLANE(data, 2)] << 2;
199 ((PIXEL_TYPE *)d)[0] = palette[v >> 12];
200 ((PIXEL_TYPE *)d)[1] = palette[(v >> 8) & 0xf];
201 ((PIXEL_TYPE *)d)[2] = palette[(v >> 4) & 0xf];
202 ((PIXEL_TYPE *)d)[3] = palette[(v >> 0) & 0xf];
203
204 v = expand2[GET_PLANE(data, 1)];
205 v |= expand2[GET_PLANE(data, 3)] << 2;
206 ((PIXEL_TYPE *)d)[4] = palette[v >> 12];
207 ((PIXEL_TYPE *)d)[5] = palette[(v >> 8) & 0xf];
208 ((PIXEL_TYPE *)d)[6] = palette[(v >> 4) & 0xf];
209 ((PIXEL_TYPE *)d)[7] = palette[(v >> 0) & 0xf];
210 d += BPP * 8;
211 s += src_inc;
212 }
213}
214
215#if BPP == 1
216#define PUT_PIXEL2(d, n, v) ((uint16_t *)d)[(n)] = (v)
217#elif BPP == 2
218#define PUT_PIXEL2(d, n, v) ((uint32_t *)d)[(n)] = (v)
219#else
220#define PUT_PIXEL2(d, n, v) \
221((uint32_t *)d)[2*(n)] = ((uint32_t *)d)[2*(n)+1] = (v)
222#endif
223
224/*
225 * 4 color mode, dup2 horizontal
226 */
227static void RT_CONCAT(vga_draw_line2d2_, DEPTH)(VGAState *s1, uint8_t *d,
228 const uint8_t *s, int width)
229{
230 uint32_t plane_mask, *palette, data, v, src_inc, dwb_mode;
231 int x;
232
233 palette = s1->last_palette;
234 plane_mask = mask16[s1->ar[0x12] & 0xf];
235 dwb_mode = (s1->cr[0x14] & 0x40) ? 2 : (s1->cr[0x17] & 0x40) ? 0 : 1;
236 src_inc = 4 << dwb_mode;
237 width >>= 3;
238 for(x = 0; x < width; x++) {
239 data = ((uint32_t *)s)[0];
240 data &= plane_mask;
241 v = expand2[GET_PLANE(data, 0)];
242 v |= expand2[GET_PLANE(data, 2)] << 2;
243 PUT_PIXEL2(d, 0, palette[v >> 12]);
244 PUT_PIXEL2(d, 1, palette[(v >> 8) & 0xf]);
245 PUT_PIXEL2(d, 2, palette[(v >> 4) & 0xf]);
246 PUT_PIXEL2(d, 3, palette[(v >> 0) & 0xf]);
247
248 v = expand2[GET_PLANE(data, 1)];
249 v |= expand2[GET_PLANE(data, 3)] << 2;
250 PUT_PIXEL2(d, 4, palette[v >> 12]);
251 PUT_PIXEL2(d, 5, palette[(v >> 8) & 0xf]);
252 PUT_PIXEL2(d, 6, palette[(v >> 4) & 0xf]);
253 PUT_PIXEL2(d, 7, palette[(v >> 0) & 0xf]);
254 d += BPP * 16;
255 s += src_inc;
256 }
257}
258
259/*
260 * 16 color mode
261 */
262static void RT_CONCAT(vga_draw_line4_, DEPTH)(VGAState *s1, uint8_t *d,
263 const uint8_t *s, int width)
264{
265 uint32_t plane_mask, data, v, *palette;
266 int x;
267
268 palette = s1->last_palette;
269 plane_mask = mask16[s1->ar[0x12] & 0xf];
270 width >>= 3;
271 for(x = 0; x < width; x++) {
272 data = ((uint32_t *)s)[0];
273 data &= plane_mask;
274 v = expand4[GET_PLANE(data, 0)];
275 v |= expand4[GET_PLANE(data, 1)] << 1;
276 v |= expand4[GET_PLANE(data, 2)] << 2;
277 v |= expand4[GET_PLANE(data, 3)] << 3;
278 ((PIXEL_TYPE *)d)[0] = palette[v >> 28];
279 ((PIXEL_TYPE *)d)[1] = palette[(v >> 24) & 0xf];
280 ((PIXEL_TYPE *)d)[2] = palette[(v >> 20) & 0xf];
281 ((PIXEL_TYPE *)d)[3] = palette[(v >> 16) & 0xf];
282 ((PIXEL_TYPE *)d)[4] = palette[(v >> 12) & 0xf];
283 ((PIXEL_TYPE *)d)[5] = palette[(v >> 8) & 0xf];
284 ((PIXEL_TYPE *)d)[6] = palette[(v >> 4) & 0xf];
285 ((PIXEL_TYPE *)d)[7] = palette[(v >> 0) & 0xf];
286 d += BPP * 8;
287 s += 4;
288 }
289}
290
291/*
292 * 16 color mode, dup2 horizontal
293 */
294static void RT_CONCAT(vga_draw_line4d2_, DEPTH)(VGAState *s1, uint8_t *d,
295 const uint8_t *s, int width)
296{
297 uint32_t plane_mask, data, v, *palette;
298 int x;
299
300 palette = s1->last_palette;
301 plane_mask = mask16[s1->ar[0x12] & 0xf];
302 width >>= 3;
303 for(x = 0; x < width; x++) {
304 data = ((uint32_t *)s)[0];
305 data &= plane_mask;
306 v = expand4[GET_PLANE(data, 0)];
307 v |= expand4[GET_PLANE(data, 1)] << 1;
308 v |= expand4[GET_PLANE(data, 2)] << 2;
309 v |= expand4[GET_PLANE(data, 3)] << 3;
310 PUT_PIXEL2(d, 0, palette[v >> 28]);
311 PUT_PIXEL2(d, 1, palette[(v >> 24) & 0xf]);
312 PUT_PIXEL2(d, 2, palette[(v >> 20) & 0xf]);
313 PUT_PIXEL2(d, 3, palette[(v >> 16) & 0xf]);
314 PUT_PIXEL2(d, 4, palette[(v >> 12) & 0xf]);
315 PUT_PIXEL2(d, 5, palette[(v >> 8) & 0xf]);
316 PUT_PIXEL2(d, 6, palette[(v >> 4) & 0xf]);
317 PUT_PIXEL2(d, 7, palette[(v >> 0) & 0xf]);
318 d += BPP * 16;
319 s += 4;
320 }
321}
322
323/*
324 * 256 color mode, double pixels
325 *
326 * XXX: add plane_mask support (never used in standard VGA modes)
327 */
328static void RT_CONCAT(vga_draw_line8d2_, DEPTH)(VGAState *s1, uint8_t *d,
329 const uint8_t *s, int width)
330{
331 uint32_t *palette;
332 int x;
333
334 palette = s1->last_palette;
335 width >>= 3;
336 for(x = 0; x < width; x++) {
337 PUT_PIXEL2(d, 0, palette[s[0]]);
338 PUT_PIXEL2(d, 1, palette[s[1]]);
339 PUT_PIXEL2(d, 2, palette[s[2]]);
340 PUT_PIXEL2(d, 3, palette[s[3]]);
341 d += BPP * 8;
342 s += 4;
343 }
344}
345
346/*
347 * standard 256 color mode
348 *
349 * XXX: add plane_mask support (never used in standard VGA modes)
350 */
351static void RT_CONCAT(vga_draw_line8_, DEPTH)(VGAState *s1, uint8_t *d,
352 const uint8_t *s, int width)
353{
354 uint32_t *palette;
355 int x;
356
357 palette = s1->last_palette;
358 width >>= 3;
359 for(x = 0; x < width; x++) {
360 ((PIXEL_TYPE *)d)[0] = palette[s[0]];
361 ((PIXEL_TYPE *)d)[1] = palette[s[1]];
362 ((PIXEL_TYPE *)d)[2] = palette[s[2]];
363 ((PIXEL_TYPE *)d)[3] = palette[s[3]];
364 ((PIXEL_TYPE *)d)[4] = palette[s[4]];
365 ((PIXEL_TYPE *)d)[5] = palette[s[5]];
366 ((PIXEL_TYPE *)d)[6] = palette[s[6]];
367 ((PIXEL_TYPE *)d)[7] = palette[s[7]];
368 d += BPP * 8;
369 s += 8;
370 }
371}
372
373#endif /* DEPTH != 15 */
374
375
376/* XXX: optimize */
377
378/*
379 * 15 bit color
380 */
381static void RT_CONCAT(vga_draw_line15_, DEPTH)(VGAState *s1, uint8_t *d,
382 const uint8_t *s, int width)
383{
384#if DEPTH == 15 && defined(WORDS_BIGENDIAN) == defined(TARGET_WORDS_BIGENDIAN)
385 memcpy(d, s, width * 2);
386#else
387 int w;
388 uint32_t v, r, g, b;
389
390 w = width;
391 do {
392 v = s[0] | (s[1] << 8);
393 r = (v >> 7) & 0xf8;
394 g = (v >> 2) & 0xf8;
395 b = (v << 3) & 0xf8;
396 ((PIXEL_TYPE *)d)[0] = RT_CONCAT(rgb_to_pixel, DEPTH)(r, g, b);
397 s += 2;
398 d += BPP;
399 } while (--w != 0);
400#endif
401 NOREF(s1);
402}
403
404/*
405 * 16 bit color
406 */
407static void RT_CONCAT(vga_draw_line16_, DEPTH)(VGAState *s1, uint8_t *d,
408 const uint8_t *s, int width)
409{
410#if DEPTH == 16 && defined(WORDS_BIGENDIAN) == defined(TARGET_WORDS_BIGENDIAN)
411 memcpy(d, s, width * 2);
412#else
413 int w;
414 uint32_t v, r, g, b;
415
416 w = width;
417 do {
418 v = s[0] | (s[1] << 8);
419 r = (v >> 8) & 0xf8;
420 g = (v >> 3) & 0xfc;
421 b = (v << 3) & 0xf8;
422 ((PIXEL_TYPE *)d)[0] = RT_CONCAT(rgb_to_pixel, DEPTH)(r, g, b);
423 s += 2;
424 d += BPP;
425 } while (--w != 0);
426#endif
427 NOREF(s1);
428}
429
430/*
431 * 24 bit color
432 */
433static void RT_CONCAT(vga_draw_line24_, DEPTH)(VGAState *s1, uint8_t *d,
434 const uint8_t *s, int width)
435{
436 int w;
437 uint32_t r, g, b;
438 NOREF(s1);
439
440 w = width;
441 do {
442#if defined(TARGET_WORDS_BIGENDIAN)
443 r = s[0];
444 g = s[1];
445 b = s[2];
446#else
447 b = s[0];
448 g = s[1];
449 r = s[2];
450#endif
451 ((PIXEL_TYPE *)d)[0] = RT_CONCAT(rgb_to_pixel, DEPTH)(r, g, b);
452 s += 3;
453 d += BPP;
454 } while (--w != 0);
455}
456
457/*
458 * 32 bit color
459 */
460static void RT_CONCAT(vga_draw_line32_, DEPTH)(VGAState *s1, uint8_t *d,
461 const uint8_t *s, int width)
462{
463#if DEPTH == 32 && defined(WORDS_BIGENDIAN) == defined(TARGET_WORDS_BIGENDIAN)
464 memcpy(d, s, width * 4);
465#else
466 int w;
467 uint32_t r, g, b;
468
469 w = width;
470 do {
471#if defined(TARGET_WORDS_BIGENDIAN)
472 r = s[1];
473 g = s[2];
474 b = s[3];
475#else
476 b = s[0];
477 g = s[1];
478 r = s[2];
479#endif
480 ((PIXEL_TYPE *)d)[0] = RT_CONCAT(rgb_to_pixel, DEPTH)(r, g, b);
481 s += 4;
482 d += BPP;
483 } while (--w != 0);
484#endif
485 NOREF(s1);
486}
487
488#if DEPTH != 15
489#ifndef VBOX
490#ifdef VBOX
491static
492#endif/* VBOX */
493void RT_CONCAT(vga_draw_cursor_line_, DEPTH)(uint8_t *d1,
494 const uint8_t *src1,
495 int poffset, int w,
496 unsigned int color0,
497 unsigned int color1,
498 unsigned int color_xor)
499{
500 const uint8_t *plane0, *plane1;
501 int x, b0, b1;
502 uint8_t *d;
503
504 d = d1;
505 plane0 = src1;
506 plane1 = src1 + poffset;
507 for(x = 0; x < w; x++) {
508 b0 = (plane0[x >> 3] >> (7 - (x & 7))) & 1;
509 b1 = (plane1[x >> 3] >> (7 - (x & 7))) & 1;
510#if DEPTH == 8
511 switch(b0 | (b1 << 1)) {
512 case 0:
513 break;
514 case 1:
515 d[0] ^= color_xor;
516 break;
517 case 2:
518 d[0] = color0;
519 break;
520 case 3:
521 d[0] = color1;
522 break;
523 }
524#elif DEPTH == 16
525 switch(b0 | (b1 << 1)) {
526 case 0:
527 break;
528 case 1:
529 ((uint16_t *)d)[0] ^= color_xor;
530 break;
531 case 2:
532 ((uint16_t *)d)[0] = color0;
533 break;
534 case 3:
535 ((uint16_t *)d)[0] = color1;
536 break;
537 }
538#elif DEPTH == 32
539 switch(b0 | (b1 << 1)) {
540 case 0:
541 break;
542 case 1:
543 ((uint32_t *)d)[0] ^= color_xor;
544 break;
545 case 2:
546 ((uint32_t *)d)[0] = color0;
547 break;
548 case 3:
549 ((uint32_t *)d)[0] = color1;
550 break;
551 }
552#else
553#error unsupported depth
554#endif
555 d += BPP;
556 }
557}
558#endif /* !VBOX */
559#endif
560
561#undef PUT_PIXEL2
562#undef DEPTH
563#undef BPP
564#undef PIXEL_TYPE
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette