2 * Copyright © 2014 Keith Packard
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.
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
23 #include "glamor_priv.h"
24 #include <dixfontstr.h>
25 #include "glamor_transform.h"
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
34 glamor_get_glyphs(FontPtr font
, glamor_font_t
*glamor_font
,
35 int count
, char *chars
, Bool sixteen
, CharInfoPtr
*charinfo
)
37 unsigned long nglyphs
;
38 FontEncoding encoding
;
44 if (FONTLASTROW(font
) == 0)
45 encoding
= Linear16Bit
;
50 encoding
= Linear8Bit
;
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
58 if (glamor_font
->default_char
) {
59 GetGlyphs(font
, (unsigned long) count
, (unsigned char *) chars
,
60 encoding
, &nglyphs
, charinfo
);
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.
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.
77 for (c
= 0; c
< count
; c
++) {
78 GetGlyphs(font
, 1, (unsigned char *) chars
,
79 encoding
, &nglyphs
, &charinfo
[c
]);
87 * Construct quads for the provided list of characters and draw them
91 glamor_text(DrawablePtr drawable
, GCPtr gc
,
92 glamor_font_t
*glamor_font
,
95 int count
, char *s_chars
, CharInfoPtr
*charinfo
,
98 unsigned char *chars
= (unsigned char *) s_chars
;
99 FontPtr font
= gc
->font
;
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
;
111 PixmapPtr pixmap
= glamor_get_drawable_pixmap(drawable
);
112 glamor_pixmap_private
*pixmap_priv
= glamor_get_pixmap_private(pixmap
);
114 /* Set the font as texture 1 */
116 glActiveTexture(GL_TEXTURE1
);
117 glBindTexture(GL_TEXTURE_2D
, glamor_font
->texture_id
);
118 glUniform1i(prog
->font_uniform
, 1);
120 /* Set up the vertex buffers for the font and destination */
122 v
= glamor_get_vbo_space(drawable
->pScreen
, count
* (6 * sizeof (GLshort
)), &vbo_offset
);
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
);
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
));
134 /* Set the vertex coordinates */
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
);
146 x
+= ci
->metrics
.characterWidth
;
149 if (ci
== glamor_font
->default_char
) {
150 row
= glamor_font
->default_row
;
151 col
= glamor_font
->default_col
;
156 if (FONTLASTROW(font
) != 0) {
157 ty
= ((row
- firstRow
) / 2) * glyph_spacing_y
;
158 second_row
= (row
- firstRow
) & 1;
163 if (ci
== glamor_font
->default_char
)
164 col
= glamor_font
->default_col
;
169 tx
= (col
- firstCol
) * glyph_spacing_x
;
170 /* adjust for second row layout */
171 tx
+= second_row
* glamor_font
->row_width
* 8;
183 chars
+= 1 + sixteen
;
185 glamor_put_vbo_space(drawable
->pScreen
);
189 glEnable(GL_SCISSOR_TEST
);
191 glamor_pixmap_loop(pixmap_priv
, box_index
) {
192 BoxPtr box
= RegionRects(gc
->pCompositeClip
);
193 int nbox
= RegionNumRects(gc
->pCompositeClip
);
195 glamor_set_destination_drawable(drawable
, box_index
, TRUE
, FALSE
,
196 prog
->matrix_uniform
,
199 /* Run over the clip list, drawing the glyphs
204 glScissor(box
->x1
+ off_x
,
209 glDrawArraysInstanced(GL_TRIANGLE_STRIP
, 0, 4, nglyph
);
212 glDisable(GL_SCISSOR_TEST
);
215 glVertexAttribDivisor(GLAMOR_VERTEX_SOURCE
, 0);
216 glDisableVertexAttribArray(GLAMOR_VERTEX_SOURCE
);
217 glVertexAttribDivisor(GLAMOR_VERTEX_POS
, 0);
218 glDisableVertexAttribArray(GLAMOR_VERTEX_POS
);
223 static const char vs_vars_text
[] =
224 "in vec4 primitive;\n"
226 "out vec2 glyph_pos;\n";
228 static const char vs_exec_text
[] =
229 " vec2 pos = primitive.zw * vec2(gl_VertexID&1, (gl_VertexID&2)>>1);\n"
230 GLAMOR_POS(gl_Position
, (primitive
.xy
+ pos
))
231 " glyph_pos = source + pos;\n";
233 static const char fs_vars_text
[] =
234 "in vec2 glyph_pos;\n";
236 static const char fs_exec_text
[] =
237 " ivec2 itile_texture = ivec2(glyph_pos);\n"
238 #if BITMAP_BIT_ORDER == MSBFirst
239 " uint x = uint(7) - uint(itile_texture.x & 7);\n"
241 " uint x = uint(itile_texture.x & 7);\n"
243 " itile_texture.x >>= 3;\n"
244 " uint texel = texelFetch(font, itile_texture, 0).x;\n"
245 " uint bit = (texel >> x) & uint(1);\n"
246 " if (bit == uint(0))\n"
249 static const char fs_exec_te
[] =
250 " ivec2 itile_texture = ivec2(glyph_pos);\n"
251 #if BITMAP_BIT_ORDER == MSBFirst
252 " uint x = uint(7) - uint(itile_texture.x & 7);\n"
254 " uint x = uint(itile_texture.x & 7);\n"
256 " itile_texture.x >>= 3;\n"
257 " uint texel = texelFetch(font, itile_texture, 0).x;\n"
258 " uint bit = (texel >> x) & uint(1);\n"
259 " if (bit == uint(0))\n"
260 " frag_color = bg;\n"
262 " frag_color = fg;\n";
264 static const glamor_facet glamor_facet_poly_text
= {
267 .vs_vars
= vs_vars_text
,
268 .vs_exec
= vs_exec_text
,
269 .fs_vars
= fs_vars_text
,
270 .fs_exec
= fs_exec_text
,
271 .source_name
= "source",
272 .locations
= glamor_program_location_font
,
276 glamor_poly_text(DrawablePtr drawable
, GCPtr gc
,
277 int x
, int y
, int count
, char *chars
, Bool sixteen
, int *final_pos
)
279 ScreenPtr screen
= drawable
->pScreen
;
280 glamor_screen_private
*glamor_priv
= glamor_get_screen_private(screen
);
281 PixmapPtr pixmap
= glamor_get_drawable_pixmap(drawable
);
282 glamor_program
*prog
;
283 glamor_pixmap_private
*pixmap_priv
;
284 glamor_font_t
*glamor_font
;
285 CharInfoPtr charinfo
[255]; /* encoding only has 1 byte for count */
287 glamor_font
= glamor_font_get(drawable
->pScreen
, gc
->font
);
291 glamor_get_glyphs(gc
->font
, glamor_font
, count
, chars
, sixteen
, charinfo
);
293 pixmap_priv
= glamor_get_pixmap_private(pixmap
);
294 if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv
))
297 glamor_make_current(glamor_priv
);
299 prog
= glamor_use_program_fill(drawable
, gc
, &glamor_priv
->poly_text_progs
, &glamor_facet_poly_text
);
304 x
= glamor_text(drawable
, gc
, glamor_font
, prog
,
305 x
, y
, count
, chars
, charinfo
, sixteen
);
315 glamor_poly_text8(DrawablePtr drawable
, GCPtr gc
,
316 int x
, int y
, int count
, char *chars
)
320 if (glamor_poly_text(drawable
, gc
, x
, y
, count
, chars
, FALSE
, &final_pos
))
322 return miPolyText8(drawable
, gc
, x
, y
, count
, chars
);
326 glamor_poly_text16(DrawablePtr drawable
, GCPtr gc
,
327 int x
, int y
, int count
, unsigned short *chars
)
331 if (glamor_poly_text(drawable
, gc
, x
, y
, count
, (char *) chars
, TRUE
, &final_pos
))
333 return miPolyText16(drawable
, gc
, x
, y
, count
, chars
);
337 * Draw image text, which is always solid in copy mode and has the
338 * background cleared while painting the text. For fonts which have
339 * their bitmap metrics exactly equal to the area to clear, we can use
340 * the accelerated version which paints both fg and bg at the same
341 * time. Otherwise, clear the whole area and then paint the glyphs on
345 static const glamor_facet glamor_facet_image_text
= {
346 .name
= "image_text",
348 .vs_vars
= vs_vars_text
,
349 .vs_exec
= vs_exec_text
,
350 .fs_vars
= fs_vars_text
,
351 .fs_exec
= fs_exec_text
,
352 .source_name
= "source",
353 .locations
= glamor_program_location_font
,
357 use_image_solid(DrawablePtr drawable
, GCPtr gc
, glamor_program
*prog
, void *arg
)
359 return glamor_set_solid(drawable
, gc
, FALSE
, prog
->fg_uniform
);
362 static const glamor_facet glamor_facet_image_fill
= {
364 .fs_exec
= " frag_color = fg;\n",
365 .locations
= glamor_program_location_fg
,
366 .use
= use_image_solid
,
370 glamor_te_text_use(DrawablePtr drawable
, GCPtr gc
, glamor_program
*prog
, void *arg
)
372 if (!glamor_set_solid(drawable
, gc
, FALSE
, prog
->fg_uniform
))
374 glamor_set_color(drawable
, gc
->bgPixel
, prog
->bg_uniform
);
378 static const glamor_facet glamor_facet_te_text
= {
381 .vs_vars
= vs_vars_text
,
382 .vs_exec
= vs_exec_text
,
383 .fs_vars
= fs_vars_text
,
384 .fs_exec
= fs_exec_te
,
385 .locations
= glamor_program_location_fg
| glamor_program_location_bg
| glamor_program_location_font
,
386 .source_name
= "source",
387 .use
= glamor_te_text_use
,
391 glamor_image_text(DrawablePtr drawable
, GCPtr gc
,
392 int x
, int y
, int count
, char *chars
,
395 ScreenPtr screen
= drawable
->pScreen
;
396 glamor_screen_private
*glamor_priv
= glamor_get_screen_private(screen
);
397 PixmapPtr pixmap
= glamor_get_drawable_pixmap(drawable
);
398 glamor_program
*prog
;
399 glamor_pixmap_private
*pixmap_priv
;
400 glamor_font_t
*glamor_font
;
401 const glamor_facet
*prim_facet
;
402 const glamor_facet
*fill_facet
;
403 CharInfoPtr charinfo
[255]; /* encoding only has 1 byte for count */
405 pixmap_priv
= glamor_get_pixmap_private(pixmap
);
406 if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv
))
409 glamor_font
= glamor_font_get(drawable
->pScreen
, gc
->font
);
413 glamor_get_glyphs(gc
->font
, glamor_font
, count
, chars
, sixteen
, charinfo
);
415 glamor_make_current(glamor_priv
);
417 if (TERMINALFONT(gc
->font
))
418 prog
= &glamor_priv
->te_text_prog
;
420 prog
= &glamor_priv
->image_text_prog
;
426 if (TERMINALFONT(gc
->font
)) {
427 prim_facet
= &glamor_facet_te_text
;
430 prim_facet
= &glamor_facet_image_text
;
431 fill_facet
= &glamor_facet_image_fill
;
434 if (!glamor_build_program(screen
, prog
, prim_facet
, fill_facet
, NULL
, NULL
))
438 if (!TERMINALFONT(gc
->font
)) {
444 /* Check planemask before drawing background to
445 * bail early if it's not OK
447 if (!glamor_set_planemask(gc
->depth
, gc
->planemask
))
449 for (c
= 0; c
< count
; c
++)
451 width
+= charinfo
[c
]->metrics
.characterWidth
;
454 box
.x1
= drawable
->x
+ x
;
455 box
.x2
= drawable
->x
+ x
+ width
;
457 box
.x1
= drawable
->x
+ x
+ width
;
458 box
.x2
= drawable
->x
+ x
;
460 box
.y1
= drawable
->y
+ y
- gc
->font
->info
.fontAscent
;
461 box
.y2
= drawable
->y
+ y
+ gc
->font
->info
.fontDescent
;
462 RegionInit(®ion
, &box
, 1);
463 RegionIntersect(®ion
, ®ion
, gc
->pCompositeClip
);
464 RegionTranslate(®ion
, -drawable
->x
, -drawable
->y
);
465 glamor_solid_boxes(drawable
, RegionRects(®ion
), RegionNumRects(®ion
), gc
->bgPixel
);
466 RegionUninit(®ion
);
469 if (!glamor_use_program(drawable
, gc
, prog
, NULL
))
472 (void) glamor_text(drawable
, gc
, glamor_font
, prog
,
473 x
, y
, count
, chars
, charinfo
, sixteen
);
482 glamor_image_text8(DrawablePtr drawable
, GCPtr gc
,
483 int x
, int y
, int count
, char *chars
)
485 if (!glamor_image_text(drawable
, gc
, x
, y
, count
, chars
, FALSE
))
486 miImageText8(drawable
, gc
, x
, y
, count
, chars
);
490 glamor_image_text16(DrawablePtr drawable
, GCPtr gc
,
491 int x
, int y
, int count
, unsigned short *chars
)
493 if (!glamor_image_text(drawable
, gc
, x
, y
, count
, (char *) chars
, TRUE
))
494 miImageText16(drawable
, gc
, x
, y
, count
, chars
);