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