xref: /OK3568_Linux_fs/external/xserver/glamor/glamor_spans.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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     glamor_pixmap_invalid(pixmap);
146 
147     ret = TRUE;
148 
149 bail:
150     glDisable(GL_SCISSOR_TEST);
151     if (glamor_priv->glsl_version >= 130)
152         glVertexAttribDivisor(GLAMOR_VERTEX_POS, 0);
153     glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
154 
155     return ret;
156 }
157 
158 static void
glamor_fill_spans_bail(DrawablePtr drawable,GCPtr gc,int n,DDXPointPtr points,int * widths,int sorted)159 glamor_fill_spans_bail(DrawablePtr drawable,
160                        GCPtr gc,
161                        int n, DDXPointPtr points, int *widths, int sorted)
162 {
163     if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RW) &&
164         glamor_prepare_access_gc(gc)) {
165         fbFillSpans(drawable, gc, n, points, widths, sorted);
166     }
167     glamor_finish_access_gc(gc);
168     glamor_finish_access(drawable);
169 }
170 
171 void
glamor_fill_spans(DrawablePtr drawable,GCPtr gc,int n,DDXPointPtr points,int * widths,int sorted)172 glamor_fill_spans(DrawablePtr drawable,
173                   GCPtr gc,
174                   int n, DDXPointPtr points, int *widths, int sorted)
175 {
176     if (GLAMOR_PREFER_GL() &&
177         glamor_fill_spans_gl(drawable, gc, n, points, widths, sorted))
178         return;
179     glamor_fill_spans_bail(drawable, gc, n, points, widths, sorted);
180 }
181 
182 static Bool
glamor_get_spans_gl(DrawablePtr drawable,int wmax,DDXPointPtr points,int * widths,int count,char * dst)183 glamor_get_spans_gl(DrawablePtr drawable, int wmax,
184                     DDXPointPtr points, int *widths, int count, char *dst)
185 {
186     ScreenPtr screen = drawable->pScreen;
187     glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
188     PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
189     glamor_pixmap_private *pixmap_priv;
190     int box_index;
191     int n;
192     char *d;
193     int off_x, off_y;
194     const struct glamor_format *f = glamor_format_for_pixmap(pixmap);
195 
196     pixmap_priv = glamor_get_pixmap_private(pixmap);
197     if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
198         goto bail;
199 
200     glamor_get_drawable_deltas(drawable, pixmap, &off_x, &off_y);
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,
238                          f->format, f->type, l);
239         }
240     }
241 
242     return TRUE;
243 bail:
244     return FALSE;
245 }
246 
247 static void
glamor_get_spans_bail(DrawablePtr drawable,int wmax,DDXPointPtr points,int * widths,int count,char * dst)248 glamor_get_spans_bail(DrawablePtr drawable, int wmax,
249                  DDXPointPtr points, int *widths, int count, char *dst)
250 {
251     if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RO))
252         fbGetSpans(drawable, wmax, points, widths, count, dst);
253     glamor_finish_access(drawable);
254 }
255 
256 void
glamor_get_spans(DrawablePtr drawable,int wmax,DDXPointPtr points,int * widths,int count,char * dst)257 glamor_get_spans(DrawablePtr drawable, int wmax,
258                  DDXPointPtr points, int *widths, int count, char *dst)
259 {
260     if (GLAMOR_PREFER_GL() &&
261         glamor_get_spans_gl(drawable, wmax, points, widths, count, dst))
262         return;
263     glamor_get_spans_bail(drawable, wmax, points, widths, count, dst);
264 }
265 
266 static Bool
glamor_set_spans_gl(DrawablePtr drawable,GCPtr gc,char * src,DDXPointPtr points,int * widths,int numPoints,int sorted)267 glamor_set_spans_gl(DrawablePtr drawable, GCPtr gc, char *src,
268                     DDXPointPtr points, int *widths, int numPoints, int sorted)
269 {
270     ScreenPtr screen = drawable->pScreen;
271     glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
272     PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
273     glamor_pixmap_private *pixmap_priv;
274     const struct glamor_format *f = glamor_format_for_pixmap(pixmap);
275     int box_index;
276     int n;
277     char *s;
278     int off_x, off_y;
279 
280     pixmap_priv = glamor_get_pixmap_private(pixmap);
281     if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
282         goto bail;
283 
284     if (gc->alu != GXcopy)
285         goto bail;
286 
287     if (!glamor_pm_is_solid(gc->depth, gc->planemask))
288         goto bail;
289 
290     glamor_get_drawable_deltas(drawable, pixmap, &off_x, &off_y);
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                                 f->format, f->type,
352                                 l);
353             }
354             s += PixmapBytePad(w, drawable->depth);
355         }
356     }
357 
358     glamor_pixmap_invalid(pixmap);
359 
360     return TRUE;
361 
362 bail:
363     return FALSE;
364 }
365 
366 static void
glamor_set_spans_bail(DrawablePtr drawable,GCPtr gc,char * src,DDXPointPtr points,int * widths,int numPoints,int sorted)367 glamor_set_spans_bail(DrawablePtr drawable, GCPtr gc, char *src,
368                       DDXPointPtr points, int *widths, int numPoints, int sorted)
369 {
370     if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RW) && glamor_prepare_access_gc(gc))
371         fbSetSpans(drawable, gc, src, points, widths, numPoints, sorted);
372     glamor_finish_access_gc(gc);
373     glamor_finish_access(drawable);
374 }
375 
376 void
glamor_set_spans(DrawablePtr drawable,GCPtr gc,char * src,DDXPointPtr points,int * widths,int numPoints,int sorted)377 glamor_set_spans(DrawablePtr drawable, GCPtr gc, char *src,
378                  DDXPointPtr points, int *widths, int numPoints, int sorted)
379 {
380     if (GLAMOR_PREFER_GL() &&
381         glamor_set_spans_gl(drawable, gc, src, points, widths, numPoints, sorted))
382         return;
383     glamor_set_spans_bail(drawable, gc, src, points, widths, numPoints, sorted);
384 }
385