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_lines = {
29     .name = "poly_lines",
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_lines_solid_gl(DrawablePtr drawable,GCPtr gc,int mode,int n,DDXPointPtr points)36 glamor_poly_lines_solid_gl(DrawablePtr drawable, GCPtr gc,
37                            int mode, int n, DDXPointPtr points)
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     DDXPointPtr 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     if (n < 2)
60         return TRUE;
61 
62     glamor_make_current(glamor_priv);
63 
64     prog = glamor_use_program_fill(pixmap, gc,
65                                    &glamor_priv->poly_line_program,
66                                    &glamor_facet_poly_lines);
67 
68     if (!prog)
69         goto bail;
70 
71     /* Set up the vertex buffers for the points */
72 
73     v = glamor_get_vbo_space(drawable->pScreen,
74                              (n + add_last) * sizeof (DDXPointRec),
75                              &vbo_offset);
76 
77     glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
78     glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_SHORT, GL_FALSE,
79                           sizeof (DDXPointRec), vbo_offset);
80 
81     if (mode == CoordModePrevious) {
82         int i;
83         DDXPointRec here = { 0, 0 };
84 
85         for (i = 0; i < n; i++) {
86             here.x += points[i].x;
87             here.y += points[i].y;
88             v[i] = here;
89         }
90     } else {
91         memcpy(v, points, n * sizeof (DDXPointRec));
92     }
93 
94     if (add_last) {
95         v[n].x = v[n-1].x + 1;
96         v[n].y = v[n-1].y;
97     }
98 
99     glamor_put_vbo_space(screen);
100 
101     glEnable(GL_SCISSOR_TEST);
102 
103     glamor_pixmap_loop(pixmap_priv, box_index) {
104         int nbox = RegionNumRects(gc->pCompositeClip);
105         BoxPtr box = RegionRects(gc->pCompositeClip);
106 
107         if (!glamor_set_destination_drawable(drawable, box_index, TRUE, TRUE,
108                                              prog->matrix_uniform, &off_x, &off_y))
109             goto bail;
110 
111         while (nbox--) {
112             glScissor(box->x1 + off_x,
113                       box->y1 + off_y,
114                       box->x2 - box->x1,
115                       box->y2 - box->y1);
116             box++;
117             glDrawArrays(GL_LINE_STRIP, 0, n + add_last);
118         }
119     }
120 
121     ret = TRUE;
122 
123 bail:
124     glDisable(GL_SCISSOR_TEST);
125     glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
126 
127     return ret;
128 }
129 
130 static Bool
glamor_poly_lines_gl(DrawablePtr drawable,GCPtr gc,int mode,int n,DDXPointPtr points)131 glamor_poly_lines_gl(DrawablePtr drawable, GCPtr gc,
132                      int mode, int n, DDXPointPtr points)
133 {
134     if (gc->lineWidth != 0)
135         return FALSE;
136 
137     switch (gc->lineStyle) {
138     case LineSolid:
139         return glamor_poly_lines_solid_gl(drawable, gc, mode, n, points);
140     case LineOnOffDash:
141         return glamor_poly_lines_dash_gl(drawable, gc, mode, n, points);
142     case LineDoubleDash:
143         if (gc->fillStyle == FillTiled)
144             return glamor_poly_lines_solid_gl(drawable, gc, mode, n, points);
145         else
146             return glamor_poly_lines_dash_gl(drawable, gc, mode, n, points);
147     default:
148         return FALSE;
149     }
150 }
151 
152 static void
glamor_poly_lines_bail(DrawablePtr drawable,GCPtr gc,int mode,int n,DDXPointPtr points)153 glamor_poly_lines_bail(DrawablePtr drawable, GCPtr gc,
154                        int mode, int n, DDXPointPtr points)
155 {
156     glamor_fallback("to %p (%c)\n", drawable,
157                     glamor_get_drawable_location(drawable));
158 
159     miPolylines(drawable, gc, mode, n, points);
160 }
161 
162 void
glamor_poly_lines(DrawablePtr drawable,GCPtr gc,int mode,int n,DDXPointPtr points)163 glamor_poly_lines(DrawablePtr drawable, GCPtr gc,
164                   int mode, int n, DDXPointPtr points)
165 {
166     if (glamor_poly_lines_gl(drawable, gc, mode, n, points))
167         return;
168     glamor_poly_lines_bail(drawable, gc, mode, n, points);
169 }
170