Replace pixman_image_get_fetchers() with pixman_image_get_scanline_{32,64}
[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 #define Alpha(x) ((x) >> 24)
35
36 pixman_bool_t
37 _pixman_init_gradient (gradient_t     *gradient,
38                        const pixman_gradient_stop_t *stops,
39                        int             n_stops)
40 {
41     return_val_if_fail (n_stops > 0, FALSE);
42
43     gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
44     if (!gradient->stops)
45         return FALSE;
46
47     memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
48
49     gradient->n_stops = n_stops;
50
51     gradient->stop_range = 0xffff;
52     gradient->color_table = NULL;
53     gradient->color_table_size = 0;
54     gradient->common.class = SOURCE_IMAGE_CLASS_UNKNOWN;
55
56     return TRUE;
57 }
58
59 pixman_image_t *
60 _pixman_image_allocate (void)
61 {
62     pixman_image_t *image = malloc (sizeof (pixman_image_t));
63
64     if (image)
65     {
66         image_common_t *common = &image->common;
67
68         pixman_region32_init (&common->full_region);
69         pixman_region32_init (&common->clip_region);
70         common->src_clip = &common->full_region;
71         common->has_client_clip = FALSE;
72         common->transform = NULL;
73         common->repeat = PIXMAN_REPEAT_NONE;
74         common->filter = PIXMAN_FILTER_NEAREST;
75         common->filter_params = NULL;
76         common->n_filter_params = 0;
77         common->alpha_map = NULL;
78         common->component_alpha = FALSE;
79         common->ref_count = 1;
80         common->read_func = NULL;
81         common->write_func = NULL;
82         common->classify = NULL;
83     }
84
85     return image;
86 }
87
88 source_pict_class_t
89 _pixman_image_classify (pixman_image_t *image,
90                         int             x,
91                         int             y,
92                         int             width,
93                         int             height)
94 {
95     if (image->common.classify)
96         return image->common.classify (image, x, y, width, height);
97     else
98         return SOURCE_IMAGE_CLASS_UNKNOWN;
99 }
100
101 void
102 _pixman_image_get_scanline_32 (pixman_image_t *image, int x, int y, int width,
103                                uint32_t *buffer, uint32_t *mask, uint32_t mask_bits)
104 {
105     image->common.get_scanline_32 (image, x, y, width, buffer, mask, mask_bits);
106 }
107
108 void
109 _pixman_image_get_scanline_64 (pixman_image_t *image, int x, int y, int width,
110                                uint32_t *buffer, uint32_t *unused, uint32_t unused2)
111 {
112     image->common.get_scanline_64 (image, x, y, width, buffer, unused, unused2);
113 }
114
115 /* Even thought the type of buffer is uint32_t *, the function actually expects
116  * a uint64_t *buffer.
117  */
118
119 scanFetchProc
120 _pixman_image_get_fetcher (pixman_image_t *image,
121                            int             wide)
122 {
123     assert (image->common.get_scanline_64);
124     assert (image->common.get_scanline_32);
125     
126     if (wide)
127         return image->common.get_scanline_64;
128     else
129         return image->common.get_scanline_32;
130 }
131
132 #define WRITE_ACCESS(f) ((image->common.write_func)? f##_accessors : f)
133
134 static void
135 fbStore(bits_image_t * image, int x, int y, int width, uint32_t *buffer)
136 {
137     uint32_t *bits;
138     int32_t stride;
139     storeProc32 store = WRITE_ACCESS(pixman_storeProcForPicture32)(image);
140     const pixman_indexed_t * indexed = image->indexed;
141
142     bits = image->bits;
143     stride = image->rowstride;
144     bits += y*stride;
145     store((pixman_image_t *)image, bits, buffer, x, width, indexed);
146 }
147
148 static void
149 fbStore64(bits_image_t * image, int x, int y, int width, uint64_t *buffer)
150 {
151     uint32_t *bits;
152     int32_t stride;
153     storeProc64 store = WRITE_ACCESS(pixman_storeProcForPicture64)(image);
154     const pixman_indexed_t * indexed = image->indexed;
155
156     bits = image->bits;
157     stride = image->rowstride;
158     bits += y*stride;
159     store((pixman_image_t *)image, bits, buffer, x, width, indexed);
160 }
161
162 scanStoreProc
163 _pixman_image_get_storer (pixman_image_t *image,
164                           int             wide)
165 {
166     if (image->common.alpha_map)
167     {
168         if (wide)
169             return (scanStoreProc)WRITE_ACCESS(fbStoreExternalAlpha64);
170         else
171             return (scanStoreProc)WRITE_ACCESS(fbStoreExternalAlpha);
172     }
173     else
174     {
175         if (wide)
176             return (scanStoreProc)fbStore64;
177         else
178             return (scanStoreProc)fbStore;
179     }
180 }
181
182 static void
183 image_property_changed (pixman_image_t *image)
184 {
185     
186     
187     image->common.property_changed (image);
188 }
189
190 /* Ref Counting */
191 PIXMAN_EXPORT pixman_image_t *
192 pixman_image_ref (pixman_image_t *image)
193 {
194     image->common.ref_count++;
195
196     return image;
197 }
198
199 /* returns TRUE when the image is freed */
200 PIXMAN_EXPORT pixman_bool_t
201 pixman_image_unref (pixman_image_t *image)
202 {
203     image_common_t *common = (image_common_t *)image;
204
205     common->ref_count--;
206
207     if (common->ref_count == 0)
208     {
209         pixman_region32_fini (&common->clip_region);
210         pixman_region32_fini (&common->full_region);
211
212         if (common->transform)
213             free (common->transform);
214
215         if (common->filter_params)
216             free (common->filter_params);
217
218         if (common->alpha_map)
219             pixman_image_unref ((pixman_image_t *)common->alpha_map);
220
221 #if 0
222         if (image->type == BITS && image->bits.indexed)
223             free (image->bits.indexed);
224 #endif
225
226 #if 0
227         memset (image, 0xaa, sizeof (pixman_image_t));
228 #endif
229         if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
230         {
231             if (image->gradient.stops)
232                 free (image->gradient.stops);
233         }
234
235
236         if (image->type == BITS && image->bits.free_me)
237             free (image->bits.free_me);
238
239         free (image);
240
241         return TRUE;
242     }
243
244     return FALSE;
245 }
246
247 /* Constructors */
248
249 void
250 _pixman_image_reset_clip_region (pixman_image_t *image)
251 {
252     pixman_region32_fini (&image->common.clip_region);
253
254     if (image->type == BITS)
255     {
256         pixman_region32_init_rect (&image->common.clip_region, 0, 0,
257                                    image->bits.width, image->bits.height);
258     }
259     else
260     {
261         pixman_region32_init (&image->common.clip_region);
262     }
263 }
264
265 PIXMAN_EXPORT pixman_bool_t
266 pixman_image_set_clip_region32 (pixman_image_t *image,
267                                 pixman_region32_t *region)
268 {
269     image_common_t *common = (image_common_t *)image;
270     pixman_bool_t result;
271
272     if (region)
273     {
274         result = pixman_region32_copy (&common->clip_region, region);
275     }
276     else
277     {
278         _pixman_image_reset_clip_region (image);
279
280         result = TRUE;
281     }
282
283     image_property_changed (image);
284
285     return result;
286 }
287
288
289 PIXMAN_EXPORT pixman_bool_t
290 pixman_image_set_clip_region (pixman_image_t    *image,
291                               pixman_region16_t *region)
292 {
293     image_common_t *common = (image_common_t *)image;
294     pixman_bool_t result;
295
296     if (region)
297     {
298         result = pixman_region32_copy_from_region16 (&common->clip_region, region);
299     }
300     else
301     {
302         _pixman_image_reset_clip_region (image);
303
304         result = TRUE;
305     }
306
307     image_property_changed (image);
308
309     return result;
310 }
311
312 /* Sets whether the clip region includes a clip region set by the client
313  */
314 PIXMAN_EXPORT void
315 pixman_image_set_has_client_clip (pixman_image_t *image,
316                                   pixman_bool_t   client_clip)
317 {
318     image->common.has_client_clip = client_clip;
319
320     image_property_changed (image);
321 }
322
323 PIXMAN_EXPORT pixman_bool_t
324 pixman_image_set_transform (pixman_image_t           *image,
325                             const pixman_transform_t *transform)
326 {
327     static const pixman_transform_t id =
328     {
329         { { pixman_fixed_1, 0, 0 },
330           { 0, pixman_fixed_1, 0 },
331           { 0, 0, pixman_fixed_1 }
332         }
333     };
334
335     image_common_t *common = (image_common_t *)image;
336     pixman_bool_t result;
337
338     if (common->transform == transform)
339         return TRUE;
340
341     if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
342     {
343         free(common->transform);
344         common->transform = NULL;
345         result = TRUE;
346         goto out;
347     }
348
349     if (common->transform == NULL)
350         common->transform = malloc (sizeof (pixman_transform_t));
351
352     if (common->transform == NULL)
353     {
354         result = FALSE;
355         goto out;
356     }
357
358     memcpy(common->transform, transform, sizeof(pixman_transform_t));
359
360 out:
361     image_property_changed (image);
362     
363     return TRUE;
364 }
365
366 PIXMAN_EXPORT void
367 pixman_image_set_repeat (pixman_image_t  *image,
368                          pixman_repeat_t  repeat)
369 {
370     image->common.repeat = repeat;
371
372     image_property_changed (image);
373 }
374
375 PIXMAN_EXPORT pixman_bool_t
376 pixman_image_set_filter (pixman_image_t       *image,
377                          pixman_filter_t       filter,
378                          const pixman_fixed_t *params,
379                          int                   n_params)
380 {
381     image_common_t *common = (image_common_t *)image;
382     pixman_fixed_t *new_params;
383
384     if (params == common->filter_params && filter == common->filter)
385         return TRUE;
386
387     new_params = NULL;
388     if (params)
389     {
390         new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
391         if (!new_params)
392             return FALSE;
393
394         memcpy (new_params,
395                 params, n_params * sizeof (pixman_fixed_t));
396     }
397
398     common->filter = filter;
399
400     if (common->filter_params)
401         free (common->filter_params);
402
403     common->filter_params = new_params;
404     common->n_filter_params = n_params;
405
406     image_property_changed (image);
407     return TRUE;
408 }
409
410 PIXMAN_EXPORT void
411 pixman_image_set_source_clipping (pixman_image_t  *image,
412                                   pixman_bool_t    source_clipping)
413 {
414     image_common_t *common = &image->common;
415
416     if (source_clipping)
417         common->src_clip = &common->clip_region;
418     else
419         common->src_clip = &common->full_region;
420
421     image_property_changed (image);
422 }
423
424 /* Unlike all the other property setters, this function does not
425  * copy the content of indexed. Doing this copying is simply
426  * way, way too expensive.
427  */
428 PIXMAN_EXPORT void
429 pixman_image_set_indexed (pixman_image_t         *image,
430                           const pixman_indexed_t *indexed)
431 {
432     bits_image_t *bits = (bits_image_t *)image;
433
434     bits->indexed = indexed;
435
436     image_property_changed (image);
437 }
438
439 PIXMAN_EXPORT void
440 pixman_image_set_alpha_map (pixman_image_t *image,
441                             pixman_image_t *alpha_map,
442                             int16_t         x,
443                             int16_t         y)
444 {
445     image_common_t *common = (image_common_t *)image;
446
447     return_if_fail (!alpha_map || alpha_map->type == BITS);
448
449     if (common->alpha_map != (bits_image_t *)alpha_map)
450     {
451         if (common->alpha_map)
452             pixman_image_unref ((pixman_image_t *)common->alpha_map);
453
454         if (alpha_map)
455             common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
456         else
457             common->alpha_map = NULL;
458     }
459
460     common->alpha_origin.x = x;
461     common->alpha_origin.y = y;
462
463     image_property_changed (image);
464 }
465
466 PIXMAN_EXPORT void
467 pixman_image_set_component_alpha   (pixman_image_t       *image,
468                                     pixman_bool_t         component_alpha)
469 {
470     image->common.component_alpha = component_alpha;
471
472     image_property_changed (image);
473 }
474
475
476 PIXMAN_EXPORT void
477 pixman_image_set_accessors (pixman_image_t             *image,
478                             pixman_read_memory_func_t   read_func,
479                             pixman_write_memory_func_t  write_func)
480 {
481     return_if_fail (image != NULL);
482
483     image->common.read_func = read_func;
484     image->common.write_func = write_func;
485
486     image_property_changed (image);
487 }
488
489 PIXMAN_EXPORT uint32_t *
490 pixman_image_get_data (pixman_image_t *image)
491 {
492     if (image->type == BITS)
493         return image->bits.bits;
494
495     return NULL;
496 }
497
498 PIXMAN_EXPORT int
499 pixman_image_get_width (pixman_image_t *image)
500 {
501     if (image->type == BITS)
502         return image->bits.width;
503
504     return 0;
505 }
506
507 PIXMAN_EXPORT int
508 pixman_image_get_height (pixman_image_t *image)
509 {
510     if (image->type == BITS)
511         return image->bits.height;
512
513     return 0;
514 }
515
516 PIXMAN_EXPORT int
517 pixman_image_get_stride (pixman_image_t *image)
518 {
519     if (image->type == BITS)
520         return image->bits.rowstride * (int) sizeof (uint32_t);
521
522     return 0;
523 }
524
525 PIXMAN_EXPORT int
526 pixman_image_get_depth (pixman_image_t *image)
527 {
528     if (image->type == BITS)
529         return PIXMAN_FORMAT_DEPTH (image->bits.format);
530
531     return 0;
532 }
533
534 static uint32_t
535 color_to_uint32 (const pixman_color_t *color)
536 {
537     return
538         (color->alpha >> 8 << 24) |
539         (color->red >> 8 << 16) |
540         (color->green & 0xff00) |
541         (color->blue >> 8);
542 }
543
544 static pixman_bool_t
545 color_to_pixel (pixman_color_t *color,
546                 uint32_t       *pixel,
547                 pixman_format_code_t format)
548 {
549     uint32_t c = color_to_uint32 (color);
550
551     if (!(format == PIXMAN_a8r8g8b8     ||
552           format == PIXMAN_x8r8g8b8     ||
553           format == PIXMAN_a8b8g8r8     ||
554           format == PIXMAN_x8b8g8r8     ||
555           format == PIXMAN_b8g8r8a8     ||
556           format == PIXMAN_b8g8r8x8     ||
557           format == PIXMAN_r5g6b5       ||
558           format == PIXMAN_b5g6r5       ||
559           format == PIXMAN_a8))
560     {
561         return FALSE;
562     }
563
564     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
565     {
566         c = ((c & 0xff000000) >>  0) |
567             ((c & 0x00ff0000) >> 16) |
568             ((c & 0x0000ff00) >>  0) |
569             ((c & 0x000000ff) << 16);
570     }
571     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
572     {
573         c = ((c & 0xff000000) >> 24) |
574             ((c & 0x00ff0000) >>  8) |
575             ((c & 0x0000ff00) <<  8) |
576             ((c & 0x000000ff) << 24);
577     }
578
579     if (format == PIXMAN_a8)
580         c = c >> 24;
581     else if (format == PIXMAN_r5g6b5 ||
582              format == PIXMAN_b5g6r5)
583         c = cvt8888to0565 (c);
584
585 #if 0
586     printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
587     printf ("pixel: %x\n", c);
588 #endif
589
590     *pixel = c;
591     return TRUE;
592 }
593
594 PIXMAN_EXPORT pixman_bool_t
595 pixman_image_fill_rectangles (pixman_op_t                   op,
596                               pixman_image_t               *dest,
597                               pixman_color_t               *color,
598                               int                           n_rects,
599                               const pixman_rectangle16_t   *rects)
600 {
601     pixman_image_t *solid;
602     pixman_color_t c;
603     int i;
604
605     if (color->alpha == 0xffff)
606     {
607         if (op == PIXMAN_OP_OVER)
608             op = PIXMAN_OP_SRC;
609     }
610
611     if (op == PIXMAN_OP_CLEAR)
612     {
613         c.red = 0;
614         c.green = 0;
615         c.blue = 0;
616         c.alpha = 0;
617
618         color = &c;
619
620         op = PIXMAN_OP_SRC;
621     }
622
623     if (op == PIXMAN_OP_SRC)
624     {
625         uint32_t pixel;
626
627         if (color_to_pixel (color, &pixel, dest->bits.format))
628         {
629             for (i = 0; i < n_rects; ++i)
630             {
631                 pixman_region32_t fill_region;
632                 int n_boxes, j;
633                 pixman_box32_t *boxes;
634
635                 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
636                 if (!pixman_region32_intersect (&fill_region,
637                                                 &fill_region,
638                                                 &dest->common.clip_region))
639                     return FALSE;
640
641
642                 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
643                 for (j = 0; j < n_boxes; ++j)
644                 {
645                     const pixman_box32_t *box = &(boxes[j]);
646                     pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
647                                  box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
648                                  pixel);
649                 }
650
651                 pixman_region32_fini (&fill_region);
652             }
653             return TRUE;
654         }
655     }
656
657     solid = pixman_image_create_solid_fill (color);
658     if (!solid)
659         return FALSE;
660
661     for (i = 0; i < n_rects; ++i)
662     {
663         const pixman_rectangle16_t *rect = &(rects[i]);
664
665         pixman_image_composite (op, solid, NULL, dest,
666                                 0, 0, 0, 0,
667                                 rect->x, rect->y,
668                                 rect->width, rect->height);
669     }
670
671     pixman_image_unref (solid);
672
673     return TRUE;
674 }
675
676 pixman_bool_t
677 pixman_image_can_get_solid (pixman_image_t *image)
678 {
679     if (image->type == SOLID)
680         return TRUE;
681
682     if (image->type != BITS     ||
683         image->bits.width != 1  ||
684         image->bits.height != 1)
685     {
686         return FALSE;
687     }
688
689     if (image->common.repeat != PIXMAN_REPEAT_NORMAL)
690         return FALSE;
691
692     switch (image->bits.format)
693     {
694     case PIXMAN_a8r8g8b8:
695     case PIXMAN_x8r8g8b8:
696     case PIXMAN_a8b8g8r8:
697     case PIXMAN_x8b8g8r8:
698     case PIXMAN_b8g8r8a8:
699     case PIXMAN_b8g8r8x8:
700     case PIXMAN_r8g8b8:
701     case PIXMAN_b8g8r8:
702     case PIXMAN_r5g6b5:
703     case PIXMAN_b5g6r5:
704         return TRUE;
705     default:
706         return FALSE;
707     }
708 }
709
710 pixman_bool_t
711 pixman_image_is_opaque(pixman_image_t *image)
712 {
713     int i = 0;
714     int gradientNumberOfColors = 0;
715
716     if(image->common.alpha_map)
717         return FALSE;
718
719     switch(image->type)
720     {
721     case BITS:
722         if(PIXMAN_FORMAT_A(image->bits.format))
723             return FALSE;
724         break;
725
726     case LINEAR:
727     case CONICAL:
728     case RADIAL:
729         gradientNumberOfColors = image->gradient.n_stops;
730         i=0;
731         while(i<gradientNumberOfColors)
732         {
733             if(image->gradient.stops[i].color.alpha != 0xffff)
734                 return FALSE;
735             i++;
736         }
737         break;
738
739     case SOLID:
740          if(Alpha(image->solid.color) != 0xff)
741             return FALSE;
742         break;
743     }
744
745     /* Convolution filters can introduce translucency if the sum of the weights
746        is lower than 1. */
747     if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)
748          return FALSE;
749
750     if (image->common.repeat == PIXMAN_REPEAT_NONE)
751     {
752         if (image->common.filter != PIXMAN_FILTER_NEAREST)
753             return FALSE;
754
755         if (image->common.transform)
756             return FALSE;
757
758         /* Gradients do not necessarily cover the entire compositing area */
759         if (image->type == LINEAR || image->type == CONICAL || image->type == RADIAL)
760             return FALSE;
761     }
762
763      return TRUE;
764 }