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 | e16f4c8 | 2016-01-29 17:49:51 +0000 | [diff] [blame] | 6 | #include "qemu/osdep.h" |
Peter Maydell | 0b08786 | 2013-01-21 12:03:47 +0000 | [diff] [blame] | 7 | #include "qemu-common.h" |
| 8 | #include "ui/console.h" |
Gerd Hoffmann | d2ec7e2 | 2012-09-25 16:23:24 +0200 | [diff] [blame] | 9 | |
Gerd Hoffmann | a93a3af | 2014-06-18 09:00:00 +0200 | [diff] [blame] | 10 | PixelFormat qemu_pixelformat_from_pixman(pixman_format_code_t format) |
| 11 | { |
| 12 | PixelFormat pf; |
| 13 | uint8_t bpp; |
| 14 | |
| 15 | bpp = pf.bits_per_pixel = PIXMAN_FORMAT_BPP(format); |
| 16 | pf.bytes_per_pixel = PIXMAN_FORMAT_BPP(format) / 8; |
| 17 | pf.depth = PIXMAN_FORMAT_DEPTH(format); |
| 18 | |
| 19 | pf.abits = PIXMAN_FORMAT_A(format); |
| 20 | pf.rbits = PIXMAN_FORMAT_R(format); |
| 21 | pf.gbits = PIXMAN_FORMAT_G(format); |
| 22 | pf.bbits = PIXMAN_FORMAT_B(format); |
| 23 | |
| 24 | switch (PIXMAN_FORMAT_TYPE(format)) { |
| 25 | case PIXMAN_TYPE_ARGB: |
| 26 | pf.ashift = pf.bbits + pf.gbits + pf.rbits; |
| 27 | pf.rshift = pf.bbits + pf.gbits; |
| 28 | pf.gshift = pf.bbits; |
| 29 | pf.bshift = 0; |
| 30 | break; |
| 31 | case PIXMAN_TYPE_ABGR: |
| 32 | pf.ashift = pf.rbits + pf.gbits + pf.bbits; |
| 33 | pf.bshift = pf.rbits + pf.gbits; |
| 34 | pf.gshift = pf.rbits; |
| 35 | pf.rshift = 0; |
| 36 | break; |
| 37 | case PIXMAN_TYPE_BGRA: |
| 38 | pf.bshift = bpp - pf.bbits; |
| 39 | pf.gshift = bpp - (pf.bbits + pf.gbits); |
| 40 | pf.rshift = bpp - (pf.bbits + pf.gbits + pf.rbits); |
| 41 | pf.ashift = 0; |
| 42 | break; |
| 43 | case PIXMAN_TYPE_RGBA: |
| 44 | pf.rshift = bpp - pf.rbits; |
| 45 | pf.gshift = bpp - (pf.rbits + pf.gbits); |
| 46 | pf.bshift = bpp - (pf.rbits + pf.gbits + pf.bbits); |
| 47 | pf.ashift = 0; |
| 48 | break; |
| 49 | default: |
| 50 | g_assert_not_reached(); |
| 51 | break; |
| 52 | } |
| 53 | |
| 54 | pf.amax = (1 << pf.abits) - 1; |
| 55 | pf.rmax = (1 << pf.rbits) - 1; |
| 56 | pf.gmax = (1 << pf.gbits) - 1; |
| 57 | pf.bmax = (1 << pf.bbits) - 1; |
| 58 | pf.amask = pf.amax << pf.ashift; |
| 59 | pf.rmask = pf.rmax << pf.rshift; |
| 60 | pf.gmask = pf.gmax << pf.gshift; |
| 61 | pf.bmask = pf.bmax << pf.bshift; |
| 62 | |
| 63 | return pf; |
| 64 | } |
| 65 | |
Gerd Hoffmann | 1527a25 | 2014-06-18 11:31:42 +0200 | [diff] [blame] | 66 | pixman_format_code_t qemu_default_pixman_format(int bpp, bool native_endian) |
| 67 | { |
| 68 | if (native_endian) { |
| 69 | switch (bpp) { |
| 70 | case 15: |
| 71 | return PIXMAN_x1r5g5b5; |
| 72 | case 16: |
| 73 | return PIXMAN_r5g6b5; |
| 74 | case 24: |
| 75 | return PIXMAN_r8g8b8; |
| 76 | case 32: |
| 77 | return PIXMAN_x8r8g8b8; |
| 78 | } |
| 79 | } else { |
| 80 | switch (bpp) { |
| 81 | case 24: |
| 82 | return PIXMAN_b8g8r8; |
| 83 | case 32: |
Gerd Hoffmann | 89ec031 | 2014-09-15 08:33:37 +0200 | [diff] [blame] | 84 | return PIXMAN_b8g8r8x8; |
Gerd Hoffmann | 1527a25 | 2014-06-18 11:31:42 +0200 | [diff] [blame] | 85 | break; |
| 86 | } |
| 87 | } |
Benjamin Herrenschmidt | aca7aaf | 2014-07-07 16:37:26 +1000 | [diff] [blame] | 88 | return 0; |
Gerd Hoffmann | 1527a25 | 2014-06-18 11:31:42 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Gerd Hoffmann | d2ec7e2 | 2012-09-25 16:23:24 +0200 | [diff] [blame] | 91 | int qemu_pixman_get_type(int rshift, int gshift, int bshift) |
| 92 | { |
| 93 | int type = PIXMAN_TYPE_OTHER; |
| 94 | |
| 95 | if (rshift > gshift && gshift > bshift) { |
| 96 | if (bshift == 0) { |
| 97 | type = PIXMAN_TYPE_ARGB; |
| 98 | } else { |
| 99 | #if PIXMAN_VERSION >= PIXMAN_VERSION_ENCODE(0, 21, 8) |
| 100 | type = PIXMAN_TYPE_RGBA; |
| 101 | #endif |
| 102 | } |
| 103 | } else if (rshift < gshift && gshift < bshift) { |
| 104 | if (rshift == 0) { |
| 105 | type = PIXMAN_TYPE_ABGR; |
| 106 | } else { |
Gerd Hoffmann | fbddfc7 | 2012-12-14 08:54:21 +0100 | [diff] [blame] | 107 | #if PIXMAN_VERSION >= PIXMAN_VERSION_ENCODE(0, 16, 0) |
Gerd Hoffmann | d2ec7e2 | 2012-09-25 16:23:24 +0200 | [diff] [blame] | 108 | type = PIXMAN_TYPE_BGRA; |
Alexander Graf | 6e72719 | 2012-11-26 19:49:58 +0100 | [diff] [blame] | 109 | #endif |
Gerd Hoffmann | d2ec7e2 | 2012-09-25 16:23:24 +0200 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | return type; |
| 113 | } |
| 114 | |
| 115 | pixman_format_code_t qemu_pixman_get_format(PixelFormat *pf) |
| 116 | { |
| 117 | pixman_format_code_t format; |
| 118 | int type; |
| 119 | |
| 120 | type = qemu_pixman_get_type(pf->rshift, pf->gshift, pf->bshift); |
| 121 | format = PIXMAN_FORMAT(pf->bits_per_pixel, type, |
| 122 | pf->abits, pf->rbits, pf->gbits, pf->bbits); |
| 123 | if (!pixman_format_supported_source(format)) { |
| 124 | return 0; |
| 125 | } |
| 126 | return format; |
| 127 | } |
| 128 | |
Gerd Hoffmann | 8cd996f | 2015-01-09 08:49:20 +0100 | [diff] [blame] | 129 | /* |
| 130 | * Return true for known-good pixman conversions. |
| 131 | * |
| 132 | * UIs using pixman for format conversion can hook this into |
| 133 | * DisplayChangeListenerOps->dpy_gfx_check_format |
| 134 | */ |
| 135 | bool qemu_pixman_check_format(DisplayChangeListener *dcl, |
| 136 | pixman_format_code_t format) |
| 137 | { |
| 138 | switch (format) { |
| 139 | /* 32 bpp */ |
| 140 | case PIXMAN_x8r8g8b8: |
| 141 | case PIXMAN_a8r8g8b8: |
| 142 | case PIXMAN_b8g8r8x8: |
| 143 | case PIXMAN_b8g8r8a8: |
| 144 | /* 24 bpp */ |
| 145 | case PIXMAN_r8g8b8: |
| 146 | case PIXMAN_b8g8r8: |
| 147 | /* 16 bpp */ |
| 148 | case PIXMAN_x1r5g5b5: |
| 149 | case PIXMAN_r5g6b5: |
| 150 | return true; |
| 151 | default: |
| 152 | return false; |
| 153 | } |
| 154 | } |
| 155 | |
Gerd Hoffmann | d2ec7e2 | 2012-09-25 16:23:24 +0200 | [diff] [blame] | 156 | pixman_image_t *qemu_pixman_linebuf_create(pixman_format_code_t format, |
| 157 | int width) |
| 158 | { |
| 159 | pixman_image_t *image = pixman_image_create_bits(format, width, 1, NULL, 0); |
| 160 | assert(image != NULL); |
| 161 | return image; |
| 162 | } |
| 163 | |
Gerd Hoffmann | 43c7d8b | 2014-06-19 13:19:01 +0200 | [diff] [blame] | 164 | /* fill linebuf from framebuffer */ |
Gerd Hoffmann | d2ec7e2 | 2012-09-25 16:23:24 +0200 | [diff] [blame] | 165 | 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] | 166 | int width, int x, int y) |
Gerd Hoffmann | d2ec7e2 | 2012-09-25 16:23:24 +0200 | [diff] [blame] | 167 | { |
| 168 | pixman_image_composite(PIXMAN_OP_SRC, fb, NULL, linebuf, |
Gerd Hoffmann | bc210eb | 2012-12-14 07:54:24 +0000 | [diff] [blame] | 169 | x, y, 0, 0, 0, 0, width, 1); |
Gerd Hoffmann | d2ec7e2 | 2012-09-25 16:23:24 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Gerd Hoffmann | 43c7d8b | 2014-06-19 13:19:01 +0200 | [diff] [blame] | 172 | /* copy linebuf to framebuffer */ |
| 173 | void qemu_pixman_linebuf_copy(pixman_image_t *fb, int width, int x, int y, |
| 174 | pixman_image_t *linebuf) |
| 175 | { |
| 176 | pixman_image_composite(PIXMAN_OP_SRC, linebuf, NULL, fb, |
| 177 | 0, 0, 0, 0, x, y, width, 1); |
| 178 | } |
| 179 | |
Gerd Hoffmann | d9a8656 | 2012-11-02 09:12:49 +0100 | [diff] [blame] | 180 | pixman_image_t *qemu_pixman_mirror_create(pixman_format_code_t format, |
| 181 | pixman_image_t *image) |
| 182 | { |
| 183 | pixman_image_t *mirror; |
| 184 | |
| 185 | mirror = pixman_image_create_bits(format, |
| 186 | pixman_image_get_width(image), |
| 187 | pixman_image_get_height(image), |
| 188 | NULL, |
| 189 | pixman_image_get_stride(image)); |
| 190 | return mirror; |
| 191 | } |
| 192 | |
Gerd Hoffmann | d2ec7e2 | 2012-09-25 16:23:24 +0200 | [diff] [blame] | 193 | void qemu_pixman_image_unref(pixman_image_t *image) |
| 194 | { |
| 195 | if (image == NULL) { |
| 196 | return; |
| 197 | } |
| 198 | pixman_image_unref(image); |
| 199 | } |
Gerd Hoffmann | 867c538 | 2013-03-06 14:14:17 +0100 | [diff] [blame] | 200 | |
| 201 | pixman_color_t qemu_pixman_color(PixelFormat *pf, uint32_t color) |
| 202 | { |
| 203 | pixman_color_t c; |
| 204 | |
| 205 | c.red = ((color & pf->rmask) >> pf->rshift) << (16 - pf->rbits); |
| 206 | c.green = ((color & pf->gmask) >> pf->gshift) << (16 - pf->gbits); |
| 207 | c.blue = ((color & pf->bmask) >> pf->bshift) << (16 - pf->bbits); |
| 208 | c.alpha = ((color & pf->amask) >> pf->ashift) << (16 - pf->abits); |
| 209 | return c; |
| 210 | } |
Gerd Hoffmann | b762795 | 2013-03-07 15:23:48 +0100 | [diff] [blame] | 211 | |
| 212 | pixman_image_t *qemu_pixman_glyph_from_vgafont(int height, const uint8_t *font, |
| 213 | unsigned int ch) |
| 214 | { |
| 215 | pixman_image_t *glyph; |
| 216 | uint8_t *data; |
| 217 | bool bit; |
| 218 | int x, y; |
| 219 | |
| 220 | glyph = pixman_image_create_bits(PIXMAN_a8, 8, height, |
| 221 | NULL, 0); |
| 222 | data = (uint8_t *)pixman_image_get_data(glyph); |
| 223 | |
| 224 | font += height * ch; |
| 225 | for (y = 0; y < height; y++, font++) { |
| 226 | for (x = 0; x < 8; x++, data++) { |
| 227 | bit = (*font) & (1 << (7-x)); |
| 228 | *data = bit ? 0xff : 0x00; |
| 229 | } |
| 230 | } |
| 231 | return glyph; |
| 232 | } |
| 233 | |
| 234 | void qemu_pixman_glyph_render(pixman_image_t *glyph, |
| 235 | pixman_image_t *surface, |
| 236 | pixman_color_t *fgcol, |
| 237 | pixman_color_t *bgcol, |
| 238 | int x, int y, int cw, int ch) |
| 239 | { |
| 240 | pixman_image_t *ifg = pixman_image_create_solid_fill(fgcol); |
| 241 | pixman_image_t *ibg = pixman_image_create_solid_fill(bgcol); |
| 242 | |
| 243 | pixman_image_composite(PIXMAN_OP_SRC, ibg, NULL, surface, |
| 244 | 0, 0, 0, 0, |
| 245 | cw * x, ch * y, |
| 246 | cw, ch); |
| 247 | pixman_image_composite(PIXMAN_OP_OVER, ifg, glyph, surface, |
| 248 | 0, 0, 0, 0, |
| 249 | cw * x, ch * y, |
| 250 | cw, ch); |
| 251 | pixman_image_unref(ifg); |
| 252 | pixman_image_unref(ibg); |
| 253 | } |