2
0

pflib.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * PixelFormat conversion library.
  3. *
  4. * Author: Gerd Hoffmann <kraxel@redhat.com>
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2. See
  7. * the COPYING file in the top-level directory.
  8. *
  9. * Contributions after 2012-01-13 are licensed under the terms of the
  10. * GNU GPL, version 2 or (at your option) any later version.
  11. */
  12. #include "qemu-common.h"
  13. #include "console.h"
  14. #include "pflib.h"
  15. typedef struct QemuPixel QemuPixel;
  16. typedef void (*pf_convert)(QemuPfConv *conv,
  17. void *dst, void *src, uint32_t cnt);
  18. typedef void (*pf_convert_from)(PixelFormat *pf,
  19. QemuPixel *dst, void *src, uint32_t cnt);
  20. typedef void (*pf_convert_to)(PixelFormat *pf,
  21. void *dst, QemuPixel *src, uint32_t cnt);
  22. struct QemuPfConv {
  23. pf_convert convert;
  24. PixelFormat src;
  25. PixelFormat dst;
  26. /* for copy_generic() */
  27. pf_convert_from conv_from;
  28. pf_convert_to conv_to;
  29. QemuPixel *conv_buf;
  30. uint32_t conv_cnt;
  31. };
  32. struct QemuPixel {
  33. uint8_t red;
  34. uint8_t green;
  35. uint8_t blue;
  36. uint8_t alpha;
  37. };
  38. /* ----------------------------------------------------------------------- */
  39. /* PixelFormat -> QemuPixel conversions */
  40. static void conv_16_to_pixel(PixelFormat *pf,
  41. QemuPixel *dst, void *src, uint32_t cnt)
  42. {
  43. uint16_t *src16 = src;
  44. while (cnt > 0) {
  45. dst->red = ((*src16 & pf->rmask) >> pf->rshift) << (8 - pf->rbits);
  46. dst->green = ((*src16 & pf->gmask) >> pf->gshift) << (8 - pf->gbits);
  47. dst->blue = ((*src16 & pf->bmask) >> pf->bshift) << (8 - pf->bbits);
  48. dst->alpha = ((*src16 & pf->amask) >> pf->ashift) << (8 - pf->abits);
  49. dst++, src16++, cnt--;
  50. }
  51. }
  52. /* assumes pf->{r,g,b,a}bits == 8 */
  53. static void conv_32_to_pixel_fast(PixelFormat *pf,
  54. QemuPixel *dst, void *src, uint32_t cnt)
  55. {
  56. uint32_t *src32 = src;
  57. while (cnt > 0) {
  58. dst->red = (*src32 & pf->rmask) >> pf->rshift;
  59. dst->green = (*src32 & pf->gmask) >> pf->gshift;
  60. dst->blue = (*src32 & pf->bmask) >> pf->bshift;
  61. dst->alpha = (*src32 & pf->amask) >> pf->ashift;
  62. dst++, src32++, cnt--;
  63. }
  64. }
  65. static void conv_32_to_pixel_generic(PixelFormat *pf,
  66. QemuPixel *dst, void *src, uint32_t cnt)
  67. {
  68. uint32_t *src32 = src;
  69. while (cnt > 0) {
  70. if (pf->rbits < 8) {
  71. dst->red = ((*src32 & pf->rmask) >> pf->rshift) << (8 - pf->rbits);
  72. } else {
  73. dst->red = ((*src32 & pf->rmask) >> pf->rshift) >> (pf->rbits - 8);
  74. }
  75. if (pf->gbits < 8) {
  76. dst->green = ((*src32 & pf->gmask) >> pf->gshift) << (8 - pf->gbits);
  77. } else {
  78. dst->green = ((*src32 & pf->gmask) >> pf->gshift) >> (pf->gbits - 8);
  79. }
  80. if (pf->bbits < 8) {
  81. dst->blue = ((*src32 & pf->bmask) >> pf->bshift) << (8 - pf->bbits);
  82. } else {
  83. dst->blue = ((*src32 & pf->bmask) >> pf->bshift) >> (pf->bbits - 8);
  84. }
  85. if (pf->abits < 8) {
  86. dst->alpha = ((*src32 & pf->amask) >> pf->ashift) << (8 - pf->abits);
  87. } else {
  88. dst->alpha = ((*src32 & pf->amask) >> pf->ashift) >> (pf->abits - 8);
  89. }
  90. dst++, src32++, cnt--;
  91. }
  92. }
  93. /* ----------------------------------------------------------------------- */
  94. /* QemuPixel -> PixelFormat conversions */
  95. static void conv_pixel_to_16(PixelFormat *pf,
  96. void *dst, QemuPixel *src, uint32_t cnt)
  97. {
  98. uint16_t *dst16 = dst;
  99. while (cnt > 0) {
  100. *dst16 = ((uint16_t)src->red >> (8 - pf->rbits)) << pf->rshift;
  101. *dst16 |= ((uint16_t)src->green >> (8 - pf->gbits)) << pf->gshift;
  102. *dst16 |= ((uint16_t)src->blue >> (8 - pf->bbits)) << pf->bshift;
  103. *dst16 |= ((uint16_t)src->alpha >> (8 - pf->abits)) << pf->ashift;
  104. dst16++, src++, cnt--;
  105. }
  106. }
  107. static void conv_pixel_to_32(PixelFormat *pf,
  108. void *dst, QemuPixel *src, uint32_t cnt)
  109. {
  110. uint32_t *dst32 = dst;
  111. while (cnt > 0) {
  112. *dst32 = ((uint32_t)src->red >> (8 - pf->rbits)) << pf->rshift;
  113. *dst32 |= ((uint32_t)src->green >> (8 - pf->gbits)) << pf->gshift;
  114. *dst32 |= ((uint32_t)src->blue >> (8 - pf->bbits)) << pf->bshift;
  115. *dst32 |= ((uint32_t)src->alpha >> (8 - pf->abits)) << pf->ashift;
  116. dst32++, src++, cnt--;
  117. }
  118. }
  119. /* ----------------------------------------------------------------------- */
  120. /* PixelFormat -> PixelFormat conversions */
  121. static void convert_copy(QemuPfConv *conv, void *dst, void *src, uint32_t cnt)
  122. {
  123. uint32_t bytes = cnt * conv->src.bytes_per_pixel;
  124. memcpy(dst, src, bytes);
  125. }
  126. static void convert_generic(QemuPfConv *conv, void *dst, void *src, uint32_t cnt)
  127. {
  128. if (conv->conv_cnt < cnt) {
  129. conv->conv_cnt = cnt;
  130. conv->conv_buf = g_realloc(conv->conv_buf, sizeof(QemuPixel) * conv->conv_cnt);
  131. }
  132. conv->conv_from(&conv->src, conv->conv_buf, src, cnt);
  133. conv->conv_to(&conv->dst, dst, conv->conv_buf, cnt);
  134. }
  135. /* ----------------------------------------------------------------------- */
  136. /* public interface */
  137. QemuPfConv *qemu_pf_conv_get(PixelFormat *dst, PixelFormat *src)
  138. {
  139. QemuPfConv *conv = g_malloc0(sizeof(QemuPfConv));
  140. conv->src = *src;
  141. conv->dst = *dst;
  142. if (memcmp(&conv->src, &conv->dst, sizeof(PixelFormat)) == 0) {
  143. /* formats identical, can simply copy */
  144. conv->convert = convert_copy;
  145. } else {
  146. /* generic two-step conversion: src -> QemuPixel -> dst */
  147. switch (conv->src.bytes_per_pixel) {
  148. case 2:
  149. conv->conv_from = conv_16_to_pixel;
  150. break;
  151. case 4:
  152. if (conv->src.rbits == 8 && conv->src.gbits == 8 && conv->src.bbits == 8) {
  153. conv->conv_from = conv_32_to_pixel_fast;
  154. } else {
  155. conv->conv_from = conv_32_to_pixel_generic;
  156. }
  157. break;
  158. default:
  159. goto err;
  160. }
  161. switch (conv->dst.bytes_per_pixel) {
  162. case 2:
  163. conv->conv_to = conv_pixel_to_16;
  164. break;
  165. case 4:
  166. conv->conv_to = conv_pixel_to_32;
  167. break;
  168. default:
  169. goto err;
  170. }
  171. conv->convert = convert_generic;
  172. }
  173. return conv;
  174. err:
  175. g_free(conv);
  176. return NULL;
  177. }
  178. void qemu_pf_conv_run(QemuPfConv *conv, void *dst, void *src, uint32_t cnt)
  179. {
  180. conv->convert(conv, dst, src, cnt);
  181. }
  182. void qemu_pf_conv_put(QemuPfConv *conv)
  183. {
  184. if (conv) {
  185. g_free(conv->conv_buf);
  186. g_free(conv);
  187. }
  188. }