xref: /OK3568_Linux_fs/external/xserver/glamor/glamor_text.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright © 2014 Keith Packard
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22 
23 #include "glamor_priv.h"
24 #include <dixfontstr.h>
25 #include "glamor_transform.h"
26 
27 /*
28  * Fill in the array of charinfo pointers for the provided characters. For
29  * missing characters, place a NULL in the array so that the charinfo array
30  * aligns exactly with chars
31  */
32 
33 static void
glamor_get_glyphs(FontPtr font,glamor_font_t * glamor_font,int count,char * chars,Bool sixteen,CharInfoPtr * charinfo)34 glamor_get_glyphs(FontPtr font, glamor_font_t *glamor_font,
35                   int count, char *chars, Bool sixteen, CharInfoPtr *charinfo)
36 {
37     unsigned long nglyphs;
38     FontEncoding encoding;
39     int char_step;
40     int c;
41 
42     if (sixteen) {
43         char_step = 2;
44         if (FONTLASTROW(font) == 0)
45             encoding = Linear16Bit;
46         else
47             encoding = TwoD16Bit;
48     } else {
49         char_step = 1;
50         encoding = Linear8Bit;
51     }
52 
53     /* If the font has a default character, then we shouldn't have to
54      * worry about missing glyphs, so just get the whole string all at
55      * once. Otherwise, we have to fetch chars one at a time to notice
56      * missing ones.
57      */
58     if (glamor_font->default_char) {
59         GetGlyphs(font, (unsigned long) count, (unsigned char *) chars,
60                   encoding, &nglyphs, charinfo);
61 
62         /* Make sure it worked. There's a bug in libXfont through
63          * version 1.4.7 which would cause it to fail when the font is
64          * a 2D font without a first row, and the application sends a
65          * 1-d request. In this case, libXfont would return zero
66          * glyphs, even when the font had a default character.
67          *
68          * It's easy enough for us to work around that bug here by
69          * simply checking the returned nglyphs and falling through to
70          * the one-at-a-time code below. Not doing this check would
71          * result in uninitialized memory accesses in the rendering code.
72          */
73         if (nglyphs == count)
74             return;
75     }
76 
77     for (c = 0; c < count; c++) {
78         GetGlyphs(font, 1, (unsigned char *) chars,
79                   encoding, &nglyphs, &charinfo[c]);
80         if (!nglyphs)
81             charinfo[c] = NULL;
82         chars += char_step;
83     }
84 }
85 
86 /*
87  * Construct quads for the provided list of characters and draw them
88  */
89 
90 static int
glamor_text(DrawablePtr drawable,GCPtr gc,glamor_font_t * glamor_font,glamor_program * prog,int x,int y,int count,char * s_chars,CharInfoPtr * charinfo,Bool sixteen)91 glamor_text(DrawablePtr drawable, GCPtr gc,
92             glamor_font_t *glamor_font,
93             glamor_program *prog,
94             int x, int y,
95             int count, char *s_chars, CharInfoPtr *charinfo,
96             Bool sixteen)
97 {
98     unsigned char *chars = (unsigned char *) s_chars;
99     FontPtr font = gc->font;
100     int off_x, off_y;
101     int c;
102     int nglyph;
103     GLshort *v;
104     char *vbo_offset;
105     CharInfoPtr ci;
106     int firstRow = font->info.firstRow;
107     int firstCol = font->info.firstCol;
108     int glyph_spacing_x = glamor_font->glyph_width_bytes * 8;
109     int glyph_spacing_y = glamor_font->glyph_height;
110     int box_index;
111     PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
112     glamor_pixmap_private *pixmap_priv = glamor_get_pixmap_private(pixmap);
113 
114     /* Set the font as texture 1 */
115 
116     glActiveTexture(GL_TEXTURE1);
117     glBindTexture(GL_TEXTURE_2D, glamor_font->texture_id);
118     glUniform1i(prog->font_uniform, 1);
119 
120     /* Set up the vertex buffers for the font and destination */
121 
122     v = glamor_get_vbo_space(drawable->pScreen, count * (6 * sizeof (GLshort)), &vbo_offset);
123 
124     glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
125     glVertexAttribDivisor(GLAMOR_VERTEX_POS, 1);
126     glVertexAttribPointer(GLAMOR_VERTEX_POS, 4, GL_SHORT, GL_FALSE,
127                           6 * sizeof (GLshort), vbo_offset);
128 
129     glEnableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
130     glVertexAttribDivisor(GLAMOR_VERTEX_SOURCE, 1);
131     glVertexAttribPointer(GLAMOR_VERTEX_SOURCE, 2, GL_SHORT, GL_FALSE,
132                           6 * sizeof (GLshort), vbo_offset + 4 * sizeof (GLshort));
133 
134     /* Set the vertex coordinates */
135     nglyph = 0;
136 
137     for (c = 0; c < count; c++) {
138         if ((ci = *charinfo++)) {
139             int     x1 = x + ci->metrics.leftSideBearing;
140             int     y1 = y - ci->metrics.ascent;
141             int     width = GLYPHWIDTHPIXELS(ci);
142             int     height = GLYPHHEIGHTPIXELS(ci);
143             int     tx, ty = 0;
144             int     row = 0, col;
145             int     second_row = 0;
146             x += ci->metrics.characterWidth;
147 
148             if (sixteen) {
149                 if (ci == glamor_font->default_char) {
150                     row = glamor_font->default_row;
151                     col = glamor_font->default_col;
152                 } else {
153                     row = chars[0];
154                     col = chars[1];
155                 }
156                 if (FONTLASTROW(font) != 0) {
157                     ty = ((row - firstRow) / 2) * glyph_spacing_y;
158                     second_row = (row - firstRow) & 1;
159                 }
160                 else
161                     col += row << 8;
162             } else {
163                 if (ci == glamor_font->default_char)
164                     col = glamor_font->default_col;
165                 else
166                     col = chars[0];
167             }
168 
169             tx = (col - firstCol) * glyph_spacing_x;
170             /* adjust for second row layout */
171             tx += second_row * glamor_font->row_width * 8;
172 
173             v[ 0] = x1;
174             v[ 1] = y1;
175             v[ 2] = width;
176             v[ 3] = height;
177             v[ 4] = tx;
178             v[ 5] = ty;
179 
180             v += 6;
181             nglyph++;
182         }
183         chars += 1 + sixteen;
184     }
185     glamor_put_vbo_space(drawable->pScreen);
186 
187     if (nglyph != 0) {
188 
189         glEnable(GL_SCISSOR_TEST);
190 
191         glamor_pixmap_loop(pixmap_priv, box_index) {
192             BoxPtr box = RegionRects(gc->pCompositeClip);
193             int nbox = RegionNumRects(gc->pCompositeClip);
194 
195             glamor_set_destination_drawable(drawable, box_index, TRUE, FALSE,
196                                             prog->matrix_uniform,
197                                             &off_x, &off_y);
198 
199             /* Run over the clip list, drawing the glyphs
200              * in each box
201              */
202 
203             while (nbox--) {
204                 glScissor(box->x1 + off_x,
205                           box->y1 + off_y,
206                           box->x2 - box->x1,
207                           box->y2 - box->y1);
208                 box++;
209                 glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, nglyph);
210             }
211         }
212         glDisable(GL_SCISSOR_TEST);
213     }
214 
215     glVertexAttribDivisor(GLAMOR_VERTEX_SOURCE, 0);
216     glDisableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
217     glVertexAttribDivisor(GLAMOR_VERTEX_POS, 0);
218     glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
219 
220     glamor_pixmap_invalid(pixmap);
221 
222     return x;
223 }
224 
225 static const char vs_vars_text[] =
226     "attribute vec4 primitive;\n"
227     "attribute vec2 source;\n"
228     "varying vec2 glyph_pos;\n";
229 
230 static const char vs_exec_text[] =
231     "       vec2 pos = primitive.zw * vec2(gl_VertexID&1, (gl_VertexID&2)>>1);\n"
232     GLAMOR_POS(gl_Position, (primitive.xy + pos))
233     "       glyph_pos = source + pos;\n";
234 
235 static const char fs_vars_text[] =
236     "varying vec2 glyph_pos;\n";
237 
238 static const char fs_exec_text[] =
239     "       ivec2 itile_texture = ivec2(glyph_pos);\n"
240     "       uint x = uint(itile_texture.x & 7);\n"
241     "       itile_texture.x >>= 3;\n"
242     "       uint texel = texelFetch(font, itile_texture, 0).x;\n"
243     "       uint bit = (texel >> x) & uint(1);\n"
244     "       if (bit == uint(0))\n"
245     "               discard;\n";
246 
247 static const char fs_exec_te[] =
248     "       ivec2 itile_texture = ivec2(glyph_pos);\n"
249     "       uint x = uint(itile_texture.x & 7);\n"
250     "       itile_texture.x >>= 3;\n"
251     "       uint texel = texelFetch(font, itile_texture, 0).x;\n"
252     "       uint bit = (texel >> x) & uint(1);\n"
253     "       if (bit == uint(0))\n"
254     "               gl_FragColor = bg;\n"
255     "       else\n"
256     "               gl_FragColor = fg;\n";
257 
258 static const glamor_facet glamor_facet_poly_text = {
259     .name = "poly_text",
260     .version = 130,
261     .vs_vars = vs_vars_text,
262     .vs_exec = vs_exec_text,
263     .fs_vars = fs_vars_text,
264     .fs_exec = fs_exec_text,
265     .source_name = "source",
266     .locations = glamor_program_location_font,
267 };
268 
269 static Bool
glamor_poly_text(DrawablePtr drawable,GCPtr gc,int x,int y,int count,char * chars,Bool sixteen,int * final_pos)270 glamor_poly_text(DrawablePtr drawable, GCPtr gc,
271                  int x, int y, int count, char *chars, Bool sixteen, int *final_pos)
272 {
273     ScreenPtr screen = drawable->pScreen;
274     glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
275     PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
276     glamor_program *prog;
277     glamor_pixmap_private *pixmap_priv;
278     glamor_font_t *glamor_font;
279     CharInfoPtr charinfo[255];  /* encoding only has 1 byte for count */
280 
281     glamor_font = glamor_font_get(drawable->pScreen, gc->font);
282     if (!glamor_font)
283         goto bail;
284 
285     glamor_get_glyphs(gc->font, glamor_font, count, chars, sixteen, charinfo);
286 
287     pixmap_priv = glamor_get_pixmap_private(pixmap);
288     if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
289         goto bail;
290 
291     glamor_make_current(glamor_priv);
292 
293     prog = glamor_use_program_fill(pixmap, gc, &glamor_priv->poly_text_progs, &glamor_facet_poly_text);
294 
295     if (!prog)
296         goto bail;
297 
298     x = glamor_text(drawable, gc, glamor_font, prog,
299                     x, y, count, chars, charinfo, sixteen);
300 
301     *final_pos = x;
302     return TRUE;
303 
304 bail:
305     return FALSE;
306 }
307 
308 int
glamor_poly_text8(DrawablePtr drawable,GCPtr gc,int x,int y,int count,char * chars)309 glamor_poly_text8(DrawablePtr drawable, GCPtr gc,
310                    int x, int y, int count, char *chars)
311 {
312     int final_pos;
313 
314     if (glamor_poly_text(drawable, gc, x, y, count, chars, FALSE, &final_pos))
315         return final_pos;
316     return miPolyText8(drawable, gc, x, y, count, chars);
317 }
318 
319 int
glamor_poly_text16(DrawablePtr drawable,GCPtr gc,int x,int y,int count,unsigned short * chars)320 glamor_poly_text16(DrawablePtr drawable, GCPtr gc,
321                     int x, int y, int count, unsigned short *chars)
322 {
323     int final_pos;
324 
325     if (glamor_poly_text(drawable, gc, x, y, count, (char *) chars, TRUE, &final_pos))
326         return final_pos;
327     return miPolyText16(drawable, gc, x, y, count, chars);
328 }
329 
330 /*
331  * Draw image text, which is always solid in copy mode and has the
332  * background cleared while painting the text. For fonts which have
333  * their bitmap metrics exactly equal to the area to clear, we can use
334  * the accelerated version which paints both fg and bg at the same
335  * time. Otherwise, clear the whole area and then paint the glyphs on
336  * top
337  */
338 
339 static const glamor_facet glamor_facet_image_text = {
340     .name = "image_text",
341     .version = 130,
342     .vs_vars = vs_vars_text,
343     .vs_exec = vs_exec_text,
344     .fs_vars = fs_vars_text,
345     .fs_exec = fs_exec_text,
346     .source_name = "source",
347     .locations = glamor_program_location_font,
348 };
349 
350 static Bool
use_image_solid(PixmapPtr pixmap,GCPtr gc,glamor_program * prog,void * arg)351 use_image_solid(PixmapPtr pixmap, GCPtr gc, glamor_program *prog, void *arg)
352 {
353     return glamor_set_solid(pixmap, gc, FALSE, prog->fg_uniform);
354 }
355 
356 static const glamor_facet glamor_facet_image_fill = {
357     .name = "solid",
358     .fs_exec = "       gl_FragColor = fg;\n",
359     .locations = glamor_program_location_fg,
360     .use = use_image_solid,
361 };
362 
363 static Bool
glamor_te_text_use(PixmapPtr pixmap,GCPtr gc,glamor_program * prog,void * arg)364 glamor_te_text_use(PixmapPtr pixmap, GCPtr gc, glamor_program *prog, void *arg)
365 {
366     if (!glamor_set_solid(pixmap, gc, FALSE, prog->fg_uniform))
367         return FALSE;
368     glamor_set_color(pixmap, gc->bgPixel, prog->bg_uniform);
369     return TRUE;
370 }
371 
372 static const glamor_facet glamor_facet_te_text = {
373     .name = "te_text",
374     .version = 130,
375     .vs_vars = vs_vars_text,
376     .vs_exec = vs_exec_text,
377     .fs_vars = fs_vars_text,
378     .fs_exec = fs_exec_te,
379     .locations = glamor_program_location_fg | glamor_program_location_bg | glamor_program_location_font,
380     .source_name = "source",
381     .use = glamor_te_text_use,
382 };
383 
384 static Bool
glamor_image_text(DrawablePtr drawable,GCPtr gc,int x,int y,int count,char * chars,Bool sixteen)385 glamor_image_text(DrawablePtr drawable, GCPtr gc,
386                   int x, int y, int count, char *chars,
387                   Bool sixteen)
388 {
389     ScreenPtr screen = drawable->pScreen;
390     glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
391     PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
392     glamor_program *prog;
393     glamor_pixmap_private *pixmap_priv;
394     glamor_font_t *glamor_font;
395     const glamor_facet *prim_facet;
396     const glamor_facet *fill_facet;
397     CharInfoPtr charinfo[255];  /* encoding only has 1 byte for count */
398 
399     pixmap_priv = glamor_get_pixmap_private(pixmap);
400     if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
401         return FALSE;
402 
403     glamor_font = glamor_font_get(drawable->pScreen, gc->font);
404     if (!glamor_font)
405         return FALSE;
406 
407     glamor_get_glyphs(gc->font, glamor_font, count, chars, sixteen, charinfo);
408 
409     glamor_make_current(glamor_priv);
410 
411     if (TERMINALFONT(gc->font))
412         prog = &glamor_priv->te_text_prog;
413     else
414         prog = &glamor_priv->image_text_prog;
415 
416     if (prog->failed)
417         goto bail;
418 
419     if (!prog->prog) {
420         if (TERMINALFONT(gc->font)) {
421             prim_facet = &glamor_facet_te_text;
422             fill_facet = NULL;
423         } else {
424             prim_facet = &glamor_facet_image_text;
425             fill_facet = &glamor_facet_image_fill;
426         }
427 
428         if (!glamor_build_program(screen, prog, prim_facet, fill_facet, NULL, NULL))
429             goto bail;
430     }
431 
432     if (!TERMINALFONT(gc->font)) {
433         int width = 0;
434         int c;
435         RegionRec region;
436         BoxRec box;
437         int off_x, off_y;
438 
439         /* Check planemask before drawing background to
440          * bail early if it's not OK
441          */
442         if (!glamor_set_planemask(gc->depth, gc->planemask))
443             goto bail;
444         for (c = 0; c < count; c++)
445             if (charinfo[c])
446                 width += charinfo[c]->metrics.characterWidth;
447 
448         glamor_get_drawable_deltas(drawable, pixmap, &off_x, &off_y);
449 
450         if (width >= 0) {
451             box.x1 = drawable->x + x;
452             box.x2 = drawable->x + x + width;
453         } else {
454             box.x1 = drawable->x + x + width;
455             box.x2 = drawable->x + x;
456         }
457         box.y1 = drawable->y + y - gc->font->info.fontAscent;
458         box.y2 = drawable->y + y + gc->font->info.fontDescent;
459         RegionInit(&region, &box, 1);
460         RegionIntersect(&region, &region, gc->pCompositeClip);
461         RegionTranslate(&region, off_x, off_y);
462         glamor_solid_boxes(pixmap, RegionRects(&region), RegionNumRects(&region), gc->bgPixel);
463         RegionUninit(&region);
464     }
465 
466     if (!glamor_use_program(pixmap, gc, prog, NULL))
467         goto bail;
468 
469     (void) glamor_text(drawable, gc, glamor_font, prog,
470                        x, y, count, chars, charinfo, sixteen);
471 
472     return TRUE;
473 
474 bail:
475     return FALSE;
476 }
477 
478 void
glamor_image_text8(DrawablePtr drawable,GCPtr gc,int x,int y,int count,char * chars)479 glamor_image_text8(DrawablePtr drawable, GCPtr gc,
480                    int x, int y, int count, char *chars)
481 {
482     if (!glamor_image_text(drawable, gc, x, y, count, chars, FALSE))
483         miImageText8(drawable, gc, x, y, count, chars);
484 }
485 
486 void
glamor_image_text16(DrawablePtr drawable,GCPtr gc,int x,int y,int count,unsigned short * chars)487 glamor_image_text16(DrawablePtr drawable, GCPtr gc,
488                     int x, int y, int count, unsigned short *chars)
489 {
490     if (!glamor_image_text(drawable, gc, x, y, count, (char *) chars, TRUE))
491         miImageText16(drawable, gc, x, y, count, chars);
492 }
493