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