2 * Functions to use the XRender extension
4 * Copyright 2001 Huw D M Davies for CodeWeavers
11 #include "debugtools.h"
15 #include "wine/unicode.h"
18 BOOL X11DRV_XRender_Installed = FALSE;
19 DEFAULT_DEBUG_CHANNEL(xrender);
21 #ifdef HAVE_LIBXRENDER
24 #include "ts_xrender.h"
26 static XRenderPictFormat *screen_format; /* format of screen */
27 static XRenderPictFormat *mono_format; /* format of mono bitmap */
32 XFORM xform; /* this is dum as we don't care about offsets */
36 #define INITIAL_REALIZED_BUF_SIZE 128
43 XRenderPictFormat *font_format;
52 gsCacheEntry *cacheEntry;
56 COLORREF lastTextColor;
60 static gsCacheEntry *glyphsetCache = NULL;
61 static DWORD glyphsetCacheSize = 0;
62 static INT lastfree = -1;
65 #define INIT_CACHE_SIZE 10
67 static int antialias = 1;
69 /***********************************************************************
72 * Let's see if our XServer has the extension available
75 void X11DRV_XRender_Init(void)
77 int error_base, event_base, i;
80 if(TSXRenderQueryExtension(gdi_display, &event_base, &error_base)) {
81 X11DRV_XRender_Installed = TRUE;
82 TRACE("Xrender is up and running error_base = %d\n", error_base);
83 screen_format = TSXRenderFindVisualFormat(gdi_display, visual);
84 pf.type = PictTypeDirect;
87 pf.direct.alphaMask = 1;
88 mono_format = TSXRenderFindFormat(gdi_display, PictFormatType |
89 PictFormatDepth | PictFormatAlpha |
90 PictFormatAlphaMask, &pf, 0);
92 glyphsetCache = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
93 sizeof(*glyphsetCache) * INIT_CACHE_SIZE);
95 glyphsetCacheSize = INIT_CACHE_SIZE;
97 for(i = 0; i < INIT_CACHE_SIZE; i++) {
98 glyphsetCache[i].next = i + 1;
99 glyphsetCache[i].count = -1;
101 glyphsetCache[i-1].next = -1;
103 TRACE("Xrender is not available on this server\n");
108 static BOOL fontcmp(LFANDSIZE *p1, LFANDSIZE *p2)
110 if(p1->hash != p2->hash) return TRUE;
111 if(memcmp(&p1->xform, &p2->xform, sizeof(p1->xform))) return TRUE;
112 if(memcmp(&p1->lf, &p2->lf, offsetof(LOGFONTW, lfFaceName))) return TRUE;
113 return strcmpW(p1->lf.lfFaceName, p2->lf.lfFaceName);
116 static void walk_cache(void)
120 for(i=mru; i >= 0; i = glyphsetCache[i].next)
121 TRACE("item %d\n", i);
124 static gsCacheEntry *LookupEntry(LFANDSIZE *plfsz)
128 for(i = mru; i >= 0; i = glyphsetCache[i].next) {
130 if(glyphsetCache[i].count == -1) { /* reached free list so stop */
135 if(!fontcmp(&glyphsetCache[i].lfsz, plfsz)) {
136 glyphsetCache[i].count++;
138 glyphsetCache[prev_i].next = glyphsetCache[i].next;
139 glyphsetCache[i].next = mru;
142 TRACE("found font in cache %d\n", i);
143 return glyphsetCache + i;
147 TRACE("font not in cache\n");
151 static gsCacheEntry *AllocEntry(void)
153 int best = -1, prev_best = -1, i, prev_i = -1;
156 assert(glyphsetCache[lastfree].count == -1);
157 glyphsetCache[lastfree].count = 1;
159 lastfree = glyphsetCache[lastfree].next;
161 glyphsetCache[best].next = mru;
164 TRACE("empty space at %d, next lastfree = %d\n", mru, lastfree);
165 return glyphsetCache + mru;
168 for(i = mru; i >= 0; i = glyphsetCache[i].next) {
169 if(glyphsetCache[i].count == 0) {
177 TRACE("freeing unused glyphset at cache %d\n", best);
178 TSXRenderFreeGlyphSet(gdi_display, glyphsetCache[best].glyphset);
179 glyphsetCache[best].glyphset = 0;
180 if(glyphsetCache[best].nrealized) { /* do we really want to do this? */
181 HeapFree(GetProcessHeap(), 0, glyphsetCache[best].realized);
182 glyphsetCache[best].realized = NULL;
183 glyphsetCache[best].nrealized = 0;
185 glyphsetCache[best].count = 1;
187 glyphsetCache[prev_best].next = glyphsetCache[best].next;
188 glyphsetCache[best].next = mru;
193 return glyphsetCache + mru;
196 TRACE("Growing cache\n");
197 glyphsetCache = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
199 (glyphsetCacheSize + INIT_CACHE_SIZE)
200 * sizeof(*glyphsetCache));
201 for(best = i = glyphsetCacheSize; i < glyphsetCacheSize + INIT_CACHE_SIZE;
203 glyphsetCache[i].next = i + 1;
204 glyphsetCache[i].count = -1;
206 glyphsetCache[i-1].next = -1;
207 glyphsetCacheSize += INIT_CACHE_SIZE;
209 lastfree = glyphsetCache[best].next;
210 glyphsetCache[best].count = 1;
211 glyphsetCache[best].next = mru;
213 TRACE("new free cache slot at %d\n", mru);
214 return glyphsetCache + mru;
217 static gsCacheEntry *GetCacheEntry(LFANDSIZE *plfsz)
219 XRenderPictFormat pf;
222 if((ret = LookupEntry(plfsz)) != NULL) return ret;
226 assert(ret->nrealized == 0);
229 if(antialias && abs(plfsz->lf.lfHeight) > 16) {
231 pf.direct.alphaMask = 0xff;
234 pf.direct.alphaMask = 1;
236 pf.type = PictTypeDirect;
239 ret->font_format = TSXRenderFindFormat(gdi_display,
246 ret->glyphset = TSXRenderCreateGlyphSet(gdi_display, ret->font_format);
250 static void dec_ref_cache(gsCacheEntry *entry)
252 TRACE("dec'ing entry %d to %d\n", entry - glyphsetCache, entry->count - 1);
253 assert(entry->count > 0);
257 static void lfsz_calc_hash(LFANDSIZE *plfsz)
259 DWORD hash = 0, *ptr;
262 for(ptr = (DWORD*)&plfsz->xform; ptr < (DWORD*)(&plfsz->xform + 1); ptr++)
264 for(i = 0, ptr = (DWORD*)&plfsz->lf; i < 7; i++, ptr++)
266 for(i = 0, ptr = (DWORD*)&plfsz->lf.lfFaceName; i < LF_FACESIZE/2; i++, ptr++) {
267 WCHAR *pwc = (WCHAR *)ptr;
277 /***********************************************************************
278 * X11DRV_XRender_Finalize
280 void X11DRV_XRender_Finalize(void)
282 FIXME("Free cached glyphsets\n");
287 /***********************************************************************
288 * X11DRV_XRender_SelectFont
290 BOOL X11DRV_XRender_SelectFont(DC *dc, HFONT hfont)
292 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
295 GetObjectW(hfont, sizeof(lfsz.lf), &lfsz.lf);
296 TRACE("h=%ld w=%ld weight=%ld it=%d charset=%d name=%s\n",
297 lfsz.lf.lfHeight, lfsz.lf.lfWidth, lfsz.lf.lfWeight,
298 lfsz.lf.lfItalic, lfsz.lf.lfCharSet, debugstr_w(lfsz.lf.lfFaceName));
299 lfsz.xform = dc->xformWorld2Vport;
300 lfsz_calc_hash(&lfsz);
302 if(!physDev->xrender)
303 physDev->xrender = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
304 sizeof(*physDev->xrender));
306 else if(physDev->xrender->cacheEntry)
307 dec_ref_cache(physDev->xrender->cacheEntry);
308 physDev->xrender->cacheEntry = GetCacheEntry(&lfsz);
312 /***********************************************************************
313 * X11DRV_XRender_DeleteDC
315 void X11DRV_XRender_DeleteDC(DC *dc)
317 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
319 if(physDev->xrender->tile_pict)
320 TSXRenderFreePicture(gdi_display, physDev->xrender->tile_pict);
322 if(physDev->xrender->tile_xpm)
323 TSXFreePixmap(gdi_display, physDev->xrender->tile_xpm);
325 if(physDev->xrender->pict) {
326 TRACE("freeing pict = %lx dc = %p\n", physDev->xrender->pict, dc);
327 TSXRenderFreePicture(gdi_display, physDev->xrender->pict);
330 if(physDev->xrender->cacheEntry)
331 dec_ref_cache(physDev->xrender->cacheEntry);
333 HeapFree(GetProcessHeap(), 0, physDev->xrender);
334 physDev->xrender = NULL;
338 /***********************************************************************
339 * X11DRV_XRender_UpdateDrawable
341 * This gets called from X11DRV_SetDrawable and deletes the pict when the
342 * drawable changes. However at the moment we delete the pict at the end of
343 * every ExtTextOut so this is basically a NOP.
345 void X11DRV_XRender_UpdateDrawable(DC *dc)
347 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
349 if(physDev->xrender->pict) {
350 TRACE("freeing pict %08lx from dc %p\n", physDev->xrender->pict, dc);
351 TSXRenderFreePicture(gdi_display, physDev->xrender->pict);
353 physDev->xrender->pict = 0;
357 static BOOL UploadGlyph(DC *dc, WCHAR glyph)
359 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
365 gsCacheEntry *entry = physDev->xrender->cacheEntry;
369 if(entry->nrealized <= glyph) {
370 entry->nrealized = (glyph / 128 + 1) * 128;
371 entry->realized = HeapReAlloc(GetProcessHeap(),
374 entry->nrealized * sizeof(BOOL));
376 entry->realized[glyph] = TRUE;
378 if(entry->font_format->depth == 8) {
380 ggo_format = WINE_GGO_GRAY16_BITMAP;
383 ggo_format = GGO_BITMAP;
386 buflen = GetGlyphOutlineW(dc->hSelf, glyph, ggo_format, &gm, 0, NULL,
388 if(buflen == GDI_ERROR)
391 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buflen);
392 GetGlyphOutlineW(dc->hSelf, glyph, ggo_format, &gm, buflen, buf, NULL);
394 TRACE("buflen = %d. Got metrics: %dx%d adv=%d,%d origin=%ld,%ld\n",
396 gm.gmBlackBoxX, gm.gmBlackBoxY, gm.gmCellIncX, gm.gmCellIncY,
397 gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
399 gi.width = gm.gmBlackBoxX;
400 gi.height = gm.gmBlackBoxY;
401 gi.x = -gm.gmptGlyphOrigin.x;
402 gi.y = gm.gmptGlyphOrigin.y;
403 gi.xOff = gm.gmCellIncX;
404 gi.yOff = gm.gmCellIncY;
406 if(TRACE_ON(xrender)) {
412 pitch = ((gi.width + 31) / 32) * 4;
413 for(i = 0; i < gi.height; i++) {
414 line = buf + i * pitch;
416 for(j = 0; j < pitch * 8; j++) {
417 strcat(output, (line[j / 8] & (1 << (7 - (j % 8)))) ? "#" : " ");
419 strcat(output, "\n");
423 char blks[] = " .:;!o*#";
427 pitch = ((gi.width + 3) / 4) * 4;
428 for(i = 0; i < gi.height; i++) {
429 line = buf + i * pitch;
431 for(j = 0; j < pitch; j++) {
432 str[0] = blks[line[j] >> 5];
435 strcat(output, "\n");
441 if(!aa && BitmapBitOrder(gdi_display) != MSBFirst) {
442 unsigned char *byte = buf, c;
448 /* magic to flip bit order */
449 c = ((c << 1) & 0xaa) | ((c >> 1) & 0x55);
450 c = ((c << 2) & 0xcc) | ((c >> 2) & 0x33);
451 c = ((c << 4) & 0xf0) | ((c >> 4) & 0x0f);
457 TSXRenderAddGlyphs(gdi_display, entry->glyphset, &gid, &gi, 1,
459 HeapFree(GetProcessHeap(), 0, buf);
463 /***********************************************************************
464 * X11DRV_XRender_ExtTextOut
466 BOOL X11DRV_XRender_ExtTextOut( DC *dc, INT x, INT y, UINT flags,
467 const RECT *lprect, LPCWSTR wstr, UINT count,
473 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
478 BOOL done_extents = FALSE;
479 INT width, xwidth, ywidth;
480 double cosEsc, sinEsc;
483 int render_op = PictOpOver;
485 TRACE("%04x, %d, %d, %08x, %p, %s, %d, %p)\n", dc->hSelf, x, y, flags,
486 lprect, debugstr_wn(wstr, count), count, lpDx);
488 TRACE("rect: %d,%d - %d,%d\n", lprect->left, lprect->top, lprect->right,
490 TRACE("align = %x bkmode = %x mapmode = %x\n", dc->textAlign,
494 if(dc->textAlign & TA_UPDATECP) {
499 GetObjectW(GetCurrentObject(dc->hSelf, OBJ_FONT), sizeof(lf), &lf);
500 if(lf.lfEscapement != 0) {
501 cosEsc = cos(lf.lfEscapement * M_PI / 1800);
502 sinEsc = sin(lf.lfEscapement * M_PI / 1800);
508 if(flags & (ETO_CLIPPED | ETO_OPAQUE)) {
510 if(flags & ETO_CLIPPED) return FALSE;
511 GetTextExtentPointW(dc->hSelf, wstr, count, &sz);
515 rc.right = x + sz.cx;
516 rc.bottom = y + sz.cy;
521 rc.left = INTERNAL_XWPTODP(dc, rc.left, rc.top);
522 rc.top = INTERNAL_YWPTODP(dc, rc.left, rc.top);
523 rc.right = INTERNAL_XWPTODP(dc, rc.right, rc.bottom);
524 rc.bottom = INTERNAL_YWPTODP(dc, rc.right, rc.bottom);
526 if(rc.left > rc.right) {INT tmp = rc.left; rc.left = rc.right; rc.right = tmp;}
527 if(rc.top > rc.bottom) {INT tmp = rc.top; rc.top = rc.bottom; rc.bottom = tmp;}
530 xgcval.function = GXcopy;
531 xgcval.background = physDev->backgroundPixel;
532 xgcval.fill_style = FillSolid;
533 TSXChangeGC( gdi_display, physDev->gc,
534 GCFunction | GCBackground | GCFillStyle, &xgcval );
536 X11DRV_LockDIBSection( dc, DIB_Status_GdiMod, FALSE );
538 if(flags & ETO_OPAQUE) {
539 TSXSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
540 TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
541 dc->DCOrgX + rc.left, dc->DCOrgY + rc.top,
542 rc.right - rc.left, rc.bottom - rc.top );
546 X11DRV_UnlockDIBSection( dc, TRUE );
550 x = INTERNAL_XWPTODP( dc, x, y );
551 y = INTERNAL_YWPTODP( dc, x, y );
553 TRACE("real x,y %d,%d\n", x, y);
557 for(idx = 0; idx < count; idx++)
561 GetTextExtentPointW(dc->hSelf, wstr, count, &sz);
566 width = INTERNAL_XWSTODS(dc, width);
567 xwidth = width * cosEsc;
568 ywidth = width * sinEsc;
570 GetTextMetricsW(dc->hSelf, &tm);
572 switch( dc->textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) ) {
574 if (dc->textAlign & TA_UPDATECP) {
575 dc->CursPosX = INTERNAL_XDPTOWP( dc, x + xwidth, y - ywidth );
576 dc->CursPosY = INTERNAL_YDPTOWP( dc, x + xwidth, y - ywidth );
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 );
595 switch( dc->textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) ) {
597 y += tm.tmAscent * cosEsc;
598 x += tm.tmAscent * sinEsc;
602 y -= tm.tmDescent * cosEsc;
603 x -= tm.tmDescent * sinEsc;
610 if (flags & ETO_CLIPPED)
612 SaveVisRgn16( dc->hSelf );
613 CLIPPING_IntersectVisRect( dc, rc.left, rc.top, rc.right,
619 if(!physDev->xrender->pict) {
620 XRenderPictureAttributes pa;
621 pa.subwindow_mode = IncludeInferiors;
623 physDev->xrender->pict =
624 TSXRenderCreatePicture(gdi_display,
626 (dc->bitsPerPixel == 1) ?
627 mono_format : screen_format,
628 CPSubwindowMode, &pa);
630 TRACE("allocing pict = %lx dc = %p drawable = %08lx\n", physDev->xrender->pict, dc, physDev->drawable);
632 TRACE("using existing pict = %lx dc = %p\n", physDev->xrender->pict, dc);
635 obj = (RGNOBJ *) GDI_GetObjPtr(dc->hGCClipRgn, REGION_MAGIC);
638 ERR("Rgn is 0. Please report this.\n");
642 if (obj->rgn->numRects > 0)
645 RECT *pRect = obj->rgn->rects;
646 RECT *pEndRect = obj->rgn->rects + obj->rgn->numRects;
648 pXrect = HeapAlloc( GetProcessHeap(), 0,
649 sizeof(*pXrect) * obj->rgn->numRects );
652 WARN("Can't alloc buffer\n");
653 GDI_ReleaseObj( dc->hGCClipRgn );
657 for(pXr = pXrect; pRect < pEndRect; pRect++, pXr++)
659 pXr->x = pRect->left;
661 pXr->width = pRect->right - pRect->left;
662 pXr->height = pRect->bottom - pRect->top;
663 TRACE("Adding clip rect %d,%d - %d,%d\n", pRect->left, pRect->top,
664 pRect->right, pRect->bottom);
668 TRACE("no clip rgn\n");
672 TSXRenderSetPictureClipRectangles( gdi_display, physDev->xrender->pict,
673 0, 0, pXrect, obj->rgn->numRects );
676 HeapFree( GetProcessHeap(), 0, pXrect );
678 GDI_ReleaseObj( dc->hGCClipRgn );
680 if(dc->backgroundMode != TRANSPARENT) {
681 if(!((flags & ETO_CLIPPED) && (flags & ETO_OPAQUE))) {
682 if(!(flags & ETO_OPAQUE) || x < rc.left || x + width >= rc.right ||
683 y - tm.tmAscent < rc.top || y + tm.tmDescent >= rc.bottom) {
684 TSXSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
685 TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
686 dc->DCOrgX + x, dc->DCOrgY + y - tm.tmAscent,
687 width, tm.tmAscent + tm.tmDescent );
692 /* Create a 1x1 pixmap to tile over the font mask */
693 if(!physDev->xrender->tile_xpm) {
694 XRenderPictureAttributes pa;
696 XRenderPictFormat *format = (dc->bitsPerPixel == 1) ? mono_format : screen_format;
697 physDev->xrender->tile_xpm = TSXCreatePixmap(gdi_display,
702 physDev->xrender->tile_pict = TSXRenderCreatePicture(gdi_display,
703 physDev->xrender->tile_xpm,
706 TRACE("Created pixmap of depth %d\n", format->depth);
707 /* init lastTextColor to something different from dc->textColor */
708 physDev->xrender->lastTextColor = ~dc->textColor;
712 if(dc->textColor != physDev->xrender->lastTextColor) {
713 if(dc->bitsPerPixel != 1) {
714 /* Map 0 -- 0xff onto 0 -- 0xffff */
715 col.red = GetRValue(dc->textColor);
716 col.red |= col.red << 8;
717 col.green = GetGValue(dc->textColor);
718 col.green |= col.green << 8;
719 col.blue = GetBValue(dc->textColor);
720 col.blue |= col.blue << 8;
722 } else { /* for a 1bpp bitmap we always need a 1 in the tile */
723 col.red = col.green = col.blue = 0;
726 TSXRenderFillRectangle(gdi_display, PictOpSrc,
727 physDev->xrender->tile_pict,
729 physDev->xrender->lastTextColor = dc->textColor;
732 /* FIXME the mapping of Text/BkColor onto 1 or 0 needs investigation.
734 if((dc->bitsPerPixel == 1) && ((dc->textColor & 0xffffff) == 0))
735 render_op = PictOpOutReverse; /* This gives us 'black' text */
737 for(idx = 0; idx < count; idx++) {
738 if(wstr[idx] >= physDev->xrender->cacheEntry->nrealized ||
739 physDev->xrender->cacheEntry->realized[wstr[idx]] == FALSE) {
740 UploadGlyph(dc, wstr[idx]);
745 TRACE("Writing %s at %d,%d\n", debugstr_wn(wstr,count), dc->DCOrgX + x,
748 TSXRenderCompositeString16(gdi_display, render_op,
749 physDev->xrender->tile_pict,
750 physDev->xrender->pict,
751 physDev->xrender->cacheEntry->font_format,
752 physDev->xrender->cacheEntry->glyphset,
753 0, 0, dc->DCOrgX + x, dc->DCOrgY + y, wstr,
757 INT offset = 0, xoff = 0, yoff = 0;
758 for(idx = 0; idx < count; idx++) {
759 TSXRenderCompositeString16(gdi_display, render_op,
760 physDev->xrender->tile_pict,
761 physDev->xrender->pict,
762 physDev->xrender->cacheEntry->font_format,
763 physDev->xrender->cacheEntry->glyphset,
764 0, 0, dc->DCOrgX + x + xoff,
765 dc->DCOrgY + y + yoff,
767 offset += INTERNAL_XWSTODS(dc, lpDx[idx]);
768 xoff = offset * cosEsc;
769 yoff = offset * sinEsc;
773 if(physDev->xrender->pict) {
774 TSXRenderFreePicture(gdi_display, physDev->xrender->pict);
776 physDev->xrender->pict = 0;
779 if (flags & ETO_CLIPPED)
780 RestoreVisRgn16( dc->hSelf );
782 X11DRV_UnlockDIBSection( dc, TRUE );
786 #else /* #ifdef HAVE_LIBXRENDER */
788 void X11DRV_XRender_Init(void)
790 TRACE("XRender support not compiled in.\n");
794 void X11DRV_XRender_Finalize(void)
800 BOOL X11DRV_XRender_SelectFont(DC *dc, HFONT hfont)
806 void X11DRV_XRender_DeleteDC(DC *dc)
812 BOOL X11DRV_XRender_ExtTextOut( DC *dc, INT x, INT y, UINT flags,
813 const RECT *lprect, LPCWSTR wstr, UINT count,
820 void X11DRV_XRender_UpdateDrawable(DC *dc)