summaryrefslogtreecommitdiff
path: root/glamor/glamor_spans.c
blob: b3c028d673ec01fbf9403ad2a01a8f0141513f58 (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
371
372
373
374
375
376
377
378
379
380
381
/*
 * 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_transform.h"
#include "glamor_transfer.h"

glamor_program  fill_spans_progs[4];

static const glamor_facet glamor_facet_fillspans_130 = {
    .name = "fill_spans",
    .version = 130,
    .vs_vars =  "attribute vec3 primitive;\n",
    .vs_exec = ("       vec2 pos = vec2(primitive.z,1) * vec2(gl_VertexID&1, (gl_VertexID&2)>>1);\n"
                GLAMOR_POS(gl_Position, (primitive.xy + pos))),
};

static const glamor_facet glamor_facet_fillspans_120 = {
    .name = "fill_spans",
    .vs_vars =  "attribute vec2 primitive;\n",
    .vs_exec = ("       vec2 pos = vec2(0,0);\n"
                GLAMOR_POS(gl_Position, primitive.xy)),
};

static Bool
glamor_fill_spans_gl(DrawablePtr drawable,
                     GCPtr gc,
                     int n, DDXPointPtr points, int *widths, int sorted)
{
    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_program *prog;
    int off_x, off_y;
    GLshort *v;
    char *vbo_offset;
    int c;
    int box_index;
    Bool ret = FALSE;

    pixmap_priv = glamor_get_pixmap_private(pixmap);
    if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
        goto bail;

    glamor_make_current(glamor_priv);

    if (glamor_priv->glsl_version >= 130) {
        prog = glamor_use_program_fill(pixmap, gc, &glamor_priv->fill_spans_program,
                                       &glamor_facet_fillspans_130);

        if (!prog)
            goto bail;

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

        v = glamor_get_vbo_space(drawable->pScreen, n * (4 * sizeof (GLshort)), &vbo_offset);

        glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
        glVertexAttribDivisor(GLAMOR_VERTEX_POS, 1);
        glVertexAttribPointer(GLAMOR_VERTEX_POS, 3, GL_SHORT, GL_FALSE,
                              4 * sizeof (GLshort), vbo_offset);

        for (c = 0; c < n; c++) {
            v[0] = points->x;
            v[1] = points->y;
            v[2] = *widths++;
            points++;
            v += 4;
        }

        glamor_put_vbo_space(screen);
    } else {
        prog = glamor_use_program_fill(pixmap, gc, &glamor_priv->fill_spans_program,
                                       &glamor_facet_fillspans_120);

        if (!prog)
            goto bail;

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

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

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

        for (c = 0; c < n; c++) {
            v[0] = points->x;           v[1] = points->y;
            v[2] = points->x;           v[3] = points->y + 1;
            v[4] = points->x + *widths; v[5] = points->y + 1;
            v[6] = points->x + *widths; v[7] = points->y;

            widths++;
            points++;
            v += 8;
        }

        glamor_put_vbo_space(screen);
    }

    glEnable(GL_SCISSOR_TEST);

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

        if (!glamor_set_destination_drawable(drawable, box_index, FALSE, FALSE,
                                             prog->matrix_uniform, &off_x, &off_y))
            goto bail;

        while (nbox--) {
            glScissor(box->x1 + off_x,
                      box->y1 + off_y,
                      box->x2 - box->x1,
                      box->y2 - box->y1);
            box++;
            if (glamor_priv->glsl_version >= 130)
                glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, n);
            else {
                glamor_glDrawArrays_GL_QUADS(glamor_priv, n);
            }
        }
    }

    ret = TRUE;

bail:
    glDisable(GL_SCISSOR_TEST);
    if (glamor_priv->glsl_version >= 130)
        glVertexAttribDivisor(GLAMOR_VERTEX_POS, 0);
    glDisableVertexAttribArray(GLAMOR_VERTEX_POS);

    return ret;
}

static void
glamor_fill_spans_bail(DrawablePtr drawable,
                       GCPtr gc,
                       int n, DDXPointPtr points, int *widths, int sorted)
{
    if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RW) &&
        glamor_prepare_access_gc(gc)) {
        fbFillSpans(drawable, gc, n, points, widths, sorted);
    }
    glamor_finish_access_gc(gc);
    glamor_finish_access(drawable);
}

void
glamor_fill_spans(DrawablePtr drawable,
                  GCPtr gc,
                  int n, DDXPointPtr points, int *widths, int sorted)
{
    if (glamor_fill_spans_gl(drawable, gc, n, points, widths, sorted))
        return;
    glamor_fill_spans_bail(drawable, gc, n, points, widths, sorted);
}

static Bool
glamor_get_spans_gl(DrawablePtr drawable, int wmax,
                    DDXPointPtr points, int *widths, int count, char *dst)
{
    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;
    int box_index;
    int n;
    char *d;
    GLenum type;
    GLenum format;
    int off_x, off_y;

    pixmap_priv = glamor_get_pixmap_private(pixmap);
    if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
        goto bail;

    glamor_get_drawable_deltas(drawable, pixmap, &off_x, &off_y);

    glamor_format_for_pixmap(pixmap, &format, &type);

    glamor_make_current(glamor_priv);

    glamor_pixmap_loop(pixmap_priv, box_index) {
        BoxPtr                  box = glamor_pixmap_box_at(pixmap_priv, box_index);
        glamor_pixmap_fbo       *fbo = glamor_pixmap_fbo_at(pixmap_priv, box_index);

        glBindFramebuffer(GL_FRAMEBUFFER, fbo->fb);
        glPixelStorei(GL_PACK_ALIGNMENT, 4);

        d = dst;
        for (n = 0; n < count; n++) {
            int x1 = points[n].x + off_x;
            int y = points[n].y + off_y;
            int w = widths[n];
            int x2 = x1 + w;
            char *l;

            l = d;
            d += PixmapBytePad(w, drawable->depth);

            /* clip */
            if (x1 < box->x1) {
                l += (box->x1 - x1) * (drawable->bitsPerPixel >> 3);
                x1 = box->x1;
            }
            if (x2 > box->x2)
                x2 = box->x2;

            if (x1 >= x2)
                continue;
            if (y < box->y1)
                continue;
            if (y >= box->y2)
                continue;

            glReadPixels(x1 - box->x1, y - box->y1, x2 - x1, 1, format, type, l);
        }
    }

    return TRUE;
