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