summaryrefslogtreecommitdiff
path: root/pixman/src/icrect.c
blob: 057498f624efd343f084e8a1de4b04f666aeff70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * Copyright © 2000 Keith Packard
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Keith Packard not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Keith Packard makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

#include "icint.h"

typedef void	(*FillFunc) (pixman_image_t *dst,
			     int16_t	     xDst,
			     int16_t	     yDst,
			     uint16_t	     width,
			     uint16_t	     height,
			     pixman_bits_t  *pixel);


static void
pixman_fill_rect_1bpp (pixman_image_t *dst,
		       int16_t	       xDst,
		       int16_t	       yDst,
		       uint16_t	       width,
		       uint16_t	       height,
		       pixman_bits_t  *pixel)
{
    uint32_t value = *pixel ? 0xffffffff : 0;
    char *line;

    line = (char *)dst->pixels->data
	   + yDst * dst->pixels->stride;

    if ((width + xDst - 1) / 32 == xDst / 32) {
        uint32_t mask = 0;
        int pos = xDst / 32;
        int i;

        for (i = xDst; i < width; i++)
#if BITMAP_BIT_ORDER == MSBFirst
            mask |= 1 << (0x1f - i);
#else
            mask |= 1 << i;
#endif

        while (height-- > 0) {
            uint32_t *cur = (uint32_t *) line;
            cur [pos] = (cur [pos] & ~mask) | (value & mask);
	    line += dst->pixels->stride;
        }
    } else {
        uint32_t smask = 0, emask = 0;
        int end = ((xDst + width) / 32);
        int i;

        if (xDst % 32)
            for (i = (xDst % 32); i < 32; i++)
#if BITMAP_BIT_ORDER == MSBFirst
                smask |= 1 << (0x1f - i);
#else
                smask |= 1 << i;
#endif

        if ((width + xDst) % 32)
            for (i = 0; i < (width + xDst) % 32; i++)
#if BITMAP_BIT_ORDER == MSBFirst
                emask |= 1 << (0x1f - i);
#else
                emask |= 1 << i;
#endif

        while (height-- > 0) {
            uint32_t *cur = (uint32_t *) line;
            int start = (xDst / 32);

            if (smask) {
                cur [start] = (cur [start] & ~smask) | (value & smask);
                start++;
            }

            if (emask)
                cur [end] = (cur [end] & ~emask) | (value & emask);

            if (end > start)
                memset (cur + start, value, (end - start) * 4);
	    line += dst->pixels->stride;
        }
    }
}

static void
pixman_fill_rect_8bpp (pixman_image_t *dst,
		       int16_t	       xDst,
		       int16_t	       yDst,
		       uint16_t	       width,
		       uint16_t	       height,
		       pixman_bits_t  *pixel)
{
    int value = (int) (*pixel);
    char *line;

    line = (char *)dst->pixels->data +
	xDst + yDst * dst->pixels->stride;
    while (height-- > 0) {
	memset (line, value, width);
	line += dst->pixels->stride;
    }
}

static void
pixman_fill_rect_32bpp (pixman_image_t *dst,
			int16_t	        xDst,
			int16_t	        yDst, 
			uint16_t	width,
			uint16_t	height,
			pixman_bits_t  *pixel)
{
    uint32_t int_pixel;
    char *line;
    char *data;
    int w;

    line = (char *)dst->pixels->data +
	xDst * 4 + yDst * dst->pixels->stride;
     
    int_pixel = *(uint32_t *)pixel;
    while (height-- > 0) {
	data = line;
	w = width;
	while (w-- > 0) {
	    *(uint32_t *)data = int_pixel;
	    data += 4;
	}
	line += dst->pixels->stride;
    }
}

static void
pixman_fill_rect_general (pixman_image_t *dst,
			  int16_t	  xDst,
			  int16_t	  yDst,
			  uint16_t	  width,
			  uint16_t	  height,
			  pixman_bits_t  *pixel)
{
    int pixel_size;
    char *line;
    char *data;
    int w;

    pixel_size = dst->pixels->bpp >> 3;

    line = (char *)dst->pixels->data +
	xDst * pixel_size + yDst * dst->pixels->stride;
     
    while (height-- > 0) {
	data = line;
	w = width;
	while (w-- > 0) {
	    memcpy (data, pixel, pixel_size);
	    data += pixel_size;
	}
	line += dst->pixels->stride;
    }
}


