xref: /OK3568_Linux_fs/external/xserver/glamor/glamor_rects.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright © 2014 Keith Packard
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Permission to use, copy, modify, distribute, and sell this software and its
5*4882a593Smuzhiyun  * documentation for any purpose is hereby granted without fee, provided that
6*4882a593Smuzhiyun  * the above copyright notice appear in all copies and that both that copyright
7*4882a593Smuzhiyun  * notice and this permission notice appear in supporting documentation, and
8*4882a593Smuzhiyun  * that the name of the copyright holders not be used in advertising or
9*4882a593Smuzhiyun  * publicity pertaining to distribution of the software without specific,
10*4882a593Smuzhiyun  * written prior permission.  The copyright holders make no representations
11*4882a593Smuzhiyun  * about the suitability of this software for any purpose.  It is provided "as
12*4882a593Smuzhiyun  * is" without express or implied warranty.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15*4882a593Smuzhiyun  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16*4882a593Smuzhiyun  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17*4882a593Smuzhiyun  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18*4882a593Smuzhiyun  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19*4882a593Smuzhiyun  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20*4882a593Smuzhiyun  * OF THIS SOFTWARE.
21*4882a593Smuzhiyun  */
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include "glamor_priv.h"
24*4882a593Smuzhiyun #include "glamor_program.h"
25*4882a593Smuzhiyun #include "glamor_transform.h"
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun static const glamor_facet glamor_facet_polyfillrect_130 = {
28*4882a593Smuzhiyun     .name = "poly_fill_rect",
29*4882a593Smuzhiyun     .version = 130,
30*4882a593Smuzhiyun     .source_name = "size",
31*4882a593Smuzhiyun     .vs_vars = "attribute vec2 primitive;\n"
32*4882a593Smuzhiyun                "attribute vec2 size;\n",
33*4882a593Smuzhiyun     .vs_exec = ("       vec2 pos = size * vec2(gl_VertexID&1, (gl_VertexID&2)>>1);\n"
34*4882a593Smuzhiyun                 GLAMOR_POS(gl_Position, (primitive.xy + pos))),
35*4882a593Smuzhiyun };
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun static const glamor_facet glamor_facet_polyfillrect_120 = {
38*4882a593Smuzhiyun     .name = "poly_fill_rect",
39*4882a593Smuzhiyun     .vs_vars = "attribute vec2 primitive;\n",
40*4882a593Smuzhiyun     .vs_exec = ("        vec2 pos = vec2(0,0);\n"
41*4882a593Smuzhiyun                 GLAMOR_POS(gl_Position, primitive.xy)),
42*4882a593Smuzhiyun };
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun static Bool
glamor_poly_fill_rect_gl(DrawablePtr drawable,GCPtr gc,int nrect,xRectangle * prect)45*4882a593Smuzhiyun glamor_poly_fill_rect_gl(DrawablePtr drawable,
46*4882a593Smuzhiyun                          GCPtr gc, int nrect, xRectangle *prect)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun     ScreenPtr screen = drawable->pScreen;
49*4882a593Smuzhiyun     glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
50*4882a593Smuzhiyun     PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
51*4882a593Smuzhiyun     glamor_pixmap_private *pixmap_priv;
52*4882a593Smuzhiyun     glamor_program *prog;
53*4882a593Smuzhiyun     int off_x, off_y;
54*4882a593Smuzhiyun     GLshort *v;
55*4882a593Smuzhiyun     char *vbo_offset;
56*4882a593Smuzhiyun     int box_index;
57*4882a593Smuzhiyun     Bool ret = FALSE;
58*4882a593Smuzhiyun     BoxRec bounds = glamor_no_rendering_bounds();
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun     pixmap_priv = glamor_get_pixmap_private(pixmap);
61*4882a593Smuzhiyun     if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
62*4882a593Smuzhiyun         goto bail;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun     glamor_make_current(glamor_priv);
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun     if (nrect < 100) {
67*4882a593Smuzhiyun         bounds = glamor_start_rendering_bounds();
68*4882a593Smuzhiyun         for (int i = 0; i < nrect; i++)
69*4882a593Smuzhiyun             glamor_bounds_union_rect(&bounds, &prect[i]);
70*4882a593Smuzhiyun     }
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun     if (glamor_priv->glsl_version >= 130) {
73*4882a593Smuzhiyun         prog = glamor_use_program_fill(pixmap, gc,
74*4882a593Smuzhiyun                                        &glamor_priv->poly_fill_rect_program,
75*4882a593Smuzhiyun                                        &glamor_facet_polyfillrect_130);
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun         if (!prog)
78*4882a593Smuzhiyun             goto bail;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun         /* Set up the vertex buffers for the points */
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun         v = glamor_get_vbo_space(drawable->pScreen, nrect * sizeof (xRectangle), &vbo_offset);
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun         glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
85*4882a593Smuzhiyun         glVertexAttribDivisor(GLAMOR_VERTEX_POS, 1);
86*4882a593Smuzhiyun         glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_SHORT, GL_FALSE,
87*4882a593Smuzhiyun                               4 * sizeof (short), vbo_offset);
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun         glEnableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
90*4882a593Smuzhiyun         glVertexAttribDivisor(GLAMOR_VERTEX_SOURCE, 1);
91*4882a593Smuzhiyun         glVertexAttribPointer(GLAMOR_VERTEX_SOURCE, 2, GL_UNSIGNED_SHORT, GL_FALSE,
92*4882a593Smuzhiyun                               4 * sizeof (short), vbo_offset + 2 * sizeof (short));
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun         memcpy(v, prect, nrect * sizeof (xRectangle));
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun         glamor_put_vbo_space(screen);
97*4882a593Smuzhiyun     } else {
98*4882a593Smuzhiyun         int n;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun         prog = glamor_use_program_fill(pixmap, gc,
101*4882a593Smuzhiyun                                        &glamor_priv->poly_fill_rect_program,
102*4882a593Smuzhiyun                                        &glamor_facet_polyfillrect_120);
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun         if (!prog)
105*4882a593Smuzhiyun             goto bail;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun         /* Set up the vertex buffers for the points */
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun         v = glamor_get_vbo_space(drawable->pScreen, nrect * 8 * sizeof (short), &vbo_offset);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun         glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
112*4882a593Smuzhiyun         glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_SHORT, GL_FALSE,
113*4882a593Smuzhiyun                               2 * sizeof (short), vbo_offset);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun         for (n = 0; n < nrect; n++) {
116*4882a593Smuzhiyun             v[0] = prect->x;                v[1] = prect->y;
117*4882a593Smuzhiyun             v[2] = prect->x;                v[3] = prect->y + prect->height;
118*4882a593Smuzhiyun             v[4] = prect->x + prect->width; v[5] = prect->y + prect->height;
119*4882a593Smuzhiyun             v[6] = prect->x + prect->width; v[7] = prect->y;
120*4882a593Smuzhiyun             prect++;
121*4882a593Smuzhiyun             v += 8;
122*4882a593Smuzhiyun         }
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun         glamor_put_vbo_space(screen);
125*4882a593Smuzhiyun     }
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun     glEnable(GL_SCISSOR_TEST);
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun     glamor_pixmap_loop(pixmap_priv, box_index) {
130*4882a593Smuzhiyun         int nbox = RegionNumRects(gc->pCompositeClip);
131*4882a593Smuzhiyun         BoxPtr box = RegionRects(gc->pCompositeClip);
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun         if (!glamor_set_destination_drawable(drawable, box_index, TRUE, FALSE,
134*4882a593Smuzhiyun                                              prog->matrix_uniform, &off_x, &off_y))
135*4882a593Smuzhiyun             goto bail;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun         while (nbox--) {
138*4882a593Smuzhiyun             BoxRec scissor = {
139*4882a593Smuzhiyun                 .x1 = max(box->x1, bounds.x1 + drawable->x),
140*4882a593Smuzhiyun                 .y1 = max(box->y1, bounds.y1 + drawable->y),
141*4882a593Smuzhiyun                 .x2 = min(box->x2, bounds.x2 + drawable->x),
142*4882a593Smuzhiyun                 .y2 = min(box->y2, bounds.y2 + drawable->y),
143*4882a593Smuzhiyun             };
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun             box++;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun             if (scissor.x1 >= scissor.x2 || scissor.y1 >= scissor.y2)
148*4882a593Smuzhiyun                 continue;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun             glScissor(scissor.x1 + off_x,
151*4882a593Smuzhiyun                       scissor.y1 + off_y,
152*4882a593Smuzhiyun                       scissor.x2 - scissor.x1,
153*4882a593Smuzhiyun                       scissor.y2 - scissor.y1);
154*4882a593Smuzhiyun             if (glamor_priv->glsl_version >= 130)
155*4882a593Smuzhiyun                 glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, nrect);
156*4882a593Smuzhiyun             else {
157*4882a593Smuzhiyun                 glamor_glDrawArrays_GL_QUADS(glamor_priv, nrect);
158*4882a593Smuzhiyun             }
159*4882a593Smuzhiyun         }
160*4882a593Smuzhiyun     }
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun     glamor_pixmap_invalid(pixmap);
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun     ret = TRUE;
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun bail:
167*4882a593Smuzhiyun     glDisable(GL_SCISSOR_TEST);
168*4882a593Smuzhiyun     if (glamor_priv->glsl_version >= 130) {
169*4882a593Smuzhiyun         glVertexAttribDivisor(GLAMOR_VERTEX_SOURCE, 0);
170*4882a593Smuzhiyun         glDisableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
171*4882a593Smuzhiyun         glVertexAttribDivisor(GLAMOR_VERTEX_POS, 0);
172*4882a593Smuzhiyun     }
173*4882a593Smuzhiyun     glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun     return ret;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun static void
glamor_poly_fill_rect_bail(DrawablePtr drawable,GCPtr gc,int nrect,xRectangle * prect)179*4882a593Smuzhiyun glamor_poly_fill_rect_bail(DrawablePtr drawable,
180*4882a593Smuzhiyun                            GCPtr gc, int nrect, xRectangle *prect)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun     glamor_fallback("to %p (%c)\n", drawable,
183*4882a593Smuzhiyun                     glamor_get_drawable_location(drawable));
184*4882a593Smuzhiyun     if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RW) &&
185*4882a593Smuzhiyun         glamor_prepare_access_gc(gc)) {
186*4882a593Smuzhiyun         fbPolyFillRect(drawable, gc, nrect, prect);
187*4882a593Smuzhiyun     }
188*4882a593Smuzhiyun     glamor_finish_access_gc(gc);
189*4882a593Smuzhiyun     glamor_finish_access(drawable);
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun void
glamor_poly_fill_rect(DrawablePtr drawable,GCPtr gc,int nrect,xRectangle * prect)193*4882a593Smuzhiyun glamor_poly_fill_rect(DrawablePtr drawable,
194*4882a593Smuzhiyun                       GCPtr gc, int nrect, xRectangle *prect)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun     if (GLAMOR_PREFER_GL() &&
197*4882a593Smuzhiyun         glamor_poly_fill_rect_gl(drawable, gc, nrect, prect))
198*4882a593Smuzhiyun         return;
199*4882a593Smuzhiyun     glamor_poly_fill_rect_bail(drawable, gc, nrect, prect);
200*4882a593Smuzhiyun }
201