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     ret = TRUE;
114 
115     glDisable(GL_SCISSOR_TEST);
116     glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
117 
118 bail:
119     return ret;
120 }
121 
122 static Bool
glamor_poly_segment_gl(DrawablePtr drawable,GCPtr gc,int nseg,xSegment * segs)123 glamor_poly_segment_gl(DrawablePtr drawable, GCPtr gc,
124                        int nseg, xSegment *segs)
125 {
126     if (gc->lineWidth != 0)
127         return FALSE;
128 
129     switch (gc->lineStyle) {
130     case LineSolid:
131         return glamor_poly_segment_solid_gl(drawable, gc, nseg, segs);
132     case LineOnOffDash:
133         return glamor_poly_segment_dash_gl(drawable, gc, nseg, segs);
134     case LineDoubleDash:
135         if (gc->fillStyle == FillTiled)
136             return glamor_poly_segment_solid_gl(drawable, gc, nseg, segs);
137         else
138             return glamor_poly_segment_dash_gl(drawable, gc, nseg, segs);
139     default:
140         return FALSE;
141     }
142 }
143 
144 static void
glamor_poly_segment_bail(DrawablePtr drawable,GCPtr gc,int nseg,xSegment * segs)145 glamor_poly_segment_bail(DrawablePtr drawable, GCPtr gc,
146                          int nseg, xSegment *segs)
147 {
148     glamor_fallback("to %p (%c)\n", drawable,
149                     glamor_get_drawable_location(drawable));
150 
151     if (gc->lineWidth == 0) {
152         if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RW) &&
153             glamor_prepare_access_gc(gc)) {
154             fbPolySegment(drawable, gc, nseg, segs);
155         }
156         glamor_finish_access_gc(gc);
157         glamor_finish_access(drawable);
158     } else
159         miPolySegment(drawable, gc, nseg, segs);
160 }
161 
162 void
glamor_poly_segment(DrawablePtr drawable,GCPtr gc,int nseg,xSegment * segs)163 glamor_poly_segment(DrawablePtr drawable, GCPtr gc,
164                     int nseg, xSegment *segs)
165 {
166     if (glamor_poly_segment_gl(drawable, gc, nseg, segs))
167         return;
168 
169     glamor_poly_segment_bail(drawable, gc, nseg, segs);
170 }
171