Added LGPL standard comment, and copyright notices where necessary.
[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
22 #include "winnt.h"
23 #include "x11drv.h"
24 #include "bitmap.h"
25 #include "wine/debug.h"
26 #include "region.h"
27 #include <string.h>
28 #include <stdlib.h>
29 #include "wine/unicode.h"
30 #include <assert.h>
31
32 BOOL X11DRV_XRender_Installed = FALSE;
33 WINE_DEFAULT_DEBUG_CHANNEL(xrender);
34
35 #ifdef HAVE_LIBXRENDER
36
37 #include "ts_xlib.h"
38 #include "ts_xrender.h"
39
40 static XRenderPictFormat *screen_format; /* format of screen */
41 static XRenderPictFormat *mono_format; /* format of mono bitmap */
42
43 typedef struct
44 {
45   LOGFONTW lf;
46   XFORM  xform; /* this is dum as we don't care about offsets */
47   DWORD hash;
48 } LFANDSIZE;
49
50 #define INITIAL_REALIZED_BUF_SIZE 128
51
52
53 typedef struct
54 {
55   LFANDSIZE lfsz;
56   GlyphSet glyphset;
57   XRenderPictFormat *font_format;
58   int nrealized;
59   BOOL *realized;
60   UINT count;
61   INT next;
62 } gsCacheEntry;
63
64 struct tagXRENDERINFO
65 {
66     gsCacheEntry       *cacheEntry;
67     Picture            pict;
68     Picture            tile_pict;
69     Pixmap             tile_xpm;
70     COLORREF           lastTextColor;
71 };
72
73
74 static gsCacheEntry *glyphsetCache = NULL;
75 static DWORD glyphsetCacheSize = 0;
76 static INT lastfree = -1;
77 static INT mru = -1;
78
79 #define INIT_CACHE_SIZE 10
80
81 static int antialias = 1;
82
83 /***********************************************************************
84  *   X11DRV_XRender_Init
85  *
86  * Let's see if our XServer has the extension available
87  *
88  */
89 void X11DRV_XRender_Init(void)
90 {
91     int error_base, event_base, i;
92     XRenderPictFormat pf;
93
94     if(TSXRenderQueryExtension(gdi_display, &event_base, &error_base)) {
95         X11DRV_XRender_Installed = TRUE;
96         TRACE("Xrender is up and running error_base = %d\n", error_base);
97         screen_format = TSXRenderFindVisualFormat(gdi_display, visual);
98         pf.type = PictTypeDirect;
99         pf.depth = 1;
100         pf.direct.alpha = 0;
101         pf.direct.alphaMask = 1;
102         mono_format = TSXRenderFindFormat(gdi_display, PictFormatType |
103                                           PictFormatDepth | PictFormatAlpha |
104                                           PictFormatAlphaMask, &pf, 0);
105
106         glyphsetCache = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
107                                   sizeof(*glyphsetCache) * INIT_CACHE_SIZE);
108
109         glyphsetCacheSize = INIT_CACHE_SIZE;
110         lastfree = 0;
111         for(i = 0; i < INIT_CACHE_SIZE; i++) {
112           glyphsetCache[i].next = i + 1;
113           glyphsetCache[i].count = -1;
114         }
115         glyphsetCache[i-1].next = -1;
116     } else {
117         TRACE("Xrender is not available on this server\n");
118     }
119     return;
120 }
121
122 static BOOL fontcmp(LFANDSIZE *p1, LFANDSIZE *p2)
123 {
124   if(p1->hash != p2->hash) return TRUE;
125   if(memcmp(&p1->xform, &p2->xform, sizeof(p1->xform))) return TRUE;
126   if(memcmp(&p1->lf, &p2->lf, offsetof(LOGFONTW, lfFaceName))) return TRUE;
127   return strcmpW(p1->lf.lfFaceName, p2->lf.lfFaceName);
128 }
129
130 static void walk_cache(void)
131 {
132   int i;
133
134   for(i=mru; i >= 0; i = glyphsetCache[i].next)
135     TRACE("item %d\n", i);
136 }
137
138 static gsCacheEntry *LookupEntry(LFANDSIZE *plfsz)
139 {
140   int i, prev_i = -1;
141
142   for(i = mru; i >= 0; i = glyphsetCache[i].next) {
143     TRACE("%d\n", i);
144     if(glyphsetCache[i].count == -1) { /* reached free list so stop */
145       i = -1;
146       break;
147     }
148
149     if(!fontcmp(&glyphsetCache[i].lfsz, plfsz)) {
150       glyphsetCache[i].count++;
151       if(prev_i >= 0) {
152         glyphsetCache[prev_i].next = glyphsetCache[i].next;
153         glyphsetCache[i].next = mru;
154         mru = i;
155       }
156       TRACE("found font in cache %d\n", i);
157       return glyphsetCache + i;
158     }
159     prev_i = i;
160   }
161   TRACE("font not in cache\n");
162   return NULL;
163 }
164
165 static gsCacheEntry *AllocEntry(void)
166 {
167   int best = -1, prev_best = -1, i, prev_i = -1;
168
169   if(lastfree >= 0) {
170     assert(glyphsetCache[lastfree].count == -1);
171     glyphsetCache[lastfree].count = 1;
172     best = lastfree;
173     lastfree = glyphsetCache[lastfree].next;    
174     assert(best != mru);
175     glyphsetCache[best].next = mru;
176     mru = best;
177
178     TRACE("empty space at %d, next lastfree = %d\n", mru, lastfree);
179     return glyphsetCache + mru;
180   }
181
182   for(i = mru; i >= 0; i = glyphsetCache[i].next) {
183     if(glyphsetCache[i].count == 0) {
184       best = i;
185       prev_best = prev_i;
186     }
187     prev_i = i;
188   }
189
190   if(best >= 0) {
191     TRACE("freeing unused glyphset at cache %d\n", best);
192     TSXRenderFreeGlyphSet(gdi_display, glyphsetCache[best].glyphset);
193     glyphsetCache[best].glyphset = 0;
194     if(glyphsetCache[best].nrealized) { /* do we really want to do this? */
195       HeapFree(GetProcessHeap(), 0, glyphsetCache[best].realized);
196       glyphsetCache[best].realized = NULL;
197       glyphsetCache[best].nrealized = 0;
198     }
199     glyphsetCache[best].count = 1;
200     if(prev_best >= 0) {
201       glyphsetCache[prev_best].next = glyphsetCache[best].next;
202       glyphsetCache[best].next = mru;
203       mru = best;
204     } else {
205       assert(mru == best);
206     }
207     return glyphsetCache + mru;
208   }
209
210   TRACE("Growing cache\n");
211   glyphsetCache = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
212                               glyphsetCache,
213                               (glyphsetCacheSize + INIT_CACHE_SIZE)
214                               * sizeof(*glyphsetCache));
215   for(best = i = glyphsetCacheSize; i < glyphsetCacheSize + INIT_CACHE_SIZE;
216       i++) {
217     glyphsetCache[i].next = i + 1;
218     glyphsetCache[i].count = -1;
219   }
220   glyphsetCache[i-1].next = -1;
221   glyphsetCacheSize += INIT_CACHE_SIZE;
222
223   lastfree = glyphsetCache[best].next;
224   glyphsetCache[best].count = 1;
225   glyphsetCache[best].next = mru;
226   mru = best;
227   TRACE("new free cache slot at %d\n", mru);
228   return glyphsetCache + mru;
229 }    
230
231 static gsCacheEntry *GetCacheEntry(LFANDSIZE *plfsz)
232 {
233   XRenderPictFormat pf;
234   gsCacheEntry *ret;
235
236   if((ret = LookupEntry(plfsz)) != NULL) return ret;
237
238   ret = AllocEntry();
239   ret->lfsz = *plfsz;
240   assert(ret->nrealized == 0);
241
242
243   if(antialias && abs(plfsz->lf.lfHeight) > 16) {
244     pf.depth = 8;
245     pf.direct.alphaMask = 0xff;
246   } else {
247     pf.depth = 1;
248     pf.direct.alphaMask = 1;
249   }
250   pf.type = PictTypeDirect;
251   pf.direct.alpha = 0;
252
253   ret->font_format = TSXRenderFindFormat(gdi_display,
254                                          PictFormatType |
255                                          PictFormatDepth |
256                                          PictFormatAlpha |
257                                          PictFormatAlphaMask,
258                                          &pf, 0);
259
260   ret->glyphset = TSXRenderCreateGlyphSet(gdi_display, ret->font_format);
261   return ret;
262 }
263
264 static void dec_ref_cache(gsCacheEntry *entry)
265 {
266   TRACE("dec'ing entry %d to %d\n", entry - glyphsetCache, entry->count - 1);
267   assert(entry->count > 0);
268   entry->count--;
269 }
270
271 static void lfsz_calc_hash(LFANDSIZE *plfsz)
272 {
273   DWORD hash = 0, *ptr;
274   int i;
275
276   for(ptr = (DWORD*)&plfsz->xform; ptr < (DWORD*)(&plfsz->xform + 1); ptr++)
277     hash ^= *ptr;
278   for(i = 0, ptr = (DWORD*)&plfsz->lf; i < 7; i++, ptr++)
279     hash ^= *ptr;
280   for(i = 0, ptr = (DWORD*)&plfsz->lf.lfFaceName; i < LF_FACESIZE/2; i++, ptr++) {
281     WCHAR *pwc = (WCHAR *)ptr;
282     if(!*pwc) break;
283     hash ^= *ptr;
284     pwc++;
285     if(!*pwc) break;
286   }
287   plfsz->hash = hash;
288   return;
289 }
290
291 /***********************************************************************
292  *   X11DRV_XRender_Finalize
293  */
294 void X11DRV_XRender_Finalize(void)
295 {
296     FIXME("Free cached glyphsets\n");
297     return;
298 }
299
300
301 /***********************************************************************
302  *   X11DRV_XRender_SelectFont
303  */
304 BOOL X11DRV_XRender_SelectFont(DC *dc, HFONT hfont)
305 {
306     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
307     LFANDSIZE lfsz;
308
309     GetObjectW(hfont, sizeof(lfsz.lf), &lfsz.lf);
310     TRACE("h=%ld w=%ld weight=%ld it=%d charset=%d name=%s\n",
311           lfsz.lf.lfHeight, lfsz.lf.lfWidth, lfsz.lf.lfWeight,
312           lfsz.lf.lfItalic, lfsz.lf.lfCharSet, debugstr_w(lfsz.lf.lfFaceName));
313     lfsz.xform = dc->xformWorld2Vport;
314     lfsz_calc_hash(&lfsz);
315
316     if(!physDev->xrender)
317         physDev->xrender = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
318                                      sizeof(*physDev->xrender));
319
320     else if(physDev->xrender->cacheEntry)
321         dec_ref_cache(physDev->xrender->cacheEntry);
322     physDev->xrender->cacheEntry = GetCacheEntry(&lfsz);
323     return 0;
324 }
325
326 /***********************************************************************
327  *   X11DRV_XRender_DeleteDC
328  */
329 void X11DRV_XRender_DeleteDC(DC *dc)
330 {
331     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
332     
333     if(physDev->xrender->tile_pict)
334         TSXRenderFreePicture(gdi_display, physDev->xrender->tile_pict);
335
336     if(physDev->xrender->tile_xpm)
337         TSXFreePixmap(gdi_display, physDev->xrender->tile_xpm);
338
339     if(physDev->xrender->pict) {
340         TRACE("freeing pict = %lx dc = %p\n", physDev->xrender->pict, dc);
341         TSXRenderFreePicture(gdi_display, physDev->xrender->pict);
342     }
343
344     if(physDev->xrender->cacheEntry)
345         dec_ref_cache(physDev->xrender->cacheEntry);
346     
347     HeapFree(GetProcessHeap(), 0, physDev->xrender);
348     physDev->xrender = NULL;
349     return;
350 }
351
352 /***********************************************************************
353  *   X11DRV_XRender_UpdateDrawable
354  *
355  * This gets called from X11DRV_SetDrawable and deletes the pict when the
356  * drawable changes.  However at the moment we delete the pict at the end of
357  * every ExtTextOut so this is basically a NOP.
358  */
359 void X11DRV_XRender_UpdateDrawable(DC *dc)
360 {
361     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
362
363     if(physDev->xrender->pict) {
364         TRACE("freeing pict %08lx from dc %p\n", physDev->xrender->pict, dc);
365         TSXRenderFreePicture(gdi_display, physDev->xrender->pict);
366     }
367     physDev->xrender->pict = 0;
368     return;
369 }
370
371 static BOOL UploadGlyph(DC *dc, WCHAR glyph)
372 {
373     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
374     int buflen;
375     char *buf;
376     Glyph gid;
377     GLYPHMETRICS gm;
378     XGlyphInfo gi;
379     gsCacheEntry *entry = physDev->xrender->cacheEntry;
380     UINT ggo_format;
381     BOOL aa;
382
383     if(entry->nrealized <= glyph) {
384         entry->nrealized = (glyph / 128 + 1) * 128;
385         entry->realized = HeapReAlloc(GetProcessHeap(),
386                                       HEAP_ZERO_MEMORY,
387                                       entry->realized,
388                                       entry->nrealized * sizeof(BOOL));
389     }
390     entry->realized[glyph] = TRUE;
391
392     if(entry->font_format->depth == 8) {
393         aa = TRUE;
394         ggo_format = WINE_GGO_GRAY16_BITMAP;
395     } else {
396         aa = FALSE;
397         ggo_format = GGO_BITMAP;
398     }
399
400     buflen = GetGlyphOutlineW(dc->hSelf, glyph, ggo_format, &gm, 0, NULL,
401                               NULL);
402     if(buflen == GDI_ERROR)
403         return FALSE;
404
405     buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buflen);
406     GetGlyphOutlineW(dc->hSelf, glyph, ggo_format, &gm, buflen, buf, NULL);
407
408     TRACE("buflen = %d. Got metrics: %dx%d adv=%d,%d origin=%ld,%ld\n",
409           buflen,
410           gm.gmBlackBoxX, gm.gmBlackBoxY, gm.gmCellIncX, gm.gmCellIncY,
411           gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
412
413     gi.width = gm.gmBlackBoxX;
414     gi.height = gm.gmBlackBoxY;
415     gi.x = -gm.gmptGlyphOrigin.x;
416     gi.y = gm.gmptGlyphOrigin.y;
417     gi.xOff = gm.gmCellIncX;
418     gi.yOff = gm.gmCellIncY;
419
420     if(TRACE_ON(xrender)) {
421         int pitch, i, j;
422         char output[300];
423         unsigned char *line;
424
425         if(!aa) {
426             pitch = ((gi.width + 31) / 32) * 4;
427             for(i = 0; i < gi.height; i++) {
428                 line = buf + i * pitch;
429                 output[0] = '\0';
430                 for(j = 0; j < pitch * 8; j++) {
431                     strcat(output, (line[j / 8] & (1 << (7 - (j % 8)))) ? "#" : " ");
432                 }
433                 strcat(output, "\n");
434                 TRACE(output);
435             }
436         } else {
437             char blks[] = " .:;!o*#";
438             char str[2];
439
440             str[1] = '\0';
441             pitch = ((gi.width + 3) / 4) * 4;
442             for(i = 0; i < gi.height; i++) {
443                 line = buf + i * pitch;
444                 output[0] = '\0';
445                 for(j = 0; j < pitch; j++) {
446                     str[0] = blks[line[j] >> 5];
447                     strcat(output, str);
448                 }
449                 strcat(output, "\n");
450                 TRACE(output);
451             }
452         }
453     }
454
455     if(!aa && BitmapBitOrder(gdi_display) != MSBFirst) {
456         unsigned char *byte = buf, c;
457         int i = buflen;
458
459         while(i--) {
460             c = *byte;
461
462             /* magic to flip bit order */
463             c = ((c << 1) & 0xaa) | ((c >> 1) & 0x55);
464             c = ((c << 2) & 0xcc) | ((c >> 2) & 0x33);
465             c = ((c << 4) & 0xf0) | ((c >> 4) & 0x0f);
466
467             *byte++ = c;
468         }
469     }
470     gid = glyph;
471     TSXRenderAddGlyphs(gdi_display, entry->glyphset, &gid, &gi, 1,
472                        buf, buflen);
473     HeapFree(GetProcessHeap(), 0, buf);
474     return TRUE;
475 }
476
477 /***********************************************************************
478  *   X11DRV_XRender_ExtTextOut
479  */
480 BOOL X11DRV_XRender_ExtTextOut( DC *dc, INT x, INT y, UINT flags,
481                                 const RECT *lprect, LPCWSTR wstr, UINT count,
482                                 const INT *lpDx )
483 {
484     XRenderColor col;
485     int idx;
486     TEXTMETRICW tm;
487     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
488     RGNOBJ *obj;
489     XRectangle *pXrect;
490     SIZE sz;
491     RECT rc;
492     BOOL done_extents = FALSE;
493     INT width, xwidth, ywidth;
494     double cosEsc, sinEsc;
495     XGCValues xgcval;
496     LOGFONTW lf;
497     int render_op = PictOpOver;
498
499     TRACE("%04x, %d, %d, %08x, %p, %s, %d, %p)\n", dc->hSelf, x, y, flags,
500           lprect, debugstr_wn(wstr, count), count, lpDx);
501     if(lprect)
502       TRACE("rect: %d,%d - %d,%d\n", lprect->left, lprect->top, lprect->right,
503             lprect->bottom);
504     TRACE("align = %x bkmode = %x mapmode = %x\n", dc->textAlign,
505           dc->backgroundMode,
506           dc->MapMode);
507
508     if(dc->textAlign & TA_UPDATECP) {
509         x = dc->CursPosX;
510         y = dc->CursPosY;
511     }
512
513     GetObjectW(GetCurrentObject(dc->hSelf, OBJ_FONT), sizeof(lf), &lf);
514     if(lf.lfEscapement != 0) {
515         cosEsc = cos(lf.lfEscapement * M_PI / 1800);
516         sinEsc = sin(lf.lfEscapement * M_PI / 1800);
517     } else {
518         cosEsc = 1;
519         sinEsc = 0;
520     }
521
522     if(flags & (ETO_CLIPPED | ETO_OPAQUE)) {
523         if(!lprect) {
524             if(flags & ETO_CLIPPED) return FALSE;
525                 GetTextExtentPointW(dc->hSelf, wstr, count, &sz);
526                 done_extents = TRUE;
527                 rc.left = x;
528                 rc.top = y;
529                 rc.right = x + sz.cx;
530                 rc.bottom = y + sz.cy;
531         } else {
532             rc = *lprect;
533         }
534
535         rc.left = INTERNAL_XWPTODP(dc, rc.left, rc.top);
536         rc.top = INTERNAL_YWPTODP(dc, rc.left, rc.top);
537         rc.right = INTERNAL_XWPTODP(dc, rc.right, rc.bottom);
538         rc.bottom = INTERNAL_YWPTODP(dc, rc.right, rc.bottom);
539
540         if(rc.left > rc.right) {INT tmp = rc.left; rc.left = rc.right; rc.right = tmp;}
541         if(rc.top > rc.bottom) {INT tmp = rc.top; rc.top = rc.bottom; rc.bottom = tmp;}
542     }
543
544     xgcval.function = GXcopy;
545     xgcval.background = physDev->backgroundPixel;
546     xgcval.fill_style = FillSolid;
547     TSXChangeGC( gdi_display, physDev->gc,
548                  GCFunction | GCBackground | GCFillStyle, &xgcval );
549
550     X11DRV_LockDIBSection( dc, DIB_Status_GdiMod, FALSE );
551
552     if(flags & ETO_OPAQUE) {
553         TSXSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
554         TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
555                           dc->DCOrgX + rc.left, dc->DCOrgY + rc.top,
556                           rc.right - rc.left, rc.bottom - rc.top );
557     }
558
559     if(count == 0) {
560         X11DRV_UnlockDIBSection( dc, TRUE );
561         return TRUE;
562     }
563
564     x = INTERNAL_XWPTODP( dc, x, y );
565     y = INTERNAL_YWPTODP( dc, x, y );
566
567     TRACE("real x,y %d,%d\n", x, y);
568
569     if(lpDx) {
570         width = 0;
571         for(idx = 0; idx < count; idx++)
572             width += lpDx[idx];
573     } else {
574         if(!done_extents) {
575             GetTextExtentPointW(dc->hSelf, wstr, count, &sz);
576             done_extents = TRUE;
577         }
578         width = sz.cx;
579     }
580     width = INTERNAL_XWSTODS(dc, width);
581     xwidth = width * cosEsc;
582     ywidth = width * sinEsc;
583
584     GetTextMetricsW(dc->hSelf, &tm);
585
586     switch( dc->textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) ) {
587     case TA_LEFT:
588         if (dc->textAlign & TA_UPDATECP) {
589             dc->CursPosX = INTERNAL_XDPTOWP( dc, x + xwidth, y - ywidth );
590             dc->CursPosY = INTERNAL_YDPTOWP( dc, x + xwidth, y - ywidth );
591         }
592         break;
593       
594     case TA_CENTER:
595         x -= xwidth / 2;
596         y += ywidth / 2;
597         break;
598
599     case TA_RIGHT:
600         x -= xwidth;
601         y += ywidth;
602         if (dc->textAlign & TA_UPDATECP) {
603             dc->CursPosX = INTERNAL_XDPTOWP( dc, x + xwidth, y - ywidth );
604             dc->CursPosY = INTERNAL_YDPTOWP( dc, x + xwidth, y - ywidth );
605         }
606         break;
607     }
608
609     switch( dc->textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) ) {
610     case TA_TOP:
611         y += tm.tmAscent * cosEsc;
612         x += tm.tmAscent * sinEsc;
613         break;
614
615     case TA_BOTTOM:
616         y -= tm.tmDescent * cosEsc;
617         x -= tm.tmDescent * sinEsc;
618         break;
619
620     case TA_BASELINE:
621         break;
622     }
623
624     if (flags & ETO_CLIPPED)
625     {
626         SaveVisRgn16( dc->hSelf );
627         CLIPPING_IntersectVisRect( dc, rc.left, rc.top, rc.right,
628                                    rc.bottom, FALSE );
629     }
630
631
632
633     if(!physDev->xrender->pict) {
634         XRenderPictureAttributes pa;
635         pa.subwindow_mode = IncludeInferiors;
636
637         physDev->xrender->pict =
638           TSXRenderCreatePicture(gdi_display,
639                                  physDev->drawable,
640                                  (dc->bitsPerPixel == 1) ?
641                                    mono_format : screen_format,
642                                  CPSubwindowMode, &pa);
643
644         TRACE("allocing pict = %lx dc = %p drawable = %08lx\n", physDev->xrender->pict, dc, physDev->drawable);
645     } else {
646         TRACE("using existing pict = %lx dc = %p\n", physDev->xrender->pict, dc);
647     }
648
649     obj = (RGNOBJ *) GDI_GetObjPtr(dc->hGCClipRgn, REGION_MAGIC);
650     if (!obj)
651     {
652         ERR("Rgn is 0. Please report this.\n");
653         return FALSE;
654     }
655     
656     if (obj->rgn->numRects > 0)
657     {
658         XRectangle *pXr;
659         RECT *pRect = obj->rgn->rects;
660         RECT *pEndRect = obj->rgn->rects + obj->rgn->numRects;
661
662         pXrect = HeapAlloc( GetProcessHeap(), 0, 
663                             sizeof(*pXrect) * obj->rgn->numRects );
664         if(!pXrect)
665         {
666             WARN("Can't alloc buffer\n");
667             GDI_ReleaseObj( dc->hGCClipRgn );
668             return FALSE;
669         }
670
671         for(pXr = pXrect; pRect < pEndRect; pRect++, pXr++)
672         {
673             pXr->x = pRect->left;
674             pXr->y = pRect->top;
675             pXr->width = pRect->right - pRect->left;
676             pXr->height = pRect->bottom - pRect->top;
677             TRACE("Adding clip rect %d,%d - %d,%d\n", pRect->left, pRect->top,
678                   pRect->right, pRect->bottom);
679         }
680     }
681     else {
682         TRACE("no clip rgn\n");
683         pXrect = NULL;
684     }
685
686     TSXRenderSetPictureClipRectangles( gdi_display, physDev->xrender->pict,
687                                        0, 0, pXrect, obj->rgn->numRects );
688
689     if(pXrect)
690         HeapFree( GetProcessHeap(), 0, pXrect );
691
692     GDI_ReleaseObj( dc->hGCClipRgn );
693
694     if(dc->backgroundMode != TRANSPARENT) {
695         if(!((flags & ETO_CLIPPED) && (flags & ETO_OPAQUE))) {
696             if(!(flags & ETO_OPAQUE) || x < rc.left || x + width >= rc.right ||
697                y - tm.tmAscent < rc.top || y + tm.tmDescent >= rc.bottom) {
698                 TSXSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
699                 TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
700                                   dc->DCOrgX + x, dc->DCOrgY + y - tm.tmAscent,
701                                   width, tm.tmAscent + tm.tmDescent );
702             }
703         }
704     }
705
706     /* Create a 1x1 pixmap to tile over the font mask */
707     if(!physDev->xrender->tile_xpm) {
708         XRenderPictureAttributes pa;
709
710         XRenderPictFormat *format = (dc->bitsPerPixel == 1) ? mono_format : screen_format;
711         physDev->xrender->tile_xpm = TSXCreatePixmap(gdi_display,
712                                                      physDev->drawable,
713                                                      1, 1,
714                                                      format->depth);
715         pa.repeat = True;
716         physDev->xrender->tile_pict = TSXRenderCreatePicture(gdi_display,
717                                                              physDev->xrender->tile_xpm,
718                                                              format,
719                                                              CPRepeat, &pa);
720         TRACE("Created pixmap of depth %d\n", format->depth);
721         /* init lastTextColor to something different from dc->textColor */
722         physDev->xrender->lastTextColor = ~dc->textColor;
723
724     }
725     
726     if(dc->textColor != physDev->xrender->lastTextColor) {
727         if(dc->bitsPerPixel != 1) {
728             /* Map 0 -- 0xff onto 0 -- 0xffff */
729             col.red = GetRValue(dc->textColor);
730             col.red |= col.red << 8;
731             col.green = GetGValue(dc->textColor);
732             col.green |= col.green << 8;
733             col.blue = GetBValue(dc->textColor);
734             col.blue |= col.blue << 8;
735             col.alpha = 0x0;
736         } else { /* for a 1bpp bitmap we always need a 1 in the tile */
737             col.red = col.green = col.blue = 0;
738             col.alpha = 0xffff;
739         }
740         TSXRenderFillRectangle(gdi_display, PictOpSrc,
741                                physDev->xrender->tile_pict,
742                                &col, 0, 0, 1, 1);
743         physDev->xrender->lastTextColor = dc->textColor;
744     }
745
746     /* FIXME the mapping of Text/BkColor onto 1 or 0 needs investigation.
747      */
748     if((dc->bitsPerPixel == 1) && ((dc->textColor & 0xffffff) == 0))
749         render_op = PictOpOutReverse; /* This gives us 'black' text */
750     
751     for(idx = 0; idx < count; idx++) {
752         if(wstr[idx] >= physDev->xrender->cacheEntry->nrealized ||
753            physDev->xrender->cacheEntry->realized[wstr[idx]] == FALSE) {
754             UploadGlyph(dc, wstr[idx]);
755         }
756     }
757
758
759     TRACE("Writing %s at %d,%d\n", debugstr_wn(wstr,count), dc->DCOrgX + x,
760           dc->DCOrgY + y);
761     if(!lpDx)
762         TSXRenderCompositeString16(gdi_display, render_op, 
763                                    physDev->xrender->tile_pict,
764                                    physDev->xrender->pict,
765                                    physDev->xrender->cacheEntry->font_format,
766                                    physDev->xrender->cacheEntry->glyphset,
767                                    0, 0, dc->DCOrgX + x, dc->DCOrgY + y, wstr,
768                                    count);
769
770     else {
771         INT offset = 0, xoff = 0, yoff = 0;
772         for(idx = 0; idx < count; idx++) {
773             TSXRenderCompositeString16(gdi_display, render_op, 
774                                        physDev->xrender->tile_pict,
775                                        physDev->xrender->pict,
776                                        physDev->xrender->cacheEntry->font_format,
777                                        physDev->xrender->cacheEntry->glyphset,
778                                        0, 0, dc->DCOrgX + x + xoff,
779                                        dc->DCOrgY + y + yoff,
780                                        wstr + idx, 1);
781             offset += INTERNAL_XWSTODS(dc, lpDx[idx]);
782             xoff = offset * cosEsc;
783             yoff = offset * sinEsc;
784         }
785     }
786
787     if(physDev->xrender->pict) {
788         TSXRenderFreePicture(gdi_display, physDev->xrender->pict);
789     }
790     physDev->xrender->pict = 0;
791
792
793     if (flags & ETO_CLIPPED) 
794         RestoreVisRgn16( dc->hSelf );
795
796     X11DRV_UnlockDIBSection( dc, TRUE );
797     return TRUE;
798 }
799
800 #else /* #ifdef HAVE_LIBXRENDER */
801
802 void X11DRV_XRender_Init(void)
803 {
804     TRACE("XRender support not compiled in.\n");
805     return;
806 }
807
808 void X11DRV_XRender_Finalize(void)
809 {
810   assert(0);
811   return;
812 }
813
814 BOOL X11DRV_XRender_SelectFont(DC *dc, HFONT hfont)
815 {
816   assert(0);
817   return FALSE;
818 }
819
820 void X11DRV_XRender_DeleteDC(DC *dc)
821 {
822   assert(0);
823   return;
824 }
825
826 BOOL X11DRV_XRender_ExtTextOut( DC *dc, INT x, INT y, UINT flags,
827                                 const RECT *lprect, LPCWSTR wstr, UINT count,
828                                 const INT *lpDx )
829 {
830   assert(0);
831   return FALSE;
832 }
833
834 void X11DRV_XRender_UpdateDrawable(DC *dc)
835 {
836   assert(0);
837   return;
838 }
839
840 #endif