| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Tiny subset of PIXMAN API commonly used by QEMU. |
| 5 | * |
| 6 | * Copyright 1987, 1988, 1989, 1998 The Open Group |
| 7 | * Copyright 1987, 1988, 1989 Digital Equipment Corporation |
| 8 | * Copyright 1999, 2004, 2008 Keith Packard |
| 9 | * Copyright 2000 SuSE, Inc. |
| 10 | * Copyright 2000 Keith Packard, member of The XFree86 Project, Inc. |
| 11 | * Copyright 2004, 2005, 2007, 2008, 2009, 2010 Red Hat, Inc. |
| 12 | * Copyright 2004 Nicholas Miell |
| 13 | * Copyright 2005 Lars Knoll & Zack Rusin, Trolltech |
| 14 | * Copyright 2005 Trolltech AS |
| 15 | * Copyright 2007 Luca Barbato |
| 16 | * Copyright 2008 Aaron Plattner, NVIDIA Corporation |
| 17 | * Copyright 2008 Rodrigo Kumpera |
| 18 | * Copyright 2008 André Tupinambá |
| 19 | * Copyright 2008 Mozilla Corporation |
| 20 | * Copyright 2008 Frederic Plourde |
| 21 | * Copyright 2009, Oracle and/or its affiliates. All rights reserved. |
| 22 | * Copyright 2009, 2010 Nokia Corporation |
| 23 | * |
| 24 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 25 | * copy of this software and associated documentation files (the "Software"), |
| 26 | * to deal in the Software without restriction, including without limitation |
| 27 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 28 | * and/or sell copies of the Software, and to permit persons to whom the |
| 29 | * Software is furnished to do so, subject to the following conditions: |
| 30 | * |
| 31 | * The above copyright notice and this permission notice (including the next |
| 32 | * paragraph) shall be included in all copies or substantial portions of the |
| 33 | * Software. |
| 34 | * |
| 35 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 36 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 37 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 38 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 39 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 40 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 41 | * DEALINGS IN THE SOFTWARE. |
| 42 | */ |
| 43 | |
| 44 | #ifndef PIXMAN_MINIMAL_H |
| 45 | #define PIXMAN_MINIMAL_H |
| 46 | |
| 47 | #define PIXMAN_TYPE_OTHER 0 |
| 48 | #define PIXMAN_TYPE_ARGB 2 |
| 49 | #define PIXMAN_TYPE_ABGR 3 |
| 50 | #define PIXMAN_TYPE_BGRA 8 |
| 51 | #define PIXMAN_TYPE_RGBA 9 |
| 52 | |
| 53 | #define PIXMAN_FORMAT(bpp, type, a, r, g, b) (((bpp) << 24) | \ |
| 54 | ((type) << 16) | \ |
| 55 | ((a) << 12) | \ |
| 56 | ((r) << 8) | \ |
| 57 | ((g) << 4) | \ |
| 58 | ((b))) |
| 59 | |
| 60 | #define PIXMAN_FORMAT_RESHIFT(val, ofs, num) \ |
| 61 | (((val >> (ofs)) & ((1 << (num)) - 1)) << ((val >> 22) & 3)) |
| 62 | |
| 63 | #define PIXMAN_FORMAT_BPP(f) PIXMAN_FORMAT_RESHIFT(f, 24, 8) |
| 64 | #define PIXMAN_FORMAT_TYPE(f) (((f) >> 16) & 0x3f) |
| 65 | #define PIXMAN_FORMAT_A(f) PIXMAN_FORMAT_RESHIFT(f, 12, 4) |
| 66 | #define PIXMAN_FORMAT_R(f) PIXMAN_FORMAT_RESHIFT(f, 8, 4) |
| 67 | #define PIXMAN_FORMAT_G(f) PIXMAN_FORMAT_RESHIFT(f, 4, 4) |
| 68 | #define PIXMAN_FORMAT_B(f) PIXMAN_FORMAT_RESHIFT(f, 0, 4) |
| 69 | #define PIXMAN_FORMAT_DEPTH(f) (PIXMAN_FORMAT_A(f) + \ |
| 70 | PIXMAN_FORMAT_R(f) + \ |
| 71 | PIXMAN_FORMAT_G(f) + \ |
| 72 | PIXMAN_FORMAT_B(f)) |
| 73 | |
| 74 | typedef enum { |
| 75 | /* 32bpp formats */ |
| 76 | PIXMAN_a8r8g8b8 = PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 8, 8, 8, 8), |
| 77 | PIXMAN_x8r8g8b8 = PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8), |
| 78 | PIXMAN_a8b8g8r8 = PIXMAN_FORMAT(32, PIXMAN_TYPE_ABGR, 8, 8, 8, 8), |
| 79 | PIXMAN_x8b8g8r8 = PIXMAN_FORMAT(32, PIXMAN_TYPE_ABGR, 0, 8, 8, 8), |
| 80 | PIXMAN_b8g8r8a8 = PIXMAN_FORMAT(32, PIXMAN_TYPE_BGRA, 8, 8, 8, 8), |
| 81 | PIXMAN_b8g8r8x8 = PIXMAN_FORMAT(32, PIXMAN_TYPE_BGRA, 0, 8, 8, 8), |
| 82 | PIXMAN_r8g8b8a8 = PIXMAN_FORMAT(32, PIXMAN_TYPE_RGBA, 8, 8, 8, 8), |
| 83 | PIXMAN_r8g8b8x8 = PIXMAN_FORMAT(32, PIXMAN_TYPE_RGBA, 0, 8, 8, 8), |
| 84 | /* 24bpp formats */ |
| 85 | PIXMAN_r8g8b8 = PIXMAN_FORMAT(24, PIXMAN_TYPE_ARGB, 0, 8, 8, 8), |
| 86 | PIXMAN_b8g8r8 = PIXMAN_FORMAT(24, PIXMAN_TYPE_ABGR, 0, 8, 8, 8), |
| 87 | /* 16bpp formats */ |
| 88 | PIXMAN_r5g6b5 = PIXMAN_FORMAT(16, PIXMAN_TYPE_ARGB, 0, 5, 6, 5), |
| 89 | PIXMAN_a1r5g5b5 = PIXMAN_FORMAT(16, PIXMAN_TYPE_ARGB, 1, 5, 5, 5), |
| 90 | PIXMAN_x1r5g5b5 = PIXMAN_FORMAT(16, PIXMAN_TYPE_ARGB, 0, 5, 5, 5), |
| 91 | } pixman_format_code_t; |
| 92 | |
| 93 | typedef struct pixman_image pixman_image_t; |
| 94 | |
| 95 | typedef void (*pixman_image_destroy_func_t)(pixman_image_t *image, void *data); |
| 96 | |
| 97 | struct pixman_image { |
| 98 | int ref_count; |
| 99 | pixman_format_code_t format; |
| 100 | int width; |
| 101 | int height; |
| 102 | int stride; |
| 103 | uint32_t *data; |
| 104 | uint32_t *free_me; |
| 105 | pixman_image_destroy_func_t destroy_func; |
| 106 | void *destroy_data; |
| 107 | }; |
| 108 | |
| 109 | typedef struct pixman_color { |
| 110 | uint16_t red; |
| 111 | uint16_t green; |
| 112 | uint16_t blue; |
| 113 | uint16_t alpha; |
| 114 | } pixman_color_t; |
| 115 | |
| 116 | #include "qemu-pixman-helpers.h" |
| 117 | |
| 118 | static inline uint32_t *create_bits(pixman_format_code_t format, |
| 119 | int width, |
| 120 | int height, |
| 121 | int *rowstride_bytes) |
| 122 | { |
| 123 | int stride = 0; |
| 124 | size_t buf_size = 0; |
| 125 | |
| 126 | if (!qemu_pixman_image_calc_size(format, width, height, |
| 127 | &stride, &buf_size)) { |
| 128 | return NULL; |
| 129 | } |
| 130 | |
| 131 | if (rowstride_bytes) { |
| 132 | *rowstride_bytes = stride; |
| 133 | } |
| 134 | |
| 135 | return g_malloc0(buf_size); |
| 136 | } |
| 137 | |
| 138 | static inline pixman_image_t *pixman_image_create_bits(pixman_format_code_t format, |
| 139 | int width, |
| 140 | int height, |
| 141 | uint32_t *bits, |
| 142 | int rowstride_bytes) |
| 143 | { |
| 144 | pixman_image_t *i = g_new0(pixman_image_t, 1); |
| 145 | |
| 146 | i->width = width; |
| 147 | i->height = height; |
| 148 | i->format = format; |
| 149 | if (bits) { |
| 150 | i->data = bits; |
| 151 | } else { |
| 152 | i->free_me = i->data = |
| 153 | create_bits(format, width, height, &rowstride_bytes); |
| 154 | if (width && height) { |
| 155 | assert(i->data); |
| 156 | } |
| 157 | } |
| 158 | i->stride = rowstride_bytes ? rowstride_bytes : |
| 159 | width * DIV_ROUND_UP(PIXMAN_FORMAT_BPP(format), 8); |
| 160 | i->ref_count = 1; |
| 161 | |
| 162 | return i; |
| 163 | } |
| 164 | |
| 165 | static inline pixman_image_t *pixman_image_ref(pixman_image_t *i) |
| 166 | { |
| 167 | i->ref_count++; |
| 168 | return i; |
| 169 | } |
| 170 | |
| 171 | static inline bool pixman_image_unref(pixman_image_t *i) |
| 172 | { |
| 173 | i->ref_count--; |
| 174 | |
| 175 | if (i->ref_count == 0) { |
| 176 | if (i->destroy_func) { |
| 177 | i->destroy_func(i, i->destroy_data); |
| 178 | } |
| 179 | g_free(i->free_me); |
| 180 | g_free(i); |
| 181 | |
| 182 | return true; |
| 183 | } |
| 184 | |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | static inline void pixman_image_set_destroy_function(pixman_image_t *i, |
| 189 | pixman_image_destroy_func_t func, |
| 190 | void *data) |
| 191 | |
| 192 | { |
| 193 | i->destroy_func = func; |
| 194 | i->destroy_data = data; |
| 195 | } |
| 196 | |
| 197 | static inline uint32_t *pixman_image_get_data(pixman_image_t *i) |
| 198 | { |
| 199 | return i->data; |
| 200 | } |
| 201 | |
| 202 | static inline int pixman_image_get_height(pixman_image_t *i) |
| 203 | { |
| 204 | return i->height; |
| 205 | } |
| 206 | |
| 207 | static inline int pixman_image_get_width(pixman_image_t *i) |
| 208 | { |
| 209 | return i->width; |
| 210 | } |
| 211 | |
| 212 | static inline int pixman_image_get_stride(pixman_image_t *i) |
| 213 | { |
| 214 | return i->stride; |
| 215 | } |
| 216 | |
| 217 | static inline pixman_format_code_t pixman_image_get_format(pixman_image_t *i) |
| 218 | { |
| 219 | return i->format; |
| 220 | } |
| 221 | |
| 222 | #endif /* PIXMAN_MINIMAL_H */ |