Use MAKE_ACCESSORS() to generate accessors for the a1 format.
[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 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <assert.h>
31
32 #include "pixman-private.h"
33
34 pixman_bool_t
35 _pixman_init_gradient (gradient_t *                  gradient,
36                        const pixman_gradient_stop_t *stops,
37                        int                           n_stops)
38 {
39     return_val_if_fail (n_stops > 0, FALSE);
40
41     gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
42     if (!gradient->stops)
43         return FALSE;
44
45     memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
46
47     gradient->n_stops = n_stops;
48
49     return TRUE;
50 }
51
52 pixman_image_t *
53 _pixman_image_allocate (void)
54 {
55     pixman_image_t *image = malloc (sizeof (pixman_image_t));
56
57     if (image)
58     {
59         image_common_t *common = &image->common;
60
61         pixman_region32_init (&common->clip_region);
62
63         common->alpha_count = 0;
64         common->have_clip_region = FALSE;
65         common->clip_sources = FALSE;
66         common->transform = NULL;
67         common->repeat = PIXMAN_REPEAT_NONE;
68         common->filter = PIXMAN_FILTER_NEAREST;
69         common->filter_params = NULL;
70         common->n_filter_params = 0;
71         common->alpha_map = NULL;
72         common->component_alpha = FALSE;
73         common->ref_count = 1;
74         common->property_changed = NULL;
75         common->client_clip = FALSE;
76         common->destroy_func = NULL;
77         common->destroy_data = NULL;
78         common->dirty = TRUE;
79     }
80
81     return image;
82 }
83
84 static void
85 image_property_changed (pixman_image_t *image)
86 {
87     image->common.dirty = TRUE;
88 }
89
90 /* Ref Counting */
91 PIXMAN_EXPORT pixman_image_t *
92 pixman_image_ref (pixman_image_t *image)
93 {
94     image->common.ref_count++;
95
96     return image;
97 }
98
99 /* returns TRUE when the image is freed */
100 PIXMAN_EXPORT pixman_bool_t
101 pixman_image_unref (pixman_image_t *image)
102 {
103     image_common_t *common = (image_common_t *)image;
104
105     common->ref_count--;
106
107     if (common->ref_count == 0)
108     {
109         if (image->common.destroy_func)
110             image->common.destroy_func (image, image->common.destroy_data);
111
112         pixman_region32_fini (&common->clip_region);
113
114         if (common->transform)
115             free (common->transform);
116
117         if (common->filter_params)
118             free (common->filter_params);
119
120         if (common->alpha_map)
121             pixman_image_unref ((pixman_image_t *)common->alpha_map);
122
123         if (image->type == LINEAR ||
124             image->type == RADIAL ||
125             image->type == CONICAL)
126         {
127             if (image->gradient.stops)
128                 free (image->gradient.stops);
129         }
130
131         if (image->type == BITS && image->bits.free_me)
132             free (image->bits.free_me);
133
134         free (image);
135
136         return TRUE;
137     }
138
139     return FALSE;
140 }
141
142 PIXMAN_EXPORT void
143 pixman_image_set_destroy_function (pixman_image_t *            image,
144                                    pixman_image_destroy_func_t func,
145                                    void *                      data)
146 {
147     image->common.destroy_func = func;
148     image->common.destroy_data = data;
149 }
150
151 PIXMAN_EXPORT void *
152 pixman_image_get_destroy_data (pixman_image_t *image)
153 {
154   return image->common.destroy_data;
155 }
156
157 void
158 _pixman_image_reset_clip_region (pixman_image_t *image)
159 {
160     image->common.have_clip_region = FALSE;
161 }
162
163 /* Executive Summary: This function is a no-op that only exists
164  * for historical reasons.
165  *
166  * There used to be a bug in the X server where it would rely on
167  * out-of-bounds accesses when it was asked to composite with a
168  * window as the source. It would create a pixman image pointing
169  * to some bogus position in memory, but then set a clip region
170  * to the position where the actual bits were.
171  *
172  * Due to a bug in old versions of pixman, where it would not clip
173  * against the image bounds when a clip region was set, this would
174  * actually work. So when the pixman bug was fixed, a workaround was
175  * added to allow certain out-of-bound accesses. This function disabled
176  * those workarounds.
177  *
178  * Since 0.21.2, pixman doesn't do these workarounds anymore, so now
179  * this function is a no-op.
180  */
181 PIXMAN_EXPORT void
182 pixman_disable_out_of_bounds_workaround (void)
183 {
184 }
185
186 static void
187 compute_image_info (pixman_image_t *image)
188 {
189     pixman_format_code_t code;
190     uint32_t flags = 0;
191
192     /* Transform */
193     if (!image->common.transform)
194     {
195         flags |= (FAST_PATH_ID_TRANSFORM        |
196                   FAST_PATH_X_UNIT_POSITIVE     |
197                   FAST_PATH_Y_UNIT_ZERO         |
198                   FAST_PATH_AFFINE_TRANSFORM);
199     }
200     else
201     {
202         flags |= FAST_PATH_HAS_TRANSFORM;
203
204         if (image->common.transform->matrix[2][0] == 0                  &&
205             image->common.transform->matrix[2][1] == 0                  &&
206             image->common.transform->matrix[2][2] == pixman_fixed_1)
207         {
208             flags |= FAST_PATH_AFFINE_TRANSFORM;
209
210             if (image->common.transform->matrix[0][1] == 0 &&
211                 image->common.transform->matrix[1][0] == 0)
212             {
213                 if (image->common.transform->matrix[0][0] == -pixman_fixed_1 &&
214                     image->common.transform->matrix[1][1] == -pixman_fixed_1)
215                 {
216                     flags |= FAST_PATH_ROTATE_180_TRANSFORM;
217                 }
218                 flags |= FAST_PATH_SCALE_TRANSFORM;
219             }
220             else if (image->common.transform->matrix[0][0] == 0 &&
221                      image->common.transform->matrix[1][1] == 0)
222             {
223                 pixman_fixed_t m01 = image->common.transform->matrix[0][1];
224                 if (m01 == -image->common.transform->matrix[1][0])
225                 {
226                         if (m01 == -pixman_fixed_1)
227                             flags |= FAST_PATH_ROTATE_90_TRANSFORM;
228                         else if (m01 == pixman_fixed_1)
229                             flags |= FAST_PATH_ROTATE_270_TRANSFORM;
230                 }
231             }
232         }
233
234         if (image->common.transform->matrix[0][0] > 0)
235             flags |= FAST_PATH_X_UNIT_POSITIVE;
236
237         if (image->common.transform->matrix[1][0] == 0)
238             flags |= FAST_PATH_Y_UNIT_ZERO;
239     }
240
241     /* Filter */
242     switch (image->common.filter)
243     {
244     case PIXMAN_FILTER_NEAREST:
245     case PIXMAN_FILTER_FAST:
246         flags |= (FAST_PATH_NEAREST_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
247         break;
248
249     case PIXMAN_FILTER_BILINEAR:
250     case PIXMAN_FILTER_GOOD:
251     case PIXMAN_FILTER_BEST:
252         flags |= (FAST_PATH_BILINEAR_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
253         break;
254
255     case PIXMAN_FILTER_CONVOLUTION:
256         break;
257
258     default:
259         flags |= FAST_PATH_NO_CONVOLUTION_FILTER;
260         break;
261     }
262
263     /* Repeat mode */
264     switch (image->common.repeat)
265     {
266     case PIXMAN_REPEAT_NONE:
267         flags |=
268             FAST_PATH_NO_REFLECT_REPEAT         |
269             FAST_PATH_NO_PAD_REPEAT             |
270             FAST_PATH_NO_NORMAL_REPEAT;
271         break;
272
273     case PIXMAN_REPEAT_REFLECT:
274         flags |=
275             FAST_PATH_NO_PAD_REPEAT             |
276             FAST_PATH_NO_NONE_REPEAT            |
277             FAST_PATH_NO_NORMAL_REPEAT;
278         break;
279
280     case PIXMAN_REPEAT_PAD:
281         flags |=
282             FAST_PATH_NO_REFLECT_REPEAT         |
283             FAST_PATH_NO_NONE_REPEAT            |
284             FAST_PATH_NO_NORMAL_REPEAT;
285         break;
286
287     default:
288         flags |=
289             FAST_PATH_NO_REFLECT_REPEAT         |
290             FAST_PATH_NO_PAD_REPEAT             |
291             FAST_PATH_NO_NONE_REPEAT;
292         break;
293     }
294
295     /* Component alpha */
296     if (image->common.component_alpha)
297         flags |= FAST_PATH_COMPONENT_ALPHA;
298     else
299         flags |= FAST_PATH_UNIFIED_ALPHA;
300
301     flags |= (FAST_PATH_NO_ACCESSORS | FAST_PATH_NARROW_FORMAT);
302
303     /* Type specific checks */
304     switch (image->type)
305     {
306     case SOLID:
307         code = PIXMAN_solid;
308
309         if (image->solid.color.alpha == 0xffff)
310             flags |= FAST_PATH_IS_OPAQUE;
311         break;
312
313     case BITS:
314         if (image->bits.width == 1      &&
315             image->bits.height == 1     &&
316             image->common.repeat != PIXMAN_REPEAT_NONE)
317         {
318             code = PIXMAN_solid;
319         }
320         else
321         {
322             code = image->bits.format;
323         }
324
325         if (!PIXMAN_FORMAT_A (image->bits.format)                               &&
326             PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_GRAY         &&
327             PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_COLOR)
328         {
329             flags |= FAST_PATH_SAMPLES_OPAQUE;
330
331             if (image->common.repeat != PIXMAN_REPEAT_NONE)
332                 flags |= FAST_PATH_IS_OPAQUE;
333         }
334
335         if (image->bits.read_func || image->bits.write_func)
336             flags &= ~FAST_PATH_NO_ACCESSORS;
337
338         if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
339             flags &= ~FAST_PATH_NARROW_FORMAT;
340         break;
341
342     case RADIAL:
343         code = PIXMAN_unknown;
344
345         /*
346          * As explained in pixman-radial-gradient.c, every point of
347          * the plane has a valid associated radius (and thus will be
348          * colored) if and only if a is negative (i.e. one of the two
349          * circles contains the other one).
350          */
351
352         if (image->radial.a >= 0)
353             break;
354
355         /* Fall through */
356
357     case CONICAL:
358     case LINEAR:
359         code = PIXMAN_unknown;
360
361         if (image->common.repeat != PIXMAN_REPEAT_NONE)
362         {
363             int i;
364
365             flags |= FAST_PATH_IS_OPAQUE;
366             for (i = 0; i < image->gradient.n_stops; ++i)
367             {
368                 if (image->gradient.stops[i].color.alpha != 0xffff)
369                 {
370                     flags &= ~FAST_PATH_IS_OPAQUE;
371                     break;
372                 }
373             }
374         }
375         break;
376
377     default:
378         code = PIXMAN_unknown;
379         break;
380     }
381
382     /* Alpha map */
383     if (!image->common.alpha_map)
384     {
385         flags |= FAST_PATH_NO_ALPHA_MAP;
386     }
387     else
388     {
389         if (PIXMAN_FORMAT_IS_WIDE (image->common.alpha_map->format))
390             flags &= ~FAST_PATH_NARROW_FORMAT;
391     }
392
393     /* Both alpha maps and convolution filters can introduce
394      * non-opaqueness in otherwise opaque images. Also
395      * an image with component alpha turned on is only opaque
396      * if all channels are opaque, so we simply turn it off
397      * unconditionally for those images.
398      */
399     if (image->common.alpha_map                                 ||
400         image->common.filter == PIXMAN_FILTER_CONVOLUTION       ||
401         image->common.component_alpha)
402     {
403         flags &= ~(FAST_PATH_IS_OPAQUE | FAST_PATH_SAMPLES_OPAQUE);
404     }
405
406     image->common.flags = flags;
407     image->common.extended_format_code = code;
408 }
409
410 void
411 _pixman_image_validate (pixman_image_t *image)
412 {
413     if (image->common.dirty)
414     {
415         compute_image_info (image);
416
417         /* It is important that property_changed is
418          * called *after* compute_image_info() because
419          * property_changed() can make use of the flags
420          * to set up accessors etc.
421          */
422         if (image->common.property_changed)
423             image->common.property_changed (image);
424
425         image->common.dirty = FALSE;
426     }
427
428     if (image->common.alpha_map)
429         _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
430 }
431
432 PIXMAN_EXPORT pixman_bool_t
433 pixman_image_set_clip_region32 (pixman_image_t *   image,
434                                 pixman_region32_t *region)
435 {
436     image_common_t *common = (image_common_t *)image;
437     pixman_bool_t result;
438
439     if (region)
440     {
441         if ((result = pixman_region32_copy (&common->clip_region, region)))
442             image->common.have_clip_region = TRUE;
443     }
444     else
445     {
446         _pixman_image_reset_clip_region (image);
447
448         result = TRUE;
449     }
450
451     image_property_changed (image);
452
453     return result;
454 }
455
456 PIXMAN_EXPORT pixman_bool_t
457 pixman_image_set_clip_region (pixman_image_t *   image,
458                               pixman_region16_t *region)
459 {
460     image_common_t *common = (image_common_t *)image;
461     pixman_bool_t result;
462
463     if (region)
464     {
465         if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
466             image->common.have_clip_region = TRUE;
467     }
468     else
469     {
470         _pixman_image_reset_clip_region (image);
471
472         result = TRUE;
473     }
474
475     image_property_changed (image);
476
477     return result;
478 }
479
480 PIXMAN_EXPORT void
481 pixman_image_set_has_client_clip (pixman_image_t *image,
482                                   pixman_bool_t   client_clip)
483 {
484     image->common.client_clip = client_clip;
485 }
486
487 PIXMAN_EXPORT pixman_bool_t
488 pixman_image_set_transform (pixman_image_t *          image,
489                             const pixman_transform_t *transform)
490 {
491     static const pixman_transform_t id =
492     {
493         { { pixman_fixed_1, 0, 0 },
494           { 0, pixman_fixed_1, 0 },
495           { 0, 0, pixman_fixed_1 } }
496     };
497
498     image_common_t *common = (image_common_t *)image;
499     pixman_bool_t result;
500
501     if (common->transform == transform)
502         return TRUE;
503
504     if (!transform || memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
505     {
506         free (common->transform);
507         common->transform = NULL;
508         result = TRUE;
509
510         goto out;
511     }
512
513     if (common->transform &&
514         memcmp (common->transform, transform, sizeof (pixman_transform_t) == 0))
515     {
516         return TRUE;
517     }
518
519     if (common->transform == NULL)
520         common->transform = malloc (sizeof (pixman_transform_t));
521
522     if (common->transform == NULL)
523     {
524         result = FALSE;
525
526         goto out;
527     }
528
529     memcpy (common->transform, transform, sizeof(pixman_transform_t));
530
531     result = TRUE;
532
533 out:
534     image_property_changed (image);
535
536     return result;
537 }
538
539 PIXMAN_EXPORT void
540 pixman_image_set_repeat (pixman_image_t *image,
541                          pixman_repeat_t repeat)
542 {
543     if (image->common.repeat == repeat)
544         return;
545
546     image->common.repeat = repeat;
547
548     image_property_changed (image);
549 }
550
551 PIXMAN_EXPORT pixman_bool_t
552 pixman_image_set_filter (pixman_image_t *      image,
553                          pixman_filter_t       filter,
554                          const pixman_fixed_t *params,
555                          int                   n_params)
556 {
557     image_common_t *common = (image_common_t *)image;
558     pixman_fixed_t *new_params;
559
560     if (params == common->filter_params && filter == common->filter)
561         return TRUE;
562
563     new_params = NULL;
564     if (params)
565     {
566         new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
567         if (!new_params)
568             return FALSE;
569
570         memcpy (new_params,
571                 params, n_params * sizeof (pixman_fixed_t));
572     }
573
574     common->filter = filter;
575
576     if (common->filter_params)
577         free (common->filter_params);
578
579     common->filter_params = new_params;
580     common->n_filter_params = n_params;
581
582     image_property_changed (image);
583     return TRUE;
584 }
585
586 PIXMAN_EXPORT void
587 pixman_image_set_source_clipping (pixman_image_t *image,
588                                   pixman_bool_t   clip_sources)
589 {
590     if (image->common.clip_sources == clip_sources)
591         return;
592
593     image->common.clip_sources = clip_sources;
594
595     image_property_changed (image);
596 }
597
598 /* Unlike all the other property setters, this function does not
599  * copy the content of indexed. Doing this copying is simply
600  * way, way too expensive.
601  */
602 PIXMAN_EXPORT void
603 pixman_image_set_indexed (pixman_image_t *        image,
604                           const pixman_indexed_t *indexed)
605 {
606     bits_image_t *bits = (bits_image_t *)image;
607
608     if (bits->indexed == indexed)
609         return;
610
611     bits->indexed = indexed;
612
613     image_property_changed (image);
614 }
615
616 PIXMAN_EXPORT void
617 pixman_image_set_alpha_map (pixman_image_t *image,
618                             pixman_image_t *alpha_map,
619                             int16_t         x,
620                             int16_t         y)
621 {
622     image_common_t *common = (image_common_t *)image;
623
624     return_if_fail (!alpha_map || alpha_map->type == BITS);
625
626     if (alpha_map && common->alpha_count > 0)
627     {
628         /* If this image is being used as an alpha map itself,
629          * then you can't give it an alpha map of its own.
630          */
631         return;
632     }
633
634     if (alpha_map && alpha_map->common.alpha_map)
635     {
636         /* If the image has an alpha map of its own,
637          * then it can't be used as an alpha map itself
638          */
639         return;
640     }
641
642     if (common->alpha_map != (bits_image_t *)alpha_map)
643     {
644         if (common->alpha_map)
645         {
646             common->alpha_map->common.alpha_count--;
647
648             pixman_image_unref ((pixman_image_t *)common->alpha_map);
649         }
650
651         if (alpha_map)
652         {
653             common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
654
655             common->alpha_map->common.alpha_count++;
656         }
657         else
658         {
659             common->alpha_map = NULL;
660         }
661     }
662
663     common->alpha_origin_x = x;
664     common->alpha_origin_y = y;
665
666     image_property_changed (image);
667 }
668
669 PIXMAN_EXPORT void
670 pixman_image_set_component_alpha   (pixman_image_t *image,
671                                     pixman_bool_t   component_alpha)
672 {
673     if (image->common.component_alpha == component_alpha)
674         return;
675
676     image->common.component_alpha = component_alpha;
677
678     image_property_changed (image);
679 }
680
681 PIXMAN_EXPORT pixman_bool_t
682 pixman_image_get_component_alpha   (pixman_image_t       *image)
683 {
684     return image->common.component_alpha;
685 }
686
687 PIXMAN_EXPORT void
688 pixman_image_set_accessors (pixman_image_t *           image,
689                             pixman_read_memory_func_t  read_func,
690                             pixman_write_memory_func_t write_func)
691 {
692     return_if_fail (image != NULL);
693
694     if (image->type == BITS)
695     {
696         image->bits.read_func = read_func;
697         image->bits.write_func = write_func;
698
699         image_property_changed (image);
700     }
701 }
702
703 PIXMAN_EXPORT uint32_t *
704 pixman_image_get_data (pixman_image_t *image)
705 {
706     if (image->type == BITS)
707         return image->bits.bits;
708
709     return NULL;
710 }
711
712 PIXMAN_EXPORT int
713 pixman_image_get_width (pixman_image_t *image)
714 {
715     if (image->type == BITS)
716         return image->bits.width;
717
718     return 0;
719 }
720
721 PIXMAN_EXPORT int
722 pixman_image_get_height (pixman_image_t *image)
723 {
724     if (image->type == BITS)
725         return image->bits.height;
726
727     return 0;
728 }
729
730 PIXMAN_EXPORT int
731 pixman_image_get_stride (pixman_image_t *image)
732 {
733     if (image->type == BITS)
734         return image->bits.rowstride * (int) sizeof (uint32_t);
735
736     return 0;
737 }
738
739 PIXMAN_EXPORT int
740 pixman_image_get_depth (pixman_image_t *image)
741 {
742     if (image->type == BITS)
743         return PIXMAN_FORMAT_DEPTH (image->bits.format);
744
745     return 0;
746 }
747
748 PIXMAN_EXPORT pixman_format_code_t
749 pixman_image_get_format (pixman_image_t *image)
750 {
751     if (image->type == BITS)
752         return image->bits.format;
753
754     return 0;
755 }
756
757 uint32_t
758 _pixman_image_get_solid (pixman_implementation_t *imp,
759                          pixman_image_t *         image,
760                          pixman_format_code_t     format)
761 {
762     uint32_t result;
763     pixman_iter_t iter;
764
765     _pixman_implementation_src_iter_init (
766         imp, &iter, image, 0, 0, 1, 1,
767         (uint8_t *)&result, ITER_NARROW);
768
769     result = *iter.get_scanline (&iter, NULL);
770
771     /* If necessary, convert RGB <--> BGR. */
772     if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
773     {
774         result = (((result & 0xff000000) >>  0) |
775                   ((result & 0x00ff0000) >> 16) |
776                   ((result & 0x0000ff00) >>  0) |
777                   ((result & 0x000000ff) << 16));
778     }
779
780     return result;
781 }