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