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