glamor_glyphblt.c 10.6 KB
Newer Older
Zhigang Gong's avatar
Zhigang Gong committed
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
/*
 * Copyright © 2009 Intel Corporation
 * Copyright © 1998 Keith Packard
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *    Zhigang Gong <zhigang.gong@gmail.com>
 *
 */

#include "glamor_priv.h"
30
#include <dixfontstr.h>
31 32 33 34 35 36 37 38
#include "glamor_transform.h"

static const glamor_facet glamor_facet_poly_glyph_blt = {
    .name = "poly_glyph_blt",
    .vs_vars = "attribute vec2 primitive;\n",
    .vs_exec = ("       vec2 pos = vec2(0,0);\n"
                GLAMOR_POS(gl_Position, primitive)),
};
39 40

static Bool
41 42 43
glamor_poly_glyph_blt_gl(DrawablePtr drawable, GCPtr gc,
                         int start_x, int y, unsigned int nglyph,
                         CharInfoPtr *ppci, void *pglyph_base)
44 45 46 47 48
{
    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;
49 50 51
    glamor_program *prog;
    RegionPtr clip = gc->pCompositeClip;
    int box_x, box_y;
52 53 54

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

57
    glamor_make_current(glamor_priv);
58

59 60 61 62
    prog = glamor_use_program_fill(pixmap, gc, &glamor_priv->poly_glyph_blt_progs,
                                   &glamor_facet_poly_glyph_blt);
    if (!prog)
        goto bail_ctx;
63 64 65

    glEnableVertexAttribArray(GLAMOR_VERTEX_POS);

66 67
    start_x += drawable->x;
    y += drawable->y;
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
    glamor_pixmap_loop(pixmap_priv, box_x, box_y) {
        int x;
        int n;
        int num_points, max_points;
        INT16 *points = NULL;
        int off_x, off_y;
        char *vbo_offset;

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

        max_points = 500;
        num_points = 0;
        x = start_x;
        for (n = 0; n < nglyph; n++) {
            CharInfoPtr charinfo = ppci[n];
            int w = GLYPHWIDTHPIXELS(charinfo);
            int h = GLYPHHEIGHTPIXELS(charinfo);
            uint8_t *glyphbits = FONTGLYPHBITS(NULL, charinfo);

            if (w && h) {
                int glyph_x = x + charinfo->metrics.leftSideBearing;
                int glyph_y = y - charinfo->metrics.ascent;
                int glyph_stride = GLYPHWIDTHBYTESPADDED(charinfo);
                int xx, yy;

                for (yy = 0; yy < h; yy++) {
                    uint8_t *glyph = glyphbits;
                    for (xx = 0; xx < w; glyph += ((xx&7) == 7), xx++) {
                        int pt_x_i = glyph_x + xx;
                        int pt_y_i = glyph_y + yy;

                        if (!(*glyph & (1 << (xx & 7))))
                            continue;

                        if (!RegionContainsPoint(clip, pt_x_i, pt_y_i, NULL))
                            continue;

                        if (!num_points) {
                            points = glamor_get_vbo_space(screen,
                                                          max_points * (2 * sizeof (INT16)),
                                                          &vbo_offset);

                            glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_SHORT,
                                                  GL_FALSE, 0, vbo_offset);
                        }

                        *points++ = pt_x_i;
                        *points++ = pt_y_i;
                        num_points++;

                        if (num_points == max_points) {
                            glamor_put_vbo_space(screen);
                            glDrawArrays(GL_POINTS, 0, num_points);
                            num_points = 0;
                        }
124
                    }
125
                    glyphbits += glyph_stride;
126 127
                }
            }
128
            x += charinfo->metrics.characterWidth;
129 130
        }

131 132 133 134
        if (num_points) {
            glamor_put_vbo_space(screen);
            glDrawArrays(GL_POINTS, 0, num_points);
        }
135 136
    }

137
    glDisable(GL_COLOR_LOGIC_OP);
138 139
    glDisableVertexAttribArray(GLAMOR_VERTEX_POS);

Eric Anholt's avatar
Eric Anholt committed
140
    return TRUE;
