summaryrefslogtreecommitdiff
path: root/glamor/glamor_dash.c
blob: e8f60fa104c13a1c7cc2a9dd682bc350cd0b96c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
 * Copyright © 2014 Keith Packard
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#include "glamor_priv.h"
#include "glamor_program.h"
#include "glamor_transform.h"
#include "glamor_transfer.h"
#include "glamor_prepare.h"

static const char dash_vs_vars[] =
    "attribute vec3 primitive;\n"
    "varying float dash_offset;\n";

static const char dash_vs_exec[] =
    "       dash_offset = primitive.z / dash_length;\n"
    GLAMOR_POS(gl_Position, primitive.xy);

static const char dash_fs_vars[] =
    "varying float dash_offset;\n";

static const char on_off_fs_exec[] =
    "       float pattern = texture2D(dash, vec2(dash_offset, 0.5)).w;\n"
    "       if (pattern == 0.0)\n"
    "               discard;\n";

/* XXX deal with stippled double dashed lines once we have stippling support */
static const char double_fs_exec[] =
    "       float pattern = texture2D(dash, vec2(dash_offset, 0.5)).w;\n"
    "       if (pattern == 0.0)\n"
    "               gl_FragColor = bg;\n"
    "       else\n"
    "               gl_FragColor = fg;\n";


static const glamor_facet glamor_facet_on_off_dash_lines = {
    .version = 130,
    .name = "poly_lines_on_off_dash",
    .vs_vars = dash_vs_vars,
    .vs_exec = dash_vs_exec,
    .fs_vars = dash_fs_vars,
    .fs_exec = on_off_fs_exec,
    .locations = glamor_program_location_dash,
};

static const glamor_facet glamor_facet_double_dash_lines = {
    .version = 130,
    .name = "poly_lines_double_dash",
    .vs_vars = dash_vs_vars,
    .vs_exec = dash_vs_exec,
    .fs_vars = dash_fs_vars,
    .fs_exec = double_fs_exec,
    .locations = (glamor_program_location_dash|
                  glamor_program_location_fg|
                  glamor_program_location_bg),
};

static PixmapPtr
glamor_get_dash_pixmap(GCPtr gc)
{
    glamor_gc_private *gc_priv = glamor_get_gc_private(gc);
    ScreenPtr   screen = gc->pScreen;
    PixmapPtr   pixmap;
    int         offset;
    int         d;
    uint32_t    pixel;
    GCPtr       scratch_gc;

    if (gc_priv->dash)
        return gc_priv->dash;

    offset = 0;
    for (d = 0; d < gc->numInDashList; d++)
        offset += gc->dash[d];

    pixmap = glamor_create_pixmap(screen, offset, 1, 8, 0);
    if (!pixmap)
        goto bail;

    scratch_gc = GetScratchGC(8, screen);
    if (!scratch_gc)
        goto bail_pixmap;

    pixel = 0xffffffff;
    offset = 0;
    for (d = 0; d < gc->numInDashList; d++) {
        xRectangle      rect;
        ChangeGCVal     changes;

        changes.val = pixel;
        (void) ChangeGC(NullClient, scratch_gc,
                        GCForeground, &changes);
        ValidateGC(&pixmap->drawable, scratch_gc);
        rect.x = offset;
        rect.y = 0;
        rect.width = gc->dash[d];
        rect.height = 1;
        scratch_gc->ops->PolyFillRect (&pixmap->drawable, scratch_gc, 1, &rect);
        offset += gc->dash[d];
        pixel = ~pixel;
    }
    FreeScratchGC(scratch_gc);

    gc_priv->dash = pixmap;
    return pixmap;

bail_pixmap:
    glamor_destroy_pixmap(pixmap);
bail:
    return NULL;
}

static glamor_program *
glamor_dash_setup(DrawablePtr drawable, GCPtr gc)
{
    ScreenPtr screen = drawable->pScreen;
    glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
    PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
    glamor_pixmap_private *pixmap_priv = glamor_get_pixmap_private(pixmap);
    PixmapPtr dash_pixmap;
    glamor_pixmap_private *dash_priv;
    glamor_program *prog;

    if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
        goto bail;

    if (gc->lineWidth != 0)
        goto bail;

    dash_pixmap = glamor_get_dash_pixmap(gc);
    dash_priv = glamor_get_pixmap_private(pixmap);

    if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(dash_priv))
        goto bail;

    glamor_make_current(glamor_priv);

    switch (gc->lineStyle) {
    case LineOnOffDash:
        prog = glamor_use_program_fill(pixmap, gc,
                                       &glamor_priv->on_off_dash_line_progs,
                                       &glamor_facet_on_off_dash_lines);
        if (!prog)
            goto bail_ctx;
        break;
    case LineDoubleDash:
        if (gc->fillStyle != FillSolid)
            goto bail_ctx;

        prog = &glamor_priv->double_dash_line_prog;

        if (!prog->prog) {
            if (!glamor_build_program(screen, prog,
                                      &glamor_facet_double_dash_lines,
                                      NULL))
                goto bail_ctx;
        }

        if (!glamor_use_program(pixmap, gc, prog, NULL))
            goto bail_ctx;

        glamor_set_color(pixmap, gc->fgPixel, prog->fg_uniform);
        glamor_set_color(pixmap, gc->bgPixel, prog->bg_uniform);
        break;

    default:
        goto bail_ctx;
    }


    /* Set the dash pattern as texture 1 */

    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, dash_priv->base.fbo->tex);
    glUniform1i(prog->dash_uniform, 1);
    glUniform1f(prog->dash_length_uniform, dash_pixmap->drawable.width);

    return prog;

