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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
|
/* 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-i915-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 _i915_spans i915_spans_t;
typedef float *
(*i915_get_rectangle_func_t) (i915_spans_t *spans);
typedef void
(*i915_span_func_t) (i915_spans_t *spans,
int x0, int x1, int y0, int y1,
int alpha);
struct _i915_spans {
cairo_span_renderer_t renderer;
i915_device_t *device;
int xmin, xmax;
cairo_bool_t is_bounded;
const cairo_rectangle_int_t *extents;
i915_get_rectangle_func_t get_rectangle;
i915_span_func_t span;
i915_shader_t shader;
cairo_region_t *clip_region;
cairo_bool_t need_clip_surface;
struct vbo {
struct vbo *next;
intel_bo_t *bo;
unsigned int count;
} head, *tail;
int rectangle_size;
unsigned int vbo_offset;
float *vbo_base;
};
static float *
i915_emit_rectangle (i915_spans_t *spans)
{
return i915_add_rectangle (spans->device);
}
static float *
i915_accumulate_rectangle (i915_spans_t *spans)
{
float *vertices;
uint32_t size;
size = spans->rectangle_size;
if (unlikely (spans->vbo_offset + size > I915_VBO_SIZE)) {
struct vbo *vbo;
intel_bo_unmap (spans->tail->bo);
vbo = malloc (sizeof (struct 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, I915_VBO_SIZE, FALSE);
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
i915_span_constant (i915_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 void
i915_span_linear (i915_spans_t *spans,
int x0, int x1, int y0, int y1,
int alpha)
{
float *vertices;
float a = alpha / 255.;
double s, t;
vertices = spans->get_rectangle (spans);
*vertices++ = x1;
*vertices++ = y1;
s = x0, t = y0;
*vertices++ = i915_shader_linear_texcoord (&spans->shader.source.linear, s, t);
*vertices++ = a;
*vertices++ = x0;
*vertices++ = y1;
s = x1, t = y0;
*vertices++ = i915_shader_linear_texcoord (&spans->shader.source.linear, s, t);
*vertices++ = a;
*vertices++ = x0;
*vertices++ = y0;
s = x1, t = y1;
*vertices++ = i915_shader_linear_texcoord (&spans->shader.source.linear, s, t);
*vertices++ = a;
}
static void
i915_span_texture (i915_spans_t *spans,
int x0, int x1, int y0, int y1,
int alpha)
{
float *vertices;
float a = alpha / 255.;
double s, t;
vertices = spans->get_rectangle (spans);
*vertices++ = x1;
*vertices++ = y1;
s = x0, t = y0;
cairo_matrix_transform_point (&spans->shader.source.base.matrix, &s, &t);
*vertices++ = s; *vertices++ = t;
*vertices++ = a;
*vertices++ = x0;
*vertices++ = y1;
s = x1, t = y0;
cairo_matrix_transform_point (&spans->shader.source.base.matrix, &s, &t);
*vertices++ = s; *vertices++ = t;
*vertices++ = a;
*vertices++ = x0;
*vertices++ = y0;
s = x1, t = y1;
cairo_matrix_transform_point (&spans->shader.source.base.matrix, &s, &t);
*vertices++ = s; *vertices++ = t;
*vertices++ = a;
}
static void
i915_span_texture16 (i915_spans_t *spans,
int x0, int x1, int y0, int y1, int alpha)
{
float *vertices;
float a = alpha / 255.;
double s, t;
vertices = spans->get_rectangle (spans);
*vertices++ = x1;
*vertices++ = y1;
s = x0, t = y0;
cairo_matrix_transform_point (&spans->shader.source.base.matrix, &s, &t);
*vertices++ = texcoord_2d_16 (s, t);
*vertices++ = a;
*vertices++ = x0;
*vertices++ = y1;
s = x1, t = y0;
cairo_matrix_transform_point (&spans->shader.source.base.matrix, &s, &t);
*vertices++ = texcoord_2d_16 (s, t);
*vertices++ = a;
*vertices++ = x0;
*vertices++ = y0;
s = x1, t = y1;
cairo_matrix_transform_point (&spans->shader.source.base.matrix, &s, &t);
*vertices++ = texcoord_2d_16 (s, t);
*vertices++ = a;
}
static void
i915_span_generic (i915_spans_t *spans,
int x0, int x1, int y0, int y1, int alpha)
{
double s, t;
float *vertices;
float a = alpha / 255.;
/* Each vertex is:
* 2 vertex coordinates
* [0-2] source texture coordinates
* 1 alpha value.
* [0,2] clip mask coordinates
*/
vertices = spans->get_rectangle (spans);
/* bottom right */
*vertices++ = x1; *vertices++ = y1;
s = x1, t = y1;
switch (spans->shader.source.type.vertex) {
case VS_CONSTANT:
break;
case VS_LINEAR:
*vertices++ = i915_shader_linear_texcoord (&spans->shader.source.linear, s, t);
break;
case VS_TEXTURE:
cairo_matrix_transform_point (&spans->shader.source.base.matrix, &s, &t);
*vertices++ = s; *vertices++ = t;
break;
case VS_TEXTURE_16:
cairo_matrix_transform_point (&spans->shader.source.base.matrix, &s, &t);
*vertices++ = texcoord_2d_16 (s, t);
break;
}
*vertices++ = a;
if (spans->need_clip_surface) {
s = x1, t = y1;
cairo_matrix_transform_point (&spans->shader.clip.base.matrix, &s, &t);
*vertices++ = texcoord_2d_16 (s, t);
}
if (spans->shader.need_combine) {
s = x1, t = y1;
cairo_matrix_transform_point (&spans->shader.dst.base.matrix, &s, &t);
*vertices++ = texcoord_2d_16 (s, t);
}
/* bottom left */
*vertices++ = x0; *vertices++ = y1;
s = x0, t = y1;
switch (spans->shader.source.type.vertex) {
case VS_CONSTANT:
break;
case VS_LINEAR:
*vertices++ = i915_shader_linear_texcoord (&spans->shader.source.linear, s, t);
break;
case VS_TEXTURE:
cairo_matrix_transform_point (&spans->shader.source.base.matrix, &s, &t);
*vertices++ = s; *vertices++ = t;
break;
case VS_TEXTURE_16:
cairo_matrix_transform_point (&spans->shader.source.base.matrix, &s, &t);
*vertices++ = texcoord_2d_16 (s, t);
break;
}
*vertices++ = a;
if (spans->need_clip_surface) {
s = x0, t = y1;
cairo_matrix_transform_point (&spans->shader.clip.base.matrix, &s, &t);
*vertices++ = texcoord_2d_16 (s, t);
}
if (spans->shader.need_combine) {
s = x0, t = y1;
cairo_matrix_transform_point (&spans->shader.dst.base.matrix, &s, &t);
*vertices++ = texcoord_2d_16 (s, t);
}
/* top left */
*vertices++ = x0; *vertices++ = y0;
s = x0, t = y0;
switch (spans->shader.source.type.vertex) {
case VS_CONSTANT:
break;
case VS_LINEAR:
*vertices++ = i915_shader_linear_texcoord (&spans->shader.source.linear, s, t);
break;
case VS_TEXTURE:
cairo_matrix_transform_point (&spans->shader.source.base.matrix, &s, &t);
*vertices++ = s; *vertices++ = t;
break;
case VS_TEXTURE_16:
cairo_matrix_transform_point (&spans->shader.source.base.matrix, &s, &t);
*vertices++ = texcoord_2d_16 (s, t);
break;
}
*vertices++ = a;
if (spans->need_clip_surface) {
s = x0, t = y0;
cairo_matrix_transform_point (&spans->shader.clip.base.matrix, &s, &t);
*vertices++ = texcoord_2d_16 (s, t);
}
if (spans->shader.need_combine) {
s = x0, t = y0;
cairo_matrix_transform_point (&spans->shader.dst.base.matrix, &s, &t);
*vertices++ = texcoord_2d_16 (s, t);
}
}
static cairo_status_t
i915_bounded_spans_mono (void *abstract_renderer,
int y, int height,
const cairo_half_open_span_t *half,
unsigned num_spans)
{
i915_spans_t *spans = abstract_renderer;
if (num_spans == 0)
return CAIRO_STATUS_SUCCESS;
do {
if (half[0].coverage >= 128) {
spans->span (spans,
half[0].x, half[1].x,
y, y + height,
255);
}
half++;
} while (--num_spans > 1);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
i915_bounded_spans (void *abstract_renderer,
int y, int height,
const cairo_half_open_span_t *half,
unsigned num_spans)
{
i915_spans_t *spans = abstract_renderer;
if (num_spans == 0)
return CAIRO_STATUS_SUCCESS;
do {
if (half[0].coverage) {
spans->span (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
i915_unbounded_spans (void *abstract_renderer,
int y, int height,
const cairo_half_open_span_t *half,
unsigned num_spans)
{
i915_spans_t *spans = abstract_renderer;
if (num_spans == 0) {
spans->span (spans,
spans->xmin, spans->xmax,
y, y + height,
0);
return CAIRO_STATUS_SUCCESS;
}
if (half[0].x != spans->xmin) {
spans->span (spans,
spans->xmin, half[0].x,
y, y + height,
0);
}
do {
spans->span (spans,
half[0].x, half[1].x,
y, y + height,
half[0].coverage);
half++;
} while (--num_spans > 1);
if (half[0].x != spans->xmax) {
spans->span (spans,
half[0].x, spans->xmax,
y, y + height,
0);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
i915_unbounded_spans_mono (void *abstract_renderer,
int y, int height,
const cairo_half_open_span_t *half,
unsigned num_spans)
{
i915_spans_t *spans = abstract_renderer;
if (num_spans == 0) {
spans->span (spans,
spans->xmin, spans->xmax,
y, y + height,
0);
return CAIRO_STATUS_SUCCESS;
}
if (half[0].x != spans->xmin) {
spans->span (spans,
spans->xmin, half[0].x,
y, y + height,
0);
}
do {
int alpha = 0;
if (half[0].coverage >= 128)
alpha = 255;
spans->span (spans,
half[0].x, half[1].x,
y, y + height,
alpha);
half++;
} while (--num_spans > 1);
if (half[0].x != spans->xmax) {
spans->span (spans,
half[0].x, spans->xmax,
y, y + height,
0);
}
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
i915_spans_init (i915_spans_t *spans,
i915_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 = (i915_device_t *) dst->intel.drm.base.device;
spans->is_bounded = extents->is_bounded;
if (extents->is_bounded) {
if (antialias == CAIRO_ANTIALIAS_NONE)
spans->renderer.render_rows = i915_bounded_spans_mono;
else
spans->renderer.render_rows = i915_bounded_spans;
spans->extents = &extents->bounded;
} else {
if (antialias == CAIRO_ANTIALIAS_NONE)
spans->renderer.render_rows = i915_unbounded_spans_mono;
else
spans->renderer.render_rows = i915_unbounded_spans;
spans->extents = &extents->unbounded;
}
spans->xmin = spans->extents->x;
spans->xmax = spans->extents->x + spans->extents->width;
spans->clip_region = NULL;
spans->need_clip_surface = FALSE;
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;
spans->need_clip_surface = status == CAIRO_INT_STATUS_UNSUPPORTED;
}
spans->head.next = NULL;
spans->head.bo = NULL;
spans->head.count = 0;
spans->tail = &spans->head;
if (spans->clip_region == NULL) {
spans->get_rectangle = i915_emit_rectangle;
} else {
assert (! extents->is_bounded);
spans->get_rectangle = i915_accumulate_rectangle;
spans->head.bo = intel_bo_create (&spans->device->intel,
I915_VBO_SIZE, FALSE);
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;
i915_shader_init (&spans->shader, dst, op);
if (spans->need_clip_surface)
i915_shader_set_clip (&spans->shader, clip);
status = i915_shader_acquire_pattern (&spans->shader, &spans->shader.source,
pattern, &extents->bounded);
if (unlikely (status))
return status;
spans->rectangle_size = 3 * (2 + i915_shader_num_texcoords (&spans->shader));
return CAIRO_STATUS_SUCCESS;
}
static void
i915_spans_fini (i915_spans_t *spans)
{
i915_shader_fini (&spans->shader);
if (spans->tail->bo && spans->tail->bo->virtual)
intel_bo_unmap (spans->tail->bo);
if (spans->head.bo != NULL) {
struct 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
i915_clip_and_composite_spans (i915_surface_t *dst,
cairo_operator_t op,
const cairo_pattern_t *pattern,
cairo_antialias_t antialias,
i915_spans_func_t draw_func,
void *draw_closure,
const cairo_composite_rectangles_t*extents,
cairo_clip_t *clip)
{
i915_spans_t spans;
i915_device_t *device;
cairo_status_t status;
struct vbo *vbo;
if (op == CAIRO_OPERATOR_CLEAR) {
pattern = &_cairo_pattern_white.base;
op = CAIRO_OPERATOR_DEST_OUT;
}
status = i915_spans_init (&spans, dst, op, pattern, antialias, clip, extents);
if (unlikely (status))
return status;
spans.shader.mask.base.texfmt = TEXCOORDFMT_1D;
spans.shader.mask.base.content = CAIRO_CONTENT_ALPHA;
spans.shader.mask.type.fragment = FS_SPANS;
status = cairo_device_acquire (dst->intel.drm.base.device);
if (unlikely (status))
goto CLEANUP_SPANS;
device = i915_device (dst);
status = i915_shader_commit (&spans.shader, device);
if (unlikely (status))
goto CLEANUP_DEVICE;
if (!spans.shader.need_combine && ! spans.need_clip_surface) {
switch (spans.shader.source.type.vertex) {
case VS_CONSTANT:
spans.span = i915_span_constant;
break;
case VS_LINEAR:
spans.span = i915_span_linear;
break;
case VS_TEXTURE:
spans.span = i915_span_texture;
break;
case VS_TEXTURE_16:
spans.span = i915_span_texture16;
break;
default:
spans.span = i915_span_generic;
break;
}
} else {
spans.span = i915_span_generic;
}
status = draw_func (draw_closure, &spans.renderer, spans.extents);
if (spans.clip_region != NULL && status == CAIRO_STATUS_SUCCESS) {
intel_bo_unmap (spans.tail->bo);
i915_vbo_finish (device);
OUT_DWORD (_3DSTATE_SCISSOR_ENABLE_CMD | ENABLE_SCISSOR_RECT);
for (vbo = &spans.head; vbo != NULL; vbo = vbo->next) {
int i, num_rectangles;
/* XXX require_space & batch_flush */
OUT_DWORD (_3DSTATE_LOAD_STATE_IMMEDIATE_1 | I1_LOAD_S (0) | I1_LOAD_S (1) | 1);
i915_batch_emit_reloc (device, vbo->bo, 0,
I915_GEM_DOMAIN_VERTEX, 0);
OUT_DWORD ((device->floats_per_vertex << S1_VERTEX_WIDTH_SHIFT) |
(device->floats_per_vertex << S1_VERTEX_PITCH_SHIFT) |
vbo->count);
num_rectangles = cairo_region_num_rectangles (spans.clip_region);
for (i = 0; i < num_rectangles; i++) {
cairo_rectangle_int_t rect;
cairo_region_get_rectangle (spans.clip_region, i, &rect);
OUT_DWORD (_3DSTATE_SCISSOR_RECT_0_CMD);
OUT_DWORD (SCISSOR_RECT_0_XMIN (rect.x) |
SCISSOR_RECT_0_YMIN (rect.y));
OUT_DWORD (SCISSOR_RECT_0_XMAX (rect.x + rect.width) |
SCISSOR_RECT_0_YMAX (rect.y + rect.height));
OUT_DWORD (PRIM3D_RECTLIST | PRIM3D_INDIRECT_SEQUENTIAL | vbo->count);
OUT_DWORD (0);
}
}
OUT_DWORD (_3DSTATE_SCISSOR_ENABLE_CMD | DISABLE_SCISSOR_RECT);
}
CLEANUP_DEVICE:
cairo_device_release (dst->intel.drm.base.device);
CLEANUP_SPANS:
i915_spans_fini (&spans);
return status;
}
|