summaryrefslogtreecommitdiff
path: root/pixman/pixman-general.c
blob: a1072fd72096380a27ae25728d8b146084832aab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
 * Copyright © 2009 Red Hat, Inc.
 * Copyright © 2000 SuSE, Inc.
 * Copyright © 2007 Red Hat, Inc.
 * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
 *             2005 Lars Knoll & Zack Rusin, Trolltech
 *             2008 Aaron Plattner, NVIDIA Corporation
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Red Hat not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  Red Hat makes no representations about the
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pixman-private.h"
#include "pixman-color-space-private.h"
#include "pixman-combine32.h"
#include "pixman-intermediate-private.h"

#define SCANLINE_BUFFER_LENGTH 8192

static pixman_color_space_t
_pixman_image_get_color_space (pixman_image_t *image)
{
    if (image->common.type == BITS)
        return image->bits.color_space;

    return PIXMAN_COLOR_SPACE_ARGB;
}

static void
general_composite_rect  (pixman_implementation_t *imp,
                         pixman_op_t              op,
                         pixman_image_t *         src,
                         pixman_image_t *         mask,
                         pixman_image_t *         dest,
                         int32_t                  src_x,
                         int32_t                  src_y,
                         int32_t                  mask_x,
                         int32_t                  mask_y,
                         int32_t                  dest_x,
                         int32_t                  dest_y,
                         int32_t                  width,
                         int32_t                  height)
{
    uint8_t stack_scanline_buffer[SCANLINE_BUFFER_LENGTH * 3];
    const pixman_format_code_t src_format =
	src->type == BITS ? src->bits.format : 0;
    const pixman_format_code_t mask_format =
	mask && mask->type == BITS ? mask->bits.format : 0;
    const pixman_format_code_t dest_format =
	dest->type == BITS ? dest->bits.format : 0;
    const int src_wide = PIXMAN_FORMAT_IS_WIDE (src_format);
    const int mask_wide = mask && PIXMAN_FORMAT_IS_WIDE (mask_format);
    const int dest_wide = PIXMAN_FORMAT_IS_WIDE (dest_format);
    const int wide = src_wide || mask_wide || dest_wide;
    const int Bpp = wide ? 8 : 4;
    uint8_t *scanline_buffer = stack_scanline_buffer;
    uint8_t *src_buffer, *mask_buffer, *dest_buffer;
    color_space_convert_t convert_src, convert_mask, convert_dest, convert_dest_inv;
    void *convert_src_data, *convert_mask_data, *convert_dest_data, *convert_dest_inv_data;
    fetch_scanline_t fetch_src = NULL, fetch_mask = NULL, fetch_dest = NULL;
    pixman_combine_32_func_t compose;
    store_scanline_t store;
    source_image_class_t src_class, mask_class;
    pixman_color_space_t color_space;
    pixman_bool_t component_alpha;
    uint32_t *bits;
    int32_t stride;
    int i;

    if (width * Bpp > SCANLINE_BUFFER_LENGTH)
    {
	scanline_buffer = pixman_malloc_abc (width, 3, Bpp);

	if (!scanline_buffer)
	    return;
    }

    src_buffer = scanline_buffer;
    mask_buffer = src_buffer + width * Bpp;
    dest_buffer = mask_buffer + width * Bpp;

    color_space = _pixman_color_space_select_for_composite (op, dest->bits.color_space);

    src_class = _pixman_image_classify (src,
                                        src_x, src_y,
                                        width, height);

    mask_class = SOURCE_IMAGE_CLASS_UNKNOWN;

    if (mask)
    {
	mask_class = _pixman_image_classify (mask,
	                                     src_x, src_y,
	                                     width, height);
    }

    if (op == PIXMAN_OP_CLEAR)
	fetch_src = NULL;
    else
	fetch_src = wide ? _pixman_image_get_scanline_64 : _pixman_image_get_scanline_32;
    if (fetch_src)
    {
        _pixman_color_space_get_converter (_pixman_image_get_color_space (src),
                                           color_space,
                                           !pixman_format_is_opaque (src->bits.format),
                                           wide,
                                           &convert_src,
                                           &convert_src_data);
    }
    else
    {
        convert_src = NULL;
        convert_src_data = NULL;
    }

    if (!mask || op == PIXMAN_OP_CLEAR)
	fetch_mask = NULL;
    else
	fetch_mask = wide ? _pixman_image_get_scanline_64 : _pixman_image_get_scanline_32;
    if (fetch_mask &&
        mask->common.type == BITS)
    {
        _pixman_color_space_get_converter (_pixman_image_get_color_space (mask),
                                           color_space,
                                           !pixman_format_is_opaque (mask->bits.format),
                                           wide,
                                           &convert_mask,
                                           &convert_mask_data);
    }
    else
    {
        convert_mask = NULL;
        convert_mask_data = NULL;
    }

    if (op == PIXMAN_OP_CLEAR || op == PIXMAN_OP_SRC)
	fetch_dest = NULL;
    else
	fetch_dest = wide ? _pixman_image_get_scanline_64 : _pixman_image_get_scanline_32;
    if (fetch_dest &&
        dest->common.type == BITS)
    {
        _pixman_color_space_get_converter (_pixman_image_get_color_space (dest),
                                           color_space,
                                           !pixman_format_is_opaque (dest->bits.format),
                                           wide,
                                           &convert_dest,
                                           &convert_dest_data);
    }
    else
    {
        convert_dest = NULL;
        convert_dest_data = NULL;
    }


    if (wide)
        store = _pixman_image_store_scanline_64;
    else
        store = _pixman_image_store_scanline_32;
    _pixman_color_space_get_converter (color_space,
                                       _pixman_image_get_color_space (dest),
                                       !pixman_format_is_opaque (dest->bits.format),
                                       wide,
                                       &convert_dest_inv,
                                       &convert_dest_inv_data);

    /* Skip the store step and composite directly into the
     * destination if the output format of the compose func matches
     * the destination format.
     *
     * If the destination format is a8r8g8b8 then we can always do
     * this. If it is x8r8g8b8, then we can only do it if the
     * operator doesn't make use of destination alpha.
     */
    if ((dest->bits.format == PIXMAN_a8r8g8b8)	||
	(dest->bits.format == PIXMAN_x8r8g8b8	&&
	 (op == PIXMAN_OP_OVER		||
	  op == PIXMAN_OP_ADD		||
	  op == PIXMAN_OP_SRC		||
	  op == PIXMAN_OP_CLEAR	||
	  op == PIXMAN_OP_IN_REVERSE	||
	  op == PIXMAN_OP_OUT_REVERSE	||
	  op == PIXMAN_OP_DST)))
    {
	if (!wide &&
            !dest->common.alpha_map &&
	    !dest->bits.write_func &&
            dest->bits.color_space == PIXMAN_COLOR_SPACE_ARGB)
	{
	    store = NULL;
	}
    }

    if (!store)
    {
	bits = dest->bits.bits;
	stride = dest->bits.rowstride;
    }
    else
    {
	bits = NULL;
	stride = 0;
    }

    component_alpha =
        fetch_src                       &&
        fetch_mask                      &&
        mask                            &&
        mask->common.type == BITS       &&
        mask->common.component_alpha    &&
        PIXMAN_FORMAT_RGB (mask->bits.format);

    if (wide)
    {
	if (component_alpha)
	    compose = (pixman_combine_32_func_t)_pixman_implementation_combine_64_ca;
	else
	    compose = (pixman_combine_32_func_t)_pixman_implementation_combine_64;
    }
    else
    {
	if (component_alpha)
	    compose = _pixman_implementation_combine_32_ca;
	else
	    compose = _pixman_implementation_combine_32;
    }

    if (!compose)
	return;

    if (!fetch_mask)
	mask_buffer = NULL;

    for (i = 0; i < height; ++i)
    {
	/* fill first half of scanline with source */
	if (fetch_src)
	{
	    if (fetch_mask)
	    {
		/* fetch mask before source so that fetching of
		   source can be optimized */
		fetch_mask (mask, mask_x, mask_y + i,
		            width, (void *)mask_buffer, 0, 0);
                if (convert_mask)
                    convert_mask (convert_mask_data, (void *)mask_buffer, width);

		if (mask_class == SOURCE_IMAGE_CLASS_HORIZONTAL)
		    fetch_mask = NULL;
	    }

	    if (src_class == SOURCE_IMAGE_CLASS_HORIZONTAL)
	    {
		fetch_src (src, src_x, src_y + i,
		           width, (void *)src_buffer, 0, 0);
		fetch_src = NULL;
	    }
	    else
	    {
		fetch_src (src, src_x, src_y + i,
		           width, (void *)src_buffer, (void *)mask_buffer,
		           0xffffffff);
	    }
            if (convert_src)
                convert_src (convert_src_data, (void *)src_buffer, width);
	}
	else if (fetch_mask)
	{
	    fetch_mask (mask, mask_x, mask_y + i,
	                width, (void *)mask_buffer, 0, 0);
            if (convert_mask)
                convert_mask (convert_mask_data, (void *)mask_buffer, width);
	}

	if (store)
	{
	    /* fill dest into second half of scanline */
	    if (fetch_dest)
	    {
		fetch_dest (dest, dest_x, dest_y + i,
		            width, (void *)dest_buffer, 0, 0);
                if (convert_dest)
                    convert_dest (convert_dest_data, (void *)dest_buffer, width);
	    }

	    /* blend */
	    compose (imp->toplevel, op,
		     (void *)dest_buffer,
		     (void *)src_buffer,
		     (void *)mask_buffer,
		     width);

            if (convert_dest_inv)
                convert_dest_inv (convert_dest_inv_data, (void *)dest_buffer, width);

	    /* write back */
	    store (&(dest->bits), dest_x, dest_y + i, width,
	           (void *)dest_buffer);
	}
	else
	{
	    /* blend */
	    compose (imp->toplevel, op,
		     bits + (dest_y + i) * stride + dest_x,
	             (void *)src_buffer, (void *)mask_buffer, width);
	}
    }

    if (scanline_buffer != stack_scanline_buffer)
	free (scanline_buffer);
}

