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-common.h" 7 #include "ui/console.h" 8 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 65 pixman_format_code_t qemu_default_pixman_format(int bpp, bool native_endian) 66 { 67 if (native_endian) { 68 switch (bpp) { 69 case 15: 70 return PIXMAN_x1r5g5b5; 71 case 16: 72 return PIXMAN_r5g6b5; 73 case 24: 74 return PIXMAN_r8g8b8; 75 case 32: 76 return PIXMAN_x8r8g8b8; 77 } 78 } else { 79 switch (bpp) { 80 case 24: 81 return PIXMAN_b8g8r8; 82 case 32: 83 return PIXMAN_b8g8r8a8; 84 break; 85 } 86 } 87 g_assert_not_reached(); 88 } 89 90 int qemu_pixman_get_type(int rshift, int gshift, int bshift) 91 { 92 int type = PIXMAN_TYPE_OTHER; 93 94 if (rshift > gshift && gshift > bshift) { 95 if (bshift == 0) { 96 type = PIXMAN_TYPE_ARGB; 97 } else { 98 #if PIXMAN_VERSION >= PIXMAN_VERSION_ENCODE(0, 21, 8) 99 type = PIXMAN_TYPE_RGBA; 100 #endif 101 } 102 } else if (rshift < gshift && gshift < bshift) { 103 if (rshift == 0) { 104 type = PIXMAN_TYPE_ABGR; 105 } else { 106 #if PIXMAN_VERSION >= PIXMAN_VERSION_ENCODE(0, 16, 0) 107 type = PIXMAN_TYPE_BGRA; 108 #endif 109 } 110 } 111 return type; 112 } 113 114 pixman_format_code_t qemu_pixman_get_format(PixelFormat *pf) 115 { 116 pixman_format_code_t format; 117 int type; 118 119 type = qemu_pixman_get_type(pf->rshift, pf->gshift, pf->bshift); 120 format = PIXMAN_FORMAT(pf->bits_per_pixel, type, 121 pf->abits, pf->rbits, pf->gbits, pf->bbits); 122 if (!pixman_format_supported_source(format)) { 123 return 0; 124 } 125 return format; 126 } 127 128 pixman_image_t *qemu_pixman_linebuf_create(pixman_format_code_t format, 129 int width) 130 { 131 pixman_image_t *image = pixman_image_create_bits(format, width, 1, NULL, 0); 132 assert(image != NULL); 133 return image; 134 } 135 136 /* fill linebuf from framebuffer */ 137 void qemu_pixman_linebuf_fill(pixman_image_t *linebuf, pixman_image_t *fb, 138 int width, int x, int y) 139 { 140 pixman_image_composite(PIXMAN_OP_SRC, fb, NULL, linebuf, 141 x, y, 0, 0, 0, 0, width, 1); 142 } 143 144 /* copy linebuf to framebuffer */ 145 void qemu_pixman_linebuf_copy(pixman_image_t *fb, int width, int x, int y, 146 pixman_image_t *linebuf) 147 { 148 pixman_image_composite(PIXMAN_OP_SRC, linebuf, NULL, fb, 149 0, 0, 0, 0, x, y, width, 1); 150 } 151 152 pixman_image_t *qemu_pixman_mirror_create(pixman_format_code_t format, 153 pixman_image_t *image) 154 { 155 pixman_image_t *mirror; 156 157 mirror = pixman_image_create_bits(format, 158 pixman_image_get_width(image), 159 pixman_image_get_height(image), 160 NULL, 161 pixman_image_get_stride(image)); 162 return mirror; 163 } 164 165 void qemu_pixman_image_unref(pixman_image_t *image) 166 { 167 if (image == NULL) { 168 return; 169 } 170 pixman_image_unref(image); 171 } 172 173 pixman_color_t qemu_pixman_color(PixelFormat *pf, uint32_t color) 174 { 175 pixman_color_t c; 176 177 c.red = ((color & pf->rmask) >> pf->rshift) << (16 - pf->rbits); 178 c.green = ((color & pf->gmask) >> pf->gshift) << (16 - pf->gbits); 179 c.blue = ((color & pf->bmask) >> pf->bshift) << (16 - pf->bbits); 180 c.alpha = ((color & pf->amask) >> pf->ashift) << (16 - pf->abits); 181 return c; 182 } 183 184 pixman_image_t *qemu_pixman_glyph_from_vgafont(int height, const uint8_t *font, 185 unsigned int ch) 186 { 187 pixman_image_t *glyph; 188 uint8_t *data; 189 bool bit; 190 int x, y; 191 192 glyph = pixman_image_create_bits(PIXMAN_a8, 8, height, 193 NULL, 0); 194 data = (uint8_t *)pixman_image_get_data(glyph); 195 196 font += height * ch; 197 for (y = 0; y < height; y++, font++) { 198 for (x = 0; x < 8; x++, data++) { 199 bit = (*font) & (1 << (7-x)); 200 *data = bit ? 0xff : 0x00; 201 } 202 } 203 return glyph; 204 } 205 206 void qemu_pixman_glyph_render(pixman_image_t *glyph, 207 pixman_image_t *surface, 208 pixman_color_t *fgcol, 209 pixman_color_t *bgcol, 210 int x, int y, int cw, int ch) 211 { 212 pixman_image_t *ifg = pixman_image_create_solid_fill(fgcol); 213 pixman_image_t *ibg = pixman_image_create_solid_fill(bgcol); 214 215 pixman_image_composite(PIXMAN_OP_SRC, ibg, NULL, surface, 216 0, 0, 0, 0, 217 cw * x, ch * y, 218 cw, ch); 219 pixman_image_composite(PIXMAN_OP_OVER, ifg, glyph, surface, 220 0, 0, 0, 0, 221 cw * x, ch * y, 222 cw, ch); 223 pixman_image_unref(ifg); 224 pixman_image_unref(ibg); 225 } 226