2 * Functions to use the XRender extension
4 * Copyright 2001, 2002 Huw D M Davies for CodeWeavers
7 * Copyright 2000 Keith Packard, member of The XFree86 Project, Inc.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/port.h"
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
37 static BOOL X11DRV_XRender_Installed = FALSE;
38 int using_client_side_fonts = FALSE;
40 WINE_DEFAULT_DEBUG_CHANNEL(xrender);
42 #ifdef HAVE_X11_EXTENSIONS_XRENDER_H
45 #include <X11/extensions/Xrender.h>
47 static XRenderPictFormat *screen_format; /* format of screen */
48 static XRenderPictFormat *mono_format; /* format of mono bitmap */
53 XFORM xform; /* this is dum as we don't care about offsets */
57 #define INITIAL_REALIZED_BUF_SIZE 128
59 typedef enum { AA_None, AA_Grey, AA_RGB, AA_BGR, AA_VRGB, AA_VBGR } AA_Type;
66 XRenderPictFormat *font_format;
81 COLORREF lastTextColor;
85 static gsCacheEntry *glyphsetCache = NULL;
86 static DWORD glyphsetCacheSize = 0;
87 static INT lastfree = -1;
90 #define INIT_CACHE_SIZE 10
92 static int antialias = 1;
94 /* some default values just in case */
96 #define SONAME_LIBX11 "libX11.so"
98 #ifndef SONAME_LIBXEXT
99 #define SONAME_LIBXEXT "libXext.so"
101 #ifndef SONAME_LIBXRENDER
102 #define SONAME_LIBXRENDER "libXrender.so"
105 static void *xrender_handle;
107 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
108 MAKE_FUNCPTR(XRenderAddGlyphs)
109 MAKE_FUNCPTR(XRenderCompositeString8)
110 MAKE_FUNCPTR(XRenderCompositeString16)
111 MAKE_FUNCPTR(XRenderCompositeString32)
112 MAKE_FUNCPTR(XRenderCreateGlyphSet)
113 MAKE_FUNCPTR(XRenderCreatePicture)
114 MAKE_FUNCPTR(XRenderFillRectangle)
115 MAKE_FUNCPTR(XRenderFindFormat)
116 MAKE_FUNCPTR(XRenderFindVisualFormat)
117 MAKE_FUNCPTR(XRenderFreeGlyphSet)
118 MAKE_FUNCPTR(XRenderFreePicture)
119 MAKE_FUNCPTR(XRenderSetPictureClipRectangles)
120 MAKE_FUNCPTR(XRenderQueryExtension)
123 static CRITICAL_SECTION xrender_cs = CRITICAL_SECTION_INIT("xrender_cs");
126 /***********************************************************************
127 * X11DRV_XRender_Init
129 * Let's see if our XServer has the extension available
132 void X11DRV_XRender_Init(void)
134 int error_base, event_base, i;
135 XRenderPictFormat pf;
137 if (client_side_with_render &&
138 wine_dlopen(SONAME_LIBX11, RTLD_NOW|RTLD_GLOBAL, NULL, 0) &&
139 wine_dlopen(SONAME_LIBXEXT, RTLD_NOW|RTLD_GLOBAL, NULL, 0) &&
140 (xrender_handle = wine_dlopen(SONAME_LIBXRENDER, RTLD_NOW, NULL, 0)))
143 #define LOAD_FUNCPTR(f) if((p##f = wine_dlsym(xrender_handle, #f, NULL, 0)) == NULL) goto sym_not_found;
144 LOAD_FUNCPTR(XRenderAddGlyphs)
145 LOAD_FUNCPTR(XRenderCompositeString8)
146 LOAD_FUNCPTR(XRenderCompositeString16)
147 LOAD_FUNCPTR(XRenderCompositeString32)
148 LOAD_FUNCPTR(XRenderCreateGlyphSet)
149 LOAD_FUNCPTR(XRenderCreatePicture)
150 LOAD_FUNCPTR(XRenderFillRectangle)
151 LOAD_FUNCPTR(XRenderFindFormat)
152 LOAD_FUNCPTR(XRenderFindVisualFormat)
153 LOAD_FUNCPTR(XRenderFreeGlyphSet)
154 LOAD_FUNCPTR(XRenderFreePicture)
155 LOAD_FUNCPTR(XRenderSetPictureClipRectangles)
156 LOAD_FUNCPTR(XRenderQueryExtension)
160 if(pXRenderQueryExtension(gdi_display, &event_base, &error_base)) {
161 X11DRV_XRender_Installed = TRUE;
162 TRACE("Xrender is up and running error_base = %d\n", error_base);
163 screen_format = pXRenderFindVisualFormat(gdi_display, visual);
164 if(!screen_format) { /* This fails in buggy versions of libXrender.so */
167 "Wine has detected that you probably have a buggy version\n"
168 "of libXrender.so . Because of this client side font rendering\n"
169 "will be disabled. Please upgrade this library.\n");
170 X11DRV_XRender_Installed = FALSE;
173 pf.type = PictTypeDirect;
176 pf.direct.alphaMask = 1;
177 mono_format = pXRenderFindFormat(gdi_display, PictFormatType |
178 PictFormatDepth | PictFormatAlpha |
179 PictFormatAlphaMask, &pf, 0);
181 ERR("mono_format == NULL?\n");
182 X11DRV_XRender_Installed = FALSE;
189 if(X11DRV_XRender_Installed || client_side_with_core)
191 glyphsetCache = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
192 sizeof(*glyphsetCache) * INIT_CACHE_SIZE);
194 glyphsetCacheSize = INIT_CACHE_SIZE;
196 for(i = 0; i < INIT_CACHE_SIZE; i++) {
197 glyphsetCache[i].next = i + 1;
198 glyphsetCache[i].count = -1;
200 glyphsetCache[i-1].next = -1;
201 using_client_side_fonts = 1;
203 if(!X11DRV_XRender_Installed) {
204 TRACE("Xrender is not available on your XServer, client side rendering with the core protocol instead.\n");
205 if(screen_depth <= 8 || !client_side_antialias_with_core)
208 if(screen_depth <= 8 || !client_side_antialias_with_render)
212 else TRACE("Using X11 core fonts\n");
215 static BOOL fontcmp(LFANDSIZE *p1, LFANDSIZE *p2)
217 if(p1->hash != p2->hash) return TRUE;
218 if(memcmp(&p1->xform, &p2->xform, sizeof(p1->xform))) return TRUE;
219 if(memcmp(&p1->lf, &p2->lf, offsetof(LOGFONTW, lfFaceName))) return TRUE;
220 return strcmpW(p1->lf.lfFaceName, p2->lf.lfFaceName);
224 static void walk_cache(void)
228 EnterCriticalSection(&xrender_cs);
229 for(i=mru; i >= 0; i = glyphsetCache[i].next)
230 TRACE("item %d\n", i);
231 LeaveCriticalSection(&xrender_cs);
235 static int LookupEntry(LFANDSIZE *plfsz)
239 for(i = mru; i >= 0; i = glyphsetCache[i].next) {
241 if(glyphsetCache[i].count == -1) { /* reached free list so stop */
246 if(!fontcmp(&glyphsetCache[i].lfsz, plfsz)) {
247 glyphsetCache[i].count++;
249 glyphsetCache[prev_i].next = glyphsetCache[i].next;
250 glyphsetCache[i].next = mru;
253 TRACE("found font in cache %d\n", i);
258 TRACE("font not in cache\n");
262 static void FreeEntry(int entry)
266 if(glyphsetCache[entry].glyphset) {
268 pXRenderFreeGlyphSet(gdi_display, glyphsetCache[entry].glyphset);
270 glyphsetCache[entry].glyphset = 0;
272 if(glyphsetCache[entry].nrealized) {
273 HeapFree(GetProcessHeap(), 0, glyphsetCache[entry].realized);
274 glyphsetCache[entry].realized = NULL;
275 if(glyphsetCache[entry].bitmaps) {
276 for(i = 0; i < glyphsetCache[entry].nrealized; i++)
277 if(glyphsetCache[entry].bitmaps[i])
278 HeapFree(GetProcessHeap(), 0, glyphsetCache[entry].bitmaps[i]);
279 HeapFree(GetProcessHeap(), 0, glyphsetCache[entry].bitmaps);
280 glyphsetCache[entry].bitmaps = NULL;
281 HeapFree(GetProcessHeap(), 0, glyphsetCache[entry].gis);
282 glyphsetCache[entry].gis = NULL;
284 glyphsetCache[entry].nrealized = 0;
288 static int AllocEntry(void)
290 int best = -1, prev_best = -1, i, prev_i = -1;
293 assert(glyphsetCache[lastfree].count == -1);
294 glyphsetCache[lastfree].count = 1;
296 lastfree = glyphsetCache[lastfree].next;
298 glyphsetCache[best].next = mru;
301 TRACE("empty space at %d, next lastfree = %d\n", mru, lastfree);
305 for(i = mru; i >= 0; i = glyphsetCache[i].next) {
306 if(glyphsetCache[i].count == 0) {
314 TRACE("freeing unused glyphset at cache %d\n", best);
316 glyphsetCache[best].count = 1;
318 glyphsetCache[prev_best].next = glyphsetCache[best].next;
319 glyphsetCache[best].next = mru;
327 TRACE("Growing cache\n");
328 glyphsetCache = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
330 (glyphsetCacheSize + INIT_CACHE_SIZE)
331 * sizeof(*glyphsetCache));
332 for(best = i = glyphsetCacheSize; i < glyphsetCacheSize + INIT_CACHE_SIZE;
334 glyphsetCache[i].next = i + 1;
335 glyphsetCache[i].count = -1;
337 glyphsetCache[i-1].next = -1;
338 glyphsetCacheSize += INIT_CACHE_SIZE;
340 lastfree = glyphsetCache[best].next;
341 glyphsetCache[best].count = 1;
342 glyphsetCache[best].next = mru;
344 TRACE("new free cache slot at %d\n", mru);
348 static int GetCacheEntry(LFANDSIZE *plfsz)
350 XRenderPictFormat pf;
354 if((ret = LookupEntry(plfsz)) != -1) return ret;
357 entry = glyphsetCache + ret;
358 entry->lfsz = *plfsz;
359 assert(entry->nrealized == 0);
366 if(X11DRV_XRender_Installed) {
370 pf.direct.alphaMask = 0xff;
374 ERR("aa = %d - not implemented\n", entry->aa);
377 pf.direct.alphaMask = 1;
381 pf.type = PictTypeDirect;
385 entry->font_format = pXRenderFindFormat(gdi_display,
392 entry->glyphset = pXRenderCreateGlyphSet(gdi_display, entry->font_format);
398 static void dec_ref_cache(int index)
401 TRACE("dec'ing entry %d to %d\n", index, glyphsetCache[index].count - 1);
402 assert(glyphsetCache[index].count > 0);
403 glyphsetCache[index].count--;
406 static void lfsz_calc_hash(LFANDSIZE *plfsz)
408 DWORD hash = 0, *ptr;
411 for(ptr = (DWORD*)&plfsz->xform; ptr < (DWORD*)(&plfsz->xform + 1); ptr++)
413 for(i = 0, ptr = (DWORD*)&plfsz->lf; i < 7; i++, ptr++)
415 for(i = 0, ptr = (DWORD*)&plfsz->lf.lfFaceName; i < LF_FACESIZE/2; i++, ptr++) {
416 WCHAR *pwc = (WCHAR *)ptr;
426 /***********************************************************************
427 * X11DRV_XRender_Finalize
429 void X11DRV_XRender_Finalize(void)
433 EnterCriticalSection(&xrender_cs);
434 for(i = mru; i >= 0; i = glyphsetCache[i].next)
436 LeaveCriticalSection(&xrender_cs);
440 /***********************************************************************
441 * X11DRV_XRender_SelectFont
443 BOOL X11DRV_XRender_SelectFont(X11DRV_PDEVICE *physDev, HFONT hfont)
447 GetObjectW(hfont, sizeof(lfsz.lf), &lfsz.lf);
448 TRACE("h=%ld w=%ld weight=%ld it=%d charset=%d name=%s\n",
449 lfsz.lf.lfHeight, lfsz.lf.lfWidth, lfsz.lf.lfWeight,
450 lfsz.lf.lfItalic, lfsz.lf.lfCharSet, debugstr_w(lfsz.lf.lfFaceName));
451 lfsz.xform = physDev->dc->xformWorld2Vport;
452 lfsz_calc_hash(&lfsz);
454 EnterCriticalSection(&xrender_cs);
455 if(!physDev->xrender) {
456 physDev->xrender = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
457 sizeof(*physDev->xrender));
458 physDev->xrender->cache_index = -1;
460 else if(physDev->xrender->cache_index != -1)
461 dec_ref_cache(physDev->xrender->cache_index);
462 physDev->xrender->cache_index = GetCacheEntry(&lfsz);
463 LeaveCriticalSection(&xrender_cs);
467 /***********************************************************************
468 * X11DRV_XRender_DeleteDC
470 void X11DRV_XRender_DeleteDC(X11DRV_PDEVICE *physDev)
473 if(physDev->xrender->tile_pict)
474 pXRenderFreePicture(gdi_display, physDev->xrender->tile_pict);
476 if(physDev->xrender->tile_xpm)
477 XFreePixmap(gdi_display, physDev->xrender->tile_xpm);
479 if(physDev->xrender->pict) {
480 TRACE("freeing pict = %lx dc = %p\n", physDev->xrender->pict, physDev->dc);
481 pXRenderFreePicture(gdi_display, physDev->xrender->pict);
485 EnterCriticalSection(&xrender_cs);
486 if(physDev->xrender->cache_index != -1)
487 dec_ref_cache(physDev->xrender->cache_index);
488 LeaveCriticalSection(&xrender_cs);
490 HeapFree(GetProcessHeap(), 0, physDev->xrender);
491 physDev->xrender = NULL;
495 /***********************************************************************
496 * X11DRV_XRender_UpdateDrawable
498 * This gets called from X11DRV_SetDrawable and X11DRV_SelectBitmap.
499 * It deletes the pict when the drawable changes.
501 void X11DRV_XRender_UpdateDrawable(X11DRV_PDEVICE *physDev)
503 if(physDev->xrender->pict) {
504 TRACE("freeing pict %08lx from dc %p drawable %08lx\n", physDev->xrender->pict,
505 physDev->dc, physDev->drawable);
508 pXRenderFreePicture(gdi_display, physDev->xrender->pict);
511 physDev->xrender->pict = 0;
515 /************************************************************************
518 * Helper to ExtTextOut. Must be called inside xrender_cs
520 static BOOL UploadGlyph(X11DRV_PDEVICE *physDev, int glyph)
527 gsCacheEntry *entry = glyphsetCache + physDev->xrender->cache_index;
528 UINT ggo_format = GGO_GLYPH_INDEX;
530 if(entry->nrealized <= glyph) {
531 entry->nrealized = (glyph / 128 + 1) * 128;
532 entry->realized = HeapReAlloc(GetProcessHeap(),
535 entry->nrealized * sizeof(BOOL));
536 if(entry->glyphset == 0) {
537 entry->bitmaps = HeapReAlloc(GetProcessHeap(),
540 entry->nrealized * sizeof(entry->bitmaps[0]));
541 entry->gis = HeapReAlloc(GetProcessHeap(),
544 entry->nrealized * sizeof(entry->gis[0]));
550 ggo_format |= WINE_GGO_GRAY16_BITMAP;
554 ERR("aa = %d - not implemented\n", entry->aa);
556 ggo_format |= GGO_BITMAP;
560 buflen = GetGlyphOutlineW(physDev->hdc, glyph, ggo_format, &gm, 0, NULL,
562 if(buflen == GDI_ERROR) {
563 LeaveCriticalSection(&xrender_cs);
567 entry->realized[glyph] = TRUE;
569 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buflen);
570 GetGlyphOutlineW(physDev->hdc, glyph, ggo_format, &gm, buflen, buf, NULL);
572 TRACE("buflen = %d. Got metrics: %dx%d adv=%d,%d origin=%ld,%ld\n",
574 gm.gmBlackBoxX, gm.gmBlackBoxY, gm.gmCellIncX, gm.gmCellIncY,
575 gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
577 gi.width = gm.gmBlackBoxX;
578 gi.height = gm.gmBlackBoxY;
579 gi.x = -gm.gmptGlyphOrigin.x;
580 gi.y = gm.gmptGlyphOrigin.y;
581 gi.xOff = gm.gmCellIncX;
582 gi.yOff = gm.gmCellIncY;
584 if(TRACE_ON(xrender)) {
589 if(entry->aa == AA_None) {
590 pitch = ((gi.width + 31) / 32) * 4;
591 for(i = 0; i < gi.height; i++) {
592 line = buf + i * pitch;
594 for(j = 0; j < pitch * 8; j++) {
595 strcat(output, (line[j / 8] & (1 << (7 - (j % 8)))) ? "#" : " ");
597 strcat(output, "\n");
601 char blks[] = " .:;!o*#";
605 pitch = ((gi.width + 3) / 4) * 4;
606 for(i = 0; i < gi.height; i++) {
607 line = buf + i * pitch;
609 for(j = 0; j < pitch; j++) {
610 str[0] = blks[line[j] >> 5];
613 strcat(output, "\n");
619 if(entry->glyphset) {
620 if(entry->aa == AA_None && BitmapBitOrder(gdi_display) != MSBFirst) {
621 unsigned char *byte = buf, c;
627 /* magic to flip bit order */
628 c = ((c << 1) & 0xaa) | ((c >> 1) & 0x55);
629 c = ((c << 2) & 0xcc) | ((c >> 2) & 0x33);
630 c = ((c << 4) & 0xf0) | ((c >> 4) & 0x0f);
637 pXRenderAddGlyphs(gdi_display, entry->glyphset, &gid, &gi, 1,
640 HeapFree(GetProcessHeap(), 0, buf);
642 entry->bitmaps[glyph] = buf;
643 memcpy(&entry->gis[glyph], &gi, sizeof(gi));
648 static void SharpGlyphMono(X11DRV_PDEVICE *physDev, INT x, INT y,
649 void *bitmap, XGlyphInfo *gi)
651 unsigned char *srcLine = bitmap, *src;
652 unsigned char bits, bitsMask;
653 int width = gi->width;
654 int stride = ((width + 31) & ~31) >> 3;
655 int height = gi->height;
659 TRACE("%d, %d\n", x, y);
668 bitsMask = 0x80; /* FreeType is always MSB first */
682 bitsMask = bitsMask >> 1;
688 } while (bits & bitsMask);
689 XFillRectangle (gdi_display, physDev->drawable,
690 physDev->gc, xspan, y, lenspan, 1);
702 bitsMask = bitsMask >> 1;
708 } while (!(bits & bitsMask));
715 static void SharpGlyphGray(X11DRV_PDEVICE *physDev, INT x, INT y,
716 void *bitmap, XGlyphInfo *gi)
718 unsigned char *srcLine = bitmap, *src, bits;
719 int width = gi->width;
720 int stride = ((width + 3) & ~3);
721 int height = gi->height;
746 } while (bits >= 0x80);
747 XFillRectangle (gdi_display, physDev->drawable,
748 physDev->gc, xspan, y, lenspan, 1);
761 } while (bits < 0x80);
769 static void ExamineBitfield (DWORD mask, int *shift, int *len)
774 while ((mask & 1) == 0)
780 while ((mask & 1) == 1)
789 static DWORD GetField (DWORD pixel, int shift, int len)
791 pixel = pixel & (((1 << (len)) - 1) << shift);
792 pixel = pixel << (32 - (shift + len)) >> 24;
795 pixel |= (pixel >> len);
802 static DWORD PutField (DWORD pixel, int shift, int len)
804 shift = shift - (8 - len);
806 pixel &= (((1 << len) - 1) << (8 - len));
814 static void SmoothGlyphGray(XImage *image, int x, int y, void *bitmap, XGlyphInfo *gi,
820 BYTE *maskLine, *mask, m;
825 BYTE src_r, src_g, src_b;
827 src_r = GetRValue(color);
828 src_g = GetGValue(color);
829 src_b = GetBValue(color);
836 maskLine = (unsigned char *) bitmap;
837 maskStride = (width + 3) & ~3;
839 ExamineBitfield (image->red_mask, &r_shift, &r_len);
840 ExamineBitfield (image->green_mask, &g_shift, &g_len);
841 ExamineBitfield (image->blue_mask, &b_shift, &b_len);
845 maskLine += maskStride;
850 if(y >= image->height) break;
854 if(tx >= image->width) break;
861 pixel = (PutField ((src_r), r_shift, r_len) |
862 PutField ((src_g), g_shift, g_len) |
863 PutField ((src_b), b_shift, b_len));
864 XPutPixel (image, tx, y, pixel);
870 pixel = XGetPixel (image, tx, y);
872 r = GetField(pixel, r_shift, r_len);
873 r = ((BYTE)~m * (WORD)r + (BYTE)m * (WORD)src_r) >> 8;
874 g = GetField(pixel, g_shift, g_len);
875 g = ((BYTE)~m * (WORD)g + (BYTE)m * (WORD)src_g) >> 8;
876 b = GetField(pixel, b_shift, b_len);
877 b = ((BYTE)~m * (WORD)b + (BYTE)m * (WORD)src_b) >> 8;
879 pixel = (PutField (r, r_shift, r_len) |
880 PutField (g, g_shift, g_len) |
881 PutField (b, b_shift, b_len));
882 XPutPixel (image, tx, y, pixel);
888 static int XRenderErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
893 /***********************************************************************
894 * X11DRV_XRender_ExtTextOut
896 BOOL X11DRV_XRender_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
897 const RECT *lprect, LPCWSTR wstr, UINT count,
906 BOOL done_extents = FALSE;
907 INT width, xwidth, ywidth;
908 double cosEsc, sinEsc;
911 int render_op = PictOpOver;
916 HDC hdc = physDev->hdc;
917 DC *dc = physDev->dc;
918 int textPixel, backgroundPixel;
922 TRACE("%p, %d, %d, %08x, %p, %s, %d, %p)\n", hdc, x, y, flags,
923 lprect, debugstr_wn(wstr, count), count, lpDx);
925 if(flags & ETO_GLYPH_INDEX)
926 glyphs = (LPWORD)wstr;
928 glyphs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
929 GetGlyphIndicesW(hdc, wstr, count, glyphs, 0);
933 TRACE("rect: %ld,%ld - %ld,%ld\n", lprect->left, lprect->top, lprect->right,
935 TRACE("align = %x bkmode = %x mapmode = %x\n", dc->textAlign, GetBkMode(hdc), dc->MapMode);
937 if(dc->textAlign & TA_UPDATECP) {
942 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
943 if(lf.lfEscapement != 0) {
944 cosEsc = cos(lf.lfEscapement * M_PI / 1800);
945 sinEsc = sin(lf.lfEscapement * M_PI / 1800);
951 if(flags & (ETO_CLIPPED | ETO_OPAQUE)) {
953 if(flags & ETO_CLIPPED) return FALSE;
954 GetTextExtentPointI(hdc, glyphs, count, &sz);
958 rc.right = x + sz.cx;
959 rc.bottom = y + sz.cy;
964 LPtoDP(physDev->hdc, (POINT*)&rc, 2);
966 if(rc.left > rc.right) {INT tmp = rc.left; rc.left = rc.right; rc.right = tmp;}
967 if(rc.top > rc.bottom) {INT tmp = rc.top; rc.top = rc.bottom; rc.bottom = tmp;}
970 xgcval.function = GXcopy;
971 xgcval.background = physDev->backgroundPixel;
972 xgcval.fill_style = FillSolid;
973 TSXChangeGC( gdi_display, physDev->gc,
974 GCFunction | GCBackground | GCFillStyle, &xgcval );
976 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
978 if(dc->bitsPerPixel == 1) {
979 if((dc->textColor & 0xffffff) == 0) {
987 textPixel = physDev->textPixel;
988 backgroundPixel = physDev->backgroundPixel;
991 if(flags & ETO_OPAQUE) {
992 TSXSetForeground( gdi_display, physDev->gc, backgroundPixel );
993 TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
994 physDev->org.x + rc.left, physDev->org.y + rc.top,
995 rc.right - rc.left, rc.bottom - rc.top );
1005 LPtoDP(physDev->hdc, &pt, 1);
1009 TRACE("real x,y %d,%d\n", x, y);
1011 if((char_extra = GetTextCharacterExtra(physDev->hdc)) != 0) {
1014 deltas = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
1015 for(i = 0; i < count; i++) {
1016 deltas[i] = char_extra;
1018 deltas[i] += lpDx[i];
1020 GetTextExtentPointI(hdc, glyphs + i, 1, &tmpsz);
1021 deltas[i] += tmpsz.cx;
1025 deltas = (INT*)lpDx;
1029 for(idx = 0; idx < count; idx++)
1030 width += deltas[idx];
1033 GetTextExtentPointI(hdc, glyphs, count, &sz);
1034 done_extents = TRUE;
1038 width = INTERNAL_XWSTODS(dc, width);
1039 xwidth = width * cosEsc;
1040 ywidth = width * sinEsc;
1042 GetTextMetricsW(hdc, &tm);
1044 tm.tmAscent = INTERNAL_YWSTODS(dc, tm.tmAscent);
1045 tm.tmDescent = INTERNAL_YWSTODS(dc, tm.tmDescent);
1046 switch( dc->textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) ) {
1048 if (dc->textAlign & TA_UPDATECP) {
1051 DPtoLP(physDev->hdc, &pt, 1);
1052 dc->CursPosX = pt.x;
1053 dc->CursPosY = pt.y;
1065 if (dc->textAlign & TA_UPDATECP) {
1068 DPtoLP(physDev->hdc, &pt, 1);
1069 dc->CursPosX = pt.x;
1070 dc->CursPosY = pt.y;
1075 switch( dc->textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) ) {
1077 y += tm.tmAscent * cosEsc;
1078 x += tm.tmAscent * sinEsc;
1082 y -= tm.tmDescent * cosEsc;
1083 x -= tm.tmDescent * sinEsc;
1090 if (flags & ETO_CLIPPED)
1092 SaveVisRgn16( HDC_16(hdc) );
1093 IntersectVisRect16( HDC_16(dc->hSelf), lprect->left, lprect->top, lprect->right, lprect->bottom );
1096 if(X11DRV_XRender_Installed) {
1097 if(!physDev->xrender->pict) {
1098 XRenderPictureAttributes pa;
1099 pa.subwindow_mode = IncludeInferiors;
1102 physDev->xrender->pict = pXRenderCreatePicture(gdi_display,
1104 (dc->bitsPerPixel == 1) ?
1105 mono_format : screen_format,
1106 CPSubwindowMode, &pa);
1107 wine_tsx11_unlock();
1109 TRACE("allocing pict = %lx dc = %p drawable = %08lx\n", physDev->xrender->pict, dc, physDev->drawable);
1111 TRACE("using existing pict = %lx dc = %p drawable = %08lx\n", physDev->xrender->pict, dc, physDev->drawable);
1114 if ((data = X11DRV_GetRegionData( dc->hGCClipRgn, 0 )))
1117 pXRenderSetPictureClipRectangles( gdi_display, physDev->xrender->pict,
1118 physDev->org.x, physDev->org.y,
1119 (XRectangle *)data->Buffer, data->rdh.nCount );
1120 wine_tsx11_unlock();
1121 HeapFree( GetProcessHeap(), 0, data );
1125 if(GetBkMode(hdc) != TRANSPARENT) {
1126 if(!((flags & ETO_CLIPPED) && (flags & ETO_OPAQUE))) {
1127 if(!(flags & ETO_OPAQUE) || x < rc.left || x + width >= rc.right ||
1128 y - tm.tmAscent < rc.top || y + tm.tmDescent >= rc.bottom) {
1129 TSXSetForeground( gdi_display, physDev->gc, backgroundPixel );
1130 TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
1131 physDev->org.x + x, physDev->org.y + y - tm.tmAscent,
1132 width, tm.tmAscent + tm.tmDescent );
1137 if(X11DRV_XRender_Installed) {
1138 /* Create a 1x1 pixmap to tile over the font mask */
1139 if(!physDev->xrender->tile_xpm) {
1140 XRenderPictureAttributes pa;
1142 XRenderPictFormat *format = (dc->bitsPerPixel == 1) ? mono_format : screen_format;
1144 physDev->xrender->tile_xpm = XCreatePixmap(gdi_display,
1149 physDev->xrender->tile_pict = pXRenderCreatePicture(gdi_display,
1150 physDev->xrender->tile_xpm,
1153 wine_tsx11_unlock();
1154 TRACE("Created pixmap of depth %d\n", format->depth);
1155 /* init lastTextColor to something different from dc->textColor */
1156 physDev->xrender->lastTextColor = ~dc->textColor;
1160 if(dc->textColor != physDev->xrender->lastTextColor) {
1161 if(dc->bitsPerPixel != 1) {
1162 /* Map 0 -- 0xff onto 0 -- 0xffff */
1163 col.red = GetRValue(dc->textColor);
1164 col.red |= col.red << 8;
1165 col.green = GetGValue(dc->textColor);
1166 col.green |= col.green << 8;
1167 col.blue = GetBValue(dc->textColor);
1168 col.blue |= col.blue << 8;
1170 } else { /* for a 1bpp bitmap we always need a 1 in the tile */
1171 col.red = col.green = col.blue = 0;
1175 pXRenderFillRectangle(gdi_display, PictOpSrc,
1176 physDev->xrender->tile_pict,
1178 wine_tsx11_unlock();
1179 physDev->xrender->lastTextColor = dc->textColor;
1182 /* FIXME the mapping of Text/BkColor onto 1 or 0 needs investigation.
1184 if((dc->bitsPerPixel == 1) && (textPixel == 0))
1185 render_op = PictOpOutReverse; /* This gives us 'black' text */
1188 EnterCriticalSection(&xrender_cs);
1189 entry = glyphsetCache + physDev->xrender->cache_index;
1191 for(idx = 0; idx < count; idx++) {
1192 if(glyphs[idx] >= entry->nrealized || entry->realized[glyphs[idx]] == FALSE) {
1193 UploadGlyph(physDev, glyphs[idx]);
1198 TRACE("Writing %s at %ld,%ld\n", debugstr_wn(wstr,count),
1199 physDev->org.x + x, physDev->org.y + y);
1201 if(X11DRV_XRender_Installed) {
1204 pXRenderCompositeString16(gdi_display, render_op,
1205 physDev->xrender->tile_pict,
1206 physDev->xrender->pict,
1207 entry->font_format, entry->glyphset,
1208 0, 0, physDev->org.x + x, physDev->org.y + y,
1212 INT offset = 0, xoff = 0, yoff = 0;
1213 for(idx = 0; idx < count; idx++) {
1214 pXRenderCompositeString16(gdi_display, render_op,
1215 physDev->xrender->tile_pict,
1216 physDev->xrender->pict,
1217 entry->font_format, entry->glyphset,
1218 0, 0, physDev->org.x + x + xoff,
1219 physDev->org.y + y + yoff,
1221 offset += INTERNAL_XWSTODS(dc, deltas[idx]);
1222 xoff = offset * cosEsc;
1223 yoff = offset * -sinEsc;
1226 wine_tsx11_unlock();
1229 INT offset = 0, xoff = 0, yoff = 0;
1231 XSetForeground( gdi_display, physDev->gc, textPixel );
1233 if(entry->aa == AA_None) {
1234 for(idx = 0; idx < count; idx++) {
1235 SharpGlyphMono(physDev, physDev->org.x + x + xoff,
1236 physDev->org.y + y + yoff,
1237 entry->bitmaps[glyphs[idx]],
1238 &entry->gis[glyphs[idx]]);
1240 offset += INTERNAL_XWSTODS(dc, deltas[idx]);
1241 xoff = offset * cosEsc;
1242 yoff = offset * -sinEsc;
1245 xoff += entry->gis[glyphs[idx]].xOff;
1246 yoff += entry->gis[glyphs[idx]].yOff;
1249 } else if(dc->bitsPerPixel == 1) {
1250 for(idx = 0; idx < count; idx++) {
1251 SharpGlyphGray(physDev, physDev->org.x + x + xoff,
1252 physDev->org.y + y + yoff,
1253 entry->bitmaps[glyphs[idx]],
1254 &entry->gis[glyphs[idx]]);
1256 offset += INTERNAL_XWSTODS(dc, deltas[idx]);
1257 xoff = offset * cosEsc;
1258 yoff = offset * -sinEsc;
1261 xoff += entry->gis[glyphs[idx]].xOff;
1262 yoff += entry->gis[glyphs[idx]].yOff;
1268 unsigned int w, h, dummy_uint;
1269 Window dummy_window;
1271 int image_x, image_y, image_off_x, image_off_y, image_w, image_h;
1272 RECT extents = {0, 0, 0, 0};
1276 XGetGeometry(gdi_display, physDev->drawable, &dummy_window, &dummy_int, &dummy_int,
1277 &w, &h, &dummy_uint, &dummy_uint);
1278 TRACE("drawable %dx%d\n", w, h);
1280 for(idx = 0; idx < count; idx++) {
1281 if(extents.left > cur.x - entry->gis[glyphs[idx]].x)
1282 extents.left = cur.x - entry->gis[glyphs[idx]].x;
1283 if(extents.top > cur.y - entry->gis[glyphs[idx]].y)
1284 extents.top = cur.y - entry->gis[glyphs[idx]].y;
1285 if(extents.right < cur.x - entry->gis[glyphs[idx]].x + entry->gis[glyphs[idx]].width)
1286 extents.right = cur.x - entry->gis[glyphs[idx]].x + entry->gis[glyphs[idx]].width;
1287 if(extents.bottom < cur.y - entry->gis[glyphs[idx]].y + entry->gis[glyphs[idx]].height)
1288 extents.bottom = cur.y - entry->gis[glyphs[idx]].y + entry->gis[glyphs[idx]].height;
1290 offset += INTERNAL_XWSTODS(dc, deltas[idx]);
1291 cur.x = offset * cosEsc;
1292 cur.y = offset * -sinEsc;
1294 cur.x += entry->gis[glyphs[idx]].xOff;
1295 cur.y += entry->gis[glyphs[idx]].yOff;
1298 TRACE("glyph extents %ld,%ld - %ld,%ld drawable x,y %ld,%ld\n", extents.left, extents.top,
1299 extents.right, extents.bottom, physDev->org.x + x, physDev->org.y + y);
1301 if(physDev->org.x + x + extents.left >= 0) {
1302 image_x = physDev->org.x + x + extents.left;
1306 image_off_x = physDev->org.x + x + extents.left;
1308 if(physDev->org.y + y + extents.top >= 0) {
1309 image_y = physDev->org.y + y + extents.top;
1313 image_off_y = physDev->org.y + y + extents.top;
1315 if(physDev->org.x + x + extents.right < w)
1316 image_w = physDev->org.x + x + extents.right - image_x;
1318 image_w = w - image_x;
1319 if(physDev->org.y + y + extents.bottom < h)
1320 image_h = physDev->org.y + y + extents.bottom - image_y;
1322 image_h = h - image_y;
1324 if(image_w <= 0 || image_h <= 0) goto no_image;
1326 X11DRV_expect_error(gdi_display, XRenderErrorHandler, NULL);
1327 image = XGetImage(gdi_display, physDev->drawable,
1328 image_x, image_y, image_w, image_h,
1329 AllPlanes, ZPixmap);
1330 X11DRV_check_error();
1332 TRACE("XGetImage(%p, %x, %d, %d, %d, %d, %lx, %x) depth = %d rets %p\n",
1333 gdi_display, (int)physDev->drawable, image_x, image_y,
1334 image_w, image_h, AllPlanes, ZPixmap,
1335 dc->bitsPerPixel, image);
1337 Pixmap xpm = XCreatePixmap(gdi_display, physDev->drawable, image_w, image_h,
1342 gcv.graphics_exposures = False;
1343 gc = XCreateGC(gdi_display, xpm, GCGraphicsExposures, &gcv);
1344 XCopyArea(gdi_display, physDev->drawable, xpm, gc, image_x, image_y,
1345 image_w, image_h, 0, 0);
1346 XFreeGC(gdi_display, gc);
1347 X11DRV_expect_error(gdi_display, XRenderErrorHandler, NULL);
1348 image = XGetImage(gdi_display, xpm, 0, 0, image_w, image_h, AllPlanes,
1350 X11DRV_check_error();
1351 XFreePixmap(gdi_display, xpm);
1353 if(!image) goto no_image;
1355 image->red_mask = visual->red_mask;
1356 image->green_mask = visual->green_mask;
1357 image->blue_mask = visual->blue_mask;
1359 offset = xoff = yoff = 0;
1360 for(idx = 0; idx < count; idx++) {
1361 SmoothGlyphGray(image, xoff + image_off_x - extents.left,
1362 yoff + image_off_y - extents.top,
1363 entry->bitmaps[glyphs[idx]],
1364 &entry->gis[glyphs[idx]],
1367 offset += INTERNAL_XWSTODS(dc, deltas[idx]);
1368 xoff = offset * cosEsc;
1369 yoff = offset * -sinEsc;
1371 xoff += entry->gis[glyphs[idx]].xOff;
1372 yoff += entry->gis[glyphs[idx]].yOff;
1376 XPutImage(gdi_display, physDev->drawable, physDev->gc, image, 0, 0,
1377 image_x, image_y, image_w, image_h);
1378 XDestroyImage(image);
1381 wine_tsx11_unlock();
1383 LeaveCriticalSection(&xrender_cs);
1385 if(deltas && deltas != lpDx)
1386 HeapFree(GetProcessHeap(), 0, deltas);
1388 if (flags & ETO_CLIPPED)
1389 RestoreVisRgn16( HDC_16(hdc) );
1394 X11DRV_UnlockDIBSection( physDev, TRUE );
1395 if(glyphs != wstr) HeapFree(GetProcessHeap(), 0, glyphs);
1399 #else /* HAVE_X11_EXTENSIONS_XRENDER_H */
1401 void X11DRV_XRender_Init(void)
1403 TRACE("XRender support not compiled in.\n");
1407 void X11DRV_XRender_Finalize(void)
1411 BOOL X11DRV_XRender_SelectFont(X11DRV_PDEVICE *physDev, HFONT hfont)
1417 void X11DRV_XRender_DeleteDC(X11DRV_PDEVICE *physDev)
1423 BOOL X11DRV_XRender_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
1424 const RECT *lprect, LPCWSTR wstr, UINT count,
1431 void X11DRV_XRender_UpdateDrawable(X11DRV_PDEVICE *physDev)
1437 #endif /* HAVE_X11_EXTENSIONS_XRENDER_H */