xref: /OK3568_Linux_fs/external/xserver/glamor/glamor_segs.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 #include "glamor_prepare.h"
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun static const glamor_facet glamor_facet_poly_segment = {
29*4882a593Smuzhiyun     .name = "poly_segment",
30*4882a593Smuzhiyun     .vs_vars = "attribute vec2 primitive;\n",
31*4882a593Smuzhiyun     .vs_exec = ("       vec2 pos = vec2(0.0,0.0);\n"
32*4882a593Smuzhiyun                 GLAMOR_POS(gl_Position, primitive.xy)),
33*4882a593Smuzhiyun };
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun static Bool
glamor_poly_segment_solid_gl(DrawablePtr drawable,GCPtr gc,int nseg,xSegment * segs)36*4882a593Smuzhiyun glamor_poly_segment_solid_gl(DrawablePtr drawable, GCPtr gc,
37*4882a593Smuzhiyun                              int nseg, xSegment *segs)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun     ScreenPtr screen = drawable->pScreen;
40*4882a593Smuzhiyun     glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
41*4882a593Smuzhiyun     PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
42*4882a593Smuzhiyun     glamor_pixmap_private *pixmap_priv;
43*4882a593Smuzhiyun     glamor_program *prog;
44*4882a593Smuzhiyun     int off_x, off_y;
45*4882a593Smuzhiyun     xSegment *v;
46*4882a593Smuzhiyun     char *vbo_offset;
47*4882a593Smuzhiyun     int box_index;
48*4882a593Smuzhiyun     int add_last;
49*4882a593Smuzhiyun     Bool ret = FALSE;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun     pixmap_priv = glamor_get_pixmap_private(pixmap);
52*4882a593Smuzhiyun     if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
53*4882a593Smuzhiyun         goto bail;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun     add_last = 0;
56*4882a593Smuzhiyun     if (gc->capStyle != CapNotLast)
57*4882a593Smuzhiyun         add_last = 1;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun     glamor_make_current(glamor_priv);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun     prog = glamor_use_program_fill(pixmap, gc,
62*4882a593Smuzhiyun                                    &glamor_priv->poly_segment_program,
63*4882a593Smuzhiyun                                    &glamor_facet_poly_segment);
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun     if (!prog)
66*4882a593Smuzhiyun         goto bail;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun     /* Set up the vertex buffers for the points */
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun     v = glamor_get_vbo_space(drawable->pScreen,
71*4882a593Smuzhiyun                              (nseg << add_last) * sizeof (xSegment),
72*4882a593Smuzhiyun                              &vbo_offset);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun     glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
75*4882a593Smuzhiyun     glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_SHORT, GL_FALSE,
76*4882a593Smuzhiyun                           sizeof(DDXPointRec), vbo_offset);
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun     if (add_last) {
79*4882a593Smuzhiyun         int i, j;
80*4882a593Smuzhiyun         for (i = 0, j=0; i < nseg; i++) {
81*4882a593Smuzhiyun             v[j++] = segs[i];
82*4882a593Smuzhiyun             v[j].x1 = segs[i].x2;
83*4882a593Smuzhiyun             v[j].y1 = segs[i].y2;
84*4882a593Smuzhiyun             v[j].x2 = segs[i].x2+1;
85*4882a593Smuzhiyun             v[j].y2 = segs[i].y2;
86*4882a593Smuzhiyun             j++;
87*4882a593Smuzhiyun         }
88*4882a593Smuzhiyun     } else
89*4882a593Smuzhiyun         memcpy(v, segs, nseg * sizeof (xSegment));
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun     glamor_put_vbo_space(screen);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun     glEnable(GL_SCISSOR_TEST);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun     glamor_pixmap_loop(pixmap_priv, box_index) {
96*4882a593Smuzhiyun         int nbox = RegionNumRects(gc->pCompositeClip);
97*4882a593Smuzhiyun         BoxPtr box = RegionRects(gc->pCompositeClip);
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun         if (!glamor_set_destination_drawable(drawable, box_index, TRUE, TRUE,
100*4882a593Smuzhiyun                                              prog->matrix_uniform, &off_x, &off_y))
101*4882a593Smuzhiyun             goto bail;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun         while (nbox--) {
104*4882a593Smuzhiyun             glScissor(box->x1 + off_x,
105*4882a593Smuzhiyun                       box->y1 + off_y,
106*4882a593Smuzhiyun                       box->x2 - box->x1,
107*4882a593Smuzhiyun                       box->y2 - box->y1);
108*4882a593Smuzhiyun             box++;
109*4882a593Smuzhiyun             glDrawArrays(GL_LINES, 0, nseg << (1 + add_last));
110*4882a593Smuzhiyun         }
111*4882a593Smuzhiyun     }
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun     glamor_pixmap_invalid(pixmap);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun     ret = TRUE;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun     glDisable(GL_SCISSOR_TEST);
118*4882a593Smuzhiyun     glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun bail:
121*4882a593Smuzhiyun     return ret;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun static Bool
glamor_poly_segment_gl(DrawablePtr drawable,GCPtr gc,int nseg,xSegment * segs)125*4882a593Smuzhiyun glamor_poly_segment_gl(DrawablePtr drawable, GCPtr gc,
126*4882a593Smuzhiyun                        int nseg, xSegment *segs)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun     if (gc->lineWidth != 0)
129*4882a593Smuzhiyun         return FALSE;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun     switch (gc->lineStyle) {
132*4882a593Smuzhiyun     case LineSolid:
133*4882a593Smuzhiyun         return glamor_poly_segment_solid_gl(drawable, gc, nseg, segs);
134*4882a593Smuzhiyun     case LineOnOffDash:
135*4882a593Smuzhiyun         return glamor_poly_segment_dash_gl(drawable, gc, nseg, segs);
136*4882a593Smuzhiyun     case LineDoubleDash:
137*4882a593Smuzhiyun         if (gc->fillStyle == FillTiled)
138*4882a593Smuzhiyun             return glamor_poly_segment_solid_gl(drawable, gc, nseg, segs);
139*4882a593Smuzhiyun         else
140*4882a593Smuzhiyun             return glamor_poly_segment_dash_gl(drawable, gc, nseg, segs);
141*4882a593Smuzhiyun     default:
142*4882a593Smuzhiyun         return FALSE;
143*4882a593Smuzhiyun     }
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun static void
glamor_poly_segment_bail(DrawablePtr drawable,GCPtr gc,int nseg,xSegment * segs)147*4882a593Smuzhiyun glamor_poly_segment_bail(DrawablePtr drawable, GCPtr gc,
148*4882a593Smuzhiyun                          int nseg, xSegment *segs)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun     glamor_fallback("to %p (%c)\n", drawable,
151*4882a593Smuzhiyun                     glamor_get_drawable_location(drawable));
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun     if (gc->lineWidth == 0) {
154*4882a593Smuzhiyun         if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RW) &&
155*4882a593Smuzhiyun             glamor_prepare_access_gc(gc)) {
156*4882a593Smuzhiyun             fbPolySegment(drawable, gc, nseg, segs);
157*4882a593Smuzhiyun         }
158*4882a593Smuzhiyun         glamor_finish_access_gc(gc);
159*4882a593Smuzhiyun         glamor_finish_access(drawable);
160*4882a593Smuzhiyun     } else
161*4882a593Smuzhiyun         miPolySegment(drawable, gc, nseg, segs);
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun void
glamor_poly_segment(DrawablePtr drawable,GCPtr gc,int nseg,xSegment * segs)165*4882a593Smuzhiyun glamor_poly_segment(DrawablePtr drawable, GCPtr gc,
166*4882a593Smuzhiyun                     int nseg, xSegment *segs)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun     if (GLAMOR_PREFER_GL() &&
169*4882a593Smuzhiyun         glamor_poly_segment_gl(drawable, gc, nseg, segs))
170*4882a593Smuzhiyun         return;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun     glamor_poly_segment_bail(drawable, gc, nseg, segs);
173*4882a593Smuzhiyun }
174