Authors: David Hammerton <david@transgaming.com>, Peter Hunnisett <peter@transgaming...
[wine.git] / dlls / x11drv / xrender.c
blobb29b2b968d585e1297dbffbe4549237f877c5ad7
1 /*
2 * Functions to use the XRender extension
4 * Copyright 2001 Huw D M Davies for CodeWeavers
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.
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.
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
20 #include "config.h"
21 #include "wine/port.h"
23 #include <assert.h>
24 #include <string.h>
25 #include <stdlib.h>
27 #include "winnt.h"
28 #include "x11drv.h"
29 #include "bitmap.h"
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
33 BOOL X11DRV_XRender_Installed = FALSE;
34 WINE_DEFAULT_DEBUG_CHANNEL(xrender);
36 #ifdef HAVE_X11_EXTENSIONS_XRENDER_H
38 #include "ts_xlib.h"
39 #include <X11/extensions/Xrender.h>
41 static XRenderPictFormat *screen_format; /* format of screen */
42 static XRenderPictFormat *mono_format; /* format of mono bitmap */
44 typedef struct
46 LOGFONTW lf;
47 XFORM xform; /* this is dum as we don't care about offsets */
48 DWORD hash;
49 } LFANDSIZE;
51 #define INITIAL_REALIZED_BUF_SIZE 128
54 typedef struct
56 LFANDSIZE lfsz;
57 GlyphSet glyphset;
58 XRenderPictFormat *font_format;
59 int nrealized;
60 BOOL *realized;
61 UINT count;
62 INT next;
63 } gsCacheEntry;
65 struct tagXRENDERINFO
67 gsCacheEntry *cacheEntry;
68 Picture pict;
69 Picture tile_pict;
70 Pixmap tile_xpm;
71 COLORREF lastTextColor;
75 static gsCacheEntry *glyphsetCache = NULL;
76 static DWORD glyphsetCacheSize = 0;
77 static INT lastfree = -1;
78 static INT mru = -1;
80 #define INIT_CACHE_SIZE 10
82 static int antialias = 1;
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
95 static void *xrender_handle;
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
113 /***********************************************************************
114 * X11DRV_XRender_Init
116 * Let's see if our XServer has the extension available
119 void X11DRV_XRender_Init(void)
121 int error_base, event_base, i;
122 XRenderPictFormat pf;
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;
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
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;
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;
172 glyphsetCache = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
173 sizeof(*glyphsetCache) * INIT_CACHE_SIZE);
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;
181 glyphsetCache[i-1].next = -1;
182 } else {
183 TRACE("Xrender is not available on this server\n");
185 wine_tsx11_unlock();
186 return;
188 sym_not_found:
189 wine_dlclose(xrender_handle, NULL, 0);
190 xrender_handle = NULL;
193 static BOOL fontcmp(LFANDSIZE *p1, LFANDSIZE *p2)
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);
201 #if 0
202 static void walk_cache(void)
204 int i;
206 for(i=mru; i >= 0; i = glyphsetCache[i].next)
207 TRACE("item %d\n", i);
209 #endif
211 static gsCacheEntry *LookupEntry(LFANDSIZE *plfsz)
213 int i, prev_i = -1;
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;
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;
229 TRACE("found font in cache %d\n", i);
230 return glyphsetCache + i;
232 prev_i = i;
234 TRACE("font not in cache\n");
235 return NULL;
238 static gsCacheEntry *AllocEntry(void)
240 int best = -1, prev_best = -1, i, prev_i = -1;
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;
251 TRACE("empty space at %d, next lastfree = %d\n", mru, lastfree);
252 return glyphsetCache + mru;
255 for(i = mru; i >= 0; i = glyphsetCache[i].next) {
256 if(glyphsetCache[i].count == 0) {
257 best = i;
258 prev_best = prev_i;
260 prev_i = i;
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;
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);
282 return glyphsetCache + mru;
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;
295 glyphsetCache[i-1].next = -1;
296 glyphsetCacheSize += INIT_CACHE_SIZE;
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;
306 static gsCacheEntry *GetCacheEntry(LFANDSIZE *plfsz)
308 XRenderPictFormat pf;
309 gsCacheEntry *ret;
311 if((ret = LookupEntry(plfsz)) != NULL) return ret;
313 ret = AllocEntry();
314 ret->lfsz = *plfsz;
315 assert(ret->nrealized == 0);
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;
325 pf.type = PictTypeDirect;
326 pf.direct.alpha = 0;
328 wine_tsx11_lock();
329 ret->font_format = pXRenderFindFormat(gdi_display,
330 PictFormatType |
331 PictFormatDepth |
332 PictFormatAlpha |
333 PictFormatAlphaMask,
334 &pf, 0);
336 ret->glyphset = pXRenderCreateGlyphSet(gdi_display, ret->font_format);
337 wine_tsx11_unlock();
338 return ret;
341 static void dec_ref_cache(gsCacheEntry *entry)
343 TRACE("dec'ing entry %d to %d\n", entry - glyphsetCache, entry->count - 1);
344 assert(entry->count > 0);
345 entry->count--;
348 static void lfsz_calc_hash(LFANDSIZE *plfsz)
350 DWORD hash = 0, *ptr;
351 int i;
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;
364 plfsz->hash = hash;
365 return;
368 /***********************************************************************
369 * X11DRV_XRender_Finalize
371 void X11DRV_XRender_Finalize(void)
373 FIXME("Free cached glyphsets\n");
374 if (xrender_handle) wine_dlclose(xrender_handle, NULL, 0);
375 xrender_handle = NULL;
379 /***********************************************************************
380 * X11DRV_XRender_SelectFont
382 BOOL X11DRV_XRender_SelectFont(X11DRV_PDEVICE *physDev, HFONT hfont)
384 LFANDSIZE lfsz;
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);
393 if(!physDev->xrender)
394 physDev->xrender = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
395 sizeof(*physDev->xrender));
397 else if(physDev->xrender->cacheEntry)
398 dec_ref_cache(physDev->xrender->cacheEntry);
399 physDev->xrender->cacheEntry = GetCacheEntry(&lfsz);
400 return 0;
403 /***********************************************************************
404 * X11DRV_XRender_DeleteDC
406 void X11DRV_XRender_DeleteDC(X11DRV_PDEVICE *physDev)
408 wine_tsx11_lock();
409 if(physDev->xrender->tile_pict)
410 pXRenderFreePicture(gdi_display, physDev->xrender->tile_pict);
412 if(physDev->xrender->tile_xpm)
413 XFreePixmap(gdi_display, physDev->xrender->tile_xpm);
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);
419 wine_tsx11_unlock();
421 if(physDev->xrender->cacheEntry)
422 dec_ref_cache(physDev->xrender->cacheEntry);
424 HeapFree(GetProcessHeap(), 0, physDev->xrender);
425 physDev->xrender = NULL;
426 return;
429 /***********************************************************************
430 * X11DRV_XRender_UpdateDrawable
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.
436 void X11DRV_XRender_UpdateDrawable(X11DRV_PDEVICE *physDev)
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();
444 physDev->xrender->pict = 0;
445 return;
448 static BOOL UploadGlyph(X11DRV_PDEVICE *physDev, int glyph)
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;
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));
466 entry->realized[glyph] = TRUE;
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;
476 buflen = GetGlyphOutlineW(physDev->hdc, glyph, ggo_format, &gm, 0, NULL,
477 NULL);
478 if(buflen == GDI_ERROR)
479 return FALSE;
481 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buflen);
482 GetGlyphOutlineW(physDev->hdc, glyph, ggo_format, &gm, buflen, buf, NULL);
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);
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;
496 if(TRACE_ON(xrender)) {
497 int pitch, i, j;
498 char output[300];
499 unsigned char *line;
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)))) ? "#" : " ");
509 strcat(output, "\n");
510 TRACE(output);
512 } else {
513 char blks[] = " .:;!o*#";
514 char str[2];
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);
525 strcat(output, "\n");
526 TRACE(output);
531 if(!aa && BitmapBitOrder(gdi_display) != MSBFirst) {
532 unsigned char *byte = buf, c;
533 int i = buflen;
535 while(i--) {
536 c = *byte;
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);
543 *byte++ = c;
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;
555 /***********************************************************************
556 * X11DRV_XRender_ExtTextOut
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 )
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;
578 TRACE("%04x, %d, %d, %08x, %p, %s, %d, %p)\n", hdc, x, y, flags,
579 lprect, debugstr_wn(wstr, count), count, lpDx);
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);
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);
593 if(dc->textAlign & TA_UPDATECP) {
594 x = dc->CursPosX;
595 y = dc->CursPosY;
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;
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;
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);
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;}
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 );
635 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
637 if(flags & ETO_OPAQUE) {
638 TSXSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
639 TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
640 physDev->org.x + rc.left, physDev->org.y + rc.top,
641 rc.right - rc.left, rc.bottom - rc.top );
644 if(count == 0) {
645 X11DRV_UnlockDIBSection( physDev, TRUE );
646 return TRUE;
649 x = INTERNAL_XWPTODP( dc, x, y );
650 y = INTERNAL_YWPTODP( dc, x, y );
652 TRACE("real x,y %d,%d\n", x, y);
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;
663 width = sz.cx;
665 width = INTERNAL_XWSTODS(dc, width);
666 xwidth = width * cosEsc;
667 ywidth = width * sinEsc;
669 GetTextMetricsW(hdc, &tm);
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 );
679 break;
681 case TA_CENTER:
682 x -= xwidth / 2;
683 y += ywidth / 2;
684 break;
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 );
693 break;
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;
702 case TA_BOTTOM:
703 y -= tm.tmDescent * cosEsc;
704 x -= tm.tmDescent * sinEsc;
705 break;
707 case TA_BASELINE:
708 break;
711 if (flags & ETO_CLIPPED)
713 SaveVisRgn16( hdc );
714 IntersectVisRect16( dc->hSelf, lprect->left, lprect->top, lprect->right, lprect->bottom );
717 if(!physDev->xrender->pict) {
718 XRenderPictureAttributes pa;
719 pa.subwindow_mode = IncludeInferiors;
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();
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);
734 if ((data = X11DRV_GetRegionData( dc->hGCClipRgn, 0 )))
736 wine_tsx11_lock();
737 pXRenderSetPictureClipRectangles( gdi_display, physDev->xrender->pict,
738 physDev->org.x, physDev->org.y,
739 (XRectangle *)data->Buffer, data->rdh.nCount );
740 wine_tsx11_unlock();
741 HeapFree( GetProcessHeap(), 0, data );
744 if(GetBkMode(hdc) != TRANSPARENT) {
745 if(!((flags & ETO_CLIPPED) && (flags & ETO_OPAQUE))) {
746 if(!(flags & ETO_OPAQUE) || x < rc.left || x + width >= rc.right ||
747 y - tm.tmAscent < rc.top || y + tm.tmDescent >= rc.bottom) {
748 TSXSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
749 TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
750 physDev->org.x + x, physDev->org.y + y - tm.tmAscent,
751 width, tm.tmAscent + tm.tmDescent );
756 /* Create a 1x1 pixmap to tile over the font mask */
757 if(!physDev->xrender->tile_xpm) {
758 XRenderPictureAttributes pa;
760 XRenderPictFormat *format = (dc->bitsPerPixel == 1) ? mono_format : screen_format;
761 wine_tsx11_lock();
762 physDev->xrender->tile_xpm = XCreatePixmap(gdi_display,
763 physDev->drawable,
764 1, 1,
765 format->depth);
766 pa.repeat = True;
767 physDev->xrender->tile_pict = pXRenderCreatePicture(gdi_display,
768 physDev->xrender->tile_xpm,
769 format,
770 CPRepeat, &pa);
771 wine_tsx11_unlock();
772 TRACE("Created pixmap of depth %d\n", format->depth);
773 /* init lastTextColor to something different from dc->textColor */
774 physDev->xrender->lastTextColor = ~dc->textColor;
778 if(dc->textColor != physDev->xrender->lastTextColor) {
779 if(dc->bitsPerPixel != 1) {
780 /* Map 0 -- 0xff onto 0 -- 0xffff */
781 col.red = GetRValue(dc->textColor);
782 col.red |= col.red << 8;
783 col.green = GetGValue(dc->textColor);
784 col.green |= col.green << 8;
785 col.blue = GetBValue(dc->textColor);
786 col.blue |= col.blue << 8;
787 col.alpha = 0x0;
788 } else { /* for a 1bpp bitmap we always need a 1 in the tile */
789 col.red = col.green = col.blue = 0;
790 col.alpha = 0xffff;
792 wine_tsx11_lock();
793 pXRenderFillRectangle(gdi_display, PictOpSrc,
794 physDev->xrender->tile_pict,
795 &col, 0, 0, 1, 1);
796 wine_tsx11_unlock();
797 physDev->xrender->lastTextColor = dc->textColor;
800 /* FIXME the mapping of Text/BkColor onto 1 or 0 needs investigation.
802 if((dc->bitsPerPixel == 1) && ((dc->textColor & 0xffffff) == 0))
803 render_op = PictOpOutReverse; /* This gives us 'black' text */
805 for(idx = 0; idx < count; idx++) {
806 if(glyphs[idx] >= physDev->xrender->cacheEntry->nrealized ||
807 physDev->xrender->cacheEntry->realized[glyphs[idx]] == FALSE) {
808 UploadGlyph(physDev, glyphs[idx]);
813 TRACE("Writing %s at %ld,%ld\n", debugstr_wn(wstr,count),
814 physDev->org.x + x, physDev->org.y + y);
816 wine_tsx11_lock();
817 if(!lpDx)
818 pXRenderCompositeString16(gdi_display, render_op,
819 physDev->xrender->tile_pict,
820 physDev->xrender->pict,
821 physDev->xrender->cacheEntry->font_format,
822 physDev->xrender->cacheEntry->glyphset,
823 0, 0, physDev->org.x + x, physDev->org.y + y,
824 glyphs, count);
826 else {
827 INT offset = 0, xoff = 0, yoff = 0;
828 for(idx = 0; idx < count; idx++) {
829 pXRenderCompositeString16(gdi_display, render_op,
830 physDev->xrender->tile_pict,
831 physDev->xrender->pict,
832 physDev->xrender->cacheEntry->font_format,
833 physDev->xrender->cacheEntry->glyphset,
834 0, 0, physDev->org.x + x + xoff,
835 physDev->org.y + y + yoff,
836 glyphs + idx, 1);
837 offset += INTERNAL_XWSTODS(dc, lpDx[idx]);
838 xoff = offset * cosEsc;
839 yoff = offset * -sinEsc;
843 if(physDev->xrender->pict) {
844 pXRenderFreePicture(gdi_display, physDev->xrender->pict);
846 physDev->xrender->pict = 0;
847 wine_tsx11_unlock();
849 if (flags & ETO_CLIPPED)
850 RestoreVisRgn16( hdc );
852 X11DRV_UnlockDIBSection( physDev, TRUE );
853 if(glyphs != wstr) HeapFree(GetProcessHeap(), 0, glyphs);
854 return TRUE;
857 #else /* HAVE_X11_EXTENSIONS_XRENDER_H */
859 void X11DRV_XRender_Init(void)
861 TRACE("XRender support not compiled in.\n");
862 return;
865 void X11DRV_XRender_Finalize(void)
869 BOOL X11DRV_XRender_SelectFont(X11DRV_PDEVICE *physDev, HFONT hfont)
871 assert(0);
872 return FALSE;
875 void X11DRV_XRender_DeleteDC(X11DRV_PDEVICE *physDev)
877 assert(0);
878 return;
881 BOOL X11DRV_XRender_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
882 const RECT *lprect, LPCWSTR wstr, UINT count,
883 const INT *lpDx )
885 assert(0);
886 return FALSE;
889 void X11DRV_XRender_UpdateDrawable(X11DRV_PDEVICE *physDev)
891 assert(0);
892 return;
895 #endif /* HAVE_X11_EXTENSIONS_XRENDER_H */