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