Turn need_workaround into another 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 void
246 compute_image_info (pixman_image_t *image)
247 {
248     pixman_format_code_t code;
249     uint32_t flags = 0;
250
251     if (!image->common.transform)
252     {
253         flags |= FAST_PATH_ID_TRANSFORM;
254     }
255     else if (image->common.transform &&
256              image->common.transform->matrix[0][1] == 0 &&
257              image->common.transform->matrix[1][0] == 0 &&
258              image->common.transform->matrix[2][0] == 0 &&
259              image->common.transform->matrix[2][1] == 0 &&
260              image->common.transform->matrix[2][2] == pixman_fixed_1)
261     {
262         flags |= FAST_PATH_SCALE_TRANSFORM;
263     }
264
265     if (!image->common.alpha_map)
266         flags |= FAST_PATH_NO_ALPHA_MAP;
267
268     if (image->common.filter != PIXMAN_FILTER_CONVOLUTION)
269     {
270         flags |= FAST_PATH_NO_CONVOLUTION_FILTER;
271
272         if (image->common.filter == PIXMAN_FILTER_NEAREST)
273             flags |= FAST_PATH_NEAREST_FILTER;
274     }
275
276     if (image->common.repeat != PIXMAN_REPEAT_PAD)
277         flags |= FAST_PATH_NO_PAD_REPEAT;
278
279     if (image->common.repeat != PIXMAN_REPEAT_REFLECT)
280         flags |= FAST_PATH_NO_REFLECT_REPEAT;
281
282     flags |= (FAST_PATH_NO_ACCESSORS | FAST_PATH_NO_WIDE_FORMAT);
283     if (image->common.type == BITS)
284     {
285         if (image->bits.read_func || image->bits.write_func)
286             flags &= ~FAST_PATH_NO_ACCESSORS;
287
288         if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
289             flags &= ~FAST_PATH_NO_WIDE_FORMAT;
290     }
291
292     if (image->common.component_alpha)
293         flags |= FAST_PATH_COMPONENT_ALPHA;
294     else
295         flags |= FAST_PATH_UNIFIED_ALPHA;
296
297     if (image->type == SOLID)
298     {
299         code = PIXMAN_solid;
300
301         if (image->solid.color.alpha == 0xffff)
302             flags |= FAST_PATH_IS_OPAQUE;
303     }
304     else if (image->common.type == BITS)
305     {
306         if (image->bits.width == 1      &&
307             image->bits.height == 1     &&
308             image->common.repeat != PIXMAN_REPEAT_NONE)
309         {
310             code = PIXMAN_solid;
311         }
312         else
313         {
314             code = image->bits.format;
315
316             if (!image->common.transform &&
317                 image->common.repeat == PIXMAN_REPEAT_NORMAL)
318             {
319                 flags |= FAST_PATH_SIMPLE_REPEAT;
320             }
321         }
322
323         if (image->common.repeat != PIXMAN_REPEAT_NONE &&
324             !PIXMAN_FORMAT_A (image->bits.format))
325         {
326             flags |= FAST_PATH_IS_OPAQUE;
327         }
328     }
329     else
330     {
331         code = PIXMAN_unknown;
332
333         if (image->type == LINEAR || image->type == RADIAL)
334         {
335             if (image->common.repeat != PIXMAN_REPEAT_NONE)
336             {
337                 int i;
338
339                 flags |= FAST_PATH_IS_OPAQUE;
340
341                 for (i = 0; i < image->gradient.n_stops; ++i)
342                 {
343                     if (image->gradient.stops[i].color.alpha != 0xffff)
344                     {
345                         flags &= ~FAST_PATH_IS_OPAQUE;
346                         break;
347                     }
348                 }
349             }
350         }
351     }
352
353     /* Both alpha maps and convolution filters can introduce
354      * non-opaqueness in otherwise opaque images. Also
355      * an image with component alpha turned on is only opaque
356      * if all channels are opaque, so we simply turn it off
357      * unconditionally for those images.
358      */
359     if (image->common.alpha_map                                 ||
360         image->common.filter == PIXMAN_FILTER_CONVOLUTION       ||
361         image->common.component_alpha)
362     {
363         flags &= ~FAST_PATH_IS_OPAQUE;
364     }
365
366     image->common.flags = flags;
367     image->common.extended_format_code = code;
368 }
369
370 void
371 _pixman_image_validate (pixman_image_t *image)
372 {
373     if (image->common.dirty)
374     {
375         compute_image_info (image);
376
377         /* It is important that property_changed is 
378          * called *after* compute_image_info() because
379          * the NEEDS_WORKAROUND flag is computed in
380          * property_changed(). And compute_image_info()
381          * completely overwrites the flags field
382          */
383         image->common.property_changed (image);
384
385         image->common.dirty = FALSE;
386     }
387
388     if (image->common.alpha_map)
389         _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
390 }
391
392 PIXMAN_EXPORT pixman_bool_t
393 pixman_image_set_clip_region32 (pixman_image_t *   image,
394                                 pixman_region32_t *region)
395 {
396     image_common_t *common = (image_common_t *)image;
397     pixman_bool_t result;
398
399     if (region)
400     {
401         if ((result = pixman_region32_copy (&common->clip_region, region)))
402             image->common.have_clip_region = TRUE;
403     }
404     else
405     {
406         _pixman_image_reset_clip_region (image);
407
408         result = TRUE;
409     }
410
411     image_property_changed (image);
412
413     return result;
414 }
415
416 PIXMAN_EXPORT pixman_bool_t
417 pixman_image_set_clip_region (pixman_image_t *   image,
418                               pixman_region16_t *region)
419 {
420     image_common_t *common = (image_common_t *)image;
421     pixman_bool_t result;
422
423     if (region)
424     {
425         if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
426             image->common.have_clip_region = TRUE;
427     }
428     else
429     {
430         _pixman_image_reset_clip_region (image);
431
432         result = TRUE;
433     }
434
435     image_property_changed (image);
436
437     return result;
438 }
439
440 PIXMAN_EXPORT void
441 pixman_image_set_has_client_clip (pixman_image_t *image,
442                                   pixman_bool_t   client_clip)
443 {
444     image->common.client_clip = client_clip;
445 }
446
447 PIXMAN_EXPORT pixman_bool_t
448 pixman_image_set_transform (pixman_image_t *          image,
449                             const pixman_transform_t *transform)
450 {
451     static const pixman_transform_t id =
452     {
453         { { pixman_fixed_1, 0, 0 },
454           { 0, pixman_fixed_1, 0 },
455           { 0, 0, pixman_fixed_1 } }
456     };
457
458     image_common_t *common = (image_common_t *)image;
459     pixman_bool_t result;
460
461     if (common->transform == transform)
462         return TRUE;
463
464     if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
465     {
466         free (common->transform);
467         common->transform = NULL;
468         result = TRUE;
469
470         goto out;
471     }
472
473     if (common->transform == NULL)
474         common->transform = malloc (sizeof (pixman_transform_t));
475
476     if (common->transform == NULL)
477     {
478         result = FALSE;
479
480         goto out;
481     }
482
483     memcpy (common->transform, transform, sizeof(pixman_transform_t));
484
485     result = TRUE;
486
487 out:
488     image_property_changed (image);
489
490     return result;
491 }
492
493 PIXMAN_EXPORT void
494 pixman_image_set_repeat (pixman_image_t *image,
495                          pixman_repeat_t repeat)
496 {
497     image->common.repeat = repeat;
498
499     image_property_changed (image);
500 }
501
502 PIXMAN_EXPORT pixman_bool_t
503 pixman_image_set_filter (pixman_image_t *      image,
504                          pixman_filter_t       filter,
505                          const pixman_fixed_t *params,
506                          int                   n_params)
507 {
508     image_common_t *common = (image_common_t *)image;
509     pixman_fixed_t *new_params;
510
511     if (params == common->filter_params && filter == common->filter)
512         return TRUE;
513
514     new_params = NULL;
515     if (params)
516     {
517         new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
518         if (!new_params)
519             return FALSE;
520
521         memcpy (new_params,
522                 params, n_params * sizeof (pixman_fixed_t));
523     }
524
525     common->filter = filter;
526
527     if (common->filter_params)
528         free (common->filter_params);
529
530     common->filter_params = new_params;
531     common->n_filter_params = n_params;
532
533     image_property_changed (image);
534     return TRUE;
535 }
536
537 PIXMAN_EXPORT void
538 pixman_image_set_source_clipping (pixman_image_t *image,
539                                   pixman_bool_t   clip_sources)
540 {
541     image->common.clip_sources = clip_sources;
542
543     image_property_changed (image);
544 }
545
546 /* Unlike all the other property setters, this function does not
547  * copy the content of indexed. Doing this copying is simply
548  * way, way too expensive.
549  */
550 PIXMAN_EXPORT void
551 pixman_image_set_indexed (pixman_image_t *        image,
552                           const pixman_indexed_t *indexed)
553 {
554     bits_image_t *bits = (bits_image_t *)image;
555
556     bits->indexed = indexed;
557
558     image_property_changed (image);
559 }
560
561 PIXMAN_EXPORT void
562 pixman_image_set_alpha_map (pixman_image_t *image,
563                             pixman_image_t *alpha_map,
564                             int16_t         x,
565                             int16_t         y)
566 {
567     image_common_t *common = (image_common_t *)image;
568
569     return_if_fail (!alpha_map || alpha_map->type == BITS);
570
571     if (common->alpha_map != (bits_image_t *)alpha_map)
572     {
573         if (common->alpha_map)
574             pixman_image_unref ((pixman_image_t *)common->alpha_map);
575
576         if (alpha_map)
577             common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
578         else
579             common->alpha_map = NULL;
580     }
581
582     common->alpha_origin_x = x;
583     common->alpha_origin_y = y;
584
585     image_property_changed (image);
586 }
587
588 PIXMAN_EXPORT void
589 pixman_image_set_component_alpha   (pixman_image_t *image,
590                                     pixman_bool_t   component_alpha)
591 {
592     image->common.component_alpha = component_alpha;
593
594     image_property_changed (image);
595 }
596
597 PIXMAN_EXPORT void
598 pixman_image_set_accessors (pixman_image_t *           image,
599                             pixman_read_memory_func_t  read_func,
600                             pixman_write_memory_func_t write_func)
601 {
602     return_if_fail (image != NULL);
603
604     if (image->type == BITS)
605     {
606         image->bits.read_func = read_func;
607         image->bits.write_func = write_func;
608
609         image_property_changed (image);
610     }
611 }
612
613 PIXMAN_EXPORT uint32_t *
614 pixman_image_get_data (pixman_image_t *image)
615 {
616     if (image->type == BITS)
617         return image->bits.bits;
618
619     return NULL;
620 }
621
622 PIXMAN_EXPORT int
623 pixman_image_get_width (pixman_image_t *image)
624 {
625     if (image->type == BITS)
626         return image->bits.width;
627
628     return 0;
629 }
630
631 PIXMAN_EXPORT int
632 pixman_image_get_height (pixman_image_t *image)
633 {
634     if (image->type == BITS)
635         return image->bits.height;
636
637     return 0;
638 }
639
640 PIXMAN_EXPORT int
641 pixman_image_get_stride (pixman_image_t *image)
642 {
643     if (image->type == BITS)
644         return image->bits.rowstride * (int) sizeof (uint32_t);
645
646     return 0;
647 }
648
649 PIXMAN_EXPORT int
650 pixman_image_get_depth (pixman_image_t *image)
651 {
652     if (image->type == BITS)
653         return PIXMAN_FORMAT_DEPTH (image->bits.format);
654
655     return 0;
656 }
657
658 uint32_t
659 _pixman_image_get_solid (pixman_image_t *     image,
660                          pixman_format_code_t format)
661 {
662     uint32_t result;
663
664     _pixman_image_get_scanline_32 (image, 0, 0, 1, &result, NULL, 0);
665
666     /* If necessary, convert RGB <--> BGR. */
667     if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
668     {
669         result = (((result & 0xff000000) >>  0) |
670                   ((result & 0x00ff0000) >> 16) |
671                   ((result & 0x0000ff00) >>  0) |
672                   ((result & 0x000000ff) << 16));
673     }
674
675     return result;
676 }