135c4bbdfSmrg/*
235c4bbdfSmrg * Copyright © 2014 Keith Packard
335c4bbdfSmrg *
435c4bbdfSmrg * Permission to use, copy, modify, distribute, and sell this software and its
535c4bbdfSmrg * documentation for any purpose is hereby granted without fee, provided that
635c4bbdfSmrg * the above copyright notice appear in all copies and that both that copyright
735c4bbdfSmrg * notice and this permission notice appear in supporting documentation, and
835c4bbdfSmrg * that the name of the copyright holders not be used in advertising or
935c4bbdfSmrg * publicity pertaining to distribution of the software without specific,
1035c4bbdfSmrg * written prior permission.  The copyright holders make no representations
1135c4bbdfSmrg * about the suitability of this software for any purpose.  It is provided "as
1235c4bbdfSmrg * is" without express or implied warranty.
1335c4bbdfSmrg *
1435c4bbdfSmrg * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
1535c4bbdfSmrg * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
1635c4bbdfSmrg * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
1735c4bbdfSmrg * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
1835c4bbdfSmrg * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
1935c4bbdfSmrg * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
2035c4bbdfSmrg * OF THIS SOFTWARE.
2135c4bbdfSmrg */
2235c4bbdfSmrg
2335c4bbdfSmrg#include "glamor_priv.h"
2435c4bbdfSmrg#include "glamor_program.h"
2535c4bbdfSmrg#include "glamor_transform.h"
2635c4bbdfSmrg#include "glamor_prepare.h"
2735c4bbdfSmrg
2835c4bbdfSmrgstatic const glamor_facet glamor_facet_poly_segment = {
2935c4bbdfSmrg    .name = "poly_segment",
3035c4bbdfSmrg    .vs_vars = "attribute vec2 primitive;\n",
3135c4bbdfSmrg    .vs_exec = ("       vec2 pos = vec2(0.0,0.0);\n"
3235c4bbdfSmrg                GLAMOR_POS(gl_Position, primitive.xy)),
3335c4bbdfSmrg};
3435c4bbdfSmrg
3535c4bbdfSmrgstatic Bool
3635c4bbdfSmrgglamor_poly_segment_solid_gl(DrawablePtr drawable, GCPtr gc,
3735c4bbdfSmrg                             int nseg, xSegment *segs)
3835c4bbdfSmrg{
3935c4bbdfSmrg    ScreenPtr screen = drawable->pScreen;
4035c4bbdfSmrg    glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
4135c4bbdfSmrg    PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
4235c4bbdfSmrg    glamor_pixmap_private *pixmap_priv;
4335c4bbdfSmrg    glamor_program *prog;
4435c4bbdfSmrg    int off_x, off_y;
4535c4bbdfSmrg    xSegment *v;
4635c4bbdfSmrg    char *vbo_offset;
4735c4bbdfSmrg    int box_index;
4835c4bbdfSmrg    int add_last;
491b5d61b8Smrg    Bool ret = FALSE;
5035c4bbdfSmrg
5135c4bbdfSmrg    pixmap_priv = glamor_get_pixmap_private(pixmap);
5235c4bbdfSmrg    if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
5335c4bbdfSmrg        goto bail;
5435c4bbdfSmrg
5535c4bbdfSmrg    add_last = 0;
5635c4bbdfSmrg    if (gc->capStyle != CapNotLast)
5735c4bbdfSmrg        add_last = 1;
5835c4bbdfSmrg
5935c4bbdfSmrg    glamor_make_current(glamor_priv);
6035c4bbdfSmrg
6135c4bbdfSmrg    prog = glamor_use_program_fill(pixmap, gc,
6235c4bbdfSmrg                                   &glamor_priv->poly_segment_program,
6335c4bbdfSmrg                                   &glamor_facet_poly_segment);
6435c4bbdfSmrg
6535c4bbdfSmrg    if (!prog)
661b5d61b8Smrg        goto bail;
6735c4bbdfSmrg
6835c4bbdfSmrg    /* Set up the vertex buffers for the points */
6935c4bbdfSmrg
7035c4bbdfSmrg    v = glamor_get_vbo_space(drawable->pScreen,
7135c4bbdfSmrg                             (nseg << add_last) * sizeof (xSegment),
7235c4bbdfSmrg                             &vbo_offset);
7335c4bbdfSmrg
7435c4bbdfSmrg    glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
7535c4bbdfSmrg    glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_SHORT, GL_FALSE,
7635c4bbdfSmrg                          sizeof(DDXPointRec), vbo_offset);
7735c4bbdfSmrg
7835c4bbdfSmrg    if (add_last) {
7935c4bbdfSmrg        int i, j;
8035c4bbdfSmrg        for (i = 0, j=0; i < nseg; i++) {
8135c4bbdfSmrg            v[j++] = segs[i];
8235c4bbdfSmrg            v[j].x1 = segs[i].x2;
8335c4bbdfSmrg            v[j].y1 = segs[i].y2;
8435c4bbdfSmrg            v[j].x2 = segs[i].x2+1;
8535c4bbdfSmrg            v[j].y2 = segs[i].y2;
8635c4bbdfSmrg            j++;
8735c4bbdfSmrg        }
8835c4bbdfSmrg    } else
8935c4bbdfSmrg        memcpy(v, segs, nseg * sizeof (xSegment));
9035c4bbdfSmrg
9135c4bbdfSmrg    glamor_put_vbo_space(screen);
9235c4bbdfSmrg
9335c4bbdfSmrg    glEnable(GL_SCISSOR_TEST);
9435c4bbdfSmrg
9535c4bbdfSmrg    glamor_pixmap_loop(pixmap_priv, box_index) {
9635c4bbdfSmrg        int nbox = RegionNumRects(gc->pCompositeClip);
9735c4bbdfSmrg        BoxPtr box = RegionRects(gc->pCompositeClip);
9835c4bbdfSmrg
991b5d61b8Smrg        if (!glamor_set_destination_drawable(drawable, box_index, TRUE, TRUE,
1001b5d61b8Smrg                                             prog->matrix_uniform, &off_x, &off_y))
1011b5d61b8Smrg            goto bail;
10235c4bbdfSmrg
10335c4bbdfSmrg        while (nbox--) {
10435c4bbdfSmrg            glScissor(box->x1 + off_x,
10535c4bbdfSmrg                      box->y1 + off_y,
10635c4bbdfSmrg                      box->x2 - box->x1,
10735c4bbdfSmrg                      box->y2 - box->y1);
10835c4bbdfSmrg            box++;
10935c4bbdfSmrg            glDrawArrays(GL_LINES, 0, nseg << (1 + add_last));
11035c4bbdfSmrg        }
11135c4bbdfSmrg    }
11235c4bbdfSmrg
1131b5d61b8Smrg    ret = TRUE;
1141b5d61b8Smrg
11535c4bbdfSmrg    glDisable(GL_SCISSOR_TEST);
11635c4bbdfSmrg    glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
11735c4bbdfSmrg
11835c4bbdfSmrgbail:
1191b5d61b8Smrg    return ret;
12035c4bbdfSmrg}
12135c4bbdfSmrg
12235c4bbdfSmrgstatic Bool
12335c4bbdfSmrgglamor_poly_segment_gl(DrawablePtr drawable, GCPtr gc,
12435c4bbdfSmrg                       int nseg, xSegment *segs)
12535c4bbdfSmrg{
12635c4bbdfSmrg    if (gc->lineWidth != 0)
12735c4bbdfSmrg        return FALSE;
12835c4bbdfSmrg
12935c4bbdfSmrg    switch (gc->lineStyle) {
13035c4bbdfSmrg    case LineSolid:
13135c4bbdfSmrg        return glamor_poly_segment_solid_gl(drawable, gc, nseg, segs);
13235c4bbdfSmrg    case LineOnOffDash:
13335c4bbdfSmrg        return glamor_poly_segment_dash_gl(drawable, gc, nseg, segs);
13435c4bbdfSmrg    case LineDoubleDash:
13535c4bbdfSmrg        if (gc->fillStyle == FillTiled)
13635c4bbdfSmrg            return glamor_poly_segment_solid_gl(drawable, gc, nseg, segs);
13735c4bbdfSmrg        else
13835c4bbdfSmrg            return glamor_poly_segment_dash_gl(drawable, gc, nseg, segs);
13935c4bbdfSmrg    default:
14035c4bbdfSmrg        return FALSE;
14135c4bbdfSmrg    }
14235c4bbdfSmrg}
14335c4bbdfSmrg
14435c4bbdfSmrgstatic void
14535c4bbdfSmrgglamor_poly_segment_bail(DrawablePtr drawable, GCPtr gc,
14635c4bbdfSmrg                         int nseg, xSegment *segs)
14735c4bbdfSmrg{
14835c4bbdfSmrg    glamor_fallback("to %p (%c)\n", drawable,
14935c4bbdfSmrg                    glamor_get_drawable_location(drawable));
15035c4bbdfSmrg
15135c4bbdfSmrg    if (gc->lineWidth == 0) {
15235c4bbdfSmrg        if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RW) &&
15335c4bbdfSmrg            glamor_prepare_access_gc(gc)) {
15435c4bbdfSmrg            fbPolySegment(drawable, gc, nseg, segs);
15535c4bbdfSmrg        }
15635c4bbdfSmrg        glamor_finish_access_gc(gc);
15735c4bbdfSmrg        glamor_finish_access(drawable);
15835c4bbdfSmrg    } else
15935c4bbdfSmrg        miPolySegment(drawable, gc, nseg, segs);
16035c4bbdfSmrg}
16135c4bbdfSmrg
16235c4bbdfSmrgvoid
16335c4bbdfSmrgglamor_poly_segment(DrawablePtr drawable, GCPtr gc,
16435c4bbdfSmrg                    int nseg, xSegment *segs)
16535c4bbdfSmrg{
16635c4bbdfSmrg    if (glamor_poly_segment_gl(drawable, gc, nseg, segs))
16735c4bbdfSmrg        return;
16835c4bbdfSmrg
16935c4bbdfSmrg    glamor_poly_segment_bail(drawable, gc, nseg, segs);
17035c4bbdfSmrg}
171