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