summaryrefslogtreecommitdiff
path: root/fimage.c
blob: 33894d089fa98d85134ee9ce5704678b485bad5a (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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#include <stdlib.h>
#include <pixman.h>
#include "fimage.h"

/* TODO:
 *
 *    rename to fragment_list?
 */

struct fimage_t
{
    pixman_bool_t	broken;
    int			n_allocated_fragments;
    int			n_fragments;
    fragment_t *	fragments[1];
};

static const fimage_t broken_fimage =
{
    TRUE,	/* broken */
    0,
    NULL
};

static fimage_t *
fimage_ensure_space (fimage_t *fimage, int n_more)
{
    size_t required_fragments = fimage->n_fragments + n_more;

    if (required_fragments > fimage->n_allocated_fragments)
    {
	size_t pot = 1;
	size_t space;
	fimage_t *new_image;

	while (pot < required_fragments)
	    pot *= 2;

	space = sizeof (fimage_t) + (pot - 1) * sizeof (fragment_t *);

	new_image = realloc (fimage, space);

	if (!new_image)
	{
	    fimage_free (fimage);
	    return (fimage_t *)&broken_fimage;
	}

	new_image->n_allocated_fragments = pot;
	
	fimage = new_image;
    }

    return fimage;
}

static fimage_t *
fimage_append_fragment (fimage_t *fimage, fragment_t *fragment)
{
    if (fimage->broken)
    {
	fragment_free (fragment);
	return fimage;
    }

    if (fragment_is_broken (fragment))
    {
	fimage_free (fimage);
	return (fimage_t *)&broken_fimage;
    }

    if (fragment_is_empty (fragment))
	return fimage;
    
    fimage = ensure_space (fimage, 1);
    if (fimage->broken)
	return fimage;

    fimage->fragments[fimage->n_fragments++] = fragment;
    return fimage;
}

static fimage_t *
fimage_new (void)
{
    fimage_t *fimage;

    fimage = malloc (sizeof *fimage);
    if (!fimage)
    {
	fragment_free (fragment);
	return (fimage_t *)&broken_fimage;
    }

    fimage->broken = FALSE;
    fimage->n_allocated_fragments = 1;
    fimage->n_fragments = 0;

    return fimage;
}

static fimage_t *
make_image (fragment_t *fragment)
{
    return fimage_append_fragment (fimage_new(), fragment);
}

fimage_t *
fimage_new_blank (int width, int height)
{
    return make_image (fragment_new_blank (width, height));
}

fimage_t *
fimage_new_white (int width, int height)
{
    return make_image (fragment_new_white (width, height));
}

fimage_t *
fimage_new_traps (int width, int height,
		  int n_traps, pixman_trapezoid_t *traps)
{
    return make_image (fragment_new_traps (width, height, n_traps, traps));
}

fimage_t *
fimage_new_glyphs (int width, int height,
		   pixman_glyph_cache_t *cache,
		   int n_glyphs,
		   pixman_glyph_t *glyphs)
{
    return make_image (
	fragment_new_glyphs (width, height, cache, n_glyphs, glyphs));
}

/* Takes ownership of the image */
fimage_t *
fimage_new_image (int width, int height, pixman_image_t *image)
{
    return make_image (fragment_new_image (width, height, image));
}


/* Makes a copy of the region; does not take ownership */
fimage_t *
fimage_new_region (int width, int height,
		   pixman_region32_t *region)
{
    fragment_t *fragment1, *fragment2;

    fragment1 = fragment_new_region (region);

    fragment2 = fragment_new_blank (width, height);
    fragment2 = fragment_subtract (fragment1, fragment_2);
    
    image = fimage_new ();
    image = fimage_append (image, fragment1);
    image = fimage_appned (image, fragment2);

    return image;
}

fimage_t *
fimage_composite (fimage_t *fimage, pixman_op_t op, fimage_t *source)
{
    fragment_t *dfrag, *sfrag;
    fimage_t *new_image;
    int i, j;
    int n;

    if (fimage->broken || source->broken)
    {
	fimage_free (fimage);
	return (fimage_t *)&broken_fimage;
    }

    new_image = fimage_new ();

    for (i = 0; fimage->n_fragments; ++i)
    {
	for (j = 0; j < source->n_fragments; ++j)
	{
	    fragment_t *new, *dfrag, *sfrag;

	    dfrag = fimage->fragments[i];
	    sfrag = source->fragments[j];

	    new = fragment_composite (dfrag, op, sfrag);

	    new_image = fimage_append_fragment (new_image, new);
	    new_image = fimage_append_fragment (new_image, dfrag);
	}
    }

    fimage->n_fragments = 0;
    fimage_free (fimage);

    return new_image;
}

void
fimage_paint (fimage_t *image, pixman_image_t *image)
{
    int i;

    for (i = 0;  i < image->n_fragments; ++i)
    {
	fragment_t *fragment = image->fragments[i];

	fragment_apply (fragment, image);
    }
}

/*
  
  Memory management:
  
  For traps we don't want to copy them, so the image should take ownership.
  Maybe the cairo->pixman conversion should just take place inside the image?
  Maybe it doesn't matter that much.
  Maybe polygon images will make the question irrelevant.
  However, we definitely don't want to copy the traps structure to
     all the fragments.
  
  For pimages, have the image take a ref would be more convenient.
  
  For initializing with a region, it would be convenient, but not essential,
  if we can avoid copying the region parameter.
  
  Structures internal to the images and fragments are of course managed and
  owned by the structures in question.

  - images take ownership
  - fragments take ownership
    - fragments wrap objects in refcountable structure
    - fragments don't ref pixman images 
    - fragments ref the given objects
    - pixman images are reffed by the fragments, and unreffed
      when the image is destroyed.

      - ref countable state object?
      
*/
typedef struct fragment_t fragment_t;
typedef struct image_t image_t;
typedef struct command_buffer_t command_buffer_t;

#define TRUE 1
#define FALSE 0

typedef enum
{
    BLANK,
    WHITE,
    SOLID,
    TRAPS,
    PIXMAN,
    COMMANDS
} state_t;

struct fragment_t
{
    pixman_bool_t	broken;
    pixman_region32_t	region;
    fragment_t *	next;
    
    /* State - should this be a separate structure? */
    state_t		state;
    pixman_bool_t	alpha_only;
    union
    {
	pixman_color_t	solid;
	struct
	{
	    int			n_traps;
	    pixman_trapezoid_t *traps;
	} traps;
	pixman_image_t *image;
	command_buffer_t *buffer;
    } u;
};

struct image_t
{
    pixman_bool_t	broken;
    int			width, height;
    fragment_t *	fragments;
};

static const image_t broken =
{
    TRUE	/* broken */
};

static const fragment_t broken_fragment =
{
    TRUE
};

static fragment_t *
fragment_new (int width, int height)
{
    fragment_t *result = malloc (sizeof *result);
    
    if (!result)
	return (fragment_t *)&broken_fragment;
    
    pixman_region32_init_rect (&result->region, 0, 0, width, height);
    
    return result;
}

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


    /* FIXME */
}

