Make the fast_path implementation run the c_fast_paths
[profile/ivi/pixman.git] / pixman / pixman-general.c
1 /*
2  * Copyright © 2009 Red Hat, Inc.
3  * Copyright © 2000 SuSE, Inc.
4  * Copyright © 2007 Red Hat, Inc.
5  * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
6  *             2005 Lars Knoll & Zack Rusin, Trolltech
7  *             2008 Aaron Plattner, NVIDIA Corporation
8  *
9  * Permission to use, copy, modify, distribute, and sell this software and its
10  * documentation for any purpose is hereby granted without fee, provided that
11  * the above copyright notice appear in all copies and that both that
12  * copyright notice and this permission notice appear in supporting
13  * documentation, and that the name of Red Hat not be used in advertising or
14  * publicity pertaining to distribution of the software without specific,
15  * written prior permission.  Red Hat makes no representations about the
16  * suitability of this software for any purpose.  It is provided "as is"
17  * without express or implied warranty.
18  *
19  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
20  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
22  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
23  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
24  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
25  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
26  * SOFTWARE.
27  */
28 #include <config.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <math.h>
32 #include <assert.h>
33 #include <limits.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include "pixman-private.h"
38 #include "pixman-mmx.h"
39 #include "pixman-vmx.h"
40 #include "pixman-sse2.h"
41 #include "pixman-arm-simd.h"
42 #include "pixman-combine32.h"
43 #include "pixman-private.h"
44
45
46 static void
47 general_combine_32 (pixman_implementation_t *imp, pixman_op_t op,
48                     uint32_t *dest, const uint32_t *src, const uint32_t *mask,
49                     int width)
50 {
51     CombineFunc32 f = pixman_composeFunctions.combineU[op];
52
53     f (dest, src, mask, width);
54 }
55
56 static void
57 general_combine_32_ca (pixman_implementation_t *imp, pixman_op_t op,
58                        uint32_t *dest, const uint32_t *src, const uint32_t *mask,
59                        int width)
60 {
61     CombineFunc32 f = pixman_composeFunctions.combineC[op];
62
63     f (dest, src, mask, width);
64 }
65
66 static void
67 general_combine_64 (pixman_implementation_t *imp, pixman_op_t op,
68                     uint64_t *dest, const uint64_t *src, const uint64_t *mask,
69                     int width)
70 {
71     CombineFunc64 f = pixman_composeFunctions64.combineU[op];
72
73     f (dest, src, mask, width);
74 }
75
76 static void
77 general_combine_64_ca (pixman_implementation_t *imp, pixman_op_t op,
78                        uint64_t *dest, const uint64_t *src, const uint64_t *mask,
79                        int width)
80 {
81     CombineFunc64 f = pixman_composeFunctions64.combineC[op];
82
83     f (dest, src, mask, width);
84 }
85
86 static void
87 pixman_composite_rect_general_internal (pixman_implementation_t *imp,
88                                         const FbComposeData *data,
89                                         void *src_buffer, void *mask_buffer, 
90                                         void *dest_buffer, const int wide)
91 {
92     int i;
93     scanStoreProc store;
94     scanFetchProc fetchSrc = NULL, fetchMask = NULL, fetchDest = NULL;
95     uint32_t *bits;
96     int32_t stride;
97     source_pict_class_t srcClass, maskClass;
98     pixman_bool_t component_alpha;
99
100     srcClass = _pixman_image_classify (data->src,
101                                        data->xSrc, data->ySrc,
102                                        data->width, data->height);
103
104     maskClass = SOURCE_IMAGE_CLASS_UNKNOWN;
105     if (data->mask)
106     {
107         maskClass = _pixman_image_classify (data->mask,
108                                             data->xSrc, data->ySrc,
109                                             data->width, data->height);
110     }
111     
112     if (data->op == PIXMAN_OP_CLEAR)
113         fetchSrc = NULL;
114     else if (wide)
115         fetchSrc = _pixman_image_get_scanline_64;
116     else
117         fetchSrc = _pixman_image_get_scanline_32;
118
119     if (!data->mask || data->op == PIXMAN_OP_CLEAR)
120         fetchMask = NULL;
121     else if (wide)
122         fetchMask = _pixman_image_get_scanline_64;
123     else
124         fetchMask = _pixman_image_get_scanline_32;
125
126     if (data->op == PIXMAN_OP_CLEAR || data->op == PIXMAN_OP_SRC)
127         fetchDest = NULL;
128     else if (wide)
129         fetchDest = _pixman_image_get_scanline_64;
130     else
131         fetchDest = _pixman_image_get_scanline_32;
132
133     if (wide)
134         store = _pixman_image_store_scanline_64;
135     else
136         store = _pixman_image_store_scanline_32;
137
138     // Skip the store step and composite directly into the
139     // destination if the output format of the compose func matches
140     // the destination format.
141     if (!wide &&
142         !data->dest->common.alpha_map &&
143         !data->dest->common.write_func && 
144         (data->op == PIXMAN_OP_ADD || data->op == PIXMAN_OP_OVER) &&
145         (data->dest->bits.format == PIXMAN_a8r8g8b8 ||
146          data->dest->bits.format == PIXMAN_x8r8g8b8))
147     {
148         store = NULL;
149     }
150
151     if (!store)
152     {
153         bits = data->dest->bits.bits;
154         stride = data->dest->bits.rowstride;
155     }
156     else
157     {
158         bits = NULL;
159         stride = 0;
160     }
161
162     component_alpha =
163         fetchSrc                   &&
164         fetchMask                  &&
165         data->mask                 &&
166         data->mask->common.type == BITS &&
167         data->mask->common.component_alpha &&
168         PIXMAN_FORMAT_RGB (data->mask->bits.format);
169
170     {
171         pixman_combine_32_func_t compose;
172
173         if (wide)
174         {
175             if (component_alpha)
176                 compose = (pixman_combine_32_func_t)_pixman_implementation_combine_64_ca;
177             else
178                 compose = (pixman_combine_32_func_t)_pixman_implementation_combine_64;
179         }
180         else
181         {
182             if (component_alpha)
183                 compose = _pixman_implementation_combine_32_ca;
184             else
185                 compose = _pixman_implementation_combine_32;
186         }
187
188         if (!compose)
189             return;
190
191         if (!fetchMask)
192             mask_buffer = NULL;
193         
194         for (i = 0; i < data->height; ++i)
195         {
196             /* fill first half of scanline with source */
197             if (fetchSrc)
198             {
199                 if (fetchMask)
200                 {
201                     /* fetch mask before source so that fetching of
202                        source can be optimized */
203                     fetchMask (data->mask, data->xMask, data->yMask + i,
204                                data->width, mask_buffer, 0, 0);
205
206                     if (maskClass == SOURCE_IMAGE_CLASS_HORIZONTAL)
207                         fetchMask = NULL;
208                 }
209
210                 if (srcClass == SOURCE_IMAGE_CLASS_HORIZONTAL)
211                 {
212                     fetchSrc (data->src, data->xSrc, data->ySrc + i,
213                               data->width, src_buffer, 0, 0);
214                     fetchSrc = NULL;
215                 }
216                 else
217                 {
218                     fetchSrc (data->src, data->xSrc, data->ySrc + i,
219                               data->width, src_buffer, mask_buffer,
220                               0xffffffff);
221                 }
222             }
223             else if (fetchMask)
224             {
225                 fetchMask (data->mask, data->xMask, data->yMask + i,
226                            data->width, mask_buffer, 0, 0);
227             }
228
229             if (store)
230             {
231                 /* fill dest into second half of scanline */
232                 if (fetchDest)
233                     fetchDest (data->dest, data->xDest, data->yDest + i,
234                                data->width, dest_buffer, 0, 0);
235
236                 /* blend */
237                 compose (imp, data->op, dest_buffer, src_buffer, mask_buffer, data->width);
238
239                 /* write back */
240                 store (&(data->dest->bits), data->xDest, data->yDest + i, data->width,
241                        dest_buffer);
242             }
243             else
244             {
245                 /* blend */
246                 compose (imp, data->op, bits + (data->yDest + i) * stride +
247                          data->xDest,
248                          src_buffer, mask_buffer, data->width);
249             }
250         }
251     }
252 }
253
254 #define SCANLINE_BUFFER_LENGTH 8192
255
256 static void
257 general_composite_rect (pixman_implementation_t *imp,
258                         const FbComposeData *data)
259 {
260     uint8_t stack_scanline_buffer[SCANLINE_BUFFER_LENGTH * 3];
261     const pixman_format_code_t srcFormat =
262         data->src->type == BITS ? data->src->bits.format : 0;
263     const pixman_format_code_t maskFormat =
264         data->mask && data->mask->type == BITS ? data->mask->bits.format : 0;
265     const pixman_format_code_t destFormat = data->dest->type == BITS ? data->dest->bits.format : 0;
266     const int srcWide = PIXMAN_FORMAT_16BPC(srcFormat);
267     const int maskWide = data->mask && PIXMAN_FORMAT_16BPC(maskFormat);
268     const int destWide = PIXMAN_FORMAT_16BPC(destFormat);
269     const int wide = srcWide || maskWide || destWide;
270     const int Bpp = wide ? 8 : 4;
271     uint8_t *scanline_buffer = stack_scanline_buffer;
272     uint8_t *src_buffer, *mask_buffer, *dest_buffer;
273     
274     if (data->width * Bpp > SCANLINE_BUFFER_LENGTH)
275     {
276         scanline_buffer = pixman_malloc_abc (data->width, 3, Bpp);
277
278         if (!scanline_buffer)
279             return;
280     }
281
282     src_buffer = scanline_buffer;
283     mask_buffer = src_buffer + data->width * Bpp;
284     dest_buffer = mask_buffer + data->width * Bpp;
285
286     pixman_composite_rect_general_internal (imp, data, src_buffer,
287                                             mask_buffer, dest_buffer,
288                                             wide);
289
290     if (scanline_buffer != stack_scanline_buffer)
291         free (scanline_buffer);
292 }
293
294 static void
295 pixman_image_composite_rect  (pixman_implementation_t *imp,
296                               pixman_op_t                   op,
297                               pixman_image_t               *src,
298                               pixman_image_t               *mask,
299                               pixman_image_t               *dest,
300                               int32_t                       src_x,
301                               int32_t                       src_y,
302                               int32_t                       mask_x,
303                               int32_t                       mask_y,
304                               int32_t                       dest_x,
305                               int32_t                       dest_y,
306                               int32_t                      width,
307                               int32_t                      height)
308 {
309     FbComposeData compose_data;
310
311     return_if_fail (src != NULL);
312     return_if_fail (dest != NULL);
313
314     compose_data.op = op;
315     compose_data.src = src;
316     compose_data.mask = mask;
317     compose_data.dest = dest;
318     compose_data.xSrc = src_x;
319     compose_data.ySrc = src_y;
320     compose_data.xMask = mask_x;
321     compose_data.yMask = mask_y;
322     compose_data.xDest = dest_x;
323     compose_data.yDest = dest_y;
324     compose_data.width = width;
325     compose_data.height = height;
326
327     general_composite_rect (imp, &compose_data);
328 }
329
330 static void
331 general_composite (pixman_implementation_t *    imp,
332                    pixman_op_t                  op,
333                    pixman_image_t *             src,
334                    pixman_image_t *             mask,
335                    pixman_image_t *             dest,
336                    int32_t                      src_x,
337                    int32_t                      src_y,
338                    int32_t                      mask_x,
339                    int32_t                      mask_y,
340                    int32_t                      dest_x,
341                    int32_t                      dest_y,
342                    int32_t                      width,
343                    int32_t                      height)
344 {
345     pixman_bool_t srcRepeat = src->type == BITS && src->common.repeat == PIXMAN_REPEAT_NORMAL;
346     pixman_bool_t maskRepeat = FALSE;
347     pixman_bool_t srcTransform = src->common.transform != NULL;
348     pixman_bool_t maskTransform = FALSE;
349
350 #ifdef USE_VMX
351     fbComposeSetupVMX();
352 #endif
353
354     if (srcRepeat && srcTransform &&
355         src->bits.width == 1 &&
356         src->bits.height == 1)
357     {
358         srcTransform = FALSE;
359     }
360
361     if (mask && mask->type == BITS)
362     {
363         maskRepeat = mask->common.repeat == PIXMAN_REPEAT_NORMAL;
364
365         maskTransform = mask->common.transform != 0;
366         if (mask->common.filter == PIXMAN_FILTER_CONVOLUTION)
367             maskTransform = TRUE;
368
369         if (maskRepeat && maskTransform &&
370             mask->bits.width == 1 &&
371             mask->bits.height == 1)
372         {
373             maskTransform = FALSE;
374         }
375     }
376     
377 #ifdef USE_VMX
378     if (_pixman_run_fast_path (vmx_fast_paths, imp,
379                                op, src, mask, dest,
380                                src_x, src_y,
381                                mask_x, mask_y,
382                                dest_x, dest_y,
383                                width, height))
384         return;
385 #endif
386
387 #ifdef USE_ARM_NEON
388     if (pixman_have_arm_neon() && _pixman_run_fast_path (arm_neon_fast_paths, imp,
389                                                          op, src, mask, dest,
390                                                          src_x, src_y,
391                                                          mask_x, mask_y,
392                                                          dest_x, dest_y,
393                                                          width, height))
394         return;
395 #endif
396
397 #ifdef USE_ARM_SIMD
398     if (pixman_have_arm_simd() && _pixman_run_fast_path (arm_simd_fast_paths, imp,
399                                                          op, src, mask, dest,
400                                                          src_x, src_y,
401                                                          mask_x, mask_y,
402                                                          dest_x, dest_y,
403                                                          width, height))
404         return;
405 #endif
406
407     /* CompositeGeneral optimizes 1x1 repeating images itself */
408     if (src->type == BITS &&
409         src->bits.width == 1 && src->bits.height == 1)
410     {
411         srcRepeat = FALSE;
412     }
413     
414     if (mask && mask->type == BITS &&
415         mask->bits.width == 1 && mask->bits.height == 1)
416     {
417         maskRepeat = FALSE;
418     }
419     
420     /* if we are transforming, repeats are handled in fbFetchTransformed */
421     if (srcTransform)
422         srcRepeat = FALSE;
423     
424     if (maskTransform)
425         maskRepeat = FALSE;
426
427     _pixman_walk_composite_region (imp, op, src, mask, dest, src_x, src_y,
428                                    mask_x, mask_y, dest_x, dest_y, width, height,
429                                    srcRepeat, maskRepeat, pixman_image_composite_rect);
430 }
431
432 static pixman_bool_t
433 general_blt (pixman_implementation_t *imp,
434              uint32_t *src_bits,
435              uint32_t *dst_bits,
436              int src_stride,
437              int dst_stride,
438              int src_bpp,
439              int dst_bpp,
440              int src_x, int src_y,
441              int dst_x, int dst_y,
442              int width, int height)
443 {
444     /* We can't blit unless we have sse2 or mmx */
445     
446     return FALSE;
447 }
448
449 static pixman_bool_t
450 general_fill (pixman_implementation_t *imp,
451               uint32_t *bits,
452               int stride,
453               int bpp,
454               int x,
455               int y,
456               int width,
457               int height,
458               uint32_t xor)
459 {
460     return FALSE;
461 }
462
463 pixman_implementation_t *
464 _pixman_implementation_create_general (pixman_implementation_t *toplevel)
465 {
466     pixman_implementation_t *imp = _pixman_implementation_create (toplevel, NULL);
467     int i;
468
469     for (i = 0; i < PIXMAN_OP_LAST; ++i)
470     {
471         imp->combine_32[i] = general_combine_32;
472         imp->combine_32_ca[i] = general_combine_32_ca;
473         imp->combine_64[i] = general_combine_64;
474         imp->combine_64_ca[i] = general_combine_64_ca;
475     }
476     imp->composite = general_composite;
477     imp->blt = general_blt;
478     imp->fill = general_fill;
479     
480     return imp;
481 }