1 /*
2 * Copyright © 2014 Keith Packard
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23 #include "glamor_priv.h"
24 #include "glamor_transform.h"
25 #include "glamor_transfer.h"
26
27 glamor_program fill_spans_progs[4];
28
29 static const glamor_facet glamor_facet_fillspans_130 = {
30 .name = "fill_spans",
31 .version = 130,
32 .vs_vars = "attribute vec3 primitive;\n",
33 .vs_exec = (" vec2 pos = vec2(primitive.z,1) * vec2(gl_VertexID&1, (gl_VertexID&2)>>1);\n"
34 GLAMOR_POS(gl_Position, (primitive.xy + pos))),
35 };
36
37 static const glamor_facet glamor_facet_fillspans_120 = {
38 .name = "fill_spans",
39 .vs_vars = "attribute vec2 primitive;\n",
40 .vs_exec = (" vec2 pos = vec2(0,0);\n"
41 GLAMOR_POS(gl_Position, primitive.xy)),
42 };
43
44 static Bool
glamor_fill_spans_gl(DrawablePtr drawable,GCPtr gc,int n,DDXPointPtr points,int * widths,int sorted)45 glamor_fill_spans_gl(DrawablePtr drawable,
46 GCPtr gc,
47 int n, DDXPointPtr points, int *widths, int sorted)
48 {
49 ScreenPtr screen = drawable->pScreen;
50 glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
51 PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
52 glamor_pixmap_private *pixmap_priv;
53 glamor_program *prog;
54 int off_x, off_y;
55 GLshort *v;
56 char *vbo_offset;
57 int c;
58 int box_index;
59 Bool ret = FALSE;
60
61 pixmap_priv = glamor_get_pixmap_private(pixmap);
62 if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
63 goto bail;
64
65 glamor_make_current(glamor_priv);
66
67 if (glamor_priv->glsl_version >= 130) {
68 prog = glamor_use_program_fill(pixmap, gc, &glamor_priv->fill_spans_program,
69 &glamor_facet_fillspans_130);
70
71 if (!prog)
72 goto bail;
73
74 /* Set up the vertex buffers for the points */
75
76 v = glamor_get_vbo_space(drawable->pScreen, n * (4 * sizeof (GLshort)), &vbo_offset);
77
78 glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
79 glVertexAttribDivisor(GLAMOR_VERTEX_POS, 1);
80 glVertexAttribPointer(GLAMOR_VERTEX_POS, 3, GL_SHORT, GL_FALSE,
81 4 * sizeof (GLshort), vbo_offset);
82
83 for (c = 0; c < n; c++) {
84 v[0] = points->x;
85 v[1] = points->y;
86 v[2] = *widths++;
87 points++;
88 v += 4;
89 }
90
91 glamor_put_vbo_space(screen);
92 } else {
93 prog = glamor_use_program_fill(pixmap, gc, &glamor_priv->fill_spans_program,
94 &glamor_facet_fillspans_120);
95
96 if (!prog)
97 goto bail;
98
99 /* Set up the vertex buffers for the points */
100
101 v = glamor_get_vbo_space(drawable->pScreen, n * 8 * sizeof (short), &vbo_offset);
102
103 glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
104 glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_SHORT, GL_FALSE,
105 2 * sizeof (short), vbo_offset);
106
107 for (c = 0; c < n; c++) {
108 v[0] = points->x; v[1] = points->y;
109 v[2] = points->x; v[3] = points->y + 1;
110 v[4] = points->x + *widths; v[5] = points->y + 1;
111 v[6] = points->x + *widths; v[7] = points->y;
112
113 widths++;
114 points++;
115 v += 8;
116 }
117
118 glamor_put_vbo_space(screen);
119 }
120
121 glEnable(GL_SCISSOR_TEST);
122
123 glamor_pixmap_loop(pixmap_priv, box_index) {
124 int nbox = RegionNumRects(gc->pCompositeClip);
125 BoxPtr box = RegionRects(gc->pCompositeClip);
126
127 if (!glamor_set_destination_drawable(drawable, box_index, FALSE, FALSE,
128 prog->matrix_uniform, &off_x, &off_y))
129 goto bail;
130
131 while (nbox--) {
132 glScissor(box->x1 + off_x,
133 box->y1 + off_y,
134 box->x2 - box->x1,
135 box->y2 - box->y1);
136 box++;
137 if (glamor_priv->glsl_version >= 130)
138 glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, n);
139 else {
140 glamor_glDrawArrays_GL_QUADS(glamor_priv, n);
141 }
142 }
143 }
144
145 ret = TRUE;
146
147 bail:
148 glDisable(GL_SCISSOR_TEST);
149 if (glamor_priv->glsl_version >= 130)
150 glVertexAttribDivisor(GLAMOR_VERTEX_POS, 0);
151 glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
152
153 return ret;
154 }
155
156 static void
glamor_fill_spans_bail(DrawablePtr drawable,GCPtr gc,int n,DDXPointPtr points,int * widths,int sorted)157 glamor_fill_spans_bail(DrawablePtr drawable,
158 GCPtr gc,
159 int n, DDXPointPtr points, int *widths, int sorted)
160 {
161 if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RW) &&
162 glamor_prepare_access_gc(gc)) {
163 fbFillSpans(drawable, gc, n, points, widths, sorted);
164 }
165 glamor_finish_access_gc(gc);
166 glamor_finish_access(drawable);
167 }
168
169 void
glamor_fill_spans(DrawablePtr drawable,GCPtr gc,int n,DDXPointPtr points,int * widths,int sorted)170 glamor_fill_spans(DrawablePtr drawable,
171 GCPtr gc,
172 int n, DDXPointPtr points, int *widths, int sorted)
173 {
174 if (glamor_fill_spans_gl(drawable, gc, n, points, widths, sorted))
175 return;
176 glamor_fill_spans_bail(drawable, gc, n, points, widths, sorted);
177 }
178
179 static Bool
glamor_get_spans_gl(DrawablePtr drawable,int wmax,DDXPointPtr points,int * widths,int count,char * dst)180 glamor_get_spans_gl(DrawablePtr drawable, int wmax,
181 DDXPointPtr points, int *widths, int count, char *dst)
182 {
183 ScreenPtr screen = drawable->pScreen;
184 glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
185 PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
186 glamor_pixmap_private *pixmap_priv;
187 int box_index;
188 int n;
189 char *d;
190 GLenum type;
191 GLenum format;
192 int off_x, off_y;
193
194 pixmap_priv = glamor_get_pixmap_private(pixmap);
195 if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
196 goto bail;
197
198 glamor_get_drawable_deltas(drawable, pixmap, &off_x, &off_y);
199
200 glamor_format_for_pixmap(pixmap, &format, &type);
201
202 glamor_make_current(glamor_priv);
203
204 glamor_pixmap_loop(pixmap_priv, box_index) {
205 BoxPtr box = glamor_pixmap_box_at(pixmap_priv, box_index);
206 glamor_pixmap_fbo *fbo = glamor_pixmap_fbo_at(pixmap_priv, box_index);
207
208 glBindFramebuffer(GL_FRAMEBUFFER, fbo->fb);
209 glPixelStorei(GL_PACK_ALIGNMENT, 4);
210
211 d = dst;
212 for (n = 0; n < count; n++) {
213 int x1 = points[n].x + off_x;
214 int y = points[n].y + off_y;
215 int w = widths[n];
216 int x2 = x1 + w;
217 char *l;
218
219 l = d;
220 d += PixmapBytePad(w, drawable->depth);
221
222 /* clip */
223 if (x1 < box->x1) {
224 l += (box->x1 - x1) * (drawable->bitsPerPixel >> 3);
225 x1 = box->x1;
226 }
227 if (x2 > box->x2)
228 x2 = box->x2;
229
230 if (x1 >= x2)
231 continue;
232 if (y < box->y1)
233 continue;
234 if (y >= box->y2)
235 continue;
236
237 glReadPixels(x1 - box->x1, y - box->y1, x2 - x1, 1, format, type, l);
238 }
239 }
240
241 return TRUE;
242 bail:
243 return FALSE;
244 }
245
246 static void
glamor_get_spans_bail(DrawablePtr drawable,int wmax,DDXPointPtr points,int * widths,int count,char * dst)247 glamor_get_spans_bail(DrawablePtr drawable, int wmax,
248 DDXPointPtr points, int *widths, int count, char *dst)
249 {
250 if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RO))
251 fbGetSpans(drawable, wmax, points, widths, count, dst);
252 glamor_finish_access(drawable);
253 }
254
255 void
glamor_get_spans(DrawablePtr drawable,int wmax,DDXPointPtr points,int * widths,int count,char * dst)256 glamor_get_spans(DrawablePtr drawable, int wmax,
257 DDXPointPtr points, int *widths, int count, char *dst)
258 {
259 if (glamor_get_spans_gl(drawable, wmax, points, widths, count, dst))
260 return;
261 glamor_get_spans_bail(drawable, wmax, points, widths, count, dst);
262 }
263
264 static Bool
glamor_set_spans_gl(DrawablePtr drawable,GCPtr gc,char * src,DDXPointPtr points,int * widths,int numPoints,int sorted)265 glamor_set_spans_gl(DrawablePtr drawable, GCPtr gc, char *src,
266 DDXPointPtr points, int *widths, int numPoints, int sorted)
267 {
268 ScreenPtr screen = drawable->pScreen;
269 glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
270 PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
271 glamor_pixmap_private *pixmap_priv;
272 int box_index;
273 int n;
274 char *s;
275 GLenum type;
276 GLenum format;
277 int off_x, off_y;
278
279 pixmap_priv = glamor_get_pixmap_private(pixmap);
280 if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
281 goto bail;
282
283 if (gc->alu != GXcopy)
284 goto bail;
285
286 if (!glamor_pm_is_solid(gc->depth, gc->planemask))
287 goto bail;
288
289 glamor_get_drawable_deltas(drawable, pixmap, &off_x, &off_y);
290 glamor_format_for_pixmap(pixmap, &format, &type);
291
292 glamor_make_current(glamor_priv);
293
294 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
295
296 glamor_pixmap_loop(pixmap_priv, box_index) {
297 BoxPtr box = glamor_pixmap_box_at(pixmap_priv, box_index);
298 glamor_pixmap_fbo *fbo = glamor_pixmap_fbo_at(pixmap_priv, box_index);
299
300 glamor_bind_texture(glamor_priv, GL_TEXTURE0, fbo, TRUE);
301
302 s = src;
303 for (n = 0; n < numPoints; n++) {
304
305 BoxPtr clip_box = RegionRects(gc->pCompositeClip);
306 int nclip_box = RegionNumRects(gc->pCompositeClip);
307 int w = widths[n];
308 int y = points[n].y;
309 int x = points[n].x;
310
311 while (nclip_box--) {
312 int x1 = x;
313 int x2 = x + w;
314 int y1 = y;
315 char *l = s;
316
317 /* clip to composite clip */
318 if (x1 < clip_box->x1) {
319 l += (clip_box->x1 - x1) * (drawable->bitsPerPixel >> 3);
320 x1 = clip_box->x1;
321 }
322 if (x2 > clip_box->x2)
323 x2 = clip_box->x2;
324
325 if (y < clip_box->y1)
326 continue;
327 if (y >= clip_box->y2)
328 continue;
329
330 /* adjust to pixmap coordinates */
331 x1 += off_x;
332 x2 += off_x;
333 y1 += off_y;
334
335 if (x1 < box->x1) {
336 l += (box->x1 - x1) * (drawable->bitsPerPixel >> 3);
337 x1 = box->x1;
338 }
339 if (x2 > box->x2)
340 x2 = box->x2;
341
342 if (x1 >= x2)
343 continue;
344 if (y1 < box->y1)
345 continue;
346 if (y1 >= box->y2)
347 continue;
348
349 glTexSubImage2D(GL_TEXTURE_2D, 0,
350 x1 - box->x1, y1 - box->y1, x2 - x1, 1,
351 format, type,
352 l);
353 }
354 s += PixmapBytePad(w, drawable->depth);
355 }
356 }
357
358 return TRUE;
359
360 bail:
361 return FALSE;
362 }
363
364 static void
glamor_set_spans_bail(DrawablePtr drawable,GCPtr gc,char * src,DDXPointPtr points,int * widths,int numPoints,int sorted)365 glamor_set_spans_bail(DrawablePtr drawable, GCPtr gc, char *src,
366 DDXPointPtr points, int *widths, int numPoints, int sorted)
367 {
368 if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RW) && glamor_prepare_access_gc(gc))
369 fbSetSpans(drawable, gc, src, points, widths, numPoints, sorted);
370 glamor_finish_access_gc(gc);
371 glamor_finish_access(drawable);
372 }
373
374 void
glamor_set_spans(DrawablePtr drawable,GCPtr gc,char * src,DDXPointPtr points,int * widths,int numPoints,int sorted)375 glamor_set_spans(DrawablePtr drawable, GCPtr gc, char *src,
376 DDXPointPtr points, int *widths, int numPoints, int sorted)
377 {
378 if (glamor_set_spans_gl(drawable, gc, src, points, widths, numPoints, sorted))
379 return;
380 glamor_set_spans_bail(drawable, gc, src, points, widths, numPoints, sorted);
381 }
382