xserver

xserver with xephyr scale patch
git clone https://git.neptards.moe/u3shit/xserver.git
Log | Files | Refs | README | LICENSE

glamor_glyphblt.c (8518B)


      1 /*
      2  * Copyright © 2009 Intel Corporation
      3  * Copyright © 1998 Keith Packard
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining a
      6  * copy of this software and associated documentation files (the "Software"),
      7  * to deal in the Software without restriction, including without limitation
      8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      9  * and/or sell copies of the Software, and to permit persons to whom the
     10  * Software is furnished to do so, subject to the following conditions:
     11  *
     12  * The above copyright notice and this permission notice (including the next
     13  * paragraph) shall be included in all copies or substantial portions of the
     14  * Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     22  * IN THE SOFTWARE.
     23  *
     24  * Authors:
     25  *    Zhigang Gong <zhigang.gong@gmail.com>
     26  *
     27  */
     28 
     29 #include "glamor_priv.h"
     30 #include <dixfontstr.h>
     31 #include "glamor_transform.h"
     32 
     33 static const glamor_facet glamor_facet_poly_glyph_blt = {
     34     .name = "poly_glyph_blt",
     35     .vs_vars = "attribute vec2 primitive;\n",
     36     .vs_exec = ("       vec2 pos = vec2(0,0);\n"
     37                 GLAMOR_POS(gl_Position, primitive)),
     38 };
     39 
     40 static Bool
     41 glamor_poly_glyph_blt_gl(DrawablePtr drawable, GCPtr gc,
     42                          int start_x, int y, unsigned int nglyph,
     43                          CharInfoPtr *ppci, void *pglyph_base)
     44 {
     45     ScreenPtr screen = drawable->pScreen;
     46     glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
     47     PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
     48     glamor_pixmap_private *pixmap_priv;
     49     glamor_program *prog;
     50     RegionPtr clip = gc->pCompositeClip;
     51     int box_index;
     52     Bool ret = FALSE;
     53 
     54     pixmap_priv = glamor_get_pixmap_private(pixmap);
     55     if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
     56         goto bail;
     57 
     58     glamor_make_current(glamor_priv);
     59 
     60     prog = glamor_use_program_fill(pixmap, gc,
     61                                    &glamor_priv->poly_glyph_blt_progs,
     62                                    &glamor_facet_poly_glyph_blt);
     63     if (!prog)
     64         goto bail;
     65 
     66     glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
     67 
     68     start_x += drawable->x;
     69     y += drawable->y;
     70 
     71     glamor_pixmap_loop(pixmap_priv, box_index) {
     72         int x;
     73         int n;
     74         int num_points, max_points;
     75         INT16 *points = NULL;
     76         int off_x, off_y;
     77         char *vbo_offset;
     78 
     79         if (!glamor_set_destination_drawable(drawable, box_index, FALSE, TRUE,
     80                                               prog->matrix_uniform, &off_x, &off_y))
     81             goto bail;
     82 
     83         max_points = 500;
     84         num_points = 0;
     85         x = start_x;
     86         for (n = 0; n < nglyph; n++) {
     87             CharInfoPtr charinfo = ppci[n];
     88             int w = GLYPHWIDTHPIXELS(charinfo);
     89             int h = GLYPHHEIGHTPIXELS(charinfo);
     90             uint8_t *glyphbits = FONTGLYPHBITS(NULL, charinfo);
     91 
     92             if (w && h) {
     93                 int glyph_x = x + charinfo->metrics.leftSideBearing;
     94                 int glyph_y = y - charinfo->metrics.ascent;
     95                 int glyph_stride = GLYPHWIDTHBYTESPADDED(charinfo);
     96                 int xx, yy;
     97 
     98                 for (yy = 0; yy < h; yy++) {
     99                     uint8_t *glyph = glyphbits;
    100                     for (xx = 0; xx < w; glyph += ((xx&7) == 7), xx++) {
    101                         int pt_x_i = glyph_x + xx;
    102                         int pt_y_i = glyph_y + yy;
    103 
    104                         if (!(*glyph & (1 << (xx & 7))))
    105                             continue;
    106 
    107                         if (!RegionContainsPoint(clip, pt_x_i, pt_y_i, NULL))
    108                             continue;
    109 
    110                         if (!num_points) {
    111                             points = glamor_get_vbo_space(screen,
    112                                                           max_points *
    113                                                           (2 * sizeof (INT16)),
    114                                                           &vbo_offset);
    115 
    116                             glVertexAttribPointer(GLAMOR_VERTEX_POS,
    117                                                   2, GL_SHORT,
    118                                                   GL_FALSE, 0, vbo_offset);
    119                         }
    120 
    121                         *points++ = pt_x_i;
    122                         *points++ = pt_y_i;
    123                         num_points++;
    124 
    125                         if (num_points == max_points) {
    126                             glamor_put_vbo_space(screen);
    127                             glDrawArrays(GL_POINTS, 0, num_points);
    128                             num_points = 0;
    129                         }
    130                     }
    131                     glyphbits += glyph_stride;
    132                 }
    133             }
    134             x += charinfo->metrics.characterWidth;
    135         }
    136 
    137         if (num_points) {
    138             glamor_put_vbo_space(screen);
    139             glDrawArrays(GL_POINTS, 0, num_points);
    140         }
    141     }
    142 
    143     ret = TRUE;
    144 
    145 bail:
    146     glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
    147 
    148     return ret;
    149 }
    150 
    151 void
    152 glamor_poly_glyph_blt(DrawablePtr drawable, GCPtr gc,
    153                       int start_x, int y, unsigned int nglyph,
    154                       CharInfoPtr *ppci, void *pglyph_base)
    155 {
    156     if (glamor_poly_glyph_blt_gl(drawable, gc, start_x, y, nglyph, ppci,
    157                                  pglyph_base))
    158         return;
    159     miPolyGlyphBlt(drawable, gc, start_x, y, nglyph,
    160                    ppci, pglyph_base);
    161 }
    162 
    163 static Bool
    164 glamor_push_pixels_gl(GCPtr gc, PixmapPtr bitmap,
    165                       DrawablePtr drawable, int w, int h, int x, int y)
    166 {
    167     ScreenPtr screen = drawable->pScreen;
    168     glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
    169     PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
    170     glamor_pixmap_private *pixmap_priv;
    171     uint8_t *bitmap_data = bitmap->devPrivate.ptr;
    172     int bitmap_stride = bitmap->devKind;
    173     glamor_program *prog;
    174     RegionPtr clip = gc->pCompositeClip;
    175     int box_index;
    176     int yy, xx;
    177     int num_points;
    178     INT16 *points = NULL;
    179     char *vbo_offset;
    180     Bool ret = FALSE;
    181 
    182     if (w * h > MAXINT / (2 * sizeof(float)))
    183         goto bail;
    184 
    185     pixmap_priv = glamor_get_pixmap_private(pixmap);
    186     if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
    187         goto bail;
    188 
    189     glamor_make_current(glamor_priv);
    190 
    191     prog = glamor_use_program_fill(pixmap, gc,
    192                                    &glamor_priv->poly_glyph_blt_progs,
    193                                    &glamor_facet_poly_glyph_blt);
    194     if (!prog)
    195         goto bail;
    196 
    197     glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
    198 
    199     points = glamor_get_vbo_space(screen, w * h * sizeof(INT16) * 2,
    200                                   &vbo_offset);
    201     num_points = 0;
    202 
    203     /* Note that because fb sets miTranslate in the GC, our incoming X
    204      * and Y are in screen coordinate space (same for spans, but not
    205      * other operations).
    206      */
    207 
    208     for (yy = 0; yy < h; yy++) {
    209         uint8_t *bitmap_row = bitmap_data + yy * bitmap_stride;
    210         for (xx = 0; xx < w; xx++) {
    211             if (bitmap_row[xx / 8] & (1 << xx % 8) &&
    212                 RegionContainsPoint(clip,
    213                                     x + xx,
    214                                     y + yy,
    215                                     NULL)) {
    216                 *points++ = x + xx;
    217                 *points++ = y + yy;
    218                 num_points++;
    219             }
    220         }
    221     }
    222     glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_SHORT,
    223                           GL_FALSE, 0, vbo_offset);
    224 
    225     glamor_put_vbo_space(screen);
    226 
    227     glamor_pixmap_loop(pixmap_priv, box_index) {
    228         if (!glamor_set_destination_drawable(drawable, box_index, FALSE, TRUE,
    229                                              prog->matrix_uniform, NULL, NULL))
    230             goto bail;
    231 
    232         glDrawArrays(GL_POINTS, 0, num_points);
    233     }
    234 
    235     ret = TRUE;
    236 
    237 bail:
    238     glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
    239 
    240     return ret;
    241 }
    242 
    243 void
    244 glamor_push_pixels(GCPtr pGC, PixmapPtr pBitmap,
    245                    DrawablePtr pDrawable, int w, int h, int x, int y)
    246 {
    247     if (glamor_push_pixels_gl(pGC, pBitmap, pDrawable, w, h, x, y))
    248         return;
    249 
    250     miPushPixels(pGC, pBitmap, pDrawable, w, h, x, y);
    251 }