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