Optimize fill rectangles in the op=PIXMAN_OP_CLEAR case
[profile/ivi/pixman.git] / pixman / pixman-image.c
1 /*
2  * Copyright © 2000 SuSE, Inc.
3  * Copyright © 2007 Red Hat, Inc.
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and its
6  * documentation for any purpose is hereby granted without fee, provided that
7  * the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of SuSE not be used in advertising or
10  * publicity pertaining to distribution of the software without specific,
11  * written prior permission.  SuSE makes no representations about the
12  * suitability of this software for any purpose.  It is provided "as is"
13  * without express or implied warranty.
14  *
15  * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE
17  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
19  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include <config.h>
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "pixman.h"
30 #include "pixman-private.h"
31
32 static void
33 init_source_image (source_image_t *image)
34 {
35     image->class = SOURCE_IMAGE_CLASS_UNKNOWN;
36 }
37
38 static pixman_bool_t
39 init_gradient (gradient_t     *gradient,
40                const pixman_gradient_stop_t *stops,
41                int             n_stops)
42 {
43     return_val_if_fail (n_stops > 0, FALSE);
44
45     init_source_image (&gradient->common);
46
47     gradient->stops = malloc (n_stops * sizeof (pixman_gradient_stop_t));
48     if (!gradient->stops)
49         return FALSE;
50
51     memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
52     
53     gradient->n_stops = n_stops;
54
55     gradient->stop_range = 0xffff;
56     gradient->color_table = NULL;
57     gradient->color_table_size = 0;
58
59     return TRUE;
60 }
61
62 static uint32_t
63 color_to_uint32 (const pixman_color_t *color)
64 {
65     return
66         (color->alpha >> 8 << 24) |
67         (color->red >> 8 << 16) |
68         (color->green & 0xff00) |
69         (color->blue >> 8);
70 }
71
72 static pixman_image_t *
73 allocate_image (void)
74 {
75     pixman_image_t *image = malloc (sizeof (pixman_image_t));
76     
77     if (image)
78     {
79         image_common_t *common = &image->common;
80
81         pixman_region_init (&common->full_region);
82         pixman_region_init (&common->clip_region);
83         common->src_clip = &common->full_region;
84         common->has_client_clip = FALSE;
85         common->transform = NULL;
86         common->repeat = PIXMAN_REPEAT_NONE;
87         common->filter = PIXMAN_FILTER_NEAREST;
88         common->filter_params = NULL;
89         common->n_filter_params = 0;
90         common->alpha_map = NULL;
91         common->component_alpha = FALSE;
92         common->ref_count = 1;
93         common->read_func = NULL;
94         common->write_func = NULL;
95     }
96
97     return image;
98 }
99
100 /* Ref Counting */
101 pixman_image_t *
102 pixman_image_ref (pixman_image_t *image)
103 {
104     image->common.ref_count++;
105
106     return image;
107 }
108
109 void
110 pixman_image_unref (pixman_image_t *image)
111 {
112     image_common_t *common = (image_common_t *)image;
113
114     common->ref_count--;
115
116     if (common->ref_count == 0)
117     {
118         pixman_region_fini (&common->clip_region);
119         pixman_region_fini (&common->full_region);
120
121         if (common->transform)
122             free (common->transform);
123
124         if (common->filter_params)
125             free (common->filter_params);
126
127         if (common->alpha_map)
128             pixman_image_unref ((pixman_image_t *)common->alpha_map);
129
130 #if 0
131         if (image->type == BITS && image->bits.indexed)
132             free (image->bits.indexed);
133 #endif
134         
135 #if 0
136         memset (image, 0xaa, sizeof (pixman_image_t));
137 #endif
138         if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
139         {
140             if (image->gradient.stops)
141                 free (image->gradient.stops);
142         }
143
144         
145         if (image->type == BITS && image->bits.free_me)
146             free (image->bits.free_me);
147         
148         free (image);
149     }
150 }
151
152 /* Constructors */
153 pixman_image_t *
154 pixman_image_create_solid_fill (pixman_color_t *color)
155 {
156     pixman_image_t *img = allocate_image();
157     if (!img)
158         return NULL;
159     
160     init_source_image (&img->solid.common);
161     
162     img->type = SOLID;
163     img->solid.color = color_to_uint32 (color);
164
165     return img;
166 }
167
168 pixman_image_t *
169 pixman_image_create_linear_gradient (pixman_point_fixed_t         *p1,
170                                      pixman_point_fixed_t         *p2,
171                                      const pixman_gradient_stop_t *stops,
172                                      int                           n_stops)
173 {
174     pixman_image_t *image;
175     linear_gradient_t *linear;
176
177     return_val_if_fail (n_stops >= 2, NULL);
178     
179     image = allocate_image();
180     
181     if (!image)
182         return NULL;
183
184     linear = &image->linear;
185     
186     if (!init_gradient (&linear->common, stops, n_stops))
187     {
188         free (image);
189         return NULL;
190     }
191
192     linear->p1 = *p1;
193     linear->p2 = *p2;
194
195     image->type = LINEAR;
196
197     return image;
198 }
199
200
201 pixman_image_t *
202 pixman_image_create_radial_gradient (pixman_point_fixed_t         *inner,
203                                      pixman_point_fixed_t         *outer,
204                                      pixman_fixed_t                inner_radius,
205                                      pixman_fixed_t                outer_radius,
206                                      const pixman_gradient_stop_t *stops,
207                                      int                           n_stops)
208 {
209     pixman_image_t *image;
210     radial_gradient_t *radial;
211
212     return_val_if_fail (n_stops >= 2, NULL);
213     
214     image = allocate_image();
215
216     if (!image)
217         return NULL;
218
219     radial = &image->radial;
220
221     if (!init_gradient (&radial->common, stops, n_stops))
222     {
223         free (image);
224         return NULL;
225     }
226
227     image->type = RADIAL;
228     
229     radial->c1.x = inner->x;
230     radial->c1.y = inner->y;
231     radial->c1.radius = inner_radius;
232     radial->c2.x = outer->x;
233     radial->c2.y = outer->y;
234     radial->c2.radius = outer_radius;
235     radial->cdx = pixman_fixed_to_double (radial->c2.x - radial->c1.x);
236     radial->cdy = pixman_fixed_to_double (radial->c2.y - radial->c1.y);
237     radial->dr = pixman_fixed_to_double (radial->c2.radius - radial->c1.radius);
238     radial->A = (radial->cdx * radial->cdx
239                  + radial->cdy * radial->cdy
240                  - radial->dr  * radial->dr);
241     
242     return image;
243 }
244
245 pixman_image_t *
246 pixman_image_create_conical_gradient (pixman_point_fixed_t *center,
247                                       pixman_fixed_t angle,
248                                       const pixman_gradient_stop_t *stops,
249                                       int n_stops)
250 {
251     pixman_image_t *image = allocate_image();
252     conical_gradient_t *conical;
253
254     if (!image)
255         return NULL;
256
257     conical = &image->conical;
258     
259     if (!init_gradient (&conical->common, stops, n_stops))
260     {
261         free (image);
262         return NULL;
263     }
264
265     image->type = CONICAL;
266     conical->center = *center;
267     conical->angle = angle;
268
269     return image;
270 }
271
272 static uint32_t *
273 create_bits (pixman_format_code_t format,
274              int                  width,
275              int                  height,
276              int                 *rowstride_bytes)
277 {
278     int stride;
279     int buf_size;
280     int bpp;
281     
282     bpp = PIXMAN_FORMAT_BPP (format);
283     stride = ((width * bpp + FB_MASK) >> FB_SHIFT) * sizeof (uint32_t);
284     buf_size = height * stride;
285
286     if (rowstride_bytes)
287         *rowstride_bytes = stride;
288
289     return calloc (buf_size, 1);
290 }
291
292 static void
293 reset_clip_region (pixman_image_t *image)
294 {
295     pixman_region_fini (&image->common.clip_region);
296     
297     if (image->type == BITS)
298     {
299         pixman_region_init_rect (&image->common.clip_region, 0, 0,
300                                  image->bits.width, image->bits.height);        
301     }
302     else
303     {
304         pixman_region_init (&image->common.clip_region);
305     }
306 }
307
308 pixman_image_t *
309 pixman_image_create_bits (pixman_format_code_t  format,
310                           int                   width,
311                           int                   height,
312                           uint32_t             *bits,
313                           int                   rowstride_bytes)
314 {
315     pixman_image_t *image;
316     uint32_t *free_me = NULL;
317
318     /* must be a whole number of uint32_t's 
319      */
320     return_val_if_fail (bits == NULL ||
321                         (rowstride_bytes % sizeof (uint32_t)) == 0, NULL); 
322
323     if (!bits)
324     {
325         free_me = bits = create_bits (format, width, height, &rowstride_bytes);
326         if (!bits)
327             return NULL;
328     }
329     
330     image = allocate_image();
331
332     if (!image)
333         return NULL;
334     
335     image->type = BITS;
336     image->bits.format = format;
337     image->bits.width = width;
338     image->bits.height = height;
339     image->bits.bits = bits;
340     image->bits.free_me = free_me;
341     
342     image->bits.rowstride = rowstride_bytes / sizeof (uint32_t); /* we store it in number
343                                                                   * of uint32_t's
344                                                                   */
345     image->bits.indexed = NULL;
346
347     pixman_region_fini (&image->common.full_region);
348     pixman_region_init_rect (&image->common.full_region, 0, 0,
349                              image->bits.width, image->bits.height);
350
351     reset_clip_region (image);
352     return image;
353 }
354
355 pixman_bool_t
356 pixman_image_set_clip_region (pixman_image_t    *image,
357                               pixman_region16_t *region)
358 {
359     image_common_t *common = (image_common_t *)image;
360
361     if (region)
362     {
363         return pixman_region_copy (&common->clip_region, region);
364     }
365     else
366     {
367         reset_clip_region (image);
368         
369         return TRUE;
370     }
371 }
372
373 /* Sets whether the clip region includes a clip region set by the client
374  */
375 void
376 pixman_image_set_has_client_clip (pixman_image_t *image,
377                                   pixman_bool_t   client_clip)
378 {
379     image->common.has_client_clip = client_clip;
380 }
381
382 pixman_bool_t
383 pixman_image_set_transform (pixman_image_t           *image,
384                             const pixman_transform_t *transform)
385 {
386     static const pixman_transform_t id =
387     {
388         { { pixman_fixed_1, 0, 0 },
389           { 0, pixman_fixed_1, 0 },
390           { 0, 0, pixman_fixed_1 }
391         }
392     };
393     
394     image_common_t *common = (image_common_t *)image;
395
396     if (common->transform == transform)
397         return TRUE;
398
399     if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
400     {
401         transform = NULL;
402         return TRUE;
403     }
404     
405     if (common->transform)
406         free (common->transform);
407
408     if (transform)
409     {
410         common->transform = malloc (sizeof (pixman_transform_t));
411         if (!common->transform)
412             return FALSE;
413
414         *common->transform = *transform;
415     }
416     else
417     {
418         common->transform = NULL;
419     }
420
421     return TRUE;
422 }
423
424 void
425 pixman_image_set_repeat (pixman_image_t  *image,
426                          pixman_repeat_t  repeat)
427 {
428     image->common.repeat = repeat;
429 }
430
431 pixman_bool_t 
432 pixman_image_set_filter (pixman_image_t       *image,
433                          pixman_filter_t       filter,
434                          const pixman_fixed_t *params,
435                          int                   n_params)
436 {
437     image_common_t *common = (image_common_t *)image;
438     pixman_fixed_t *new_params;
439
440     if (params == common->filter_params && filter == common->filter)
441         return TRUE;
442
443     new_params = NULL;
444     if (params)
445     {
446         new_params = malloc (n_params * sizeof (pixman_fixed_t));
447         if (!new_params)
448             return FALSE;
449
450         memcpy (new_params,
451                 params, n_params * sizeof (pixman_fixed_t));
452     }
453
454     common->filter = filter;
455         
456     if (common->filter_params)
457         free (common->filter_params);
458
459     common->filter_params = new_params;
460     common->n_filter_params = n_params;
461     return TRUE;
462 }
463
464 /* Unlike all the other property setters, this function does not
465  * copy the content of indexed. Doing this copying is simply
466  * way, way too expensive.
467  */
468 void
469 pixman_image_set_indexed (pixman_image_t         *image,
470                           const pixman_indexed_t *indexed)
471 {
472     bits_image_t *bits = (bits_image_t *)image;
473
474     bits->indexed = indexed;
475 }
476
477 void
478 pixman_image_set_alpha_map (pixman_image_t *image,
479                             pixman_image_t *alpha_map,
480                             int16_t         x,
481                             int16_t         y)
482 {
483     image_common_t *common = (image_common_t *)image;
484     
485     return_if_fail (!alpha_map || alpha_map->type == BITS);
486
487     if (common->alpha_map != (bits_image_t *)alpha_map)
488     {
489         if (common->alpha_map)
490             pixman_image_unref ((pixman_image_t *)common->alpha_map);
491
492         if (alpha_map)
493             common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
494         else
495             common->alpha_map = NULL;
496     }
497
498     common->alpha_origin.x = x;
499     common->alpha_origin.y = y;
500 }
501
502 void
503 pixman_image_set_component_alpha   (pixman_image_t       *image,
504                                     pixman_bool_t         component_alpha)
505 {
506     image->common.component_alpha = component_alpha;
507 }
508
509
510 void
511 pixman_image_set_accessors (pixman_image_t             *image,
512                             pixman_read_memory_func_t   read_func,
513                             pixman_write_memory_func_t  write_func)
514 {
515     return_if_fail (image != NULL);
516
517     image->common.read_func = read_func;
518     image->common.write_func = write_func;
519 }
520
521 uint32_t *
522 pixman_image_get_data (pixman_image_t *image)
523 {
524     if (image->type == BITS)
525         return image->bits.bits;
526
527     return NULL;
528 }
529
530 int
531 pixman_image_get_width (pixman_image_t *image)
532 {
533     if (image->type == BITS)
534         return image->bits.width;
535
536     return 0;
537 }
538
539 int
540 pixman_image_get_height (pixman_image_t *image)
541 {
542     if (image->type == BITS)
543         return image->bits.height;
544
545     return 0;
546 }
547
548 int
549 pixman_image_get_stride (pixman_image_t *image)
550 {
551     if (image->type == BITS)
552         return image->bits.rowstride * sizeof (uint32_t);
553
554     return 0;
555 }
556
557 int
558 pixman_image_get_depth (pixman_image_t *image)
559 {
560     if (image->type == BITS)
561         return PIXMAN_FORMAT_DEPTH (image->bits.format);
562
563     return 0;
564 }
565
566 pixman_bool_t
567 pixman_image_fill_rectangles (pixman_op_t                   op,
568                               pixman_image_t               *dest,
569                               pixman_color_t               *color,
570                               int                           n_rects,
571                               const pixman_rectangle16_t   *rects)
572 {
573     pixman_image_t *solid;
574     pixman_color_t c;
575     int i;
576     
577     if (color->alpha == 0xffff)
578     {
579         if (op == PIXMAN_OP_OVER)
580             op = PIXMAN_OP_SRC;
581     }
582
583     if (op == PIXMAN_OP_CLEAR)
584     {
585         c.red = 0;
586         c.green = 0;
587         c.blue = 0;
588         c.alpha = 0;
589
590         color = &c;
591         
592         op = PIXMAN_OP_SRC;
593     }
594
595     solid = pixman_image_create_solid_fill (color);
596     if (!solid)
597         return FALSE;
598
599     for (i = 0; i < n_rects; ++i)
600     {
601         const pixman_rectangle16_t *rect = &(rects[i]);
602         
603         pixman_image_composite (op, solid, NULL, dest,
604                                 0, 0, 0, 0,
605                                 rect->x, rect->y,
606                                 rect->width, rect->height);
607     }
608     
609     pixman_image_unref (solid);
610
611     return TRUE;
612 }