static void
fragment_set_blank (fragment_t *fragment)
{
    if (fragment->broken)
	return;
    
    fragment->state = BLANK;
}

static void
fragment_set_pixman (fragment_t *fragment, pixman_image_t *image)
{
    if (fragment->broken)
	return;
    
    fragment->state = PIXMAN;
    fragment->u.image = image;
}

/* Split @fragment into two parts: one that intersects @source
 * and one that doesn't. Both may be NULL. @dest is freed or reused.
 */
static void
fragment_intersect (fragment_t *dest, fragment_t *source,
		    fragment_t **intersection,
		    fragment_t **remains)
{
    *intersection = NULL;
    
    if (dest->broken)
    {
	*remains = NULL;
	return;
    }
    
    *remains = dest;
    
    if (source->broken)
    {
	*intersection = NULL;
	return;
    }
    
    *intersection = fragment_new (0, 0);
    
    if ((*intersection)->broken)
    {
	*intersection = NULL;
	return;
    }
    
    if (!pixman_region32_intersect (&((*intersection)->region),
				    &(dest->region),
				    &(source->region)))
    {
	fragment_free (*intersection);
	*intersection = NULL;
	return;
    }
    
    if (!pixman_region32_subtract (&(dest->region), &(dest->region),
				   &(*intersection)->region))
    {
	fragment_free (dest);
	fragment_free (*intersection);
	*remains = NULL;
	*intersection = NULL;
	return;
    }
    
    if (!pixman_region32_not_empty (&(*intersection)->region))
    {
	fragment_free (*intersection);
	*intersection = NULL;
    }
    
    if (!pixman_region32_not_empty (&dest->region))
    {
	fragment_free (dest);
	*remains = NULL;
    }
}

