summaryrefslogtreecommitdiff
path: root/src/drm/cairo-drm-i965-spans.c
blob: 5cba7cec8d92a38d5f36ed384ebb30090cda662c (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
/* cairo - a vector graphics library with display and print output
 *
 * Copyright © 2009 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
 * You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 *
 * The Original Code is the cairo graphics library.
 *
 * The Initial Developer of the Original Code is Red Hat, Inc.
 *
 * Contributor(s):
 *	Chris Wilson <chris@chris-wilson.co.uk>
 */

#include "cairoint.h"

#include "cairo-composite-rectangles-private.h"
#include "cairo-boxes-private.h"
#include "cairo-error-private.h"
#include "cairo-drm-i965-private.h"

/* Operates in either immediate or retained mode.
 * When given a clip region we record the sequence of vbo and then
 * replay them for each clip rectangle, otherwise we simply emit
 * the vbo straight into the command stream.
 */

typedef struct _i965_spans i965_spans_t;

typedef float *
(*i965_get_rectangle_func_t) (i965_spans_t *spans);

struct _i965_spans {
    cairo_span_renderer_t renderer;

    i965_device_t *device;

    int xmin, xmax;
    cairo_bool_t is_bounded;
    const cairo_rectangle_int_t *extents;

    i965_get_rectangle_func_t get_rectangle;
    i965_shader_t shader;

    cairo_region_t *clip_region;

    struct i965_vbo head, *tail;

    unsigned int vbo_offset;
    float *vbo_base;
};

static float *
i965_spans_emit_rectangle (i965_spans_t *spans)
{
    return i965_add_rectangle (spans->device);
}

static float *
i965_spans_accumulate_rectangle (i965_spans_t *spans)
{
    float *vertices;
    uint32_t size;

    size = spans->device->rectangle_size;
    if (unlikely (spans->vbo_offset + size > I965_VERTEX_SIZE)) {
	struct i965_vbo *vbo;

	vbo = malloc (sizeof (struct i965_vbo));
	if (unlikely (vbo == NULL)) {
	    /* throw error! */
	}

	spans->tail->next = vbo;
	spans->tail = vbo;

	vbo->next = NULL;
	vbo->bo = intel_bo_create (&spans->device->intel,
				   I965_VERTEX_SIZE, I965_VERTEX_SIZE,
				   FALSE, I915_TILING_NONE, 0);
	vbo->count = 0;

	spans->vbo_offset = 0;
	spans->vbo_base = intel_bo_map (&spans->device->intel, vbo->bo);
    }

    vertices = spans->vbo_base + spans->vbo_offset;
    spans->vbo_offset += size;
    spans->tail->count += 3;

    return vertices;
}

static void
i965_span_rectangle (i965_spans_t *spans,
		     int x0, int x1, int y0, int y1,
		     int alpha)
{
    float *vertices;
    float a = alpha / 255.;

    vertices = spans->get_rectangle (spans);

    *vertices++ = x1;
    *vertices++ = y1;
    *vertices++ = a;

    *vertices++ = x0;
    *vertices++ = y1;
    *vertices++ = a;

    *vertices++ = x0;
    *vertices++ = y0;
    *vertices++ = a;
}

static cairo_status_t
i965_bounded_spans_mono (void *abstract_renderer,
			 int y, int height,
			 const cairo_half_open_span_t *half,
			 unsigned num_spans)
{
    i965_spans_t *spans = abstract_renderer;

    if (num_spans == 0)
	return CAIRO_STATUS_SUCCESS;

    do {
	if (half[0].coverage >= 128) {
	    i965_span_rectangle (spans,
				 half[0].x, half[1].x,
				 y, y + height,
				 255);
	}
	half++;
    } while (--num_spans > 1);

    return CAIRO_STATUS_SUCCESS;
}

static cairo_status_t
i965_bounded_spans (void *abstract_renderer,
		    int y, int height,
		    const cairo_half_open_span_t *half,
		    unsigned num_spans)
{
    i965_spans_t *spans = abstract_renderer;

    if (num_spans == 0)
	return CAIRO_STATUS_SUCCESS;

    do {
	if (half[0].coverage) {
	    i965_span_rectangle (spans,
				 half[0].x, half[1].x,
				 y, y + height,
				 half[0].coverage);
	}
	half++;
    } while (--num_spans > 1);

    return CAIRO_STATUS_SUCCESS;
}

static cairo_status_t
i965_unbounded_spans (void *abstract_renderer,
		      int y, int height,
		      const cairo_half_open_span_t *half,
		      unsigned num_spans)
{
    i965_spans_t *spans = abstract_renderer;

    if (num_spans == 0) {
	i965_span_rectangle (spans,
			     spans->xmin, spans->xmax,
			     y, y + height,
			     0);
	return CAIRO_STATUS_SUCCESS;
    }

    if (half[0].x != spans->xmin) {
	i965_span_rectangle (spans,
			     spans->xmin, half[0].x,
			     y, y + height,
			     0);
    }

    do {
	i965_span_rectangle (spans,
			     half[0].x, half[1].x,
			     y, y + height,
			     half[0].coverage);
	half++;
    } while (--num_spans > 1);

    if (half[0].x != spans->xmax) {
	i965_span_rectangle (spans,
			     half[0].x, spans->xmax,
			     y, y + height,
			     0);
    }

    return CAIRO_STATUS_SUCCESS;
}

static cairo_status_t
i965_unbounded_spans_mono (void *abstract_renderer,
			   int y, int height,
			   const cairo_half_open_span_t *half,
			   unsigned num_spans)
{
    i965_spans_t *spans = abstract_renderer;

    if (num_spans == 0) {
	i965_span_rectangle (spans,
			     spans->xmin, spans->xmax,
			     y, y + height,
			     0);
	return CAIRO_STATUS_SUCCESS;
    }

    if (half[0].x != spans->xmin) {
	i965_span_rectangle (spans,
			     spans->xmin, half[0].x,
			     y, y + height,
			     0);
    }

    do {
	int alpha = 0;
	if (half[0].coverage >= 128)
	    alpha = 255;
	i965_span_rectangle (spans,
			     half[0].x, half[1].x,
			     y, y + height,
			     alpha);
	half++;
    } while (--num_spans > 1);

    if (half[0].x != spans->xmax) {
	i965_span_rectangle (spans,
			     half[0].x, spans->xmax,
			     y, y + height,
			     0);
    }

    return CAIRO_STATUS_SUCCESS;
}

static cairo_status_t
i965_spans_init (i965_spans_t *spans,
		 i965_surface_t *dst,
		 cairo_operator_t op,
		 const cairo_pattern_t *pattern,
		 cairo_antialias_t antialias,
		 cairo_clip_t *clip,
		 const cairo_composite_rectangles_t *extents)
{
    cairo_status_t status;

    spans->device = i965_device (dst);
    i965_shader_init (&spans->shader, dst, op);

    spans->is_bounded = extents->is_bounded;
    if (extents->is_bounded) {
	if (antialias == CAIRO_ANTIALIAS_NONE)
	    spans->renderer.render_rows = i965_bounded_spans_mono;
	else
	    spans->renderer.render_rows = i965_bounded_spans;

	spans->extents = &extents->bounded;
    } else {
	if (antialias == CAIRO_ANTIALIAS_NONE)
	    spans->renderer.render_rows = i965_unbounded_spans_mono;
	else
	    spans->renderer.render_rows = i965_unbounded_spans;

	spans->extents = &extents->unbounded;
    }
    spans->xmin = spans->extents->x;
    spans->xmax = spans->extents->x + spans->extents->width;

    spans->clip_region = NULL;
    if (clip != NULL) {
	cairo_region_t *clip_region = NULL;

	status = _cairo_clip_get_region (clip, &clip_region);
	assert (status == CAIRO_STATUS_SUCCESS || status == CAIRO_INT_STATUS_UNSUPPORTED);

	if (clip_region != NULL && cairo_region_num_rectangles (clip_region) == 1)
	    clip_region = NULL;

	spans->clip_region = clip_region;
	if (status == CAIRO_INT_STATUS_UNSUPPORTED)
	    i965_shader_set_clip (&spans->shader, clip);
    }

    spans->head.next  = NULL;
    spans->head.bo    = NULL;
    spans->head.count = 0;
    spans->tail = &spans->head;

    if (spans->clip_region == NULL) {
	spans->get_rectangle = i965_spans_emit_rectangle;
    } else {
	spans->get_rectangle = i965_spans_accumulate_rectangle;
	spans->head.bo = intel_bo_create (&spans->device->intel,
					  I965_VERTEX_SIZE, I965_VERTEX_SIZE,
					  FALSE, I915_TILING_NONE, 0);
	if (unlikely (spans->head.bo == NULL))
	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);

	spans->vbo_base = intel_bo_map (&spans->device->intel, spans->head.bo);
    }
    spans->vbo_offset = 0;

    return i965_shader_acquire_pattern (&spans->shader,
					&spans->shader.source,
					pattern, &extents->bounded);
}

