os: if inet_ntop() is available, use it for IPv4 addresses as well
[xserver.git] / glamor / glamor_compositerects.c
blob0796fed41f55111e25f9adc14c9a58e00f4672ba
1 /*
2 * Copyright © 2009 Intel Corporation
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:
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.
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.
23 * Authors:
24 * Zhigang Gong <zhigang.gong@linux.intel.com>
26 * original author is Chris Wilson at sna.
30 #include "glamor_priv.h"
31 #include "mipict.h"
32 #include "damage.h"
34 /** @file glamor_compositerects.
36 * compositeRects acceleration implementation
39 static int16_t
40 bound(int16_t a, uint16_t b)
42 int v = (int) a + (int) b;
44 if (v > MAXSHORT)
45 return MAXSHORT;
46 return v;
49 static Bool
50 _pixman_region_init_clipped_rectangles(pixman_region16_t * region,
51 unsigned int num_rects,
52 xRectangle *rects,
53 int tx, int ty, BoxPtr extents)
55 pixman_box16_t stack_boxes[64], *boxes = stack_boxes;
56 pixman_bool_t ret;
57 unsigned int i, j;
59 if (num_rects > ARRAY_SIZE(stack_boxes)) {
60 boxes = xallocarray(num_rects, sizeof(pixman_box16_t));
61 if (boxes == NULL)
62 return FALSE;
65 for (i = j = 0; i < num_rects; i++) {
66 boxes[j].x1 = rects[i].x + tx;
67 if (boxes[j].x1 < extents->x1)
68 boxes[j].x1 = extents->x1;
70 boxes[j].y1 = rects[i].y + ty;
71 if (boxes[j].y1 < extents->y1)
72 boxes[j].y1 = extents->y1;
74 boxes[j].x2 = bound(rects[i].x + tx, rects[i].width);
75 if (boxes[j].x2 > extents->x2)
76 boxes[j].x2 = extents->x2;
78 boxes[j].y2 = bound(rects[i].y + ty, rects[i].height);
79 if (boxes[j].y2 > extents->y2)
80 boxes[j].y2 = extents->y2;
82 if (boxes[j].x2 > boxes[j].x1 && boxes[j].y2 > boxes[j].y1)
83 j++;
86 ret = FALSE;
87 if (j)
88 ret = pixman_region_init_rects(region, boxes, j);
90 if (boxes != stack_boxes)
91 free(boxes);
93 DEBUGF("%s: nrects=%d, region=(%d, %d), (%d, %d) x %d\n",
94 __FUNCTION__, num_rects,
95 region->extents.x1, region->extents.y1,
96 region->extents.x2, region->extents.y2, j);
97 return ret;
100 void
101 glamor_composite_rectangles(CARD8 op,
102 PicturePtr dst,
103 xRenderColor * color,
104 int num_rects, xRectangle *rects)
106 PixmapPtr pixmap;
107 struct glamor_pixmap_private *priv;
108 pixman_region16_t region;
109 pixman_box16_t *boxes;
110 int num_boxes;
111 PicturePtr source = NULL;
112 Bool need_free_region = FALSE;
114 DEBUGF("%s(op=%d, %08x x %d [(%d, %d)x(%d, %d) ...])\n",
115 __FUNCTION__, op,
116 (color->alpha >> 8 << 24) |
117 (color->red >> 8 << 16) |
118 (color->green >> 8 << 8) |
119 (color->blue >> 8 << 0),
120 num_rects, rects[0].x, rects[0].y, rects[0].width, rects[0].height);
122 if (!num_rects)
123 return;
125 if (RegionNil(dst->pCompositeClip)) {
126 DEBUGF("%s: empty clip, skipping\n", __FUNCTION__);
127 return;
130 if ((color->red | color->green | color->blue | color->alpha) <= 0x00ff) {
131 switch (op) {
132 case PictOpOver:
133 case PictOpOutReverse:
134 case PictOpAdd:
135 return;
136 case PictOpInReverse:
137 case PictOpSrc:
138 op = PictOpClear;
139 break;
140 case PictOpAtopReverse:
141 op = PictOpOut;
142 break;
143 case PictOpXor:
144 op = PictOpOverReverse;
145 break;
148 if (color->alpha <= 0x00ff) {
149 switch (op) {
150 case PictOpOver:
151 case PictOpOutReverse:
152 return;
153 case PictOpInReverse:
154 op = PictOpClear;
155 break;
156 case PictOpAtopReverse:
157 op = PictOpOut;
158 break;
159 case PictOpXor:
160 op = PictOpOverReverse;
161 break;
164 else if (color->alpha >= 0xff00) {
165 switch (op) {
166 case PictOpOver:
167 op = PictOpSrc;
168 break;
169 case PictOpInReverse:
170 return;
171 case PictOpOutReverse:
172 op = PictOpClear;
173 break;
174 case PictOpAtopReverse:
175 op = PictOpOverReverse;
176 break;
177 case PictOpXor:
178 op = PictOpOut;
179 break;
182 DEBUGF("%s: converted to op %d\n", __FUNCTION__, op);
184 if (!_pixman_region_init_clipped_rectangles(&region,
185 num_rects, rects,
186 dst->pDrawable->x,
187 dst->pDrawable->y,
188 &dst->pCompositeClip->extents))
190 DEBUGF("%s: allocation failed for region\n", __FUNCTION__);
191 return;
194 pixmap = glamor_get_drawable_pixmap(dst->pDrawable);
195 priv = glamor_get_pixmap_private(pixmap);
197 if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(priv))
198 goto fallback;
199 if (dst->alphaMap) {
200 DEBUGF("%s: fallback, dst has an alpha-map\n", __FUNCTION__);
201 goto fallback;
204 need_free_region = TRUE;
206 DEBUGF("%s: drawable extents (%d, %d),(%d, %d) x %d\n",
207 __FUNCTION__,
208 RegionExtents(&region)->x1, RegionExtents(&region)->y1,
209 RegionExtents(&region)->x2, RegionExtents(&region)->y2,
210 RegionNumRects(&region));
212 if (dst->pCompositeClip->data &&
213 (!pixman_region_intersect(&region, &region, dst->pCompositeClip) ||
214 RegionNil(&region))) {
215 DEBUGF("%s: zero-intersection between rectangles and clip\n",
216 __FUNCTION__);
217 pixman_region_fini(&region);
218 return;
221 DEBUGF("%s: clipped extents (%d, %d),(%d, %d) x %d\n",
222 __FUNCTION__,
223 RegionExtents(&region)->x1, RegionExtents(&region)->y1,
224 RegionExtents(&region)->x2, RegionExtents(&region)->y2,
225 RegionNumRects(&region));
227 boxes = pixman_region_rectangles(&region, &num_boxes);
228 if (op == PictOpSrc || op == PictOpClear) {
229 CARD32 pixel;
231 pixman_region_translate(&region, -dst->pDrawable->x, -dst->pDrawable->y);
233 DEBUGF("%s: drawable extents (%d, %d),(%d, %d)\n",
234 __FUNCTION__, dst_x, dst_y,
235 RegionExtents(&region)->x1, RegionExtents(&region)->y1,
236 RegionExtents(&region)->x2, RegionExtents(&region)->y2);
238 if (op == PictOpClear)
239 pixel = 0;
240 else
241 miRenderColorToPixel(dst->pFormat, color, &pixel);
242 glamor_solid_boxes(dst->pDrawable, boxes, num_boxes, pixel);
244 goto done;
246 else {
247 if (_X_LIKELY(glamor_pixmap_priv_is_small(priv))) {
248 int error;
250 source = CreateSolidPicture(0, color, &error);
251 if (!source)
252 goto done;
253 if (glamor_composite_clipped_region(op, source,
254 NULL, dst,
255 NULL, NULL, pixmap,
256 &region, 0, 0, 0, 0, 0, 0))
257 goto done;
260 fallback:
261 miCompositeRects(op, dst, color, num_rects, rects);
262 done:
263 /* XXX xserver-1.8: CompositeRects is not tracked by Damage, so we must
264 * manually append the damaged regions ourselves.
266 DamageRegionAppend(&pixmap->drawable, &region);
267 DamageRegionProcessPending(&pixmap->drawable);
269 if (need_free_region)
270 pixman_region_fini(&region);
271 if (source)
272 FreePicture(source, 0);
273 return;