glamor_compositerects.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright © 2009 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Zhigang Gong <zhigang.gong@linux.intel.com>
  25. *
  26. * original author is Chris Wilson at sna.
  27. *
  28. */
  29. #include "glamor_priv.h"
  30. #include "mipict.h"
  31. #include "damage.h"
  32. /** @file glamor_compositerects.
  33. *
  34. * compositeRects acceleration implementation
  35. */
  36. static int16_t
  37. bound(int16_t a, uint16_t b)
  38. {
  39. int v = (int) a + (int) b;
  40. if (v > MAXSHORT)
  41. return MAXSHORT;
  42. return v;
  43. }
  44. static Bool
  45. _pixman_region_init_clipped_rectangles(pixman_region16_t * region,
  46. unsigned int num_rects,
  47. xRectangle *rects,
  48. int tx, int ty, BoxPtr extents)
  49. {
  50. pixman_box16_t stack_boxes[64], *boxes = stack_boxes;
  51. pixman_bool_t ret;
  52. unsigned int i, j;
  53. if (num_rects > ARRAY_SIZE(stack_boxes)) {
  54. boxes = malloc(sizeof(pixman_box16_t) * num_rects);
  55. if (boxes == NULL)
  56. return FALSE;
  57. }
  58. for (i = j = 0; i < num_rects; i++) {
  59. boxes[j].x1 = rects[i].x + tx;
  60. if (boxes[j].x1 < extents->x1)
  61. boxes[j].x1 = extents->x1;
  62. boxes[j].y1 = rects[i].y + ty;
  63. if (boxes[j].y1 < extents->y1)
  64. boxes[j].y1 = extents->y1;
  65. boxes[j].x2 = bound(rects[i].x + tx, rects[i].width);
  66. if (boxes[j].x2 > extents->x2)
  67. boxes[j].x2 = extents->x2;
  68. boxes[j].y2 = bound(rects[i].y + ty, rects[i].height);
  69. if (boxes[j].y2 > extents->y2)
  70. boxes[j].y2 = extents->y2;
  71. if (boxes[j].x2 > boxes[j].x1 && boxes[j].y2 > boxes[j].y1)
  72. j++;
  73. }
  74. ret = FALSE;
  75. if (j)
  76. ret = pixman_region_init_rects(region, boxes, j);
  77. if (boxes != stack_boxes)
  78. free(boxes);
  79. DEBUGF("%s: nrects=%d, region=(%d, %d), (%d, %d) x %d\n",
  80. __FUNCTION__, num_rects,
  81. region->extents.x1, region->extents.y1,
  82. region->extents.x2, region->extents.y2, j);
  83. return ret;
  84. }
  85. void
  86. glamor_composite_rectangles(CARD8 op,
  87. PicturePtr dst,
  88. xRenderColor * color,
  89. int num_rects, xRectangle *rects)
  90. {
  91. PixmapPtr pixmap;
  92. struct glamor_pixmap_private *priv;
  93. pixman_region16_t region;
  94. pixman_box16_t *boxes;
  95. int dst_x, dst_y;
  96. int num_boxes;
  97. PicturePtr source = NULL;
  98. Bool need_free_region = FALSE;
  99. DEBUGF("%s(op=%d, %08x x %d [(%d, %d)x(%d, %d) ...])\n",
  100. __FUNCTION__, op,
  101. (color->alpha >> 8 << 24) |
  102. (color->red >> 8 << 16) |
  103. (color->green >> 8 << 8) |
  104. (color->blue >> 8 << 0),
  105. num_rects, rects[0].x, rects[0].y, rects[0].width, rects[0].height);
  106. if (!num_rects)
  107. return;
  108. if (RegionNil(dst->pCompositeClip)) {
  109. DEBUGF("%s: empty clip, skipping\n", __FUNCTION__);
  110. return;
  111. }
  112. if ((color->red | color->green | color->blue | color->alpha) <= 0x00ff) {
  113. switch (op) {
  114. case PictOpOver:
  115. case PictOpOutReverse:
  116. case PictOpAdd:
  117. return;
  118. case PictOpInReverse:
  119. case PictOpSrc:
  120. op = PictOpClear;
  121. break;
  122. case PictOpAtopReverse:
  123. op = PictOpOut;
  124. break;
  125. case PictOpXor:
  126. op = PictOpOverReverse;
  127. break;
  128. }
  129. }
  130. if (color->alpha <= 0x00ff) {
  131. switch (op) {
  132. case PictOpOver:
  133. case PictOpOutReverse:
  134. return;
  135. case PictOpInReverse:
  136. op = PictOpClear;
  137. break;
  138. case PictOpAtopReverse:
  139. op = PictOpOut;
  140. break;
  141. case PictOpXor:
  142. op = PictOpOverReverse;
  143. break;
  144. }
  145. }
  146. else if (color->alpha >= 0xff00) {
  147. switch (op) {
  148. case PictOpOver:
  149. op = PictOpSrc;
  150. break;
  151. case PictOpInReverse:
  152. return;
  153. case PictOpOutReverse:
  154. op = PictOpClear;
  155. break;
  156. case PictOpAtopReverse:
  157. op = PictOpOverReverse;
  158. break;
  159. case PictOpXor:
  160. op = PictOpOut;
  161. break;
  162. }
  163. }
  164. DEBUGF("%s: converted to op %d\n", __FUNCTION__, op);
  165. if (!_pixman_region_init_clipped_rectangles(&region,
  166. num_rects, rects,
  167. dst->pDrawable->x,
  168. dst->pDrawable->y,
  169. &dst->pCompositeClip->extents))
  170. {
  171. DEBUGF("%s: allocation failed for region\n", __FUNCTION__);
  172. return;
  173. }
  174. pixmap = glamor_get_drawable_pixmap(dst->pDrawable);
  175. priv = glamor_get_pixmap_private(pixmap);
  176. if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(priv))
  177. goto fallback;
  178. if (dst->alphaMap) {
  179. DEBUGF("%s: fallback, dst has an alpha-map\n", __FUNCTION__);
  180. goto fallback;
  181. }
  182. need_free_region = TRUE;
  183. DEBUGF("%s: drawable extents (%d, %d),(%d, %d) x %d\n",
  184. __FUNCTION__,
  185. RegionExtents(&region)->x1, RegionExtents(&region)->y1,
  186. RegionExtents(&region)->x2, RegionExtents(&region)->y2,
  187. RegionNumRects(&region));
  188. if (dst->pCompositeClip->data &&
  189. (!pixman_region_intersect(&region, &region, dst->pCompositeClip) ||
  190. RegionNil(&region))) {
  191. DEBUGF("%s: zero-intersection between rectangles and clip\n",
  192. __FUNCTION__);
  193. pixman_region_fini(&region);
  194. return;
  195. }
  196. DEBUGF("%s: clipped extents (%d, %d),(%d, %d) x %d\n",
  197. __FUNCTION__,
  198. RegionExtents(&region)->x1, RegionExtents(&region)->y1,
  199. RegionExtents(&region)->x2, RegionExtents(&region)->y2,
  200. RegionNumRects(&region));
  201. glamor_get_drawable_deltas(dst->pDrawable, pixmap, &dst_x, &dst_y);
  202. pixman_region_translate(&region, dst_x, dst_y);
  203. DEBUGF("%s: pixmap +(%d, %d) extents (%d, %d),(%d, %d)\n",
  204. __FUNCTION__, dst_x, dst_y,
  205. RegionExtents(&region)->x1, RegionExtents(&region)->y1,
  206. RegionExtents(&region)->x2, RegionExtents(&region)->y2);
  207. boxes = pixman_region_rectangles(&region, &num_boxes);
  208. if (op == PictOpSrc || op == PictOpClear) {
  209. CARD32 pixel;
  210. if (op == PictOpClear)
  211. pixel = 0;
  212. else
  213. miRenderColorToPixel(dst->pFormat, color, &pixel);
  214. glamor_solid_boxes(pixmap, boxes, num_boxes, pixel);
  215. goto done;
  216. }
  217. else {
  218. if (_X_LIKELY(priv->type != GLAMOR_TEXTURE_LARGE)) {
  219. int error;
  220. source = CreateSolidPicture(0, color, &error);
  221. if (!source)
  222. goto done;
  223. if (glamor_composite_clipped_region(op, source,
  224. NULL, dst,
  225. NULL, NULL, priv,
  226. &region, 0, 0, 0, 0, 0, 0))
  227. goto done;
  228. }
  229. }
  230. fallback:
  231. miCompositeRects(op, dst, color, num_rects, rects);
  232. done:
  233. /* XXX xserver-1.8: CompositeRects is not tracked by Damage, so we must
  234. * manually append the damaged regions ourselves.
  235. */
  236. DamageRegionAppend(&pixmap->drawable, &region);
  237. DamageRegionProcessPending(&pixmap->drawable);
  238. if (need_free_region)
  239. pixman_region_fini(&region);
  240. if (source)
  241. FreePicture(source, 0);
  242. return;
  243. }