static void
pixman_color_rects (pixman_image_t	 *dst,
	      pixman_image_t	 *clipPict,
	      pixman_color_t	 *color,
	      int	 nRect,
	      pixman_rectangle_t *rects,
	      int	 xoff,
	      int	 yoff)
{
    pixman_bits_t	pixel;
    pixman_region16_t  *clip;
    pixman_region16_t  *rects_as_region;
    pixman_box16_t     *clipped_rects;
    int	                i, n_clipped_rects;
    FillFunc            func;

    pixman_color_to_pixel (&dst->image_format,
			   color,
			   &pixel);

    /* offset to the right place on the destination image */
    xoff -= dst->pixels->x;
    yoff -= dst->pixels->y;
    
    clip = pixman_region_create();
    pixman_region_union_rect (clip, clip,
			      dst->pixels->x, dst->pixels->y,
			      dst->pixels->width, dst->pixels->height);

    pixman_region_intersect (clip, clip, clipPict->pCompositeClip);
    if (clipPict->alphaMap)
    {
	pixman_region_translate (clip, 
				 -clipPict->alphaOrigin.x,
				 -clipPict->alphaOrigin.y);
	pixman_region_intersect (clip, clip, clipPict->alphaMap->pCompositeClip);
	pixman_region_translate (clip, 
				 clipPict->alphaOrigin.x,
				 clipPict->alphaOrigin.y);
    }

    if (xoff || yoff)
    {
	for (i = 0; i < nRect; i++)
	{
	    rects[i].x -= xoff;
	    rects[i].y -= yoff;
	}
    }

    rects_as_region = pixman_region_create ();
    for (i = 0; i < nRect; i++)
    {
	pixman_region_union_rect (rects_as_region, rects_as_region,
				  rects[i].x, rects[i].y,
				  rects[i].width, rects[i].height);
    }

    pixman_region_intersect (rects_as_region, rects_as_region, clip);
    pixman_region_destroy (clip);

    n_clipped_rects = pixman_region_num_rects (rects_as_region);
    clipped_rects = pixman_region_rects (rects_as_region);

    if (dst->pixels->bpp == 8)
	func = pixman_fill_rect_8bpp;
    else if (dst->pixels->bpp == 32)
	func = pixman_fill_rect_32bpp;
    else if (dst->pixels->bpp == 1)
	func = pixman_fill_rect_1bpp;
    else 
	func = pixman_fill_rect_general;
    
    for (i = 0; i < n_clipped_rects; i++) {
	(*func) (dst,
		 clipped_rects[i].x1, 
		 clipped_rects[i].y1, 
		 clipped_rects[i].x2 - clipped_rects[i].x1, 
		 clipped_rects[i].y2 - clipped_rects[i].y1,
		 &pixel);
    }

    pixman_region_destroy (rects_as_region);

    if (xoff || yoff)
    {
	for (i = 0; i < nRect; i++)
	{
	    rects[i].x += xoff;
	    rects[i].y += yoff;
	}
    }
}

void pixman_fill_rectangle (pixman_operator_t	op,
		      pixman_image_t		*dst,
		      const pixman_color_t	*color,
		      int		x,
		      int		y,
		      unsigned int	width,
		      unsigned int	height)
{
    pixman_rectangle_t rect;

    rect.x = x;
    rect.y = y;
    rect.width = width;
    rect.height = height;

    pixman_fill_rectangles (op, dst, color, &rect, 1);
}

void
pixman_fill_rectangles (pixman_operator_t		op,
		  pixman_image_t		*dst,
		  const pixman_color_t		*color,
		  const pixman_rectangle_t	*rects,
		  int			nRects)
{
    pixman_color_t color_s = *color;

    if (color_s.alpha == 0xffff)
    {
	if (op == PIXMAN_OPERATOR_OVER)
	    op = PIXMAN_OPERATOR_SRC;
    }
    if (op == PIXMAN_OPERATOR_CLEAR)
	color_s.red = color_s.green = color_s.blue = color_s.alpha = 0;

    if (op == PIXMAN_OPERATOR_SRC || op == PIXMAN_OPERATOR_CLEAR)
    {
      /* We cast away the constness of rects here, because pixman_color_rects
	 temporarily modifies it */
	pixman_color_rects (dst, dst, &color_s, nRects, (pixman_rectangle_t *)rects, 0, 0);
	if (dst->alphaMap)
	    pixman_color_rects (dst->alphaMap, dst,
			  &color_s, nRects, (pixman_rectangle_t *)rects,
			  dst->alphaOrigin.x,
			  dst->alphaOrigin.y);
    }
    else
    {
	pixman_format_t	rgbaFormat;
	FbPixels	*pixels;
	pixman_image_t		*src;
	pixman_bits_t		pixel;

	pixman_format_init (&rgbaFormat, PICT_a8r8g8b8);
	
	pixels = FbPixelsCreate (1, 1, rgbaFormat.depth);
	if (!pixels)
	    goto bail1;
	
	pixman_color_to_pixel (&rgbaFormat, &color_s, &pixel);

	/* XXX: Originally, fb had the following:

	   (*pGC->ops->PolyFillRect) (&pPixmap->drawable, pGC, 1, &one);

	   I haven't checked to see what I might be breaking with a
	   trivial assignment instead.
	*/
	pixels->data[0] = pixel;

	src = pixman_image_createForPixels (pixels, &rgbaFormat);
	if (!src)
	    goto bail2;

	pixman_image_set_repeat (src, 1);

	while (nRects--)
	{
	    pixman_composite (op, src, NULL, dst, 0, 0, 0, 0, 
			 rects->x,
			 rects->y,
			 rects->width,
			 rects->height);
	    rects++;
	}

	pixman_image_destroy (src);
bail2:
	FbPixelsDestroy (pixels);
bail1:
	;
    }
}
slim_hidden_def(pixman_fill_rectangles);