Move the gradient walker code to its own file
[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
11 #ifndef FALSE
12 #define FALSE 0
13 #endif
14
15 #ifndef TRUE
16 #define TRUE 1
17 #endif
18
19 #define MSBFirst 0
20 #define LSBFirst 1
21
22 #ifdef WORDS_BIGENDIAN
23 #  define IMAGE_BYTE_ORDER MSBFirst
24 #  define BITMAP_BIT_ORDER MSBFirst
25 #else
26 #  define IMAGE_BYTE_ORDER LSBFirst
27 #  define BITMAP_BIT_ORDER LSBFirst
28 #endif
29
30 #undef DEBUG
31 #define DEBUG 0
32
33 #if defined (__GNUC__)
34 #  define FUNC     ((const char*) (__PRETTY_FUNCTION__))
35 #elif defined (__sun) || (defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
36 #  define FUNC     ((const char*) (__func__))
37 #else
38 #  define FUNC     ((const char*) ("???"))
39 #endif
40
41 #ifndef INT16_MIN
42 # define INT16_MIN              (-32767-1)
43 #endif
44
45 #ifndef INT16_MAX
46 # define INT16_MAX              (32767)
47 #endif
48
49 #ifndef INT32_MIN
50 # define INT32_MIN              (-2147483647-1)
51 #endif
52
53 #ifndef INT32_MAX
54 # define INT32_MAX              (2147483647)
55 #endif
56
57 #ifndef UINT32_MIN
58 # define UINT32_MIN             (0)
59 #endif
60
61 #ifndef UINT32_MAX
62 # define UINT32_MAX             (4294967295U)
63 #endif
64
65 #ifndef M_PI
66 # define M_PI                   3.14159265358979323846
67 #endif
68
69 #ifdef _MSC_VER
70 /* 'inline' is available only in C++ in MSVC */
71 #   define inline __inline
72 #   define force_inline __forceinline
73 #elif defined __GNUC__ || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
74 #   define inline __inline__
75 #   define force_inline __inline__ __attribute__ ((__always_inline__))
76 #else
77 # ifndef force_inline
78 #  define force_inline inline
79 # endif
80 #endif
81
82 #define FB_SHIFT    5
83 #define FB_UNIT     (1 << FB_SHIFT)
84 #define FB_HALFUNIT (1 << (FB_SHIFT-1))
85 #define FB_MASK     (FB_UNIT - 1)
86 #define FB_ALLONES  ((uint32_t) -1)
87
88 /* Memory allocation helpers */
89 void *pixman_malloc_ab (unsigned int n, unsigned int b);
90 void *pixman_malloc_abc (unsigned int a, unsigned int b, unsigned int c);
91 pixman_bool_t pixman_multiply_overflows_int (unsigned int a, unsigned int b);
92 pixman_bool_t pixman_addition_overflows_int (unsigned int a, unsigned int b);
93
94 #if DEBUG
95
96 #define return_if_fail(expr)                                            \
97     do                                                                  \
98     {                                                                   \
99         if (!(expr))                                                    \
100         {                                                               \
101             fprintf(stderr, "In %s: %s failed\n", FUNC, #expr);         \
102             return;                                                     \
103         }                                                               \
104     }                                                                   \
105     while (0)
106
107 #define return_val_if_fail(expr, retval)                                \
108     do                                                                  \
109     {                                                                   \
110         if (!(expr))                                                    \
111         {                                                               \
112             fprintf(stderr, "In %s: %s failed\n", FUNC, #expr);         \
113             return (retval);                                            \
114         }                                                               \
115     }                                                                   \
116     while (0)
117
118 #else
119
120 #define return_if_fail(expr)                                            \
121     do                                                                  \
122     {                                                                   \
123         if (!(expr))                                                    \
124             return;                                                     \
125     }                                                                   \
126     while (0)
127
128 #define return_val_if_fail(expr, retval)                                \
129     do                                                                  \
130     {                                                                   \
131         if (!(expr))                                                    \
132             return (retval);                                            \
133     }                                                                   \
134     while (0)
135
136 #endif
137
138 typedef struct image_common image_common_t;
139 typedef struct source_image source_image_t;
140 typedef struct solid_fill solid_fill_t;
141 typedef struct gradient gradient_t;
142 typedef struct linear_gradient linear_gradient_t;
143 typedef struct horizontal_gradient horizontal_gradient_t;
144 typedef struct vertical_gradient vertical_gradient_t;
145 typedef struct conical_gradient conical_gradient_t;
146 typedef struct radial_gradient radial_gradient_t;
147 typedef struct bits_image bits_image_t;
148 typedef struct circle circle_t;
149 typedef struct point point_t;
150
151 /* FIXME - the types and structures below should be give proper names
152  */
153
154 #define FASTCALL
155 typedef FASTCALL void (*CombineFunc32) (uint32_t *dest, const uint32_t *src, const uint32_t *mask, int width);
156 typedef FASTCALL void (*fetchProc32)(bits_image_t *pict, int x, int y, int width,
157                                      uint32_t *buffer);
158 typedef FASTCALL uint32_t (*fetchPixelProc32)(bits_image_t *pict, int offset, int line);
159 typedef FASTCALL void (*storeProc32)(pixman_image_t *, uint32_t *bits,
160                                      const uint32_t *values, int x, int width,
161                                      const pixman_indexed_t *);
162
163 typedef FASTCALL void (*CombineFunc64) (uint64_t *dest, const uint64_t *src, const uint64_t *mask, int width);
164 typedef FASTCALL void (*fetchProc64)(bits_image_t *pict, int x, int y, int width,
165                                      uint64_t *buffer);
166 typedef FASTCALL uint64_t (*fetchPixelProc64)(bits_image_t *pict, int offset, int line);
167 typedef FASTCALL void (*storeProc64)(pixman_image_t *, uint32_t *bits,
168                                      const uint64_t *values, int x, int width,
169                                      const pixman_indexed_t *);
170
171 typedef struct _FbComposeData {
172     uint8_t      op;
173     pixman_image_t      *src;
174     pixman_image_t      *mask;
175     pixman_image_t      *dest;
176     int16_t      xSrc;
177     int16_t      ySrc;
178     int16_t      xMask;
179     int16_t      yMask;
180     int16_t      xDest;
181     int16_t      yDest;
182     uint16_t     width;
183     uint16_t     height;
184 } FbComposeData;
185
186 typedef struct _FbComposeFunctions32 {
187     CombineFunc32 *combineU;
188     CombineFunc32 *combineC;
189 } FbComposeFunctions32;
190
191 typedef struct _FbComposeFunctions64 {
192     CombineFunc64 *combineU;
193     CombineFunc64 *combineC;
194 } FbComposeFunctions64;
195
196 extern FbComposeFunctions32 pixman_composeFunctions;
197 extern FbComposeFunctions64 pixman_composeFunctions64;
198
199 void pixman_composite_rect_general_accessors (const FbComposeData *data,
200                                               void *src_buffer,
201                                               void *mask_buffer,
202                                               void *dest_buffer,
203                                               const int wide);
204 void pixman_composite_rect_general (const FbComposeData *data);
205
206 fetchProc32 pixman_fetchProcForPicture32 (bits_image_t *);
207 fetchPixelProc32 pixman_fetchPixelProcForPicture32 (bits_image_t *);
208 storeProc32 pixman_storeProcForPicture32 (bits_image_t *);
209 fetchProc32 pixman_fetchProcForPicture32_accessors (bits_image_t *);
210 fetchPixelProc32 pixman_fetchPixelProcForPicture32_accessors (bits_image_t *);
211 storeProc32 pixman_storeProcForPicture32_accessors (bits_image_t *);
212
213 fetchProc64 pixman_fetchProcForPicture64 (bits_image_t *);
214 fetchPixelProc64 pixman_fetchPixelProcForPicture64 (bits_image_t *);
215 storeProc64 pixman_storeProcForPicture64 (bits_image_t *);
216 fetchProc64 pixman_fetchProcForPicture64_accessors (bits_image_t *);
217 fetchPixelProc64 pixman_fetchPixelProcForPicture64_accessors (bits_image_t *);
218 storeProc64 pixman_storeProcForPicture64_accessors (bits_image_t *);
219
220 void pixman_expand(uint64_t *dst, const uint32_t *src, pixman_format_code_t, int width);
221 void pixman_contract(uint32_t *dst, const uint64_t *src, int width);
222
223 void pixmanFetchSourcePict(source_image_t *, int x, int y, int width,
224                            uint32_t *buffer, uint32_t *mask, uint32_t maskBits);
225 void pixmanFetchSourcePict64(source_image_t *, int x, int y, int width,
226                              uint64_t *buffer, uint64_t *mask, uint32_t maskBits);
227
228 void fbFetchTransformed(bits_image_t *, int x, int y, int width,
229                         uint32_t *buffer, uint32_t *mask, uint32_t maskBits);
230 void fbStoreExternalAlpha(bits_image_t *, int x, int y, int width,
231                           uint32_t *buffer);
232 void fbFetchExternalAlpha(bits_image_t *, int x, int y, int width,
233                           uint32_t *buffer, uint32_t *mask, uint32_t maskBits);
234
235 void fbFetchTransformed_accessors(bits_image_t *, int x, int y, int width,
236                                   uint32_t *buffer, uint32_t *mask,
237                                   uint32_t maskBits);
238 void fbStoreExternalAlpha_accessors(bits_image_t *, int x, int y, int width,
239                                     uint32_t *buffer);
240 void fbFetchExternalAlpha_accessors(bits_image_t *, int x, int y, int width,
241                                     uint32_t *buffer, uint32_t *mask,
242                                     uint32_t maskBits);
243
244 void fbFetchTransformed64(bits_image_t *, int x, int y, int width,
245                           uint64_t *buffer, uint64_t *mask, uint32_t maskBits);
246 void fbStoreExternalAlpha64(bits_image_t *, int x, int y, int width,
247                             uint64_t *buffer);
248 void fbFetchExternalAlpha64(bits_image_t *, int x, int y, int width,
249                             uint64_t *buffer, uint64_t *mask, uint32_t maskBits);
250
251 void fbFetchTransformed64_accessors(bits_image_t *, int x, int y, int width,
252                                     uint64_t *buffer, uint64_t *mask,
253                                     uint32_t maskBits);
254 void fbStoreExternalAlpha64_accessors(bits_image_t *, int x, int y, int width,
255                                       uint64_t *buffer);
256 void fbFetchExternalAlpha64_accessors(bits_image_t *, int x, int y, int width,
257                                       uint64_t *buffer, uint64_t *mask,
258                                       uint32_t maskBits);
259
260 /* end */
261
262 typedef enum
263 {
264     BITS,
265     LINEAR,
266     CONICAL,
267     RADIAL,
268     SOLID
269 } image_type_t;
270
271 #define IS_SOURCE_IMAGE(img)     (((image_common_t *)img)->type > BITS)
272
273 typedef enum
274 {
275     SOURCE_IMAGE_CLASS_UNKNOWN,
276     SOURCE_IMAGE_CLASS_HORIZONTAL,
277     SOURCE_IMAGE_CLASS_VERTICAL,
278 } source_pict_class_t;
279
280 typedef void (*scanStoreProc)(pixman_image_t *, int, int, int, uint32_t *);
281 typedef void (*scanFetchProc)(pixman_image_t *, int, int, int, uint32_t *,
282                               uint32_t *, uint32_t);
283
284 source_pict_class_t _pixman_image_classify (pixman_image_t *image,
285                                             int             x,
286                                             int             y,
287                                             int             width,
288                                             int             height);
289
290 void
291 _pixman_image_get_scanline_32 (pixman_image_t *image, int x, int y, int width,
292                                uint32_t *buffer, uint32_t *mask, uint32_t mask_bits);
293
294 /* Even thought the type of buffer is uint32_t *, the function actually expects
295  * a uint64_t *buffer.
296  */
297 void
298 _pixman_image_get_scanline_64 (pixman_image_t *image, int x, int y, int width,
299                                uint32_t *buffer, uint32_t *unused, uint32_t unused2);
300
301 scanStoreProc
302 _pixman_image_get_storer (pixman_image_t *image,
303                           int             wide);
304
305 pixman_image_t *
306 _pixman_image_allocate (void);
307
308 pixman_bool_t
309 _pixman_init_gradient (gradient_t     *gradient,
310                        const pixman_gradient_stop_t *stops,
311                        int             n_stops);
312 void
313 _pixman_image_reset_clip_region (pixman_image_t *image);
314
315 struct point
316 {
317     int16_t x, y;
318 };
319
320 typedef source_pict_class_t (* classify_func_t) (pixman_image_t *image,
321                                                  int             x,
322                                                  int             y,
323                                                  int             width,
324                                                  int             height);
325 typedef void (* property_changed_func_t)        (pixman_image_t *image);
326
327 struct image_common
328 {
329     image_type_t                type;
330     int32_t                     ref_count;
331     pixman_region32_t           full_region;
332     pixman_region32_t           clip_region;
333     pixman_region32_t          *src_clip;
334     pixman_bool_t               has_client_clip;
335     pixman_transform_t         *transform;
336     pixman_repeat_t             repeat;
337     pixman_filter_t             filter;
338     pixman_fixed_t             *filter_params;
339     int                         n_filter_params;
340     bits_image_t               *alpha_map;
341     point_t                     alpha_origin;
342     pixman_bool_t               component_alpha;
343     pixman_read_memory_func_t   read_func;
344     pixman_write_memory_func_t  write_func;
345     classify_func_t             classify;
346     property_changed_func_t     property_changed;
347     scanFetchProc               get_scanline_32;
348     scanFetchProc               get_scanline_64;
349 };
350
351 struct source_image
352 {
353     image_common_t      common;
354     source_pict_class_t class;
355 };
356
357 struct solid_fill
358 {
359     source_image_t      common;
360     uint32_t            color;          /* FIXME: shouldn't this be a pixman_color_t? */
361 };
362
363 struct gradient
364 {
365     source_image_t              common;
366     int                         n_stops;
367     pixman_gradient_stop_t *    stops;
368     int                         stop_range;
369     uint32_t *                  color_table;
370     int                         color_table_size;
371 };
372
373 struct linear_gradient
374 {
375     gradient_t                  common;
376     pixman_point_fixed_t        p1;
377     pixman_point_fixed_t        p2;
378 };
379
380 struct circle
381 {
382     pixman_fixed_t x;
383     pixman_fixed_t y;
384     pixman_fixed_t radius;
385 };
386
387 struct radial_gradient
388 {
389     gradient_t  common;
390
391     circle_t    c1;
392     circle_t    c2;
393     double      cdx;
394     double      cdy;
395     double      dr;
396     double      A;
397 };
398
399 struct conical_gradient
400 {
401     gradient_t                  common;
402     pixman_point_fixed_t        center;
403     pixman_fixed_t              angle;
404 };
405
406 struct bits_image
407 {
408     image_common_t              common;
409     pixman_format_code_t        format;
410     const pixman_indexed_t     *indexed;
411     int                         width;
412     int                         height;
413     uint32_t *                  bits;
414     uint32_t *                  free_me;
415     int                         rowstride; /* in number of uint32_t's */
416 };
417
418 union pixman_image
419 {
420     image_type_t                type;
421     image_common_t              common;
422     bits_image_t                bits;
423     source_image_t              source;
424     gradient_t                  gradient;
425     linear_gradient_t           linear;
426     conical_gradient_t          conical;
427     radial_gradient_t           radial;
428     solid_fill_t                solid;
429 };
430
431 /* Gradient walker
432  */
433 typedef struct
434 {
435     uint32_t        left_ag;
436     uint32_t        left_rb;
437     uint32_t        right_ag;
438     uint32_t        right_rb;
439     int32_t       left_x;
440     int32_t       right_x;
441     int32_t       stepper;
442
443     pixman_gradient_stop_t      *stops;
444     int                      num_stops;
445     unsigned int             spread;
446
447     int           need_reset;
448 } GradientWalker;
449
450 void
451 _pixman_gradient_walker_init (GradientWalker  *walker,
452                               gradient_t      *gradient,
453                               unsigned int     spread);
454
455 void
456 _pixman_gradient_walker_reset (GradientWalker       *walker,
457                                pixman_fixed_32_32_t  pos);
458
459 uint32_t
460 _pixman_gradient_walker_pixel (GradientWalker       *walker,
461                                pixman_fixed_32_32_t  x);
462
463
464
465 #define LOG2_BITMAP_PAD 5
466 #define FB_STIP_SHIFT   LOG2_BITMAP_PAD
467 #define FB_STIP_UNIT    (1 << FB_STIP_SHIFT)
468 #define FB_STIP_MASK    (FB_STIP_UNIT - 1)
469 #define FB_STIP_ALLONES ((uint32_t) -1)
470
471 #if BITMAP_BIT_ORDER == LSBFirst
472 #define FbScrLeft(x,n)  ((x) >> (n))
473 #define FbScrRight(x,n) ((x) << (n))
474 #define FbLeftStipBits(x,n) ((x) & ((((uint32_t) 1) << (n)) - 1))
475 #else
476 #define FbScrLeft(x,n)  ((x) << (n))
477 #define FbScrRight(x,n) ((x) >> (n))
478 #define FbLeftStipBits(x,n) ((x) >> (FB_STIP_UNIT - (n)))
479 #endif
480
481 #define FbStipLeft(x,n) FbScrLeft(x,n)
482 #define FbStipRight(x,n) FbScrRight(x,n)
483 #define FbStipMask(x,w) (FbStipRight(FB_STIP_ALLONES,(x) & FB_STIP_MASK) & \
484                          FbStipLeft(FB_STIP_ALLONES,(FB_STIP_UNIT - ((x)+(w))) & FB_STIP_MASK))
485
486 #define FbLeftMask(x)       ( ((x) & FB_MASK) ? \
487                               FbScrRight(FB_ALLONES,(x) & FB_MASK) : 0)
488 #define FbRightMask(x)      ( ((FB_UNIT - (x)) & FB_MASK) ? \
489                               FbScrLeft(FB_ALLONES,(FB_UNIT - (x)) & FB_MASK) : 0)
490
491 #define FbMaskBits(x,w,l,n,r) {                                         \
492         n = (w); \
493         r = FbRightMask((x)+n); \
494         l = FbLeftMask(x); \
495         if (l) { \
496             n -= FB_UNIT - ((x) & FB_MASK); \
497             if (n < 0) { \
498                 n = 0; \
499                 l &= r; \
500                 r = 0; \
501             } \
502         } \
503         n >>= FB_SHIFT; \
504     }
505
506 #if IMAGE_BYTE_ORDER == MSBFirst
507 #define Fetch24(img, a)  ((unsigned long) (a) & 1 ?           \
508     ((READ(img, a) << 16) | READ(img, (uint16_t *) ((a)+1))) : \
509     ((READ(img, (uint16_t *) (a)) << 8) | READ(img, (a)+2)))
510 #define Store24(img,a,v) ((unsigned long) (a) & 1 ? \
511     (WRITE(img, a, (uint8_t) ((v) >> 16)),                \
512      WRITE(img, (uint16_t *) ((a)+1), (uint16_t) (v))) :  \
513     (WRITE(img, (uint16_t *) (a), (uint16_t) ((v) >> 8)), \
514      WRITE(img, (a)+2, (uint8_t) (v))))
515 #else
516 #define Fetch24(img,a)  ((unsigned long) (a) & 1 ?                           \
517     (READ(img, a) | (READ(img, (uint16_t *) ((a)+1)) << 8)) : \
518     (READ(img, (uint16_t *) (a)) | (READ(img, (a)+2) << 16)))
519 #define Store24(img,a,v) ((unsigned long) (a) & 1 ? \
520     (WRITE(img, a, (uint8_t) (v)),                              \
521      WRITE(img, (uint16_t *) ((a)+1), (uint16_t) ((v) >> 8))) : \
522     (WRITE(img, (uint16_t *) (a), (uint16_t) (v)),              \
523      WRITE(img, (a)+2, (uint8_t) ((v) >> 16))))
524 #endif
525
526 #define CvtR8G8B8toY15(s)       (((((s) >> 16) & 0xff) * 153 + \
527                                   (((s) >>  8) & 0xff) * 301 +          \
528                                   (((s)      ) & 0xff) * 58) >> 2)
529 #define miCvtR8G8B8to15(s) ((((s) >> 3) & 0x001f) |  \
530                             (((s) >> 6) & 0x03e0) |  \
531                             (((s) >> 9) & 0x7c00))
532 #define miIndexToEnt15(mif,rgb15) ((mif)->ent[rgb15])
533 #define miIndexToEnt24(mif,rgb24) miIndexToEnt15(mif,miCvtR8G8B8to15(rgb24))
534
535 #define miIndexToEntY24(mif,rgb24) ((mif)->ent[CvtR8G8B8toY15(rgb24)])
536
537
538 #define FbIntMult(a,b,t) ( (t) = (a) * (b) + 0x80, ( ( ( (t)>>8 ) + (t) )>>8 ) )
539 #define FbIntDiv(a,b)    (((uint16_t) (a) * 255) / (b))
540
541 #define FbGet8(v,i)   ((uint16_t) (uint8_t) ((v) >> i))
542
543
544 #define cvt8888to0565(s)    ((((s) >> 3) & 0x001f) | \
545                              (((s) >> 5) & 0x07e0) | \
546                              (((s) >> 8) & 0xf800))
547 #define cvt0565to0888(s)    (((((s) << 3) & 0xf8) | (((s) >> 2) & 0x7)) | \
548                              ((((s) << 5) & 0xfc00) | (((s) >> 1) & 0x300)) | \
549                              ((((s) << 8) & 0xf80000) | (((s) << 3) & 0x70000)))
550
551 /*
552  * There are two ways of handling alpha -- either as a single unified value or
553  * a separate value for each component, hence each macro must have two
554  * versions.  The unified alpha version has a 'U' at the end of the name,
555  * the component version has a 'C'.  Similarly, functions which deal with
556  * this difference will have two versions using the same convention.
557  */
558
559 #define FbOverU(x,y,i,a,t) ((t) = FbIntMult(FbGet8(y,i),(a),(t)) + FbGet8(x,i), \
560                             (uint32_t) ((uint8_t) ((t) | (0 - ((t) >> 8)))) << (i))
561
562 #define FbOverC(x,y,i,a,t) ((t) = FbIntMult(FbGet8(y,i),FbGet8(a,i),(t)) + FbGet8(x,i), \
563                             (uint32_t) ((uint8_t) ((t) | (0 - ((t) >> 8)))) << (i))
564
565 #define FbInU(x,i,a,t) ((uint32_t) FbIntMult(FbGet8(x,i),(a),(t)) << (i))
566
567 #define FbInC(x,i,a,t) ((uint32_t) FbIntMult(FbGet8(x,i),FbGet8(a,i),(t)) << (i))
568
569 #define FbAdd(x,y,i,t)  ((t) = FbGet8(x,i) + FbGet8(y,i),               \
570                          (uint32_t) ((uint8_t) ((t) | (0 - ((t) >> 8)))) << (i))
571
572 #define div_255(x) (((x) + 0x80 + (((x) + 0x80) >> 8)) >> 8)
573 #define div_65535(x) (((x) + 0x8000 + (((x) + 0x8000) >> 16)) >> 16)
574
575 #define MOD(a,b) ((a) < 0 ? ((b) - ((-(a) - 1) % (b))) - 1 : (a) % (b))
576
577 #define DIV(a,b) ((((a) < 0) == ((b) < 0)) ? (a) / (b) :                \
578                   ((a) - (b) + 1 - (((b) < 0) << 1)) / (b))
579
580 #define CLIP(a,b,c) ((a) < (b) ? (b) : ((a) > (c) ? (c) : (a)))
581
582 #if 0
583 /* FIXME: the MOD macro above is equivalent, but faster I think */
584 #define mod(a,b) ((b) == 1 ? 0 : (a) >= 0 ? (a) % (b) : (b) - (-a) % (b))
585 #endif
586
587 /* FIXME: the (void)__read_func hides lots of warnings (which is what they
588  * are supposed to do), but some of them are real. For example the one
589  * where Fetch4 doesn't have a READ
590  */
591
592 #if 0
593 /* Framebuffer access support macros */
594 #define ACCESS_MEM(code)                                                \
595     do {                                                                \
596         const image_common_t *const com__ =                             \
597             (image_common_t *)image;                                    \
598                                                                         \
599         if (!com__->read_func && !com__->write_func)                    \
600         {                                                               \
601             const int do_access__ = 0;                                  \
602             const pixman_read_memory_func_t read_func__ = NULL;         \
603             const pixman_write_memory_func_t write_func__ = NULL;       \
604             (void)read_func__;                                          \
605             (void)write_func__;                                         \
606             (void)do_access__;                                          \
607                                                                         \
608             {code}                                                      \
609         }                                                               \
610         else                                                            \
611         {                                                               \
612             const int do_access__ = 1;                                  \
613             const pixman_read_memory_func_t read_func__ =               \
614                 com__->read_func;                                       \
615             const pixman_write_memory_func_t write_func__ =             \
616                 com__->write_func;                                      \
617             (void)read_func__;                                          \
618             (void)write_func__;                                         \
619             (void)do_access__;                                          \
620                                                                         \
621             {code}                                                      \
622         }                                                               \
623     } while (0)
624 #endif
625
626 #ifdef PIXMAN_FB_ACCESSORS
627
628 #define ACCESS(sym) sym##_accessors
629
630 #define READ(img, ptr)                                                  \
631     ((img)->common.read_func ((ptr), sizeof(*(ptr))))
632 #define WRITE(img, ptr,val)                                             \
633     ((img)->common.write_func ((ptr), (val), sizeof (*(ptr))))
634
635 #define MEMCPY_WRAPPED(img, dst, src, size)                             \
636     do {                                                                \
637         size_t _i;                                                      \
638         uint8_t *_dst = (uint8_t*)(dst), *_src = (uint8_t*)(src);       \
639         for(_i = 0; _i < size; _i++) {                                  \
640             WRITE((img), _dst +_i, READ((img), _src + _i));             \
641         }                                                               \
642     } while (0)
643
644 #define MEMSET_WRAPPED(img, dst, val, size)                             \
645     do {                                                                \
646         size_t _i;                                                      \
647         uint8_t *_dst = (uint8_t*)(dst);                                \
648         for(_i = 0; _i < (size_t) size; _i++) {                         \
649             WRITE((img), _dst +_i, (val));                              \
650         }                                                               \
651     } while (0)
652
653 #else
654
655 #define ACCESS(sym) sym
656
657 #define READ(img, ptr)          (*(ptr))
658 #define WRITE(img, ptr, val)    (*(ptr) = (val))
659 #define MEMCPY_WRAPPED(img, dst, src, size)                                     \
660     memcpy(dst, src, size)
661 #define MEMSET_WRAPPED(img, dst, val, size)                                     \
662     memset(dst, val, size)
663
664 #endif
665
666 #define fbComposeGetSolid(img, res, fmt)                                \
667     do                                                                  \
668     {                                                                   \
669         pixman_format_code_t format__;                                  \
670         if (img->type == SOLID)                                         \
671         {                                                               \
672             format__ = PIXMAN_a8r8g8b8;                                 \
673             (res) = img->solid.color;                                   \
674         }                                                               \
675         else                                                            \
676         {                                                               \
677             uint32_t           *bits__   = (img)->bits.bits;            \
678             format__ = (img)->bits.format;                              \
679                                                                         \
680             switch (PIXMAN_FORMAT_BPP((img)->bits.format))              \
681             {                                                           \
682             case 32:                                                    \
683                 (res) = READ(img, (uint32_t *)bits__);                  \
684                 break;                                                  \
685             case 24:                                                    \
686                 (res) = Fetch24(img, (uint8_t *) bits__);                       \
687                 break;                                                  \
688             case 16:                                                    \
689                 (res) = READ(img, (uint16_t *) bits__);                 \
690                 (res) = cvt0565to0888(res);                             \
691                 break;                                                  \
692             case 8:                                                     \
693                 (res) = READ(img, (uint8_t *) bits__);                  \
694                 (res) = (res) << 24;                                    \
695                 break;                                                  \
696             case 1:                                                     \
697                 (res) = READ(img, (uint32_t *) bits__);                 \
698                 (res) = FbLeftStipBits((res),1) ? 0xff000000 : 0x00000000; \
699                 break;                                                  \
700             default:                                                    \
701                 return;                                                 \
702             }                                                           \
703             /* manage missing src alpha */                              \
704             if (!PIXMAN_FORMAT_A((img)->bits.format))                   \
705                 (res) |= 0xff000000;                                    \
706         }                                                               \
707                                                                         \
708         /* If necessary, convert RGB <--> BGR. */                       \
709         if (PIXMAN_FORMAT_TYPE (format__) != PIXMAN_FORMAT_TYPE(fmt))   \
710         {                                                               \
711             (res) = ((((res) & 0xff000000) >>  0) |                     \
712                      (((res) & 0x00ff0000) >> 16) |                     \
713                      (((res) & 0x0000ff00) >>  0) |                     \
714                      (((res) & 0x000000ff) << 16));                     \
715         }                                                               \
716     }                                                                   \
717     while (0)
718
719 #define fbComposeGetStart(pict,x,y,type,out_stride,line,mul) do {       \
720         uint32_t        *__bits__;                                      \
721         int             __stride__;                                     \
722         int             __bpp__;                                        \
723                                                                         \
724         __bits__ = pict->bits.bits;                                     \
725         __stride__ = pict->bits.rowstride;                              \
726         __bpp__ = PIXMAN_FORMAT_BPP(pict->bits.format);                 \
727         (out_stride) = __stride__ * (int) sizeof (uint32_t) / (int) sizeof (type);      \
728         (line) = ((type *) __bits__) +                                  \
729             (out_stride) * (y) + (mul) * (x);                           \
730     } while (0)
731
732
733 #define PIXMAN_FORMAT_16BPC(f)  (PIXMAN_FORMAT_A(f) > 8 || \
734                                  PIXMAN_FORMAT_R(f) > 8 || \
735                                  PIXMAN_FORMAT_G(f) > 8 || \
736                                  PIXMAN_FORMAT_B(f) > 8)
737 /*
738  * Edges
739  */
740
741 #define MAX_ALPHA(n)    ((1 << (n)) - 1)
742 #define N_Y_FRAC(n)     ((n) == 1 ? 1 : (1 << ((n)/2)) - 1)
743 #define N_X_FRAC(n)     ((n) == 1 ? 1 : (1 << ((n)/2)) + 1)
744
745 #define STEP_Y_SMALL(n) (pixman_fixed_1 / N_Y_FRAC(n))
746 #define STEP_Y_BIG(n)   (pixman_fixed_1 - (N_Y_FRAC(n) - 1) * STEP_Y_SMALL(n))
747
748 #define Y_FRAC_FIRST(n) (STEP_Y_SMALL(n) / 2)
749 #define Y_FRAC_LAST(n)  (Y_FRAC_FIRST(n) + (N_Y_FRAC(n) - 1) * STEP_Y_SMALL(n))
750
751 #define STEP_X_SMALL(n) (pixman_fixed_1 / N_X_FRAC(n))
752 #define STEP_X_BIG(n)   (pixman_fixed_1 - (N_X_FRAC(n) - 1) * STEP_X_SMALL(n))
753
754 #define X_FRAC_FIRST(n) (STEP_X_SMALL(n) / 2)
755 #define X_FRAC_LAST(n)  (X_FRAC_FIRST(n) + (N_X_FRAC(n) - 1) * STEP_X_SMALL(n))
756
757 #define RenderSamplesX(x,n)     ((n) == 1 ? 0 : (pixman_fixed_frac (x) + X_FRAC_FIRST(n)) / STEP_X_SMALL(n))
758
759 /*
760  * Step across a small sample grid gap
761  */
762 #define RenderEdgeStepSmall(edge) { \
763     edge->x += edge->stepx_small;   \
764     edge->e += edge->dx_small;      \
765     if (edge->e > 0)                \
766     {                               \
767         edge->e -= edge->dy;        \
768         edge->x += edge->signdx;    \
769     }                               \
770 }
771
772 /*
773  * Step across a large sample grid gap
774  */
775 #define RenderEdgeStepBig(edge) {   \
776     edge->x += edge->stepx_big;     \
777     edge->e += edge->dx_big;        \
778     if (edge->e > 0)                \
779     {                               \
780         edge->e -= edge->dy;        \
781         edge->x += edge->signdx;    \
782     }                               \
783 }
784
785 void
786 pixman_rasterize_edges_accessors (pixman_image_t *image,
787                                   pixman_edge_t *l,
788                                   pixman_edge_t *r,
789                                   pixman_fixed_t        t,
790                                   pixman_fixed_t        b);
791
792 pixman_bool_t
793 pixman_image_is_opaque(pixman_image_t *image);
794
795 pixman_bool_t
796 pixman_image_can_get_solid (pixman_image_t *image);
797
798 pixman_bool_t
799 pixman_compute_composite_region32 (pixman_region32_t *  pRegion,
800                                    pixman_image_t *     pSrc,
801                                    pixman_image_t *     pMask,
802                                    pixman_image_t *     pDst,
803                                    int16_t              xSrc,
804                                    int16_t              ySrc,
805                                    int16_t              xMask,
806                                    int16_t              yMask,
807                                    int16_t              xDst,
808                                    int16_t              yDst,
809                                    uint16_t             width,
810                                    uint16_t             height);
811
812 /* GCC visibility */
813 #if defined(__GNUC__) && __GNUC__ >= 4
814 #define PIXMAN_EXPORT __attribute__ ((visibility("default")))
815 /* Sun Studio 8 visibility */
816 #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
817 #define PIXMAN_EXPORT __global
818 #else
819 #define PIXMAN_EXPORT
820 #endif
821
822 /* Region Helpers */
823 pixman_bool_t pixman_region32_copy_from_region16 (pixman_region32_t *dst,
824                                                   pixman_region16_t *src);
825 pixman_bool_t pixman_region16_copy_from_region32 (pixman_region16_t *dst,
826                                                   pixman_region32_t *src);
827 void pixman_region_internal_set_static_pointers (pixman_box16_t *empty_box,
828                                                  pixman_region16_data_t *empty_data,
829                                                  pixman_region16_data_t *broken_data);
830
831 #ifdef PIXMAN_TIMING
832
833 /* Timing */
834 static inline uint64_t
835 oil_profile_stamp_rdtsc (void)
836 {
837     uint64_t ts;
838     __asm__ __volatile__("rdtsc\n" : "=A" (ts));
839     return ts;
840 }
841 #define OIL_STAMP oil_profile_stamp_rdtsc
842
843 typedef struct PixmanTimer PixmanTimer;
844
845 struct PixmanTimer
846 {
847     int initialized;
848     const char *name;
849     uint64_t n_times;
850     uint64_t total;
851     PixmanTimer *next;
852 };
853
854 extern int timer_defined;
855 void pixman_timer_register (PixmanTimer *timer);
856
857 #define TIMER_BEGIN(tname)                                              \
858     {                                                                   \
859         static PixmanTimer      timer##tname;                           \
860         uint64_t                begin##tname;                           \
861                                                                         \
862         if (!timer##tname.initialized)                                  \
863         {                                                               \
864             timer##tname.initialized = 1;                               \
865             timer##tname.name = #tname;                                 \
866             pixman_timer_register (&timer##tname);                      \
867         }                                                               \
868                                                                         \
869         timer##tname.n_times++;                                         \
870         begin##tname = OIL_STAMP();
871
872 #define TIMER_END(tname)                                                \
873         timer##tname.total += OIL_STAMP() - begin##tname;               \
874     }
875
876 #endif /* PIXMAN_TIMING */
877
878 #endif /* PIXMAN_PRIVATE_H */