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
25 #include "wine/debug.h"
29 #include "wine/unicode.h"
32 BOOL X11DRV_XRender_Installed = FALSE;
33 WINE_DEFAULT_DEBUG_CHANNEL(xrender);
35 #ifdef HAVE_LIBXRENDER
38 #include "ts_xrender.h"
40 static XRenderPictFormat *screen_format; /* format of screen */
41 static XRenderPictFormat *mono_format; /* format of mono bitmap */
46 XFORM xform; /* this is dum as we don't care about offsets */
50 #define INITIAL_REALIZED_BUF_SIZE 128
57 XRenderPictFormat *font_format;
66 gsCacheEntry *cacheEntry;
70 COLORREF lastTextColor;
74 static gsCacheEntry *glyphsetCache = NULL;
75 static DWORD glyphsetCacheSize = 0;
76 static INT lastfree = -1;
79 #define INIT_CACHE_SIZE 10
81 static int antialias = 1;
83 /***********************************************************************
86 * Let's see if our XServer has the extension available
89 void X11DRV_XRender_Init(void)
91 int error_base, event_base, i;
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;
101 pf.direct.alphaMask = 1;
102 mono_format = TSXRenderFindFormat(gdi_display, PictFormatType |
103 PictFormatDepth | PictFormatAlpha |
104 PictFormatAlphaMask, &pf, 0);
106 glyphsetCache = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
107 sizeof(*glyphsetCache) * INIT_CACHE_SIZE);
109 glyphsetCacheSize = INIT_CACHE_SIZE;
111 for(i = 0; i < INIT_CACHE_SIZE; i++) {
112 glyphsetCache[i].next = i + 1;
113 glyphsetCache[i].count = -1;
115 glyphsetCache[i-1].next = -1;
117 TRACE("Xrender is not available on this server\n");
122 static BOOL fontcmp(LFANDSIZE *p1, LFANDSIZE *p2)
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);
130 static void walk_cache(void)
134 for(i=mru; i >= 0; i = glyphsetCache[i].next)
135 TRACE("item %d\n", i);
138 static gsCacheEntry *LookupEntry(LFANDSIZE *plfsz)
142 for(i = mru; i >= 0; i = glyphsetCache[i].next) {
144 if(glyphsetCache[i].count == -1) { /* reached free list so stop */
149 if(!fontcmp(&glyphsetCache[i].lfsz, plfsz)) {
150 glyphsetCache[i].count++;
152 glyphsetCache[prev_i].next = glyphsetCache[i].next;
153 glyphsetCache[i].next = mru;
156 TRACE("found font in cache %d\n", i);
157 return glyphsetCache + i;
161 TRACE("font not in cache\n");
165 static gsCacheEntry *AllocEntry(void)
167 int best = -1, prev_best = -1, i, prev_i = -1;
170 assert(glyphsetCache[lastfree].count == -1);
171 glyphsetCache[lastfree].count = 1;
173 lastfree = glyphsetCache[lastfree].next;
175 glyphsetCache[best].next = mru;
178 TRACE("empty space at %d, next lastfree = %d\n", mru, lastfree);
179 return glyphsetCache + mru;
182 for(i = mru; i >= 0; i = glyphsetCache[i].next) {
183 if(glyphsetCache[i].count == 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;
199 glyphsetCache[best].count = 1;
201 glyphsetCache[prev_best].next = glyphsetCache[best].next;
202 glyphsetCache[best].next = mru;
207 return glyphsetCache + mru;
210 TRACE("Growing cache\n");
211 glyphsetCache = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
213 (glyphsetCacheSize + INIT_CACHE_SIZE)
214 * sizeof(*glyphsetCache));
215 for(best = i = glyphsetCacheSize; i < glyphsetCacheSize + INIT_CACHE_SIZE;
217 glyphsetCache[i].next = i + 1;
218 glyphsetCache[i].count = -1;
220 glyphsetCache[i-1].next = -1;
221 glyphsetCacheSize += INIT_CACHE_SIZE;
223 lastfree = glyphsetCache[best].next;
224 glyphsetCache[best].count = 1;
225 glyphsetCache[best].next = mru;
227 TRACE("new free cache slot at %d\n", mru);
228 return glyphsetCache + mru;
231 static gsCacheEntry *GetCacheEntry(LFANDSIZE *plfsz)
233 XRenderPictFormat pf;
236 if((ret = LookupEntry(plfsz)) != NULL) return ret;
240 assert(ret->nrealized == 0);
243 if(antialias && abs(plfsz->lf.lfHeight) > 16) {
245 pf.direct.alphaMask = 0xff;
248 pf.direct.alphaMask = 1;
250 pf.type = PictTypeDirect;
253 ret->font_format = TSXRenderFindFormat(gdi_display,
260 ret->glyphset = TSXRenderCreateGlyphSet(gdi_display, ret->font_format);
264 static void dec_ref_cache(gsCacheEntry *entry)
266 TRACE("dec'ing entry %d to %d\n", entry - glyphsetCache, entry->count - 1);
267 assert(entry->count > 0);
271 static void lfsz_calc_hash(LFANDSIZE *plfsz)
273 DWORD hash = 0, *ptr;
276 for(ptr = (DWORD*)&plfsz->xform; ptr < (DWORD*)(&plfsz->xform + 1); ptr++)
278 for(i = 0, ptr = (DWORD*)&plfsz->lf; i < 7; i++, ptr++)
280 for(i = 0, ptr = (DWORD*)&plfsz->lf.lfFaceName; i < LF_FACESIZE/2; i++, ptr++) {
281 WCHAR *pwc = (WCHAR *)ptr;
291 /***********************************************************************
292 * X11DRV_XRender_Finalize
294 void X11DRV_XRender_Finalize(void)
296 FIXME("Free cached glyphsets\n");
301 /***********************************************************************
302 * X11DRV_XRender_SelectFont
304 BOOL X11DRV_XRender_SelectFont(DC *dc, HFONT hfont)
306 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
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);
316 if(!physDev->xrender)
317 physDev->xrender = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
318 sizeof(*physDev->xrender));
320 else if(physDev->xrender->cacheEntry)
321 dec_ref_cache(physDev->xrender->cacheEntry);
322 physDev->xrender->cacheEntry = GetCacheEntry(&lfsz);
326 /***********************************************************************
327 * X11DRV_XRender_DeleteDC
329 void X11DRV_XRender_DeleteDC(DC *dc)
331 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
333 if(physDev->xrender->tile_pict)
334 TSXRenderFreePicture(gdi_display, physDev->xrender->tile_pict);
336 if(physDev->xrender->tile_xpm)
337 TSXFreePixmap(gdi_display, physDev->xrender->tile_xpm);
339 if(physDev->xrender->pict) {
340 TRACE("freeing pict = %lx dc = %p\n", physDev->xrender->pict, dc);
341 TSXRenderFreePicture(gdi_display, physDev->xrender->pict);
344 if(physDev->xrender->cacheEntry)
345 dec_ref_cache(physDev->xrender->cacheEntry);
347 HeapFree(GetProcessHeap(), 0, physDev->xrender);
348 physDev->xrender = NULL;
352 /***********************************************************************
353 * X11DRV_XRender_UpdateDrawable
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.
359 void X11DRV_XRender_UpdateDrawable(DC *dc)
361 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
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);
367 physDev->xrender->pict = 0;
371 static BOOL UploadGlyph(DC *dc, WCHAR glyph)
373 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
379 gsCacheEntry *entry = physDev->xrender->cacheEntry;
383 if(entry->nrealized <= glyph) {
384 entry->nrealized = (glyph / 128 + 1) * 128;
385 entry->realized = HeapReAlloc(GetProcessHeap(),
388 entry->nrealized * sizeof(BOOL));
390 entry->realized[glyph] = TRUE;
392 if(entry->font_format->depth == 8) {
394 ggo_format = WINE_GGO_GRAY16_BITMAP;
397 ggo_format = GGO_BITMAP;
400 buflen = GetGlyphOutlineW(dc->hSelf, glyph, ggo_format, &gm, 0, NULL,
402 if(buflen == GDI_ERROR)
405 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buflen);
406 GetGlyphOutlineW(dc->hSelf, glyph, ggo_format, &gm, buflen, buf, NULL);
408 TRACE("buflen = %d. Got metrics: %dx%d adv=%d,%d origin=%ld,%ld\n",
410 gm.gmBlackBoxX, gm.gmBlackBoxY, gm.gmCellIncX, gm.gmCellIncY,
411 gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
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;
420 if(TRACE_ON(xrender)) {
426 pitch = ((gi.width + 31) / 32) * 4;
427 for(i = 0; i < gi.height; i++) {
428 line = buf + i * pitch;
430 for(j = 0; j < pitch * 8; j++) {
431 strcat(output, (line[j / 8] & (1 << (7 - (j % 8)))) ? "#" : " ");
433 strcat(output, "\n");
437 char blks[] = " .:;!o*#";
441 pitch = ((gi.width + 3) / 4) * 4;
442 for(i = 0; i < gi.height; i++) {
443 line = buf + i * pitch;
445 for(j = 0; j < pitch; j++) {
446 str[0] = blks[line[j] >> 5];
449 strcat(output, "\n");
455 if(!aa && BitmapBitOrder(gdi_display) != MSBFirst) {
456 unsigned char *byte = buf, c;
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);
471 TSXRenderAddGlyphs(gdi_display, entry->glyphset, &gid, &gi, 1,
473 HeapFree(GetProcessHeap(), 0, buf);
477 /***********************************************************************
478 * X11DRV_XRender_ExtTextOut
480 BOOL X11DRV_XRender_ExtTextOut( DC *dc, INT x, INT y, UINT flags,
481 const RECT *lprect, LPCWSTR wstr, UINT count,
487 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
492 BOOL done_extents = FALSE;
493 INT width, xwidth, ywidth;
494 double cosEsc, sinEsc;
497 int render_op = PictOpOver;
499 TRACE("%04x, %d, %d, %08x, %p, %s, %d, %p)\n", dc->hSelf, x, y, flags,
500 lprect, debugstr_wn(wstr, count), count, lpDx);
502 TRACE("rect: %d,%d - %d,%d\n", lprect->left, lprect->top, lprect->right,
504 TRACE("align = %x bkmode = %x mapmode = %x\n", dc->textAlign,
508 if(dc->textAlign & TA_UPDATECP) {
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);
522 if(flags & (ETO_CLIPPED | ETO_OPAQUE)) {
524 if(flags & ETO_CLIPPED) return FALSE;
525 GetTextExtentPointW(dc->hSelf, wstr, count, &sz);
529 rc.right = x + sz.cx;
530 rc.bottom = y + sz.cy;
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);
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;}
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 );
550 X11DRV_LockDIBSection( dc, DIB_Status_GdiMod, FALSE );
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 );
560 X11DRV_UnlockDIBSection( dc, TRUE );
564 x = INTERNAL_XWPTODP( dc, x, y );
565 y = INTERNAL_YWPTODP( dc, x, y );
567 TRACE("real x,y %d,%d\n", x, y);
571 for(idx = 0; idx < count; idx++)
575 GetTextExtentPointW(dc->hSelf, wstr, count, &sz);
580 width = INTERNAL_XWSTODS(dc, width);
581 xwidth = width * cosEsc;
582 ywidth = width * sinEsc;
584 GetTextMetricsW(dc->hSelf, &tm);
586 switch( dc->textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) ) {
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 );
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 );
609 switch( dc->textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) ) {
611 y += tm.tmAscent * cosEsc;
612 x += tm.tmAscent * sinEsc;
616 y -= tm.tmDescent * cosEsc;
617 x -= tm.tmDescent * sinEsc;
624 if (flags & ETO_CLIPPED)
626 SaveVisRgn16( dc->hSelf );
627 CLIPPING_IntersectVisRect( dc, rc.left, rc.top, rc.right,
633 if(!physDev->xrender->pict) {
634 XRenderPictureAttributes pa;
635 pa.subwindow_mode = IncludeInferiors;
637 physDev->xrender->pict =
638 TSXRenderCreatePicture(gdi_display,
640 (dc->bitsPerPixel == 1) ?
641 mono_format : screen_format,
642 CPSubwindowMode, &pa);
644 TRACE("allocing pict = %lx dc = %p drawable = %08lx\n", physDev->xrender->pict, dc, physDev->drawable);
646 TRACE("using existing pict = %lx dc = %p\n", physDev->xrender->pict, dc);
649 obj = (RGNOBJ *) GDI_GetObjPtr(dc->hGCClipRgn, REGION_MAGIC);
652 ERR("Rgn is 0. Please report this.\n");
656 if (obj->rgn->numRects > 0)
659 RECT *pRect = obj->rgn->rects;
660 RECT *pEndRect = obj->rgn->rects + obj->rgn->numRects;
662 pXrect = HeapAlloc( GetProcessHeap(), 0,
663 sizeof(*pXrect) * obj->rgn->numRects );
666 WARN("Can't alloc buffer\n");
667 GDI_ReleaseObj( dc->hGCClipRgn );
671 for(pXr = pXrect; pRect < pEndRect; pRect++, pXr++)
673 pXr->x = pRect->left;
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);
682 TRACE("no clip rgn\n");
686 TSXRenderSetPictureClipRectangles( gdi_display, physDev->xrender->pict,
687 0, 0, pXrect, obj->rgn->numRects );
690 HeapFree( GetProcessHeap(), 0, pXrect );
692 GDI_ReleaseObj( dc->hGCClipRgn );
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 );
706 /* Create a 1x1 pixmap to tile over the font mask */
707 if(!physDev->xrender->tile_xpm) {
708 XRenderPictureAttributes pa;
710 XRenderPictFormat *format = (dc->bitsPerPixel == 1) ? mono_format : screen_format;
711 physDev->xrender->tile_xpm = TSXCreatePixmap(gdi_display,
716 physDev->xrender->tile_pict = TSXRenderCreatePicture(gdi_display,
717 physDev->xrender->tile_xpm,
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;
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;
736 } else { /* for a 1bpp bitmap we always need a 1 in the tile */
737 col.red = col.green = col.blue = 0;
740 TSXRenderFillRectangle(gdi_display, PictOpSrc,
741 physDev->xrender->tile_pict,
743 physDev->xrender->lastTextColor = dc->textColor;
746 /* FIXME the mapping of Text/BkColor onto 1 or 0 needs investigation.
748 if((dc->bitsPerPixel == 1) && ((dc->textColor & 0xffffff) == 0))
749 render_op = PictOpOutReverse; /* This gives us 'black' text */
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]);
759 TRACE("Writing %s at %d,%d\n", debugstr_wn(wstr,count), dc->DCOrgX + x,
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,
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,
781 offset += INTERNAL_XWSTODS(dc, lpDx[idx]);
782 xoff = offset * cosEsc;
783 yoff = offset * sinEsc;
787 if(physDev->xrender->pict) {
788 TSXRenderFreePicture(gdi_display, physDev->xrender->pict);
790 physDev->xrender->pict = 0;
793 if (flags & ETO_CLIPPED)
794 RestoreVisRgn16( dc->hSelf );
796 X11DRV_UnlockDIBSection( dc, TRUE );
800 #else /* #ifdef HAVE_LIBXRENDER */
802 void X11DRV_XRender_Init(void)
804 TRACE("XRender support not compiled in.\n");
808 void X11DRV_XRender_Finalize(void)
814 BOOL X11DRV_XRender_SelectFont(DC *dc, HFONT hfont)
820 void X11DRV_XRender_DeleteDC(DC *dc)
826 BOOL X11DRV_XRender_ExtTextOut( DC *dc, INT x, INT y, UINT flags,
827 const RECT *lprect, LPCWSTR wstr, UINT count,
834 void X11DRV_XRender_UpdateDrawable(DC *dc)