141 142 143 144
bail_ctx:
    glDisable(GL_COLOR_LOGIC_OP);
bail:
    return FALSE;
Zhigang Gong's avatar
Zhigang Gong committed
145 146 147
}

void
148 149 150
glamor_poly_glyph_blt(DrawablePtr drawable, GCPtr gc,
                      int start_x, int y, unsigned int nglyph,
                      CharInfoPtr *ppci, void *pglyph_base)
Zhigang Gong's avatar
Zhigang Gong committed
151
{
152 153 154 155
    if (glamor_poly_glyph_blt_gl(drawable, gc, start_x, y, nglyph, ppci, pglyph_base))
        return;
    miPolyGlyphBlt(drawable, gc, start_x, y, nglyph,
                   ppci, pglyph_base);
Zhigang Gong's avatar
Zhigang Gong committed
156 157 158
}

Bool
159 160 161
glamor_poly_glyph_blt_nf(DrawablePtr drawable, GCPtr gc,
                         int start_x, int y, unsigned int nglyph,
                         CharInfoPtr *ppci, void *pglyph_base)
Zhigang Gong's avatar
Zhigang Gong committed
162
{
163
    if (glamor_poly_glyph_blt_gl(drawable, gc, start_x, y, nglyph, ppci, pglyph_base))
164
        return TRUE;
165
    if (glamor_ddx_fallback_check_pixmap(drawable) && glamor_ddx_fallback_check_gc(gc))
Eric Anholt's avatar
Eric Anholt committed
166
        return FALSE;
167 168
    miPolyGlyphBlt(drawable, gc, start_x, y, nglyph,
                   ppci, pglyph_base);
Eric Anholt's avatar
Eric Anholt committed
169
    return TRUE;
Zhigang Gong's avatar
Zhigang Gong committed
170 171 172
}

Bool
173 174 175
glamor_image_glyph_blt_nf(DrawablePtr drawable, GCPtr gc,
                          int start_x, int y, unsigned int nglyph,
                          CharInfoPtr *ppci, void *pglyph_base)
Zhigang Gong's avatar
Zhigang Gong committed
176
{
177 178
    miImageGlyphBlt(drawable, gc, start_x, y, nglyph, ppci, pglyph_base);
    return TRUE;
Zhigang Gong's avatar
Zhigang Gong committed
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
static Bool
glamor_push_pixels_points(GCPtr gc, PixmapPtr bitmap,
                          DrawablePtr drawable, int w, int h, int x, int y)
{
    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;
    uint8_t *bitmap_data = bitmap->devPrivate.ptr;
    int bitmap_stride = bitmap->devKind;
    int off_x, off_y;
    int yy, xx;
    GLfloat xscale, yscale;
    float color[4];
    unsigned long fg_pixel = gc->fgPixel;
    float *points, *next_point;
    int num_points = 0;
    char *vbo_offset;
    RegionPtr clip;

    if (w * h > MAXINT / (2 * sizeof(float)))
        return FALSE;

    if (gc->fillStyle != FillSolid) {
        glamor_fallback("gc fillstyle not solid\n");
        return FALSE;
    }

    pixmap_priv = glamor_get_pixmap_private(pixmap);
    if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
        return FALSE;

213
    glamor_make_current(glamor_priv);
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
    if (!glamor_set_alu(screen, gc->alu)) {
        if (gc->alu == GXclear)
            fg_pixel = 0;
        else {
            glamor_fallback("unsupported alu %x\n", gc->alu);
            return FALSE;
        }
    }

    if (!glamor_set_planemask(pixmap, gc->planemask)) {
        glamor_fallback("Failed to set planemask in %s.\n", __FUNCTION__);
        return FALSE;
    }

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

    glamor_set_destination_pixmap_priv_nc(pixmap_priv);
    pixmap_priv_get_dest_scale(pixmap_priv, &xscale, &yscale);

    glUseProgram(glamor_priv->solid_prog);

    glamor_get_rgba_from_pixel(fg_pixel,
                               &color[0], &color[1], &color[2], &color[3],
                               format_for_pixmap(pixmap));
    glUniform4fv(glamor_priv->solid_color_uniform_location, 1, color);

    points = glamor_get_vbo_space(screen, w * h * sizeof(float) * 2,
                                  &vbo_offset);
    next_point = points;

    clip = fbGetCompositeClip(gc);

    /* Note that because fb sets miTranslate in the GC, our incoming X
     * and Y are in screen coordinate space (same for spans, but not
     * other operations).
     */
    for (yy = 0; yy < h; yy++) {
        uint8_t *bitmap_row = bitmap_data + yy * bitmap_stride;
        for (xx = 0; xx < w; xx++) {
            if (bitmap_row[xx / 8] & (1 << xx % 8) &&
                RegionContainsPoint(clip,
                                    x + xx,
                                    y + yy,
                                    NULL)) {
                next_point[0] = v_from_x_coord_x(xscale, x + xx + off_x + 0.5);
                if (glamor_priv->yInverted)
                    next_point[1] = v_from_x_coord_y_inverted(yscale, y + yy + off_y + 0.5);
                else
                    next_point[1] = v_from_x_coord_y(yscale, y + yy + off_y + 0.5);

                next_point += 2;
                num_points++;
            }
        }
    }
    glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_FLOAT,
                          GL_FALSE, 2 * sizeof(float),
                          vbo_offset);
    glEnableVertexAttribArray(GLAMOR_VERTEX_POS);

    glamor_put_vbo_space(screen);

    glDrawArrays(GL_POINTS, 0, num_points);

    glDisableVertexAttribArray(GLAMOR_VERTEX_POS);

    return TRUE;
}

