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