Eliminate mask_bits from all the scanline fetchers.
[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 #define PIXMAN_DISABLE_DEPRECATED
9 #define PIXMAN_USE_INTERNAL_API
10
11 #include "pixman.h"
12 #include <time.h>
13 #include <assert.h>
14 #include <stdio.h>
15 #include <string.h>
16
17 #include "pixman-compiler.h"
18
19 /*
20  * Images
21  */
22 typedef struct image_common image_common_t;
23 typedef struct source_image source_image_t;
24 typedef struct solid_fill solid_fill_t;
25 typedef struct gradient gradient_t;
26 typedef struct linear_gradient linear_gradient_t;
27 typedef struct horizontal_gradient horizontal_gradient_t;
28 typedef struct vertical_gradient vertical_gradient_t;
29 typedef struct conical_gradient conical_gradient_t;
30 typedef struct radial_gradient radial_gradient_t;
31 typedef struct bits_image bits_image_t;
32 typedef struct circle circle_t;
33
34 typedef void (*fetch_scanline_t) (pixman_image_t *image,
35                                   int             x,
36                                   int             y,
37                                   int             width,
38                                   uint32_t       *buffer,
39                                   const uint32_t *mask);
40
41 typedef uint32_t (*fetch_pixel_32_t) (bits_image_t *image,
42                                       int           x,
43                                       int           y);
44
45 typedef uint64_t (*fetch_pixel_64_t) (bits_image_t *image,
46                                       int           x,
47                                       int           y);
48
49 typedef void (*store_scanline_t) (bits_image_t *  image,
50                                   int             x,
51                                   int             y,
52                                   int             width,
53                                   const uint32_t *values);
54
55 typedef enum
56 {
57     BITS,
58     LINEAR,
59     CONICAL,
60     RADIAL,
61     SOLID
62 } image_type_t;
63
64 typedef enum
65 {
66     SOURCE_IMAGE_CLASS_UNKNOWN,
67     SOURCE_IMAGE_CLASS_HORIZONTAL,
68     SOURCE_IMAGE_CLASS_VERTICAL,
69 } source_image_class_t;
70
71 typedef source_image_class_t (*classify_func_t) (pixman_image_t *image,
72                                                 int             x,
73                                                 int             y,
74                                                 int             width,
75                                                 int             height);
76 typedef void (*property_changed_func_t) (pixman_image_t *image);
77
78 struct image_common
79 {
80     image_type_t                type;
81     int32_t                     ref_count;
82     pixman_region32_t           clip_region;
83     pixman_bool_t               have_clip_region;   /* FALSE if there is no clip */
84     pixman_bool_t               client_clip;        /* Whether the source clip was
85                                                        set by a client */
86     pixman_bool_t               clip_sources;       /* Whether the clip applies when
87                                                      * the image is used as a source
88                                                      */
89     pixman_bool_t               dirty;
90     pixman_transform_t *        transform;
91     pixman_repeat_t             repeat;
92     pixman_filter_t             filter;
93     pixman_fixed_t *            filter_params;
94     int                         n_filter_params;
95     bits_image_t *              alpha_map;
96     int                         alpha_origin_x;
97     int                         alpha_origin_y;
98     pixman_bool_t               component_alpha;
99     classify_func_t             classify;
100     property_changed_func_t     property_changed;
101     fetch_scanline_t            get_scanline_32;
102     fetch_scanline_t            get_scanline_64;
103
104     pixman_image_destroy_func_t destroy_func;
105     void *                      destroy_data;
106
107     uint32_t                    flags;
108     pixman_format_code_t        extended_format_code;
109 };
110
111 struct source_image
112 {
113     image_common_t common;
114     source_image_class_t class;
115 };
116
117 struct solid_fill
118 {
119     source_image_t common;
120     pixman_color_t color;
121     
122     uint32_t       color_32;
123     uint64_t       color_64;
124 };
125
126 struct gradient
127 {
128     source_image_t          common;
129     int                     n_stops;
130     pixman_gradient_stop_t *stops;
131     int                     stop_range;
132 };
133
134 struct linear_gradient
135 {
136     gradient_t           common;
137     pixman_point_fixed_t p1;
138     pixman_point_fixed_t p2;
139 };
140
141 struct circle
142 {
143     pixman_fixed_t x;
144     pixman_fixed_t y;
145     pixman_fixed_t radius;
146 };
147
148 struct radial_gradient
149 {
150     gradient_t common;
151
152     circle_t   c1;
153     circle_t   c2;
154     double     cdx;
155     double     cdy;
156     double     dr;
157     double     A;
158 };
159
160 struct conical_gradient
161 {
162     gradient_t           common;
163     pixman_point_fixed_t center;
164     pixman_fixed_t       angle;
165 };
166
167 struct bits_image
168 {
169     image_common_t             common;
170     pixman_format_code_t       format;
171     const pixman_indexed_t *   indexed;
172     int                        width;
173     int                        height;
174     uint32_t *                 bits;
175     uint32_t *                 free_me;
176     int                        rowstride;  /* in number of uint32_t's */
177
178     /* Fetch a pixel, disregarding alpha maps, transformations etc. */
179     fetch_pixel_32_t           fetch_pixel_raw_32;
180     fetch_pixel_64_t           fetch_pixel_raw_64;
181
182     /* Fetch a pixel, taking alpha maps into account */
183     fetch_pixel_32_t           fetch_pixel_32;
184     fetch_pixel_64_t           fetch_pixel_64;
185
186     /* Fetch raw scanlines, with no regard for transformations, alpha maps etc. */
187     fetch_scanline_t           fetch_scanline_raw_32;
188     fetch_scanline_t           fetch_scanline_raw_64;
189
190     /* Store scanlines with no regard for alpha maps */
191     store_scanline_t           store_scanline_raw_32;
192     store_scanline_t           store_scanline_raw_64;
193
194     /* Store a scanline, taking alpha maps into account */
195     store_scanline_t           store_scanline_32;
196     store_scanline_t           store_scanline_64;
197
198     /* Used for indirect access to the bits */
199     pixman_read_memory_func_t  read_func;
200     pixman_write_memory_func_t write_func;
201 };
202
203 union pixman_image
204 {
205     image_type_t       type;
206     image_common_t     common;
207     bits_image_t       bits;
208     source_image_t     source;
209     gradient_t         gradient;
210     linear_gradient_t  linear;
211     conical_gradient_t conical;
212     radial_gradient_t  radial;
213     solid_fill_t       solid;
214 };
215
216
217 void
218 _pixman_bits_image_setup_raw_accessors (bits_image_t *image);
219
220 void
221 _pixman_image_get_scanline_generic_64  (pixman_image_t *image,
222                                         int             x,
223                                         int             y,
224                                         int             width,
225                                         uint32_t *      buffer,
226                                         const uint32_t *mask);
227
228 source_image_class_t
229 _pixman_image_classify (pixman_image_t *image,
230                         int             x,
231                         int             y,
232                         int             width,
233                         int             height);
234
235 void
236 _pixman_image_get_scanline_32 (pixman_image_t *image,
237                                int             x,
238                                int             y,
239                                int             width,
240                                uint32_t *      buffer,
241                                const uint32_t *mask);
242
243 /* Even thought the type of buffer is uint32_t *, the function actually expects
244  * a uint64_t *buffer.
245  */
246 void
247 _pixman_image_get_scanline_64 (pixman_image_t *image,
248                                int             x,
249                                int             y,
250                                int             width,
251                                uint32_t *      buffer,
252                                const uint32_t *unused);
253
254 void
255 _pixman_image_store_scanline_32 (bits_image_t *  image,
256                                  int             x,
257                                  int             y,
258                                  int             width,
259                                  const uint32_t *buffer);
260
261 /* Even though the type of buffer is uint32_t *, the function
262  * actually expects a uint64_t *buffer.
263  */
264 void
265 _pixman_image_store_scanline_64 (bits_image_t *  image,
266                                  int             x,
267                                  int             y,
268                                  int             width,
269                                  const uint32_t *buffer);
270
271 pixman_image_t *
272 _pixman_image_allocate (void);
273
274 pixman_bool_t
275 _pixman_init_gradient (gradient_t *                  gradient,
276                        const pixman_gradient_stop_t *stops,
277                        int                           n_stops);
278 void
279 _pixman_image_reset_clip_region (pixman_image_t *image);
280
281 void
282 _pixman_image_validate (pixman_image_t *image);
283
284 uint32_t
285 _pixman_image_get_solid (pixman_image_t *     image,
286                          pixman_format_code_t format);
287
288 #define PIXMAN_IMAGE_GET_LINE(image, x, y, type, out_stride, line, mul) \
289     do                                                                  \
290     {                                                                   \
291         uint32_t *__bits__;                                             \
292         int       __stride__;                                           \
293                                                                         \
294         __bits__ = image->bits.bits;                                    \
295         __stride__ = image->bits.rowstride;                             \
296         (out_stride) =                                                  \
297             __stride__ * (int) sizeof (uint32_t) / (int) sizeof (type); \
298         (line) =                                                        \
299             ((type *) __bits__) + (out_stride) * (y) + (mul) * (x);     \
300     } while (0)
301
302 /*
303  * Gradient walker
304  */
305 typedef struct
306 {
307     uint32_t                left_ag;
308     uint32_t                left_rb;
309     uint32_t                right_ag;
310     uint32_t                right_rb;
311     int32_t                 left_x;
312     int32_t                 right_x;
313     int32_t                 stepper;
314
315     pixman_gradient_stop_t *stops;
316     int                     num_stops;
317     unsigned int            spread;
318
319     int                     need_reset;
320 } pixman_gradient_walker_t;
321
322 void
323 _pixman_gradient_walker_init (pixman_gradient_walker_t *walker,
324                               gradient_t *              gradient,
325                               unsigned int              spread);
326
327 void
328 _pixman_gradient_walker_reset (pixman_gradient_walker_t *walker,
329                                pixman_fixed_32_32_t      pos);
330
331 uint32_t
332 _pixman_gradient_walker_pixel (pixman_gradient_walker_t *walker,
333                                pixman_fixed_32_32_t      x);
334
335 /*
336  * Edges
337  */
338
339 #define MAX_ALPHA(n)    ((1 << (n)) - 1)
340 #define N_Y_FRAC(n)     ((n) == 1 ? 1 : (1 << ((n) / 2)) - 1)
341 #define N_X_FRAC(n)     ((n) == 1 ? 1 : (1 << ((n) / 2)) + 1)
342
343 #define STEP_Y_SMALL(n) (pixman_fixed_1 / N_Y_FRAC (n))
344 #define STEP_Y_BIG(n)   (pixman_fixed_1 - (N_Y_FRAC (n) - 1) * STEP_Y_SMALL (n))
345
346 #define Y_FRAC_FIRST(n) (STEP_Y_BIG (n) / 2)
347 #define Y_FRAC_LAST(n)  (Y_FRAC_FIRST (n) + (N_Y_FRAC (n) - 1) * STEP_Y_SMALL (n))
348
349 #define STEP_X_SMALL(n) (pixman_fixed_1 / N_X_FRAC (n))
350 #define STEP_X_BIG(n)   (pixman_fixed_1 - (N_X_FRAC (n) - 1) * STEP_X_SMALL (n))
351
352 #define X_FRAC_FIRST(n) (STEP_X_BIG (n) / 2)
353 #define X_FRAC_LAST(n)  (X_FRAC_FIRST (n) + (N_X_FRAC (n) - 1) * STEP_X_SMALL (n))
354
355 #define RENDER_SAMPLES_X(x, n)                                          \
356     ((n) == 1? 0 : (pixman_fixed_frac (x) +                             \
357                     X_FRAC_FIRST (n)) / STEP_X_SMALL (n))
358
359 void
360 pixman_rasterize_edges_accessors (pixman_image_t *image,
361                                   pixman_edge_t * l,
362                                   pixman_edge_t * r,
363                                   pixman_fixed_t  t,
364                                   pixman_fixed_t  b);
365
366 /*
367  * Implementations
368  */
369 typedef struct pixman_implementation_t pixman_implementation_t;
370
371 typedef void (*pixman_combine_32_func_t) (pixman_implementation_t *imp,
372                                           pixman_op_t              op,
373                                           uint32_t *               dest,
374                                           const uint32_t *         src,
375                                           const uint32_t *         mask,
376                                           int                      width);
377
378 typedef void (*pixman_combine_64_func_t) (pixman_implementation_t *imp,
379                                           pixman_op_t              op,
380                                           uint64_t *               dest,
381                                           const uint64_t *         src,
382                                           const uint64_t *         mask,
383                                           int                      width);
384
385 typedef void (*pixman_composite_func_t) (pixman_implementation_t *imp,
386                                          pixman_op_t              op,
387                                          pixman_image_t *         src,
388                                          pixman_image_t *         mask,
389                                          pixman_image_t *         dest,
390                                          int32_t                  src_x,
391                                          int32_t                  src_y,
392                                          int32_t                  mask_x,
393                                          int32_t                  mask_y,
394                                          int32_t                  dest_x,
395                                          int32_t                  dest_y,
396                                          int32_t                  width,
397                                          int32_t                  height);
398 typedef pixman_bool_t (*pixman_blt_func_t) (pixman_implementation_t *imp,
399                                             uint32_t *               src_bits,
400                                             uint32_t *               dst_bits,
401                                             int                      src_stride,
402                                             int                      dst_stride,
403                                             int                      src_bpp,
404                                             int                      dst_bpp,
405                                             int                      src_x,
406                                             int                      src_y,
407                                             int                      dst_x,
408                                             int                      dst_y,
409                                             int                      width,
410                                             int                      height);
411 typedef pixman_bool_t (*pixman_fill_func_t) (pixman_implementation_t *imp,
412                                              uint32_t *               bits,
413                                              int                      stride,
414                                              int                      bpp,
415                                              int                      x,
416                                              int                      y,
417                                              int                      width,
418                                              int                      height,
419                                              uint32_t                 xor);
420
421 void _pixman_setup_combiner_functions_32 (pixman_implementation_t *imp);
422 void _pixman_setup_combiner_functions_64 (pixman_implementation_t *imp);
423
424 typedef struct
425 {
426     pixman_op_t             op;
427     pixman_format_code_t    src_format;
428     uint32_t                src_flags;
429     pixman_format_code_t    mask_format;
430     uint32_t                mask_flags;
431     pixman_format_code_t    dest_format;
432     uint32_t                dest_flags;
433     pixman_composite_func_t func;
434 } pixman_fast_path_t;
435
436 struct pixman_implementation_t
437 {
438     pixman_implementation_t *   toplevel;
439     pixman_implementation_t *   delegate;
440     const pixman_fast_path_t *  fast_paths;
441     
442     pixman_blt_func_t           blt;
443     pixman_fill_func_t          fill;
444
445     pixman_combine_32_func_t    combine_32[PIXMAN_N_OPERATORS];
446     pixman_combine_32_func_t    combine_32_ca[PIXMAN_N_OPERATORS];
447     pixman_combine_64_func_t    combine_64[PIXMAN_N_OPERATORS];
448     pixman_combine_64_func_t    combine_64_ca[PIXMAN_N_OPERATORS];
449 };
450
451 pixman_implementation_t *
452 _pixman_implementation_create (pixman_implementation_t *delegate,
453                                const pixman_fast_path_t *fast_paths);
454
455 void
456 _pixman_implementation_combine_32 (pixman_implementation_t *imp,
457                                    pixman_op_t              op,
458                                    uint32_t *               dest,
459                                    const uint32_t *         src,
460                                    const uint32_t *         mask,
461                                    int                      width);
462 void
463 _pixman_implementation_combine_64 (pixman_implementation_t *imp,
464                                    pixman_op_t              op,
465                                    uint64_t *               dest,
466                                    const uint64_t *         src,
467                                    const uint64_t *         mask,
468                                    int                      width);
469 void
470 _pixman_implementation_combine_32_ca (pixman_implementation_t *imp,
471                                       pixman_op_t              op,
472                                       uint32_t *               dest,
473                                       const uint32_t *         src,
474                                       const uint32_t *         mask,
475                                       int                      width);
476 void
477 _pixman_implementation_combine_64_ca (pixman_implementation_t *imp,
478                                       pixman_op_t              op,
479                                       uint64_t *               dest,
480                                       const uint64_t *         src,
481                                       const uint64_t *         mask,
482                                       int                      width);
483
484 pixman_bool_t
485 _pixman_implementation_blt (pixman_implementation_t *imp,
486                             uint32_t *               src_bits,
487                             uint32_t *               dst_bits,
488                             int                      src_stride,
489                             int                      dst_stride,
490                             int                      src_bpp,
491                             int                      dst_bpp,
492                             int                      src_x,
493                             int                      src_y,
494                             int                      dst_x,
495                             int                      dst_y,
496                             int                      width,
497                             int                      height);
498
499 pixman_bool_t
500 _pixman_implementation_fill (pixman_implementation_t *imp,
501                              uint32_t *               bits,
502                              int                      stride,
503                              int                      bpp,
504                              int                      x,
505                              int                      y,
506                              int                      width,
507                              int                      height,
508                              uint32_t                 xor);
509
510 /* Specific implementations */
511 pixman_implementation_t *
512 _pixman_implementation_create_general (void);
513
514 pixman_implementation_t *
515 _pixman_implementation_create_fast_path (void);
516
517 #ifdef USE_MMX
518 pixman_implementation_t *
519 _pixman_implementation_create_mmx (void);
520 #endif
521
522 #ifdef USE_SSE2
523 pixman_implementation_t *
524 _pixman_implementation_create_sse2 (void);
525 #endif
526
527 #ifdef USE_ARM_SIMD
528 pixman_implementation_t *
529 _pixman_implementation_create_arm_simd (void);
530 #endif
531
532 #ifdef USE_ARM_NEON
533 pixman_implementation_t *
534 _pixman_implementation_create_arm_neon (void);
535 #endif
536
537 #ifdef USE_VMX
538 pixman_implementation_t *
539 _pixman_implementation_create_vmx (void);
540 #endif
541
542 pixman_implementation_t *
543 _pixman_choose_implementation (void);
544
545
546
547 /*
548  * Utilities
549  */
550
551 /* These "formats" all have depth 0, so they
552  * will never clash with any real ones
553  */
554 #define PIXMAN_null             PIXMAN_FORMAT (0, 0, 0, 0, 0, 0)
555 #define PIXMAN_solid            PIXMAN_FORMAT (0, 1, 0, 0, 0, 0)
556 #define PIXMAN_pixbuf           PIXMAN_FORMAT (0, 2, 0, 0, 0, 0)
557 #define PIXMAN_rpixbuf          PIXMAN_FORMAT (0, 3, 0, 0, 0, 0)
558 #define PIXMAN_unknown          PIXMAN_FORMAT (0, 4, 0, 0, 0, 0)
559 #define PIXMAN_any              PIXMAN_FORMAT (0, 5, 0, 0, 0, 0)
560
561 #define PIXMAN_OP_any           (PIXMAN_N_OPERATORS + 1)
562
563 #define FAST_PATH_ID_TRANSFORM                  (1 <<  0)
564 #define FAST_PATH_NO_ALPHA_MAP                  (1 <<  1)
565 #define FAST_PATH_NO_CONVOLUTION_FILTER         (1 <<  2)
566 #define FAST_PATH_NO_PAD_REPEAT                 (1 <<  3)
567 #define FAST_PATH_NO_REFLECT_REPEAT             (1 <<  4)
568 #define FAST_PATH_NO_ACCESSORS                  (1 <<  5)
569 #define FAST_PATH_NO_WIDE_FORMAT                (1 <<  6)
570 #define FAST_PATH_COVERS_CLIP                   (1 <<  7)
571 #define FAST_PATH_COMPONENT_ALPHA               (1 <<  8)
572 #define FAST_PATH_UNIFIED_ALPHA                 (1 <<  9)
573 #define FAST_PATH_SCALE_TRANSFORM               (1 << 10)
574 #define FAST_PATH_NEAREST_FILTER                (1 << 11)
575 #define FAST_PATH_SIMPLE_REPEAT                 (1 << 12)
576 #define FAST_PATH_IS_OPAQUE                     (1 << 13)
577 #define FAST_PATH_NEEDS_WORKAROUND              (1 << 14)
578 #define FAST_PATH_NO_NONE_REPEAT                (1 << 15)
579 #define FAST_PATH_SAMPLES_COVER_CLIP            (1 << 16)
580 #define FAST_PATH_16BIT_SAFE                    (1 << 17)
581 #define FAST_PATH_X_UNIT_POSITIVE               (1 << 18)
582
583 #define _FAST_PATH_STANDARD_FLAGS                                       \
584     (FAST_PATH_ID_TRANSFORM             |                               \
585      FAST_PATH_NO_ALPHA_MAP             |                               \
586      FAST_PATH_NO_CONVOLUTION_FILTER    |                               \
587      FAST_PATH_NO_PAD_REPEAT            |                               \
588      FAST_PATH_NO_REFLECT_REPEAT        |                               \
589      FAST_PATH_NO_ACCESSORS             |                               \
590      FAST_PATH_NO_WIDE_FORMAT           |                               \
591      FAST_PATH_COVERS_CLIP)
592
593 #define FAST_PATH_STD_SRC_FLAGS                                         \
594     _FAST_PATH_STANDARD_FLAGS
595 #define FAST_PATH_STD_MASK_U_FLAGS                                      \
596     (_FAST_PATH_STANDARD_FLAGS          |                               \
597      FAST_PATH_UNIFIED_ALPHA)
598 #define FAST_PATH_STD_MASK_CA_FLAGS                                     \
599     (_FAST_PATH_STANDARD_FLAGS          |                               \
600      FAST_PATH_COMPONENT_ALPHA)
601 #define FAST_PATH_STD_DEST_FLAGS                                        \
602     (FAST_PATH_NO_ACCESSORS             |                               \
603      FAST_PATH_NO_WIDE_FORMAT)
604
605 #define FAST_PATH(op, src, src_flags, mask, mask_flags, dest, dest_flags, func) \
606     PIXMAN_OP_ ## op,                                                   \
607     PIXMAN_ ## src,                                                     \
608     src_flags,                                                          \
609     PIXMAN_ ## mask,                                                    \
610     mask_flags,                                                         \
611     PIXMAN_ ## dest,                                                    \
612     dest_flags,                                                         \
613     func
614
615 #define PIXMAN_STD_FAST_PATH(op, src, mask, dest, func)                 \
616     { FAST_PATH (                                                       \
617           op,                                                           \
618           src, FAST_PATH_STD_SRC_FLAGS,                                 \
619           mask, (PIXMAN_ ## mask) ? FAST_PATH_STD_MASK_U_FLAGS : 0,     \
620           dest, FAST_PATH_STD_DEST_FLAGS,                               \
621           func) }
622
623 #define PIXMAN_STD_FAST_PATH_CA(op, src, mask, dest, func)              \
624     { FAST_PATH (                                                       \
625           op,                                                           \
626           src, FAST_PATH_STD_SRC_FLAGS,                                 \
627           mask, FAST_PATH_STD_MASK_CA_FLAGS,                            \
628           dest, FAST_PATH_STD_DEST_FLAGS,                               \
629           func) }
630
631 /* Memory allocation helpers */
632 void *
633 pixman_malloc_ab (unsigned int n, unsigned int b);
634
635 void *
636 pixman_malloc_abc (unsigned int a, unsigned int b, unsigned int c);
637
638 pixman_bool_t
639 pixman_multiply_overflows_int (unsigned int a, unsigned int b);
640
641 pixman_bool_t
642 pixman_addition_overflows_int (unsigned int a, unsigned int b);
643
644 /* Compositing utilities */
645 void
646 pixman_expand (uint64_t *           dst,
647                const uint32_t *     src,
648                pixman_format_code_t format,
649                int                  width);
650
651 void
652 pixman_contract (uint32_t *      dst,
653                  const uint64_t *src,
654                  int             width);
655
656
657 /* Region Helpers */
658 pixman_bool_t
659 pixman_region32_copy_from_region16 (pixman_region32_t *dst,
660                                     pixman_region16_t *src);
661
662 pixman_bool_t
663 pixman_region16_copy_from_region32 (pixman_region16_t *dst,
664                                     pixman_region32_t *src);
665
666
667 /* Misc macros */
668
669 #ifndef FALSE
670 #   define FALSE 0
671 #endif
672
673 #ifndef TRUE
674 #   define TRUE 1
675 #endif
676
677 #ifndef MIN
678 #  define MIN(a, b) ((a < b) ? a : b)
679 #endif
680
681 #ifndef MAX
682 #  define MAX(a, b) ((a > b) ? a : b)
683 #endif
684
685 /* Integer division that rounds towards -infinity */
686 #define DIV(a, b)                                          \
687     ((((a) < 0) == ((b) < 0)) ? (a) / (b) :                \
688      ((a) - (b) + 1 - (((b) < 0) << 1)) / (b))
689
690 /* Modulus that produces the remainder wrt. DIV */
691 #define MOD(a, b) ((a) < 0 ? ((b) - ((-(a) - 1) % (b))) - 1 : (a) % (b))
692
693 #define CLIP(v, low, high) ((v) < (low) ? (low) : ((v) > (high) ? (high) : (v)))
694
695 /* Conversion between 8888 and 0565 */
696
697 #define CONVERT_8888_TO_0565(s)                                         \
698     ((((s) >> 3) & 0x001f) |                                            \
699      (((s) >> 5) & 0x07e0) |                                            \
700      (((s) >> 8) & 0xf800))
701
702 #define CONVERT_0565_TO_0888(s)                                         \
703     (((((s) << 3) & 0xf8) | (((s) >> 2) & 0x7)) |                       \
704      ((((s) << 5) & 0xfc00) | (((s) >> 1) & 0x300)) |                   \
705      ((((s) << 8) & 0xf80000) | (((s) << 3) & 0x70000)))
706
707 #define CONVERT_0565_TO_8888(s) (CONVERT_0565_TO_0888(s) | 0xff000000)
708
709 /* Trivial versions that are useful in macros */
710 #define CONVERT_8888_TO_8888(s) (s)
711 #define CONVERT_0565_TO_0565(s) (s)
712
713 #define PIXMAN_FORMAT_IS_WIDE(f)                                        \
714     (PIXMAN_FORMAT_A (f) > 8 ||                                         \
715      PIXMAN_FORMAT_R (f) > 8 ||                                         \
716      PIXMAN_FORMAT_G (f) > 8 ||                                         \
717      PIXMAN_FORMAT_B (f) > 8)
718
719 #ifdef WORDS_BIGENDIAN
720 #   define SCREEN_SHIFT_LEFT(x,n)       ((x) << (n))
721 #   define SCREEN_SHIFT_RIGHT(x,n)      ((x) >> (n))
722 #else
723 #   define SCREEN_SHIFT_LEFT(x,n)       ((x) >> (n))
724 #   define SCREEN_SHIFT_RIGHT(x,n)      ((x) << (n))
725 #endif
726
727 /*
728  * Various debugging code
729  */
730
731 #undef DEBUG
732
733 /* Turn on debugging depending on what type of release this is
734  */
735 #if (((PIXMAN_VERSION_MICRO % 2) == 0) && ((PIXMAN_VERSION_MINOR % 2) == 1))
736
737 /* Debugging gets turned on for development releases because these
738  * are the things that end up in bleeding edge distributions such
739  * as Rawhide etc.
740  *
741  * For performance reasons we don't turn it on for stable releases or
742  * random git checkouts. (Random git checkouts are often used for
743  * performance work).
744  */
745
746 #    define DEBUG
747
748 #endif
749
750 #ifdef DEBUG
751
752 void
753 _pixman_log_error (const char *function, const char *message);
754
755 #define return_if_fail(expr)                                            \
756     do                                                                  \
757     {                                                                   \
758         if (!(expr))                                                    \
759         {                                                               \
760             _pixman_log_error (FUNC, "The expression " # expr " was false"); \
761             return;                                                     \
762         }                                                               \
763     }                                                                   \
764     while (0)
765
766 #define return_val_if_fail(expr, retval)                                \
767     do                                                                  \
768     {                                                                   \
769         if (!(expr))                                                    \
770         {                                                               \
771             _pixman_log_error (FUNC, "The expression " # expr " was false"); \
772             return (retval);                                            \
773         }                                                               \
774     }                                                                   \
775     while (0)
776
777 #define critical_if_fail(expr)                                          \
778     do                                                                  \
779     {                                                                   \
780         if (!(expr))                                                    \
781             _pixman_log_error (FUNC, "The expression " # expr " was false"); \
782     }                                                                   \
783     while (0)
784
785
786 #else
787
788 #define _pixman_log_error(f,m) do { } while (0)                         \
789
790 #define return_if_fail(expr)                                            \
791     do                                                                  \
792     {                                                                   \
793         if (!(expr))                                                    \
794             return;                                                     \
795     }                                                                   \
796     while (0)
797
798 #define return_val_if_fail(expr, retval)                                \
799     do                                                                  \
800     {                                                                   \
801         if (!(expr))                                                    \
802             return (retval);                                            \
803     }                                                                   \
804     while (0)
805
806 #define critical_if_fail(expr)                                          \
807     do                                                                  \
808     {                                                                   \
809     }                                                                   \
810     while (0)
811 #endif
812
813 /*
814  * Timers
815  */
816
817 #ifdef PIXMAN_TIMERS
818
819 static inline uint64_t
820 oil_profile_stamp_rdtsc (void)
821 {
822     uint64_t ts;
823
824     __asm__ __volatile__ ("rdtsc\n" : "=A" (ts));
825     return ts;
826 }
827
828 #define OIL_STAMP oil_profile_stamp_rdtsc
829
830 typedef struct pixman_timer_t pixman_timer_t;
831
832 struct pixman_timer_t
833 {
834     int             initialized;
835     const char *    name;
836     uint64_t        n_times;
837     uint64_t        total;
838     pixman_timer_t *next;
839 };
840
841 extern int timer_defined;
842
843 void pixman_timer_register (pixman_timer_t *timer);
844
845 #define TIMER_BEGIN(tname)                                              \
846     {                                                                   \
847         static pixman_timer_t timer ## tname;                           \
848         uint64_t              begin ## tname;                           \
849                                                                         \
850         if (!timer ## tname.initialized)                                \
851         {                                                               \
852             timer ## tname.initialized = 1;                             \
853             timer ## tname.name = # tname;                              \
854             pixman_timer_register (&timer ## tname);                    \
855         }                                                               \
856                                                                         \
857         timer ## tname.n_times++;                                       \
858         begin ## tname = OIL_STAMP ();
859
860 #define TIMER_END(tname)                                                \
861     timer ## tname.total += OIL_STAMP () - begin ## tname;              \
862     }
863
864 #endif /* PIXMAN_TIMERS */
865
866 #endif /* PIXMAN_PRIVATE_H */