static void
fragment_composite (fragment_t *result, pixman_op_t op,
		    fragment_t *dest, fragment_t *src)
{
    if ((op == PIXMAN_OP_ADD && dest->state == BLANK)	||
	(op == PIXMAN_OP_IN && is_opaque (dest)))
    {
	op = PIXMAN_OP_SRC;
    }
    
    switch (op)
    {
    case PIXMAN_OP_SRC:
	copy_state (result, src);
	break;
	
    case PIXMAN_OP_IN:
	if (dest->state == BLANK || src->state == BLANK)
	    result->state == BLANK;
	break;
	
    case PIXMAN_OP_OUT_REVERSE:
	if (is_opaque (src))
	    result->state == BLANK;
	else if (src->state == BLANK)
	    copy_state (result, dest->state);
	break;
	
    default:
	/* FIXME: add command to composite the images */
	break;
    }
}

static image_t *
allocate_image (int width, int height)
{
    image_t *image = malloc (sizeof *image);
    
    if (!image)
	return (image_t *)&broken;
    
    image->width = width;
    image->height = height;
    image->fragments = fragment_new (width, height);
    
    return image;
}

static image_t *
image_new_blank (int width, int height)
{
    image_t *image;
    
    image = allocate_image (width, height);
    if (image->broken)
	return image;
    
    fragment_set_blank (image->fragments);
    return image;
}

void
image_free (image_t *image)
{
    if (image->broken)
	return;
    
    /* FIXME */
}

static image_t *
image_new_pixman (int width, int height, pixman_image_t *pimage)
{
    image_t *image;
    
    image = allocate_image (width, height);
    if (image->broken)
	return image;
    
    fragment_set_pixman (image->fragments, pimage);
    return image;
}

static void
image_composite (image_t *dest, pixman_op_t op, image_t *src, image_t *mask)
{
    fragment_t *sfrag;
    
    if (dest->broken || src->broken || (mask && mask->broken))
	return;
    
    if (mask)
    {
	image_t *tmp = allocate_image (dest->width, dest->height);
	
	image_composite (tmp, PIXMAN_OP_SRC, mask, NULL);
	image_composite (tmp, PIXMAN_OP_IN, src, NULL);
	image_composite (dest, op, tmp, NULL);
	
	image_free (tmp);
	
	return;
    }
    
    sfrag = src->fragments;
    while (sfrag)
    {
	fragment_t *new_fragments = NULL;
	fragment_t *dfrag;
	
	dfrag = dest->fragments;
	while (dfrag != NULL)
	{
	    fragment_t *intersection, *remains;
	    fragment_t *next = dfrag->next;
	    
	    fragment_intersect (dfrag, sfrag, &intersection, &remains);
	    
	    if (intersection)
	    {
		fragment_composite (intersect, op, dfrag, sfrag);
		
		intersection->next = new_fragments;
		new_fragments = intersection;
	    }
	    
	    if (remains)
	    {
		remains->next = new_fragments;
		new_fragments = remains;
	    }
	    
	    dfrag = next;
	}
	
	sfrag = sfrag->next;
    }
    
    dest->fragments = new_fragments;
}