xref: /OK3568_Linux_fs/external/xserver/exa/exa_glyphs.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright © 2008 Red Hat, Inc.
3*4882a593Smuzhiyun  * Partly based on code Copyright © 2000 SuSE, Inc.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Permission to use, copy, modify, distribute, and sell this software and its
6*4882a593Smuzhiyun  * documentation for any purpose is hereby granted without fee, provided that
7*4882a593Smuzhiyun  * the above copyright notice appear in all copies and that both that
8*4882a593Smuzhiyun  * copyright notice and this permission notice appear in supporting
9*4882a593Smuzhiyun  * documentation, and that the name of Red Hat not be used in advertising or
10*4882a593Smuzhiyun  * publicity pertaining to distribution of the software without specific,
11*4882a593Smuzhiyun  * written prior permission.  Red Hat makes no representations about the
12*4882a593Smuzhiyun  * suitability of this software for any purpose.  It is provided "as is"
13*4882a593Smuzhiyun  * without express or implied warranty.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * Red Hat DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
16*4882a593Smuzhiyun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL Red Hat
17*4882a593Smuzhiyun  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18*4882a593Smuzhiyun  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
19*4882a593Smuzhiyun  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20*4882a593Smuzhiyun  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * Permission to use, copy, modify, distribute, and sell this software and its
23*4882a593Smuzhiyun  * documentation for any purpose is hereby granted without fee, provided that
24*4882a593Smuzhiyun  * the above copyright notice appear in all copies and that both that
25*4882a593Smuzhiyun  * copyright notice and this permission notice appear in supporting
26*4882a593Smuzhiyun  * documentation, and that the name of SuSE not be used in advertising or
27*4882a593Smuzhiyun  * publicity pertaining to distribution of the software without specific,
28*4882a593Smuzhiyun  * written prior permission.  SuSE makes no representations about the
29*4882a593Smuzhiyun  * suitability of this software for any purpose.  It is provided "as is"
30*4882a593Smuzhiyun  * without express or implied warranty.
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
33*4882a593Smuzhiyun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE
34*4882a593Smuzhiyun  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35*4882a593Smuzhiyun  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
36*4882a593Smuzhiyun  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
37*4882a593Smuzhiyun  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38*4882a593Smuzhiyun  *
39*4882a593Smuzhiyun  * Author: Owen Taylor <otaylor@fishsoup.net>
40*4882a593Smuzhiyun  * Based on code by: Keith Packard
41*4882a593Smuzhiyun  */
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #ifdef HAVE_DIX_CONFIG_H
44*4882a593Smuzhiyun #include <dix-config.h>
45*4882a593Smuzhiyun #endif
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun #include <stdlib.h>
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun #include "exa_priv.h"
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #include "mipict.h"
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun #if DEBUG_GLYPH_CACHE
54*4882a593Smuzhiyun #define DBG_GLYPH_CACHE(a) ErrorF a
55*4882a593Smuzhiyun #else
56*4882a593Smuzhiyun #define DBG_GLYPH_CACHE(a)
57*4882a593Smuzhiyun #endif
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun /* Width of the pixmaps we use for the caches; this should be less than
60*4882a593Smuzhiyun  * max texture size of the driver; this may need to actually come from
61*4882a593Smuzhiyun  * the driver.
62*4882a593Smuzhiyun  */
63*4882a593Smuzhiyun #define CACHE_PICTURE_WIDTH 1024
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun /* Maximum number of glyphs we buffer on the stack before flushing
66*4882a593Smuzhiyun  * rendering to the mask or destination surface.
67*4882a593Smuzhiyun  */
68*4882a593Smuzhiyun #define GLYPH_BUFFER_SIZE 256
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun typedef struct {
71*4882a593Smuzhiyun     PicturePtr mask;
72*4882a593Smuzhiyun     ExaCompositeRectRec rects[GLYPH_BUFFER_SIZE];
73*4882a593Smuzhiyun     int count;
74*4882a593Smuzhiyun } ExaGlyphBuffer, *ExaGlyphBufferPtr;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun typedef enum {
77*4882a593Smuzhiyun     ExaGlyphSuccess,            /* Glyph added to render buffer */
78*4882a593Smuzhiyun     ExaGlyphFail,               /* out of memory, etc */
79*4882a593Smuzhiyun     ExaGlyphNeedFlush,          /* would evict a glyph already in the buffer */
80*4882a593Smuzhiyun } ExaGlyphCacheResult;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun void
exaGlyphsInit(ScreenPtr pScreen)83*4882a593Smuzhiyun exaGlyphsInit(ScreenPtr pScreen)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun     ExaScreenPriv(pScreen);
86*4882a593Smuzhiyun     int i = 0;
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun     memset(pExaScr->glyphCaches, 0, sizeof(pExaScr->glyphCaches));
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun     pExaScr->glyphCaches[i].format = PICT_a8;
91*4882a593Smuzhiyun     pExaScr->glyphCaches[i].glyphWidth = pExaScr->glyphCaches[i].glyphHeight =
92*4882a593Smuzhiyun         16;
93*4882a593Smuzhiyun     i++;
94*4882a593Smuzhiyun     pExaScr->glyphCaches[i].format = PICT_a8;
95*4882a593Smuzhiyun     pExaScr->glyphCaches[i].glyphWidth = pExaScr->glyphCaches[i].glyphHeight =
96*4882a593Smuzhiyun         32;
97*4882a593Smuzhiyun     i++;
98*4882a593Smuzhiyun     pExaScr->glyphCaches[i].format = PICT_a8r8g8b8;
99*4882a593Smuzhiyun     pExaScr->glyphCaches[i].glyphWidth = pExaScr->glyphCaches[i].glyphHeight =
100*4882a593Smuzhiyun         16;
101*4882a593Smuzhiyun     i++;
102*4882a593Smuzhiyun     pExaScr->glyphCaches[i].format = PICT_a8r8g8b8;
103*4882a593Smuzhiyun     pExaScr->glyphCaches[i].glyphWidth = pExaScr->glyphCaches[i].glyphHeight =
104*4882a593Smuzhiyun         32;
105*4882a593Smuzhiyun     i++;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun     assert(i == EXA_NUM_GLYPH_CACHES);
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun     for (i = 0; i < EXA_NUM_GLYPH_CACHES; i++) {
110*4882a593Smuzhiyun         pExaScr->glyphCaches[i].columns =
111*4882a593Smuzhiyun             CACHE_PICTURE_WIDTH / pExaScr->glyphCaches[i].glyphWidth;
112*4882a593Smuzhiyun         pExaScr->glyphCaches[i].size = 256;
113*4882a593Smuzhiyun         pExaScr->glyphCaches[i].hashSize = 557;
114*4882a593Smuzhiyun     }
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun static void
exaUnrealizeGlyphCaches(ScreenPtr pScreen,unsigned int format)118*4882a593Smuzhiyun exaUnrealizeGlyphCaches(ScreenPtr pScreen, unsigned int format)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun     ExaScreenPriv(pScreen);
121*4882a593Smuzhiyun     int i;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun     for (i = 0; i < EXA_NUM_GLYPH_CACHES; i++) {
124*4882a593Smuzhiyun         ExaGlyphCachePtr cache = &pExaScr->glyphCaches[i];
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun         if (cache->format != format)
127*4882a593Smuzhiyun             continue;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun         if (cache->picture) {
130*4882a593Smuzhiyun             FreePicture((void *) cache->picture, (XID) 0);
131*4882a593Smuzhiyun             cache->picture = NULL;
132*4882a593Smuzhiyun         }
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun         free(cache->hashEntries);
135*4882a593Smuzhiyun         cache->hashEntries = NULL;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun         free(cache->glyphs);
138*4882a593Smuzhiyun         cache->glyphs = NULL;
139*4882a593Smuzhiyun         cache->glyphCount = 0;
140*4882a593Smuzhiyun     }
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun #define NeedsComponent(f) (PICT_FORMAT_A(f) != 0 && PICT_FORMAT_RGB(f) != 0)
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun /* All caches for a single format share a single pixmap for glyph storage,
146*4882a593Smuzhiyun  * allowing mixing glyphs of different sizes without paying a penalty
147*4882a593Smuzhiyun  * for switching between mask pixmaps. (Note that for a size of font
148*4882a593Smuzhiyun  * right at the border between two sizes, we might be switching for almost
149*4882a593Smuzhiyun  * every glyph.)
150*4882a593Smuzhiyun  *
151*4882a593Smuzhiyun  * This function allocates the storage pixmap, and then fills in the
152*4882a593Smuzhiyun  * rest of the allocated structures for all caches with the given format.
153*4882a593Smuzhiyun  */
154*4882a593Smuzhiyun static Bool
exaRealizeGlyphCaches(ScreenPtr pScreen,unsigned int format)155*4882a593Smuzhiyun exaRealizeGlyphCaches(ScreenPtr pScreen, unsigned int format)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun     ExaScreenPriv(pScreen);
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun     int depth = PIXMAN_FORMAT_DEPTH(format);
160*4882a593Smuzhiyun     PictFormatPtr pPictFormat;
161*4882a593Smuzhiyun     PixmapPtr pPixmap;
162*4882a593Smuzhiyun     PicturePtr pPicture;
163*4882a593Smuzhiyun     CARD32 component_alpha;
164*4882a593Smuzhiyun     int height;
165*4882a593Smuzhiyun     int i;
166*4882a593Smuzhiyun     int error;
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun     pPictFormat = PictureMatchFormat(pScreen, depth, format);
169*4882a593Smuzhiyun     if (!pPictFormat)
170*4882a593Smuzhiyun         return FALSE;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun     /* Compute the total vertical size needed for the format */
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun     height = 0;
175*4882a593Smuzhiyun     for (i = 0; i < EXA_NUM_GLYPH_CACHES; i++) {
176*4882a593Smuzhiyun         ExaGlyphCachePtr cache = &pExaScr->glyphCaches[i];
177*4882a593Smuzhiyun         int rows;
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun         if (cache->format != format)
180*4882a593Smuzhiyun             continue;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun         cache->yOffset = height;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun         rows = (cache->size + cache->columns - 1) / cache->columns;
185*4882a593Smuzhiyun         height += rows * cache->glyphHeight;
186*4882a593Smuzhiyun     }
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun     /* Now allocate the pixmap and picture */
189*4882a593Smuzhiyun     pPixmap = (*pScreen->CreatePixmap) (pScreen,
190*4882a593Smuzhiyun                                         CACHE_PICTURE_WIDTH, height, depth, 0);
191*4882a593Smuzhiyun     if (!pPixmap)
192*4882a593Smuzhiyun         return FALSE;
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun     component_alpha = NeedsComponent(pPictFormat->format);
195*4882a593Smuzhiyun     pPicture = CreatePicture(0, &pPixmap->drawable, pPictFormat,
196*4882a593Smuzhiyun                              CPComponentAlpha, &component_alpha, serverClient,
197*4882a593Smuzhiyun                              &error);
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun     (*pScreen->DestroyPixmap) (pPixmap);        /* picture holds a refcount */
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun     if (!pPicture)
202*4882a593Smuzhiyun         return FALSE;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun     /* And store the picture in all the caches for the format */
205*4882a593Smuzhiyun     for (i = 0; i < EXA_NUM_GLYPH_CACHES; i++) {
206*4882a593Smuzhiyun         ExaGlyphCachePtr cache = &pExaScr->glyphCaches[i];
207*4882a593Smuzhiyun         int j;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun         if (cache->format != format)
210*4882a593Smuzhiyun             continue;
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun         cache->picture = pPicture;
213*4882a593Smuzhiyun         cache->picture->refcnt++;
214*4882a593Smuzhiyun         cache->hashEntries = xallocarray(cache->hashSize, sizeof(int));
215*4882a593Smuzhiyun         cache->glyphs = xallocarray(cache->size, sizeof(ExaCachedGlyphRec));
216*4882a593Smuzhiyun         cache->glyphCount = 0;
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun         if (!cache->hashEntries || !cache->glyphs)
219*4882a593Smuzhiyun             goto bail;
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun         for (j = 0; j < cache->hashSize; j++)
222*4882a593Smuzhiyun             cache->hashEntries[j] = -1;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun         cache->evictionPosition = rand() % cache->size;
225*4882a593Smuzhiyun     }
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun     /* Each cache references the picture individually */
228*4882a593Smuzhiyun     FreePicture((void *) pPicture, (XID) 0);
229*4882a593Smuzhiyun     return TRUE;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun  bail:
232*4882a593Smuzhiyun     exaUnrealizeGlyphCaches(pScreen, format);
233*4882a593Smuzhiyun     return FALSE;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun void
exaGlyphsFini(ScreenPtr pScreen)237*4882a593Smuzhiyun exaGlyphsFini(ScreenPtr pScreen)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun     ExaScreenPriv(pScreen);
240*4882a593Smuzhiyun     int i;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun     for (i = 0; i < EXA_NUM_GLYPH_CACHES; i++) {
243*4882a593Smuzhiyun         ExaGlyphCachePtr cache = &pExaScr->glyphCaches[i];
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun         if (cache->picture)
246*4882a593Smuzhiyun             exaUnrealizeGlyphCaches(pScreen, cache->format);
247*4882a593Smuzhiyun     }
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun static int
exaGlyphCacheHashLookup(ExaGlyphCachePtr cache,GlyphPtr pGlyph)251*4882a593Smuzhiyun exaGlyphCacheHashLookup(ExaGlyphCachePtr cache, GlyphPtr pGlyph)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun     int slot;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun     slot = (*(CARD32 *) pGlyph->sha1) % cache->hashSize;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun     while (TRUE) {              /* hash table can never be full */
258*4882a593Smuzhiyun         int entryPos = cache->hashEntries[slot];
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun         if (entryPos == -1)
261*4882a593Smuzhiyun             return -1;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun         if (memcmp
264*4882a593Smuzhiyun             (pGlyph->sha1, cache->glyphs[entryPos].sha1,
265*4882a593Smuzhiyun              sizeof(pGlyph->sha1)) == 0) {
266*4882a593Smuzhiyun             return entryPos;
267*4882a593Smuzhiyun         }
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun         slot--;
270*4882a593Smuzhiyun         if (slot < 0)
271*4882a593Smuzhiyun             slot = cache->hashSize - 1;
272*4882a593Smuzhiyun     }
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun static void
exaGlyphCacheHashInsert(ExaGlyphCachePtr cache,GlyphPtr pGlyph,int pos)276*4882a593Smuzhiyun exaGlyphCacheHashInsert(ExaGlyphCachePtr cache, GlyphPtr pGlyph, int pos)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun     int slot;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun     memcpy(cache->glyphs[pos].sha1, pGlyph->sha1, sizeof(pGlyph->sha1));
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun     slot = (*(CARD32 *) pGlyph->sha1) % cache->hashSize;
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun     while (TRUE) {              /* hash table can never be full */
285*4882a593Smuzhiyun         if (cache->hashEntries[slot] == -1) {
286*4882a593Smuzhiyun             cache->hashEntries[slot] = pos;
287*4882a593Smuzhiyun             return;
288*4882a593Smuzhiyun         }
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun         slot--;
291*4882a593Smuzhiyun         if (slot < 0)
292*4882a593Smuzhiyun             slot = cache->hashSize - 1;
293*4882a593Smuzhiyun     }
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun static void
exaGlyphCacheHashRemove(ExaGlyphCachePtr cache,int pos)297*4882a593Smuzhiyun exaGlyphCacheHashRemove(ExaGlyphCachePtr cache, int pos)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun     int slot;
300*4882a593Smuzhiyun     int emptiedSlot = -1;
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun     slot = (*(CARD32 *) cache->glyphs[pos].sha1) % cache->hashSize;
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun     while (TRUE) {              /* hash table can never be full */
305*4882a593Smuzhiyun         int entryPos = cache->hashEntries[slot];
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun         if (entryPos == -1)
308*4882a593Smuzhiyun             return;
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun         if (entryPos == pos) {
311*4882a593Smuzhiyun             cache->hashEntries[slot] = -1;
312*4882a593Smuzhiyun             emptiedSlot = slot;
313*4882a593Smuzhiyun         }
314*4882a593Smuzhiyun         else if (emptiedSlot != -1) {
315*4882a593Smuzhiyun             /* See if we can move this entry into the emptied slot, we can't
316*4882a593Smuzhiyun              * do that if if entry would have hashed between the current position
317*4882a593Smuzhiyun              * and the emptied slot. (taking wrapping into account). Bad positions
318*4882a593Smuzhiyun              * are:
319*4882a593Smuzhiyun              *
320*4882a593Smuzhiyun              * |   XXXXXXXXXX             |
321*4882a593Smuzhiyun              *     i         j
322*4882a593Smuzhiyun              *
323*4882a593Smuzhiyun              * |XXX                   XXXX|
324*4882a593Smuzhiyun              *     j                  i
325*4882a593Smuzhiyun              *
326*4882a593Smuzhiyun              * i - slot, j - emptiedSlot
327*4882a593Smuzhiyun              *
328*4882a593Smuzhiyun              * (Knuth 6.4R)
329*4882a593Smuzhiyun              */
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun             int entrySlot =
332*4882a593Smuzhiyun                 (*(CARD32 *) cache->glyphs[entryPos].sha1) % cache->hashSize;
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun             if (!((entrySlot >= slot && entrySlot < emptiedSlot) ||
335*4882a593Smuzhiyun                   (emptiedSlot < slot &&
336*4882a593Smuzhiyun                    (entrySlot < emptiedSlot || entrySlot >= slot)))) {
337*4882a593Smuzhiyun                 cache->hashEntries[emptiedSlot] = entryPos;
338*4882a593Smuzhiyun                 cache->hashEntries[slot] = -1;
339*4882a593Smuzhiyun                 emptiedSlot = slot;
340*4882a593Smuzhiyun             }
341*4882a593Smuzhiyun         }
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun         slot--;
344*4882a593Smuzhiyun         if (slot < 0)
345*4882a593Smuzhiyun             slot = cache->hashSize - 1;
346*4882a593Smuzhiyun     }
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun #define CACHE_X(pos) (((pos) % cache->columns) * cache->glyphWidth)
350*4882a593Smuzhiyun #define CACHE_Y(pos) (cache->yOffset + ((pos) / cache->columns) * cache->glyphHeight)
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun /* The most efficient thing to way to upload the glyph to the screen
353*4882a593Smuzhiyun  * is to use the UploadToScreen() driver hook; this allows us to
354*4882a593Smuzhiyun  * pipeline glyph uploads and to avoid creating gpu backed pixmaps for
355*4882a593Smuzhiyun  * glyphs that we'll never use again.
356*4882a593Smuzhiyun  *
357*4882a593Smuzhiyun  * If we can't do it with UploadToScreen (because the glyph has a gpu copy,
358*4882a593Smuzhiyun  * etc), we fall back to CompositePicture.
359*4882a593Smuzhiyun  *
360*4882a593Smuzhiyun  * We need to damage the cache pixmap manually in either case because the damage
361*4882a593Smuzhiyun  * layer unwrapped the picture screen before calling exaGlyphs.
362*4882a593Smuzhiyun  */
363*4882a593Smuzhiyun static void
exaGlyphCacheUploadGlyph(ScreenPtr pScreen,ExaGlyphCachePtr cache,int x,int y,GlyphPtr pGlyph)364*4882a593Smuzhiyun exaGlyphCacheUploadGlyph(ScreenPtr pScreen,
365*4882a593Smuzhiyun                          ExaGlyphCachePtr cache, int x, int y, GlyphPtr pGlyph)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun     ExaScreenPriv(pScreen);
368*4882a593Smuzhiyun     PicturePtr pGlyphPicture = GetGlyphPicture(pGlyph, pScreen);
369*4882a593Smuzhiyun     PixmapPtr pGlyphPixmap = (PixmapPtr) pGlyphPicture->pDrawable;
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun     ExaPixmapPriv(pGlyphPixmap);
372*4882a593Smuzhiyun     PixmapPtr pCachePixmap = (PixmapPtr) cache->picture->pDrawable;
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun     if (!pExaScr->info->UploadToScreen || pExaScr->swappedOut ||
375*4882a593Smuzhiyun         pExaPixmap->accel_blocked)
376*4882a593Smuzhiyun         goto composite;
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun     /* If the glyph pixmap is already uploaded, no point in doing
379*4882a593Smuzhiyun      * things this way */
380*4882a593Smuzhiyun     if (exaPixmapHasGpuCopy(pGlyphPixmap))
381*4882a593Smuzhiyun         goto composite;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun     /* UploadToScreen only works if bpp match */
384*4882a593Smuzhiyun     if (pGlyphPixmap->drawable.bitsPerPixel !=
385*4882a593Smuzhiyun         pCachePixmap->drawable.bitsPerPixel)
386*4882a593Smuzhiyun         goto composite;
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun     if (pExaScr->do_migration) {
389*4882a593Smuzhiyun         ExaMigrationRec pixmaps[1];
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun         /* cache pixmap must have a gpu copy. */
392*4882a593Smuzhiyun         pixmaps[0].as_dst = TRUE;
393*4882a593Smuzhiyun         pixmaps[0].as_src = FALSE;
394*4882a593Smuzhiyun         pixmaps[0].pPix = pCachePixmap;
395*4882a593Smuzhiyun         pixmaps[0].pReg = NULL;
396*4882a593Smuzhiyun         exaDoMigration(pixmaps, 1, TRUE);
397*4882a593Smuzhiyun     }
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun     if (!exaPixmapHasGpuCopy(pCachePixmap))
400*4882a593Smuzhiyun         goto composite;
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun     /* x,y are in pixmap coordinates, no need for cache{X,Y}off */
403*4882a593Smuzhiyun     if (pExaScr->info->UploadToScreen(pCachePixmap,
404*4882a593Smuzhiyun                                       x,
405*4882a593Smuzhiyun                                       y,
406*4882a593Smuzhiyun                                       pGlyph->info.width,
407*4882a593Smuzhiyun                                       pGlyph->info.height,
408*4882a593Smuzhiyun                                       (char *) pExaPixmap->sys_ptr,
409*4882a593Smuzhiyun                                       pExaPixmap->sys_pitch))
410*4882a593Smuzhiyun         goto damage;
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun  composite:
413*4882a593Smuzhiyun     CompositePicture(PictOpSrc,
414*4882a593Smuzhiyun                      pGlyphPicture,
415*4882a593Smuzhiyun                      None,
416*4882a593Smuzhiyun                      cache->picture,
417*4882a593Smuzhiyun                      0, 0, 0, 0, x, y, pGlyph->info.width, pGlyph->info.height);
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun  damage:
420*4882a593Smuzhiyun     /* The cache pixmap isn't a window, so no need to offset coordinates. */
421*4882a593Smuzhiyun     exaPixmapDirty(pCachePixmap,
422*4882a593Smuzhiyun                    x, y, x + cache->glyphWidth, y + cache->glyphHeight);
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun static ExaGlyphCacheResult
exaGlyphCacheBufferGlyph(ScreenPtr pScreen,ExaGlyphCachePtr cache,ExaGlyphBufferPtr buffer,GlyphPtr pGlyph,PicturePtr pSrc,PicturePtr pDst,INT16 xSrc,INT16 ySrc,INT16 xMask,INT16 yMask,INT16 xDst,INT16 yDst)426*4882a593Smuzhiyun exaGlyphCacheBufferGlyph(ScreenPtr pScreen,
427*4882a593Smuzhiyun                          ExaGlyphCachePtr cache,
428*4882a593Smuzhiyun                          ExaGlyphBufferPtr buffer,
429*4882a593Smuzhiyun                          GlyphPtr pGlyph,
430*4882a593Smuzhiyun                          PicturePtr pSrc,
431*4882a593Smuzhiyun                          PicturePtr pDst,
432*4882a593Smuzhiyun                          INT16 xSrc,
433*4882a593Smuzhiyun                          INT16 ySrc,
434*4882a593Smuzhiyun                          INT16 xMask, INT16 yMask, INT16 xDst, INT16 yDst)
435*4882a593Smuzhiyun {
436*4882a593Smuzhiyun     ExaCompositeRectPtr rect;
437*4882a593Smuzhiyun     int pos;
438*4882a593Smuzhiyun     int x, y;
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun     if (buffer->mask && buffer->mask != cache->picture)
441*4882a593Smuzhiyun         return ExaGlyphNeedFlush;
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun     if (!cache->picture) {
444*4882a593Smuzhiyun         if (!exaRealizeGlyphCaches(pScreen, cache->format))
445*4882a593Smuzhiyun             return ExaGlyphFail;
446*4882a593Smuzhiyun     }
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun     DBG_GLYPH_CACHE(("(%d,%d,%s): buffering glyph %lx\n",
449*4882a593Smuzhiyun                      cache->glyphWidth, cache->glyphHeight,
450*4882a593Smuzhiyun                      cache->format == PICT_a8 ? "A" : "ARGB",
451*4882a593Smuzhiyun                      (long) *(CARD32 *) pGlyph->sha1));
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun     pos = exaGlyphCacheHashLookup(cache, pGlyph);
454*4882a593Smuzhiyun     if (pos != -1) {
455*4882a593Smuzhiyun         DBG_GLYPH_CACHE(("  found existing glyph at %d\n", pos));
456*4882a593Smuzhiyun         x = CACHE_X(pos);
457*4882a593Smuzhiyun         y = CACHE_Y(pos);
458*4882a593Smuzhiyun     }
459*4882a593Smuzhiyun     else {
460*4882a593Smuzhiyun         if (cache->glyphCount < cache->size) {
461*4882a593Smuzhiyun             /* Space remaining; we fill from the start */
462*4882a593Smuzhiyun             pos = cache->glyphCount;
463*4882a593Smuzhiyun             x = CACHE_X(pos);
464*4882a593Smuzhiyun             y = CACHE_Y(pos);
465*4882a593Smuzhiyun             cache->glyphCount++;
466*4882a593Smuzhiyun             DBG_GLYPH_CACHE(("  storing glyph in free space at %d\n", pos));
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun             exaGlyphCacheHashInsert(cache, pGlyph, pos);
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun         }
471*4882a593Smuzhiyun         else {
472*4882a593Smuzhiyun             /* Need to evict an entry. We have to see if any glyphs
473*4882a593Smuzhiyun              * already in the output buffer were at this position in
474*4882a593Smuzhiyun              * the cache
475*4882a593Smuzhiyun              */
476*4882a593Smuzhiyun             pos = cache->evictionPosition;
477*4882a593Smuzhiyun             x = CACHE_X(pos);
478*4882a593Smuzhiyun             y = CACHE_Y(pos);
479*4882a593Smuzhiyun             DBG_GLYPH_CACHE(("  evicting glyph at %d\n", pos));
480*4882a593Smuzhiyun             if (buffer->count) {
481*4882a593Smuzhiyun                 int i;
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun                 for (i = 0; i < buffer->count; i++) {
484*4882a593Smuzhiyun                     if (pSrc ?
485*4882a593Smuzhiyun                         (buffer->rects[i].xMask == x &&
486*4882a593Smuzhiyun                          buffer->rects[i].yMask ==
487*4882a593Smuzhiyun                          y) : (buffer->rects[i].xSrc == x &&
488*4882a593Smuzhiyun                                buffer->rects[i].ySrc == y)) {
489*4882a593Smuzhiyun                         DBG_GLYPH_CACHE(("  must flush buffer\n"));
490*4882a593Smuzhiyun                         return ExaGlyphNeedFlush;
491*4882a593Smuzhiyun                     }
492*4882a593Smuzhiyun                 }
493*4882a593Smuzhiyun             }
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun             /* OK, we're all set, swap in the new glyph */
496*4882a593Smuzhiyun             exaGlyphCacheHashRemove(cache, pos);
497*4882a593Smuzhiyun             exaGlyphCacheHashInsert(cache, pGlyph, pos);
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun             /* And pick a new eviction position */
500*4882a593Smuzhiyun             cache->evictionPosition = rand() % cache->size;
501*4882a593Smuzhiyun         }
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun         exaGlyphCacheUploadGlyph(pScreen, cache, x, y, pGlyph);
504*4882a593Smuzhiyun     }
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun     buffer->mask = cache->picture;
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun     rect = &buffer->rects[buffer->count];
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun     if (pSrc) {
511*4882a593Smuzhiyun         rect->xSrc = xSrc;
512*4882a593Smuzhiyun         rect->ySrc = ySrc;
513*4882a593Smuzhiyun         rect->xMask = x;
514*4882a593Smuzhiyun         rect->yMask = y;
515*4882a593Smuzhiyun     }
516*4882a593Smuzhiyun     else {
517*4882a593Smuzhiyun         rect->xSrc = x;
518*4882a593Smuzhiyun         rect->ySrc = y;
519*4882a593Smuzhiyun         rect->xMask = 0;
520*4882a593Smuzhiyun         rect->yMask = 0;
521*4882a593Smuzhiyun     }
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun     rect->pDst = pDst;
524*4882a593Smuzhiyun     rect->xDst = xDst;
525*4882a593Smuzhiyun     rect->yDst = yDst;
526*4882a593Smuzhiyun     rect->width = pGlyph->info.width;
527*4882a593Smuzhiyun     rect->height = pGlyph->info.height;
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun     buffer->count++;
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun     return ExaGlyphSuccess;
532*4882a593Smuzhiyun }
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun #undef CACHE_X
535*4882a593Smuzhiyun #undef CACHE_Y
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun static ExaGlyphCacheResult
exaBufferGlyph(ScreenPtr pScreen,ExaGlyphBufferPtr buffer,GlyphPtr pGlyph,PicturePtr pSrc,PicturePtr pDst,INT16 xSrc,INT16 ySrc,INT16 xMask,INT16 yMask,INT16 xDst,INT16 yDst)538*4882a593Smuzhiyun exaBufferGlyph(ScreenPtr pScreen,
539*4882a593Smuzhiyun                ExaGlyphBufferPtr buffer,
540*4882a593Smuzhiyun                GlyphPtr pGlyph,
541*4882a593Smuzhiyun                PicturePtr pSrc,
542*4882a593Smuzhiyun                PicturePtr pDst,
543*4882a593Smuzhiyun                INT16 xSrc,
544*4882a593Smuzhiyun                INT16 ySrc, INT16 xMask, INT16 yMask, INT16 xDst, INT16 yDst)
545*4882a593Smuzhiyun {
546*4882a593Smuzhiyun     ExaScreenPriv(pScreen);
547*4882a593Smuzhiyun     unsigned int format = (GetGlyphPicture(pGlyph, pScreen))->format;
548*4882a593Smuzhiyun     int width = pGlyph->info.width;
549*4882a593Smuzhiyun     int height = pGlyph->info.height;
550*4882a593Smuzhiyun     ExaCompositeRectPtr rect;
551*4882a593Smuzhiyun     PicturePtr mask;
552*4882a593Smuzhiyun     int i;
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun     if (buffer->count == GLYPH_BUFFER_SIZE)
555*4882a593Smuzhiyun         return ExaGlyphNeedFlush;
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun     if (PICT_FORMAT_BPP(format) == 1)
558*4882a593Smuzhiyun         format = PICT_a8;
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun     for (i = 0; i < EXA_NUM_GLYPH_CACHES; i++) {
561*4882a593Smuzhiyun         ExaGlyphCachePtr cache = &pExaScr->glyphCaches[i];
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun         if (format == cache->format &&
564*4882a593Smuzhiyun             width <= cache->glyphWidth && height <= cache->glyphHeight) {
565*4882a593Smuzhiyun             ExaGlyphCacheResult result = exaGlyphCacheBufferGlyph(pScreen,
566*4882a593Smuzhiyun                                                                   &pExaScr->
567*4882a593Smuzhiyun                                                                   glyphCaches
568*4882a593Smuzhiyun                                                                   [i],
569*4882a593Smuzhiyun                                                                   buffer,
570*4882a593Smuzhiyun                                                                   pGlyph,
571*4882a593Smuzhiyun                                                                   pSrc,
572*4882a593Smuzhiyun                                                                   pDst,
573*4882a593Smuzhiyun                                                                   xSrc, ySrc,
574*4882a593Smuzhiyun                                                                   xMask, yMask,
575*4882a593Smuzhiyun                                                                   xDst, yDst);
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun             switch (result) {
578*4882a593Smuzhiyun             case ExaGlyphFail:
579*4882a593Smuzhiyun                 break;
580*4882a593Smuzhiyun             case ExaGlyphSuccess:
581*4882a593Smuzhiyun             case ExaGlyphNeedFlush:
582*4882a593Smuzhiyun                 return result;
583*4882a593Smuzhiyun             }
584*4882a593Smuzhiyun         }
585*4882a593Smuzhiyun     }
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun     /* Couldn't find the glyph in the cache, use the glyph picture directly */
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun     mask = GetGlyphPicture(pGlyph, pScreen);
590*4882a593Smuzhiyun     if (buffer->mask && buffer->mask != mask)
591*4882a593Smuzhiyun         return ExaGlyphNeedFlush;
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun     buffer->mask = mask;
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun     rect = &buffer->rects[buffer->count];
596*4882a593Smuzhiyun     rect->xSrc = xSrc;
597*4882a593Smuzhiyun     rect->ySrc = ySrc;
598*4882a593Smuzhiyun     rect->xMask = xMask;
599*4882a593Smuzhiyun     rect->yMask = yMask;
600*4882a593Smuzhiyun     rect->xDst = xDst;
601*4882a593Smuzhiyun     rect->yDst = yDst;
602*4882a593Smuzhiyun     rect->width = width;
603*4882a593Smuzhiyun     rect->height = height;
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun     buffer->count++;
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun     return ExaGlyphSuccess;
608*4882a593Smuzhiyun }
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun static void
exaGlyphsToMask(PicturePtr pMask,ExaGlyphBufferPtr buffer)611*4882a593Smuzhiyun exaGlyphsToMask(PicturePtr pMask, ExaGlyphBufferPtr buffer)
612*4882a593Smuzhiyun {
613*4882a593Smuzhiyun     exaCompositeRects(PictOpAdd, buffer->mask, NULL, pMask,
614*4882a593Smuzhiyun                       buffer->count, buffer->rects);
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun     buffer->count = 0;
617*4882a593Smuzhiyun     buffer->mask = NULL;
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun static void
exaGlyphsToDst(CARD8 op,PicturePtr pSrc,PicturePtr pDst,ExaGlyphBufferPtr buffer)621*4882a593Smuzhiyun exaGlyphsToDst(CARD8 op, PicturePtr pSrc, PicturePtr pDst, ExaGlyphBufferPtr buffer)
622*4882a593Smuzhiyun {
623*4882a593Smuzhiyun     exaCompositeRects(op, pSrc, buffer->mask, pDst, buffer->count,
624*4882a593Smuzhiyun                       buffer->rects);
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun     buffer->count = 0;
627*4882a593Smuzhiyun     buffer->mask = NULL;
628*4882a593Smuzhiyun }
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun /* Cut and paste from render/glyph.c - probably should export it instead */
631*4882a593Smuzhiyun static void
GlyphExtents(int nlist,GlyphListPtr list,GlyphPtr * glyphs,BoxPtr extents)632*4882a593Smuzhiyun GlyphExtents(int nlist, GlyphListPtr list, GlyphPtr * glyphs, BoxPtr extents)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun     int x1, x2, y1, y2;
635*4882a593Smuzhiyun     int n;
636*4882a593Smuzhiyun     GlyphPtr glyph;
637*4882a593Smuzhiyun     int x, y;
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun     x = 0;
640*4882a593Smuzhiyun     y = 0;
641*4882a593Smuzhiyun     extents->x1 = MAXSHORT;
642*4882a593Smuzhiyun     extents->x2 = MINSHORT;
643*4882a593Smuzhiyun     extents->y1 = MAXSHORT;
644*4882a593Smuzhiyun     extents->y2 = MINSHORT;
645*4882a593Smuzhiyun     while (nlist--) {
646*4882a593Smuzhiyun         x += list->xOff;
647*4882a593Smuzhiyun         y += list->yOff;
648*4882a593Smuzhiyun         n = list->len;
649*4882a593Smuzhiyun         list++;
650*4882a593Smuzhiyun         while (n--) {
651*4882a593Smuzhiyun             glyph = *glyphs++;
652*4882a593Smuzhiyun             x1 = x - glyph->info.x;
653*4882a593Smuzhiyun             if (x1 < MINSHORT)
654*4882a593Smuzhiyun                 x1 = MINSHORT;
655*4882a593Smuzhiyun             y1 = y - glyph->info.y;
656*4882a593Smuzhiyun             if (y1 < MINSHORT)
657*4882a593Smuzhiyun                 y1 = MINSHORT;
658*4882a593Smuzhiyun             x2 = x1 + glyph->info.width;
659*4882a593Smuzhiyun             if (x2 > MAXSHORT)
660*4882a593Smuzhiyun                 x2 = MAXSHORT;
661*4882a593Smuzhiyun             y2 = y1 + glyph->info.height;
662*4882a593Smuzhiyun             if (y2 > MAXSHORT)
663*4882a593Smuzhiyun                 y2 = MAXSHORT;
664*4882a593Smuzhiyun             if (x1 < extents->x1)
665*4882a593Smuzhiyun                 extents->x1 = x1;
666*4882a593Smuzhiyun             if (x2 > extents->x2)
667*4882a593Smuzhiyun                 extents->x2 = x2;
668*4882a593Smuzhiyun             if (y1 < extents->y1)
669*4882a593Smuzhiyun                 extents->y1 = y1;
670*4882a593Smuzhiyun             if (y2 > extents->y2)
671*4882a593Smuzhiyun                 extents->y2 = y2;
672*4882a593Smuzhiyun             x += glyph->info.xOff;
673*4882a593Smuzhiyun             y += glyph->info.yOff;
674*4882a593Smuzhiyun         }
675*4882a593Smuzhiyun     }
676*4882a593Smuzhiyun }
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun void
exaGlyphs(CARD8 op,PicturePtr pSrc,PicturePtr pDst,PictFormatPtr maskFormat,INT16 xSrc,INT16 ySrc,int nlist,GlyphListPtr list,GlyphPtr * glyphs)679*4882a593Smuzhiyun exaGlyphs(CARD8 op,
680*4882a593Smuzhiyun           PicturePtr pSrc,
681*4882a593Smuzhiyun           PicturePtr pDst,
682*4882a593Smuzhiyun           PictFormatPtr maskFormat,
683*4882a593Smuzhiyun           INT16 xSrc,
684*4882a593Smuzhiyun           INT16 ySrc, int nlist, GlyphListPtr list, GlyphPtr * glyphs)
685*4882a593Smuzhiyun {
686*4882a593Smuzhiyun     PixmapPtr pMaskPixmap = 0;
687*4882a593Smuzhiyun     PicturePtr pMask = NULL;
688*4882a593Smuzhiyun     ScreenPtr pScreen = pDst->pDrawable->pScreen;
689*4882a593Smuzhiyun     int width = 0, height = 0;
690*4882a593Smuzhiyun     int x, y;
691*4882a593Smuzhiyun     int first_xOff = list->xOff, first_yOff = list->yOff;
692*4882a593Smuzhiyun     int n;
693*4882a593Smuzhiyun     GlyphPtr glyph;
694*4882a593Smuzhiyun     int error;
695*4882a593Smuzhiyun     BoxRec extents = { 0, 0, 0, 0 };
696*4882a593Smuzhiyun     CARD32 component_alpha;
697*4882a593Smuzhiyun     ExaGlyphBuffer buffer;
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun     if (maskFormat) {
700*4882a593Smuzhiyun         ExaScreenPriv(pScreen);
701*4882a593Smuzhiyun         GCPtr pGC;
702*4882a593Smuzhiyun         xRectangle rect;
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun         GlyphExtents(nlist, list, glyphs, &extents);
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun         if (extents.x2 <= extents.x1 || extents.y2 <= extents.y1)
707*4882a593Smuzhiyun             return;
708*4882a593Smuzhiyun         width = extents.x2 - extents.x1;
709*4882a593Smuzhiyun         height = extents.y2 - extents.y1;
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun         if (maskFormat->depth == 1) {
712*4882a593Smuzhiyun             PictFormatPtr a8Format = PictureMatchFormat(pScreen, 8, PICT_a8);
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun             if (a8Format)
715*4882a593Smuzhiyun                 maskFormat = a8Format;
716*4882a593Smuzhiyun         }
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun         pMaskPixmap = (*pScreen->CreatePixmap) (pScreen, width, height,
719*4882a593Smuzhiyun                                                 maskFormat->depth,
720*4882a593Smuzhiyun                                                 CREATE_PIXMAP_USAGE_SCRATCH);
721*4882a593Smuzhiyun         if (!pMaskPixmap)
722*4882a593Smuzhiyun             return;
723*4882a593Smuzhiyun         component_alpha = NeedsComponent(maskFormat->format);
724*4882a593Smuzhiyun         pMask = CreatePicture(0, &pMaskPixmap->drawable,
725*4882a593Smuzhiyun                               maskFormat, CPComponentAlpha, &component_alpha,
726*4882a593Smuzhiyun                               serverClient, &error);
727*4882a593Smuzhiyun         if (!pMask ||
728*4882a593Smuzhiyun             (!component_alpha && pExaScr->info->CheckComposite &&
729*4882a593Smuzhiyun              !(*pExaScr->info->CheckComposite) (PictOpAdd, pSrc, NULL, pMask)))
730*4882a593Smuzhiyun         {
731*4882a593Smuzhiyun             PictFormatPtr argbFormat;
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun             (*pScreen->DestroyPixmap) (pMaskPixmap);
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun             if (!pMask)
736*4882a593Smuzhiyun                 return;
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun             /* The driver can't seem to composite to a8, let's try argb (but
739*4882a593Smuzhiyun              * without component-alpha) */
740*4882a593Smuzhiyun             FreePicture((void *) pMask, (XID) 0);
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun             argbFormat = PictureMatchFormat(pScreen, 32, PICT_a8r8g8b8);
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun             if (argbFormat)
745*4882a593Smuzhiyun                 maskFormat = argbFormat;
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun             pMaskPixmap = (*pScreen->CreatePixmap) (pScreen, width, height,
748*4882a593Smuzhiyun                                                     maskFormat->depth,
749*4882a593Smuzhiyun                                                     CREATE_PIXMAP_USAGE_SCRATCH);
750*4882a593Smuzhiyun             if (!pMaskPixmap)
751*4882a593Smuzhiyun                 return;
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun             pMask = CreatePicture(0, &pMaskPixmap->drawable, maskFormat, 0, 0,
754*4882a593Smuzhiyun                                   serverClient, &error);
755*4882a593Smuzhiyun             if (!pMask) {
756*4882a593Smuzhiyun                 (*pScreen->DestroyPixmap) (pMaskPixmap);
757*4882a593Smuzhiyun                 return;
758*4882a593Smuzhiyun             }
759*4882a593Smuzhiyun         }
760*4882a593Smuzhiyun         pGC = GetScratchGC(pMaskPixmap->drawable.depth, pScreen);
761*4882a593Smuzhiyun         ValidateGC(&pMaskPixmap->drawable, pGC);
762*4882a593Smuzhiyun         rect.x = 0;
763*4882a593Smuzhiyun         rect.y = 0;
764*4882a593Smuzhiyun         rect.width = width;
765*4882a593Smuzhiyun         rect.height = height;
766*4882a593Smuzhiyun         (*pGC->ops->PolyFillRect) (&pMaskPixmap->drawable, pGC, 1, &rect);
767*4882a593Smuzhiyun         FreeScratchGC(pGC);
768*4882a593Smuzhiyun         x = -extents.x1;
769*4882a593Smuzhiyun         y = -extents.y1;
770*4882a593Smuzhiyun     }
771*4882a593Smuzhiyun     else {
772*4882a593Smuzhiyun         x = 0;
773*4882a593Smuzhiyun         y = 0;
774*4882a593Smuzhiyun     }
775*4882a593Smuzhiyun     buffer.count = 0;
776*4882a593Smuzhiyun     buffer.mask = NULL;
777*4882a593Smuzhiyun     while (nlist--) {
778*4882a593Smuzhiyun         x += list->xOff;
779*4882a593Smuzhiyun         y += list->yOff;
780*4882a593Smuzhiyun         n = list->len;
781*4882a593Smuzhiyun         while (n--) {
782*4882a593Smuzhiyun             glyph = *glyphs++;
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun             if (glyph->info.width > 0 && glyph->info.height > 0) {
785*4882a593Smuzhiyun                 /* pGlyph->info.{x,y} compensate for empty space in the glyph. */
786*4882a593Smuzhiyun                 if (maskFormat) {
787*4882a593Smuzhiyun                     if (exaBufferGlyph(pScreen, &buffer, glyph, NULL, pMask,
788*4882a593Smuzhiyun                                        0, 0, 0, 0, x - glyph->info.x,
789*4882a593Smuzhiyun                                        y - glyph->info.y) ==
790*4882a593Smuzhiyun                         ExaGlyphNeedFlush) {
791*4882a593Smuzhiyun                         exaGlyphsToMask(pMask, &buffer);
792*4882a593Smuzhiyun                         exaBufferGlyph(pScreen, &buffer, glyph, NULL, pMask,
793*4882a593Smuzhiyun                                        0, 0, 0, 0, x - glyph->info.x,
794*4882a593Smuzhiyun                                        y - glyph->info.y);
795*4882a593Smuzhiyun                     }
796*4882a593Smuzhiyun                 }
797*4882a593Smuzhiyun                 else {
798*4882a593Smuzhiyun                     if (exaBufferGlyph(pScreen, &buffer, glyph, pSrc, pDst,
799*4882a593Smuzhiyun                                        xSrc + (x - glyph->info.x) - first_xOff,
800*4882a593Smuzhiyun                                        ySrc + (y - glyph->info.y) - first_yOff,
801*4882a593Smuzhiyun                                        0, 0, x - glyph->info.x,
802*4882a593Smuzhiyun                                        y - glyph->info.y)
803*4882a593Smuzhiyun                         == ExaGlyphNeedFlush) {
804*4882a593Smuzhiyun                         exaGlyphsToDst(op, pSrc, pDst, &buffer);
805*4882a593Smuzhiyun                         exaBufferGlyph(pScreen, &buffer, glyph, pSrc, pDst,
806*4882a593Smuzhiyun                                        xSrc + (x - glyph->info.x) - first_xOff,
807*4882a593Smuzhiyun                                        ySrc + (y - glyph->info.y) - first_yOff,
808*4882a593Smuzhiyun                                        0, 0, x - glyph->info.x,
809*4882a593Smuzhiyun                                        y - glyph->info.y);
810*4882a593Smuzhiyun                     }
811*4882a593Smuzhiyun                 }
812*4882a593Smuzhiyun             }
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun             x += glyph->info.xOff;
815*4882a593Smuzhiyun             y += glyph->info.yOff;
816*4882a593Smuzhiyun         }
817*4882a593Smuzhiyun         list++;
818*4882a593Smuzhiyun     }
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun     if (buffer.count) {
821*4882a593Smuzhiyun         if (maskFormat)
822*4882a593Smuzhiyun             exaGlyphsToMask(pMask, &buffer);
823*4882a593Smuzhiyun         else
824*4882a593Smuzhiyun             exaGlyphsToDst(op, pSrc, pDst, &buffer);
825*4882a593Smuzhiyun     }
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun     if (maskFormat) {
828*4882a593Smuzhiyun         x = extents.x1;
829*4882a593Smuzhiyun         y = extents.y1;
830*4882a593Smuzhiyun         CompositePicture(op,
831*4882a593Smuzhiyun                          pSrc,
832*4882a593Smuzhiyun                          pMask,
833*4882a593Smuzhiyun                          pDst,
834*4882a593Smuzhiyun                          xSrc + x - first_xOff,
835*4882a593Smuzhiyun                          ySrc + y - first_yOff, 0, 0, x, y, width, height);
836*4882a593Smuzhiyun         FreePicture((void *) pMask, (XID) 0);
837*4882a593Smuzhiyun         (*pScreen->DestroyPixmap) (pMaskPixmap);
838*4882a593Smuzhiyun     }
839*4882a593Smuzhiyun }
840