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