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