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