summaryrefslogtreecommitdiff
path: root/fragment.c
blob: 883fca861b5d75c9f7251f9ec982a6fc82bbe920 (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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#include <assert.h>
#include <stdlib.h>
#include <pixman.h>
#include "fimage.h"

/* State should be considered a 'value' object that can be copied and written
 * to. It may use copy-on-write internally, but the first iteration will
 * just copy. What if the copy in copy-on-write fails? Either writes can
 * fail or states must be broken.
 *
 * There may be only one operation required: composite. If so,
 * having that copy and write wouldn't be too bad.
 *
 * Actually composite doesn't even need to update the state. We can
 * just always copy because once a state is created it doesn't ever change.
 * Which also means that command buffer is just "command".
 */
typedef union state_t state_t;
typedef enum
{
    STATE_WHITE,
    STATE_BLANK,
    STATE_TRAPS,
    STATE_GLYPHS,
    STATE_IMAGE,
    STATE_COMPOSITE
} state_type_t;

typedef struct
{
    int			ref_count;
    state_type_t	type;
} state_common_t;

typedef struct
{
    state_common_t	 common;
    int			 n_traps;
    pixman_trapezoid_t	*traps;
} state_traps_t;

typedef struct
{
    state_common_t	  common;
    pixman_glyph_cache_t *cache;
    int			  n_glyphs;
    pixman_glyph_t *	  glyphs;
} state_glyphs_t;

typedef struct
{
    state_common_t	  common;
    pixman_image_t *	  image;
} state_image_t;

/* This state is interpreted as "copy @dest, then composite @source
 * on top using @op as the operator".
 */
typedef struct
{
    state_t *		dest;
    pixman_op_t		op;
    state_t *		source;
} state_composite_t;

union state_t
{
    state_common_t	common;
    state_traps_t	traps;
    state_glyphs_t	glyphs;
    state_image_t	image;
    state_composite_t	composite;
};

struct fragment_t
{
    pixman_bool_t	broken;
    pixman_region32_t	region;
    state_t *		state;
};

static state_t *
state_new (state_type_t type)
{
    state_t *state = malloc (sizeof *state);
    if (!state)
	return NULL;
    
    state->common.ref_count = 1;
    state->common.type = type;
    
    return state;
}

static state_t *
state_ref (state_t *state)
{
    state->common.ref_count++;
    
    return state;
}

static void
state_unref (state_t *state)
{
    if (--state->common.ref_count == 0)
    {
	switch (state->common.type)
	{
	case STATE_WHITE:
	case STATE_BLANK:
	    break;
	case STATE_TRAPS:
	    free (state->traps.traps);
	    break;
	case STATE_GLYPHS:
	    free (state->glyphs.glyphs);
	    break;
	case STATE_IMAGE:
	    pixman_image_unref (state->image.image);
	    break;
	case STATE_COMPOSITE:
	    state_unref (state->composite.dest);
	    state_unref (state->composite.source);
	    break;
	}

	free (state);
    }
}

static state_t *
state_composite (state_t *dest, pixman_op_t op, state_t *source)
{
    state_t *new_state;

    new_state = malloc (sizeof *new_state);
    if (!new_state)
	return NULL;
    
    /* FIXME: Always using COMPOSITE should be correct, but
     * we can do a lot better with a few simple optimizations
     */
    new_state->common.type = STATE_COMPOSITE;
    new_state->common.ref_count = 1;

    new_state->composite.dest = state_ref (dest);
    new_state->composite.op = op;
    new_state->composite.source = state_ref (source);
    
    return new_state;
}

static const fragment_t broken_fragment =
{
    TRUE,			/* broken */
    { { 0, 0, 0, 0  }, NULL },	/* region */
    NULL			/* state */
};

static fragment_t *
fragment_alloc (int width, int height, state_type_t type)
{
    fragment_t *fragment = malloc (sizeof *fragment);
    
    if (!fragment)
	return (fragment_t *)&broken_fragment;
    
    if (!(fragment->state = state_new (type)))
    {
	free (fragment);
	return (fragment_t *)&broken_fragment;
    }
    
    fragment->broken = FALSE;
    pixman_region32_init_rect (&fragment->region, 0, 0, width, height);
    
    return fragment;
}

fragment_t *
fragment_new_blank (int width, int height)
{
    return fragment_alloc (width, height, STATE_BLANK);
}

fragment_t *
fragment_new_white (int width, int height)
{
    return fragment_alloc (width, height, STATE_WHITE);
}

fragment_t *
fragment_new_traps (int width, int height,
		    int n_traps,
		    pixman_trapezoid_t *traps)
{
    fragment_t *fragment;
    
    fragment = fragment_alloc (width, height, STATE_TRAPS);
    if (fragment->broken)
	return fragment;
    
    fragment->state->traps.n_traps = n_traps;
    fragment->state->traps.traps = traps;
    
    return fragment;
}

fragment_t *
fragment_new_region (pixman_region32_t *region)
{
    fragment_t *fragment;

    fragment = fragment_alloc (1, 1, STATE_WHITE);
    if (fragment->broken)
	return fragment;

    if (!pixman_region32_copy (&fragment->region, region))
    {
	fragment_free (fragment);
	return (fragment_t *)&broken_fragment;
    }

    return fragment;
}

fragment_t *
fragment_new_glyphs (int width, int height,
		     pixman_glyph_cache_t *cache,
		     int		   n_glyphs,
		     pixman_glyph_t *glyphs)
{
    fragment_t *fragment;
    
    fragment = fragment_alloc (width, height, STATE_GLYPHS);
    if (fragment->broken)
	return fragment;
    
    fragment->state->glyphs.n_glyphs = n_glyphs;
    fragment->state->glyphs.cache = cache;
    fragment->state->glyphs.glyphs = glyphs;
    
    return fragment;
}

fragment_t *
fragment_new_image (int width, int height,
		    pixman_image_t *image)
{
    fragment_t *fragment;
    fragment = fragment_alloc (width, height, STATE_IMAGE);
    if (fragment->broken)
	return fragment;

    fragment->state->image.image = image;

    return fragment;
}

fragment_t *
fragment_subtract (fragment_t *dest, fragment_t *other)
{
    if (dest->broken || other->broken ||
	!pixman_region32_subtract (
	    &dest->region, &dest->region, &(other->region)))
    {
	fragment_free (dest);

	return (fragment_t *)&broken_fragment;
    }

    return dest;
}

pixman_bool_t
fragment_is_empty (fragment_t *fragment)
{
    if (fragment->broken)
	return FALSE;

    return !pixman_region32_not_empty (&fragment->region);
}

pixman_bool_t
fragment_is_broken (fragment_t *fragment)
{
    return fragment->broken;
}

static fragment_t *
fragment_intersect (fragment_t *dest, fragment_t *source)
{
    fragment_t *new_fragment;
    
    if (!(new_fragment = malloc (sizeof *new_fragment)))
	return (fragment_t *)&broken_fragment;
    
    new_fragment->broken = FALSE;
    
    pixman_region32_init (&new_fragment->region);
    if (!pixman_region32_intersect (
	    &new_fragment->region, &dest->region, &source->region))
    {
	free (new_fragment);
	return (fragment_t *)&broken_fragment;
    }
    
    if (!pixman_region32_subtract (
	    &dest->region, &dest->region, &new_fragment->region))
    {
	pixman_region32_fini (&new_fragment->region);
	free (new_fragment);
	
	return (fragment_t *)&broken_fragment;
    }
    
    return new_fragment;
}

/* Compute intersection of dest and source with state corresponding
 * to compositing source with dest. Then subtract the region of
 * intersection from dest and return the intersection fragment.
 * Both intersection and dest may end up empty or broken.
 */
fragment_t *
fragment_composite (fragment_t *dest, pixman_op_t op, fragment_t *source)
{
    fragment_t *result;
    state_t *new_state;
    
    if (dest->broken || source->broken)
	return (fragment_t *)&broken_fragment;
    
    new_state = state_composite (dest->state, op, source->state);
    if (!new_state)
	return (fragment_t *)&broken_fragment;
    
    result = fragment_intersect (dest, source);
    if (result->broken)
    {
	state_unref (new_state);
	return result;
    }
    
    result->broken = FALSE;
    result->state = new_state;
    
    return result;
}

void
fragment_free (fragment_t *fragment)
{
    if (fragment->broken)
	return;

    pixman_region32_fini (&fragment->region);
    state_unref (fragment->state);
}

static const pixman_color_t white =
{ 
    0xffff, 0xffff, 0xffff, 0xffff
};

static void
state_apply (state_t *state, pixman_image_t *image)
{
    int width = pixman_image_get_width (image);
    int height = pixman_image_get_height (image);
    pixman_image_t *white_img, *temp;
    pixman_format_code_t mask_format;
    pixman_glyph_cache_t *glyph_cache;
    pixman_glyph_t *glyphs;
    int n_glyphs;

    white_img = pixman_image_create_solid_fill (&white);
    if (!white_img)
	return; /* FIXME status - FIXME: only create when necessary */

    switch (state->common.type)
    {
    case STATE_BLANK:
	pixman_image_composite32 (
	    PIXMAN_OP_CLEAR, white_img, NULL, image,
	    0, 0, 0, 0, 0, 0, width, height);
	break;

    case STATE_TRAPS:
	/* FIXME: there may be a pixman bug here that we need
	 * to fix or work around: it incorrectly clips rendering
	 * to the extent of the trapezoids. (That is wrong for SRC
	 * and other operators).
	 */
	pixman_composite_trapezoids (
	    PIXMAN_OP_SRC, white_img, image, PIXMAN_a8,
	    0, 0, 0, 0, state->traps.n_traps, state->traps.traps);
	break;

    case STATE_GLYPHS:
	glyph_cache = state->glyphs.cache;
	n_glyphs = state->glyphs.n_glyphs;
	glyphs = state->glyphs.glyphs;

	mask_format = pixman_glyph_get_mask_format (
	    glyph_cache, n_glyphs, glyphs);
	pixman_composite_glyphs (
	    PIXMAN_OP_SRC, white_img, image, mask_format,
	    0, 0, 0, 0, 0, 0, width, height,
	    glyph_cache, n_glyphs, glyphs);
	break;
	
    case STATE_WHITE:
	pixman_image_composite32 (
	    PIXMAN_OP_SRC, white_img, NULL, image,
	    0, 0, 0, 0, 0, 0, width, height);
	break;
	
    case STATE_IMAGE:
	if (state->image.image == image)
	    return;

	pixman_image_composite (
	    PIXMAN_OP_SRC, state->image.image, NULL, image,
	    0, 0, 0, 0, 0, 0, width, height);
	break;

    case STATE_COMPOSITE:
	/* FIXME: track alpha-only */
	temp = pixman_image_create_bits (
	    PIXMAN_a8r8g8b8, width, height, NULL, -1);

	if (!temp)
	    break; /* FIXME: status */
	
	state_apply (state->composite.dest, image);

	/* FIXME: check if source can be expressed as
	 * (s IN m), (s IN glyphs), (s IN traps)
	 */
	state_apply (state->composite.source, temp);

	pixman_image_composite32 (
	    state->composite.op, temp, NULL, image,
	    0, 0, 0, 0, 0, 0, width, height);

	pixman_image_unref (temp);
	break;
    }
}

pixman_bool_t
fragment_apply (fragment_t *fragment, pixman_image_t *image)
{
    if (fragment->broken)
	return FALSE;
    
    pixman_image_set_clip_region32 (image, &fragment->region);

    state_apply (fragment->state, image);

    pixman_image_set_clip_region32 (image, NULL);
    
    return TRUE;
}