bail:
    return FALSE;
}

static void
glamor_get_spans_bail(DrawablePtr drawable, int wmax,
                 DDXPointPtr points, int *widths, int count, char *dst)
{
    if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RO))
        fbGetSpans(drawable, wmax, points, widths, count, dst);
    glamor_finish_access(drawable);
}

void
glamor_get_spans(DrawablePtr drawable, int wmax,
                 DDXPointPtr points, int *widths, int count, char *dst)
{
    if (glamor_get_spans_gl(drawable, wmax, points, widths, count, dst))
        return;
    glamor_get_spans_bail(drawable, wmax, points, widths, count, dst);
}

static Bool
glamor_set_spans_gl(DrawablePtr drawable, GCPtr gc, char *src,
                    DDXPointPtr points, int *widths, int numPoints, int sorted)
{
    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;
    int box_index;
    int n;
    char *s;
    GLenum type;
    GLenum format;
    int off_x, off_y;

    pixmap_priv = glamor_get_pixmap_private(pixmap);
    if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
        goto bail;

    if (gc->alu != GXcopy)
        goto bail;

    if (!glamor_pm_is_solid(gc->depth, gc->planemask))
        goto bail;

    glamor_get_drawable_deltas(drawable, pixmap, &off_x, &off_y);
    glamor_format_for_pixmap(pixmap, &format, &type);

    glamor_make_current(glamor_priv);

    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

    glamor_pixmap_loop(pixmap_priv, box_index) {
        BoxPtr              box = glamor_pixmap_box_at(pixmap_priv, box_index);
        glamor_pixmap_fbo  *fbo = glamor_pixmap_fbo_at(pixmap_priv, box_index);

        glamor_bind_texture(glamor_priv, GL_TEXTURE0, fbo, TRUE);

        s = src;
        for (n = 0; n < numPoints; n++) {

            BoxPtr      clip_box = RegionRects(gc->pCompositeClip);
            int         nclip_box = RegionNumRects(gc->pCompositeClip);
            int         w = widths[n];
            int         y = points[n].y;
            int         x = points[n].x;

            while (nclip_box--) {
                int x1 = x;
                int x2 = x + w;
                int y1 = y;
                char *l = s;

                /* clip to composite clip */
                if (x1 < clip_box->x1) {
                    l += (clip_box->x1 - x1) * (drawable->bitsPerPixel >> 3);
                    x1 = clip_box->x1;
                }
                if (x2 > clip_box->x2)
                    x2 = clip_box->x2;

                if (y < clip_box->y1)
                    continue;
                if (y >= clip_box->y2)
                    continue;

                /* adjust to pixmap coordinates */
                x1 += off_x;
                x2 += off_x;
                y1 += off_y;

                if (x1 < box->x1) {
                    l += (box->x1 - x1) * (drawable->bitsPerPixel >> 3);
                    x1 = box->x1;
                }
                if (x2 > box->x2)
                    x2 = box->x2;

                if (x1 >= x2)
                    continue;
                if (y1 < box->y1)
                    continue;
                if (y1 >= box->y2)
                    continue;

                glTexSubImage2D(GL_TEXTURE_2D, 0,
                                x1 - box->x1, y1 - box->y1, x2 - x1, 1,
                                format, type,
                                l);
            }
            s += PixmapBytePad(w, drawable->depth);
        }
    }

    return TRUE;

bail:
    return FALSE;
}

static void
glamor_set_spans_bail(DrawablePtr drawable, GCPtr gc, char *src,
                      DDXPointPtr points, int *widths, int numPoints, int sorted)
{
    if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RW) && glamor_prepare_access_gc(gc))
        fbSetSpans(drawable, gc, src, points, widths, numPoints, sorted);
    glamor_finish_access_gc(gc);
    glamor_finish_access(drawable);
}

void
glamor_set_spans(DrawablePtr drawable, GCPtr gc, char *src,
                 DDXPointPtr points, int *widths, int numPoints, int sorted)
{
    if (glamor_set_spans_gl(drawable, gc, src, points, widths, numPoints, sorted))
        return;
    glamor_set_spans_bail(drawable, gc, src, points, widths, numPoints, sorted);
}