Add qemu 2.4.0
[kvmfornfv.git] / qemu / pixman / pixman / pixman-private.h
1 #include <float.h>
2
3 #ifndef PIXMAN_PRIVATE_H
4 #define PIXMAN_PRIVATE_H
5
6 /*
7  * The defines which are shared between C and assembly code
8  */
9
10 /* bilinear interpolation precision (must be <= 8) */
11 #define BILINEAR_INTERPOLATION_BITS 7
12 #define BILINEAR_INTERPOLATION_RANGE (1 << BILINEAR_INTERPOLATION_BITS)
13
14 /*
15  * C specific part
16  */
17
18 #ifndef __ASSEMBLER__
19
20 #ifndef PACKAGE
21 #  error config.h must be included before pixman-private.h
22 #endif
23
24 #define PIXMAN_DISABLE_DEPRECATED
25 #define PIXMAN_USE_INTERNAL_API
26
27 #include "pixman.h"
28 #include <time.h>
29 #include <assert.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stddef.h>
33
34 #include "pixman-compiler.h"
35
36 /*
37  * Images
38  */
39 typedef struct image_common image_common_t;
40 typedef struct solid_fill solid_fill_t;
41 typedef struct gradient gradient_t;
42 typedef struct linear_gradient linear_gradient_t;
43 typedef struct horizontal_gradient horizontal_gradient_t;
44 typedef struct vertical_gradient vertical_gradient_t;
45 typedef struct conical_gradient conical_gradient_t;
46 typedef struct radial_gradient radial_gradient_t;
47 typedef struct bits_image bits_image_t;
48 typedef struct circle circle_t;
49
50 typedef struct argb_t argb_t;
51
52 struct argb_t
53 {
54     float a;
55     float r;
56     float g;
57     float b;
58 };
59
60 typedef void (*fetch_scanline_t) (bits_image_t   *image,
61                                   int             x,
62                                   int             y,
63                                   int             width,
64                                   uint32_t       *buffer,
65                                   const uint32_t *mask);
66
67 typedef uint32_t (*fetch_pixel_32_t) (bits_image_t *image,
68                                       int           x,
69                                       int           y);
70
71 typedef argb_t (*fetch_pixel_float_t) (bits_image_t *image,
72                                        int           x,
73                                        int           y);
74
75 typedef void (*store_scanline_t) (bits_image_t *  image,
76                                   int             x,
77                                   int             y,
78                                   int             width,
79                                   const uint32_t *values);
80
81 typedef enum
82 {
83     BITS,
84     LINEAR,
85     CONICAL,
86     RADIAL,
87     SOLID
88 } image_type_t;
89
90 typedef void (*property_changed_func_t) (pixman_image_t *image);
91
92 struct image_common
93 {
94     image_type_t                type;
95     int32_t                     ref_count;
96     pixman_region32_t           clip_region;
97     int32_t                     alpha_count;        /* How many times this image is being used as an alpha map */
98     pixman_bool_t               have_clip_region;   /* FALSE if there is no clip */
99     pixman_bool_t               client_clip;        /* Whether the source clip was
100                                                        set by a client */
101     pixman_bool_t               clip_sources;       /* Whether the clip applies when
102                                                      * the image is used as a source
103                                                      */
104     pixman_bool_t               dirty;
105     pixman_transform_t *        transform;
106     pixman_repeat_t             repeat;
107     pixman_filter_t             filter;
108     pixman_fixed_t *            filter_params;
109     int                         n_filter_params;
110     bits_image_t *              alpha_map;
111     int                         alpha_origin_x;
112     int                         alpha_origin_y;
113     pixman_bool_t               component_alpha;
114     property_changed_func_t     property_changed;
115
116     pixman_image_destroy_func_t destroy_func;
117     void *                      destroy_data;
118
119     uint32_t                    flags;
120     pixman_format_code_t        extended_format_code;
121 };
122
123 struct solid_fill
124 {
125     image_common_t common;
126     pixman_color_t color;
127
128     uint32_t       color_32;
129     argb_t         color_float;
130 };
131
132 struct gradient
133 {
134     image_common_t          common;
135     int                     n_stops;
136     pixman_gradient_stop_t *stops;
137 };
138
139 struct linear_gradient
140 {
141     gradient_t           common;
142     pixman_point_fixed_t p1;
143     pixman_point_fixed_t p2;
144 };
145
146 struct circle
147 {
148     pixman_fixed_t x;
149     pixman_fixed_t y;
150     pixman_fixed_t radius;
151 };
152
153 struct radial_gradient
154 {
155     gradient_t common;
156
157     circle_t   c1;
158     circle_t   c2;
159
160     circle_t   delta;
161     double     a;
162     double     inva;
163     double     mindr;
164 };
165
166 struct conical_gradient
167 {
168     gradient_t           common;
169     pixman_point_fixed_t center;
170     double               angle;
171 };
172
173 struct bits_image
174 {
175     image_common_t             common;
176     pixman_format_code_t       format;
177     const pixman_indexed_t *   indexed;
178     int                        width;
179     int                        height;
180     uint32_t *                 bits;
181     uint32_t *                 free_me;
182     int                        rowstride;  /* in number of uint32_t's */
183
184     fetch_scanline_t           fetch_scanline_32;
185     fetch_pixel_32_t           fetch_pixel_32;
186     store_scanline_t           store_scanline_32;
187
188     fetch_scanline_t           fetch_scanline_float;
189     fetch_pixel_float_t        fetch_pixel_float;
190     store_scanline_t           store_scanline_float;
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     gradient_t         gradient;
203     linear_gradient_t  linear;
204     conical_gradient_t conical;
205     radial_gradient_t  radial;
206     solid_fill_t       solid;
207 };
208
209 typedef struct pixman_iter_t pixman_iter_t;
210 typedef uint32_t *(* pixman_iter_get_scanline_t) (pixman_iter_t *iter, const uint32_t *mask);
211 typedef void      (* pixman_iter_write_back_t)   (pixman_iter_t *iter);
212 typedef void      (* pixman_iter_fini_t)         (pixman_iter_t *iter);
213
214 typedef enum
215 {
216     ITER_NARROW =               (1 << 0),
217     ITER_WIDE =                 (1 << 1),
218
219     /* "Localized alpha" is when the alpha channel is used only to compute
220      * the alpha value of the destination. This means that the computation
221      * of the RGB values of the result is independent of the alpha value.
222      *
223      * For example, the OVER operator has localized alpha for the
224      * destination, because the RGB values of the result can be computed
225      * without knowing the destination alpha. Similarly, ADD has localized
226      * alpha for both source and destination because the RGB values of the
227      * result can be computed without knowing the alpha value of source or
228      * destination.
229      *
230      * When he destination is xRGB, this is useful knowledge, because then
231      * we can treat it as if it were ARGB, which means in some cases we can
232      * avoid copying it to a temporary buffer.
233      */
234     ITER_LOCALIZED_ALPHA =      (1 << 2),
235     ITER_IGNORE_ALPHA =         (1 << 3),
236     ITER_IGNORE_RGB =           (1 << 4),
237
238     /* These indicate whether the iterator is for a source
239      * or a destination image
240      */
241     ITER_SRC =                  (1 << 5),
242     ITER_DEST =                 (1 << 6)
243 } iter_flags_t;
244
245 struct pixman_iter_t
246 {
247     /* These are initialized by _pixman_implementation_{src,dest}_init */
248     pixman_image_t *            image;
249     uint32_t *                  buffer;
250     int                         x, y;
251     int                         width;
252     int                         height;
253     iter_flags_t                iter_flags;
254     uint32_t                    image_flags;
255
256     /* These function pointers are initialized by the implementation */
257     pixman_iter_get_scanline_t  get_scanline;
258     pixman_iter_write_back_t    write_back;
259     pixman_iter_fini_t          fini;
260
261     /* These fields are scratch data that implementations can use */
262     void *                      data;
263     uint8_t *                   bits;
264     int                         stride;
265 };
266
267 typedef struct pixman_iter_info_t pixman_iter_info_t;
268 typedef void (* pixman_iter_initializer_t) (pixman_iter_t *iter,
269                                             const pixman_iter_info_t *info);
270 struct pixman_iter_info_t
271 {
272     pixman_format_code_t        format;
273     uint32_t                    image_flags;
274     iter_flags_t                iter_flags;
275     pixman_iter_initializer_t   initializer;
276     pixman_iter_get_scanline_t  get_scanline;
277     pixman_iter_write_back_t    write_back;
278 };
279
280 void
281 _pixman_bits_image_setup_accessors (bits_image_t *image);
282
283 void
284 _pixman_bits_image_src_iter_init (pixman_image_t *image, pixman_iter_t *iter);
285
286 void
287 _pixman_bits_image_dest_iter_init (pixman_image_t *image, pixman_iter_t *iter);
288
289 void
290 _pixman_linear_gradient_iter_init (pixman_image_t *image, pixman_iter_t  *iter);
291
292 void
293 _pixman_radial_gradient_iter_init (pixman_image_t *image, pixman_iter_t *iter);
294
295 void
296 _pixman_conical_gradient_iter_init (pixman_image_t *image, pixman_iter_t *iter);
297
298 void
299 _pixman_image_init (pixman_image_t *image);
300
301 pixman_bool_t
302 _pixman_bits_image_init (pixman_image_t *     image,
303                          pixman_format_code_t format,
304                          int                  width,
305                          int                  height,
306                          uint32_t *           bits,
307                          int                  rowstride,
308                          pixman_bool_t        clear);
309 pixman_bool_t
310 _pixman_image_fini (pixman_image_t *image);
311
312 pixman_image_t *
313 _pixman_image_allocate (void);
314
315 pixman_bool_t
316 _pixman_init_gradient (gradient_t *                  gradient,
317                        const pixman_gradient_stop_t *stops,
318                        int                           n_stops);
319 void
320 _pixman_image_reset_clip_region (pixman_image_t *image);
321
322 void
323 _pixman_image_validate (pixman_image_t *image);
324
325 #define PIXMAN_IMAGE_GET_LINE(image, x, y, type, out_stride, line, mul) \
326     do                                                                  \
327     {                                                                   \
328         uint32_t *__bits__;                                             \
329         int       __stride__;                                           \
330                                                                         \
331         __bits__ = image->bits.bits;                                    \
332         __stride__ = image->bits.rowstride;                             \
333         (out_stride) =                                                  \
334             __stride__ * (int) sizeof (uint32_t) / (int) sizeof (type); \
335         (line) =                                                        \
336             ((type *) __bits__) + (out_stride) * (y) + (mul) * (x);     \
337     } while (0)
338
339 /*
340  * Gradient walker
341  */
342 typedef struct
343 {
344     float                   a_s, a_b;
345     float                   r_s, r_b;
346     float                   g_s, g_b;
347     float                   b_s, b_b;
348     pixman_fixed_t          left_x;
349     pixman_fixed_t          right_x;
350
351     pixman_gradient_stop_t *stops;
352     int                     num_stops;
353     pixman_repeat_t         repeat;
354
355     pixman_bool_t           need_reset;
356 } pixman_gradient_walker_t;
357
358 void
359 _pixman_gradient_walker_init (pixman_gradient_walker_t *walker,
360                               gradient_t *              gradient,
361                               pixman_repeat_t           repeat);
362
363 void
364 _pixman_gradient_walker_reset (pixman_gradient_walker_t *walker,
365                                pixman_fixed_48_16_t      pos);
366
367 uint32_t
368 _pixman_gradient_walker_pixel (pixman_gradient_walker_t *walker,
369                                pixman_fixed_48_16_t      x);
370
371 /*
372  * Edges
373  */
374
375 #define MAX_ALPHA(n)    ((1 << (n)) - 1)
376 #define N_Y_FRAC(n)     ((n) == 1 ? 1 : (1 << ((n) / 2)) - 1)
377 #define N_X_FRAC(n)     ((n) == 1 ? 1 : (1 << ((n) / 2)) + 1)
378
379 #define STEP_Y_SMALL(n) (pixman_fixed_1 / N_Y_FRAC (n))
380 #define STEP_Y_BIG(n)   (pixman_fixed_1 - (N_Y_FRAC (n) - 1) * STEP_Y_SMALL (n))
381
382 #define Y_FRAC_FIRST(n) (STEP_Y_BIG (n) / 2)
383 #define Y_FRAC_LAST(n)  (Y_FRAC_FIRST (n) + (N_Y_FRAC (n) - 1) * STEP_Y_SMALL (n))
384
385 #define STEP_X_SMALL(n) (pixman_fixed_1 / N_X_FRAC (n))
386 #define STEP_X_BIG(n)   (pixman_fixed_1 - (N_X_FRAC (n) - 1) * STEP_X_SMALL (n))
387
388 #define X_FRAC_FIRST(n) (STEP_X_BIG (n) / 2)
389 #define X_FRAC_LAST(n)  (X_FRAC_FIRST (n) + (N_X_FRAC (n) - 1) * STEP_X_SMALL (n))
390
391 #define RENDER_SAMPLES_X(x, n)                                          \
392     ((n) == 1? 0 : (pixman_fixed_frac (x) +                             \
393                     X_FRAC_FIRST (n)) / STEP_X_SMALL (n))
394
395 void
396 pixman_rasterize_edges_accessors (pixman_image_t *image,
397                                   pixman_edge_t * l,
398                                   pixman_edge_t * r,
399                                   pixman_fixed_t  t,
400                                   pixman_fixed_t  b);
401
402 /*
403  * Implementations
404  */
405 typedef struct pixman_implementation_t pixman_implementation_t;
406
407 typedef struct
408 {
409     pixman_op_t              op;
410     pixman_image_t *         src_image;
411     pixman_image_t *         mask_image;
412     pixman_image_t *         dest_image;
413     int32_t                  src_x;
414     int32_t                  src_y;
415     int32_t                  mask_x;
416     int32_t                  mask_y;
417     int32_t                  dest_x;
418     int32_t                  dest_y;
419     int32_t                  width;
420     int32_t                  height;
421
422     uint32_t                 src_flags;
423     uint32_t                 mask_flags;
424     uint32_t                 dest_flags;
425 } pixman_composite_info_t;
426
427 #define PIXMAN_COMPOSITE_ARGS(info)                                     \
428     MAYBE_UNUSED pixman_op_t        op = info->op;                      \
429     MAYBE_UNUSED pixman_image_t *   src_image = info->src_image;        \
430     MAYBE_UNUSED pixman_image_t *   mask_image = info->mask_image;      \
431     MAYBE_UNUSED pixman_image_t *   dest_image = info->dest_image;      \
432     MAYBE_UNUSED int32_t            src_x = info->src_x;                \
433     MAYBE_UNUSED int32_t            src_y = info->src_y;                \
434     MAYBE_UNUSED int32_t            mask_x = info->mask_x;              \
435     MAYBE_UNUSED int32_t            mask_y = info->mask_y;              \
436     MAYBE_UNUSED int32_t            dest_x = info->dest_x;              \
437     MAYBE_UNUSED int32_t            dest_y = info->dest_y;              \
438     MAYBE_UNUSED int32_t            width = info->width;                \
439     MAYBE_UNUSED int32_t            height = info->height
440
441 typedef void (*pixman_combine_32_func_t) (pixman_implementation_t *imp,
442                                           pixman_op_t              op,
443                                           uint32_t *               dest,
444                                           const uint32_t *         src,
445                                           const uint32_t *         mask,
446                                           int                      width);
447
448 typedef void (*pixman_combine_float_func_t) (pixman_implementation_t *imp,
449                                              pixman_op_t              op,
450                                              float *                  dest,
451                                              const float *            src,
452                                              const float *            mask,
453                                              int                      n_pixels);
454
455 typedef void (*pixman_composite_func_t) (pixman_implementation_t *imp,
456                                          pixman_composite_info_t *info);
457 typedef pixman_bool_t (*pixman_blt_func_t) (pixman_implementation_t *imp,
458                                             uint32_t *               src_bits,
459                                             uint32_t *               dst_bits,
460                                             int                      src_stride,
461                                             int                      dst_stride,
462                                             int                      src_bpp,
463                                             int                      dst_bpp,
464                                             int                      src_x,
465                                             int                      src_y,
466                                             int                      dest_x,
467                                             int                      dest_y,
468                                             int                      width,
469                                             int                      height);
470 typedef pixman_bool_t (*pixman_fill_func_t) (pixman_implementation_t *imp,
471                                              uint32_t *               bits,
472                                              int                      stride,
473                                              int                      bpp,
474                                              int                      x,
475                                              int                      y,
476                                              int                      width,
477                                              int                      height,
478                                              uint32_t                 filler);
479
480 void _pixman_setup_combiner_functions_32 (pixman_implementation_t *imp);
481 void _pixman_setup_combiner_functions_float (pixman_implementation_t *imp);
482
483 typedef struct
484 {
485     pixman_op_t             op;
486     pixman_format_code_t    src_format;
487     uint32_t                src_flags;
488     pixman_format_code_t    mask_format;
489     uint32_t                mask_flags;
490     pixman_format_code_t    dest_format;
491     uint32_t                dest_flags;
492     pixman_composite_func_t func;
493 } pixman_fast_path_t;
494
495 struct pixman_implementation_t
496 {
497     pixman_implementation_t *   toplevel;
498     pixman_implementation_t *   fallback;
499     const pixman_fast_path_t *  fast_paths;
500     const pixman_iter_info_t *  iter_info;
501
502     pixman_blt_func_t           blt;
503     pixman_fill_func_t          fill;
504
505     pixman_combine_32_func_t    combine_32[PIXMAN_N_OPERATORS];
506     pixman_combine_32_func_t    combine_32_ca[PIXMAN_N_OPERATORS];
507     pixman_combine_float_func_t combine_float[PIXMAN_N_OPERATORS];
508     pixman_combine_float_func_t combine_float_ca[PIXMAN_N_OPERATORS];
509 };
510
511 uint32_t
512 _pixman_image_get_solid (pixman_implementation_t *imp,
513                          pixman_image_t *         image,
514                          pixman_format_code_t     format);
515
516 pixman_implementation_t *
517 _pixman_implementation_create (pixman_implementation_t *fallback,
518                                const pixman_fast_path_t *fast_paths);
519
520 void
521 _pixman_implementation_lookup_composite (pixman_implementation_t  *toplevel,
522                                          pixman_op_t               op,
523                                          pixman_format_code_t      src_format,
524                                          uint32_t                  src_flags,
525                                          pixman_format_code_t      mask_format,
526                                          uint32_t                  mask_flags,
527                                          pixman_format_code_t      dest_format,
528                                          uint32_t                  dest_flags,
529                                          pixman_implementation_t **out_imp,
530                                          pixman_composite_func_t  *out_func);
531
532 pixman_combine_32_func_t
533 _pixman_implementation_lookup_combiner (pixman_implementation_t *imp,
534                                         pixman_op_t              op,
535                                         pixman_bool_t            component_alpha,
536                                         pixman_bool_t            wide);
537
538 pixman_bool_t
539 _pixman_implementation_blt (pixman_implementation_t *imp,
540                             uint32_t *               src_bits,
541                             uint32_t *               dst_bits,
542                             int                      src_stride,
543                             int                      dst_stride,
544                             int                      src_bpp,
545                             int                      dst_bpp,
546                             int                      src_x,
547                             int                      src_y,
548                             int                      dest_x,
549                             int                      dest_y,
550                             int                      width,
551                             int                      height);
552
553 pixman_bool_t
554 _pixman_implementation_fill (pixman_implementation_t *imp,
555                              uint32_t *               bits,
556                              int                      stride,
557                              int                      bpp,
558                              int                      x,
559                              int                      y,
560                              int                      width,
561                              int                      height,
562                              uint32_t                 filler);
563
564 void
565 _pixman_implementation_iter_init (pixman_implementation_t       *imp,
566                                   pixman_iter_t                 *iter,
567                                   pixman_image_t                *image,
568                                   int                            x,
569                                   int                            y,
570                                   int                            width,
571                                   int                            height,
572                                   uint8_t                       *buffer,
573                                   iter_flags_t                   flags,
574                                   uint32_t                       image_flags);
575
576 /* Specific implementations */
577 pixman_implementation_t *
578 _pixman_implementation_create_general (void);
579
580 pixman_implementation_t *
581 _pixman_implementation_create_fast_path (pixman_implementation_t *fallback);
582
583 pixman_implementation_t *
584 _pixman_implementation_create_noop (pixman_implementation_t *fallback);
585
586 #if defined USE_X86_MMX || defined USE_ARM_IWMMXT || defined USE_LOONGSON_MMI
587 pixman_implementation_t *
588 _pixman_implementation_create_mmx (pixman_implementation_t *fallback);
589 #endif
590
591 #ifdef USE_SSE2
592 pixman_implementation_t *
593 _pixman_implementation_create_sse2 (pixman_implementation_t *fallback);
594 #endif
595
596 #ifdef USE_SSSE3
597 pixman_implementation_t *
598 _pixman_implementation_create_ssse3 (pixman_implementation_t *fallback);
599 #endif
600
601 #ifdef USE_ARM_SIMD
602 pixman_implementation_t *
603 _pixman_implementation_create_arm_simd (pixman_implementation_t *fallback);
604 #endif
605
606 #ifdef USE_ARM_NEON
607 pixman_implementation_t *
608 _pixman_implementation_create_arm_neon (pixman_implementation_t *fallback);
609 #endif
610
611 #ifdef USE_MIPS_DSPR2
612 pixman_implementation_t *
613 _pixman_implementation_create_mips_dspr2 (pixman_implementation_t *fallback);
614 #endif
615
616 #ifdef USE_VMX
617 pixman_implementation_t *
618 _pixman_implementation_create_vmx (pixman_implementation_t *fallback);
619 #endif
620
621 pixman_bool_t
622 _pixman_implementation_disabled (const char *name);
623
624 pixman_implementation_t *
625 _pixman_x86_get_implementations (pixman_implementation_t *imp);
626
627 pixman_implementation_t *
628 _pixman_arm_get_implementations (pixman_implementation_t *imp);
629
630 pixman_implementation_t *
631 _pixman_ppc_get_implementations (pixman_implementation_t *imp);
632
633 pixman_implementation_t *
634 _pixman_mips_get_implementations (pixman_implementation_t *imp);
635
636 pixman_implementation_t *
637 _pixman_choose_implementation (void);
638
639 pixman_bool_t
640 _pixman_disabled (const char *name);
641
642
643 /*
644  * Utilities
645  */
646 pixman_bool_t
647 _pixman_compute_composite_region32 (pixman_region32_t * region,
648                                     pixman_image_t *    src_image,
649                                     pixman_image_t *    mask_image,
650                                     pixman_image_t *    dest_image,
651                                     int32_t             src_x,
652                                     int32_t             src_y,
653                                     int32_t             mask_x,
654                                     int32_t             mask_y,
655                                     int32_t             dest_x,
656                                     int32_t             dest_y,
657                                     int32_t             width,
658                                     int32_t             height);
659 uint32_t *
660 _pixman_iter_get_scanline_noop (pixman_iter_t *iter, const uint32_t *mask);
661
662 void
663 _pixman_iter_init_bits_stride (pixman_iter_t *iter, const pixman_iter_info_t *info);
664
665 /* These "formats" all have depth 0, so they
666  * will never clash with any real ones
667  */
668 #define PIXMAN_null             PIXMAN_FORMAT (0, 0, 0, 0, 0, 0)
669 #define PIXMAN_solid            PIXMAN_FORMAT (0, 1, 0, 0, 0, 0)
670 #define PIXMAN_pixbuf           PIXMAN_FORMAT (0, 2, 0, 0, 0, 0)
671 #define PIXMAN_rpixbuf          PIXMAN_FORMAT (0, 3, 0, 0, 0, 0)
672 #define PIXMAN_unknown          PIXMAN_FORMAT (0, 4, 0, 0, 0, 0)
673 #define PIXMAN_any              PIXMAN_FORMAT (0, 5, 0, 0, 0, 0)
674
675 #define PIXMAN_OP_any           (PIXMAN_N_OPERATORS + 1)
676
677 #define FAST_PATH_ID_TRANSFORM                  (1 <<  0)
678 #define FAST_PATH_NO_ALPHA_MAP                  (1 <<  1)
679 #define FAST_PATH_NO_CONVOLUTION_FILTER         (1 <<  2)
680 #define FAST_PATH_NO_PAD_REPEAT                 (1 <<  3)
681 #define FAST_PATH_NO_REFLECT_REPEAT             (1 <<  4)
682 #define FAST_PATH_NO_ACCESSORS                  (1 <<  5)
683 #define FAST_PATH_NARROW_FORMAT                 (1 <<  6)
684 #define FAST_PATH_COMPONENT_ALPHA               (1 <<  8)
685 #define FAST_PATH_SAMPLES_OPAQUE                (1 <<  7)
686 #define FAST_PATH_UNIFIED_ALPHA                 (1 <<  9)
687 #define FAST_PATH_SCALE_TRANSFORM               (1 << 10)
688 #define FAST_PATH_NEAREST_FILTER                (1 << 11)
689 #define FAST_PATH_HAS_TRANSFORM                 (1 << 12)
690 #define FAST_PATH_IS_OPAQUE                     (1 << 13)
691 #define FAST_PATH_NO_NORMAL_REPEAT              (1 << 14)
692 #define FAST_PATH_NO_NONE_REPEAT                (1 << 15)
693 #define FAST_PATH_X_UNIT_POSITIVE               (1 << 16)
694 #define FAST_PATH_AFFINE_TRANSFORM              (1 << 17)
695 #define FAST_PATH_Y_UNIT_ZERO                   (1 << 18)
696 #define FAST_PATH_BILINEAR_FILTER               (1 << 19)
697 #define FAST_PATH_ROTATE_90_TRANSFORM           (1 << 20)
698 #define FAST_PATH_ROTATE_180_TRANSFORM          (1 << 21)
699 #define FAST_PATH_ROTATE_270_TRANSFORM          (1 << 22)
700 #define FAST_PATH_SAMPLES_COVER_CLIP_NEAREST    (1 << 23)
701 #define FAST_PATH_SAMPLES_COVER_CLIP_BILINEAR   (1 << 24)
702 #define FAST_PATH_BITS_IMAGE                    (1 << 25)
703 #define FAST_PATH_SEPARABLE_CONVOLUTION_FILTER  (1 << 26)
704
705 #define FAST_PATH_PAD_REPEAT                                            \
706     (FAST_PATH_NO_NONE_REPEAT           |                               \
707      FAST_PATH_NO_NORMAL_REPEAT         |                               \
708      FAST_PATH_NO_REFLECT_REPEAT)
709
710 #define FAST_PATH_NORMAL_REPEAT                                         \
711     (FAST_PATH_NO_NONE_REPEAT           |                               \
712      FAST_PATH_NO_PAD_REPEAT            |                               \
713      FAST_PATH_NO_REFLECT_REPEAT)
714
715 #define FAST_PATH_NONE_REPEAT                                           \
716     (FAST_PATH_NO_NORMAL_REPEAT         |                               \
717      FAST_PATH_NO_PAD_REPEAT            |                               \
718      FAST_PATH_NO_REFLECT_REPEAT)
719
720 #define FAST_PATH_REFLECT_REPEAT                                        \
721     (FAST_PATH_NO_NONE_REPEAT           |                               \
722      FAST_PATH_NO_NORMAL_REPEAT         |                               \
723      FAST_PATH_NO_PAD_REPEAT)
724
725 #define FAST_PATH_STANDARD_FLAGS                                        \
726     (FAST_PATH_NO_CONVOLUTION_FILTER    |                               \
727      FAST_PATH_NO_ACCESSORS             |                               \
728      FAST_PATH_NO_ALPHA_MAP             |                               \
729      FAST_PATH_NARROW_FORMAT)
730
731 #define FAST_PATH_STD_DEST_FLAGS                                        \
732     (FAST_PATH_NO_ACCESSORS             |                               \
733      FAST_PATH_NO_ALPHA_MAP             |                               \
734      FAST_PATH_NARROW_FORMAT)
735
736 #define SOURCE_FLAGS(format)                                            \
737     (FAST_PATH_STANDARD_FLAGS |                                         \
738      ((PIXMAN_ ## format == PIXMAN_solid) ?                             \
739       0 : (FAST_PATH_SAMPLES_COVER_CLIP_NEAREST | FAST_PATH_NEAREST_FILTER | FAST_PATH_ID_TRANSFORM)))
740
741 #define MASK_FLAGS(format, extra)                                       \
742     ((PIXMAN_ ## format == PIXMAN_null) ? 0 : (SOURCE_FLAGS (format) | extra))
743
744 #define FAST_PATH(op, src, src_flags, mask, mask_flags, dest, dest_flags, func) \
745     PIXMAN_OP_ ## op,                                                   \
746     PIXMAN_ ## src,                                                     \
747     src_flags,                                                          \
748     PIXMAN_ ## mask,                                                    \
749     mask_flags,                                                         \
750     PIXMAN_ ## dest,                                                    \
751     dest_flags,                                                         \
752     func
753
754 #define PIXMAN_STD_FAST_PATH(op, src, mask, dest, func)                 \
755     { FAST_PATH (                                                       \
756             op,                                                         \
757             src,  SOURCE_FLAGS (src),                                   \
758             mask, MASK_FLAGS (mask, FAST_PATH_UNIFIED_ALPHA),           \
759             dest, FAST_PATH_STD_DEST_FLAGS,                             \
760             func) }
761
762 #define PIXMAN_STD_FAST_PATH_CA(op, src, mask, dest, func)              \
763     { FAST_PATH (                                                       \
764             op,                                                         \
765             src,  SOURCE_FLAGS (src),                                   \
766             mask, MASK_FLAGS (mask, FAST_PATH_COMPONENT_ALPHA),         \
767             dest, FAST_PATH_STD_DEST_FLAGS,                             \
768             func) }
769
770 extern pixman_implementation_t *global_implementation;
771
772 static force_inline pixman_implementation_t *
773 get_implementation (void)
774 {
775 #ifndef TOOLCHAIN_SUPPORTS_ATTRIBUTE_CONSTRUCTOR
776     if (!global_implementation)
777         global_implementation = _pixman_choose_implementation ();
778 #endif
779     return global_implementation;
780 }
781
782 /* This function is exported for the sake of the test suite and not part
783  * of the ABI.
784  */
785 PIXMAN_EXPORT pixman_implementation_t *
786 _pixman_internal_only_get_implementation (void);
787
788 /* Memory allocation helpers */
789 void *
790 pixman_malloc_ab (unsigned int n, unsigned int b);
791
792 void *
793 pixman_malloc_abc (unsigned int a, unsigned int b, unsigned int c);
794
795 void *
796 pixman_malloc_ab_plus_c (unsigned int a, unsigned int b, unsigned int c);
797
798 pixman_bool_t
799 _pixman_multiply_overflows_size (size_t a, size_t b);
800
801 pixman_bool_t
802 _pixman_multiply_overflows_int (unsigned int a, unsigned int b);
803
804 pixman_bool_t
805 _pixman_addition_overflows_int (unsigned int a, unsigned int b);
806
807 /* Compositing utilities */
808 void
809 pixman_expand_to_float (argb_t               *dst,
810                         const uint32_t       *src,
811                         pixman_format_code_t  format,
812                         int                   width);
813
814 void
815 pixman_contract_from_float (uint32_t     *dst,
816                             const argb_t *src,
817                             int           width);
818
819 /* Region Helpers */
820 pixman_bool_t
821 pixman_region32_copy_from_region16 (pixman_region32_t *dst,
822                                     pixman_region16_t *src);
823
824 pixman_bool_t
825 pixman_region16_copy_from_region32 (pixman_region16_t *dst,
826                                     pixman_region32_t *src);
827
828 /* Doubly linked lists */
829 typedef struct pixman_link_t pixman_link_t;
830 struct pixman_link_t
831 {
832     pixman_link_t *next;
833     pixman_link_t *prev;
834 };
835
836 typedef struct pixman_list_t pixman_list_t;
837 struct pixman_list_t
838 {
839     pixman_link_t *head;
840     pixman_link_t *tail;
841 };
842
843 static force_inline void
844 pixman_list_init (pixman_list_t *list)
845 {
846     list->head = (pixman_link_t *)list;
847     list->tail = (pixman_link_t *)list;
848 }
849
850 static force_inline void
851 pixman_list_prepend (pixman_list_t *list, pixman_link_t *link)
852 {
853     link->next = list->head;
854     link->prev = (pixman_link_t *)list;
855     list->head->prev = link;
856     list->head = link;
857 }
858
859 static force_inline void
860 pixman_list_unlink (pixman_link_t *link)
861 {
862     link->prev->next = link->next;
863     link->next->prev = link->prev;
864 }
865
866 static force_inline void
867 pixman_list_move_to_front (pixman_list_t *list, pixman_link_t *link)
868 {
869     pixman_list_unlink (link);
870     pixman_list_prepend (list, link);
871 }
872
873 /* Misc macros */
874
875 #ifndef FALSE
876 #   define FALSE 0
877 #endif
878
879 #ifndef TRUE
880 #   define TRUE 1
881 #endif
882
883 #ifndef MIN
884 #  define MIN(a, b) ((a < b) ? a : b)
885 #endif
886
887 #ifndef MAX
888 #  define MAX(a, b) ((a > b) ? a : b)
889 #endif
890
891 /* Integer division that rounds towards -infinity */
892 #define DIV(a, b)                                          \
893     ((((a) < 0) == ((b) < 0)) ? (a) / (b) :                \
894      ((a) - (b) + 1 - (((b) < 0) << 1)) / (b))
895
896 /* Modulus that produces the remainder wrt. DIV */
897 #define MOD(a, b) ((a) < 0 ? ((b) - ((-(a) - 1) % (b))) - 1 : (a) % (b))
898
899 #define CLIP(v, low, high) ((v) < (low) ? (low) : ((v) > (high) ? (high) : (v)))
900
901 #define FLOAT_IS_ZERO(f)     (-FLT_MIN < (f) && (f) < FLT_MIN)
902
903 /* Conversion between 8888 and 0565 */
904
905 static force_inline uint16_t
906 convert_8888_to_0565 (uint32_t s)
907 {
908     /* The following code can be compiled into just 4 instructions on ARM */
909     uint32_t a, b;
910     a = (s >> 3) & 0x1F001F;
911     b = s & 0xFC00;
912     a |= a >> 5;
913     a |= b >> 5;
914     return (uint16_t)a;
915 }
916
917 static force_inline uint32_t
918 convert_0565_to_0888 (uint16_t s)
919 {
920     return (((((s) << 3) & 0xf8) | (((s) >> 2) & 0x7)) |
921             ((((s) << 5) & 0xfc00) | (((s) >> 1) & 0x300)) |
922             ((((s) << 8) & 0xf80000) | (((s) << 3) & 0x70000)));
923 }
924
925 static force_inline uint32_t
926 convert_0565_to_8888 (uint16_t s)
927 {
928     return convert_0565_to_0888 (s) | 0xff000000;
929 }
930
931 /* Trivial versions that are useful in macros */
932
933 static force_inline uint32_t
934 convert_8888_to_8888 (uint32_t s)
935 {
936     return s;
937 }
938
939 static force_inline uint32_t
940 convert_x888_to_8888 (uint32_t s)
941 {
942     return s | 0xff000000;
943 }
944
945 static force_inline uint16_t
946 convert_0565_to_0565 (uint16_t s)
947 {
948     return s;
949 }
950
951 #define PIXMAN_FORMAT_IS_WIDE(f)                                        \
952     (PIXMAN_FORMAT_A (f) > 8 ||                                         \
953      PIXMAN_FORMAT_R (f) > 8 ||                                         \
954      PIXMAN_FORMAT_G (f) > 8 ||                                         \
955      PIXMAN_FORMAT_B (f) > 8 ||                                         \
956      PIXMAN_FORMAT_TYPE (f) == PIXMAN_TYPE_ARGB_SRGB)
957
958 #ifdef WORDS_BIGENDIAN
959 #   define SCREEN_SHIFT_LEFT(x,n)       ((x) << (n))
960 #   define SCREEN_SHIFT_RIGHT(x,n)      ((x) >> (n))
961 #else
962 #   define SCREEN_SHIFT_LEFT(x,n)       ((x) >> (n))
963 #   define SCREEN_SHIFT_RIGHT(x,n)      ((x) << (n))
964 #endif
965
966 static force_inline uint32_t
967 unorm_to_unorm (uint32_t val, int from_bits, int to_bits)
968 {
969     uint32_t result;
970
971     if (from_bits == 0)
972         return 0;
973
974     /* Delete any extra bits */
975     val &= ((1 << from_bits) - 1);
976
977     if (from_bits >= to_bits)
978         return val >> (from_bits - to_bits);
979
980     /* Start out with the high bit of val in the high bit of result. */
981     result = val << (to_bits - from_bits);
982
983     /* Copy the bits in result, doubling the number of bits each time, until
984      * we fill all to_bits. Unrolled manually because from_bits and to_bits
985      * are usually known statically, so the compiler can turn all of this
986      * into a few shifts.
987      */
988 #define REPLICATE()                                                     \
989     do                                                                  \
990     {                                                                   \
991         if (from_bits < to_bits)                                        \
992         {                                                               \
993             result |= result >> from_bits;                              \
994                                                                         \
995             from_bits *= 2;                                             \
996         }                                                               \
997     }                                                                   \
998     while (0)
999
1000     REPLICATE();
1001     REPLICATE();
1002     REPLICATE();
1003     REPLICATE();
1004     REPLICATE();
1005
1006     return result;
1007 }
1008
1009 uint16_t pixman_float_to_unorm (float f, int n_bits);
1010 float pixman_unorm_to_float (uint16_t u, int n_bits);
1011
1012 /*
1013  * Various debugging code
1014  */
1015
1016 #undef DEBUG
1017
1018 #define COMPILE_TIME_ASSERT(x)                                          \
1019     do { typedef int compile_time_assertion [(x)?1:-1]; } while (0)
1020
1021 /* Turn on debugging depending on what type of release this is
1022  */
1023 #if (((PIXMAN_VERSION_MICRO % 2) == 0) && ((PIXMAN_VERSION_MINOR % 2) == 1))
1024
1025 /* Debugging gets turned on for development releases because these
1026  * are the things that end up in bleeding edge distributions such
1027  * as Rawhide etc.
1028  *
1029  * For performance reasons we don't turn it on for stable releases or
1030  * random git checkouts. (Random git checkouts are often used for
1031  * performance work).
1032  */
1033
1034 #    define DEBUG
1035
1036 #endif
1037
1038 void
1039 _pixman_log_error (const char *function, const char *message);
1040
1041 #define return_if_fail(expr)                                            \
1042     do                                                                  \
1043     {                                                                   \
1044         if (unlikely (!(expr)))                                         \
1045         {                                                               \
1046             _pixman_log_error (FUNC, "The expression " # expr " was false"); \
1047             return;                                                     \
1048         }                                                               \
1049     }                                                                   \
1050     while (0)
1051
1052 #define return_val_if_fail(expr, retval)                                \
1053     do                                                                  \
1054     {                                                                   \
1055         if (unlikely (!(expr)))                                         \
1056         {                                                               \
1057             _pixman_log_error (FUNC, "The expression " # expr " was false"); \
1058             return (retval);                                            \
1059         }                                                               \
1060     }                                                                   \
1061     while (0)
1062
1063 #define critical_if_fail(expr)                                          \
1064     do                                                                  \
1065     {                                                                   \
1066         if (unlikely (!(expr)))                                         \
1067             _pixman_log_error (FUNC, "The expression " # expr " was false"); \
1068     }                                                                   \
1069     while (0)
1070
1071 /*
1072  * Matrix
1073  */
1074
1075 typedef struct { pixman_fixed_48_16_t v[3]; } pixman_vector_48_16_t;
1076
1077 pixman_bool_t
1078 pixman_transform_point_31_16 (const pixman_transform_t    *t,
1079                               const pixman_vector_48_16_t *v,
1080                               pixman_vector_48_16_t       *result);
1081
1082 void
1083 pixman_transform_point_31_16_3d (const pixman_transform_t    *t,
1084                                  const pixman_vector_48_16_t *v,
1085                                  pixman_vector_48_16_t       *result);
1086
1087 void
1088 pixman_transform_point_31_16_affine (const pixman_transform_t    *t,
1089                                      const pixman_vector_48_16_t *v,
1090                                      pixman_vector_48_16_t       *result);
1091
1092 /*
1093  * Timers
1094  */
1095
1096 #ifdef PIXMAN_TIMERS
1097
1098 static inline uint64_t
1099 oil_profile_stamp_rdtsc (void)
1100 {
1101     uint32_t hi, lo;
1102
1103     __asm__ __volatile__ ("rdtsc\n" : "=a" (lo), "=d" (hi));
1104
1105     return lo | (((uint64_t)hi) << 32);
1106 }
1107
1108 #define OIL_STAMP oil_profile_stamp_rdtsc
1109
1110 typedef struct pixman_timer_t pixman_timer_t;
1111
1112 struct pixman_timer_t
1113 {
1114     int             initialized;
1115     const char *    name;
1116     uint64_t        n_times;
1117     uint64_t        total;
1118     pixman_timer_t *next;
1119 };
1120
1121 extern int timer_defined;
1122
1123 void pixman_timer_register (pixman_timer_t *timer);
1124
1125 #define TIMER_BEGIN(tname)                                              \
1126     {                                                                   \
1127         static pixman_timer_t timer ## tname;                           \
1128         uint64_t              begin ## tname;                           \
1129                                                                         \
1130         if (!timer ## tname.initialized)                                \
1131         {                                                               \
1132             timer ## tname.initialized = 1;                             \
1133             timer ## tname.name = # tname;                              \
1134             pixman_timer_register (&timer ## tname);                    \
1135         }                                                               \
1136                                                                         \
1137         timer ## tname.n_times++;                                       \
1138         begin ## tname = OIL_STAMP ();
1139
1140 #define TIMER_END(tname)                                                \
1141     timer ## tname.total += OIL_STAMP () - begin ## tname;              \
1142     }
1143
1144 #else
1145
1146 #define TIMER_BEGIN(tname)
1147 #define TIMER_END(tname)
1148
1149 #endif /* PIXMAN_TIMERS */
1150
1151 #endif /* __ASSEMBLER__ */
1152
1153 #endif /* PIXMAN_PRIVATE_H */