VirtualBox

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

Last change on this file since 82088 was 76553, checked in by vboxsync, 5 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.5 KB
Line 
1/* $Id: DevVGATmpl.h 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * DevVGA - VBox VGA/VESA device, code templates.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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, vram_ofs;
266 int x;
267
268 vram_ofs = s - s1->vram_ptrR3;
269 palette = s1->last_palette;
270 plane_mask = mask16[s1->ar[0x12] & 0xf];
271 width >>= 3;
272 for(x = 0; x < width; x++) {
273 s = s1->vram_ptrR3 + (vram_ofs & s1->vga_addr_mask);
274 data = ((uint32_t *)s)[0];
275 data &= plane_mask;
276 v = expand4[GET_PLANE(data, 0)];
277 v |= expand4[GET_PLANE(data, 1)] << 1;
278 v |= expand4[GET_PLANE(data, 2)] << 2;
279 v |= expand4[GET_PLANE(data, 3)] << 3;
280 ((PIXEL_TYPE *)d)[0] = palette[v >> 28];
281 ((PIXEL_TYPE *)d)[1] = palette[(v >> 24) & 0xf];
282 ((PIXEL_TYPE *)d)[2] = palette[(v >> 20) & 0xf];
283 ((PIXEL_TYPE *)d)[3] = palette[(v >> 16) & 0xf];
284 ((PIXEL_TYPE *)d)[4] = palette[(v >> 12) & 0xf];
285 ((PIXEL_TYPE *)d)[5] = palette[(v >> 8) & 0xf];
286 ((PIXEL_TYPE *)d)[6] = palette[(v >> 4) & 0xf];
287 ((PIXEL_TYPE *)d)[7] = palette[(v >> 0) & 0xf];
288 d += BPP * 8;
289 vram_ofs += 4;
290 }
291}
292
293/*
294 * 16 color mode, dup2 horizontal
295 */
296static void RT_CONCAT(vga_draw_line4d2_, DEPTH)(VGAState *s1, uint8_t *d,
297 const uint8_t *s, int width)
298{
299 uint32_t plane_mask, data, v, *palette;
300 int x;
301
302 palette = s1->last_palette;
303 plane_mask = mask16[s1->ar[0x12] & 0xf];
304 width >>= 3;
305 for(x = 0; x < width; x++) {
306 data = ((uint32_t *)s)[0];
307 data &= plane_mask;
308 v = expand4[GET_PLANE(data, 0)];
309 v |= expand4[GET_PLANE(data, 1)] << 1;
310 v |= expand4[GET_PLANE(data, 2)] << 2;
311 v |= expand4[GET_PLANE(data, 3)] << 3;
312 PUT_PIXEL2(d, 0, palette[v >> 28]);
313 PUT_PIXEL2(d, 1, palette[(v >> 24) & 0xf]);
314 PUT_PIXEL2(d, 2, palette[(v >> 20) & 0xf]);
315 PUT_PIXEL2(d, 3, palette[(v >> 16) & 0xf]);
316 PUT_PIXEL2(d, 4, palette[(v >> 12) & 0xf]);
317 PUT_PIXEL2(d, 5, palette[(v >> 8) & 0xf]);
318 PUT_PIXEL2(d, 6, palette[(v >> 4) & 0xf]);
319 PUT_PIXEL2(d, 7, palette[(v >> 0) & 0xf]);
320 d += BPP * 16;
321 s += 4;
322 }
323}
324
325/*
326 * 256 color mode, double pixels
327 *
328 * XXX: add plane_mask support (never used in standard VGA modes)
329 */
330static void RT_CONCAT(vga_draw_line8d2_, DEPTH)(VGAState *s1, uint8_t *d,
331 const uint8_t *s, int width)
332{
333 uint32_t *palette;
334 int x;
335
336 palette = s1->last_palette;
337 width >>= 3;
338 for(x = 0; x < width; x++) {
339 PUT_PIXEL2(d, 0, palette[s[0]]);
340 PUT_PIXEL2(d, 1, palette[s[1]]);
341 PUT_PIXEL2(d, 2, palette[s[2]]);
342 PUT_PIXEL2(d, 3, palette[s[3]]);
343 d += BPP * 8;
344 s += 4;
345 }
346}
347
348/*
349 * standard 256 color mode
350 *
351 * XXX: add plane_mask support (never used in standard VGA modes)
352 */
353static void RT_CONCAT(vga_draw_line8_, DEPTH)(VGAState *s1, uint8_t *d,
354 const uint8_t *s, int width)
355{
356 uint32_t *palette;
357 int x;
358
359 palette = s1->last_palette;
360 width >>= 3;
361 for(x = 0; x < width; x++) {
362 ((PIXEL_TYPE *)d)[0] = palette[s[0]];
363 ((PIXEL_TYPE *)d)[1] = palette[s[1]];
364 ((PIXEL_TYPE *)d)[2] = palette[s[2]];
365 ((PIXEL_TYPE *)d)[3] = palette[s[3]];
366 ((PIXEL_TYPE *)d)[4] = palette[s[4]];
367 ((PIXEL_TYPE *)d)[5] = palette[s[5]];
368 ((PIXEL_TYPE *)d)[6] = palette[s[6]];
369 ((PIXEL_TYPE *)d)[7] = palette[s[7]];
370 d += BPP * 8;
371 s += 8;
372 }
373}
374
375#endif /* DEPTH != 15 */
376
377
378/* XXX: optimize */
379
380/*
381 * 15 bit color
382 */
383static void RT_CONCAT(vga_draw_line15_, DEPTH)(VGAState *s1, uint8_t *d,
384 const uint8_t *s, int width)
385{
386#if DEPTH == 15 && defined(WORDS_BIGENDIAN) == defined(TARGET_WORDS_BIGENDIAN)
387 memcpy(d, s, width * 2);
388#else
389 int w;
390 uint32_t v, r, g, b;
391
392 w = width;
393 do {
394 v = s[0] | (s[1] << 8);
395 r = (v >> 7) & 0xf8;
396 g = (v >> 2) & 0xf8;
397 b = (v << 3) & 0xf8;
398 ((PIXEL_TYPE *)d)[0] = RT_CONCAT(rgb_to_pixel, DEPTH)(r, g, b);
399 s += 2;
400 d += BPP;
401 } while (--w != 0);
402#endif
403 NOREF(s1);
404}
405
406/*
407 * 16 bit color
408 */
409static void RT_CONCAT(vga_draw_line16_, DEPTH)(VGAState *s1, uint8_t *d,
410 const uint8_t *s, int width)
411{
412#if DEPTH == 16 && defined(WORDS_BIGENDIAN) == defined(TARGET_WORDS_BIGENDIAN)
413 memcpy(d, s, width * 2);
414#else
415 int w;
416 uint32_t v, r, g, b;
417
418 w = width;
419 do {
420 v = s[0] | (s[1] << 8);
421 r = (v >> 8) & 0xf8;
422 g = (v >> 3) & 0xfc;
423 b = (v << 3) & 0xf8;
424 ((PIXEL_TYPE *)d)[0] = RT_CONCAT(rgb_to_pixel, DEPTH)(r, g, b);
425 s += 2;
426 d += BPP;
427 } while (--w != 0);
428#endif
429 NOREF(s1);
430}
431
432/*
433 * 24 bit color
434 */
435static void RT_CONCAT(vga_draw_line24_, DEPTH)(VGAState *s1, uint8_t *d,
436 const uint8_t *s, int width)
437{
438 int w;
439 uint32_t r, g, b;
440 NOREF(s1);
441
442 w = width;
443 do {
444#if defined(TARGET_WORDS_BIGENDIAN)
445 r = s[0];
446 g = s[1];
447 b = s[2];
448#else
449 b = s[0];
450 g = s[1];
451 r = s[2];
452#endif
453 ((PIXEL_TYPE *)d)[0] = RT_CONCAT(rgb_to_pixel, DEPTH)(r, g, b);
454 s += 3;
455 d += BPP;
456 } while (--w != 0);
457}
458
459/*
460 * 32 bit color
461 */
462static void RT_CONCAT(vga_draw_line32_, DEPTH)(VGAState *s1, uint8_t *d,
463 const uint8_t *s, int width)
464{
465#if DEPTH == 32 && defined(WORDS_BIGENDIAN) == defined(TARGET_WORDS_BIGENDIAN)
466 memcpy(d, s, width * 4);
467#else
468 int w;
469 uint32_t r, g, b;
470
471 w = width;
472 do {
473#if defined(TARGET_WORDS_BIGENDIAN)
474 r = s[1];
475 g = s[2];
476 b = s[3];
477#else
478 b = s[0];
479 g = s[1];
480 r = s[2];
481#endif
482 ((PIXEL_TYPE *)d)[0] = RT_CONCAT(rgb_to_pixel, DEPTH)(r, g, b);
483 s += 4;
484 d += BPP;
485 } while (--w != 0);
486#endif
487 NOREF(s1);
488}
489
490#if DEPTH != 15
491#ifndef VBOX
492#ifdef VBOX
493static
494#endif/* VBOX */
495void RT_CONCAT(vga_draw_cursor_line_, DEPTH)(uint8_t *d1,
496 const uint8_t *src1,
497 int poffset, int w,
498 unsigned int color0,
499 unsigned int color1,
500 unsigned int color_xor)
501{
502 const uint8_t *plane0, *plane1;
503 int x, b0, b1;
504 uint8_t *d;
505
506 d = d1;
507 plane0 = src1;
508 plane1 = src1 + poffset;
509 for(x = 0; x < w; x++) {
510 b0 = (plane0[x >> 3] >> (7 - (x & 7))) & 1;
511 b1 = (plane1[x >> 3] >> (7 - (x & 7))) & 1;
512#if DEPTH == 8
513 switch(b0 | (b1 << 1)) {
514 case 0:
515 break;
516 case 1:
517 d[0] ^= color_xor;
518 break;
519 case 2:
520 d[0] = color0;
521 break;
522 case 3:
523 d[0] = color1;
524 break;
525 }
526#elif DEPTH == 16
527 switch(b0 | (b1 << 1)) {
528 case 0:
529 break;
530 case 1:
531 ((uint16_t *)d)[0] ^= color_xor;
532 break;
533 case 2:
534 ((uint16_t *)d)[0] = color0;
535 break;
536 case 3:
537 ((uint16_t *)d)[0] = color1;
538 break;
539 }
540#elif DEPTH == 32
541 switch(b0 | (b1 << 1)) {
542 case 0:
543 break;
544 case 1:
545 ((uint32_t *)d)[0] ^= color_xor;
546 break;
547 case 2:
548 ((uint32_t *)d)[0] = color0;
549 break;
550 case 3:
551 ((uint32_t *)d)[0] = color1;
552 break;
553 }
554#else
555#error unsupported depth
556#endif
557 d += BPP;
558 }
559}
560#endif /* !VBOX */
561#endif
562
563#undef PUT_PIXEL2
564#undef DEPTH
565#undef BPP
566#undef PIXEL_TYPE
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use