bail_ctx:
    glDisable(GL_COLOR_LOGIC_OP);
bail:
    return NULL;
}

static void
glamor_dash_loop(DrawablePtr drawable, GCPtr gc, glamor_program *prog,
                 int n, GLenum mode)
{
    PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
    glamor_pixmap_private *pixmap_priv = glamor_get_pixmap_private(pixmap);
    int box_x, box_y;
    int off_x, off_y;

    glEnable(GL_SCISSOR_TEST);

    glamor_pixmap_loop(pixmap_priv, box_x, box_y) {
        int nbox = RegionNumRects(gc->pCompositeClip);
        BoxPtr box = RegionRects(gc->pCompositeClip);

        glamor_set_destination_drawable(drawable, box_x, box_y, TRUE, TRUE,
                                        prog->matrix_uniform, &off_x, &off_y);

        while (nbox--) {
            glScissor(box->x1 + off_x,
                      box->y1 + off_y,
                      box->x2 - box->x1,
                      box->y2 - box->y1);
            box++;
            glDrawArrays(mode, 0, n);
        }
    }

    glDisable(GL_SCISSOR_TEST);
    glDisable(GL_COLOR_LOGIC_OP);
    glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
}

static int
glamor_line_length(short x1, short y1, short x2, short y2)
{
    return max(abs(x2 - x1), abs(y2 - y1));
}

Bool
glamor_poly_lines_dash_gl(DrawablePtr drawable, GCPtr gc,
                          int mode, int n, DDXPointPtr points)
{
    ScreenPtr screen = drawable->pScreen;
    glamor_program *prog;
    short *v;
    char *vbo_offset;
    int add_last;
    int dash_pos;
    int prev_x, prev_y;
    int i;

    if (n < 2)
        return TRUE;

    if (!(prog = glamor_dash_setup(drawable, gc)))
        return FALSE;

    add_last = 0;
    if (gc->capStyle != CapNotLast)
        add_last = 1;

    /* Set up the vertex buffers for the points */

    v = glamor_get_vbo_space(drawable->pScreen,
                             (n + add_last) * 3 * sizeof (short),
                             &vbo_offset);

    glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
    glVertexAttribPointer(GLAMOR_VERTEX_POS, 3, GL_SHORT, GL_FALSE,
                          3 * sizeof (short), vbo_offset);

    dash_pos = gc->dashOffset;
    prev_x = prev_y = 0;
    for (i = 0; i < n; i++) {
        int this_x = points[i].x;
        int this_y = points[i].y;
        if (i) {
            if (mode == CoordModePrevious) {
                this_x += prev_x;
                this_y += prev_y;
            }
            dash_pos += glamor_line_length(prev_x, prev_y,
                                           this_x, this_y);
        }
        v[0] = prev_x = this_x;
        v[1] = prev_y = this_y;
        v[2] = dash_pos;
        v += 3;
    }

    if (add_last) {
        v[0] = prev_x + 1;
        v[1] = prev_y;
        v[2] = dash_pos + 1;
    }

    glamor_put_vbo_space(screen);

    glamor_dash_loop(drawable, gc, prog, n + add_last, GL_LINE_STRIP);

    return TRUE;
}

static short *
glamor_add_segment(short *v, short x1, short y1, short x2, short y2,
                   int dash_start, int dash_end)
{
    v[0] = x1;
    v[1] = y1;
    v[2] = dash_start;

    v[3] = x2;
    v[4] = y2;
    v[5] = dash_end;
    return v + 6;
}

Bool
glamor_poly_segment_dash_gl(DrawablePtr drawable, GCPtr gc,
                            int nseg, xSegment *segs)
{
    ScreenPtr screen = drawable->pScreen;
    glamor_program *prog;
    short *v;
    char *vbo_offset;
    int dash_start = gc->dashOffset;
    int add_last;
    int i;

    if (!(prog = glamor_dash_setup(drawable, gc)))
        return FALSE;

    add_last = 0;
    if (gc->capStyle != CapNotLast)
        add_last = 1;

    /* Set up the vertex buffers for the points */

    v = glamor_get_vbo_space(drawable->pScreen,
                             (nseg<<add_last) * 6 * sizeof (short),
                             &vbo_offset);

    glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
    glVertexAttribPointer(GLAMOR_VERTEX_POS, 3, GL_SHORT, GL_FALSE,
                          3 * sizeof (short), vbo_offset);

    for (i = 0; i < nseg; i++) {
        int dash_end = dash_start + glamor_line_length(segs[i].x1, segs[i].y1,
                                                       segs[i].x2, segs[i].y2);
        v = glamor_add_segment(v,
                               segs[i].x1, segs[i].y1,
                               segs[i].x2, segs[i].y2,
                               dash_start, dash_end);
        if (add_last)
            v = glamor_add_segment(v,
                                   segs[i].x2, segs[i].y2,
                                   segs[i].x2 + 1, segs[i].y2,
                                   dash_end, dash_end + 1);
    }

    glamor_put_vbo_space(screen);

    glamor_dash_loop(drawable, gc, prog, nseg << (1 + add_last), GL_LINES);

    return TRUE;
}