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
33static 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
40static Bool
41glamor_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 BITMAP_BIT_ORDER == MSBFirst
105                        if (!(*glyph & (128 >> (xx & 7))))
106#else
107                        if (!(*glyph & (1 << (xx & 7))))
108#endif
109                            continue;
110
111                        if (!RegionContainsPoint(clip, pt_x_i, pt_y_i, NULL))
112                            continue;
113
114                        if (!num_points) {
115                            points = glamor_get_vbo_space(screen,
116                                                          max_points *
117                                                          (2 * sizeof (INT16)),
118                                                          &vbo_offset);
119
120                            glVertexAttribPointer(GLAMOR_VERTEX_POS,
121                                                  2, GL_SHORT,
122                                                  GL_FALSE, 0, vbo_offset);
123                        }
124
125                        *points++ = pt_x_i;
126                        *points++ = pt_y_i;
127                        num_points++;
128
129                        if (num_points == max_points) {
130                            glamor_put_vbo_space(screen);
131                            glDrawArrays(GL_POINTS, 0, num_points);
132                            num_points = 0;
133                        }
134                    }
135                    glyphbits += glyph_stride;
136                }
137            }
138            x += charinfo->metrics.characterWidth;
139        }
140
141        if (num_points) {
142            glamor_put_vbo_space(screen);
143            glDrawArrays(GL_POINTS, 0, num_points);
144        }
145    }
146
147    ret = TRUE;
148
149bail:
150    glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
151
152    return ret;
153}
154
155void
156glamor_poly_glyph_blt(DrawablePtr drawable, GCPtr gc,
157                      int start_x, int y, unsigned int nglyph,
158                      CharInfoPtr *ppci, void *pglyph_base)
159{
160    if (glamor_poly_glyph_blt_gl(drawable, gc, start_x, y, nglyph, ppci,
161                                 pglyph_base))
162        return;
163    miPolyGlyphBlt(drawable, gc, start_x, y, nglyph,
164                   ppci, pglyph_base);
165}
166
167static Bool
168glamor_push_pixels_gl(GCPtr gc, PixmapPtr bitmap,
169                      DrawablePtr drawable, int w, int h, int x, int y)
170{
171    ScreenPtr screen = drawable->pScreen;
172    glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
173    PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
174    glamor_pixmap_private *pixmap_priv;
175    uint8_t *bitmap_data = bitmap->devPrivate.ptr;
176    int bitmap_stride = bitmap->devKind;
177    glamor_program *prog;
178    RegionPtr clip = gc->pCompositeClip;
179    int box_index;
180    int yy, xx;
181    int num_points;
182    INT16 *points = NULL;
183    char *vbo_offset;
184    Bool ret = FALSE;
185
186    if (w * h > MAXINT / (2 * sizeof(float)))
187        goto bail;
188
189    pixmap_priv = glamor_get_pixmap_private(pixmap);
190    if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
191        goto bail;
192
193    glamor_make_current(glamor_priv);
194
195    prog = glamor_use_program_fill(pixmap, gc,
196                                   &glamor_priv->poly_glyph_blt_progs,
197                                   &glamor_facet_poly_glyph_blt);
198    if (!prog)
199        goto bail;
200
201    glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
202
203    points = glamor_get_vbo_space(screen, w * h * sizeof(INT16) * 2,
204                                  &vbo_offset);
205    num_points = 0;
206
207    /* Note that because fb sets miTranslate in the GC, our incoming X
208     * and Y are in screen coordinate space (same for spans, but not
209     * other operations).
210     */
211
212    for (yy = 0; yy < h; yy++) {
213        uint8_t *bitmap_row = bitmap_data + yy * bitmap_stride;
214        for (xx = 0; xx < w; xx++) {
215#if BITMAP_BIT_ORDER == MSBFirst
216            if (bitmap_row[xx / 8] & (128 >> xx % 8) &&
217#else
218            if (bitmap_row[xx / 8] & (1 << xx % 8) &&
219#endif
220                RegionContainsPoint(clip,
221                                    x + xx,
222                                    y + yy,
223                                    NULL)) {
224                *points++ = x + xx;
225                *points++ = y + yy;
226                num_points++;
227            }
228        }
229    }
230    glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_SHORT,
231                          GL_FALSE, 0, vbo_offset);
232
233    glamor_put_vbo_space(screen);
234
235    glamor_pixmap_loop(pixmap_priv, box_index) {
236        if (!glamor_set_destination_drawable(drawable, box_index, FALSE, TRUE,
237                                             prog->matrix_uniform, NULL, NULL))
238            goto bail;
239
240        glDrawArrays(GL_POINTS, 0, num_points);
241    }
242
243    ret = TRUE;
244
245bail:
246    glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
247
248    return ret;
249}
250
251void
252glamor_push_pixels(GCPtr pGC, PixmapPtr pBitmap,
253                   DrawablePtr pDrawable, int w, int h, int x, int y)
254{
255    if (glamor_push_pixels_gl(pGC, pBitmap, pDrawable, w, h, x, y))
256        return;
257
258    miPushPixels(pGC, pBitmap, pDrawable, w, h, x, y);
259}
260