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 "ui/console.h" 8 #include "standard-headers/drm/drm_fourcc.h" 9 #include "trace.h" 10 11 PixelFormat qemu_pixelformat_from_pixman(pixman_format_code_t format) 12 { 13 PixelFormat pf; 14 uint8_t bpp; 15 16 bpp = pf.bits_per_pixel = PIXMAN_FORMAT_BPP(format); 17 pf.bytes_per_pixel = PIXMAN_FORMAT_BPP(format) / 8; 18 pf.depth = PIXMAN_FORMAT_DEPTH(format); 19 20 pf.abits = PIXMAN_FORMAT_A(format); 21 pf.rbits = PIXMAN_FORMAT_R(format); 22 pf.gbits = PIXMAN_FORMAT_G(format); 23 pf.bbits = PIXMAN_FORMAT_B(format); 24 25 switch (PIXMAN_FORMAT_TYPE(format)) { 26 case PIXMAN_TYPE_ARGB: 27 pf.ashift = pf.bbits + pf.gbits + pf.rbits; 28 pf.rshift = pf.bbits + pf.gbits; 29 pf.gshift = pf.bbits; 30 pf.bshift = 0; 31 break; 32 case PIXMAN_TYPE_ABGR: 33 pf.ashift = pf.rbits + pf.gbits + pf.bbits; 34 pf.bshift = pf.rbits + pf.gbits; 35 pf.gshift = pf.rbits; 36 pf.rshift = 0; 37 break; 38 case PIXMAN_TYPE_BGRA: 39 pf.bshift = bpp - pf.bbits; 40 pf.gshift = bpp - (pf.bbits + pf.gbits); 41 pf.rshift = bpp - (pf.bbits + pf.gbits + pf.rbits); 42 pf.ashift = 0; 43 break; 44 case PIXMAN_TYPE_RGBA: 45 pf.rshift = bpp - pf.rbits; 46 pf.gshift = bpp - (pf.rbits + pf.gbits); 47 pf.bshift = bpp - (pf.rbits + pf.gbits + pf.bbits); 48 pf.ashift = 0; 49 break; 50 default: 51 g_assert_not_reached(); 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 /* Note: drm is little endian, pixman is native endian */ 92 static const struct { 93 uint32_t drm_format; 94 pixman_format_code_t pixman_format; 95 } drm_format_pixman_map[] = { 96 { DRM_FORMAT_RGB888, PIXMAN_LE_r8g8b8 }, 97 { DRM_FORMAT_ARGB8888, PIXMAN_LE_a8r8g8b8 }, 98 { DRM_FORMAT_XRGB8888, PIXMAN_LE_x8r8g8b8 }, 99 { DRM_FORMAT_XBGR8888, PIXMAN_LE_x8b8g8r8 }, 100 { DRM_FORMAT_ABGR8888, PIXMAN_LE_a8b8g8r8 }, 101 }; 102 103 pixman_format_code_t qemu_drm_format_to_pixman(uint32_t drm_format) 104 { 105 int i; 106 107 for (i = 0; i < ARRAY_SIZE(drm_format_pixman_map); i++) { 108 if (drm_format == drm_format_pixman_map[i].drm_format) { 109 return drm_format_pixman_map[i].pixman_format; 110 } 111 } 112 return 0; 113 } 114 115 uint32_t qemu_pixman_to_drm_format(pixman_format_code_t pixman_format) 116 { 117 int i; 118 119 for (i = 0; i < ARRAY_SIZE(drm_format_pixman_map); i++) { 120 if (pixman_format == drm_format_pixman_map[i].pixman_format) { 121 return drm_format_pixman_map[i].drm_format; 122 } 123 } 124 return 0; 125 } 126 127 int qemu_pixman_get_type(int rshift, int gshift, int bshift) 128 { 129 int type = PIXMAN_TYPE_OTHER; 130 131 if (rshift > gshift && gshift > bshift) { 132 if (bshift == 0) { 133 type = PIXMAN_TYPE_ARGB; 134 } else { 135 type = PIXMAN_TYPE_RGBA; 136 } 137 } else if (rshift < gshift && gshift < bshift) { 138 if (rshift == 0) { 139 type = PIXMAN_TYPE_ABGR; 140 } else { 141 type = PIXMAN_TYPE_BGRA; 142 } 143 } 144 return type; 145 } 146 147 #ifdef CONFIG_PIXMAN 148 pixman_format_code_t qemu_pixman_get_format(PixelFormat *pf) 149 { 150 pixman_format_code_t format; 151 int type; 152 153 type = qemu_pixman_get_type(pf->rshift, pf->gshift, pf->bshift); 154 format = PIXMAN_FORMAT(pf->bits_per_pixel, type, 155 pf->abits, pf->rbits, pf->gbits, pf->bbits); 156 if (!pixman_format_supported_source(format)) { 157 return 0; 158 } 159 return format; 160 } 161 #endif 162 163 /* 164 * Return true for known-good pixman conversions. 165 * 166 * UIs using pixman for format conversion can hook this into 167 * DisplayChangeListenerOps->dpy_gfx_check_format 168 */ 169 bool qemu_pixman_check_format(DisplayChangeListener *dcl, 170 pixman_format_code_t format) 171 { 172 switch (format) { 173 /* 32 bpp */ 174 case PIXMAN_x8r8g8b8: 175 case PIXMAN_a8r8g8b8: 176 case PIXMAN_b8g8r8x8: 177 case PIXMAN_b8g8r8a8: 178 /* 24 bpp */ 179 case PIXMAN_r8g8b8: 180 case PIXMAN_b8g8r8: 181 /* 16 bpp */ 182 case PIXMAN_x1r5g5b5: 183 case PIXMAN_r5g6b5: 184 return true; 185 default: 186 return false; 187 } 188 } 189 190 #ifdef CONFIG_PIXMAN 191 pixman_image_t *qemu_pixman_linebuf_create(pixman_format_code_t format, 192 int width) 193 { 194 pixman_image_t *image = pixman_image_create_bits(format, width, 1, NULL, 0); 195 assert(image != NULL); 196 return image; 197 } 198 199 /* fill linebuf from framebuffer */ 200 void qemu_pixman_linebuf_fill(pixman_image_t *linebuf, pixman_image_t *fb, 201 int width, int x, int y) 202 { 203 pixman_image_composite(PIXMAN_OP_SRC, fb, NULL, linebuf, 204 x, y, 0, 0, 0, 0, width, 1); 205 } 206 207 pixman_image_t *qemu_pixman_mirror_create(pixman_format_code_t format, 208 pixman_image_t *image) 209 { 210 return pixman_image_create_bits(format, 211 pixman_image_get_width(image), 212 pixman_image_get_height(image), 213 NULL, 214 pixman_image_get_stride(image)); 215 } 216 #endif 217 218 void qemu_pixman_image_unref(pixman_image_t *image) 219 { 220 if (image == NULL) { 221 return; 222 } 223 pixman_image_unref(image); 224 } 225 226 #ifdef CONFIG_PIXMAN 227 pixman_image_t *qemu_pixman_glyph_from_vgafont(int height, const uint8_t *font, 228 unsigned int ch) 229 { 230 pixman_image_t *glyph; 231 uint8_t *data; 232 bool bit; 233 int x, y; 234 235 glyph = pixman_image_create_bits(PIXMAN_a8, 8, height, 236 NULL, 0); 237 data = (uint8_t *)pixman_image_get_data(glyph); 238 239 font += height * ch; 240 for (y = 0; y < height; y++, font++) { 241 for (x = 0; x < 8; x++, data++) { 242 bit = (*font) & (1 << (7-x)); 243 *data = bit ? 0xff : 0x00; 244 } 245 } 246 return glyph; 247 } 248 249 void qemu_pixman_glyph_render(pixman_image_t *glyph, 250 pixman_image_t *surface, 251 pixman_color_t *fgcol, 252 pixman_color_t *bgcol, 253 int x, int y, int cw, int ch) 254 { 255 pixman_image_t *ifg = pixman_image_create_solid_fill(fgcol); 256 pixman_image_t *ibg = pixman_image_create_solid_fill(bgcol); 257 258 pixman_image_composite(PIXMAN_OP_SRC, ibg, NULL, surface, 259 0, 0, 0, 0, 260 cw * x, ch * y, 261 cw, ch); 262 pixman_image_composite(PIXMAN_OP_OVER, ifg, glyph, surface, 263 0, 0, 0, 0, 264 cw * x, ch * y, 265 cw, ch); 266 pixman_image_unref(ifg); 267 pixman_image_unref(ibg); 268 } 269 #endif /* CONFIG_PIXMAN */ 270