xref: /openbmc/qemu/include/ui/pixman-minimal.h (revision 7200fb21)
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 static inline pixman_image_t *pixman_image_create_bits(pixman_format_code_t format,
117                                                        int width,
118                                                        int height,
119                                                        uint32_t *bits,
120                                                        int rowstride_bytes)
121 {
122     pixman_image_t *i = g_new0(pixman_image_t, 1);
123 
124     i->width = width;
125     i->height = height;
126     i->stride = rowstride_bytes ?: width * DIV_ROUND_UP(PIXMAN_FORMAT_BPP(format), 8);
127     i->format = format;
128     if (bits) {
129         i->data = bits;
130     } else {
131         i->free_me = i->data = g_malloc0(rowstride_bytes * height);
132     }
133     i->ref_count = 1;
134 
135     return i;
136 }
137 
138 static inline pixman_image_t *pixman_image_ref(pixman_image_t *i)
139 {
140     i->ref_count++;
141     return i;
142 }
143 
144 static inline bool pixman_image_unref(pixman_image_t *i)
145 {
146     i->ref_count--;
147 
148     if (i->ref_count == 0) {
149         if (i->destroy_func) {
150             i->destroy_func(i, i->destroy_data);
151         }
152         g_free(i->free_me);
153         g_free(i);
154 
155         return true;
156     }
157 
158     return false;
159 }
160 
161 static inline void pixman_image_set_destroy_function(pixman_image_t *i,
162                                                      pixman_image_destroy_func_t func,
163                                                      void *data)
164 
165 {
166     i->destroy_func = func;
167     i->destroy_data = data;
168 }
169 
170 static inline uint32_t *pixman_image_get_data(pixman_image_t *i)
171 {
172     return i->data;
173 }
174 
175 static inline int pixman_image_get_height(pixman_image_t *i)
176 {
177     return i->height;
178 }
179 
180 static inline int pixman_image_get_width(pixman_image_t *i)
181 {
182     return i->width;
183 }
184 
185 static inline int pixman_image_get_stride(pixman_image_t *i)
186 {
187     return i->stride;
188 }
189 
190 static inline pixman_format_code_t pixman_image_get_format(pixman_image_t *i)
191 {
192     return i->format;
193 }
194 
195 #endif /* PIXMAN_MINIMAL_H */
196