static const pixman_fast_path_t general_fast_path[] =
{
    { PIXMAN_OP_any, PIXMAN_any, 0, PIXMAN_any,	0, PIXMAN_any, 0, general_composite_rect },
    { PIXMAN_OP_NONE }
};

static pixman_bool_t
general_blt (pixman_implementation_t *imp,
             uint32_t *               src_bits,
             uint32_t *               dst_bits,
             int                      src_stride,
             int                      dst_stride,
             int                      src_bpp,
             int                      dst_bpp,
             int                      src_x,
             int                      src_y,
             int                      dst_x,
             int                      dst_y,
             int                      width,
             int                      height)
{
    /* We can't blit unless we have sse2 or mmx */

    return FALSE;
}

static pixman_bool_t
general_fill (pixman_implementation_t *imp,
              uint32_t *               bits,
              int                      stride,
              int                      bpp,
              int                      x,
              int                      y,
              int                      width,
              int                      height,
              uint32_t xor)
{
    return FALSE;
}

pixman_implementation_t *
_pixman_implementation_create_general (void)
{
    pixman_implementation_t *imp = _pixman_implementation_create (NULL, general_fast_path);

    _pixman_setup_combiner_functions_32 (imp);
    _pixman_setup_combiner_functions_64 (imp);

    imp->blt = general_blt;
    imp->fill = general_fill;

    return imp;
}