2
0

pixman-minimal.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #ifndef PIXMAN_MINIMAL_H
  44. #define PIXMAN_MINIMAL_H
  45. #define PIXMAN_TYPE_OTHER 0
  46. #define PIXMAN_TYPE_ARGB 2
  47. #define PIXMAN_TYPE_ABGR 3
  48. #define PIXMAN_TYPE_BGRA 8
  49. #define PIXMAN_TYPE_RGBA 9
  50. #define PIXMAN_FORMAT(bpp, type, a, r, g, b) (((bpp) << 24) | \
  51. ((type) << 16) | \
  52. ((a) << 12) | \
  53. ((r) << 8) | \
  54. ((g) << 4) | \
  55. ((b)))
  56. #define PIXMAN_FORMAT_RESHIFT(val, ofs, num) \
  57. (((val >> (ofs)) & ((1 << (num)) - 1)) << ((val >> 22) & 3))
  58. #define PIXMAN_FORMAT_BPP(f) PIXMAN_FORMAT_RESHIFT(f, 24, 8)
  59. #define PIXMAN_FORMAT_TYPE(f) (((f) >> 16) & 0x3f)
  60. #define PIXMAN_FORMAT_A(f) PIXMAN_FORMAT_RESHIFT(f, 12, 4)
  61. #define PIXMAN_FORMAT_R(f) PIXMAN_FORMAT_RESHIFT(f, 8, 4)
  62. #define PIXMAN_FORMAT_G(f) PIXMAN_FORMAT_RESHIFT(f, 4, 4)
  63. #define PIXMAN_FORMAT_B(f) PIXMAN_FORMAT_RESHIFT(f, 0, 4)
  64. #define PIXMAN_FORMAT_DEPTH(f) (PIXMAN_FORMAT_A(f) + \
  65. PIXMAN_FORMAT_R(f) + \
  66. PIXMAN_FORMAT_G(f) + \
  67. PIXMAN_FORMAT_B(f))
  68. typedef enum {
  69. /* 32bpp formats */
  70. PIXMAN_a8r8g8b8 = PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 8, 8, 8, 8),
  71. PIXMAN_x8r8g8b8 = PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8),
  72. PIXMAN_a8b8g8r8 = PIXMAN_FORMAT(32, PIXMAN_TYPE_ABGR, 8, 8, 8, 8),
  73. PIXMAN_x8b8g8r8 = PIXMAN_FORMAT(32, PIXMAN_TYPE_ABGR, 0, 8, 8, 8),
  74. PIXMAN_b8g8r8a8 = PIXMAN_FORMAT(32, PIXMAN_TYPE_BGRA, 8, 8, 8, 8),
  75. PIXMAN_b8g8r8x8 = PIXMAN_FORMAT(32, PIXMAN_TYPE_BGRA, 0, 8, 8, 8),
  76. PIXMAN_r8g8b8a8 = PIXMAN_FORMAT(32, PIXMAN_TYPE_RGBA, 8, 8, 8, 8),
  77. PIXMAN_r8g8b8x8 = PIXMAN_FORMAT(32, PIXMAN_TYPE_RGBA, 0, 8, 8, 8),
  78. /* 24bpp formats */
  79. PIXMAN_r8g8b8 = PIXMAN_FORMAT(24, PIXMAN_TYPE_ARGB, 0, 8, 8, 8),
  80. PIXMAN_b8g8r8 = PIXMAN_FORMAT(24, PIXMAN_TYPE_ABGR, 0, 8, 8, 8),
  81. /* 16bpp formats */
  82. PIXMAN_r5g6b5 = PIXMAN_FORMAT(16, PIXMAN_TYPE_ARGB, 0, 5, 6, 5),
  83. PIXMAN_a1r5g5b5 = PIXMAN_FORMAT(16, PIXMAN_TYPE_ARGB, 1, 5, 5, 5),
  84. PIXMAN_x1r5g5b5 = PIXMAN_FORMAT(16, PIXMAN_TYPE_ARGB, 0, 5, 5, 5),
  85. } pixman_format_code_t;
  86. typedef struct pixman_image pixman_image_t;
  87. typedef void (*pixman_image_destroy_func_t)(pixman_image_t *image, void *data);
  88. struct pixman_image {
  89. int ref_count;
  90. pixman_format_code_t format;
  91. int width;
  92. int height;
  93. int stride;
  94. uint32_t *data;
  95. uint32_t *free_me;
  96. pixman_image_destroy_func_t destroy_func;
  97. void *destroy_data;
  98. };
  99. typedef struct pixman_color {
  100. uint16_t red;
  101. uint16_t green;
  102. uint16_t blue;
  103. uint16_t alpha;
  104. } pixman_color_t;
  105. static inline uint32_t *create_bits(pixman_format_code_t format,
  106. int width,
  107. int height,
  108. int *rowstride_bytes)
  109. {
  110. int stride = 0;
  111. size_t buf_size = 0;
  112. int bpp = PIXMAN_FORMAT_BPP(format);
  113. /*
  114. * Calculate the following while checking for overflow truncation:
  115. * stride = ((width * bpp + 0x1f) >> 5) * sizeof(uint32_t);
  116. */
  117. if (unlikely(__builtin_mul_overflow(width, bpp, &stride))) {
  118. return NULL;
  119. }
  120. if (unlikely(__builtin_add_overflow(stride, 0x1f, &stride))) {
  121. return NULL;
  122. }
  123. stride >>= 5;
  124. stride *= sizeof(uint32_t);
  125. if (unlikely(__builtin_mul_overflow((size_t) height,
  126. (size_t) stride,
  127. &buf_size))) {
  128. return NULL;
  129. }
  130. if (rowstride_bytes) {
  131. *rowstride_bytes = stride;
  132. }
  133. return g_malloc0(buf_size);
  134. }
  135. static inline pixman_image_t *pixman_image_create_bits(pixman_format_code_t format,
  136. int width,
  137. int height,
  138. uint32_t *bits,
  139. int rowstride_bytes)
  140. {
  141. pixman_image_t *i = g_new0(pixman_image_t, 1);
  142. i->width = width;
  143. i->height = height;
  144. i->format = format;
  145. if (bits) {
  146. i->data = bits;
  147. } else {
  148. i->free_me = i->data =
  149. create_bits(format, width, height, &rowstride_bytes);
  150. if (width && height) {
  151. assert(i->data);
  152. }
  153. }
  154. i->stride = rowstride_bytes ? rowstride_bytes :
  155. width * DIV_ROUND_UP(PIXMAN_FORMAT_BPP(format), 8);
  156. i->ref_count = 1;
  157. return i;
  158. }
  159. static inline pixman_image_t *pixman_image_ref(pixman_image_t *i)
  160. {
  161. i->ref_count++;
  162. return i;
  163. }
  164. static inline bool pixman_image_unref(pixman_image_t *i)
  165. {
  166. i->ref_count--;
  167. if (i->ref_count == 0) {
  168. if (i->destroy_func) {
  169. i->destroy_func(i, i->destroy_data);
  170. }
  171. g_free(i->free_me);
  172. g_free(i);
  173. return true;
  174. }
  175. return false;
  176. }
  177. static inline void pixman_image_set_destroy_function(pixman_image_t *i,
  178. pixman_image_destroy_func_t func,
  179. void *data)
  180. {
  181. i->destroy_func = func;
  182. i->destroy_data = data;
  183. }
  184. static inline uint32_t *pixman_image_get_data(pixman_image_t *i)
  185. {
  186. return i->data;
  187. }
  188. static inline int pixman_image_get_height(pixman_image_t *i)
  189. {
  190. return i->height;
  191. }
  192. static inline int pixman_image_get_width(pixman_image_t *i)
  193. {
  194. return i->width;
  195. }
  196. static inline int pixman_image_get_stride(pixman_image_t *i)
  197. {
  198. return i->stride;
  199. }
  200. static inline pixman_format_code_t pixman_image_get_format(pixman_image_t *i)
  201. {
  202. return i->format;
  203. }
  204. #endif /* PIXMAN_MINIMAL_H */