static void
i965_spans_fini (i965_spans_t *spans)
{
    i965_shader_fini (&spans->shader);

    if (spans->head.bo != NULL) {
	struct i965_vbo *vbo, *next;

	intel_bo_destroy (&spans->device->intel, spans->head.bo);
	for (vbo = spans->head.next; vbo != NULL; vbo = next) {
	    next = vbo->next;
	    intel_bo_destroy (&spans->device->intel, vbo->bo);
	    free (vbo);
	}
    }
}

cairo_status_t
i965_clip_and_composite_spans (i965_surface_t		*dst,
			       cairo_operator_t		 op,
			       const cairo_pattern_t	*pattern,
			       cairo_antialias_t	 antialias,
			       i965_spans_func_t	 draw_func,
			       void			*draw_closure,
			       const cairo_composite_rectangles_t*extents,
			       cairo_clip_t		*clip)
{
    i965_spans_t spans;
    i965_device_t *device;
    cairo_status_t status;

    if (op == CAIRO_OPERATOR_CLEAR) {
	pattern = &_cairo_pattern_white.base;
	op = CAIRO_OPERATOR_DEST_OUT;
    }

    status = i965_spans_init (&spans, dst, op, pattern, antialias, clip, extents);
    if (unlikely (status))
	return status;

    spans.shader.mask.base.content  = CAIRO_CONTENT_ALPHA;
    spans.shader.mask.type.fragment = FS_SPANS;
    spans.shader.mask.type.vertex   = VS_SPANS;
    spans.shader.mask.type.pattern  = PATTERN_BASE;

    status = cairo_device_acquire (dst->intel.drm.base.device);
    if (unlikely (status))
	goto CLEANUP_SPANS;

    device = i965_device (dst);
    status = i965_shader_commit (&spans.shader, device);
    if (unlikely (status))
	goto CLEANUP_DEVICE;

    status = draw_func (draw_closure, &spans.renderer, spans.extents);
    if (spans.clip_region != NULL && status == CAIRO_STATUS_SUCCESS)
	i965_clipped_vertices (device, &spans.head, spans.clip_region);

  CLEANUP_DEVICE:
    cairo_device_release (dst->intel.drm.base.device);
  CLEANUP_SPANS:
    i965_spans_fini (&spans);

    return status;
}