[WINESYNC] d3dx9: Implement ID3DXFont_PreloadGlyphs.

Based on a patch by Tony Wasserka.

Signed-off-by: Sven Baars <sbaars@codeweavers.com>
Signed-off-by: Matteo Bruni <mbruni@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>

wine commit id ac5687463700b070fc1eecca67c4a46ec15127fa by Sven Baars <sbaars@codeweavers.com>
This commit is contained in:
winesync 2020-09-21 23:03:28 +02:00 committed by Jérôme Gardou
parent 9cb965ea9b
commit 541dbf1b36
3 changed files with 255 additions and 41 deletions

View File

@ -26,6 +26,16 @@
WINE_DEFAULT_DEBUG_CHANNEL(d3dx); WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
struct d3dx_glyph
{
unsigned int id;
RECT black_box;
POINT cell_inc;
IDirect3DTexture9 *texture;
struct wine_rb_entry entry;
};
struct d3dx_font struct d3dx_font
{ {
ID3DXFont ID3DXFont_iface; ID3DXFont ID3DXFont_iface;
@ -33,11 +43,34 @@ struct d3dx_font
IDirect3DDevice9 *device; IDirect3DDevice9 *device;
D3DXFONT_DESCW desc; D3DXFONT_DESCW desc;
TEXTMETRICW metrics;
HDC hdc; HDC hdc;
HFONT hfont; HFONT hfont;
struct wine_rb_tree glyph_tree;
IDirect3DTexture9 **textures;
unsigned int texture_count, texture_pos;
unsigned int texture_size, glyph_size, glyphs_per_texture;
}; };
static int glyph_rb_compare(const void *key, const struct wine_rb_entry *entry)
{
struct d3dx_glyph *glyph = WINE_RB_ENTRY_VALUE(entry, struct d3dx_glyph, entry);
unsigned int id = (UINT_PTR)key;
return id - glyph->id;
}
static void glyph_rb_free(struct wine_rb_entry *entry, void *context)
{
struct d3dx_glyph *glyph = WINE_RB_ENTRY_VALUE(entry, struct d3dx_glyph, entry);
heap_free(glyph);
}
static inline struct d3dx_font *impl_from_ID3DXFont(ID3DXFont *iface) static inline struct d3dx_font *impl_from_ID3DXFont(ID3DXFont *iface)
{ {
return CONTAINING_RECORD(iface, struct d3dx_font, ID3DXFont_iface); return CONTAINING_RECORD(iface, struct d3dx_font, ID3DXFont_iface);
@ -63,85 +96,95 @@ static HRESULT WINAPI ID3DXFontImpl_QueryInterface(ID3DXFont *iface, REFIID riid
static ULONG WINAPI ID3DXFontImpl_AddRef(ID3DXFont *iface) static ULONG WINAPI ID3DXFontImpl_AddRef(ID3DXFont *iface)
{ {
struct d3dx_font *This = impl_from_ID3DXFont(iface); struct d3dx_font *font = impl_from_ID3DXFont(iface);
ULONG ref=InterlockedIncrement(&This->ref); ULONG ref = InterlockedIncrement(&font->ref);
TRACE("%p increasing refcount to %u\n", iface, ref); TRACE("%p increasing refcount to %u\n", iface, ref);
return ref; return ref;
} }
static ULONG WINAPI ID3DXFontImpl_Release(ID3DXFont *iface) static ULONG WINAPI ID3DXFontImpl_Release(ID3DXFont *iface)
{ {
struct d3dx_font *This = impl_from_ID3DXFont(iface); struct d3dx_font *font = impl_from_ID3DXFont(iface);
ULONG ref=InterlockedDecrement(&This->ref); ULONG ref = InterlockedDecrement(&font->ref);
unsigned int i;
TRACE("%p decreasing refcount to %u\n", iface, ref); TRACE("%p decreasing refcount to %u\n", iface, ref);
if(ref==0) { if (!ref)
DeleteObject(This->hfont); {
DeleteDC(This->hdc); for (i = 0; i < font->texture_count; ++i)
IDirect3DDevice9_Release(This->device); IDirect3DTexture9_Release(font->textures[i]);
HeapFree(GetProcessHeap(), 0, This);
heap_free(font->textures);
wine_rb_destroy(&font->glyph_tree, glyph_rb_free, NULL);
DeleteObject(font->hfont);
DeleteDC(font->hdc);
IDirect3DDevice9_Release(font->device);
heap_free(font);
} }
return ref; return ref;
} }
static HRESULT WINAPI ID3DXFontImpl_GetDevice(ID3DXFont *iface, IDirect3DDevice9 **device) static HRESULT WINAPI ID3DXFontImpl_GetDevice(ID3DXFont *iface, IDirect3DDevice9 **device)
{ {
struct d3dx_font *This = impl_from_ID3DXFont(iface); struct d3dx_font *font = impl_from_ID3DXFont(iface);
TRACE("iface %p, device %p\n", iface, device); TRACE("iface %p, device %p\n", iface, device);
if( !device ) return D3DERR_INVALIDCALL; if( !device ) return D3DERR_INVALIDCALL;
*device = This->device; *device = font->device;
IDirect3DDevice9_AddRef(This->device); IDirect3DDevice9_AddRef(font->device);
return D3D_OK; return D3D_OK;
} }
static HRESULT WINAPI ID3DXFontImpl_GetDescA(ID3DXFont *iface, D3DXFONT_DESCA *desc) static HRESULT WINAPI ID3DXFontImpl_GetDescA(ID3DXFont *iface, D3DXFONT_DESCA *desc)
{ {
struct d3dx_font *This = impl_from_ID3DXFont(iface); struct d3dx_font *font = impl_from_ID3DXFont(iface);
TRACE("iface %p, desc %p\n", iface, desc); TRACE("iface %p, desc %p\n", iface, desc);
if( !desc ) return D3DERR_INVALIDCALL; if( !desc ) return D3DERR_INVALIDCALL;
memcpy(desc, &This->desc, FIELD_OFFSET(D3DXFONT_DESCA, FaceName)); memcpy(desc, &font->desc, FIELD_OFFSET(D3DXFONT_DESCA, FaceName));
WideCharToMultiByte(CP_ACP, 0, This->desc.FaceName, -1, desc->FaceName, ARRAY_SIZE(desc->FaceName), NULL, NULL); WideCharToMultiByte(CP_ACP, 0, font->desc.FaceName, -1, desc->FaceName, ARRAY_SIZE(desc->FaceName), NULL, NULL);
return D3D_OK; return D3D_OK;
} }
static HRESULT WINAPI ID3DXFontImpl_GetDescW(ID3DXFont *iface, D3DXFONT_DESCW *desc) static HRESULT WINAPI ID3DXFontImpl_GetDescW(ID3DXFont *iface, D3DXFONT_DESCW *desc)
{ {
struct d3dx_font *This = impl_from_ID3DXFont(iface); struct d3dx_font *font = impl_from_ID3DXFont(iface);
TRACE("iface %p, desc %p\n", iface, desc); TRACE("iface %p, desc %p\n", iface, desc);
if( !desc ) return D3DERR_INVALIDCALL; if( !desc ) return D3DERR_INVALIDCALL;
*desc = This->desc; *desc = font->desc;
return D3D_OK; return D3D_OK;
} }
static BOOL WINAPI ID3DXFontImpl_GetTextMetricsA(ID3DXFont *iface, TEXTMETRICA *metrics) static BOOL WINAPI ID3DXFontImpl_GetTextMetricsA(ID3DXFont *iface, TEXTMETRICA *metrics)
{ {
struct d3dx_font *This = impl_from_ID3DXFont(iface); struct d3dx_font *font = impl_from_ID3DXFont(iface);
TRACE("iface %p, metrics %p\n", iface, metrics); TRACE("iface %p, metrics %p\n", iface, metrics);
return GetTextMetricsA(This->hdc, metrics); return GetTextMetricsA(font->hdc, metrics);
} }
static BOOL WINAPI ID3DXFontImpl_GetTextMetricsW(ID3DXFont *iface, TEXTMETRICW *metrics) static BOOL WINAPI ID3DXFontImpl_GetTextMetricsW(ID3DXFont *iface, TEXTMETRICW *metrics)
{ {
struct d3dx_font *This = impl_from_ID3DXFont(iface); struct d3dx_font *font = impl_from_ID3DXFont(iface);
TRACE("iface %p, metrics %p\n", iface, metrics); TRACE("iface %p, metrics %p\n", iface, metrics);
return GetTextMetricsW(This->hdc, metrics); return GetTextMetricsW(font->hdc, metrics);
} }
static HDC WINAPI ID3DXFontImpl_GetDC(ID3DXFont *iface) static HDC WINAPI ID3DXFontImpl_GetDC(ID3DXFont *iface)
{ {
struct d3dx_font *This = impl_from_ID3DXFont(iface); struct d3dx_font *font = impl_from_ID3DXFont(iface);
TRACE("iface %p\n", iface); TRACE("iface %p\n", iface);
return This->hdc; return font->hdc;
} }
static HRESULT WINAPI ID3DXFontImpl_GetGlyphData(ID3DXFont *iface, UINT glyph, static HRESULT WINAPI ID3DXFontImpl_GetGlyphData(ID3DXFont *iface, UINT glyph,
@ -158,10 +201,156 @@ static HRESULT WINAPI ID3DXFontImpl_PreloadCharacters(ID3DXFont *iface, UINT fir
return S_OK; return S_OK;
} }
static uint32_t morton_decode(uint32_t x)
{
x &= 0x55555555;
x = (x ^ (x >> 1)) & 0x33333333;
x = (x ^ (x >> 2)) & 0x0f0f0f0f;
x = (x ^ (x >> 4)) & 0x00ff00ff;
x = (x ^ (x >> 8)) & 0x0000ffff;
return x;
}
/* The glyphs are stored in a grid. Cell sizes vary between different font
* sizes.
*
* The grid is filled in Morton order:
* 1 2 5 6 17 18 21 22
* 3 4 7 8 19 20 23 24
* 9 10 13 14 25 26 29 30
* 11 12 15 16 27 28 31 32
* 33 34 ...
* ...
*
* i.e. we try to fill one small square, then three equal-sized squares so
* that we get one big square, etc.
*
* The glyphs are positioned around their baseline, which is located at y
* position glyph_size * i + tmAscent. Concerning the x position, the glyphs
* are centered around glyph_size * (i + 0.5). */
static HRESULT WINAPI ID3DXFontImpl_PreloadGlyphs(ID3DXFont *iface, UINT first, UINT last) static HRESULT WINAPI ID3DXFontImpl_PreloadGlyphs(ID3DXFont *iface, UINT first, UINT last)
{ {
FIXME("iface %p, first %u, last %u stub!\n", iface, first, last); static const MAT2 mat = { {0,1}, {0,0}, {0,0}, {0,1} };
return E_NOTIMPL; struct d3dx_font *font = impl_from_ID3DXFont(iface);
IDirect3DTexture9 *current_texture = NULL;
unsigned int size, stride, glyph, x, y;
struct d3dx_glyph *current_glyph;
D3DLOCKED_RECT lockrect;
GLYPHMETRICS metrics;
BOOL mapped = FALSE;
DWORD *pixel_data;
BYTE *buffer;
HRESULT hr;
TRACE("iface %p, first %u, last %u.\n", iface, first, last);
if (last < first)
return D3D_OK;
if (font->texture_count)
current_texture = font->textures[font->texture_count - 1];
for (glyph = first; glyph <= last; ++glyph)
{
if (wine_rb_get(&font->glyph_tree, ULongToPtr(glyph)))
continue;
current_glyph = heap_alloc(sizeof(*current_glyph));
if (!current_glyph)
{
if (mapped)
IDirect3DTexture9_UnlockRect(current_texture, 0);
return E_OUTOFMEMORY;
}
current_glyph->id = glyph;
current_glyph->texture = NULL;
wine_rb_put(&font->glyph_tree, ULongToPtr(current_glyph->id), &current_glyph->entry);
size = GetGlyphOutlineW(font->hdc, glyph, GGO_GLYPH_INDEX | GGO_GRAY8_BITMAP, &metrics, 0, NULL, &mat);
if (size == GDI_ERROR)
{
WARN("GetGlyphOutlineW failed.\n");
continue;
}
if (!size)
continue;
buffer = heap_alloc(size);
if (!buffer)
{
if (mapped)
IDirect3DTexture9_UnlockRect(current_texture, 0);
return E_OUTOFMEMORY;
}
GetGlyphOutlineW(font->hdc, glyph, GGO_GLYPH_INDEX | GGO_GRAY8_BITMAP, &metrics, size, buffer, &mat);
if (font->texture_pos == font->glyphs_per_texture)
{
unsigned int new_texture_count = font->texture_count + 1;
IDirect3DTexture9 **new_textures;
if (mapped)
IDirect3DTexture9_UnlockRect(current_texture, 0);
mapped = FALSE;
new_textures = heap_realloc(font->textures, new_texture_count * sizeof(*new_textures));
if (!new_textures)
{
heap_free(buffer);
return E_OUTOFMEMORY;
}
font->textures = new_textures;
if (FAILED(hr = IDirect3DDevice9_CreateTexture(font->device, font->texture_size,
font->texture_size, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED,
&font->textures[font->texture_count], NULL)))
{
heap_free(buffer);
return hr;
}
current_texture = font->textures[font->texture_count++];
font->texture_pos = 0;
}
if (!mapped)
{
if (FAILED(hr = IDirect3DTexture9_LockRect(current_texture, 0, &lockrect, NULL, 0)))
{
heap_free(buffer);
return hr;
}
mapped = TRUE;
}
x = morton_decode(font->texture_pos) * font->glyph_size;
y = morton_decode(font->texture_pos >> 1) * font->glyph_size;
current_glyph->black_box.left = x - metrics.gmptGlyphOrigin.x + font->glyph_size / 2
- metrics.gmBlackBoxX / 2;
current_glyph->black_box.top = y - metrics.gmptGlyphOrigin.y + font->metrics.tmAscent + 1;
current_glyph->black_box.right = current_glyph->black_box.left + metrics.gmBlackBoxX;
current_glyph->black_box.bottom = current_glyph->black_box.top + metrics.gmBlackBoxY;
current_glyph->cell_inc.x = metrics.gmptGlyphOrigin.x - 1;
current_glyph->cell_inc.y = font->metrics.tmAscent - metrics.gmptGlyphOrigin.y - 1;
current_glyph->texture = current_texture;
pixel_data = lockrect.pBits;
stride = (metrics.gmBlackBoxX + 3) & ~3;
for (y = 0; y < metrics.gmBlackBoxY; ++y)
for (x = 0; x < metrics.gmBlackBoxX; ++x)
pixel_data[(current_glyph->black_box.top + y) * lockrect.Pitch / 4
+ current_glyph->black_box.left + x] =
(buffer[y * stride + x] * 255 / 64 << 24) | 0x00ffffffu;
heap_free(buffer);
++font->texture_pos;
}
if (mapped)
IDirect3DTexture9_UnlockRect(current_texture, 0);
return D3D_OK;
} }
static HRESULT WINAPI ID3DXFontImpl_PreloadTextA(ID3DXFont *iface, const char *string, INT count) static HRESULT WINAPI ID3DXFontImpl_PreloadTextA(ID3DXFont *iface, const char *string, INT count)
@ -295,8 +484,8 @@ HRESULT WINAPI D3DXCreateFontIndirectA(IDirect3DDevice9 *device, const D3DXFONT_
HRESULT WINAPI D3DXCreateFontIndirectW(IDirect3DDevice9 *device, const D3DXFONT_DESCW *desc, ID3DXFont **font) HRESULT WINAPI D3DXCreateFontIndirectW(IDirect3DDevice9 *device, const D3DXFONT_DESCW *desc, ID3DXFont **font)
{ {
D3DDEVICE_CREATION_PARAMETERS cpars; D3DDEVICE_CREATION_PARAMETERS cpars;
D3DDISPLAYMODE mode;
struct d3dx_font *object; struct d3dx_font *object;
D3DDISPLAYMODE mode;
IDirect3D9 *d3d; IDirect3D9 *d3d;
HRESULT hr; HRESULT hr;
@ -309,39 +498,63 @@ HRESULT WINAPI D3DXCreateFontIndirectW(IDirect3DDevice9 *device, const D3DXFONT_
IDirect3DDevice9_GetCreationParameters(device, &cpars); IDirect3DDevice9_GetCreationParameters(device, &cpars);
IDirect3DDevice9_GetDisplayMode(device, 0, &mode); IDirect3DDevice9_GetDisplayMode(device, 0, &mode);
hr = IDirect3D9_CheckDeviceFormat(d3d, cpars.AdapterOrdinal, cpars.DeviceType, mode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8R8G8B8); hr = IDirect3D9_CheckDeviceFormat(d3d, cpars.AdapterOrdinal, cpars.DeviceType, mode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8R8G8B8);
if(FAILED(hr)) { if (FAILED(hr))
{
IDirect3D9_Release(d3d); IDirect3D9_Release(d3d);
return D3DXERR_INVALIDDATA; return D3DXERR_INVALIDDATA;
} }
IDirect3D9_Release(d3d); IDirect3D9_Release(d3d);
object = HeapAlloc(GetProcessHeap(), 0, sizeof(struct d3dx_font)); object = heap_alloc_zero(sizeof(*object));
if(object==NULL) { if (!object)
*font=NULL; {
*font = NULL;
return E_OUTOFMEMORY; return E_OUTOFMEMORY;
} }
object->ID3DXFont_iface.lpVtbl = &D3DXFont_Vtbl; object->ID3DXFont_iface.lpVtbl = &D3DXFont_Vtbl;
object->ref=1; object->ref = 1;
object->device=device; object->device = device;
object->desc=*desc; object->desc = *desc;
object->hdc = CreateCompatibleDC(NULL); object->hdc = CreateCompatibleDC(NULL);
if( !object->hdc ) { if (!object->hdc)
HeapFree(GetProcessHeap(), 0, object); {
heap_free(object);
return D3DXERR_INVALIDDATA; return D3DXERR_INVALIDDATA;
} }
object->hfont = CreateFontW(desc->Height, desc->Width, 0, 0, desc->Weight, desc->Italic, FALSE, FALSE, desc->CharSet, object->hfont = CreateFontW(desc->Height, desc->Width, 0, 0, desc->Weight, desc->Italic, FALSE, FALSE, desc->CharSet,
desc->OutputPrecision, CLIP_DEFAULT_PRECIS, desc->Quality, desc->PitchAndFamily, desc->FaceName); desc->OutputPrecision, CLIP_DEFAULT_PRECIS, desc->Quality, desc->PitchAndFamily, desc->FaceName);
if( !object->hfont ) { if (!object->hfont)
{
DeleteDC(object->hdc); DeleteDC(object->hdc);
HeapFree(GetProcessHeap(), 0, object); heap_free(object);
return D3DXERR_INVALIDDATA; return D3DXERR_INVALIDDATA;
} }
SelectObject(object->hdc, object->hfont); SelectObject(object->hdc, object->hfont);
wine_rb_init(&object->glyph_tree, glyph_rb_compare);
if (!GetTextMetricsW(object->hdc, &object->metrics))
{
DeleteObject(object->hfont);
DeleteDC(object->hdc);
heap_free(object);
return D3DXERR_INVALIDDATA;
}
object->glyph_size = make_pow2(object->metrics.tmHeight);
object->texture_size = object->glyph_size;
if (object->glyph_size < 256)
object->texture_size = min(256, object->texture_size * 16);
object->glyphs_per_texture = object->texture_size * object->texture_size
/ (object->glyph_size * object->glyph_size);
object->texture_pos = object->glyphs_per_texture;
IDirect3DDevice9_AddRef(device); IDirect3DDevice9_AddRef(device);
*font=&object->ID3DXFont_iface; *font = &object->ID3DXFont_iface;
return D3D_OK; return D3D_OK;
} }

View File

@ -556,7 +556,7 @@ static void test_ID3DXFont(IDirect3DDevice9 *device)
hr = ID3DXFont_PreloadCharacters(font, 'b', 'a'); hr = ID3DXFont_PreloadCharacters(font, 'b', 'a');
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr); ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = ID3DXFont_PreloadGlyphs(font, 1, 0); hr = ID3DXFont_PreloadGlyphs(font, 1, 0);
todo_wine ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr); ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
hr = ID3DXFont_PreloadCharacters(font, 'a', 'a'); hr = ID3DXFont_PreloadCharacters(font, 'a', 'a');
ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr); ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
@ -586,6 +586,7 @@ static void test_ID3DXFont(IDirect3DDevice9 *device)
ret = ID3DXFont_GetTextMetricsW(font, &tm); ret = ID3DXFont_GetTextMetricsW(font, &tm);
ok(ret, "Got unexpected ret %#x.\n", ret); ok(ret, "Got unexpected ret %#x.\n", ret);
ok(blackbox.right - blackbox.left == glyph_metrics.gmBlackBoxX + 2, "Character %c: got %d, expected %d.\n", ok(blackbox.right - blackbox.left == glyph_metrics.gmBlackBoxX + 2, "Character %c: got %d, expected %d.\n",
c, blackbox.right - blackbox.left, glyph_metrics.gmBlackBoxX + 2); c, blackbox.right - blackbox.left, glyph_metrics.gmBlackBoxX + 2);
ok(blackbox.bottom - blackbox.top == glyph_metrics.gmBlackBoxY + 2, "Character %c: got %d, expected %d.\n", ok(blackbox.bottom - blackbox.top == glyph_metrics.gmBlackBoxY + 2, "Character %c: got %d, expected %d.\n",
@ -603,7 +604,7 @@ static void test_ID3DXFont(IDirect3DDevice9 *device)
/* Test multiple textures */ /* Test multiple textures */
hr = ID3DXFont_PreloadGlyphs(font, 0, 1000); hr = ID3DXFont_PreloadGlyphs(font, 0, 1000);
todo_wine ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr); ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
/* Test glyphs that are not rendered */ /* Test glyphs that are not rendered */
for (glyph = 1; glyph < 4; ++glyph) for (glyph = 1; glyph < 4; ++glyph)

View File

@ -15,4 +15,4 @@ files: {include/d3dx9.h: sdk/include/dxsdk/d3dx9.h, include/d3dx9anim.h: sdk/inc
include/d3dx9mesh.h: sdk/include/dxsdk/d3dx9mesh.h, include/d3dx9of.h: sdk/include/dxsdk/d3dx9of.h, include/d3dx9mesh.h: sdk/include/dxsdk/d3dx9mesh.h, include/d3dx9of.h: sdk/include/dxsdk/d3dx9of.h,
include/d3dx9shader.h: sdk/include/dxsdk/d3dx9shader.h, include/d3dx9shape.h: sdk/include/dxsdk/d3dx9shape.h, include/d3dx9shader.h: sdk/include/dxsdk/d3dx9shader.h, include/d3dx9shape.h: sdk/include/dxsdk/d3dx9shape.h,
include/d3dx9tex.h: sdk/include/dxsdk/d3dx9tex.h, include/d3dx9xof.h: sdk/include/dxsdk/d3dx9xof.h} include/d3dx9tex.h: sdk/include/dxsdk/d3dx9tex.h, include/d3dx9xof.h: sdk/include/dxsdk/d3dx9xof.h}
tags: {wine: 1b0ad92db1c21d5052c45be2291759d60553476c} tags: {wine: ac5687463700b070fc1eecca67c4a46ec15127fa}