Stub implementation for GetLastInputInfo.
[wine] / dlls / x11drv / xrender.c
1 /*
2  * Functions to use the XRender extension
3  *
4  * Copyright 2001, 2002 Huw D M Davies for CodeWeavers
5  *
6  * Some parts also:
7  * Copyright 2000 Keith Packard, member of The XFree86 Project, Inc.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <assert.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wownt32.h"
34 #include "x11drv.h"
35 #include "gdi.h"
36 #include "wine/library.h"
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
39
40 static BOOL X11DRV_XRender_Installed = FALSE;
41 int using_client_side_fonts = FALSE;
42
43 WINE_DEFAULT_DEBUG_CHANNEL(xrender);
44
45 #ifdef HAVE_X11_EXTENSIONS_XRENDER_H
46
47 #include <X11/Xlib.h>
48 #include <X11/extensions/Xrender.h>
49
50 /* Older version of the Xrender headers don't define these */
51 #ifndef PictStandardARGB32
52
53 #define PictStandardARGB32 0
54 XRenderPictFormat * XRenderFindStandardFormat (Display *dpy, int format);
55
56 #endif
57
58 static XRenderPictFormat *screen_format; /* format of screen */
59 static XRenderPictFormat *mono_format; /* format of mono bitmap */
60
61 typedef struct
62 {
63     LOGFONTW lf;
64     SIZE     devsize;  /* size in device coords */
65     DWORD    hash;
66 } LFANDSIZE;
67
68 #define INITIAL_REALIZED_BUF_SIZE 128
69
70 typedef enum { AA_None, AA_Grey, AA_RGB, AA_BGR, AA_VRGB, AA_VBGR } AA_Type;
71
72 typedef struct
73 {
74     LFANDSIZE lfsz;
75     AA_Type aa;
76     GlyphSet glyphset;
77     XRenderPictFormat *font_format;
78     int nrealized;
79     BOOL *realized;
80     void **bitmaps;
81     XGlyphInfo *gis;
82     UINT count;
83     INT next;
84 } gsCacheEntry;
85
86 struct tagXRENDERINFO
87 {
88     int                cache_index;
89     Picture            pict;
90     Picture            tile_pict;
91     Pixmap             tile_xpm;
92     COLORREF           lastTextColor;
93 };
94
95
96 static gsCacheEntry *glyphsetCache = NULL;
97 static DWORD glyphsetCacheSize = 0;
98 static INT lastfree = -1;
99 static INT mru = -1;
100
101 #define INIT_CACHE_SIZE 10
102
103 static int antialias = 1;
104
105 /* some default values just in case */
106 #ifndef SONAME_LIBX11
107 #define SONAME_LIBX11 "libX11.so"
108 #endif
109 #ifndef SONAME_LIBXEXT
110 #define SONAME_LIBXEXT "libXext.so"
111 #endif
112 #ifndef SONAME_LIBXRENDER
113 #define SONAME_LIBXRENDER "libXrender.so"
114 #endif
115
116 static void *xrender_handle;
117
118 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
119 MAKE_FUNCPTR(XRenderAddGlyphs)
120 MAKE_FUNCPTR(XRenderComposite)
121 MAKE_FUNCPTR(XRenderCompositeString8)
122 MAKE_FUNCPTR(XRenderCompositeString16)
123 MAKE_FUNCPTR(XRenderCompositeString32)
124 MAKE_FUNCPTR(XRenderCreateGlyphSet)
125 MAKE_FUNCPTR(XRenderCreatePicture)
126 MAKE_FUNCPTR(XRenderFillRectangle)
127 MAKE_FUNCPTR(XRenderFindFormat)
128 MAKE_FUNCPTR(XRenderFindStandardFormat)
129 MAKE_FUNCPTR(XRenderFindVisualFormat)
130 MAKE_FUNCPTR(XRenderFreeGlyphSet)
131 MAKE_FUNCPTR(XRenderFreePicture)
132 MAKE_FUNCPTR(XRenderSetPictureClipRectangles)
133 #ifdef HAVE_XRENDERSETPICTURETRANSFORM
134 MAKE_FUNCPTR(XRenderSetPictureTransform)
135 #endif
136 MAKE_FUNCPTR(XRenderQueryExtension)
137 #undef MAKE_FUNCPTR
138
139 static CRITICAL_SECTION xrender_cs;
140 static CRITICAL_SECTION_DEBUG critsect_debug =
141 {
142     0, 0, &xrender_cs,
143     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
144       0, 0, { 0, (DWORD)(__FILE__ ": xrender_cs") }
145 };
146 static CRITICAL_SECTION xrender_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
147
148
149 /***********************************************************************
150  *   X11DRV_XRender_Init
151  *
152  * Let's see if our XServer has the extension available
153  *
154  */
155 void X11DRV_XRender_Init(void)
156 {
157     int error_base, event_base, i;
158     XRenderPictFormat pf;
159
160     if (client_side_with_render &&
161         wine_dlopen(SONAME_LIBX11, RTLD_NOW|RTLD_GLOBAL, NULL, 0) &&
162         wine_dlopen(SONAME_LIBXEXT, RTLD_NOW|RTLD_GLOBAL, NULL, 0) && 
163         (xrender_handle = wine_dlopen(SONAME_LIBXRENDER, RTLD_NOW, NULL, 0)))
164     {
165
166 #define LOAD_FUNCPTR(f) if((p##f = wine_dlsym(xrender_handle, #f, NULL, 0)) == NULL) goto sym_not_found;
167 LOAD_FUNCPTR(XRenderAddGlyphs)
168 LOAD_FUNCPTR(XRenderComposite)
169 LOAD_FUNCPTR(XRenderCompositeString8)
170 LOAD_FUNCPTR(XRenderCompositeString16)
171 LOAD_FUNCPTR(XRenderCompositeString32)
172 LOAD_FUNCPTR(XRenderCreateGlyphSet)
173 LOAD_FUNCPTR(XRenderCreatePicture)
174 LOAD_FUNCPTR(XRenderFillRectangle)
175 LOAD_FUNCPTR(XRenderFindFormat)
176 LOAD_FUNCPTR(XRenderFindStandardFormat)
177 LOAD_FUNCPTR(XRenderFindVisualFormat)
178 LOAD_FUNCPTR(XRenderFreeGlyphSet)
179 LOAD_FUNCPTR(XRenderFreePicture)
180 LOAD_FUNCPTR(XRenderSetPictureClipRectangles)
181 LOAD_FUNCPTR(XRenderQueryExtension)
182 #undef LOAD_FUNCPTR
183 #ifdef HAVE_XRENDERSETPICTURETRANSFORM
184 #define LOAD_OPTIONAL_FUNCPTR(f) p##f = wine_dlsym(xrender_handle, #f, NULL, 0);
185 LOAD_OPTIONAL_FUNCPTR(XRenderSetPictureTransform)
186 #undef LOAD_OPTIONAL_FUNCPTR
187 #endif
188
189
190         wine_tsx11_lock();
191         if(pXRenderQueryExtension(gdi_display, &event_base, &error_base)) {
192             X11DRV_XRender_Installed = TRUE;
193             TRACE("Xrender is up and running error_base = %d\n", error_base);
194             screen_format = pXRenderFindVisualFormat(gdi_display, visual);
195             if(!screen_format) { /* This fails in buggy versions of libXrender.so */
196                 wine_tsx11_unlock();
197                 WINE_MESSAGE(
198                     "Wine has detected that you probably have a buggy version\n"
199                     "of libXrender.so .  Because of this client side font rendering\n"
200                     "will be disabled.  Please upgrade this library.\n");
201                 X11DRV_XRender_Installed = FALSE;
202                 return;
203             }
204             pf.type = PictTypeDirect;
205             pf.depth = 1;
206             pf.direct.alpha = 0;
207             pf.direct.alphaMask = 1;
208             mono_format = pXRenderFindFormat(gdi_display, PictFormatType |
209                                              PictFormatDepth | PictFormatAlpha |
210                                              PictFormatAlphaMask, &pf, 0);
211             if(!mono_format) {
212                 ERR("mono_format == NULL?\n");
213                 X11DRV_XRender_Installed = FALSE;
214             }
215         }
216         wine_tsx11_unlock();
217     }
218
219 sym_not_found:
220     if(X11DRV_XRender_Installed || client_side_with_core)
221     {
222         glyphsetCache = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
223                                   sizeof(*glyphsetCache) * INIT_CACHE_SIZE);
224
225         glyphsetCacheSize = INIT_CACHE_SIZE;
226         lastfree = 0;
227         for(i = 0; i < INIT_CACHE_SIZE; i++) {
228           glyphsetCache[i].next = i + 1;
229           glyphsetCache[i].count = -1;
230         }
231         glyphsetCache[i-1].next = -1;
232         using_client_side_fonts = 1;
233
234         if(!X11DRV_XRender_Installed) {
235             TRACE("Xrender is not available on your XServer, client side rendering with the core protocol instead.\n");
236             if(screen_depth <= 8 || !client_side_antialias_with_core)
237                 antialias = 0;
238         } else {
239             if(screen_depth <= 8 || !client_side_antialias_with_render)
240                 antialias = 0;
241         }
242     }
243     else TRACE("Using X11 core fonts\n");
244 }
245
246 static BOOL fontcmp(LFANDSIZE *p1, LFANDSIZE *p2)
247 {
248   if(p1->hash != p2->hash) return TRUE;
249   if(memcmp(&p1->devsize, &p2->devsize, sizeof(p1->devsize))) return TRUE;
250   if(memcmp(&p1->lf, &p2->lf, offsetof(LOGFONTW, lfFaceName))) return TRUE;
251   return strcmpW(p1->lf.lfFaceName, p2->lf.lfFaceName);
252 }
253
254 #if 0
255 static void walk_cache(void)
256 {
257   int i;
258
259   EnterCriticalSection(&xrender_cs);
260   for(i=mru; i >= 0; i = glyphsetCache[i].next)
261     TRACE("item %d\n", i);
262   LeaveCriticalSection(&xrender_cs);
263 }
264 #endif
265
266 static int LookupEntry(LFANDSIZE *plfsz)
267 {
268   int i, prev_i = -1;
269
270   for(i = mru; i >= 0; i = glyphsetCache[i].next) {
271     TRACE("%d\n", i);
272     if(glyphsetCache[i].count == -1) { /* reached free list so stop */
273       i = -1;
274       break;
275     }
276
277     if(!fontcmp(&glyphsetCache[i].lfsz, plfsz)) {
278       glyphsetCache[i].count++;
279       if(prev_i >= 0) {
280         glyphsetCache[prev_i].next = glyphsetCache[i].next;
281         glyphsetCache[i].next = mru;
282         mru = i;
283       }
284       TRACE("found font in cache %d\n", i);
285       return i;
286     }
287     prev_i = i;
288   }
289   TRACE("font not in cache\n");
290   return -1;
291 }
292
293 static void FreeEntry(int entry)
294 {
295     int i;
296
297     if(glyphsetCache[entry].glyphset) {
298         wine_tsx11_lock();
299         pXRenderFreeGlyphSet(gdi_display, glyphsetCache[entry].glyphset);
300         wine_tsx11_unlock();
301         glyphsetCache[entry].glyphset = 0;
302     }
303     if(glyphsetCache[entry].nrealized) {
304         HeapFree(GetProcessHeap(), 0, glyphsetCache[entry].realized);
305         glyphsetCache[entry].realized = NULL;
306         if(glyphsetCache[entry].bitmaps) {
307             for(i = 0; i < glyphsetCache[entry].nrealized; i++)
308                 if(glyphsetCache[entry].bitmaps[i])
309                     HeapFree(GetProcessHeap(), 0, glyphsetCache[entry].bitmaps[i]);
310             HeapFree(GetProcessHeap(), 0, glyphsetCache[entry].bitmaps);
311             glyphsetCache[entry].bitmaps = NULL;
312             HeapFree(GetProcessHeap(), 0, glyphsetCache[entry].gis);
313             glyphsetCache[entry].gis = NULL;
314         }
315         glyphsetCache[entry].nrealized = 0;
316     }
317 }
318
319 static int AllocEntry(void)
320 {
321   int best = -1, prev_best = -1, i, prev_i = -1;
322
323   if(lastfree >= 0) {
324     assert(glyphsetCache[lastfree].count == -1);
325     glyphsetCache[lastfree].count = 1;
326     best = lastfree;
327     lastfree = glyphsetCache[lastfree].next;
328     assert(best != mru);
329     glyphsetCache[best].next = mru;
330     mru = best;
331
332     TRACE("empty space at %d, next lastfree = %d\n", mru, lastfree);
333     return mru;
334   }
335
336   for(i = mru; i >= 0; i = glyphsetCache[i].next) {
337     if(glyphsetCache[i].count == 0) {
338       best = i;
339       prev_best = prev_i;
340     }
341     prev_i = i;
342   }
343
344   if(best >= 0) {
345     TRACE("freeing unused glyphset at cache %d\n", best);
346     FreeEntry(best);
347     glyphsetCache[best].count = 1;
348     if(prev_best >= 0) {
349       glyphsetCache[prev_best].next = glyphsetCache[best].next;
350       glyphsetCache[best].next = mru;
351       mru = best;
352     } else {
353       assert(mru == best);
354     }
355     return mru;
356   }
357
358   TRACE("Growing cache\n");
359   
360   if (glyphsetCache)
361     glyphsetCache = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
362                               glyphsetCache,
363                               (glyphsetCacheSize + INIT_CACHE_SIZE)
364                               * sizeof(*glyphsetCache));
365   else
366     glyphsetCache = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
367                               (glyphsetCacheSize + INIT_CACHE_SIZE)
368                               * sizeof(*glyphsetCache));
369
370   for(best = i = glyphsetCacheSize; i < glyphsetCacheSize + INIT_CACHE_SIZE;
371       i++) {
372     glyphsetCache[i].next = i + 1;
373     glyphsetCache[i].count = -1;
374   }
375   glyphsetCache[i-1].next = -1;
376   glyphsetCacheSize += INIT_CACHE_SIZE;
377
378   lastfree = glyphsetCache[best].next;
379   glyphsetCache[best].count = 1;
380   glyphsetCache[best].next = mru;
381   mru = best;
382   TRACE("new free cache slot at %d\n", mru);
383   return mru;
384 }
385
386 static int GetCacheEntry(LFANDSIZE *plfsz)
387 {
388     int ret;
389     gsCacheEntry *entry;
390
391     if((ret = LookupEntry(plfsz)) != -1) return ret;
392
393     ret = AllocEntry();
394     entry = glyphsetCache + ret;
395     entry->lfsz = *plfsz;
396     assert(entry->nrealized == 0);
397
398     if(antialias)
399         entry->aa = AA_Grey;
400     else
401         entry->aa = AA_None;
402
403     entry->font_format = NULL;
404     entry->glyphset = 0;
405     return ret;
406 }
407
408 static void dec_ref_cache(int index)
409 {
410     assert(index >= 0);
411     TRACE("dec'ing entry %d to %d\n", index, glyphsetCache[index].count - 1);
412     assert(glyphsetCache[index].count > 0);
413     glyphsetCache[index].count--;
414 }
415
416 static void lfsz_calc_hash(LFANDSIZE *plfsz)
417 {
418   DWORD hash = 0, *ptr;
419   int i;
420
421   hash ^= plfsz->devsize.cx;
422   hash ^= plfsz->devsize.cy;
423   for(i = 0, ptr = (DWORD*)&plfsz->lf; i < 7; i++, ptr++)
424     hash ^= *ptr;
425   for(i = 0, ptr = (DWORD*)&plfsz->lf.lfFaceName; i < LF_FACESIZE/2; i++, ptr++) {
426     WCHAR *pwc = (WCHAR *)ptr;
427     if(!*pwc) break;
428     hash ^= *ptr;
429     pwc++;
430     if(!*pwc) break;
431   }
432   plfsz->hash = hash;
433   return;
434 }
435
436 /***********************************************************************
437  *   X11DRV_XRender_Finalize
438  */
439 void X11DRV_XRender_Finalize(void)
440 {
441     int i;
442
443     EnterCriticalSection(&xrender_cs);
444     for(i = mru; i >= 0; i = glyphsetCache[i].next)
445         FreeEntry(i);
446     LeaveCriticalSection(&xrender_cs);
447 }
448
449
450 /***********************************************************************
451  *   X11DRV_XRender_SelectFont
452  */
453 BOOL X11DRV_XRender_SelectFont(X11DRV_PDEVICE *physDev, HFONT hfont)
454 {
455     LFANDSIZE lfsz;
456
457     GetObjectW(hfont, sizeof(lfsz.lf), &lfsz.lf);
458     TRACE("h=%ld w=%ld weight=%ld it=%d charset=%d name=%s\n",
459           lfsz.lf.lfHeight, lfsz.lf.lfWidth, lfsz.lf.lfWeight,
460           lfsz.lf.lfItalic, lfsz.lf.lfCharSet, debugstr_w(lfsz.lf.lfFaceName));
461     lfsz.devsize.cx = X11DRV_XWStoDS( physDev, lfsz.lf.lfWidth );
462     lfsz.devsize.cy = X11DRV_YWStoDS( physDev, lfsz.lf.lfHeight );
463     lfsz_calc_hash(&lfsz);
464
465     EnterCriticalSection(&xrender_cs);
466     if(!physDev->xrender) {
467         physDev->xrender = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
468                                      sizeof(*physDev->xrender));
469         physDev->xrender->cache_index = -1;
470     }
471     else if(physDev->xrender->cache_index != -1)
472         dec_ref_cache(physDev->xrender->cache_index);
473     physDev->xrender->cache_index = GetCacheEntry(&lfsz);
474     LeaveCriticalSection(&xrender_cs);
475     return 0;
476 }
477
478 /***********************************************************************
479  *   X11DRV_XRender_DeleteDC
480  */
481 void X11DRV_XRender_DeleteDC(X11DRV_PDEVICE *physDev)
482 {
483     wine_tsx11_lock();
484     if(physDev->xrender->tile_pict)
485         pXRenderFreePicture(gdi_display, physDev->xrender->tile_pict);
486
487     if(physDev->xrender->tile_xpm)
488         XFreePixmap(gdi_display, physDev->xrender->tile_xpm);
489
490     if(physDev->xrender->pict) {
491         TRACE("freeing pict = %lx dc = %p\n", physDev->xrender->pict, physDev->hdc);
492         pXRenderFreePicture(gdi_display, physDev->xrender->pict);
493     }
494     wine_tsx11_unlock();
495
496     EnterCriticalSection(&xrender_cs);
497     if(physDev->xrender->cache_index != -1)
498         dec_ref_cache(physDev->xrender->cache_index);
499     LeaveCriticalSection(&xrender_cs);
500
501     HeapFree(GetProcessHeap(), 0, physDev->xrender);
502     physDev->xrender = NULL;
503     return;
504 }
505
506 /***********************************************************************
507  *   X11DRV_XRender_UpdateDrawable
508  *
509  * This gets called from X11DRV_SetDrawable and X11DRV_SelectBitmap.
510  * It deletes the pict when the drawable changes.
511  */
512 void X11DRV_XRender_UpdateDrawable(X11DRV_PDEVICE *physDev)
513 {
514     if(physDev->xrender->pict) {
515         TRACE("freeing pict %08lx from dc %p drawable %08lx\n", physDev->xrender->pict,
516               physDev->hdc, physDev->drawable);
517         wine_tsx11_lock();
518         XFlush(gdi_display);
519         pXRenderFreePicture(gdi_display, physDev->xrender->pict);
520         wine_tsx11_unlock();
521     }
522     physDev->xrender->pict = 0;
523     return;
524 }
525
526 /************************************************************************
527  *   UploadGlyph
528  *
529  * Helper to ExtTextOut.  Must be called inside xrender_cs
530  */
531 static BOOL UploadGlyph(X11DRV_PDEVICE *physDev, int glyph)
532 {
533     unsigned int buflen;
534     char *buf;
535     Glyph gid;
536     GLYPHMETRICS gm;
537     XGlyphInfo gi;
538     gsCacheEntry *entry = glyphsetCache + physDev->xrender->cache_index;
539     UINT ggo_format = GGO_GLYPH_INDEX;
540     XRenderPictFormat pf;
541
542     if(entry->nrealized <= glyph) {
543         entry->nrealized = (glyph / 128 + 1) * 128;
544
545         if (entry->realized)
546             entry->realized = HeapReAlloc(GetProcessHeap(),
547                                       HEAP_ZERO_MEMORY,
548                                       entry->realized,
549                                       entry->nrealized * sizeof(BOOL));
550         else
551             entry->realized = HeapAlloc(GetProcessHeap(),
552                                       HEAP_ZERO_MEMORY,
553                                       entry->nrealized * sizeof(BOOL));
554
555         if(!X11DRV_XRender_Installed) {
556           if (entry->bitmaps)
557             entry->bitmaps = HeapReAlloc(GetProcessHeap(),
558                                       HEAP_ZERO_MEMORY,
559                                       entry->bitmaps,
560                                       entry->nrealized * sizeof(entry->bitmaps[0]));
561           else
562             entry->bitmaps = HeapAlloc(GetProcessHeap(),
563                                       HEAP_ZERO_MEMORY,
564                                       entry->nrealized * sizeof(entry->bitmaps[0]));
565
566           if (entry->gis)
567             entry->gis = HeapReAlloc(GetProcessHeap(),
568                                    HEAP_ZERO_MEMORY,
569                                    entry->gis,
570                                    entry->nrealized * sizeof(entry->gis[0]));
571           else
572             entry->gis = HeapAlloc(GetProcessHeap(),
573                                    HEAP_ZERO_MEMORY,
574                                    entry->nrealized * sizeof(entry->gis[0]));
575         }
576     }
577
578     switch(entry->aa) {
579     case AA_Grey:
580         ggo_format |= WINE_GGO_GRAY16_BITMAP;
581         break;
582
583     default:
584         ERR("aa = %d - not implemented\n", entry->aa);
585     case AA_None:
586         ggo_format |= GGO_BITMAP;
587         break;
588     }
589
590     buflen = GetGlyphOutlineW(physDev->hdc, glyph, ggo_format, &gm, 0, NULL,
591                               NULL);
592     if(buflen == GDI_ERROR) {
593         if(entry->aa != AA_None) {
594             entry->aa = AA_None;
595             ggo_format &= ~WINE_GGO_GRAY16_BITMAP;
596             ggo_format |= GGO_BITMAP;
597             buflen = GetGlyphOutlineW(physDev->hdc, glyph, ggo_format, &gm, 0, NULL,
598                                       NULL);
599         }
600         if(buflen == GDI_ERROR) {
601             ERR("GetGlyphOutlineW failed\n");
602             return FALSE;
603         }
604         TRACE("Turning off antialiasing for this monochrome font\n");
605     }
606
607     if(entry->glyphset == 0 && X11DRV_XRender_Installed) {
608         switch(entry->aa) {
609         case AA_Grey:
610             pf.depth = 8;
611             pf.direct.alphaMask = 0xff;
612             break;
613
614         default:
615             ERR("aa = %d - not implemented\n", entry->aa);
616         case AA_None:
617             pf.depth = 1;
618             pf.direct.alphaMask = 1;
619             break;
620         }
621
622         pf.type = PictTypeDirect;
623         pf.direct.alpha = 0;
624
625         wine_tsx11_lock();
626         entry->font_format = pXRenderFindFormat(gdi_display,
627                                                 PictFormatType |
628                                                 PictFormatDepth |
629                                                 PictFormatAlpha |
630                                                 PictFormatAlphaMask,
631                                                 &pf, 0);
632
633         entry->glyphset = pXRenderCreateGlyphSet(gdi_display, entry->font_format);
634         wine_tsx11_unlock();
635     }
636
637
638     buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buflen);
639     GetGlyphOutlineW(physDev->hdc, glyph, ggo_format, &gm, buflen, buf, NULL);
640     entry->realized[glyph] = TRUE;
641
642     TRACE("buflen = %d. Got metrics: %dx%d adv=%d,%d origin=%ld,%ld\n",
643           buflen,
644           gm.gmBlackBoxX, gm.gmBlackBoxY, gm.gmCellIncX, gm.gmCellIncY,
645           gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
646
647     gi.width = gm.gmBlackBoxX;
648     gi.height = gm.gmBlackBoxY;
649     gi.x = -gm.gmptGlyphOrigin.x;
650     gi.y = gm.gmptGlyphOrigin.y;
651     gi.xOff = gm.gmCellIncX;
652     gi.yOff = gm.gmCellIncY;
653
654     if(TRACE_ON(xrender)) {
655         int pitch, i, j;
656         char output[300];
657         unsigned char *line;
658
659         if(entry->aa == AA_None) {
660             pitch = ((gi.width + 31) / 32) * 4;
661             for(i = 0; i < gi.height; i++) {
662                 line = buf + i * pitch;
663                 output[0] = '\0';
664                 for(j = 0; j < pitch * 8; j++) {
665                     strcat(output, (line[j / 8] & (1 << (7 - (j % 8)))) ? "#" : " ");
666                 }
667                 strcat(output, "\n");
668                 TRACE(output);
669             }
670         } else {
671             static const char blks[] = " .:;!o*#";
672             char str[2];
673
674             str[1] = '\0';
675             pitch = ((gi.width + 3) / 4) * 4;
676             for(i = 0; i < gi.height; i++) {
677                 line = buf + i * pitch;
678                 output[0] = '\0';
679                 for(j = 0; j < pitch; j++) {
680                     str[0] = blks[line[j] >> 5];
681                     strcat(output, str);
682                 }
683                 strcat(output, "\n");
684                 TRACE(output);
685             }
686         }
687     }
688
689     if(entry->glyphset) {
690         if(entry->aa == AA_None && BitmapBitOrder(gdi_display) != MSBFirst) {
691             unsigned char *byte = buf, c;
692             int i = buflen;
693
694             while(i--) {
695                 c = *byte;
696
697                 /* magic to flip bit order */
698                 c = ((c << 1) & 0xaa) | ((c >> 1) & 0x55);
699                 c = ((c << 2) & 0xcc) | ((c >> 2) & 0x33);
700                 c = ((c << 4) & 0xf0) | ((c >> 4) & 0x0f);
701
702                 *byte++ = c;
703             }
704         }
705         gid = glyph;
706         wine_tsx11_lock();
707         pXRenderAddGlyphs(gdi_display, entry->glyphset, &gid, &gi, 1,
708                           buf, buflen);
709         wine_tsx11_unlock();
710         HeapFree(GetProcessHeap(), 0, buf);
711     } else {
712         entry->bitmaps[glyph] = buf;
713         memcpy(&entry->gis[glyph], &gi, sizeof(gi));
714     }
715     return TRUE;
716 }
717
718 static void SharpGlyphMono(X11DRV_PDEVICE *physDev, INT x, INT y,
719                             void *bitmap, XGlyphInfo *gi)
720 {
721     unsigned char   *srcLine = bitmap, *src;
722     unsigned char   bits, bitsMask;
723     int             width = gi->width;
724     int             stride = ((width + 31) & ~31) >> 3;
725     int             height = gi->height;
726     int             w;
727     int             xspan, lenspan;
728
729     TRACE("%d, %d\n", x, y);
730     x -= gi->x;
731     y -= gi->y;
732     while (height--)
733     {
734         src = srcLine;
735         srcLine += stride;
736         w = width;
737         
738         bitsMask = 0x80;    /* FreeType is always MSB first */
739         bits = *src++;
740         
741         xspan = x;
742         while (w)
743         {
744             if (bits & bitsMask)
745             {
746                 lenspan = 0;
747                 do
748                 {
749                     lenspan++;
750                     if (lenspan == w)
751                         break;
752                     bitsMask = bitsMask >> 1;
753                     if (!bitsMask)
754                     {
755                         bits = *src++;
756                         bitsMask = 0x80;
757                     }
758                 } while (bits & bitsMask);
759                 XFillRectangle (gdi_display, physDev->drawable, 
760                                 physDev->gc, xspan, y, lenspan, 1);
761                 xspan += lenspan;
762                 w -= lenspan;
763             }
764             else
765             {
766                 do
767                 {
768                     w--;
769                     xspan++;
770                     if (!w)
771                         break;
772                     bitsMask = bitsMask >> 1;
773                     if (!bitsMask)
774                     {
775                         bits = *src++;
776                         bitsMask = 0x80;
777                     }
778                 } while (!(bits & bitsMask));
779             }
780         }
781         y++;
782     }
783 }
784
785 static void SharpGlyphGray(X11DRV_PDEVICE *physDev, INT x, INT y,
786                             void *bitmap, XGlyphInfo *gi)
787 {
788     unsigned char   *srcLine = bitmap, *src, bits;
789     int             width = gi->width;
790     int             stride = ((width + 3) & ~3);
791     int             height = gi->height;
792     int             w;
793     int             xspan, lenspan;
794
795     x -= gi->x;
796     y -= gi->y;
797     while (height--)
798     {
799         src = srcLine;
800         srcLine += stride;
801         w = width;
802         
803         bits = *src++;
804         xspan = x;
805         while (w)
806         {
807             if (bits >= 0x80)
808             {
809                 lenspan = 0;
810                 do
811                 {
812                     lenspan++;
813                     if (lenspan == w)
814                         break;
815                     bits = *src++;
816                 } while (bits >= 0x80);
817                 XFillRectangle (gdi_display, physDev->drawable, 
818                                 physDev->gc, xspan, y, lenspan, 1);
819                 xspan += lenspan;
820                 w -= lenspan;
821             }
822             else
823             {
824                 do
825                 {
826                     w--;
827                     xspan++;
828                     if (!w)
829                         break;
830                     bits = *src++;
831                 } while (bits < 0x80);
832             }
833         }
834         y++;
835     }
836 }
837
838
839 static void ExamineBitfield (DWORD mask, int *shift, int *len)
840 {
841     int s, l;
842
843     s = 0;
844     while ((mask & 1) == 0)
845     {
846         mask >>= 1;
847         s++;
848     }
849     l = 0;
850     while ((mask & 1) == 1)
851     {
852         mask >>= 1;
853         l++;
854     }
855     *shift = s;
856     *len = l;
857 }
858
859 static DWORD GetField (DWORD pixel, int shift, int len)
860 {
861     pixel = pixel & (((1 << (len)) - 1) << shift);
862     pixel = pixel << (32 - (shift + len)) >> 24;
863     while (len < 8)
864     {
865         pixel |= (pixel >> len);
866         len <<= 1;
867     }
868     return pixel;
869 }
870
871
872 static DWORD PutField (DWORD pixel, int shift, int len)
873 {
874     shift = shift - (8 - len);
875     if (len <= 8)
876         pixel &= (((1 << len) - 1) << (8 - len));
877     if (shift < 0)
878         pixel >>= -shift;
879     else
880         pixel <<= shift;
881     return pixel;
882 }
883
884 static void SmoothGlyphGray(XImage *image, int x, int y, void *bitmap, XGlyphInfo *gi,
885                             int color)
886 {
887     int             r_shift, r_len;
888     int             g_shift, g_len;
889     int             b_shift, b_len;
890     BYTE            *maskLine, *mask, m;
891     int             maskStride;
892     DWORD           pixel;
893     int             width, height;
894     int             w, tx;
895     BYTE            src_r, src_g, src_b;
896
897     x -= gi->x;
898     y -= gi->y;
899     width = gi->width;
900     height = gi->height;
901
902     maskLine = (unsigned char *) bitmap;
903     maskStride = (width + 3) & ~3;
904
905     ExamineBitfield (image->red_mask, &r_shift, &r_len);
906     ExamineBitfield (image->green_mask, &g_shift, &g_len);
907     ExamineBitfield (image->blue_mask, &b_shift, &b_len);
908
909     src_r = GetField(color, r_shift, r_len);
910     src_g = GetField(color, g_shift, g_len);
911     src_b = GetField(color, b_shift, b_len);
912     
913     for(; height--; y++)
914     {
915         mask = maskLine;
916         maskLine += maskStride;
917         w = width;
918         tx = x;
919
920         if(y < 0) continue;
921         if(y >= image->height) break;
922
923         for(; w--; tx++)
924         {
925             if(tx >= image->width) break;
926
927             m = *mask++;
928             if(tx < 0) continue;
929
930             if (m == 0xff)
931                 XPutPixel (image, tx, y, color);
932             else if (m)
933             {
934                 BYTE r, g, b;
935
936                 pixel = XGetPixel (image, tx, y);
937
938                 r = GetField(pixel, r_shift, r_len);
939                 r = ((BYTE)~m * (WORD)r + (BYTE)m * (WORD)src_r) >> 8;
940                 g = GetField(pixel, g_shift, g_len);
941                 g = ((BYTE)~m * (WORD)g + (BYTE)m * (WORD)src_g) >> 8;
942                 b = GetField(pixel, b_shift, b_len);
943                 b = ((BYTE)~m * (WORD)b + (BYTE)m * (WORD)src_b) >> 8;
944
945                 pixel = (PutField (r, r_shift, r_len) |
946                          PutField (g, g_shift, g_len) |
947                          PutField (b, b_shift, b_len));
948                 XPutPixel (image, tx, y, pixel);
949             }
950         }
951     }
952 }
953
954 static int XRenderErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
955 {
956     return 1;
957 }
958
959 /***********************************************************************
960  *   X11DRV_XRender_ExtTextOut
961  */
962 BOOL X11DRV_XRender_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
963                                 const RECT *lprect, LPCWSTR wstr, UINT count,
964                                 const INT *lpDx, INT breakExtra )
965 {
966     XRenderColor col;
967     unsigned int idx;
968     TEXTMETRICW tm;
969     RGNDATA *data;
970     SIZE sz;
971     RECT rc;
972     BOOL done_extents = FALSE;
973     INT width, xwidth, ywidth;
974     double cosEsc, sinEsc;
975     XGCValues xgcval;
976     LOGFONTW lf;
977     int render_op = PictOpOver;
978     const WORD *glyphs;
979     POINT pt;
980     gsCacheEntry *entry;
981     BOOL retv = FALSE;
982     HDC hdc = physDev->hdc;
983     int textPixel, backgroundPixel;
984     INT *deltas = NULL, char_extra;
985     HRGN saved_region = 0;
986     UINT align = GetTextAlign( hdc );
987
988     TRACE("%p, %d, %d, %08x, %p, %s, %d, %p)\n", hdc, x, y, flags,
989           lprect, debugstr_wn(wstr, count), count, lpDx);
990
991     if(flags & ETO_GLYPH_INDEX)
992         glyphs = (const WORD*)wstr;
993     else {
994         glyphs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
995         GetGlyphIndicesW(hdc, wstr, count, (WORD*)glyphs, 0);
996     }
997
998     if(lprect)
999       TRACE("rect: %ld,%ld - %ld,%ld\n", lprect->left, lprect->top, lprect->right,
1000             lprect->bottom);
1001     TRACE("align = %x bkmode = %x mapmode = %x\n", align, GetBkMode(hdc), GetMapMode(hdc));
1002
1003     if(align & TA_UPDATECP)
1004     {
1005         GetCurrentPositionEx( hdc, &pt );
1006         x = pt.x;
1007         y = pt.y;
1008     }
1009
1010     GetTextMetricsW(hdc, &tm);
1011     GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
1012
1013     if(!(tm.tmPitchAndFamily & TMPF_VECTOR)) /* Non-scalable fonts shouldn't be rotated */
1014         lf.lfEscapement = 0;
1015
1016     if(lf.lfEscapement != 0) {
1017         cosEsc = cos(lf.lfEscapement * M_PI / 1800);
1018         sinEsc = sin(lf.lfEscapement * M_PI / 1800);
1019     } else {
1020         cosEsc = 1;
1021         sinEsc = 0;
1022     }
1023
1024     if(flags & (ETO_CLIPPED | ETO_OPAQUE)) {
1025         if(!lprect) {
1026             if(flags & ETO_CLIPPED) return FALSE;
1027                 GetTextExtentPointI(hdc, glyphs, count, &sz);
1028                 done_extents = TRUE;
1029                 rc.left = x;
1030                 rc.top = y;
1031                 rc.right = x + sz.cx;
1032                 rc.bottom = y + sz.cy;
1033         } else {
1034             rc = *lprect;
1035         }
1036
1037         LPtoDP(hdc, (POINT*)&rc, 2);
1038
1039         if(rc.left > rc.right) {INT tmp = rc.left; rc.left = rc.right; rc.right = tmp;}
1040         if(rc.top > rc.bottom) {INT tmp = rc.top; rc.top = rc.bottom; rc.bottom = tmp;}
1041     }
1042
1043     xgcval.function = GXcopy;
1044     xgcval.background = physDev->backgroundPixel;
1045     xgcval.fill_style = FillSolid;
1046     wine_tsx11_lock();
1047     XChangeGC( gdi_display, physDev->gc, GCFunction | GCBackground | GCFillStyle, &xgcval );
1048     wine_tsx11_unlock();
1049
1050     X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
1051
1052     if(physDev->depth == 1) {
1053         if((physDev->textPixel & 0xffffff) == 0) {
1054             textPixel = 0;
1055             backgroundPixel = 1;
1056         } else {
1057             textPixel = 1;
1058             backgroundPixel = 0;
1059         }
1060     } else {
1061         textPixel = physDev->textPixel;
1062         backgroundPixel = physDev->backgroundPixel;
1063     }
1064
1065     if(flags & ETO_OPAQUE) {
1066         wine_tsx11_lock();
1067         XSetForeground( gdi_display, physDev->gc, backgroundPixel );
1068         XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
1069                         physDev->org.x + rc.left, physDev->org.y + rc.top,
1070                         rc.right - rc.left, rc.bottom - rc.top );
1071         wine_tsx11_unlock();
1072     }
1073
1074     if(count == 0) {
1075         retv =  TRUE;
1076         goto done;
1077     }
1078
1079     pt.x = x;
1080     pt.y = y;
1081     LPtoDP(hdc, &pt, 1);
1082     x = pt.x;
1083     y = pt.y;
1084
1085     TRACE("real x,y %d,%d\n", x, y);
1086
1087     if((char_extra = GetTextCharacterExtra(hdc)) != 0) {
1088         UINT i;
1089         SIZE tmpsz;
1090         deltas = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
1091         for(i = 0; i < count; i++) {
1092             if(lpDx)
1093                 deltas[i] = lpDx[i] + char_extra;
1094             else {
1095                 GetTextExtentPointI(hdc, glyphs + i, 1, &tmpsz);
1096                 deltas[i] = tmpsz.cx;
1097             }
1098         }
1099     } else if(lpDx)
1100         deltas = (INT*)lpDx;
1101
1102     if(deltas) {
1103         width = 0;
1104         for(idx = 0; idx < count; idx++)
1105             width += deltas[idx];
1106     } else {
1107         if(!done_extents) {
1108             GetTextExtentPointI(hdc, glyphs, count, &sz);
1109             done_extents = TRUE;
1110         }
1111         width = sz.cx;
1112     }
1113     width = X11DRV_XWStoDS(physDev, width);
1114     xwidth = width * cosEsc;
1115     ywidth = width * sinEsc;
1116
1117     tm.tmAscent = abs(X11DRV_YWStoDS(physDev, tm.tmAscent));
1118     tm.tmDescent = abs(X11DRV_YWStoDS(physDev, tm.tmDescent));
1119     switch( align & (TA_LEFT | TA_RIGHT | TA_CENTER) ) {
1120     case TA_LEFT:
1121         if (align & TA_UPDATECP) {
1122             pt.x = x + xwidth;
1123             pt.y = y - ywidth;
1124             DPtoLP(hdc, &pt, 1);
1125             MoveToEx(hdc, pt.x, pt.y, NULL);
1126         }
1127         break;
1128
1129     case TA_CENTER:
1130         x -= xwidth / 2;
1131         y += ywidth / 2;
1132         break;
1133
1134     case TA_RIGHT:
1135         x -= xwidth;
1136         y += ywidth;
1137         if (align & TA_UPDATECP) {
1138             pt.x = x;
1139             pt.y = y;
1140             DPtoLP(hdc, &pt, 1);
1141             MoveToEx(hdc, pt.x, pt.y, NULL);
1142         }
1143         break;
1144     }
1145
1146     switch( align & (TA_TOP | TA_BOTTOM | TA_BASELINE) ) {
1147     case TA_TOP:
1148         y += tm.tmAscent * cosEsc;
1149         x += tm.tmAscent * sinEsc;
1150         break;
1151
1152     case TA_BOTTOM:
1153         y -= tm.tmDescent * cosEsc;
1154         x -= tm.tmDescent * sinEsc;
1155         break;
1156
1157     case TA_BASELINE:
1158         break;
1159     }
1160
1161     if (flags & ETO_CLIPPED)
1162     {
1163         HRGN clip_region;
1164         RECT clip_rect = *lprect;
1165
1166         LPtoDP( hdc, (POINT *)&clip_rect, 2 );
1167         clip_region = CreateRectRgnIndirect( &clip_rect );
1168         /* make a copy of the current device region */
1169         saved_region = CreateRectRgn( 0, 0, 0, 0 );
1170         CombineRgn( saved_region, physDev->region, 0, RGN_COPY );
1171         X11DRV_SetDeviceClipping( physDev, saved_region, clip_region );
1172         DeleteObject( clip_region );
1173     }
1174
1175     if(X11DRV_XRender_Installed) {
1176         if(!physDev->xrender->pict) {
1177             XRenderPictureAttributes pa;
1178             pa.subwindow_mode = IncludeInferiors;
1179
1180             wine_tsx11_lock();
1181             physDev->xrender->pict = pXRenderCreatePicture(gdi_display,
1182                                                            physDev->drawable,
1183                                                            (physDev->depth == 1) ?
1184                                                            mono_format : screen_format,
1185                                                            CPSubwindowMode, &pa);
1186             wine_tsx11_unlock();
1187
1188             TRACE("allocing pict = %lx dc = %p drawable = %08lx\n",
1189                   physDev->xrender->pict, hdc, physDev->drawable);
1190         } else {
1191             TRACE("using existing pict = %lx dc = %p drawable = %08lx\n",
1192                   physDev->xrender->pict, hdc, physDev->drawable);
1193         }
1194
1195         if ((data = X11DRV_GetRegionData( physDev->region, 0 )))
1196         {
1197             wine_tsx11_lock();
1198             pXRenderSetPictureClipRectangles( gdi_display, physDev->xrender->pict,
1199                                               physDev->org.x, physDev->org.y,
1200                                               (XRectangle *)data->Buffer, data->rdh.nCount );
1201             wine_tsx11_unlock();
1202             HeapFree( GetProcessHeap(), 0, data );
1203         }
1204     }
1205
1206     if(GetBkMode(hdc) != TRANSPARENT) {
1207         if(!((flags & ETO_CLIPPED) && (flags & ETO_OPAQUE))) {
1208             if(!(flags & ETO_OPAQUE) || x < rc.left || x + width >= rc.right ||
1209                y - tm.tmAscent < rc.top || y + tm.tmDescent >= rc.bottom) {
1210                 wine_tsx11_lock();
1211                 XSetForeground( gdi_display, physDev->gc, backgroundPixel );
1212                 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
1213                                 physDev->org.x + x, physDev->org.y + y - tm.tmAscent,
1214                                 width, tm.tmAscent + tm.tmDescent );
1215                 wine_tsx11_unlock();
1216             }
1217         }
1218     }
1219
1220     if(X11DRV_XRender_Installed) {
1221         /* Create a 1x1 pixmap to tile over the font mask */
1222         if(!physDev->xrender->tile_xpm) {
1223             XRenderPictureAttributes pa;
1224
1225             XRenderPictFormat *format = (physDev->depth == 1) ? mono_format : screen_format;
1226             wine_tsx11_lock();
1227             physDev->xrender->tile_xpm = XCreatePixmap(gdi_display,
1228                                                        physDev->drawable,
1229                                                        1, 1,
1230                                                        format->depth);
1231             pa.repeat = True;
1232             physDev->xrender->tile_pict = pXRenderCreatePicture(gdi_display,
1233                                                                 physDev->xrender->tile_xpm,
1234                                                                 format,
1235                                                                 CPRepeat, &pa);
1236             wine_tsx11_unlock();
1237             TRACE("Created pixmap of depth %d\n", format->depth);
1238             /* init lastTextColor to something different from textPixel */
1239             physDev->xrender->lastTextColor = ~physDev->textPixel;
1240
1241         }
1242
1243         if(physDev->textPixel != physDev->xrender->lastTextColor) {
1244             if(physDev->depth != 1) {
1245                 /* Map 0 -- 0xff onto 0 -- 0xffff */
1246                 int             r_shift, r_len;
1247                 int             g_shift, g_len;
1248                 int             b_shift, b_len;
1249
1250                 ExamineBitfield (visual->red_mask, &r_shift, &r_len );
1251                 ExamineBitfield (visual->green_mask, &g_shift, &g_len);
1252                 ExamineBitfield (visual->blue_mask, &b_shift, &b_len);
1253
1254                 col.red = GetField(physDev->textPixel, r_shift, r_len);
1255                 col.red |= col.red << 8;
1256                 col.green = GetField(physDev->textPixel, g_shift, g_len);
1257                 col.green |= col.green << 8;
1258                 col.blue = GetField(physDev->textPixel, b_shift, b_len);
1259                 col.blue |= col.blue << 8;
1260                 col.alpha = 0x0;
1261             } else { /* for a 1bpp bitmap we always need a 1 in the tile */
1262                 col.red = col.green = col.blue = 0;
1263                 col.alpha = 0xffff;
1264             }
1265             wine_tsx11_lock();
1266             pXRenderFillRectangle(gdi_display, PictOpSrc,
1267                                   physDev->xrender->tile_pict,
1268                                   &col, 0, 0, 1, 1);
1269             wine_tsx11_unlock();
1270             physDev->xrender->lastTextColor = physDev->textPixel;
1271         }
1272
1273         /* FIXME the mapping of Text/BkColor onto 1 or 0 needs investigation.
1274          */
1275         if((physDev->depth == 1) && (textPixel == 0))
1276             render_op = PictOpOutReverse; /* This gives us 'black' text */
1277     }
1278
1279     EnterCriticalSection(&xrender_cs);
1280     entry = glyphsetCache + physDev->xrender->cache_index;
1281
1282     for(idx = 0; idx < count; idx++) {
1283         if(glyphs[idx] >= entry->nrealized || entry->realized[glyphs[idx]] == FALSE) {
1284             UploadGlyph(physDev, glyphs[idx]);
1285         }
1286     }
1287
1288
1289     TRACE("Writing %s at %ld,%ld\n", debugstr_wn(wstr,count),
1290           physDev->org.x + x, physDev->org.y + y);
1291
1292     if(X11DRV_XRender_Installed) {
1293         wine_tsx11_lock();
1294         if(!deltas)
1295             pXRenderCompositeString16(gdi_display, render_op,
1296                                       physDev->xrender->tile_pict,
1297                                       physDev->xrender->pict,
1298                                       entry->font_format, entry->glyphset,
1299                                       0, 0, physDev->org.x + x, physDev->org.y + y,
1300                                       glyphs, count);
1301
1302         else {
1303             INT offset = 0, xoff = 0, yoff = 0;
1304             for(idx = 0; idx < count; idx++) {
1305                 pXRenderCompositeString16(gdi_display, render_op,
1306                                           physDev->xrender->tile_pict,
1307                                           physDev->xrender->pict,
1308                                           entry->font_format, entry->glyphset,
1309                                           0, 0, physDev->org.x + x + xoff,
1310                                           physDev->org.y + y + yoff,
1311                                           glyphs + idx, 1);
1312                 offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1313                 xoff = offset * cosEsc;
1314                 yoff = offset * -sinEsc;
1315             }
1316         }
1317         wine_tsx11_unlock();
1318
1319     } else {
1320         INT offset = 0, xoff = 0, yoff = 0;
1321         wine_tsx11_lock();
1322         XSetForeground( gdi_display, physDev->gc, textPixel );
1323
1324         if(entry->aa == AA_None) {
1325             for(idx = 0; idx < count; idx++) {
1326                 SharpGlyphMono(physDev, physDev->org.x + x + xoff,
1327                                physDev->org.y + y + yoff,
1328                                entry->bitmaps[glyphs[idx]],
1329                                &entry->gis[glyphs[idx]]);
1330                 if(deltas) {
1331                     offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1332                     xoff = offset * cosEsc;
1333                     yoff = offset * -sinEsc;
1334
1335                 } else {
1336                     xoff += entry->gis[glyphs[idx]].xOff;
1337                     yoff += entry->gis[glyphs[idx]].yOff;
1338                 }
1339             }
1340         } else if(physDev->depth == 1) {
1341             for(idx = 0; idx < count; idx++) {
1342                 SharpGlyphGray(physDev, physDev->org.x + x + xoff,
1343                                physDev->org.y + y + yoff,
1344                                entry->bitmaps[glyphs[idx]],
1345                                &entry->gis[glyphs[idx]]);
1346                 if(deltas) {
1347                     offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1348                     xoff = offset * cosEsc;
1349                     yoff = offset * -sinEsc;
1350
1351                 } else {
1352                     xoff += entry->gis[glyphs[idx]].xOff;
1353                     yoff += entry->gis[glyphs[idx]].yOff;
1354                 }
1355                     
1356             }
1357         } else {
1358             XImage *image;
1359             unsigned int w, h, dummy_uint;
1360             Window dummy_window;
1361             int dummy_int;
1362             int image_x, image_y, image_off_x, image_off_y, image_w, image_h;
1363             RECT extents = {0, 0, 0, 0};
1364             POINT cur = {0, 0};
1365             
1366             
1367             XGetGeometry(gdi_display, physDev->drawable, &dummy_window, &dummy_int, &dummy_int,
1368                          &w, &h, &dummy_uint, &dummy_uint);
1369             TRACE("drawable %dx%d\n", w, h);
1370
1371             for(idx = 0; idx < count; idx++) {
1372                 if(extents.left > cur.x - entry->gis[glyphs[idx]].x)
1373                     extents.left = cur.x - entry->gis[glyphs[idx]].x;
1374                 if(extents.top > cur.y - entry->gis[glyphs[idx]].y)
1375                     extents.top = cur.y - entry->gis[glyphs[idx]].y;
1376                 if(extents.right < cur.x - entry->gis[glyphs[idx]].x + entry->gis[glyphs[idx]].width)
1377                     extents.right = cur.x - entry->gis[glyphs[idx]].x + entry->gis[glyphs[idx]].width;
1378                 if(extents.bottom < cur.y - entry->gis[glyphs[idx]].y + entry->gis[glyphs[idx]].height)
1379                     extents.bottom = cur.y - entry->gis[glyphs[idx]].y + entry->gis[glyphs[idx]].height;
1380                 if(deltas) {
1381                     offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1382                     cur.x = offset * cosEsc;
1383                     cur.y = offset * -sinEsc;
1384                 } else {
1385                     cur.x += entry->gis[glyphs[idx]].xOff;
1386                     cur.y += entry->gis[glyphs[idx]].yOff;
1387                 }
1388             }
1389             TRACE("glyph extents %ld,%ld - %ld,%ld drawable x,y %ld,%ld\n", extents.left, extents.top,
1390                   extents.right, extents.bottom, physDev->org.x + x, physDev->org.y + y);
1391
1392             if(physDev->org.x + x + extents.left >= 0) {
1393                 image_x = physDev->org.x + x + extents.left;
1394                 image_off_x = 0;
1395             } else {
1396                 image_x = 0;
1397                 image_off_x = physDev->org.x + x + extents.left;
1398             }
1399             if(physDev->org.y + y + extents.top >= 0) {
1400                 image_y = physDev->org.y + y + extents.top;
1401                 image_off_y = 0;
1402             } else {
1403                 image_y = 0;
1404                 image_off_y = physDev->org.y + y + extents.top;
1405             }
1406             if(physDev->org.x + x + extents.right < w)
1407                 image_w = physDev->org.x + x + extents.right - image_x;
1408             else
1409                 image_w = w - image_x;
1410             if(physDev->org.y + y + extents.bottom < h)
1411                 image_h = physDev->org.y + y + extents.bottom - image_y;
1412             else
1413                 image_h = h - image_y;
1414
1415             if(image_w <= 0 || image_h <= 0) goto no_image;
1416
1417             X11DRV_expect_error(gdi_display, XRenderErrorHandler, NULL);
1418             image = XGetImage(gdi_display, physDev->drawable,
1419                               image_x, image_y, image_w, image_h,
1420                               AllPlanes, ZPixmap);
1421             X11DRV_check_error();
1422
1423             TRACE("XGetImage(%p, %x, %d, %d, %d, %d, %lx, %x) depth = %d rets %p\n",
1424                   gdi_display, (int)physDev->drawable, image_x, image_y,
1425                   image_w, image_h, AllPlanes, ZPixmap,
1426                   physDev->depth, image);
1427             if(!image) {
1428                 Pixmap xpm = XCreatePixmap(gdi_display, physDev->drawable, image_w, image_h,
1429                                            physDev->depth);
1430                 GC gc;
1431                 XGCValues gcv;
1432
1433                 gcv.graphics_exposures = False;
1434                 gc = XCreateGC(gdi_display, xpm, GCGraphicsExposures, &gcv);
1435                 XCopyArea(gdi_display, physDev->drawable, xpm, gc, image_x, image_y,
1436                           image_w, image_h, 0, 0);
1437                 XFreeGC(gdi_display, gc);
1438                 X11DRV_expect_error(gdi_display, XRenderErrorHandler, NULL);
1439                 image = XGetImage(gdi_display, xpm, 0, 0, image_w, image_h, AllPlanes,
1440                                   ZPixmap);
1441                 X11DRV_check_error();
1442                 XFreePixmap(gdi_display, xpm);
1443             }
1444             if(!image) goto no_image;
1445
1446             image->red_mask = visual->red_mask;
1447             image->green_mask = visual->green_mask;
1448             image->blue_mask = visual->blue_mask;
1449
1450             offset = xoff = yoff = 0;
1451             for(idx = 0; idx < count; idx++) {
1452                 SmoothGlyphGray(image, xoff + image_off_x - extents.left,
1453                                 yoff + image_off_y - extents.top,
1454                                 entry->bitmaps[glyphs[idx]],
1455                                 &entry->gis[glyphs[idx]],
1456                                 physDev->textPixel);
1457                 if(deltas) {
1458                     offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1459                     xoff = offset * cosEsc;
1460                     yoff = offset * -sinEsc;
1461                 } else {
1462                     xoff += entry->gis[glyphs[idx]].xOff;
1463                     yoff += entry->gis[glyphs[idx]].yOff;
1464                 }
1465                     
1466             }
1467             XPutImage(gdi_display, physDev->drawable, physDev->gc, image, 0, 0,
1468                       image_x, image_y, image_w, image_h);
1469             XDestroyImage(image);
1470         }
1471     no_image:
1472         wine_tsx11_unlock();
1473     }
1474     LeaveCriticalSection(&xrender_cs);
1475
1476     if (lf.lfUnderline || lf.lfStrikeOut) {
1477         int underlinePos, strikeoutPos;
1478         int underlineWidth, strikeoutWidth;
1479         UINT nMetricsSize = GetOutlineTextMetricsW(hdc, 0, NULL);
1480         OUTLINETEXTMETRICW* otm = NULL;
1481
1482         if(!nMetricsSize) {
1483             underlinePos = 0;
1484             underlineWidth = tm.tmAscent / 20 + 1;
1485             strikeoutPos = tm.tmAscent / 2;
1486             strikeoutWidth = underlineWidth;
1487         } else {
1488             otm = HeapAlloc(GetProcessHeap(), 0, nMetricsSize);
1489             if (!otm) goto done;
1490
1491             GetOutlineTextMetricsW(hdc, nMetricsSize, otm);
1492             underlinePos = otm->otmsUnderscorePosition;
1493             underlineWidth = otm->otmsUnderscoreSize;
1494             strikeoutPos = otm->otmsStrikeoutPosition;
1495             strikeoutWidth = otm->otmsStrikeoutSize;
1496         }
1497
1498         wine_tsx11_lock();
1499         XSetForeground( gdi_display, physDev->gc, physDev->textPixel );
1500
1501         if (lf.lfUnderline) {
1502             underlinePos = X11DRV_YWStoDS(physDev, underlinePos);
1503             underlineWidth = X11DRV_YWStoDS(physDev, underlineWidth);
1504
1505             XSetLineAttributes( gdi_display, physDev->gc, underlineWidth,
1506                                 LineSolid, CapProjecting, JoinBevel );
1507             XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1508                        physDev->org.x + x - underlinePos * sinEsc,
1509                        physDev->org.y + y - underlinePos * cosEsc,
1510                        physDev->org.x + x + width * cosEsc - underlinePos * sinEsc,
1511                        physDev->org.y + y - width * sinEsc - underlinePos * cosEsc );
1512         }
1513
1514         if (lf.lfStrikeOut) { 
1515             strikeoutPos = X11DRV_YWStoDS(physDev, strikeoutPos);
1516             strikeoutWidth = X11DRV_YWStoDS(physDev, strikeoutWidth);
1517             
1518             XSetLineAttributes( gdi_display, physDev->gc, strikeoutWidth,
1519                                 LineSolid, CapProjecting, JoinBevel );
1520             XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1521                        physDev->org.x + x - strikeoutPos * sinEsc,
1522                        physDev->org.y + y - strikeoutPos * cosEsc,
1523                        physDev->org.x + x + width * cosEsc - strikeoutPos * sinEsc,
1524                        physDev->org.y + y - width * sinEsc - strikeoutPos * cosEsc);
1525         }
1526         wine_tsx11_unlock();
1527         if(otm)
1528             HeapFree(GetProcessHeap(), 0, otm);
1529     }
1530
1531     if(deltas && deltas != lpDx)
1532         HeapFree(GetProcessHeap(), 0, deltas);
1533
1534     if (flags & ETO_CLIPPED)
1535     {
1536         /* restore the device region */
1537         X11DRV_SetDeviceClipping( physDev, saved_region, 0 );
1538         DeleteObject( saved_region );
1539     }
1540
1541     retv = TRUE;
1542
1543 done:
1544     X11DRV_UnlockDIBSection( physDev, TRUE );
1545     if(glyphs != wstr) HeapFree(GetProcessHeap(), 0, (WORD*)glyphs);
1546     return retv;
1547 }
1548
1549 BOOL X11DRV_AlphaBlend(X11DRV_PDEVICE *devDst, INT xDst, INT yDst, INT widthDst, INT heightDst,
1550                        X11DRV_PDEVICE *devSrc, INT xSrc, INT ySrc, INT widthSrc, INT heightSrc,
1551                        BLENDFUNCTION blendfn)
1552 {
1553     XRenderPictureAttributes pa;
1554     XRenderPictFormat *src_format;
1555     Picture dst_pict, src_pict;
1556     Pixmap xpm;
1557     HBITMAP hBitmap;
1558     BITMAPOBJ *bmp;
1559     XImage *image;
1560     GC gc;
1561     XGCValues gcv;
1562     char *dstbits, *data;
1563     int y;
1564     POINT pts[2];
1565     BOOL top_down = FALSE;
1566
1567     if(!X11DRV_XRender_Installed) {
1568         FIXME("Unable to AlphaBlend without Xrender\n");
1569         return FALSE;
1570     }
1571     pts[0].x = xDst;
1572     pts[0].y = yDst;
1573     pts[1].x = xDst + widthDst;
1574     pts[1].y = yDst + heightDst;
1575     LPtoDP(devDst->hdc, pts, 2);
1576     xDst      = pts[0].x;
1577     yDst      = pts[0].y;
1578     widthDst  = pts[1].x - pts[0].x;
1579     heightDst = pts[1].y - pts[0].y;
1580
1581     pts[0].x = xSrc;
1582     pts[0].y = ySrc;
1583     pts[1].x = xSrc + widthSrc;
1584     pts[1].y = ySrc + heightSrc;
1585     LPtoDP(devSrc->hdc, pts, 2);
1586     xSrc      = pts[0].x;
1587     ySrc      = pts[0].y;
1588     widthSrc  = pts[1].x - pts[0].x;
1589     heightSrc = pts[1].y - pts[0].y;
1590
1591 #ifndef HAVE_XRENDERSETPICTURETRANSFORM
1592     if(widthDst != widthSrc || heightDst != heightSrc)
1593 #else
1594     if(!pXRenderSetPictureTransform)
1595 #endif
1596     {
1597         FIXME("Unable to Stretch, XRenderSetPictureTransform is currently required\n");
1598         return FALSE;
1599     }
1600
1601     hBitmap = GetCurrentObject( devSrc->hdc, OBJ_BITMAP );
1602     bmp = (BITMAPOBJ *)GDI_GetObjPtr( hBitmap, BITMAP_MAGIC );
1603     if(!bmp || !bmp->dib) {
1604         FIXME("not a dibsection\n");
1605         GDI_ReleaseObj( hBitmap );
1606         return FALSE;
1607     }
1608
1609     if(bmp->dib->dsBm.bmBitsPixel != 32) {
1610         FIXME("not a 32 bpp dibsection\n");
1611         GDI_ReleaseObj( hBitmap );
1612         return FALSE;
1613     }
1614     dstbits = data = HeapAlloc(GetProcessHeap(), 0, heightSrc * widthSrc * 4);
1615
1616     if(bmp->dib->dsBmih.biHeight < 0) { /* top-down dib */
1617         top_down = TRUE;
1618         dstbits += widthSrc * (heightSrc - 1) * 4;
1619     }
1620     for(y = ySrc + heightSrc - 1; y >= ySrc; y--) {
1621         memcpy(dstbits, (char *)bmp->dib->dsBm.bmBits + y * bmp->dib->dsBm.bmWidthBytes + xSrc * 4,
1622                widthSrc * 4);
1623         dstbits += (top_down ? -1 : 1) * widthSrc * 4;
1624     }
1625
1626     wine_tsx11_lock();
1627     image = XCreateImage(gdi_display, visual, 32, ZPixmap, 0,
1628                          data, widthSrc, heightSrc, 32, widthSrc * 4);
1629
1630     src_format = pXRenderFindStandardFormat(gdi_display, PictStandardARGB32);
1631
1632
1633     TRACE("src_format %p\n", src_format);
1634
1635     pa.subwindow_mode = IncludeInferiors;
1636
1637     /* FIXME use devDst->xrender->pict ? */
1638     dst_pict = pXRenderCreatePicture(gdi_display,
1639                                      devDst->drawable,
1640                                      (devDst->depth == 1) ?
1641                                      mono_format : screen_format,
1642                                      CPSubwindowMode, &pa);
1643     TRACE("dst_pict %08lx\n", dst_pict);
1644     TRACE("src_drawable = %08lx\n", devSrc->drawable);
1645     xpm = XCreatePixmap(gdi_display,
1646                         devSrc->drawable,
1647                         widthSrc, heightSrc, 32);
1648     gcv.graphics_exposures = False;
1649     gc = XCreateGC(gdi_display, xpm, GCGraphicsExposures, &gcv);
1650     TRACE("xpm = %08lx\n", xpm);
1651     XPutImage(gdi_display, xpm, gc, image, 0, 0, 0, 0, widthSrc, heightSrc);
1652
1653     src_pict = pXRenderCreatePicture(gdi_display,
1654                                      xpm, src_format,
1655                                      CPSubwindowMode, &pa);
1656     TRACE("src_pict %08lx\n", src_pict);
1657
1658 #ifdef HAVE_XRENDERSETPICTURETRANSFORM
1659     if(widthDst != widthSrc || heightDst != heightSrc) {
1660         double xscale = widthSrc/(double)widthDst;
1661         double yscale = heightSrc/(double)heightDst;
1662         XTransform xform = {{
1663             { XDoubleToFixed(xscale), XDoubleToFixed(0), XDoubleToFixed(0) },
1664             { XDoubleToFixed(0), XDoubleToFixed(yscale), XDoubleToFixed(0) },
1665             { XDoubleToFixed(0), XDoubleToFixed(0), XDoubleToFixed(1) }
1666         }};
1667         pXRenderSetPictureTransform(gdi_display, src_pict, &xform);
1668     }
1669 #endif
1670     pXRenderComposite(gdi_display, PictOpOver, src_pict, 0, dst_pict,
1671                       xSrc, ySrc, 0, 0,
1672                       xDst + devDst->org.x, yDst + devDst->org.y, widthDst, heightDst);
1673
1674
1675     pXRenderFreePicture(gdi_display, src_pict);
1676     XFreePixmap(gdi_display, xpm);
1677     XFreeGC(gdi_display, gc);
1678     pXRenderFreePicture(gdi_display, dst_pict);
1679     image->data = NULL;
1680     XDestroyImage(image);
1681
1682     wine_tsx11_unlock();
1683     HeapFree(GetProcessHeap(), 0, data);
1684     GDI_ReleaseObj( hBitmap );
1685     return TRUE;
1686 }
1687
1688 #else /* HAVE_X11_EXTENSIONS_XRENDER_H */
1689
1690 void X11DRV_XRender_Init(void)
1691 {
1692     TRACE("XRender support not compiled in.\n");
1693     return;
1694 }
1695
1696 void X11DRV_XRender_Finalize(void)
1697 {
1698 }
1699
1700 BOOL X11DRV_XRender_SelectFont(X11DRV_PDEVICE *physDev, HFONT hfont)
1701 {
1702   assert(0);
1703   return FALSE;
1704 }
1705
1706 void X11DRV_XRender_DeleteDC(X11DRV_PDEVICE *physDev)
1707 {
1708   assert(0);
1709   return;
1710 }
1711
1712 BOOL X11DRV_XRender_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
1713                                 const RECT *lprect, LPCWSTR wstr, UINT count,
1714                                 const INT *lpDx, INT breakExtra )
1715 {
1716   assert(0);
1717   return FALSE;
1718 }
1719
1720 void X11DRV_XRender_UpdateDrawable(X11DRV_PDEVICE *physDev)
1721 {
1722   assert(0);
1723   return;
1724 }
1725
1726 BOOL X11DRV_AlphaBlend(X11DRV_PDEVICE *devDst, INT xDst, INT yDst, INT widthDst, INT heightDst,
1727                        X11DRV_PDEVICE *devSrc, INT xSrc, INT ySrc, INT widthSrc, INT heightSrc,
1728                        BLENDFUNCTION blendfn)
1729 {
1730   FIXME("not supported - XRENDER headers were missing at compile time\n");
1731   return FALSE;
1732 }
1733
1734 #endif /* HAVE_X11_EXTENSIONS_XRENDER_H */