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);
131 static void walk_cache(void)
135 for(i=mru; i >= 0; i = glyphsetCache[i].next)
136 TRACE("item %d\n", i);
140 static gsCacheEntry *LookupEntry(LFANDSIZE *plfsz)
144 for(i = mru; i >= 0; i = glyphsetCache[i].next) {
146 if(glyphsetCache[i].count == -1) { /* reached free list so stop */
151 if(!fontcmp(&glyphsetCache[i].lfsz, plfsz)) {
152 glyphsetCache[i].count++;
154 glyphsetCache[prev_i].next = glyphsetCache[i].next;
155 glyphsetCache[i].next = mru;
158 TRACE("found font in cache %d\n", i);
159 return glyphsetCache + i;
163 TRACE("font not in cache\n");
167 static gsCacheEntry *AllocEntry(void)
169 int best = -1, prev_best = -1, i, prev_i = -1;
172 assert(glyphsetCache[lastfree].count == -1);
173 glyphsetCache[lastfree].count = 1;
175 lastfree = glyphsetCache[lastfree].next;
177 glyphsetCache[best].next = mru;
180 TRACE("empty space at %d, next lastfree = %d\n", mru, lastfree);
181 return glyphsetCache + mru;
184 for(i = mru; i >= 0; i = glyphsetCache[i].next) {
185 if(glyphsetCache[i].count == 0) {
193 TRACE("freeing unused glyphset at cache %d\n", best);
194 TSXRenderFreeGlyphSet(gdi_display, glyphsetCache[best].glyphset);
195 glyphsetCache[best].glyphset = 0;
196 if(glyphsetCache[best].nrealized) { /* do we really want to do this? */
197 HeapFree(GetProcessHeap(), 0, glyphsetCache[best].realized);
198 glyphsetCache[best].realized = NULL;
199 glyphsetCache[best].nrealized = 0;
201 glyphsetCache[best].count = 1;
203 glyphsetCache[prev_best].next = glyphsetCache[best].next;
204 glyphsetCache[best].next = mru;
209 return glyphsetCache + mru;
212 TRACE("Growing cache\n");
213 glyphsetCache = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
215 (glyphsetCacheSize + INIT_CACHE_SIZE)
216 * sizeof(*glyphsetCache));
217 for(best = i = glyphsetCacheSize; i < glyphsetCacheSize + INIT_CACHE_SIZE;
219 glyphsetCache[i].next = i + 1;
220 glyphsetCache[i].count = -1;
222 glyphsetCache[i-1].next = -1;
223 glyphsetCacheSize += INIT_CACHE_SIZE;
225 lastfree = glyphsetCache[best].next;
226 glyphsetCache[best].count = 1;
227 glyphsetCache[best].next = mru;
229 TRACE("new free cache slot at %d\n", mru);
230 return glyphsetCache + mru;
233 static gsCacheEntry *GetCacheEntry(LFANDSIZE *plfsz)
235 XRenderPictFormat pf;
238 if((ret = LookupEntry(plfsz)) != NULL) return ret;
242 assert(ret->nrealized == 0);
245 if(antialias && abs(plfsz->lf.lfHeight) > 16) {
247 pf.direct.alphaMask = 0xff;
250 pf.direct.alphaMask = 1;
252 pf.type = PictTypeDirect;
255 ret->font_format = TSXRenderFindFormat(gdi_display,
262 ret->glyphset = TSXRenderCreateGlyphSet(gdi_display, ret->font_format);
266 static void dec_ref_cache(gsCacheEntry *entry)
268 TRACE("dec'ing entry %d to %d\n", entry - glyphsetCache, entry->count - 1);
269 assert(entry->count > 0);
273 static void lfsz_calc_hash(LFANDSIZE *plfsz)
275 DWORD hash = 0, *ptr;
278 for(ptr = (DWORD*)&plfsz->xform; ptr < (DWORD*)(&plfsz->xform + 1); ptr++)
280 for(i = 0, ptr = (DWORD*)&plfsz->lf; i < 7; i++, ptr++)
282 for(i = 0, ptr = (DWORD*)&plfsz->lf.lfFaceName; i < LF_FACESIZE/2; i++, ptr++) {
283 WCHAR *pwc = (WCHAR *)ptr;
293 /***********************************************************************
294 * X11DRV_XRender_Finalize
296 void X11DRV_XRender_Finalize(void)
298 FIXME("Free cached glyphsets\n");
303 /***********************************************************************
304 * X11DRV_XRender_SelectFont
306 BOOL X11DRV_XRender_SelectFont(X11DRV_PDEVICE *physDev, HFONT hfont)
310 GetObjectW(hfont, sizeof(lfsz.lf), &lfsz.lf);
311 TRACE("h=%ld w=%ld weight=%ld it=%d charset=%d name=%s\n",
312 lfsz.lf.lfHeight, lfsz.lf.lfWidth, lfsz.lf.lfWeight,
313 lfsz.lf.lfItalic, lfsz.lf.lfCharSet, debugstr_w(lfsz.lf.lfFaceName));
314 lfsz.xform = physDev->dc->xformWorld2Vport;
315 lfsz_calc_hash(&lfsz);
317 if(!physDev->xrender)
318 physDev->xrender = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
319 sizeof(*physDev->xrender));
321 else if(physDev->xrender->cacheEntry)
322 dec_ref_cache(physDev->xrender->cacheEntry);
323 physDev->xrender->cacheEntry = GetCacheEntry(&lfsz);
327 /***********************************************************************
328 * X11DRV_XRender_DeleteDC
330 void X11DRV_XRender_DeleteDC(X11DRV_PDEVICE *physDev)
332 if(physDev->xrender->tile_pict)
333 TSXRenderFreePicture(gdi_display, physDev->xrender->tile_pict);
335 if(physDev->xrender->tile_xpm)
336 TSXFreePixmap(gdi_display, physDev->xrender->tile_xpm);
338 if(physDev->xrender->pict) {
339 TRACE("freeing pict = %lx dc = %p\n", physDev->xrender->pict, physDev->dc);
340 TSXRenderFreePicture(gdi_display, physDev->xrender->pict);
343 if(physDev->xrender->cacheEntry)
344 dec_ref_cache(physDev->xrender->cacheEntry);
346 HeapFree(GetProcessHeap(), 0, physDev->xrender);
347 physDev->xrender = NULL;
351 /***********************************************************************
352 * X11DRV_XRender_UpdateDrawable
354 * This gets called from X11DRV_SetDrawable and deletes the pict when the
355 * drawable changes. However at the moment we delete the pict at the end of
356 * every ExtTextOut so this is basically a NOP.
358 void X11DRV_XRender_UpdateDrawable(X11DRV_PDEVICE *physDev)
360 if(physDev->xrender->pict) {
361 TRACE("freeing pict %08lx from dc %p\n", physDev->xrender->pict, physDev->dc);
362 TSXRenderFreePicture(gdi_display, physDev->xrender->pict);
364 physDev->xrender->pict = 0;
368 static BOOL UploadGlyph(X11DRV_PDEVICE *physDev, WCHAR glyph)
375 gsCacheEntry *entry = physDev->xrender->cacheEntry;
379 if(entry->nrealized <= glyph) {
380 entry->nrealized = (glyph / 128 + 1) * 128;
381 entry->realized = HeapReAlloc(GetProcessHeap(),
384 entry->nrealized * sizeof(BOOL));
386 entry->realized[glyph] = TRUE;
388 if(entry->font_format->depth == 8) {
390 ggo_format = WINE_GGO_GRAY16_BITMAP;
393 ggo_format = GGO_BITMAP;
396 buflen = GetGlyphOutlineW(physDev->hdc, glyph, ggo_format, &gm, 0, NULL,
398 if(buflen == GDI_ERROR)
401 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buflen);
402 GetGlyphOutlineW(physDev->hdc, glyph, ggo_format, &gm, buflen, buf, NULL);
404 TRACE("buflen = %d. Got metrics: %dx%d adv=%d,%d origin=%ld,%ld\n",
406 gm.gmBlackBoxX, gm.gmBlackBoxY, gm.gmCellIncX, gm.gmCellIncY,
407 gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
409 gi.width = gm.gmBlackBoxX;
410 gi.height = gm.gmBlackBoxY;
411 gi.x = -gm.gmptGlyphOrigin.x;
412 gi.y = gm.gmptGlyphOrigin.y;
413 gi.xOff = gm.gmCellIncX;
414 gi.yOff = gm.gmCellIncY;
416 if(TRACE_ON(xrender)) {
422 pitch = ((gi.width + 31) / 32) * 4;
423 for(i = 0; i < gi.height; i++) {
424 line = buf + i * pitch;
426 for(j = 0; j < pitch * 8; j++) {
427 strcat(output, (line[j / 8] & (1 << (7 - (j % 8)))) ? "#" : " ");
429 strcat(output, "\n");
433 char blks[] = " .:;!o*#";
437 pitch = ((gi.width + 3) / 4) * 4;
438 for(i = 0; i < gi.height; i++) {
439 line = buf + i * pitch;
441 for(j = 0; j < pitch; j++) {
442 str[0] = blks[line[j] >> 5];
445 strcat(output, "\n");
451 if(!aa && BitmapBitOrder(gdi_display) != MSBFirst) {
452 unsigned char *byte = buf, c;
458 /* magic to flip bit order */
459 c = ((c << 1) & 0xaa) | ((c >> 1) & 0x55);
460 c = ((c << 2) & 0xcc) | ((c >> 2) & 0x33);
461 c = ((c << 4) & 0xf0) | ((c >> 4) & 0x0f);
467 TSXRenderAddGlyphs(gdi_display, entry->glyphset, &gid, &gi, 1,
469 HeapFree(GetProcessHeap(), 0, buf);
473 /***********************************************************************
474 * X11DRV_XRender_ExtTextOut
476 BOOL X11DRV_XRender_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
477 const RECT *lprect, LPCWSTR wstr, UINT count,
487 BOOL done_extents = FALSE;
488 INT width, xwidth, ywidth;
489 double cosEsc, sinEsc;
492 int render_op = PictOpOver;
493 HDC hdc = physDev->hdc;
494 DC *dc = physDev->dc;
496 TRACE("%04x, %d, %d, %08x, %p, %s, %d, %p)\n", hdc, x, y, flags,
497 lprect, debugstr_wn(wstr, count), count, lpDx);
499 TRACE("rect: %d,%d - %d,%d\n", lprect->left, lprect->top, lprect->right,
501 TRACE("align = %x bkmode = %x mapmode = %x\n", dc->textAlign, GetBkMode(hdc), dc->MapMode);
503 if(dc->textAlign & TA_UPDATECP) {
508 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
509 if(lf.lfEscapement != 0) {
510 cosEsc = cos(lf.lfEscapement * M_PI / 1800);
511 sinEsc = sin(lf.lfEscapement * M_PI / 1800);
517 if(flags & (ETO_CLIPPED | ETO_OPAQUE)) {
519 if(flags & ETO_CLIPPED) return FALSE;
520 GetTextExtentPointW(hdc, wstr, count, &sz);
524 rc.right = x + sz.cx;
525 rc.bottom = y + sz.cy;
530 rc.left = INTERNAL_XWPTODP(dc, rc.left, rc.top);
531 rc.top = INTERNAL_YWPTODP(dc, rc.left, rc.top);
532 rc.right = INTERNAL_XWPTODP(dc, rc.right, rc.bottom);
533 rc.bottom = INTERNAL_YWPTODP(dc, rc.right, rc.bottom);
535 if(rc.left > rc.right) {INT tmp = rc.left; rc.left = rc.right; rc.right = tmp;}
536 if(rc.top > rc.bottom) {INT tmp = rc.top; rc.top = rc.bottom; rc.bottom = tmp;}
539 xgcval.function = GXcopy;
540 xgcval.background = physDev->backgroundPixel;
541 xgcval.fill_style = FillSolid;
542 TSXChangeGC( gdi_display, physDev->gc,
543 GCFunction | GCBackground | GCFillStyle, &xgcval );
545 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
547 if(flags & ETO_OPAQUE) {
548 TSXSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
549 TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
550 dc->DCOrgX + rc.left, dc->DCOrgY + rc.top,
551 rc.right - rc.left, rc.bottom - rc.top );
555 X11DRV_UnlockDIBSection( physDev, TRUE );
559 x = INTERNAL_XWPTODP( dc, x, y );
560 y = INTERNAL_YWPTODP( dc, x, y );
562 TRACE("real x,y %d,%d\n", x, y);
566 for(idx = 0; idx < count; idx++)
570 GetTextExtentPointW(hdc, wstr, count, &sz);
575 width = INTERNAL_XWSTODS(dc, width);
576 xwidth = width * cosEsc;
577 ywidth = width * sinEsc;
579 GetTextMetricsW(hdc, &tm);
581 tm.tmAscent = INTERNAL_YWSTODS(dc, tm.tmAscent);
582 tm.tmDescent = INTERNAL_YWSTODS(dc, tm.tmDescent);
583 switch( dc->textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) ) {
585 if (dc->textAlign & TA_UPDATECP) {
586 dc->CursPosX = INTERNAL_XDPTOWP( dc, x + xwidth, y - ywidth );
587 dc->CursPosY = INTERNAL_YDPTOWP( dc, x + xwidth, y - ywidth );
599 if (dc->textAlign & TA_UPDATECP) {
600 dc->CursPosX = INTERNAL_XDPTOWP( dc, x + xwidth, y - ywidth );
601 dc->CursPosY = INTERNAL_YDPTOWP( dc, x + xwidth, y - ywidth );
606 switch( dc->textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) ) {
608 y += tm.tmAscent * cosEsc;
609 x += tm.tmAscent * sinEsc;
613 y -= tm.tmDescent * cosEsc;
614 x -= tm.tmDescent * sinEsc;
621 if (flags & ETO_CLIPPED)
624 CLIPPING_IntersectVisRect( dc, rc.left, rc.top, rc.right,
630 if(!physDev->xrender->pict) {
631 XRenderPictureAttributes pa;
632 pa.subwindow_mode = IncludeInferiors;
634 physDev->xrender->pict =
635 TSXRenderCreatePicture(gdi_display,
637 (dc->bitsPerPixel == 1) ?
638 mono_format : screen_format,
639 CPSubwindowMode, &pa);
641 TRACE("allocing pict = %lx dc = %p drawable = %08lx\n", physDev->xrender->pict, dc, physDev->drawable);
643 TRACE("using existing pict = %lx dc = %p\n", physDev->xrender->pict, dc);
646 obj = (RGNOBJ *) GDI_GetObjPtr(dc->hGCClipRgn, REGION_MAGIC);
649 ERR("Rgn is 0. Please report this.\n");
653 if (obj->rgn->numRects > 0)
656 RECT *pRect = obj->rgn->rects;
657 RECT *pEndRect = obj->rgn->rects + obj->rgn->numRects;
659 pXrect = HeapAlloc( GetProcessHeap(), 0,
660 sizeof(*pXrect) * obj->rgn->numRects );
663 WARN("Can't alloc buffer\n");
664 GDI_ReleaseObj( dc->hGCClipRgn );
668 for(pXr = pXrect; pRect < pEndRect; pRect++, pXr++)
670 pXr->x = pRect->left;
672 pXr->width = pRect->right - pRect->left;
673 pXr->height = pRect->bottom - pRect->top;
674 TRACE("Adding clip rect %d,%d - %d,%d\n", pRect->left, pRect->top,
675 pRect->right, pRect->bottom);
679 TRACE("no clip rgn\n");
683 TSXRenderSetPictureClipRectangles( gdi_display, physDev->xrender->pict,
684 0, 0, pXrect, obj->rgn->numRects );
687 HeapFree( GetProcessHeap(), 0, pXrect );
689 GDI_ReleaseObj( dc->hGCClipRgn );
691 if(GetBkMode(hdc) != TRANSPARENT) {
692 if(!((flags & ETO_CLIPPED) && (flags & ETO_OPAQUE))) {
693 if(!(flags & ETO_OPAQUE) || x < rc.left || x + width >= rc.right ||
694 y - tm.tmAscent < rc.top || y + tm.tmDescent >= rc.bottom) {
695 TSXSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
696 TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
697 dc->DCOrgX + x, dc->DCOrgY + y - tm.tmAscent,
698 width, tm.tmAscent + tm.tmDescent );
703 /* Create a 1x1 pixmap to tile over the font mask */
704 if(!physDev->xrender->tile_xpm) {
705 XRenderPictureAttributes pa;
707 XRenderPictFormat *format = (dc->bitsPerPixel == 1) ? mono_format : screen_format;
708 physDev->xrender->tile_xpm = TSXCreatePixmap(gdi_display,
713 physDev->xrender->tile_pict = TSXRenderCreatePicture(gdi_display,
714 physDev->xrender->tile_xpm,
717 TRACE("Created pixmap of depth %d\n", format->depth);
718 /* init lastTextColor to something different from dc->textColor */
719 physDev->xrender->lastTextColor = ~dc->textColor;
723 if(dc->textColor != physDev->xrender->lastTextColor) {
724 if(dc->bitsPerPixel != 1) {
725 /* Map 0 -- 0xff onto 0 -- 0xffff */
726 col.red = GetRValue(dc->textColor);
727 col.red |= col.red << 8;
728 col.green = GetGValue(dc->textColor);
729 col.green |= col.green << 8;
730 col.blue = GetBValue(dc->textColor);
731 col.blue |= col.blue << 8;
733 } else { /* for a 1bpp bitmap we always need a 1 in the tile */
734 col.red = col.green = col.blue = 0;
737 TSXRenderFillRectangle(gdi_display, PictOpSrc,
738 physDev->xrender->tile_pict,
740 physDev->xrender->lastTextColor = dc->textColor;
743 /* FIXME the mapping of Text/BkColor onto 1 or 0 needs investigation.
745 if((dc->bitsPerPixel == 1) && ((dc->textColor & 0xffffff) == 0))
746 render_op = PictOpOutReverse; /* This gives us 'black' text */
748 for(idx = 0; idx < count; idx++) {
749 if(wstr[idx] >= physDev->xrender->cacheEntry->nrealized ||
750 physDev->xrender->cacheEntry->realized[wstr[idx]] == FALSE) {
751 UploadGlyph(physDev, wstr[idx]);
756 TRACE("Writing %s at %d,%d\n", debugstr_wn(wstr,count), dc->DCOrgX + x,
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, dc->DCOrgY + y, (unsigned short *)wstr,
768 INT offset = 0, xoff = 0, yoff = 0;
769 for(idx = 0; idx < count; idx++) {
770 TSXRenderCompositeString16(gdi_display, render_op,
771 physDev->xrender->tile_pict,
772 physDev->xrender->pict,
773 physDev->xrender->cacheEntry->font_format,
774 physDev->xrender->cacheEntry->glyphset,
775 0, 0, dc->DCOrgX + x + xoff,
776 dc->DCOrgY + y + yoff,
777 (unsigned short *)wstr + idx, 1);
778 offset += INTERNAL_XWSTODS(dc, lpDx[idx]);
779 xoff = offset * cosEsc;
780 yoff = offset * -sinEsc;
784 if(physDev->xrender->pict) {
785 TSXRenderFreePicture(gdi_display, physDev->xrender->pict);
787 physDev->xrender->pict = 0;
790 if (flags & ETO_CLIPPED)
791 RestoreVisRgn16( hdc );
793 X11DRV_UnlockDIBSection( physDev, TRUE );
797 #else /* #ifdef HAVE_LIBXRENDER */
799 void X11DRV_XRender_Init(void)
801 TRACE("XRender support not compiled in.\n");
805 void X11DRV_XRender_Finalize(void)
811 BOOL X11DRV_XRender_SelectFont(X11DRV_PDEVICE *physDev, HFONT hfont)
817 void X11DRV_XRender_DeleteDC(X11DRV_PDEVICE *physDev)
823 BOOL X11DRV_XRender_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
824 const RECT *lprect, LPCWSTR wstr, UINT count,
831 void X11DRV_XRender_UpdateDrawable(X11DRV_PDEVICE *physDev)