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