Gerd Hoffmann | daa8e5a | 2012-11-13 09:38:06 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 3 | * See the COPYING file in the top-level directory. |
| 4 | */ |
| 5 | |
Peter Maydell | 0b08786 | 2013-01-21 12:03:47 +0000 | [diff] [blame] | 6 | #include "qemu-common.h" |
| 7 | #include "ui/console.h" |
Gerd Hoffmann | d2ec7e2 | 2012-09-25 16:23:24 +0200 | [diff] [blame] | 8 | |
Gerd Hoffmann | a93a3af | 2014-06-18 09:00:00 +0200 | [diff] [blame^] | 9 | PixelFormat qemu_pixelformat_from_pixman(pixman_format_code_t format) |
| 10 | { |
| 11 | PixelFormat pf; |
| 12 | uint8_t bpp; |
| 13 | |
| 14 | bpp = pf.bits_per_pixel = PIXMAN_FORMAT_BPP(format); |
| 15 | pf.bytes_per_pixel = PIXMAN_FORMAT_BPP(format) / 8; |
| 16 | pf.depth = PIXMAN_FORMAT_DEPTH(format); |
| 17 | |
| 18 | pf.abits = PIXMAN_FORMAT_A(format); |
| 19 | pf.rbits = PIXMAN_FORMAT_R(format); |
| 20 | pf.gbits = PIXMAN_FORMAT_G(format); |
| 21 | pf.bbits = PIXMAN_FORMAT_B(format); |
| 22 | |
| 23 | switch (PIXMAN_FORMAT_TYPE(format)) { |
| 24 | case PIXMAN_TYPE_ARGB: |
| 25 | pf.ashift = pf.bbits + pf.gbits + pf.rbits; |
| 26 | pf.rshift = pf.bbits + pf.gbits; |
| 27 | pf.gshift = pf.bbits; |
| 28 | pf.bshift = 0; |
| 29 | break; |
| 30 | case PIXMAN_TYPE_ABGR: |
| 31 | pf.ashift = pf.rbits + pf.gbits + pf.bbits; |
| 32 | pf.bshift = pf.rbits + pf.gbits; |
| 33 | pf.gshift = pf.rbits; |
| 34 | pf.rshift = 0; |
| 35 | break; |
| 36 | case PIXMAN_TYPE_BGRA: |
| 37 | pf.bshift = bpp - pf.bbits; |
| 38 | pf.gshift = bpp - (pf.bbits + pf.gbits); |
| 39 | pf.rshift = bpp - (pf.bbits + pf.gbits + pf.rbits); |
| 40 | pf.ashift = 0; |
| 41 | break; |
| 42 | case PIXMAN_TYPE_RGBA: |
| 43 | pf.rshift = bpp - pf.rbits; |
| 44 | pf.gshift = bpp - (pf.rbits + pf.gbits); |
| 45 | pf.bshift = bpp - (pf.rbits + pf.gbits + pf.bbits); |
| 46 | pf.ashift = 0; |
| 47 | break; |
| 48 | default: |
| 49 | g_assert_not_reached(); |
| 50 | break; |
| 51 | } |
| 52 | |
| 53 | pf.amax = (1 << pf.abits) - 1; |
| 54 | pf.rmax = (1 << pf.rbits) - 1; |
| 55 | pf.gmax = (1 << pf.gbits) - 1; |
| 56 | pf.bmax = (1 << pf.bbits) - 1; |
| 57 | pf.amask = pf.amax << pf.ashift; |
| 58 | pf.rmask = pf.rmax << pf.rshift; |
| 59 | pf.gmask = pf.gmax << pf.gshift; |
| 60 | pf.bmask = pf.bmax << pf.bshift; |
| 61 | |
| 62 | return pf; |
| 63 | } |
| 64 | |
Gerd Hoffmann | d2ec7e2 | 2012-09-25 16:23:24 +0200 | [diff] [blame] | 65 | int qemu_pixman_get_type(int rshift, int gshift, int bshift) |
| 66 | { |
| 67 | int type = PIXMAN_TYPE_OTHER; |
| 68 | |
| 69 | if (rshift > gshift && gshift > bshift) { |
| 70 | if (bshift == 0) { |
| 71 | type = PIXMAN_TYPE_ARGB; |
| 72 | } else { |
| 73 | #if PIXMAN_VERSION >= PIXMAN_VERSION_ENCODE(0, 21, 8) |
| 74 | type = PIXMAN_TYPE_RGBA; |
| 75 | #endif |
| 76 | } |
| 77 | } else if (rshift < gshift && gshift < bshift) { |
| 78 | if (rshift == 0) { |
| 79 | type = PIXMAN_TYPE_ABGR; |
| 80 | } else { |
Gerd Hoffmann | fbddfc7 | 2012-12-14 08:54:21 +0100 | [diff] [blame] | 81 | #if PIXMAN_VERSION >= PIXMAN_VERSION_ENCODE(0, 16, 0) |
Gerd Hoffmann | d2ec7e2 | 2012-09-25 16:23:24 +0200 | [diff] [blame] | 82 | type = PIXMAN_TYPE_BGRA; |
Alexander Graf | 6e72719 | 2012-11-26 19:49:58 +0100 | [diff] [blame] | 83 | #endif |
Gerd Hoffmann | d2ec7e2 | 2012-09-25 16:23:24 +0200 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | return type; |
| 87 | } |
| 88 | |
| 89 | pixman_format_code_t qemu_pixman_get_format(PixelFormat *pf) |
| 90 | { |
| 91 | pixman_format_code_t format; |
| 92 | int type; |
| 93 | |
| 94 | type = qemu_pixman_get_type(pf->rshift, pf->gshift, pf->bshift); |
| 95 | format = PIXMAN_FORMAT(pf->bits_per_pixel, type, |
| 96 | pf->abits, pf->rbits, pf->gbits, pf->bbits); |
| 97 | if (!pixman_format_supported_source(format)) { |
| 98 | return 0; |
| 99 | } |
| 100 | return format; |
| 101 | } |
| 102 | |
| 103 | pixman_image_t *qemu_pixman_linebuf_create(pixman_format_code_t format, |
| 104 | int width) |
| 105 | { |
| 106 | pixman_image_t *image = pixman_image_create_bits(format, width, 1, NULL, 0); |
| 107 | assert(image != NULL); |
| 108 | return image; |
| 109 | } |
| 110 | |
| 111 | void qemu_pixman_linebuf_fill(pixman_image_t *linebuf, pixman_image_t *fb, |
Gerd Hoffmann | bc210eb | 2012-12-14 07:54:24 +0000 | [diff] [blame] | 112 | int width, int x, int y) |
Gerd Hoffmann | d2ec7e2 | 2012-09-25 16:23:24 +0200 | [diff] [blame] | 113 | { |
| 114 | pixman_image_composite(PIXMAN_OP_SRC, fb, NULL, linebuf, |
Gerd Hoffmann | bc210eb | 2012-12-14 07:54:24 +0000 | [diff] [blame] | 115 | x, y, 0, 0, 0, 0, width, 1); |
Gerd Hoffmann | d2ec7e2 | 2012-09-25 16:23:24 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Gerd Hoffmann | d9a8656 | 2012-11-02 09:12:49 +0100 | [diff] [blame] | 118 | pixman_image_t *qemu_pixman_mirror_create(pixman_format_code_t format, |
| 119 | pixman_image_t *image) |
| 120 | { |
| 121 | pixman_image_t *mirror; |
| 122 | |
| 123 | mirror = pixman_image_create_bits(format, |
| 124 | pixman_image_get_width(image), |
| 125 | pixman_image_get_height(image), |
| 126 | NULL, |
| 127 | pixman_image_get_stride(image)); |
| 128 | return mirror; |
| 129 | } |
| 130 | |
Gerd Hoffmann | d2ec7e2 | 2012-09-25 16:23:24 +0200 | [diff] [blame] | 131 | void qemu_pixman_image_unref(pixman_image_t *image) |
| 132 | { |
| 133 | if (image == NULL) { |
| 134 | return; |
| 135 | } |
| 136 | pixman_image_unref(image); |
| 137 | } |
Gerd Hoffmann | 867c538 | 2013-03-06 14:14:17 +0100 | [diff] [blame] | 138 | |
| 139 | pixman_color_t qemu_pixman_color(PixelFormat *pf, uint32_t color) |
| 140 | { |
| 141 | pixman_color_t c; |
| 142 | |
| 143 | c.red = ((color & pf->rmask) >> pf->rshift) << (16 - pf->rbits); |
| 144 | c.green = ((color & pf->gmask) >> pf->gshift) << (16 - pf->gbits); |
| 145 | c.blue = ((color & pf->bmask) >> pf->bshift) << (16 - pf->bbits); |
| 146 | c.alpha = ((color & pf->amask) >> pf->ashift) << (16 - pf->abits); |
| 147 | return c; |
| 148 | } |
Gerd Hoffmann | b762795 | 2013-03-07 15:23:48 +0100 | [diff] [blame] | 149 | |
| 150 | pixman_image_t *qemu_pixman_glyph_from_vgafont(int height, const uint8_t *font, |
| 151 | unsigned int ch) |
| 152 | { |
| 153 | pixman_image_t *glyph; |
| 154 | uint8_t *data; |
| 155 | bool bit; |
| 156 | int x, y; |
| 157 | |
| 158 | glyph = pixman_image_create_bits(PIXMAN_a8, 8, height, |
| 159 | NULL, 0); |
| 160 | data = (uint8_t *)pixman_image_get_data(glyph); |
| 161 | |
| 162 | font += height * ch; |
| 163 | for (y = 0; y < height; y++, font++) { |
| 164 | for (x = 0; x < 8; x++, data++) { |
| 165 | bit = (*font) & (1 << (7-x)); |
| 166 | *data = bit ? 0xff : 0x00; |
| 167 | } |
| 168 | } |
| 169 | return glyph; |
| 170 | } |
| 171 | |
| 172 | void qemu_pixman_glyph_render(pixman_image_t *glyph, |
| 173 | pixman_image_t *surface, |
| 174 | pixman_color_t *fgcol, |
| 175 | pixman_color_t *bgcol, |
| 176 | int x, int y, int cw, int ch) |
| 177 | { |
| 178 | pixman_image_t *ifg = pixman_image_create_solid_fill(fgcol); |
| 179 | pixman_image_t *ibg = pixman_image_create_solid_fill(bgcol); |
| 180 | |
| 181 | pixman_image_composite(PIXMAN_OP_SRC, ibg, NULL, surface, |
| 182 | 0, 0, 0, 0, |
| 183 | cw * x, ch * y, |
| 184 | cw, ch); |
| 185 | pixman_image_composite(PIXMAN_OP_OVER, ifg, glyph, surface, |
| 186 | 0, 0, 0, 0, |
| 187 | cw * x, ch * y, |
| 188 | cw, ch); |
| 189 | pixman_image_unref(ifg); |
| 190 | pixman_image_unref(ibg); |
| 191 | } |