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