Move accessor macros to their own header.
[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  * Images
16  */
17 typedef struct image_common image_common_t;
18 typedef struct source_image source_image_t;
19 typedef struct solid_fill solid_fill_t;
20 typedef struct gradient gradient_t;
21 typedef struct linear_gradient linear_gradient_t;
22 typedef struct horizontal_gradient horizontal_gradient_t;
23 typedef struct vertical_gradient vertical_gradient_t;
24 typedef struct conical_gradient conical_gradient_t;
25 typedef struct radial_gradient radial_gradient_t;
26 typedef struct bits_image bits_image_t;
27 typedef struct circle circle_t;
28
29 typedef void     (*fetchProc32)        (bits_image_t *pict,
30                                         int x, int y, int width,
31                                         uint32_t *buffer);
32 typedef uint32_t (*fetchPixelProc32)   (bits_image_t *pict,
33                                         int offset, int line);
34 typedef void     (*storeProc32)        (pixman_image_t *, uint32_t *bits,
35                                         const uint32_t *values,
36                                         int x, int width);
37 typedef void     (*fetchProc64)        (bits_image_t *pict,
38                                         int x, int y, int width,
39                                         uint64_t *buffer);
40 typedef uint64_t (*fetchPixelProc64)   (bits_image_t *pict,
41                                         int offset, int line);
42 typedef void     (*storeProc64)        (pixman_image_t *, uint32_t *bits,
43                                         const uint64_t *values,
44                                         int x, int width);
45 typedef void     (*fetch_pixels_32_t) (bits_image_t *image,
46                                        uint32_t *buffer, int n_pixels);
47 typedef void     (*fetch_pixels_64_t) (bits_image_t *image,
48                                        uint64_t *buffer, int n_pixels);
49 typedef void     (*scanStoreProc)     (bits_image_t *img,
50                                        int x, int y, int width,
51                                        uint32_t *buffer);
52 typedef void     (*scanFetchProc)     (pixman_image_t *,
53                                        int, int, int, uint32_t *,
54                                        uint32_t *, uint32_t);
55
56 typedef enum
57 {
58     BITS,
59     LINEAR,
60     CONICAL,
61     RADIAL,
62     SOLID
63 } image_type_t;
64
65 typedef enum
66 {
67     SOURCE_IMAGE_CLASS_UNKNOWN,
68     SOURCE_IMAGE_CLASS_HORIZONTAL,
69     SOURCE_IMAGE_CLASS_VERTICAL,
70 } source_pict_class_t;
71
72 typedef source_pict_class_t (* classify_func_t) (pixman_image_t *image,
73                                                  int             x,
74                                                  int             y,
75                                                  int             width,
76                                                  int             height);
77 typedef void (* property_changed_func_t)        (pixman_image_t *image);
78
79 struct image_common
80 {
81     image_type_t                type;
82     int32_t                     ref_count;
83     pixman_region32_t           clip_region;
84     pixman_bool_t               have_clip_region;   /* FALSE if there is no clip */
85     pixman_bool_t               client_clip;        /* Whether the source clip was
86                                                        set by a client */
87     pixman_bool_t               clip_sources;       /* Whether the clip applies when
88                                                      * the image is used as a source
89                                                      */
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     pixman_read_memory_func_t   read_func;
100     pixman_write_memory_func_t  write_func;
101     classify_func_t             classify;
102     property_changed_func_t     property_changed;
103     scanFetchProc               get_scanline_32;
104     scanFetchProc               get_scanline_64;
105
106     pixman_image_destroy_func_t destroy_func;
107     void *                      destroy_data;
108 };
109
110 struct source_image
111 {
112     image_common_t      common;
113     source_pict_class_t class;
114 };
115
116 struct solid_fill
117 {
118     source_image_t      common;
119     uint32_t            color;          /* FIXME: shouldn't this be a pixman_color_t? */
120 };
121
122 struct gradient
123 {
124     source_image_t              common;
125     int                         n_stops;
126     pixman_gradient_stop_t *    stops;
127     int                         stop_range;
128     uint32_t *                  color_table;
129     int                         color_table_size;
130 };
131
132 struct linear_gradient
133 {
134     gradient_t                  common;
135     pixman_point_fixed_t        p1;
136     pixman_point_fixed_t        p2;
137 };
138
139 struct circle
140 {
141     pixman_fixed_t x;
142     pixman_fixed_t y;
143     pixman_fixed_t radius;
144 };
145
146 struct radial_gradient
147 {
148     gradient_t  common;
149
150     circle_t    c1;
151     circle_t    c2;
152     double      cdx;
153     double      cdy;
154     double      dr;
155     double      A;
156 };
157
158 struct conical_gradient
159 {
160     gradient_t                  common;
161     pixman_point_fixed_t        center;
162     pixman_fixed_t              angle;
163 };
164
165 struct bits_image
166 {
167     image_common_t              common;
168     pixman_format_code_t        format;
169     const pixman_indexed_t     *indexed;
170     int                         width;
171     int                         height;
172     uint32_t *                  bits;
173     uint32_t *                  free_me;
174     int                         rowstride; /* in number of uint32_t's */
175
176     /* Fetch raw pixels, with no regard for transformations, alpha map etc. */
177     fetch_pixels_32_t           fetch_pixels_raw_32;
178     fetch_pixels_64_t           fetch_pixels_raw_64;
179
180     /* Fetch raw scanlines, with no regard for transformations, alpha maps etc. */
181     fetchProc32                 fetch_scanline_raw_32;
182     fetchProc64                 fetch_scanline_raw_64;
183
184     /* Store scanlines with no regard for alpha maps */
185     storeProc32                 store_scanline_raw_32;
186     storeProc64                 store_scanline_raw_64;
187
188     /* Store a scanline, taking alpha maps into account */
189     scanStoreProc               store_scanline_32;
190     scanStoreProc               store_scanline_64;
191
192     /* Used for indirect access to the bits */
193     pixman_read_memory_func_t   read_func;
194     pixman_write_memory_func_t  write_func;
195 };
196
197 union pixman_image
198 {
199     image_type_t                type;
200     image_common_t              common;
201     bits_image_t                bits;
202     source_image_t              source;
203     gradient_t                  gradient;
204     linear_gradient_t           linear;
205     conical_gradient_t          conical;
206     radial_gradient_t           radial;
207     solid_fill_t                solid;
208 };
209
210
211 void
212 _pixman_bits_image_setup_raw_accessors (bits_image_t   *image);
213
214 void
215 _pixman_image_get_scanline_64_generic  (pixman_image_t *pict,
216                                         int             x,
217                                         int             y,
218                                         int             width,
219                                         uint64_t       *buffer,
220                                         uint64_t       *mask,
221                                         uint32_t        maskBits);
222
223 source_pict_class_t
224 _pixman_image_classify (pixman_image_t *image,
225                         int             x,
226                         int             y,
227                         int             width,
228                         int             height);
229
230 void
231 _pixman_image_get_scanline_32 (pixman_image_t *image, int x, int y, int width,
232                                uint32_t *buffer, uint32_t *mask,
233                                uint32_t mask_bits);
234
235 /* Even thought the type of buffer is uint32_t *, the function actually expects
236  * a uint64_t *buffer.
237  */
238 void
239 _pixman_image_get_scanline_64 (pixman_image_t *image, int x, int y, int width,
240                                uint32_t *buffer,
241                                uint32_t *unused, uint32_t unused2);
242
243 void
244 _pixman_image_store_scanline_32 (bits_image_t *image, int x, int y, int width,
245                                  uint32_t *buffer);
246 void
247 _pixman_image_fetch_pixels (bits_image_t *image, uint32_t *buffer,
248                             int n_pixels);
249
250 /* Even thought the type of buffer is uint32_t *, the function actually expects
251  * a uint64_t *buffer.
252  */
253 void
254 _pixman_image_store_scanline_64 (bits_image_t *image, int x, int y, int width,
255                                  uint32_t *buffer);
256
257 pixman_image_t *
258 _pixman_image_allocate (void);
259
260 pixman_bool_t
261 _pixman_init_gradient (gradient_t                   *gradient,
262                        const pixman_gradient_stop_t *stops,
263                        int                           n_stops);
264 void
265 _pixman_image_reset_clip_region (pixman_image_t *image);
266
267 pixman_bool_t
268 _pixman_image_is_opaque(pixman_image_t *image);
269
270 pixman_bool_t
271 _pixman_image_is_solid (pixman_image_t *image);
272
273 uint32_t
274 _pixman_image_get_solid (pixman_image_t *image,
275                         pixman_format_code_t format);
276
277 #define fbComposeGetStart(pict,x,y,type,out_stride,line,mul) do {       \
278         uint32_t        *__bits__;                                      \
279         int             __stride__;                                     \
280                                                                         \
281         __bits__ = pict->bits.bits;                                     \
282         __stride__ = pict->bits.rowstride;                              \
283         (out_stride) = __stride__ * (int) sizeof (uint32_t) / (int) sizeof (type); \
284         (line) = ((type *) __bits__) +                                  \
285             (out_stride) * (y) + (mul) * (x);                           \
286     } while (0)
287
288 /*
289  * Gradient walker
290  */
291 typedef struct
292 {
293     uint32_t        left_ag;
294     uint32_t        left_rb;
295     uint32_t        right_ag;
296     uint32_t        right_rb;
297     int32_t       left_x;
298     int32_t       right_x;
299     int32_t       stepper;
300
301     pixman_gradient_stop_t      *stops;
302     int                      num_stops;
303     unsigned int             spread;
304
305     int           need_reset;
306 } pixman_gradient_walker_t;
307
308 void
309 _pixman_gradient_walker_init (pixman_gradient_walker_t  *walker,
310                               gradient_t      *gradient,
311                               unsigned int     spread);
312
313 void
314 _pixman_gradient_walker_reset (pixman_gradient_walker_t       *walker,
315                                pixman_fixed_32_32_t  pos);
316
317 uint32_t
318 _pixman_gradient_walker_pixel (pixman_gradient_walker_t       *walker,
319                                pixman_fixed_32_32_t  x);
320
321 #define FbIntMult(a,b,t) ((t) = (a) * (b) + 0x80, ((((t) >> 8) + (t)) >> 8))
322 #define FbIntDiv(a,b)    (((uint16_t) (a) * 255) / (b))
323 #define FbIntAdd(x,y,t) (                                               \
324         (t) = x + y,                                                    \
325         (uint32_t) (uint8_t) ((t) | (0 - ((t) >> 8))))
326 #define div_255(x) (((x) + 0x80 + (((x) + 0x80) >> 8)) >> 8)
327 #define div_65535(x) (((x) + 0x8000 + (((x) + 0x8000) >> 16)) >> 16)
328
329 #define PIXMAN_FORMAT_16BPC(f)  (PIXMAN_FORMAT_A(f) > 8 || \
330                                  PIXMAN_FORMAT_R(f) > 8 || \
331                                  PIXMAN_FORMAT_G(f) > 8 || \
332                                  PIXMAN_FORMAT_B(f) > 8)
333 /*
334  * Edges
335  */
336
337 #define MAX_ALPHA(n)    ((1 << (n)) - 1)
338 #define N_Y_FRAC(n)     ((n) == 1 ? 1 : (1 << ((n)/2)) - 1)
339 #define N_X_FRAC(n)     ((n) == 1 ? 1 : (1 << ((n)/2)) + 1)
340
341 #define STEP_Y_SMALL(n) (pixman_fixed_1 / N_Y_FRAC(n))
342 #define STEP_Y_BIG(n)   (pixman_fixed_1 - (N_Y_FRAC(n) - 1) * STEP_Y_SMALL(n))
343
344 #define Y_FRAC_FIRST(n) (STEP_Y_SMALL(n) / 2)
345 #define Y_FRAC_LAST(n)  (Y_FRAC_FIRST(n) + (N_Y_FRAC(n) - 1) * STEP_Y_SMALL(n))
346
347 #define STEP_X_SMALL(n) (pixman_fixed_1 / N_X_FRAC(n))
348 #define STEP_X_BIG(n)   (pixman_fixed_1 - (N_X_FRAC(n) - 1) * STEP_X_SMALL(n))
349
350 #define X_FRAC_FIRST(n) (STEP_X_SMALL(n) / 2)
351 #define X_FRAC_LAST(n)  (X_FRAC_FIRST(n) + (N_X_FRAC(n) - 1) * STEP_X_SMALL(n))
352
353 #define RenderSamplesX(x,n)     ((n) == 1 ? 0 : (pixman_fixed_frac (x) + X_FRAC_FIRST(n)) / STEP_X_SMALL(n))
354
355 void
356 pixman_rasterize_edges_accessors (pixman_image_t *image,
357                                   pixman_edge_t *l,
358                                   pixman_edge_t *r,
359                                   pixman_fixed_t        t,
360                                   pixman_fixed_t        b);
361
362 /*
363  * Implementations
364  */
365
366 typedef struct pixman_implementation_t pixman_implementation_t;
367
368 typedef void (* pixman_combine_32_func_t) (pixman_implementation_t *    imp,
369                                            pixman_op_t                  op,
370                                            uint32_t *                   dest,
371                                            const uint32_t *             src,
372                                            const uint32_t *             mask,
373                                            int                          width);
374
375 typedef void (* pixman_combine_64_func_t) (pixman_implementation_t *    imp,
376                                            pixman_op_t                  op,
377                                            uint64_t *                   dest,
378                                            const uint64_t *             src,
379                                            const uint64_t *             mask,
380                                            int                          width);
381
382 typedef void (* pixman_composite_func_t)  (pixman_implementation_t *    imp,
383                                            pixman_op_t                  op,
384                                            pixman_image_t *             src,
385                                            pixman_image_t *             mask,
386                                            pixman_image_t *             dest,
387                                            int32_t                      src_x,
388                                            int32_t                      src_y,
389                                            int32_t                      mask_x,
390                                            int32_t                      mask_y,
391                                            int32_t                      dest_x,
392                                            int32_t                      dest_y,
393                                            int32_t                      width,
394                                            int32_t                      height);
395 typedef pixman_bool_t (* pixman_blt_func_t) (pixman_implementation_t *  imp,
396                                              uint32_t *                 src_bits,
397                                              uint32_t *                 dst_bits,
398                                              int                        src_stride,
399                                              int                        dst_stride,
400                                              int                        src_bpp,
401                                              int                        dst_bpp,
402                                              int                        src_x,
403                                              int                        src_y,
404                                              int                        dst_x,
405                                              int                        dst_y,
406                                              int                        width,
407                                              int                        height);
408 typedef pixman_bool_t (* pixman_fill_func_t) (pixman_implementation_t *imp,
409                                               uint32_t *bits,
410                                               int stride,
411                                               int bpp,
412                                               int x,
413                                               int y,
414                                               int width,
415                                               int height,
416                                               uint32_t xor);
417
418 void _pixman_setup_combiner_functions_32 (pixman_implementation_t *imp);
419 void _pixman_setup_combiner_functions_64 (pixman_implementation_t *imp);
420
421 struct pixman_implementation_t
422 {
423     pixman_implementation_t *   toplevel;
424     pixman_implementation_t *   delegate;
425
426     pixman_composite_func_t     composite;
427     pixman_blt_func_t           blt;
428     pixman_fill_func_t          fill;
429     
430     pixman_combine_32_func_t    combine_32[PIXMAN_OP_LAST];
431     pixman_combine_32_func_t    combine_32_ca[PIXMAN_OP_LAST];
432     pixman_combine_64_func_t    combine_64[PIXMAN_OP_LAST];
433     pixman_combine_64_func_t    combine_64_ca[PIXMAN_OP_LAST];
434 };
435
436 pixman_implementation_t *
437 _pixman_implementation_create (pixman_implementation_t *delegate);
438
439 void
440 _pixman_implementation_combine_32 (pixman_implementation_t *    imp,
441                                    pixman_op_t                  op,
442                                    uint32_t *                   dest,
443                                    const uint32_t *             src,
444                                    const uint32_t *             mask,
445                                    int                          width);
446 void
447 _pixman_implementation_combine_64 (pixman_implementation_t *    imp,
448                                    pixman_op_t                  op,
449                                    uint64_t *                   dest,
450                                    const uint64_t *             src,
451                                    const uint64_t *             mask,
452                                    int                          width);
453 void
454 _pixman_implementation_combine_32_ca (pixman_implementation_t * imp,
455                                       pixman_op_t               op,
456                                       uint32_t *                dest,
457                                       const uint32_t *          src,
458                                       const uint32_t *          mask,
459                                       int                       width);
460 void
461 _pixman_implementation_combine_64_ca (pixman_implementation_t * imp,
462                                       pixman_op_t               op,
463                                       uint64_t *                dest,
464                                       const uint64_t *          src,
465                                       const uint64_t *          mask,
466                                       int                       width);
467 void
468 _pixman_implementation_composite (pixman_implementation_t *     imp,
469                                   pixman_op_t                   op,
470                                   pixman_image_t *              src,
471                                   pixman_image_t *              mask,
472                                   pixman_image_t *              dest,
473                                   int32_t                       src_x,
474                                   int32_t                       src_y,
475                                   int32_t                       mask_x,
476                                   int32_t                       mask_y,
477                                   int32_t                       dest_x,
478                                   int32_t                       dest_y,
479                                   int32_t                       width,
480                                   int32_t                       height);
481
482 pixman_bool_t
483 _pixman_implementation_blt (pixman_implementation_t *   imp,
484                             uint32_t *                  src_bits,
485                             uint32_t *                  dst_bits,
486                             int                         src_stride,
487                             int                         dst_stride,
488                             int                         src_bpp,
489                             int                         dst_bpp,
490                             int                         src_x,
491                             int                         src_y,
492                             int                         dst_x,
493                             int                         dst_y,
494                             int                         width,
495                             int                         height);
496 pixman_bool_t
497 _pixman_implementation_fill (pixman_implementation_t *   imp,
498                              uint32_t *bits,
499                              int stride,
500                              int bpp,
501                              int x,
502                              int y,
503                              int width,
504                              int height,
505                              uint32_t xor);
506     
507 /* Specific implementations */
508 pixman_implementation_t *
509 _pixman_implementation_create_general (void);
510 pixman_implementation_t *
511 _pixman_implementation_create_fast_path (void);
512 #ifdef USE_MMX
513 pixman_implementation_t *
514 _pixman_implementation_create_mmx (void);
515 #endif
516 #ifdef USE_SSE2
517 pixman_implementation_t *
518 _pixman_implementation_create_sse2 (void);
519 #endif
520 #ifdef USE_ARM_SIMD
521 pixman_implementation_t *
522 _pixman_implementation_create_arm_simd (void);
523 #endif
524 #ifdef USE_ARM_NEON
525 pixman_implementation_t *
526 _pixman_implementation_create_arm_neon (void);
527 #endif
528 #ifdef USE_VMX
529 pixman_implementation_t *
530 _pixman_implementation_create_vmx (void);
531 #endif
532
533 pixman_implementation_t *
534 _pixman_choose_implementation (void);
535
536
537
538 /*
539  * Utilities
540  */
541
542 /* These "formats" both have depth 0, so they
543  * will never clash with any real ones
544  */
545 #define PIXMAN_null             PIXMAN_FORMAT(0,0,0,0,0,0)
546 #define PIXMAN_solid            PIXMAN_FORMAT(0,1,0,0,0,0)
547
548 #define NEED_COMPONENT_ALPHA            (1 << 0)
549 #define NEED_PIXBUF                     (1 << 1)
550 #define NEED_SOLID_MASK                 (1 << 2)
551
552 typedef struct
553 {
554     pixman_op_t                 op;
555     pixman_format_code_t        src_format;
556     pixman_format_code_t        mask_format;
557     pixman_format_code_t        dest_format;
558     pixman_composite_func_t     func;
559     uint32_t                    flags;
560 } pixman_fast_path_t;
561
562 /* Memory allocation helpers */
563 void *
564 pixman_malloc_ab (unsigned int n, unsigned int b);
565
566 void *
567 pixman_malloc_abc (unsigned int a, unsigned int b, unsigned int c);
568
569 pixman_bool_t
570 pixman_multiply_overflows_int (unsigned int a, unsigned int b);
571
572 pixman_bool_t
573 pixman_addition_overflows_int (unsigned int a, unsigned int b);
574
575 /* Compositing utilities */
576 pixman_bool_t
577 _pixman_run_fast_path (const pixman_fast_path_t *paths,
578                        pixman_implementation_t *imp,
579                        pixman_op_t op,
580                        pixman_image_t *src,
581                        pixman_image_t *mask,
582                        pixman_image_t *dest,
583                        int32_t src_x,
584                        int32_t src_y,
585                        int32_t mask_x,
586                        int32_t mask_y,
587                        int32_t dest_x,
588                        int32_t dest_y,
589                        int32_t width,
590                        int32_t height);
591     
592 void
593 _pixman_walk_composite_region (pixman_implementation_t *imp,
594                                pixman_op_t op,
595                                pixman_image_t * pSrc,
596                                pixman_image_t * pMask,
597                                pixman_image_t * pDst,
598                                int16_t xSrc,
599                                int16_t ySrc,
600                                int16_t xMask,
601                                int16_t yMask,
602                                int16_t xDst,
603                                int16_t yDst,
604                                uint16_t width,
605                                uint16_t height,
606                                pixman_composite_func_t compositeRect);
607
608 void
609 pixman_expand (uint64_t *dst, const uint32_t *src, pixman_format_code_t, int width);
610
611 void
612 pixman_contract (uint32_t *dst, const uint64_t *src, int width);
613
614
615 /* Region Helpers */
616 pixman_bool_t
617 pixman_region32_copy_from_region16 (pixman_region32_t *dst,
618                                     pixman_region16_t *src);
619
620 pixman_bool_t
621 pixman_region16_copy_from_region32 (pixman_region16_t *dst,
622                                     pixman_region32_t *src);
623
624
625 #ifndef FALSE
626 #   define FALSE 0
627 #endif
628
629 #ifndef TRUE
630 #   define TRUE 1
631 #endif
632
633 #ifndef MIN
634 #  define MIN(a,b) ((a < b)? a : b)
635 #endif
636
637 #ifndef MAX
638 #  define MAX(a,b) ((a > b)? a : b)
639 #endif
640
641 /* Integer division that rounds towards -infinity */
642 #define DIV(a,b) ((((a) < 0) == ((b) < 0)) ? (a) / (b) :                \
643                   ((a) - (b) + 1 - (((b) < 0) << 1)) / (b))
644
645 /* Modulus that produces the remainder wrt. DIV */
646 #define MOD(a,b) ((a) < 0 ? ((b) - ((-(a) - 1) % (b))) - 1 : (a) % (b))
647
648 #define CLIP(v,low,high) ((v) < (low) ? (low) : ((v) > (high) ? (high) : (v)))
649
650 /* Conversion between 8888 and 0565 */
651
652 #define cvt8888to0565(s)    ((((s) >> 3) & 0x001f) | \
653                              (((s) >> 5) & 0x07e0) | \
654                              (((s) >> 8) & 0xf800))
655 #define cvt0565to0888(s)    (((((s) << 3) & 0xf8) | (((s) >> 2) & 0x7)) | \
656                              ((((s) << 5) & 0xfc00) | (((s) >> 1) & 0x300)) | \
657                              ((((s) << 8) & 0xf80000) | (((s) << 3) & 0x70000)))
658
659 /*
660  * Various debugging code
661  */
662
663 #undef DEBUG
664 #define DEBUG 0
665
666 #if DEBUG
667
668 #define return_if_fail(expr)                                            \
669     do                                                                  \
670     {                                                                   \
671         if (!(expr))                                                    \
672         {                                                               \
673             fprintf(stderr, "In %s: %s failed\n", FUNC, #expr);         \
674             return;                                                     \
675         }                                                               \
676     }                                                                   \
677     while (0)
678
679 #define return_val_if_fail(expr, retval)                                \
680     do                                                                  \
681     {                                                                   \
682         if (!(expr))                                                    \
683         {                                                               \
684             fprintf(stderr, "In %s: %s failed\n", FUNC, #expr);         \
685             return (retval);                                            \
686         }                                                               \
687     }                                                                   \
688     while (0)
689
690 #else
691
692 #define return_if_fail(expr)                                            \
693     do                                                                  \
694     {                                                                   \
695         if (!(expr))                                                    \
696             return;                                                     \
697     }                                                                   \
698     while (0)
699
700 #define return_val_if_fail(expr, retval)                                \
701     do                                                                  \
702     {                                                                   \
703         if (!(expr))                                                    \
704             return (retval);                                            \
705     }                                                                   \
706     while (0)
707
708 #endif
709
710 /*
711  * Timers
712  */
713
714 #ifdef PIXMAN_TIMERS
715
716 static inline uint64_t
717 oil_profile_stamp_rdtsc (void)
718 {
719     uint64_t ts;
720     __asm__ __volatile__("rdtsc\n" : "=A" (ts));
721     return ts;
722 }
723 #define OIL_STAMP oil_profile_stamp_rdtsc
724
725 typedef struct pixman_timer_t pixman_timer_t;
726
727 struct pixman_timer_t
728 {
729     int initialized;
730     const char *name;
731     uint64_t n_times;
732     uint64_t total;
733     pixman_timer_t *next;
734 };
735
736 extern int timer_defined;
737 void pixman_timer_register (pixman_timer_t *timer);
738
739 #define TIMER_BEGIN(tname)                                              \
740     {                                                                   \
741         static pixman_timer_t   timer##tname;                           \
742         uint64_t                begin##tname;                           \
743                                                                         \
744         if (!timer##tname.initialized)                                  \
745         {                                                               \
746             timer##tname.initialized = 1;                               \
747             timer##tname.name = #tname;                                 \
748             pixman_timer_register (&timer##tname);                      \
749         }                                                               \
750                                                                         \
751         timer##tname.n_times++;                                         \
752         begin##tname = OIL_STAMP();
753
754 #define TIMER_END(tname)                                                \
755         timer##tname.total += OIL_STAMP() - begin##tname;               \
756     }
757
758 #endif /* PIXMAN_TIMERS */
759
760 #endif /* PIXMAN_PRIVATE_H */