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 6 #include "qemu/osdep.h" 7 #include "qemu-common.h" 8 #include "ui/console.h" 9 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 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: 84 return PIXMAN_b8g8r8x8; 85 break; 86 } 87 } 88 return 0; 89 } 90 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 type = PIXMAN_TYPE_RGBA; 100 } 101 } else if (rshift < gshift && gshift < bshift) { 102 if (rshift == 0) { 103 type = PIXMAN_TYPE_ABGR; 104 } else { 105 type = PIXMAN_TYPE_BGRA; 106 } 107 } 108 return type; 109 } 110 111 pixman_format_code_t qemu_pixman_get_format(PixelFormat *pf) 112 { 113 pixman_format_code_t format; 114 int type; 115 116 type = qemu_pixman_get_type(pf->rshift, pf->gshift, pf->bshift); 117 format = PIXMAN_FORMAT(pf->bits_per_pixel, type, 118 pf->abits, pf->rbits, pf->gbits, pf->bbits); 119 if (!pixman_format_supported_source(format)) { 120 return 0; 121 } 122 return format; 123 } 124 125 /* 126 * Return true for known-good pixman conversions. 127 * 128 * UIs using pixman for format conversion can hook this into 129 * DisplayChangeListenerOps->dpy_gfx_check_format 130 */ 131 bool qemu_pixman_check_format(DisplayChangeListener *dcl, 132 pixman_format_code_t format) 133 { 134 switch (format) { 135 /* 32 bpp */ 136 case PIXMAN_x8r8g8b8: 137 case PIXMAN_a8r8g8b8: 138 case PIXMAN_b8g8r8x8: 139 case PIXMAN_b8g8r8a8: 140 /* 24 bpp */ 141 case PIXMAN_r8g8b8: 142 case PIXMAN_b8g8r8: 143 /* 16 bpp */ 144 case PIXMAN_x1r5g5b5: 145 case PIXMAN_r5g6b5: 146 return true; 147 default: 148 return false; 149 } 150 } 151 152 pixman_image_t *qemu_pixman_linebuf_create(pixman_format_code_t format, 153 int width) 154 { 155 pixman_image_t *image = pixman_image_create_bits(format, width, 1, NULL, 0); 156 assert(image != NULL); 157 return image; 158 } 159 160 /* fill linebuf from framebuffer */ 161 void qemu_pixman_linebuf_fill(pixman_image_t *linebuf, pixman_image_t *fb, 162 int width, int x, int y) 163 { 164 pixman_image_composite(PIXMAN_OP_SRC, fb, NULL, linebuf, 165 x, y, 0, 0, 0, 0, width, 1); 166 } 167 168 /* copy linebuf to framebuffer */ 169 void qemu_pixman_linebuf_copy(pixman_image_t *fb, int width, int x, int y, 170 pixman_image_t *linebuf) 171 { 172 pixman_image_composite(PIXMAN_OP_SRC, linebuf, NULL, fb, 173 0, 0, 0, 0, x, y, width, 1); 174 } 175 176 pixman_image_t *qemu_pixman_mirror_create(pixman_format_code_t format, 177 pixman_image_t *image) 178 { 179 return pixman_image_create_bits(format, 180 pixman_image_get_width(image), 181 pixman_image_get_height(image), 182 NULL, 183 pixman_image_get_stride(image)); 184 } 185 186 void qemu_pixman_image_unref(pixman_image_t *image) 187 { 188 if (image == NULL) { 189 return; 190 } 191 pixman_image_unref(image); 192 } 193 194 pixman_color_t qemu_pixman_color(PixelFormat *pf, uint32_t color) 195 { 196 pixman_color_t c; 197 198 c.red = ((color & pf->rmask) >> pf->rshift) << (16 - pf->rbits); 199 c.green = ((color & pf->gmask) >> pf->gshift) << (16 - pf->gbits); 200 c.blue = ((color & pf->bmask) >> pf->bshift) << (16 - pf->bbits); 201 c.alpha = ((color & pf->amask) >> pf->ashift) << (16 - pf->abits); 202 return c; 203 } 204 205 pixman_image_t *qemu_pixman_glyph_from_vgafont(int height, const uint8_t *font, 206 unsigned int ch) 207 { 208 pixman_image_t *glyph; 209 uint8_t *data; 210 bool bit; 211 int x, y; 212 213 glyph = pixman_image_create_bits(PIXMAN_a8, 8, height, 214 NULL, 0); 215 data = (uint8_t *)pixman_image_get_data(glyph); 216 217 font += height * ch; 218 for (y = 0; y < height; y++, font++) { 219 for (x = 0; x < 8; x++, data++) { 220 bit = (*font) & (1 << (7-x)); 221 *data = bit ? 0xff : 0x00; 222 } 223 } 224 return glyph; 225 } 226 227 void qemu_pixman_glyph_render(pixman_image_t *glyph, 228 pixman_image_t *surface, 229 pixman_color_t *fgcol, 230 pixman_color_t *bgcol, 231 int x, int y, int cw, int ch) 232 { 233 pixman_image_t *ifg = pixman_image_create_solid_fill(fgcol); 234 pixman_image_t *ibg = pixman_image_create_solid_fill(bgcol); 235 236 pixman_image_composite(PIXMAN_OP_SRC, ibg, NULL, surface, 237 0, 0, 0, 0, 238 cw * x, ch * y, 239 cw, ch); 240 pixman_image_composite(PIXMAN_OP_OVER, ifg, glyph, surface, 241 0, 0, 0, 0, 242 cw * x, ch * y, 243 cw, ch); 244 pixman_image_unref(ifg); 245 pixman_image_unref(ibg); 246 } 247