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
25
#include "glamor_priv.h"
26
#include "glamor_transform.h"
27
#include "glamor_transfer.h"
31
#define DEFAULT_ATLAS_DIM 1024
33
static DevPrivateKeyRec glamor_glyph_private_key;
35
struct glamor_glyph_private {
41
struct glamor_glyph_atlas {
50
static inline struct glamor_glyph_private *glamor_get_glyph_private(PixmapPtr pixmap) {
51
return dixLookupPrivate(&pixmap->devPrivates, &glamor_glyph_private_key);
55
glamor_copy_glyph(PixmapPtr glyph_pixmap,
56
DrawablePtr atlas_draw,
60
DrawablePtr glyph_draw = &glyph_pixmap->drawable;
64
.x2 = glyph_draw->width,
65
.y2 = glyph_draw->height,
67
PixmapPtr upload_pixmap = glyph_pixmap;
69
if (glyph_pixmap->drawable.bitsPerPixel != atlas_draw->bitsPerPixel) {
71
/* If we're dealing with 1-bit glyphs, we copy them to a
72
* temporary 8-bit pixmap and upload them from there, since
73
* that's what GL can handle.
75
ScreenPtr screen = atlas_draw->pScreen;
77
ChangeGCVal changes[2];
79
upload_pixmap = glamor_create_pixmap(screen,
83
GLAMOR_CREATE_PIXMAP_CPU);
87
scratch_gc = GetScratchGC(upload_pixmap->drawable.depth, screen);
89
glamor_destroy_pixmap(upload_pixmap);
92
changes[0].val = 0xff;
93
changes[1].val = 0x00;
94
if (ChangeGC(NullClient, scratch_gc,
95
GCForeground|GCBackground, changes) != Success) {
96
glamor_destroy_pixmap(upload_pixmap);
97
FreeScratchGC(scratch_gc);
100
ValidateGC(&upload_pixmap->drawable, scratch_gc);
102
(*scratch_gc->ops->CopyPlane)(glyph_draw,
103
&upload_pixmap->drawable,
110
glamor_upload_boxes((PixmapPtr) atlas_draw,
114
upload_pixmap->devPrivate.ptr,
115
upload_pixmap->devKind);
117
if (upload_pixmap != glyph_pixmap)
118
glamor_destroy_pixmap(upload_pixmap);
122
glamor_glyph_atlas_init(ScreenPtr screen, struct glamor_glyph_atlas *atlas)
124
glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
125
PictFormatPtr format = atlas->format;
127
atlas->atlas = glamor_create_pixmap(screen, glamor_priv->glyph_atlas_dim,
128
glamor_priv->glyph_atlas_dim, format->depth,
129
GLAMOR_CREATE_FBO_NO_FBO);
130
if (!glamor_pixmap_has_fbo(atlas->atlas)) {
131
glamor_destroy_pixmap(atlas->atlas);
136
atlas->row_height = 0;
143
glamor_glyph_can_add(struct glamor_glyph_atlas *atlas, int dim, DrawablePtr glyph_draw)
146
if (atlas->x + glyph_draw->width > dim) {
148
atlas->y += atlas->row_height;
149
atlas->row_height = 0;
152
/* Check for overfull */
153
if (atlas->y + glyph_draw->height > dim)
160
glamor_glyph_add(struct glamor_glyph_atlas *atlas, DrawablePtr glyph_draw)
162
PixmapPtr glyph_pixmap = (PixmapPtr) glyph_draw;
163
struct glamor_glyph_private *glyph_priv = glamor_get_glyph_private(glyph_pixmap);
165
glamor_copy_glyph(glyph_pixmap, &atlas->atlas->drawable, atlas->x, atlas->y);
167
glyph_priv->x = atlas->x;
168
glyph_priv->y = atlas->y;
169
glyph_priv->serial = atlas->serial;
171
atlas->x += glyph_draw->width;
172
if (atlas->row_height < glyph_draw->height)
173
atlas->row_height = glyph_draw->height;
180
static const glamor_facet glamor_facet_composite_glyphs_130 = {
181
.name = "composite_glyphs",
183
.vs_vars = ("attribute vec4 primitive;\n"
184
"attribute vec2 source;\n"
185
"varying vec2 glyph_pos;\n"),
186
.vs_exec = (" vec2 pos = primitive.zw * vec2(gl_VertexID&1, (gl_VertexID&2)>>1);\n"
187
GLAMOR_POS(gl_Position, (primitive.xy + pos))
188
" glyph_pos = (source + pos) * ATLAS_DIM_INV;\n"),
189
.fs_vars = ("varying vec2 glyph_pos;\n"
191
"out vec4 color1;\n"),
192
.fs_exec = (" vec4 mask = texture2D(atlas, glyph_pos);\n"),
193
.source_name = "source",
194
.locations = glamor_program_location_atlas,
197
static const glamor_facet glamor_facet_composite_glyphs_120 = {
198
.name = "composite_glyphs",
199
.vs_vars = ("attribute vec2 primitive;\n"
200
"attribute vec2 source;\n"
201
"varying vec2 glyph_pos;\n"),
202
.vs_exec = (GLAMOR_POS(gl_Position, primitive)
203
" glyph_pos = source.xy * ATLAS_DIM_INV;\n"),
204
.fs_vars = ("varying vec2 glyph_pos;\n"),
205
.fs_exec = (" vec4 mask = texture2D(atlas, glyph_pos);\n"),
206
.source_name = "source",
207
.locations = glamor_program_location_atlas,
211
glamor_glyph_use_130(glamor_screen_private *glamor_priv) {
212
return glamor_priv->glsl_version >= 130;
216
glamor_glyphs_init_facet(ScreenPtr screen)
218
glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
220
return asprintf(&glamor_priv->glyph_defines, "#define ATLAS_DIM_INV %20.18f\n", 1.0/glamor_priv->glyph_atlas_dim) > 0;
224
glamor_glyphs_fini_facet(ScreenPtr screen)
226
glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
228
free(glamor_priv->glyph_defines);
232
glamor_glyphs_flush(CARD8 op, PicturePtr src, PicturePtr dst,
233
glamor_program *prog,
234
struct glamor_glyph_atlas *atlas, int nglyph)
236
DrawablePtr drawable = dst->pDrawable;
237
glamor_screen_private *glamor_priv = glamor_get_screen_private(drawable->pScreen);
238
PixmapPtr atlas_pixmap = atlas->atlas;
239
glamor_pixmap_private *atlas_priv = glamor_get_pixmap_private(atlas_pixmap);
240
glamor_pixmap_fbo *atlas_fbo = glamor_pixmap_fbo_at(atlas_priv, 0);
241
PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
242
glamor_pixmap_private *pixmap_priv = glamor_get_pixmap_private(pixmap);
246
glamor_put_vbo_space(drawable->pScreen);
248
glEnable(GL_SCISSOR_TEST);
249
glActiveTexture(GL_TEXTURE1);
250
glBindTexture(GL_TEXTURE_2D, atlas_fbo->tex);
253
if (!glamor_use_program_render(prog, op, src, dst))
256
glUniform1i(prog->atlas_uniform, 1);
258
glamor_pixmap_loop(pixmap_priv, box_index) {
259
BoxPtr box = RegionRects(dst->pCompositeClip);
260
int nbox = RegionNumRects(dst->pCompositeClip);
262
glamor_set_destination_drawable(drawable, box_index, TRUE, FALSE,
263
prog->matrix_uniform,
266
/* Run over the clip list, drawing the glyphs
271
glScissor(box->x1 + off_x,
277
if (glamor_glyph_use_130(glamor_priv))
278
glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, nglyph);
280
glamor_glDrawArrays_GL_QUADS(glamor_priv, nglyph);
283
if (prog->alpha != glamor_program_alpha_ca_first)
288
glDisable(GL_SCISSOR_TEST);
290
if (glamor_glyph_use_130(glamor_priv)) {
291
glVertexAttribDivisor(GLAMOR_VERTEX_SOURCE, 0);
292
glVertexAttribDivisor(GLAMOR_VERTEX_POS, 0);
294
glDisableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
295
glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
300
glamor_glyph_start(ScreenPtr screen, int count)
302
glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
306
/* Set up the vertex buffers for the font and destination */
308
if (glamor_glyph_use_130(glamor_priv)) {
309
v = glamor_get_vbo_space(screen, count * (6 * sizeof (GLshort)), &vbo_offset);
311
glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
312
glVertexAttribDivisor(GLAMOR_VERTEX_POS, 1);
313
glVertexAttribPointer(GLAMOR_VERTEX_POS, 4, GL_SHORT, GL_FALSE,
314
6 * sizeof (GLshort), vbo_offset);
316
glEnableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
317
glVertexAttribDivisor(GLAMOR_VERTEX_SOURCE, 1);
318
glVertexAttribPointer(GLAMOR_VERTEX_SOURCE, 2, GL_SHORT, GL_FALSE,
319
6 * sizeof (GLshort), vbo_offset + 4 * sizeof (GLshort));
321
v = glamor_get_vbo_space(screen, count * (16 * sizeof (GLshort)), &vbo_offset);
323
glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
324
glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_SHORT, GL_FALSE,
325
4 * sizeof (GLshort), vbo_offset);
327
glEnableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
328
glVertexAttribPointer(GLAMOR_VERTEX_SOURCE, 2, GL_SHORT, GL_FALSE,
329
4 * sizeof (GLshort), vbo_offset + 2 * sizeof (GLshort));
334
static inline struct glamor_glyph_atlas *
335
glamor_atlas_for_glyph(glamor_screen_private *glamor_priv, DrawablePtr drawable)
337
if (drawable->depth == 32)
338
return glamor_priv->glyph_atlas_argb;
340
return glamor_priv->glyph_atlas_a;
344
glamor_composite_glyphs(CARD8 op,
347
PictFormatPtr glyph_format,
349
INT16 y_src, int nlist, GlyphListPtr list,
354
DrawablePtr drawable = dst->pDrawable;
355
ScreenPtr screen = drawable->pScreen;
356
glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
357
glamor_program *prog = NULL;
358
glamor_program_render *glyphs_program = &glamor_priv->glyphs_program;
359
struct glamor_glyph_atlas *glyph_atlas = NULL;
362
int glyph_atlas_dim = glamor_priv->glyph_atlas_dim;
363
int glyph_max_dim = glamor_priv->glyph_max_dim;
365
int screen_num = screen->myNum;
367
for (n = 0; n < nlist; n++)
368
nglyph += list[n].len;
370
glamor_make_current(glamor_priv);
380
GlyphPtr glyph = *glyphs++;
384
if (glyph->info.width && glyph->info.height) {
385
PicturePtr glyph_pict = GlyphPicture(glyph)[screen_num];
386
DrawablePtr glyph_draw = glyph_pict->pDrawable;
388
/* Need to draw with slow path?
390
if (_X_UNLIKELY(glyph_draw->width > glyph_max_dim ||
391
glyph_draw->height > glyph_max_dim ||
392
!glamor_pixmap_is_memory((PixmapPtr)glyph_draw)))
395
glamor_glyphs_flush(op, src, dst, prog, glyph_atlas, glyphs_queued);
399
glamor_composite(op, src, glyph_pict, dst,
400
x_src + (x - glyph->info.x), (y - glyph->info.y),
402
x - glyph->info.x, y - glyph->info.y,
403
glyph_draw->width, glyph_draw->height);
405
struct glamor_glyph_private *glyph_priv = glamor_get_glyph_private((PixmapPtr)(glyph_draw));
406
struct glamor_glyph_atlas *next_atlas = glamor_atlas_for_glyph(glamor_priv, glyph_draw);
408
/* Switching source glyph format?
410
if (_X_UNLIKELY(next_atlas != glyph_atlas)) {
412
glamor_glyphs_flush(op, src, dst, prog, glyph_atlas, glyphs_queued);
415
glyph_atlas = next_atlas;
418
/* Glyph not cached in current atlas?
420
if (_X_UNLIKELY(glyph_priv->serial != glyph_atlas->serial)) {
421
if (!glamor_glyph_can_add(glyph_atlas, glyph_atlas_dim, glyph_draw)) {
423
glamor_glyphs_flush(op, src, dst, prog, glyph_atlas, glyphs_queued);
426
if (glyph_atlas->atlas) {
427
(*screen->DestroyPixmap)(glyph_atlas->atlas);
428
glyph_atlas->atlas = NULL;
431
if (!glyph_atlas->atlas) {
432
glamor_glyph_atlas_init(screen, glyph_atlas);
433
if (!glyph_atlas->atlas)
436
glamor_glyph_add(glyph_atlas, glyph_draw);
439
/* First glyph in the current atlas?
441
if (_X_UNLIKELY(glyphs_queued == 0)) {
442
if (glamor_glyph_use_130(glamor_priv))
443
prog = glamor_setup_program_render(op, src, glyph_pict, dst,
445
&glamor_facet_composite_glyphs_130,
446
glamor_priv->glyph_defines);
448
prog = glamor_setup_program_render(op, src, glyph_pict, dst,
450
&glamor_facet_composite_glyphs_120,
451
glamor_priv->glyph_defines);
454
v = glamor_glyph_start(screen, nglyph);
461
if (_X_LIKELY(glamor_glyph_use_130(glamor_priv))) {
462
v[0] = x - glyph->info.x;
463
v[1] = y - glyph->info.y;
464
v[2] = glyph_draw->width;
465
v[3] = glyph_draw->height;
466
v[4] = glyph_priv->x;
467
v[5] = glyph_priv->y;
470
v[0] = x - glyph->info.x;
471
v[1] = y - glyph->info.y;
472
v[2] = glyph_priv->x;
473
v[3] = glyph_priv->y;
476
v[0] = x - glyph->info.x + glyph_draw->width;
477
v[1] = y - glyph->info.y;
478
v[2] = glyph_priv->x + glyph_draw->width;
479
v[3] = glyph_priv->y;
482
v[0] = x - glyph->info.x + glyph_draw->width;
483
v[1] = y - glyph->info.y + glyph_draw->height;
484
v[2] = glyph_priv->x + glyph_draw->width;
485
v[3] = glyph_priv->y + glyph_draw->height;
488
v[0] = x - glyph->info.x;
489
v[1] = y - glyph->info.y + glyph_draw->height;
490
v[2] = glyph_priv->x;
491
v[3] = glyph_priv->y + glyph_draw->height;
496
x += glyph->info.xOff;
497
y += glyph->info.yOff;
503
glamor_glyphs_flush(op, src, dst, prog, glyph_atlas, glyphs_queued);
508
static struct glamor_glyph_atlas *
509
glamor_alloc_glyph_atlas(ScreenPtr screen, int depth, CARD32 f)
511
PictFormatPtr format;
512
struct glamor_glyph_atlas *glyph_atlas;
514
format = PictureMatchFormat(screen, depth, f);
517
glyph_atlas = calloc (1, sizeof (struct glamor_glyph_atlas));
520
glyph_atlas->format = format;
521
glyph_atlas->serial = 1;
527
glamor_composite_glyphs_init(ScreenPtr screen)
529
glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
531
if (!dixRegisterPrivateKey(&glamor_glyph_private_key, PRIVATE_PIXMAP, sizeof (struct glamor_glyph_private)))
534
/* Make glyph atlases of a reasonable size, but no larger than the maximum
535
* supported by the hardware
537
glamor_priv->glyph_atlas_dim = MIN(DEFAULT_ATLAS_DIM, glamor_priv->max_fbo_size);
539
/* Don't stick huge glyphs in the atlases */
540
glamor_priv->glyph_max_dim = glamor_priv->glyph_atlas_dim / 8;
542
glamor_priv->glyph_atlas_a = glamor_alloc_glyph_atlas(screen, 8, PICT_a8);
543
if (!glamor_priv->glyph_atlas_a)
545
glamor_priv->glyph_atlas_argb = glamor_alloc_glyph_atlas(screen, 32, PICT_a8r8g8b8);
546
if (!glamor_priv->glyph_atlas_argb) {
547
free (glamor_priv->glyph_atlas_a);
550
if (!glamor_glyphs_init_facet(screen))
556
glamor_free_glyph_atlas(struct glamor_glyph_atlas *atlas)
561
FreePicture(atlas->atlas, 0);
566
glamor_composite_glyphs_fini(ScreenPtr screen)
568
glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
570
glamor_glyphs_fini_facet(screen);
571
glamor_free_glyph_atlas(glamor_priv->glyph_atlas_a);
572
glamor_free_glyph_atlas(glamor_priv->glyph_atlas_argb);