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