Strength-reduce BILINEAR filter to NEAREST filter for identity transforms
[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
254         /* Reduce BILINEAR to NEAREST for identity transforms */
255         if (flags & FAST_PATH_ID_TRANSFORM)
256             flags |= FAST_PATH_NEAREST_FILTER;
257         break;
258
259     case PIXMAN_FILTER_CONVOLUTION:
260         break;
261
262     default:
263         flags |= FAST_PATH_NO_CONVOLUTION_FILTER;
264         break;
265     }
266
267     /* Repeat mode */
268     switch (image->common.repeat)
269     {
270     case PIXMAN_REPEAT_NONE:
271         flags |=
272             FAST_PATH_NO_REFLECT_REPEAT         |
273             FAST_PATH_NO_PAD_REPEAT             |
274             FAST_PATH_NO_NORMAL_REPEAT;
275         break;
276
277     case PIXMAN_REPEAT_REFLECT:
278         flags |=
279             FAST_PATH_NO_PAD_REPEAT             |
280             FAST_PATH_NO_NONE_REPEAT            |
281             FAST_PATH_NO_NORMAL_REPEAT;
282         break;
283
284     case PIXMAN_REPEAT_PAD:
285         flags |=
286             FAST_PATH_NO_REFLECT_REPEAT         |
287             FAST_PATH_NO_NONE_REPEAT            |
288             FAST_PATH_NO_NORMAL_REPEAT;
289         break;
290
291     default:
292         flags |=
293             FAST_PATH_NO_REFLECT_REPEAT         |
294             FAST_PATH_NO_PAD_REPEAT             |
295             FAST_PATH_NO_NONE_REPEAT;
296         break;
297     }
298
299     /* Component alpha */
300     if (image->common.component_alpha)
301         flags |= FAST_PATH_COMPONENT_ALPHA;
302     else
303         flags |= FAST_PATH_UNIFIED_ALPHA;
304
305     flags |= (FAST_PATH_NO_ACCESSORS | FAST_PATH_NARROW_FORMAT);
306
307     /* Type specific checks */
308     switch (image->type)
309     {
310     case SOLID:
311         code = PIXMAN_solid;
312
313         if (image->solid.color.alpha == 0xffff)
314             flags |= FAST_PATH_IS_OPAQUE;
315         break;
316
317     case BITS:
318         if (image->bits.width == 1      &&
319             image->bits.height == 1     &&
320             image->common.repeat != PIXMAN_REPEAT_NONE)
321         {
322             code = PIXMAN_solid;
323         }
324         else
325         {
326             code = image->bits.format;
327         }
328
329         if (!PIXMAN_FORMAT_A (image->bits.format)                               &&
330             PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_GRAY         &&
331             PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_COLOR)
332         {
333             flags |= FAST_PATH_SAMPLES_OPAQUE;
334
335             if (image->common.repeat != PIXMAN_REPEAT_NONE)
336                 flags |= FAST_PATH_IS_OPAQUE;
337         }
338
339         if (image->bits.read_func || image->bits.write_func)
340             flags &= ~FAST_PATH_NO_ACCESSORS;
341
342         if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
343             flags &= ~FAST_PATH_NARROW_FORMAT;
344         break;
345
346     case RADIAL:
347         code = PIXMAN_unknown;
348
349         /*
350          * As explained in pixman-radial-gradient.c, every point of
351          * the plane has a valid associated radius (and thus will be
352          * colored) if and only if a is negative (i.e. one of the two
353          * circles contains the other one).
354          */
355
356         if (image->radial.a >= 0)
357             break;
358
359         /* Fall through */
360
361     case CONICAL:
362     case LINEAR:
363         code = PIXMAN_unknown;
364
365         if (image->common.repeat != PIXMAN_REPEAT_NONE)
366         {
367             int i;
368
369             flags |= FAST_PATH_IS_OPAQUE;
370             for (i = 0; i < image->gradient.n_stops; ++i)
371             {
372                 if (image->gradient.stops[i].color.alpha != 0xffff)
373                 {
374                     flags &= ~FAST_PATH_IS_OPAQUE;
375                     break;
376                 }
377             }
378         }
379         break;
380
381     default:
382         code = PIXMAN_unknown;
383         break;
384     }
385
386     /* Alpha map */
387     if (!image->common.alpha_map)
388     {
389         flags |= FAST_PATH_NO_ALPHA_MAP;
390     }
391     else
392     {
393         if (PIXMAN_FORMAT_IS_WIDE (image->common.alpha_map->format))
394             flags &= ~FAST_PATH_NARROW_FORMAT;
395     }
396
397     /* Both alpha maps and convolution filters can introduce
398      * non-opaqueness in otherwise opaque images. Also
399      * an image with component alpha turned on is only opaque
400      * if all channels are opaque, so we simply turn it off
401      * unconditionally for those images.
402      */
403     if (image->common.alpha_map                                 ||
404         image->common.filter == PIXMAN_FILTER_CONVOLUTION       ||
405         image->common.component_alpha)
406     {
407         flags &= ~(FAST_PATH_IS_OPAQUE | FAST_PATH_SAMPLES_OPAQUE);
408     }
409
410     image->common.flags = flags;
411     image->common.extended_format_code = code;
412 }
413
414 void
415 _pixman_image_validate (pixman_image_t *image)
416 {
417     if (image->common.dirty)
418     {
419         compute_image_info (image);
420
421         /* It is important that property_changed is
422          * called *after* compute_image_info() because
423          * property_changed() can make use of the flags
424          * to set up accessors etc.
425          */
426         if (image->common.property_changed)
427             image->common.property_changed (image);
428
429         image->common.dirty = FALSE;
430     }
431
432     if (image->common.alpha_map)
433         _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
434 }
435
436 PIXMAN_EXPORT pixman_bool_t
437 pixman_image_set_clip_region32 (pixman_image_t *   image,
438                                 pixman_region32_t *region)
439 {
440     image_common_t *common = (image_common_t *)image;
441     pixman_bool_t result;
442
443     if (region)
444     {
445         if ((result = pixman_region32_copy (&common->clip_region, region)))
446             image->common.have_clip_region = TRUE;
447     }
448     else
449     {
450         _pixman_image_reset_clip_region (image);
451
452         result = TRUE;
453     }
454
455     image_property_changed (image);
456
457     return result;
458 }
459
460 PIXMAN_EXPORT pixman_bool_t
461 pixman_image_set_clip_region (pixman_image_t *   image,
462                               pixman_region16_t *region)
463 {
464     image_common_t *common = (image_common_t *)image;
465     pixman_bool_t result;
466
467     if (region)
468     {
469         if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
470             image->common.have_clip_region = TRUE;
471     }
472     else
473     {
474         _pixman_image_reset_clip_region (image);
475
476         result = TRUE;
477     }
478
479     image_property_changed (image);
480
481     return result;
482 }
483
484 PIXMAN_EXPORT void
485 pixman_image_set_has_client_clip (pixman_image_t *image,
486                                   pixman_bool_t   client_clip)
487 {
488     image->common.client_clip = client_clip;
489 }
490
491 PIXMAN_EXPORT pixman_bool_t
492 pixman_image_set_transform (pixman_image_t *          image,
493                             const pixman_transform_t *transform)
494 {
495     static const pixman_transform_t id =
496     {
497         { { pixman_fixed_1, 0, 0 },
498           { 0, pixman_fixed_1, 0 },
499           { 0, 0, pixman_fixed_1 } }
500     };
501
502     image_common_t *common = (image_common_t *)image;
503     pixman_bool_t result;
504
505     if (common->transform == transform)
506         return TRUE;
507
508     if (!transform || memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
509     {
510         free (common->transform);
511         common->transform = NULL;
512         result = TRUE;
513
514         goto out;
515     }
516
517     if (common->transform &&
518         memcmp (common->transform, transform, sizeof (pixman_transform_t) == 0))
519     {
520         return TRUE;
521     }
522
523     if (common->transform == NULL)
524         common->transform = malloc (sizeof (pixman_transform_t));
525
526     if (common->transform == NULL)
527     {
528         result = FALSE;
529
530         goto out;
531     }
532
533     memcpy (common->transform, transform, sizeof(pixman_transform_t));
534
535     result = TRUE;
536
537 out:
538     image_property_changed (image);
539
540     return result;
541 }
542
543 PIXMAN_EXPORT void
544 pixman_image_set_repeat (pixman_image_t *image,
545                          pixman_repeat_t repeat)
546 {
547     if (image->common.repeat == repeat)
548         return;
549
550     image->common.repeat = repeat;
551
552     image_property_changed (image);
553 }
554
555 PIXMAN_EXPORT pixman_bool_t
556 pixman_image_set_filter (pixman_image_t *      image,
557                          pixman_filter_t       filter,
558                          const pixman_fixed_t *params,
559                          int                   n_params)
560 {
561     image_common_t *common = (image_common_t *)image;
562     pixman_fixed_t *new_params;
563
564     if (params == common->filter_params && filter == common->filter)
565         return TRUE;
566
567     new_params = NULL;
568     if (params)
569     {
570         new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
571         if (!new_params)
572             return FALSE;
573
574         memcpy (new_params,
575                 params, n_params * sizeof (pixman_fixed_t));
576     }
577
578     common->filter = filter;
579
580     if (common->filter_params)
581         free (common->filter_params);
582
583     common->filter_params = new_params;
584     common->n_filter_params = n_params;
585
586     image_property_changed (image);
587     return TRUE;
588 }
589
590 PIXMAN_EXPORT void
591 pixman_image_set_source_clipping (pixman_image_t *image,
592                                   pixman_bool_t   clip_sources)
593 {
594     if (image->common.clip_sources == clip_sources)
595         return;
596
597     image->common.clip_sources = clip_sources;
598
599     image_property_changed (image);
600 }
601
602 /* Unlike all the other property setters, this function does not
603  * copy the content of indexed. Doing this copying is simply
604  * way, way too expensive.
605  */
606 PIXMAN_EXPORT void
607 pixman_image_set_indexed (pixman_image_t *        image,
608                           const pixman_indexed_t *indexed)
609 {
610     bits_image_t *bits = (bits_image_t *)image;
611
612     if (bits->indexed == indexed)
613         return;
614
615     bits->indexed = indexed;
616
617     image_property_changed (image);
618 }
619
620 PIXMAN_EXPORT void
621 pixman_image_set_alpha_map (pixman_image_t *image,
622                             pixman_image_t *alpha_map,
623                             int16_t         x,
624                             int16_t         y)
625 {
626     image_common_t *common = (image_common_t *)image;
627
628     return_if_fail (!alpha_map || alpha_map->type == BITS);
629
630     if (alpha_map && common->alpha_count > 0)
631     {
632         /* If this image is being used as an alpha map itself,
633          * then you can't give it an alpha map of its own.
634          */
635         return;
636     }
637
638     if (alpha_map && alpha_map->common.alpha_map)
639     {
640         /* If the image has an alpha map of its own,
641          * then it can't be used as an alpha map itself
642          */
643         return;
644     }
645
646     if (common->alpha_map != (bits_image_t *)alpha_map)
647     {
648         if (common->alpha_map)
649         {
650             common->alpha_map->common.alpha_count--;
651
652             pixman_image_unref ((pixman_image_t *)common->alpha_map);
653         }
654
655         if (alpha_map)
656         {
657             common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
658
659             common->alpha_map->common.alpha_count++;
660         }
661         else
662         {
663             common->alpha_map = NULL;
664         }
665     }
666
667     common->alpha_origin_x = x;
668     common->alpha_origin_y = y;
669
670     image_property_changed (image);
671 }
672
673 PIXMAN_EXPORT void
674 pixman_image_set_component_alpha   (pixman_image_t *image,
675                                     pixman_bool_t   component_alpha)
676 {
677     if (image->common.component_alpha == component_alpha)
678         return;
679
680     image->common.component_alpha = component_alpha;
681
682     image_property_changed (image);
683 }
684
685 PIXMAN_EXPORT pixman_bool_t
686 pixman_image_get_component_alpha   (pixman_image_t       *image)
687 {
688     return image->common.component_alpha;
689 }
690
691 PIXMAN_EXPORT void
692 pixman_image_set_accessors (pixman_image_t *           image,
693                             pixman_read_memory_func_t  read_func,
694                             pixman_write_memory_func_t write_func)
695 {
696     return_if_fail (image != NULL);
697
698     if (image->type == BITS)
699     {
700         image->bits.read_func = read_func;
701         image->bits.write_func = write_func;
702
703         image_property_changed (image);
704     }
705 }
706
707 PIXMAN_EXPORT uint32_t *
708 pixman_image_get_data (pixman_image_t *image)
709 {
710     if (image->type == BITS)
711         return image->bits.bits;
712
713     return NULL;
714 }
715
716 PIXMAN_EXPORT int
717 pixman_image_get_width (pixman_image_t *image)
718 {
719     if (image->type == BITS)
720         return image->bits.width;
721
722     return 0;
723 }
724
725 PIXMAN_EXPORT int
726 pixman_image_get_height (pixman_image_t *image)
727 {
728     if (image->type == BITS)
729         return image->bits.height;
730
731     return 0;
732 }
733
734 PIXMAN_EXPORT int
735 pixman_image_get_stride (pixman_image_t *image)
736 {
737     if (image->type == BITS)
738         return image->bits.rowstride * (int) sizeof (uint32_t);
739
740     return 0;
741 }
742
743 PIXMAN_EXPORT int
744 pixman_image_get_depth (pixman_image_t *image)
745 {
746     if (image->type == BITS)
747         return PIXMAN_FORMAT_DEPTH (image->bits.format);
748
749     return 0;
750 }
751
752 PIXMAN_EXPORT pixman_format_code_t
753 pixman_image_get_format (pixman_image_t *image)
754 {
755     if (image->type == BITS)
756         return image->bits.format;
757
758     return 0;
759 }
760
761 uint32_t
762 _pixman_image_get_solid (pixman_implementation_t *imp,
763                          pixman_image_t *         image,
764                          pixman_format_code_t     format)
765 {
766     uint32_t result;
767     pixman_iter_t iter;
768
769     _pixman_implementation_src_iter_init (
770         imp, &iter, image, 0, 0, 1, 1,
771         (uint8_t *)&result, ITER_NARROW);
772
773     result = *iter.get_scanline (&iter, NULL);
774
775     /* If necessary, convert RGB <--> BGR. */
776     if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
777     {
778         result = (((result & 0xff000000) >>  0) |
779                   ((result & 0x00ff0000) >> 16) |
780                   ((result & 0x0000ff00) >>  0) |
781                   ((result & 0x000000ff) << 16));
782     }
783
784     return result;
785 }