Delete unused IS_SOURCE_IMAGE() macro
[profile/ivi/pixman.git] / pixman / pixman-private.h
1 #ifndef PACKAGE
2 #  error config.h must be included before pixman-private.h
3 #endif
4
5 #ifndef PIXMAN_PRIVATE_H
6 #define PIXMAN_PRIVATE_H
7
8 #include "pixman.h"
9 #include <time.h>
10 #include <assert.h>
11
12 #ifndef FALSE
13 #define FALSE 0
14 #endif
15
16 #ifndef TRUE
17 #define TRUE 1
18 #endif
19
20 #define MSBFirst 0
21 #define LSBFirst 1
22
23 #ifdef WORDS_BIGENDIAN
24 #  define IMAGE_BYTE_ORDER MSBFirst
25 #  define BITMAP_BIT_ORDER MSBFirst
26 #else
27 #  define IMAGE_BYTE_ORDER LSBFirst
28 #  define BITMAP_BIT_ORDER LSBFirst
29 #endif
30
31 #undef DEBUG
32 #define DEBUG 0
33
34 #if defined (__GNUC__)
35 #  define FUNC     ((const char*) (__PRETTY_FUNCTION__))
36 #elif defined (__sun) || (defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
37 #  define FUNC     ((const char*) (__func__))
38 #else
39 #  define FUNC     ((const char*) ("???"))
40 #endif
41
42 #ifndef INT16_MIN
43 # define INT16_MIN              (-32767-1)
44 #endif
45
46 #ifndef INT16_MAX
47 # define INT16_MAX              (32767)
48 #endif
49
50 #ifndef INT32_MIN
51 # define INT32_MIN              (-2147483647-1)
52 #endif
53
54 #ifndef INT32_MAX
55 # define INT32_MAX              (2147483647)
56 #endif
57
58 #ifndef UINT32_MIN
59 # define UINT32_MIN             (0)
60 #endif
61
62 #ifndef UINT32_MAX
63 # define UINT32_MAX             (4294967295U)
64 #endif
65
66 #ifndef M_PI
67 # define M_PI                   3.14159265358979323846
68 #endif
69
70 #ifdef _MSC_VER
71 /* 'inline' is available only in C++ in MSVC */
72 #   define inline __inline
73 #   define force_inline __forceinline
74 #elif defined __GNUC__ || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
75 #   define inline __inline__
76 #   define force_inline __inline__ __attribute__ ((__always_inline__))
77 #else
78 # ifndef force_inline
79 #  define force_inline inline
80 # endif
81 #endif
82
83 #define FB_SHIFT    5
84 #define FB_UNIT     (1 << FB_SHIFT)
85 #define FB_HALFUNIT (1 << (FB_SHIFT-1))
86 #define FB_MASK     (FB_UNIT - 1)
87 #define FB_ALLONES  ((uint32_t) -1)
88
89 /* Memory allocation helpers */
90 void *pixman_malloc_ab (unsigned int n, unsigned int b);
91 void *pixman_malloc_abc (unsigned int a, unsigned int b, unsigned int c);
92 pixman_bool_t pixman_multiply_overflows_int (unsigned int a, unsigned int b);
93 pixman_bool_t pixman_addition_overflows_int (unsigned int a, unsigned int b);
94
95 #if DEBUG
96
97 #define return_if_fail(expr)                                            \
98     do                                                                  \
99     {                                                                   \
100         if (!(expr))                                                    \
101         {                                                               \
102             fprintf(stderr, "In %s: %s failed\n", FUNC, #expr);         \
103             return;                                                     \
104         }                                                               \
105     }                                                                   \
106     while (0)
107
108 #define return_val_if_fail(expr, retval)                                \
109     do                                                                  \
110     {                                                                   \
111         if (!(expr))                                                    \
112         {                                                               \
113             fprintf(stderr, "In %s: %s failed\n", FUNC, #expr);         \
114             return (retval);                                            \
115         }                                                               \
116     }                                                                   \
117     while (0)
118
119 #else
120
121 #define return_if_fail(expr)                                            \
122     do                                                                  \
123     {                                                                   \
124         if (!(expr))                                                    \
125             return;                                                     \
126     }                                                                   \
127     while (0)
128
129 #define return_val_if_fail(expr, retval)                                \
130     do                                                                  \
131     {                                                                   \
132         if (!(expr))                                                    \
133             return (retval);                                            \
134     }                                                                   \
135     while (0)
136
137 #endif
138
139 typedef struct image_common image_common_t;
140 typedef struct source_image source_image_t;
141 typedef struct solid_fill solid_fill_t;
142 typedef struct gradient gradient_t;
143 typedef struct linear_gradient linear_gradient_t;
144 typedef struct horizontal_gradient horizontal_gradient_t;
145 typedef struct vertical_gradient vertical_gradient_t;
146 typedef struct conical_gradient conical_gradient_t;
147 typedef struct radial_gradient radial_gradient_t;
148 typedef struct bits_image bits_image_t;
149 typedef struct circle circle_t;
150 typedef struct point point_t;
151
152 /* FIXME - the types and structures below should be give proper names
153  */
154
155 typedef void (*fetchProc32)(bits_image_t *pict, int x, int y, int width,
156                                      uint32_t *buffer);
157 typedef uint32_t (*fetchPixelProc32)(bits_image_t *pict, int offset, int line);
158 typedef void (*storeProc32)(pixman_image_t *, uint32_t *bits,
159                                      const uint32_t *values, int x, int width,
160                                      const pixman_indexed_t *);
161
162 typedef void (*fetchProc64)(bits_image_t *pict, int x, int y, int width,
163                                      uint64_t *buffer);
164 typedef uint64_t (*fetchPixelProc64)(bits_image_t *pict, int offset, int line);
165 typedef void (*storeProc64)(pixman_image_t *, uint32_t *bits,
166                                      const uint64_t *values, int x, int width,
167                                      const pixman_indexed_t *);
168
169 typedef void (* fetch_pixels_32_t) (bits_image_t *image, uint32_t *buffer, int n_pixels);
170 typedef void (* fetch_pixels_64_t) (bits_image_t *image, uint64_t *buffer, int n_pixels);
171
172 fetchProc32 pixman_fetchProcForPicture32 (bits_image_t *);
173 fetch_pixels_32_t pixman_fetchPixelProcForPicture32 (bits_image_t *);
174 storeProc32 pixman_storeProcForPicture32 (bits_image_t *);
175 fetchProc32 pixman_fetchProcForPicture32_accessors (bits_image_t *);
176 fetch_pixels_32_t pixman_fetchPixelProcForPicture32_accessors (bits_image_t *);
177 storeProc32 pixman_storeProcForPicture32_accessors (bits_image_t *);
178
179 fetchProc64 pixman_fetchProcForPicture64 (bits_image_t *);
180 fetch_pixels_64_t pixman_fetchPixelProcForPicture64 (bits_image_t *);
181 storeProc64 pixman_storeProcForPicture64 (bits_image_t *);
182 fetchProc64 pixman_fetchProcForPicture64_accessors (bits_image_t *);
183 fetch_pixels_64_t pixman_fetchPixelProcForPicture64_accessors (bits_image_t *);
184 storeProc64 pixman_storeProcForPicture64_accessors (bits_image_t *);
185
186 void pixman_expand(uint64_t *dst, const uint32_t *src, pixman_format_code_t, int width);
187 void pixman_contract(uint32_t *dst, const uint64_t *src, int width);
188
189 void _pixman_image_get_scanline_64_generic (pixman_image_t * pict, int x, int y, int width,
190                                             uint64_t *buffer, uint64_t *mask, uint32_t maskBits);
191
192 /* end */
193
194 typedef enum
195 {
196     BITS,
197     LINEAR,
198     CONICAL,
199     RADIAL,
200     SOLID
201 } image_type_t;
202
203 typedef enum
204 {
205     SOURCE_IMAGE_CLASS_UNKNOWN,
206     SOURCE_IMAGE_CLASS_HORIZONTAL,
207     SOURCE_IMAGE_CLASS_VERTICAL,
208 } source_pict_class_t;
209
210 typedef void (*scanStoreProc)(bits_image_t *img, int x, int y, int width, uint32_t *buffer);
211 typedef void (*scanFetchProc)(pixman_image_t *, int, int, int, uint32_t *,
212                               uint32_t *, uint32_t);
213
214 source_pict_class_t _pixman_image_classify (pixman_image_t *image,
215                                             int             x,
216                                             int             y,
217                                             int             width,
218                                             int             height);
219
220 void
221 _pixman_image_get_scanline_32 (pixman_image_t *image, int x, int y, int width,
222                                uint32_t *buffer, uint32_t *mask, uint32_t mask_bits);
223
224 /* Even thought the type of buffer is uint32_t *, the function actually expects
225  * a uint64_t *buffer.
226  */
227 void
228 _pixman_image_get_scanline_64 (pixman_image_t *image, int x, int y, int width,
229                                uint32_t *buffer, uint32_t *unused, uint32_t unused2);
230
231 void
232 _pixman_image_store_scanline_32 (bits_image_t *image, int x, int y, int width,
233                                  uint32_t *buffer);
234 void
235 _pixman_image_fetch_pixels (bits_image_t *image, uint32_t *buffer, int n_pixels);
236
237 /* Even thought the type of buffer is uint32_t *, the function actually expects
238  * a uint64_t *buffer.
239  */
240 void
241 _pixman_image_store_scanline_64 (bits_image_t *image, int x, int y, int width,
242                                  uint32_t *buffer);
243
244 pixman_image_t *
245 _pixman_image_allocate (void);
246
247 pixman_bool_t
248 _pixman_init_gradient (gradient_t     *gradient,
249                        const pixman_gradient_stop_t *stops,
250                        int             n_stops);
251 void
252 _pixman_image_reset_clip_region (pixman_image_t *image);
253
254 struct point
255 {
256     int16_t x, y;
257 };
258
259 typedef source_pict_class_t (* classify_func_t) (pixman_image_t *image,
260                                                  int             x,
261                                                  int             y,
262                                                  int             width,
263                                                  int             height);
264 typedef void (* property_changed_func_t)        (pixman_image_t *image);
265
266 struct image_common
267 {
268     image_type_t                type;
269     int32_t                     ref_count;
270     pixman_region32_t           clip_region;
271     pixman_bool_t               have_clip_region;       /* FALSE if there is no clip */
272     pixman_bool_t               client_clip;    /* Whether the source clip was set by a client */
273     pixman_bool_t               clip_sources;           /* Whether the clip applies when
274                                                          * the image is used as a source
275                                                          */
276     pixman_transform_t         *transform;
277     pixman_repeat_t             repeat;
278     pixman_filter_t             filter;
279     pixman_fixed_t             *filter_params;
280     int                         n_filter_params;
281     bits_image_t               *alpha_map;
282     point_t                     alpha_origin;
283     pixman_bool_t               component_alpha;
284     pixman_read_memory_func_t   read_func;
285     pixman_write_memory_func_t  write_func;
286     classify_func_t             classify;
287     property_changed_func_t     property_changed;
288     scanFetchProc               get_scanline_32;
289     scanFetchProc               get_scanline_64;
290
291     pixman_image_destroy_func_t destroy_func;
292     void *                      destroy_data;
293 };
294
295 struct source_image
296 {
297     image_common_t      common;
298     source_pict_class_t class;
299 };
300
301 struct solid_fill
302 {
303     source_image_t      common;
304     uint32_t            color;          /* FIXME: shouldn't this be a pixman_color_t? */
305 };
306
307 struct gradient
308 {
309     source_image_t              common;
310     int                         n_stops;
311     pixman_gradient_stop_t *    stops;
312     int                         stop_range;
313     uint32_t *                  color_table;
314     int                         color_table_size;
315 };
316
317 struct linear_gradient
318 {
319     gradient_t                  common;
320     pixman_point_fixed_t        p1;
321     pixman_point_fixed_t        p2;
322 };
323
324 struct circle
325 {
326     pixman_fixed_t x;
327     pixman_fixed_t y;
328     pixman_fixed_t radius;
329 };
330
331 struct radial_gradient
332 {
333     gradient_t  common;
334
335     circle_t    c1;
336     circle_t    c2;
337     double      cdx;
338     double      cdy;
339     double      dr;
340     double      A;
341 };
342
343 struct conical_gradient
344 {
345     gradient_t                  common;
346     pixman_point_fixed_t        center;
347     pixman_fixed_t              angle;
348 };
349
350 struct bits_image
351 {
352     image_common_t              common;
353     pixman_format_code_t        format;
354     const pixman_indexed_t     *indexed;
355     int                         width;
356     int                         height;
357     uint32_t *                  bits;
358     uint32_t *                  free_me;
359     int                         rowstride; /* in number of uint32_t's */
360
361     /* Fetch raw pixels, with no regard for transformations, alpha map etc. */
362     fetch_pixels_32_t           fetch_pixels_raw_32;
363     fetch_pixels_64_t           fetch_pixels_raw_64;
364
365     /* Fetch raw scanlines, with no regard for transformations, alpha maps etc. */
366     fetchProc32                 fetch_scanline_raw_32;
367     fetchProc64                 fetch_scanline_raw_64;
368
369     /* Store scanlines with no regard for alpha maps */
370     storeProc32                 store_scanline_raw_32;
371     storeProc64                 store_scanline_raw_64;
372
373     /* Store a scanline, taking alpha maps into account */
374     scanStoreProc               store_scanline_32;
375     scanStoreProc               store_scanline_64;
376
377     /* Used for indirect access to the bits */
378     pixman_read_memory_func_t   read_func;
379     pixman_write_memory_func_t  write_func;
380     
381     void *                      orig_data;      /* Stores pointer to original
382                                                  * data, when the image is
383                                                  * being accessed.
384                                                  */
385 };
386
387 union pixman_image
388 {
389     image_type_t                type;
390     image_common_t              common;
391     bits_image_t                bits;
392     source_image_t              source;
393     gradient_t                  gradient;
394     linear_gradient_t           linear;
395     conical_gradient_t          conical;
396     radial_gradient_t           radial;
397     solid_fill_t                solid;
398 };
399
400 /* Gradient walker
401  */
402 typedef struct
403 {
404     uint32_t        left_ag;
405     uint32_t        left_rb;
406     uint32_t        right_ag;
407     uint32_t        right_rb;
408     int32_t       left_x;
409     int32_t       right_x;
410     int32_t       stepper;
411
412     pixman_gradient_stop_t      *stops;
413     int                      num_stops;
414     unsigned int             spread;
415
416     int           need_reset;
417 } GradientWalker;
418
419 void
420 _pixman_gradient_walker_init (GradientWalker  *walker,
421                               gradient_t      *gradient,
422                               unsigned int     spread);
423
424 void
425 _pixman_gradient_walker_reset (GradientWalker       *walker,
426                                pixman_fixed_32_32_t  pos);
427
428 uint32_t
429 _pixman_gradient_walker_pixel (GradientWalker       *walker,
430                                pixman_fixed_32_32_t  x);
431
432
433
434 #define LOG2_BITMAP_PAD 5
435 #define FB_STIP_SHIFT   LOG2_BITMAP_PAD
436 #define FB_STIP_UNIT    (1 << FB_STIP_SHIFT)
437 #define FB_STIP_MASK    (FB_STIP_UNIT - 1)
438 #define FB_STIP_ALLONES ((uint32_t) -1)
439
440 #if BITMAP_BIT_ORDER == LSBFirst
441 #define FbScrLeft(x,n)  ((x) >> (n))
442 #define FbScrRight(x,n) ((x) << (n))
443 #define FbLeftStipBits(x,n) ((x) & ((((uint32_t) 1) << (n)) - 1))
444 #else
445 #define FbScrLeft(x,n)  ((x) << (n))
446 #define FbScrRight(x,n) ((x) >> (n))
447 #define FbLeftStipBits(x,n) ((x) >> (FB_STIP_UNIT - (n)))
448 #endif
449
450 #define FbStipLeft(x,n) FbScrLeft(x,n)
451 #define FbStipRight(x,n) FbScrRight(x,n)
452 #define FbStipMask(x,w) (FbStipRight(FB_STIP_ALLONES,(x) & FB_STIP_MASK) & \
453                          FbStipLeft(FB_STIP_ALLONES,(FB_STIP_UNIT - ((x)+(w))) & FB_STIP_MASK))
454
455 #define FbLeftMask(x)       ( ((x) & FB_MASK) ? \
456                               FbScrRight(FB_ALLONES,(x) & FB_MASK) : 0)
457 #define FbRightMask(x)      ( ((FB_UNIT - (x)) & FB_MASK) ? \
458                               FbScrLeft(FB_ALLONES,(FB_UNIT - (x)) & FB_MASK) : 0)
459
460 #define FbMaskBits(x,w,l,n,r) {                                         \
461         n = (w); \
462         r = FbRightMask((x)+n); \
463         l = FbLeftMask(x); \
464         if (l) { \
465             n -= FB_UNIT - ((x) & FB_MASK); \
466             if (n < 0) { \
467                 n = 0; \
468                 l &= r; \
469                 r = 0; \
470             } \
471         } \
472         n >>= FB_SHIFT; \
473     }
474
475 #if IMAGE_BYTE_ORDER == MSBFirst
476 #define Fetch24(img, a)  ((unsigned long) (a) & 1 ?           \
477     ((READ(img, a) << 16) | READ(img, (uint16_t *) ((a)+1))) : \
478     ((READ(img, (uint16_t *) (a)) << 8) | READ(img, (a)+2)))
479 #define Store24(img,a,v) ((unsigned long) (a) & 1 ? \
480     (WRITE(img, a, (uint8_t) ((v) >> 16)),                \
481      WRITE(img, (uint16_t *) ((a)+1), (uint16_t) (v))) :  \
482     (WRITE(img, (uint16_t *) (a), (uint16_t) ((v) >> 8)), \
483      WRITE(img, (a)+2, (uint8_t) (v))))
484 #else
485 #define Fetch24(img,a)  ((unsigned long) (a) & 1 ?                           \
486     (READ(img, a) | (READ(img, (uint16_t *) ((a)+1)) << 8)) : \
487     (READ(img, (uint16_t *) (a)) | (READ(img, (a)+2) << 16)))
488 #define Store24(img,a,v) ((unsigned long) (a) & 1 ? \
489     (WRITE(img, a, (uint8_t) (v)),                              \
490      WRITE(img, (uint16_t *) ((a)+1), (uint16_t) ((v) >> 8))) : \
491     (WRITE(img, (uint16_t *) (a), (uint16_t) (v)),              \
492      WRITE(img, (a)+2, (uint8_t) ((v) >> 16))))
493 #endif
494
495 #define CvtR8G8B8toY15(s)       (((((s) >> 16) & 0xff) * 153 + \
496                                   (((s) >>  8) & 0xff) * 301 +          \
497                                   (((s)      ) & 0xff) * 58) >> 2)
498 #define miCvtR8G8B8to15(s) ((((s) >> 3) & 0x001f) |  \
499                             (((s) >> 6) & 0x03e0) |  \
500                             (((s) >> 9) & 0x7c00))
501 #define miIndexToEnt15(mif,rgb15) ((mif)->ent[rgb15])
502 #define miIndexToEnt24(mif,rgb24) miIndexToEnt15(mif,miCvtR8G8B8to15(rgb24))
503
504 #define miIndexToEntY24(mif,rgb24) ((mif)->ent[CvtR8G8B8toY15(rgb24)])
505
506
507 #define FbIntMult(a,b,t) ( (t) = (a) * (b) + 0x80, ( ( ( (t)>>8 ) + (t) )>>8 ) )
508 #define FbIntDiv(a,b)    (((uint16_t) (a) * 255) / (b))
509
510 #define FbGet8(v,i)   ((uint16_t) (uint8_t) ((v) >> i))
511
512
513 #define cvt8888to0565(s)    ((((s) >> 3) & 0x001f) | \
514                              (((s) >> 5) & 0x07e0) | \
515                              (((s) >> 8) & 0xf800))
516 #define cvt0565to0888(s)    (((((s) << 3) & 0xf8) | (((s) >> 2) & 0x7)) | \
517                              ((((s) << 5) & 0xfc00) | (((s) >> 1) & 0x300)) | \
518                              ((((s) << 8) & 0xf80000) | (((s) << 3) & 0x70000)))
519
520 /*
521  * There are two ways of handling alpha -- either as a single unified value or
522  * a separate value for each component, hence each macro must have two
523  * versions.  The unified alpha version has a 'U' at the end of the name,
524  * the component version has a 'C'.  Similarly, functions which deal with
525  * this difference will have two versions using the same convention.
526  */
527
528 #define FbOverU(x,y,i,a,t) ((t) = FbIntMult(FbGet8(y,i),(a),(t)) + FbGet8(x,i), \
529                             (uint32_t) ((uint8_t) ((t) | (0 - ((t) >> 8)))) << (i))
530
531 #define FbOverC(x,y,i,a,t) ((t) = FbIntMult(FbGet8(y,i),FbGet8(a,i),(t)) + FbGet8(x,i), \
532                             (uint32_t) ((uint8_t) ((t) | (0 - ((t) >> 8)))) << (i))
533
534 #define FbInU(x,i,a,t) ((uint32_t) FbIntMult(FbGet8(x,i),(a),(t)) << (i))
535
536 #define FbInC(x,i,a,t) ((uint32_t) FbIntMult(FbGet8(x,i),FbGet8(a,i),(t)) << (i))
537
538 #define FbAdd(x,y,i,t)  ((t) = FbGet8(x,i) + FbGet8(y,i),               \
539                          (uint32_t) ((uint8_t) ((t) | (0 - ((t) >> 8)))) << (i))
540
541 #define div_255(x) (((x) + 0x80 + (((x) + 0x80) >> 8)) >> 8)
542 #define div_65535(x) (((x) + 0x8000 + (((x) + 0x8000) >> 16)) >> 16)
543
544 #define MOD(a,b) ((a) < 0 ? ((b) - ((-(a) - 1) % (b))) - 1 : (a) % (b))
545
546 /* Divides two fixed-point numbers and returns an integer */
547 #define DIV(a,b) ((((a) < 0) == ((b) < 0)) ? (a) / (b) :                \
548                   ((a) - (b) + 1 - (((b) < 0) << 1)) / (b))
549
550 #define CLIP(a,b,c) ((a) < (b) ? (b) : ((a) > (c) ? (c) : (a)))
551
552 #ifndef MIN
553 #  define MIN(a,b) ((a < b)? a : b)
554 #endif
555
556 #ifndef MAX
557 #  define MAX(a,b) ((a > b)? a : b)
558 #endif
559
560 #if 0
561 /* FIXME: the MOD macro above is equivalent, but faster I think */
562 #define mod(a,b) ((b) == 1 ? 0 : (a) >= 0 ? (a) % (b) : (b) - (-a) % (b))
563 #endif
564
565 /* FIXME: the (void)__read_func hides lots of warnings (which is what they
566  * are supposed to do), but some of them are real. For example the one
567  * where Fetch4 doesn't have a READ
568  */
569
570 #ifdef PIXMAN_FB_ACCESSORS
571
572 #define ACCESS(sym) sym##_accessors
573
574 #define READ(img, ptr)                                                  \
575     ((img)->common.read_func ((ptr), sizeof(*(ptr))))
576 #define WRITE(img, ptr,val)                                             \
577     ((img)->common.write_func ((ptr), (val), sizeof (*(ptr))))
578
579 #define MEMCPY_WRAPPED(img, dst, src, size)                             \
580     do {                                                                \
581         size_t _i;                                                      \
582         uint8_t *_dst = (uint8_t*)(dst), *_src = (uint8_t*)(src);       \
583         for(_i = 0; _i < size; _i++) {                                  \
584             WRITE((img), _dst +_i, READ((img), _src + _i));             \
585         }                                                               \
586     } while (0)
587
588 #define MEMSET_WRAPPED(img, dst, val, size)                             \
589     do {                                                                \
590         size_t _i;                                                      \
591         uint8_t *_dst = (uint8_t*)(dst);                                \
592         for(_i = 0; _i < (size_t) size; _i++) {                         \
593             WRITE((img), _dst +_i, (val));                              \
594         }                                                               \
595     } while (0)
596
597 #else
598
599 #define ACCESS(sym) sym
600
601 #define READ(img, ptr)          (*(ptr))
602 #define WRITE(img, ptr, val)    (*(ptr) = (val))
603 #define MEMCPY_WRAPPED(img, dst, src, size)                             \
604     memcpy(dst, src, size)
605 #define MEMSET_WRAPPED(img, dst, val, size)                             \
606     memset(dst, val, size)
607
608 #endif
609
610 #define fbComposeGetSolid(img, res, fmt)                                \
611     do                                                                  \
612     {                                                                   \
613         pixman_format_code_t format__;                                  \
614         if (img->type == SOLID)                                         \
615         {                                                               \
616             format__ = PIXMAN_a8r8g8b8;                                 \
617             (res) = img->solid.color;                                   \
618         }                                                               \
619         else                                                            \
620         {                                                               \
621             uint32_t           *bits__   = (img)->bits.bits;            \
622             format__ = (img)->bits.format;                              \
623                                                                         \
624             switch (PIXMAN_FORMAT_BPP((img)->bits.format))              \
625             {                                                           \
626             case 32:                                                    \
627                 (res) = READ(img, (uint32_t *)bits__);                  \
628                 break;                                                  \
629             case 24:                                                    \
630                 (res) = Fetch24(img, (uint8_t *) bits__);               \
631                 break;                                                  \
632             case 16:                                                    \
633                 (res) = READ(img, (uint16_t *) bits__);                 \
634                 (res) = cvt0565to0888(res);                             \
635                 break;                                                  \
636             case 8:                                                     \
637                 (res) = READ(img, (uint8_t *) bits__);                  \
638                 (res) = (res) << 24;                                    \
639                 break;                                                  \
640             case 1:                                                     \
641                 (res) = READ(img, (uint32_t *) bits__);                 \
642                 (res) = FbLeftStipBits((res),1) ? 0xff000000 : 0x00000000; \
643                 break;                                                  \
644             default:                                                    \
645                 return;                                                 \
646             }                                                           \
647             /* manage missing src alpha */                              \
648             if (!PIXMAN_FORMAT_A((img)->bits.format))                   \
649                 (res) |= 0xff000000;                                    \
650         }                                                               \
651                                                                         \
652         /* If necessary, convert RGB <--> BGR. */                       \
653         if (PIXMAN_FORMAT_TYPE (format__) != PIXMAN_FORMAT_TYPE(fmt))   \
654         {                                                               \
655             (res) = ((((res) & 0xff000000) >>  0) |                     \
656                      (((res) & 0x00ff0000) >> 16) |                     \
657                      (((res) & 0x0000ff00) >>  0) |                     \
658                      (((res) & 0x000000ff) << 16));                     \
659         }                                                               \
660     }                                                                   \
661     while (0)
662
663 #define fbComposeGetStart(pict,x,y,type,out_stride,line,mul) do {       \
664         uint32_t        *__bits__;                                      \
665         int             __stride__;                                     \
666         int             __bpp__;                                        \
667                                                                         \
668         __bits__ = pict->bits.bits;                                     \
669         __stride__ = pict->bits.rowstride;                              \
670         __bpp__ = PIXMAN_FORMAT_BPP(pict->bits.format);                 \
671         (out_stride) = __stride__ * (int) sizeof (uint32_t) / (int) sizeof (type);      \
672         (line) = ((type *) __bits__) +                                  \
673             (out_stride) * (y) + (mul) * (x);                           \
674     } while (0)
675
676
677 #define PIXMAN_FORMAT_16BPC(f)  (PIXMAN_FORMAT_A(f) > 8 || \
678                                  PIXMAN_FORMAT_R(f) > 8 || \
679                                  PIXMAN_FORMAT_G(f) > 8 || \
680                                  PIXMAN_FORMAT_B(f) > 8)
681 /*
682  * Edges
683  */
684
685 #define MAX_ALPHA(n)    ((1 << (n)) - 1)
686 #define N_Y_FRAC(n)     ((n) == 1 ? 1 : (1 << ((n)/2)) - 1)
687 #define N_X_FRAC(n)     ((n) == 1 ? 1 : (1 << ((n)/2)) + 1)
688
689 #define STEP_Y_SMALL(n) (pixman_fixed_1 / N_Y_FRAC(n))
690 #define STEP_Y_BIG(n)   (pixman_fixed_1 - (N_Y_FRAC(n) - 1) * STEP_Y_SMALL(n))
691
692 #define Y_FRAC_FIRST(n) (STEP_Y_SMALL(n) / 2)
693 #define Y_FRAC_LAST(n)  (Y_FRAC_FIRST(n) + (N_Y_FRAC(n) - 1) * STEP_Y_SMALL(n))
694
695 #define STEP_X_SMALL(n) (pixman_fixed_1 / N_X_FRAC(n))
696 #define STEP_X_BIG(n)   (pixman_fixed_1 - (N_X_FRAC(n) - 1) * STEP_X_SMALL(n))
697
698 #define X_FRAC_FIRST(n) (STEP_X_SMALL(n) / 2)
699 #define X_FRAC_LAST(n)  (X_FRAC_FIRST(n) + (N_X_FRAC(n) - 1) * STEP_X_SMALL(n))
700
701 #define RenderSamplesX(x,n)     ((n) == 1 ? 0 : (pixman_fixed_frac (x) + X_FRAC_FIRST(n)) / STEP_X_SMALL(n))
702
703 /*
704  * Step across a small sample grid gap
705  */
706 #define RenderEdgeStepSmall(edge) { \
707     edge->x += edge->stepx_small;   \
708     edge->e += edge->dx_small;      \
709     if (edge->e > 0)                \
710     {                               \
711         edge->e -= edge->dy;        \
712         edge->x += edge->signdx;    \
713     }                               \
714 }
715
716 /*
717  * Step across a large sample grid gap
718  */
719 #define RenderEdgeStepBig(edge) {   \
720     edge->x += edge->stepx_big;     \
721     edge->e += edge->dx_big;        \
722     if (edge->e > 0)                \
723     {                               \
724         edge->e -= edge->dy;        \
725         edge->x += edge->signdx;    \
726     }                               \
727 }
728
729 void
730 pixman_rasterize_edges_accessors (pixman_image_t *image,
731                                   pixman_edge_t *l,
732                                   pixman_edge_t *r,
733                                   pixman_fixed_t        t,
734                                   pixman_fixed_t        b);
735
736 pixman_bool_t
737 pixman_image_is_opaque(pixman_image_t *image);
738
739 pixman_bool_t
740 pixman_image_can_get_solid (pixman_image_t *image);
741
742 pixman_bool_t
743 pixman_compute_composite_region32 (pixman_region32_t *  pRegion,
744                                    pixman_image_t *     pSrc,
745                                    pixman_image_t *     pMask,
746                                    pixman_image_t *     pDst,
747                                    int16_t              xSrc,
748                                    int16_t              ySrc,
749                                    int16_t              xMask,
750                                    int16_t              yMask,
751                                    int16_t              xDst,
752                                    int16_t              yDst,
753                                    uint16_t             width,
754                                    uint16_t             height);
755
756 /* GCC visibility */
757 #if defined(__GNUC__) && __GNUC__ >= 4
758 #define PIXMAN_EXPORT __attribute__ ((visibility("default")))
759 /* Sun Studio 8 visibility */
760 #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
761 #define PIXMAN_EXPORT __global
762 #else
763 #define PIXMAN_EXPORT
764 #endif
765
766 /* Region Helpers */
767 pixman_bool_t pixman_region32_copy_from_region16 (pixman_region32_t *dst,
768                                                   pixman_region16_t *src);
769 pixman_bool_t pixman_region16_copy_from_region32 (pixman_region16_t *dst,
770                                                   pixman_region32_t *src);
771 void pixman_region_internal_set_static_pointers (pixman_box16_t *empty_box,
772                                                  pixman_region16_data_t *empty_data,
773                                                  pixman_region16_data_t *broken_data);
774
775 #ifdef PIXMAN_TIMERS
776
777 /* Timing */
778 static inline uint64_t
779 oil_profile_stamp_rdtsc (void)
780 {
781     uint64_t ts;
782     __asm__ __volatile__("rdtsc\n" : "=A" (ts));
783     return ts;
784 }
785 #define OIL_STAMP oil_profile_stamp_rdtsc
786
787 typedef struct PixmanTimer PixmanTimer;
788
789 struct PixmanTimer
790 {
791     int initialized;
792     const char *name;
793     uint64_t n_times;
794     uint64_t total;
795     PixmanTimer *next;
796 };
797
798 extern int timer_defined;
799 void pixman_timer_register (PixmanTimer *timer);
800
801 #define TIMER_BEGIN(tname)                                              \
802     {                                                                   \
803         static PixmanTimer      timer##tname;                           \
804         uint64_t                begin##tname;                           \
805                                                                         \
806         if (!timer##tname.initialized)                                  \
807         {                                                               \
808             timer##tname.initialized = 1;                               \
809             timer##tname.name = #tname;                                 \
810             pixman_timer_register (&timer##tname);                      \
811         }                                                               \
812                                                                         \
813         timer##tname.n_times++;                                         \
814         begin##tname = OIL_STAMP();
815
816 #define TIMER_END(tname)                                                \
817         timer##tname.total += OIL_STAMP() - begin##tname;               \
818     }
819
820 #endif /* PIXMAN_TIMERS */
821
822 typedef struct pixman_implementation_t pixman_implementation_t;
823
824 typedef void (* pixman_combine_32_func_t) (pixman_implementation_t *    imp,
825                                            pixman_op_t                  op,
826                                            uint32_t *                   dest,
827                                            const uint32_t *             src,
828                                            const uint32_t *             mask,
829                                            int                          width);
830
831 typedef void (* pixman_combine_64_func_t) (pixman_implementation_t *    imp,
832                                            pixman_op_t                  op,
833                                            uint64_t *                   dest,
834                                            const uint64_t *             src,
835                                            const uint64_t *             mask,
836                                            int                          width);
837
838 typedef void (* pixman_composite_func_t)  (pixman_implementation_t *    imp,
839                                            pixman_op_t                  op,
840                                            pixman_image_t *             src,
841                                            pixman_image_t *             mask,
842                                            pixman_image_t *             dest,
843                                            int32_t                      src_x,
844                                            int32_t                      src_y,
845                                            int32_t                      mask_x,
846                                            int32_t                      mask_y,
847                                            int32_t                      dest_x,
848                                            int32_t                      dest_y,
849                                            int32_t                      width,
850                                            int32_t                      height);
851 typedef pixman_bool_t (* pixman_blt_func_t) (pixman_implementation_t *  imp,
852                                              uint32_t *                 src_bits,
853                                              uint32_t *                 dst_bits,
854                                              int                        src_stride,
855                                              int                        dst_stride,
856                                              int                        src_bpp,
857                                              int                        dst_bpp,
858                                              int                        src_x,
859                                              int                        src_y,
860                                              int                        dst_x,
861                                              int                        dst_y,
862                                              int                        width,
863                                              int                        height);
864 typedef pixman_bool_t (* pixman_fill_func_t) (pixman_implementation_t *imp,
865                                               uint32_t *bits,
866                                               int stride,
867                                               int bpp,
868                                               int x,
869                                               int y,
870                                               int width,
871                                               int height,
872                                               uint32_t xor);
873
874 void
875 _pixman_walk_composite_region (pixman_implementation_t *imp,
876                                pixman_op_t op,
877                                pixman_image_t * pSrc,
878                                pixman_image_t * pMask,
879                                pixman_image_t * pDst,
880                                int16_t xSrc,
881                                int16_t ySrc,
882                                int16_t xMask,
883                                int16_t yMask,
884                                int16_t xDst,
885                                int16_t yDst,
886                                uint16_t width,
887                                uint16_t height,
888                                pixman_composite_func_t compositeRect);
889
890 void _pixman_setup_combiner_functions_32 (pixman_implementation_t *imp);
891 void _pixman_setup_combiner_functions_64 (pixman_implementation_t *imp);
892
893 /* These "formats" both have depth 0, so they
894  * will never clash with any real ones
895  */
896 #define PIXMAN_null             PIXMAN_FORMAT(0,0,0,0,0,0)
897 #define PIXMAN_solid            PIXMAN_FORMAT(0,1,0,0,0,0)
898
899 #define NEED_COMPONENT_ALPHA            (1 << 0)
900 #define NEED_PIXBUF                     (1 << 1)
901 #define NEED_SOLID_MASK                 (1 << 2)
902
903 typedef struct
904 {
905     pixman_op_t                 op;
906     pixman_format_code_t        src_format;
907     pixman_format_code_t        mask_format;
908     pixman_format_code_t        dest_format;
909     pixman_composite_func_t     func;
910     uint32_t                    flags;
911 } FastPathInfo;
912
913 struct pixman_implementation_t
914 {
915     pixman_implementation_t *   toplevel;
916     pixman_implementation_t *   delegate;
917
918     pixman_composite_func_t     composite;
919     pixman_blt_func_t           blt;
920     pixman_fill_func_t          fill;
921     
922     pixman_combine_32_func_t    combine_32[PIXMAN_OP_LAST];
923     pixman_combine_32_func_t    combine_32_ca[PIXMAN_OP_LAST];
924     pixman_combine_64_func_t    combine_64[PIXMAN_OP_LAST];
925     pixman_combine_64_func_t    combine_64_ca[PIXMAN_OP_LAST];
926 };
927
928 pixman_implementation_t *
929 _pixman_implementation_create (pixman_implementation_t *delegate);
930
931 void
932 _pixman_implementation_combine_32 (pixman_implementation_t *    imp,
933                                    pixman_op_t                  op,
934                                    uint32_t *                   dest,
935                                    const uint32_t *             src,
936                                    const uint32_t *             mask,
937                                    int                          width);
938 void
939 _pixman_implementation_combine_64 (pixman_implementation_t *    imp,
940                                    pixman_op_t                  op,
941                                    uint64_t *                   dest,
942                                    const uint64_t *             src,
943                                    const uint64_t *             mask,
944                                    int                          width);
945 void
946 _pixman_implementation_combine_32_ca (pixman_implementation_t * imp,
947                                       pixman_op_t               op,
948                                       uint32_t *                dest,
949                                       const uint32_t *          src,
950                                       const uint32_t *          mask,
951                                       int                       width);
952 void
953 _pixman_implementation_combine_64_ca (pixman_implementation_t * imp,
954                                       pixman_op_t               op,
955                                       uint64_t *                dest,
956                                       const uint64_t *          src,
957                                       const uint64_t *          mask,
958                                       int                       width);
959 void
960 _pixman_implementation_composite (pixman_implementation_t *     imp,
961                                   pixman_op_t                   op,
962                                   pixman_image_t *              src,
963                                   pixman_image_t *              mask,
964                                   pixman_image_t *              dest,
965                                   int32_t                       src_x,
966                                   int32_t                       src_y,
967                                   int32_t                       mask_x,
968                                   int32_t                       mask_y,
969                                   int32_t                       dest_x,
970                                   int32_t                       dest_y,
971                                   int32_t                       width,
972                                   int32_t                       height);
973
974 pixman_bool_t
975 _pixman_implementation_blt (pixman_implementation_t *   imp,
976                             uint32_t *                  src_bits,
977                             uint32_t *                  dst_bits,
978                             int                         src_stride,
979                             int                         dst_stride,
980                             int                         src_bpp,
981                             int                         dst_bpp,
982                             int                         src_x,
983                             int                         src_y,
984                             int                         dst_x,
985                             int                         dst_y,
986                             int                         width,
987                             int                         height);
988 pixman_bool_t
989 _pixman_implementation_fill (pixman_implementation_t *   imp,
990                              uint32_t *bits,
991                              int stride,
992                              int bpp,
993                              int x,
994                              int y,
995                              int width,
996                              int height,
997                              uint32_t xor);
998     
999 /* Specific implementations */
1000 pixman_implementation_t *
1001 _pixman_implementation_create_general (void);
1002 pixman_implementation_t *
1003 _pixman_implementation_create_fast_path (void);
1004 #ifdef USE_MMX
1005 pixman_implementation_t *
1006 _pixman_implementation_create_mmx (void);
1007 #endif
1008 #ifdef USE_SSE2
1009 pixman_implementation_t *
1010 _pixman_implementation_create_sse2 (void);
1011 #endif
1012 #ifdef USE_ARM_SIMD
1013 pixman_implementation_t *
1014 _pixman_implementation_create_arm_simd (void);
1015 #endif
1016 #ifdef USE_ARM_NEON
1017 pixman_implementation_t *
1018 _pixman_implementation_create_arm_neon (void);
1019 #endif
1020 #ifdef USE_VMX
1021 pixman_implementation_t *
1022 _pixman_implementation_create_vmx (void);
1023 #endif
1024
1025 pixman_bool_t
1026 _pixman_run_fast_path (const FastPathInfo *paths,
1027                        pixman_implementation_t *imp,
1028                        pixman_op_t op,
1029                        pixman_image_t *src,
1030                        pixman_image_t *mask,
1031                        pixman_image_t *dest,
1032                        int32_t src_x,
1033                        int32_t src_y,
1034                        int32_t mask_x,
1035                        int32_t mask_y,
1036                        int32_t dest_x,
1037                        int32_t dest_y,
1038                        int32_t width,
1039                        int32_t height);
1040     
1041 pixman_implementation_t *
1042 _pixman_choose_implementation (void);
1043
1044 #endif /* PIXMAN_PRIVATE_H */