summaryrefslogtreecommitdiff
path: root/fragments.c
blob: cf6f6340f62c93ec3e4394ce616641ed3d87b314 (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
#include <stdlib.h>
#include <pixman.h>

/*
  
  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.
  
*/


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;
}