Zhigang Gong's avatar
Zhigang Gong committed
283 284
static Bool
_glamor_push_pixels(GCPtr pGC, PixmapPtr pBitmap,
Eric Anholt's avatar
Eric Anholt committed
285 286
                    DrawablePtr pDrawable, int w, int h, int x, int y,
                    Bool fallback)
Zhigang Gong's avatar
Zhigang Gong committed
287
{
288 289
    glamor_pixmap_private *pixmap_priv;

Eric Anholt's avatar
Eric Anholt committed
290 291 292 293
    if (!fallback && glamor_ddx_fallback_check_pixmap(pDrawable)
        && glamor_ddx_fallback_check_pixmap(&pBitmap->drawable)
        && glamor_ddx_fallback_check_gc(pGC))
        return FALSE;
Zhigang Gong's avatar
Zhigang Gong committed
294

295 296 297 298 299 300
    pixmap_priv = glamor_get_pixmap_private(pBitmap);
    if (pixmap_priv->type == GLAMOR_MEMORY) {
        if (glamor_push_pixels_points(pGC, pBitmap, pDrawable, w, h, x, y))
            return TRUE;
    }

Eric Anholt's avatar
Eric Anholt committed
301 302
    miPushPixels(pGC, pBitmap, pDrawable, w, h, x, y);
    return TRUE;
Zhigang Gong's avatar
Zhigang Gong committed
303 304 305 306
}

void
glamor_push_pixels(GCPtr pGC, PixmapPtr pBitmap,
Eric Anholt's avatar
Eric Anholt committed
307
                   DrawablePtr pDrawable, int w, int h, int x, int y)
Zhigang Gong's avatar
Zhigang Gong committed
308
{
Eric Anholt's avatar
Eric Anholt committed
309
    _glamor_push_pixels(pGC, pBitmap, pDrawable, w, h, x, y, TRUE);
Zhigang Gong's avatar
Zhigang Gong committed
310 311 312 313
}

Bool
glamor_push_pixels_nf(GCPtr pGC, PixmapPtr pBitmap,
Eric Anholt's avatar
Eric Anholt committed
314
                      DrawablePtr pDrawable, int w, int h, int x, int y)
Zhigang Gong's avatar
Zhigang Gong committed
315
{
Eric Anholt's avatar
Eric Anholt committed
316
    return _glamor_push_pixels(pGC, pBitmap, pDrawable, w, h, x, y, FALSE);
Zhigang Gong's avatar
Zhigang Gong committed
317
}