Implement SHCreateStreamOnFileA/W/Ex, ordinals @166,184,212-214.
[wine] / dlls / x11drv / xrender.c
1 /*
2  * Functions to use the XRender extension
3  *
4  * Copyright 2001 Huw D M Davies for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include "config.h"
21 #include "wine/port.h"
22
23 #include <assert.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include "winnt.h"
28 #include "x11drv.h"
29 #include "bitmap.h"
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
32
33 BOOL X11DRV_XRender_Installed = FALSE;
34 WINE_DEFAULT_DEBUG_CHANNEL(xrender);
35
36 #ifdef HAVE_X11_EXTENSIONS_XRENDER_H
37
38 #include "ts_xlib.h"
39 #include <X11/extensions/Xrender.h>
40
41 static XRenderPictFormat *screen_format; /* format of screen */
42 static XRenderPictFormat *mono_format; /* format of mono bitmap */
43
44 typedef struct
45 {
46   LOGFONTW lf;
47   XFORM  xform; /* this is dum as we don't care about offsets */
48   DWORD hash;
49 } LFANDSIZE;
50
51 #define INITIAL_REALIZED_BUF_SIZE 128
52
53
54 typedef struct
55 {
56   LFANDSIZE lfsz;
57   GlyphSet glyphset;
58   XRenderPictFormat *font_format;
59   int nrealized;
60   BOOL *realized;
61   UINT count;
62   INT next;
63 } gsCacheEntry;
64
65 struct tagXRENDERINFO
66 {
67     int                cache_index;
68     Picture            pict;
69     Picture            tile_pict;
70     Pixmap             tile_xpm;
71     COLORREF           lastTextColor;
72 };
73
74
75 static gsCacheEntry *glyphsetCache = NULL;
76 static DWORD glyphsetCacheSize = 0;
77 static INT lastfree = -1;
78 static INT mru = -1;
79
80 #define INIT_CACHE_SIZE 10
81
82 static int antialias = 1;
83
84 /* some default values just in case */
85 #ifndef SONAME_LIBX11
86 #define SONAME_LIBX11 "libX11.so"
87 #endif
88 #ifndef SONAME_LIBXEXT
89 #define SONAME_LIBXEXT "libXext.so"
90 #endif
91 #ifndef SONAME_LIBXRENDER
92 #define SONAME_LIBXRENDER "libXrender.so"
93 #endif
94
95 static void *xrender_handle;
96
97 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
98 MAKE_FUNCPTR(XRenderAddGlyphs)
99 MAKE_FUNCPTR(XRenderCompositeString8)
100 MAKE_FUNCPTR(XRenderCompositeString16)
101 MAKE_FUNCPTR(XRenderCompositeString32)
102 MAKE_FUNCPTR(XRenderCreateGlyphSet)
103 MAKE_FUNCPTR(XRenderCreatePicture)
104 MAKE_FUNCPTR(XRenderFillRectangle)
105 MAKE_FUNCPTR(XRenderFindFormat)
106 MAKE_FUNCPTR(XRenderFindVisualFormat)
107 MAKE_FUNCPTR(XRenderFreeGlyphSet)
108 MAKE_FUNCPTR(XRenderFreePicture)
109 MAKE_FUNCPTR(XRenderSetPictureClipRectangles)
110 MAKE_FUNCPTR(XRenderQueryExtension)
111 #undef MAKE_FUNCPTR
112
113 static CRITICAL_SECTION xrender_cs = CRITICAL_SECTION_INIT("xrender_cs");
114
115
116 /***********************************************************************
117  *   X11DRV_XRender_Init
118  *
119  * Let's see if our XServer has the extension available
120  *
121  */
122 void X11DRV_XRender_Init(void)
123 {
124     int error_base, event_base, i;
125     XRenderPictFormat pf;
126
127     if (!wine_dlopen(SONAME_LIBX11, RTLD_NOW|RTLD_GLOBAL, NULL, 0)) return;
128     if (!wine_dlopen(SONAME_LIBXEXT, RTLD_NOW|RTLD_GLOBAL, NULL, 0)) return;
129     xrender_handle = wine_dlopen(SONAME_LIBXRENDER, RTLD_NOW, NULL, 0);
130     if(!xrender_handle) return;
131
132 #define LOAD_FUNCPTR(f) if((p##f = wine_dlsym(xrender_handle, #f, NULL, 0)) == NULL) goto sym_not_found;
133 LOAD_FUNCPTR(XRenderAddGlyphs)
134 LOAD_FUNCPTR(XRenderCompositeString8)
135 LOAD_FUNCPTR(XRenderCompositeString16)
136 LOAD_FUNCPTR(XRenderCompositeString32)
137 LOAD_FUNCPTR(XRenderCreateGlyphSet)
138 LOAD_FUNCPTR(XRenderCreatePicture)
139 LOAD_FUNCPTR(XRenderFillRectangle)
140 LOAD_FUNCPTR(XRenderFindFormat)
141 LOAD_FUNCPTR(XRenderFindVisualFormat)
142 LOAD_FUNCPTR(XRenderFreeGlyphSet)
143 LOAD_FUNCPTR(XRenderFreePicture)
144 LOAD_FUNCPTR(XRenderSetPictureClipRectangles)
145 LOAD_FUNCPTR(XRenderQueryExtension)
146 #undef LOAD_FUNCPTR
147
148     wine_tsx11_lock();
149     if(pXRenderQueryExtension(gdi_display, &event_base, &error_base)) {
150         X11DRV_XRender_Installed = TRUE;
151         TRACE("Xrender is up and running error_base = %d\n", error_base);
152         screen_format = pXRenderFindVisualFormat(gdi_display, visual);
153         if(!screen_format) { /* This fails in buggy versions of libXrender.so */
154             wine_tsx11_unlock();
155             WINE_MESSAGE(
156              "Wine has detected that you probably have a buggy version\n"
157              "of libXrender.so .  Because of this client side font rendering\n"
158              "will be disabled.  Please upgrade this library.\n");
159             X11DRV_XRender_Installed = FALSE;
160             return;
161         }
162         pf.type = PictTypeDirect;
163         pf.depth = 1;
164         pf.direct.alpha = 0;
165         pf.direct.alphaMask = 1;
166         mono_format = pXRenderFindFormat(gdi_display, PictFormatType |
167                                           PictFormatDepth | PictFormatAlpha |
168                                           PictFormatAlphaMask, &pf, 0);
169         if(!mono_format) {
170             wine_tsx11_unlock();
171             ERR("mono_format == NULL?\n");
172             X11DRV_XRender_Installed = FALSE;
173             return;
174         }
175         glyphsetCache = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
176                                   sizeof(*glyphsetCache) * INIT_CACHE_SIZE);
177
178         glyphsetCacheSize = INIT_CACHE_SIZE;
179         lastfree = 0;
180         for(i = 0; i < INIT_CACHE_SIZE; i++) {
181           glyphsetCache[i].next = i + 1;
182           glyphsetCache[i].count = -1;
183         }
184         glyphsetCache[i-1].next = -1;
185     } else {
186         TRACE("Xrender is not available on this server\n");
187     }
188     wine_tsx11_unlock();
189     return;
190
191 sym_not_found:
192     wine_dlclose(xrender_handle, NULL, 0);
193     xrender_handle = NULL;
194 }
195
196 static BOOL fontcmp(LFANDSIZE *p1, LFANDSIZE *p2)
197 {
198   if(p1->hash != p2->hash) return TRUE;
199   if(memcmp(&p1->xform, &p2->xform, sizeof(p1->xform))) return TRUE;
200   if(memcmp(&p1->lf, &p2->lf, offsetof(LOGFONTW, lfFaceName))) return TRUE;
201   return strcmpW(p1->lf.lfFaceName, p2->lf.lfFaceName);
202 }
203
204 #if 0
205 static void walk_cache(void)
206 {
207   int i;
208
209   EnterCriticalSection(&xrender_cs);
210   for(i=mru; i >= 0; i = glyphsetCache[i].next)
211     TRACE("item %d\n", i);
212   LeaveCriticalSection(&xrender_cs);
213 }
214 #endif
215
216 static int LookupEntry(LFANDSIZE *plfsz)
217 {
218   int i, prev_i = -1;
219
220   for(i = mru; i >= 0; i = glyphsetCache[i].next) {
221     TRACE("%d\n", i);
222     if(glyphsetCache[i].count == -1) { /* reached free list so stop */
223       i = -1;
224       break;
225     }
226
227     if(!fontcmp(&glyphsetCache[i].lfsz, plfsz)) {
228       glyphsetCache[i].count++;
229       if(prev_i >= 0) {
230         glyphsetCache[prev_i].next = glyphsetCache[i].next;
231         glyphsetCache[i].next = mru;
232         mru = i;
233       }
234       TRACE("found font in cache %d\n", i);
235       return i;
236     }
237     prev_i = i;
238   }
239   TRACE("font not in cache\n");
240   return -1;
241 }
242
243 static int AllocEntry(void)
244 {
245   int best = -1, prev_best = -1, i, prev_i = -1;
246
247   if(lastfree >= 0) {
248     assert(glyphsetCache[lastfree].count == -1);
249     glyphsetCache[lastfree].count = 1;
250     best = lastfree;
251     lastfree = glyphsetCache[lastfree].next;
252     assert(best != mru);
253     glyphsetCache[best].next = mru;
254     mru = best;
255
256     TRACE("empty space at %d, next lastfree = %d\n", mru, lastfree);
257     return mru;
258   }
259
260   for(i = mru; i >= 0; i = glyphsetCache[i].next) {
261     if(glyphsetCache[i].count == 0) {
262       best = i;
263       prev_best = prev_i;
264     }
265     prev_i = i;
266   }
267
268   if(best >= 0) {
269     TRACE("freeing unused glyphset at cache %d\n", best);
270     wine_tsx11_lock();
271     pXRenderFreeGlyphSet(gdi_display, glyphsetCache[best].glyphset);
272     wine_tsx11_unlock();
273     glyphsetCache[best].glyphset = 0;
274     if(glyphsetCache[best].nrealized) { /* do we really want to do this? */
275       HeapFree(GetProcessHeap(), 0, glyphsetCache[best].realized);
276       glyphsetCache[best].realized = NULL;
277       glyphsetCache[best].nrealized = 0;
278     }
279     glyphsetCache[best].count = 1;
280     if(prev_best >= 0) {
281       glyphsetCache[prev_best].next = glyphsetCache[best].next;
282       glyphsetCache[best].next = mru;
283       mru = best;
284     } else {
285       assert(mru == best);
286     }
287     return mru;
288   }
289
290   TRACE("Growing cache\n");
291   glyphsetCache = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
292                               glyphsetCache,
293                               (glyphsetCacheSize + INIT_CACHE_SIZE)
294                               * sizeof(*glyphsetCache));
295   for(best = i = glyphsetCacheSize; i < glyphsetCacheSize + INIT_CACHE_SIZE;
296       i++) {
297     glyphsetCache[i].next = i + 1;
298     glyphsetCache[i].count = -1;
299   }
300   glyphsetCache[i-1].next = -1;
301   glyphsetCacheSize += INIT_CACHE_SIZE;
302
303   lastfree = glyphsetCache[best].next;
304   glyphsetCache[best].count = 1;
305   glyphsetCache[best].next = mru;
306   mru = best;
307   TRACE("new free cache slot at %d\n", mru);
308   return mru;
309 }
310
311 static int GetCacheEntry(LFANDSIZE *plfsz)
312 {
313   XRenderPictFormat pf;
314   int ret;
315   gsCacheEntry *entry;
316
317   if((ret = LookupEntry(plfsz)) != -1) return ret;
318
319   ret = AllocEntry();
320   entry = glyphsetCache + ret;
321   entry->lfsz = *plfsz;
322   assert(entry->nrealized == 0);
323
324
325   if(antialias && abs(plfsz->lf.lfHeight * plfsz->xform.eM22) > 16) {
326     pf.depth = 8;
327     pf.direct.alphaMask = 0xff;
328   } else {
329     pf.depth = 1;
330     pf.direct.alphaMask = 1;
331   }
332   pf.type = PictTypeDirect;
333   pf.direct.alpha = 0;
334
335   wine_tsx11_lock();
336   entry->font_format = pXRenderFindFormat(gdi_display,
337                                          PictFormatType |
338                                          PictFormatDepth |
339                                          PictFormatAlpha |
340                                          PictFormatAlphaMask,
341                                          &pf, 0);
342
343   entry->glyphset = pXRenderCreateGlyphSet(gdi_display, entry->font_format);
344   wine_tsx11_unlock();
345   return ret;
346 }
347
348 static void dec_ref_cache(int index)
349 {
350   assert(index >= 0);
351   TRACE("dec'ing entry %d to %d\n", index, glyphsetCache[index].count - 1);
352   assert(glyphsetCache[index].count > 0);
353   glyphsetCache[index].count--;
354 }
355
356 static void lfsz_calc_hash(LFANDSIZE *plfsz)
357 {
358   DWORD hash = 0, *ptr;
359   int i;
360
361   for(ptr = (DWORD*)&plfsz->xform; ptr < (DWORD*)(&plfsz->xform + 1); ptr++)
362     hash ^= *ptr;
363   for(i = 0, ptr = (DWORD*)&plfsz->lf; i < 7; i++, ptr++)
364     hash ^= *ptr;
365   for(i = 0, ptr = (DWORD*)&plfsz->lf.lfFaceName; i < LF_FACESIZE/2; i++, ptr++) {
366     WCHAR *pwc = (WCHAR *)ptr;
367     if(!*pwc) break;
368     hash ^= *ptr;
369     pwc++;
370     if(!*pwc) break;
371   }
372   plfsz->hash = hash;
373   return;
374 }
375
376 /***********************************************************************
377  *   X11DRV_XRender_Finalize
378  */
379 void X11DRV_XRender_Finalize(void)
380 {
381     FIXME("Free cached glyphsets\n");
382 #if 0
383     if (xrender_handle) wine_dlclose(xrender_handle, NULL, 0);
384     xrender_handle = NULL;
385 #endif
386 }
387
388
389 /***********************************************************************
390  *   X11DRV_XRender_SelectFont
391  */
392 BOOL X11DRV_XRender_SelectFont(X11DRV_PDEVICE *physDev, HFONT hfont)
393 {
394     LFANDSIZE lfsz;
395
396     GetObjectW(hfont, sizeof(lfsz.lf), &lfsz.lf);
397     TRACE("h=%ld w=%ld weight=%ld it=%d charset=%d name=%s\n",
398           lfsz.lf.lfHeight, lfsz.lf.lfWidth, lfsz.lf.lfWeight,
399           lfsz.lf.lfItalic, lfsz.lf.lfCharSet, debugstr_w(lfsz.lf.lfFaceName));
400     lfsz.xform = physDev->dc->xformWorld2Vport;
401     lfsz_calc_hash(&lfsz);
402
403     EnterCriticalSection(&xrender_cs);
404     if(!physDev->xrender) {
405         physDev->xrender = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
406                                      sizeof(*physDev->xrender));
407         physDev->xrender->cache_index = -1;
408     }
409     else if(physDev->xrender->cache_index != -1)
410         dec_ref_cache(physDev->xrender->cache_index);
411     physDev->xrender->cache_index = GetCacheEntry(&lfsz);
412     LeaveCriticalSection(&xrender_cs);
413     return 0;
414 }
415
416 /***********************************************************************
417  *   X11DRV_XRender_DeleteDC
418  */
419 void X11DRV_XRender_DeleteDC(X11DRV_PDEVICE *physDev)
420 {
421     wine_tsx11_lock();
422     if(physDev->xrender->tile_pict)
423         pXRenderFreePicture(gdi_display, physDev->xrender->tile_pict);
424
425     if(physDev->xrender->tile_xpm)
426         XFreePixmap(gdi_display, physDev->xrender->tile_xpm);
427
428     if(physDev->xrender->pict) {
429         TRACE("freeing pict = %lx dc = %p\n", physDev->xrender->pict, physDev->dc);
430         pXRenderFreePicture(gdi_display, physDev->xrender->pict);
431     }
432     wine_tsx11_unlock();
433
434     EnterCriticalSection(&xrender_cs);
435     if(physDev->xrender->cache_index != -1)
436         dec_ref_cache(physDev->xrender->cache_index);
437     LeaveCriticalSection(&xrender_cs);
438
439     HeapFree(GetProcessHeap(), 0, physDev->xrender);
440     physDev->xrender = NULL;
441     return;
442 }
443
444 /***********************************************************************
445  *   X11DRV_XRender_UpdateDrawable
446  *
447  * This gets called from X11DRV_SetDrawable and deletes the pict when the
448  * drawable changes.  However at the moment we delete the pict at the end of
449  * every ExtTextOut so this is basically a NOP.
450  */
451 void X11DRV_XRender_UpdateDrawable(X11DRV_PDEVICE *physDev)
452 {
453     if(physDev->xrender->pict) {
454         TRACE("freeing pict %08lx from dc %p\n", physDev->xrender->pict, physDev->dc);
455         wine_tsx11_lock();
456         pXRenderFreePicture(gdi_display, physDev->xrender->pict);
457         wine_tsx11_unlock();
458     }
459     physDev->xrender->pict = 0;
460     return;
461 }
462
463 /************************************************************************
464  *   UploadGlyph
465  *
466  * Helper to ExtTextOut.  Must be called inside xrender_cs
467  */
468 static BOOL UploadGlyph(X11DRV_PDEVICE *physDev, int glyph)
469 {
470     int buflen;
471     char *buf;
472     Glyph gid;
473     GLYPHMETRICS gm;
474     XGlyphInfo gi;
475     gsCacheEntry *entry = glyphsetCache + physDev->xrender->cache_index;
476     UINT ggo_format = GGO_GLYPH_INDEX;
477     BOOL aa;
478
479     if(entry->nrealized <= glyph) {
480         entry->nrealized = (glyph / 128 + 1) * 128;
481         entry->realized = HeapReAlloc(GetProcessHeap(),
482                                       HEAP_ZERO_MEMORY,
483                                       entry->realized,
484                                       entry->nrealized * sizeof(BOOL));
485     }
486
487     if(entry->font_format->depth == 8) {
488         aa = TRUE;
489         ggo_format |= WINE_GGO_GRAY16_BITMAP;
490     } else {
491         aa = FALSE;
492         ggo_format |= GGO_BITMAP;
493     }
494
495     buflen = GetGlyphOutlineW(physDev->hdc, glyph, ggo_format, &gm, 0, NULL,
496                               NULL);
497     if(buflen == GDI_ERROR) {
498         LeaveCriticalSection(&xrender_cs);
499         return FALSE;
500     }
501
502     entry->realized[glyph] = TRUE;
503
504     buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buflen);
505     GetGlyphOutlineW(physDev->hdc, glyph, ggo_format, &gm, buflen, buf, NULL);
506
507     TRACE("buflen = %d. Got metrics: %dx%d adv=%d,%d origin=%ld,%ld\n",
508           buflen,
509           gm.gmBlackBoxX, gm.gmBlackBoxY, gm.gmCellIncX, gm.gmCellIncY,
510           gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
511
512     gi.width = gm.gmBlackBoxX;
513     gi.height = gm.gmBlackBoxY;
514     gi.x = -gm.gmptGlyphOrigin.x;
515     gi.y = gm.gmptGlyphOrigin.y;
516     gi.xOff = gm.gmCellIncX;
517     gi.yOff = gm.gmCellIncY;
518
519     if(TRACE_ON(xrender)) {
520         int pitch, i, j;
521         char output[300];
522         unsigned char *line;
523
524         if(!aa) {
525             pitch = ((gi.width + 31) / 32) * 4;
526             for(i = 0; i < gi.height; i++) {
527                 line = buf + i * pitch;
528                 output[0] = '\0';
529                 for(j = 0; j < pitch * 8; j++) {
530                     strcat(output, (line[j / 8] & (1 << (7 - (j % 8)))) ? "#" : " ");
531                 }
532                 strcat(output, "\n");
533                 TRACE(output);
534             }
535         } else {
536             char blks[] = " .:;!o*#";
537             char str[2];
538
539             str[1] = '\0';
540             pitch = ((gi.width + 3) / 4) * 4;
541             for(i = 0; i < gi.height; i++) {
542                 line = buf + i * pitch;
543                 output[0] = '\0';
544                 for(j = 0; j < pitch; j++) {
545                     str[0] = blks[line[j] >> 5];
546                     strcat(output, str);
547                 }
548                 strcat(output, "\n");
549                 TRACE(output);
550             }
551         }
552     }
553
554     if(!aa && BitmapBitOrder(gdi_display) != MSBFirst) {
555         unsigned char *byte = buf, c;
556         int i = buflen;
557
558         while(i--) {
559             c = *byte;
560
561             /* magic to flip bit order */
562             c = ((c << 1) & 0xaa) | ((c >> 1) & 0x55);
563             c = ((c << 2) & 0xcc) | ((c >> 2) & 0x33);
564             c = ((c << 4) & 0xf0) | ((c >> 4) & 0x0f);
565
566             *byte++ = c;
567         }
568     }
569     gid = glyph;
570     wine_tsx11_lock();
571     pXRenderAddGlyphs(gdi_display, entry->glyphset, &gid, &gi, 1,
572                        buf, buflen);
573     wine_tsx11_unlock();
574     HeapFree(GetProcessHeap(), 0, buf);
575     return TRUE;
576 }
577
578 /***********************************************************************
579  *   X11DRV_XRender_ExtTextOut
580  */
581 BOOL X11DRV_XRender_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
582                                 const RECT *lprect, LPCWSTR wstr, UINT count,
583                                 const INT *lpDx )
584 {
585     XRenderColor col;
586     int idx;
587     TEXTMETRICW tm;
588     RGNDATA *data;
589     SIZE sz;
590     RECT rc;
591     BOOL done_extents = FALSE;
592     INT width, xwidth, ywidth;
593     double cosEsc, sinEsc;
594     XGCValues xgcval;
595     LOGFONTW lf;
596     int render_op = PictOpOver;
597     WORD *glyphs;
598     POINT pt;
599     gsCacheEntry *entry;
600     BOOL retv = FALSE;
601     HDC hdc = physDev->hdc;
602     DC *dc = physDev->dc;
603
604     TRACE("%04x, %d, %d, %08x, %p, %s, %d, %p)\n", hdc, x, y, flags,
605           lprect, debugstr_wn(wstr, count), count, lpDx);
606
607     if(flags & ETO_GLYPH_INDEX)
608         glyphs = (LPWORD)wstr;
609     else {
610         glyphs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
611         GetGlyphIndicesW(hdc, wstr, count, glyphs, 0);
612     }
613
614     if(lprect)
615       TRACE("rect: %d,%d - %d,%d\n", lprect->left, lprect->top, lprect->right,
616             lprect->bottom);
617     TRACE("align = %x bkmode = %x mapmode = %x\n", dc->textAlign, GetBkMode(hdc), dc->MapMode);
618
619     if(dc->textAlign & TA_UPDATECP) {
620         x = dc->CursPosX;
621         y = dc->CursPosY;
622     }
623
624     GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
625     if(lf.lfEscapement != 0) {
626         cosEsc = cos(lf.lfEscapement * M_PI / 1800);
627         sinEsc = sin(lf.lfEscapement * M_PI / 1800);
628     } else {
629         cosEsc = 1;
630         sinEsc = 0;
631     }
632
633     if(flags & (ETO_CLIPPED | ETO_OPAQUE)) {
634         if(!lprect) {
635             if(flags & ETO_CLIPPED) return FALSE;
636                 GetTextExtentPointI(hdc, glyphs, count, &sz);
637                 done_extents = TRUE;
638                 rc.left = x;
639                 rc.top = y;
640                 rc.right = x + sz.cx;
641                 rc.bottom = y + sz.cy;
642         } else {
643             rc = *lprect;
644         }
645
646         LPtoDP(physDev->hdc, (POINT*)&rc, 2);
647
648         if(rc.left > rc.right) {INT tmp = rc.left; rc.left = rc.right; rc.right = tmp;}
649         if(rc.top > rc.bottom) {INT tmp = rc.top; rc.top = rc.bottom; rc.bottom = tmp;}
650     }
651
652     xgcval.function = GXcopy;
653     xgcval.background = physDev->backgroundPixel;
654     xgcval.fill_style = FillSolid;
655     TSXChangeGC( gdi_display, physDev->gc,
656                  GCFunction | GCBackground | GCFillStyle, &xgcval );
657
658     X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
659
660     if(flags & ETO_OPAQUE) {
661         TSXSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
662         TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
663                           physDev->org.x + rc.left, physDev->org.y + rc.top,
664                           rc.right - rc.left, rc.bottom - rc.top );
665     }
666
667     if(count == 0) {
668         retv =  TRUE;
669         goto done;
670     }
671
672     pt.x = x;
673     pt.y = y;
674     LPtoDP(physDev->hdc, &pt, 1);
675     x = pt.x;
676     y = pt.y;
677
678     TRACE("real x,y %d,%d\n", x, y);
679
680     if(lpDx) {
681         width = 0;
682         for(idx = 0; idx < count; idx++)
683             width += lpDx[idx];
684     } else {
685         if(!done_extents) {
686             GetTextExtentPointI(hdc, glyphs, count, &sz);
687             done_extents = TRUE;
688         }
689         width = sz.cx;
690     }
691     width = INTERNAL_XWSTODS(dc, width);
692     xwidth = width * cosEsc;
693     ywidth = width * sinEsc;
694
695     GetTextMetricsW(hdc, &tm);
696
697     tm.tmAscent = INTERNAL_YWSTODS(dc, tm.tmAscent);
698     tm.tmDescent = INTERNAL_YWSTODS(dc, tm.tmDescent);
699     switch( dc->textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) ) {
700     case TA_LEFT:
701         if (dc->textAlign & TA_UPDATECP) {
702             pt.x = x + xwidth;
703             pt.y = y - ywidth;
704             DPtoLP(physDev->hdc, &pt, 1);
705             dc->CursPosX = pt.x;
706             dc->CursPosY = pt.y;
707         }
708         break;
709
710     case TA_CENTER:
711         x -= xwidth / 2;
712         y += ywidth / 2;
713         break;
714
715     case TA_RIGHT:
716         x -= xwidth;
717         y += ywidth;
718         if (dc->textAlign & TA_UPDATECP) {
719             pt.x = x;
720             pt.y = y;
721             DPtoLP(physDev->hdc, &pt, 1);
722             dc->CursPosX = pt.x;
723             dc->CursPosY = pt.y;
724         }
725         break;
726     }
727
728     switch( dc->textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) ) {
729     case TA_TOP:
730         y += tm.tmAscent * cosEsc;
731         x += tm.tmAscent * sinEsc;
732         break;
733
734     case TA_BOTTOM:
735         y -= tm.tmDescent * cosEsc;
736         x -= tm.tmDescent * sinEsc;
737         break;
738
739     case TA_BASELINE:
740         break;
741     }
742
743     if (flags & ETO_CLIPPED)
744     {
745         SaveVisRgn16( hdc );
746         IntersectVisRect16( dc->hSelf, lprect->left, lprect->top, lprect->right, lprect->bottom );
747     }
748
749     if(!physDev->xrender->pict) {
750         XRenderPictureAttributes pa;
751         pa.subwindow_mode = IncludeInferiors;
752
753         wine_tsx11_lock();
754         physDev->xrender->pict = pXRenderCreatePicture(gdi_display,
755                                                        physDev->drawable,
756                                                        (dc->bitsPerPixel == 1) ?
757                                                        mono_format : screen_format,
758                                                        CPSubwindowMode, &pa);
759         wine_tsx11_unlock();
760
761         TRACE("allocing pict = %lx dc = %p drawable = %08lx\n", physDev->xrender->pict, dc, physDev->drawable);
762     } else {
763         TRACE("using existing pict = %lx dc = %p\n", physDev->xrender->pict, dc);
764     }
765
766     if ((data = X11DRV_GetRegionData( dc->hGCClipRgn, 0 )))
767     {
768         wine_tsx11_lock();
769         pXRenderSetPictureClipRectangles( gdi_display, physDev->xrender->pict,
770                                           physDev->org.x, physDev->org.y,
771                                           (XRectangle *)data->Buffer, data->rdh.nCount );
772         wine_tsx11_unlock();
773         HeapFree( GetProcessHeap(), 0, data );
774     }
775
776     if(GetBkMode(hdc) != TRANSPARENT) {
777         if(!((flags & ETO_CLIPPED) && (flags & ETO_OPAQUE))) {
778             if(!(flags & ETO_OPAQUE) || x < rc.left || x + width >= rc.right ||
779                y - tm.tmAscent < rc.top || y + tm.tmDescent >= rc.bottom) {
780                 TSXSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
781                 TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
782                                   physDev->org.x + x, physDev->org.y + y - tm.tmAscent,
783                                   width, tm.tmAscent + tm.tmDescent );
784             }
785         }
786     }
787
788     /* Create a 1x1 pixmap to tile over the font mask */
789     if(!physDev->xrender->tile_xpm) {
790         XRenderPictureAttributes pa;
791
792         XRenderPictFormat *format = (dc->bitsPerPixel == 1) ? mono_format : screen_format;
793         wine_tsx11_lock();
794         physDev->xrender->tile_xpm = XCreatePixmap(gdi_display,
795                                                      physDev->drawable,
796                                                      1, 1,
797                                                      format->depth);
798         pa.repeat = True;
799         physDev->xrender->tile_pict = pXRenderCreatePicture(gdi_display,
800                                                              physDev->xrender->tile_xpm,
801                                                              format,
802                                                              CPRepeat, &pa);
803         wine_tsx11_unlock();
804         TRACE("Created pixmap of depth %d\n", format->depth);
805         /* init lastTextColor to something different from dc->textColor */
806         physDev->xrender->lastTextColor = ~dc->textColor;
807
808     }
809
810     if(dc->textColor != physDev->xrender->lastTextColor) {
811         if(dc->bitsPerPixel != 1) {
812             /* Map 0 -- 0xff onto 0 -- 0xffff */
813             col.red = GetRValue(dc->textColor);
814             col.red |= col.red << 8;
815             col.green = GetGValue(dc->textColor);
816             col.green |= col.green << 8;
817             col.blue = GetBValue(dc->textColor);
818             col.blue |= col.blue << 8;
819             col.alpha = 0x0;
820         } else { /* for a 1bpp bitmap we always need a 1 in the tile */
821             col.red = col.green = col.blue = 0;
822             col.alpha = 0xffff;
823         }
824         wine_tsx11_lock();
825         pXRenderFillRectangle(gdi_display, PictOpSrc,
826                                physDev->xrender->tile_pict,
827                                &col, 0, 0, 1, 1);
828         wine_tsx11_unlock();
829         physDev->xrender->lastTextColor = dc->textColor;
830     }
831
832     /* FIXME the mapping of Text/BkColor onto 1 or 0 needs investigation.
833      */
834     if((dc->bitsPerPixel == 1) && ((dc->textColor & 0xffffff) == 0))
835         render_op = PictOpOutReverse; /* This gives us 'black' text */
836
837     EnterCriticalSection(&xrender_cs);
838     entry = glyphsetCache + physDev->xrender->cache_index;
839
840     for(idx = 0; idx < count; idx++) {
841         if(glyphs[idx] >= entry->nrealized || entry->realized[glyphs[idx]] == FALSE) {
842             UploadGlyph(physDev, glyphs[idx]);
843         }
844     }
845
846
847     TRACE("Writing %s at %ld,%ld\n", debugstr_wn(wstr,count),
848           physDev->org.x + x, physDev->org.y + y);
849
850     wine_tsx11_lock();
851     if(!lpDx)
852         pXRenderCompositeString16(gdi_display, render_op,
853                                    physDev->xrender->tile_pict,
854                                    physDev->xrender->pict,
855                                    entry->font_format, entry->glyphset,
856                                    0, 0, physDev->org.x + x, physDev->org.y + y,
857                                    glyphs, count);
858
859     else {
860         INT offset = 0, xoff = 0, yoff = 0;
861         for(idx = 0; idx < count; idx++) {
862             pXRenderCompositeString16(gdi_display, render_op,
863                                        physDev->xrender->tile_pict,
864                                        physDev->xrender->pict,
865                                        entry->font_format, entry->glyphset,
866                                        0, 0, physDev->org.x + x + xoff,
867                                        physDev->org.y + y + yoff,
868                                        glyphs + idx, 1);
869             offset += INTERNAL_XWSTODS(dc, lpDx[idx]);
870             xoff = offset * cosEsc;
871             yoff = offset * -sinEsc;
872         }
873     }
874
875     LeaveCriticalSection(&xrender_cs);
876
877     if(physDev->xrender->pict) {
878         pXRenderFreePicture(gdi_display, physDev->xrender->pict);
879     }
880     physDev->xrender->pict = 0;
881     wine_tsx11_unlock();
882
883     if (flags & ETO_CLIPPED)
884         RestoreVisRgn16( hdc );
885
886     retv = TRUE;
887
888 done:
889     X11DRV_UnlockDIBSection( physDev, TRUE );
890     if(glyphs != wstr) HeapFree(GetProcessHeap(), 0, glyphs);
891     return retv;
892 }
893
894 #else /* HAVE_X11_EXTENSIONS_XRENDER_H */
895
896 void X11DRV_XRender_Init(void)
897 {
898     TRACE("XRender support not compiled in.\n");
899     return;
900 }
901
902 void X11DRV_XRender_Finalize(void)
903 {
904 }
905
906 BOOL X11DRV_XRender_SelectFont(X11DRV_PDEVICE *physDev, HFONT hfont)
907 {
908   assert(0);
909   return FALSE;
910 }
911
912 void X11DRV_XRender_DeleteDC(X11DRV_PDEVICE *physDev)
913 {
914   assert(0);
915   return;
916 }
917
918 BOOL X11DRV_XRender_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
919                                 const RECT *lprect, LPCWSTR wstr, UINT count,
920                                 const INT *lpDx )
921 {
922   assert(0);
923   return FALSE;
924 }
925
926 void X11DRV_XRender_UpdateDrawable(X11DRV_PDEVICE *physDev)
927 {
928   assert(0);
929   return;
930 }
931
932 #endif /* HAVE_X11_EXTENSIONS_XRENDER_H */