d2d1: Move bitmap creation helpers to common location.

Signed-off-by: Nikolay Sivov <nsivov@codeweavers.com>
Signed-off-by: Henri Verbeet <hverbeet@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Nikolay Sivov 2016-03-29 21:30:38 +03:00 committed by Alexandre Julliard
parent 6e1f687e4d
commit c1ea9df0a0
3 changed files with 137 additions and 132 deletions

View File

@ -20,6 +20,7 @@
#include "wine/port.h"
#include "d2d1_private.h"
#include "wincodec.h"
WINE_DEFAULT_DEBUG_CHANNEL(d2d);
@ -238,8 +239,8 @@ static void d2d_bitmap_init(struct d2d_bitmap *bitmap, ID2D1Factory *factory,
}
}
HRESULT d2d_bitmap_init_memory(struct d2d_bitmap *bitmap, ID2D1Factory *factory, ID3D10Device *device,
D2D1_SIZE_U size, const void *src_data, UINT32 pitch, const D2D1_BITMAP_PROPERTIES *desc)
HRESULT d2d_bitmap_create(ID2D1Factory *factory, ID3D10Device *device, D2D1_SIZE_U size, const void *src_data,
UINT32 pitch, const D2D1_BITMAP_PROPERTIES *desc, struct d2d_bitmap **bitmap)
{
D3D10_SUBRESOURCE_DATA resource_data;
D3D10_TEXTURE2D_DESC texture_desc;
@ -284,14 +285,19 @@ HRESULT d2d_bitmap_init_memory(struct d2d_bitmap *bitmap, ID2D1Factory *factory,
return hr;
}
d2d_bitmap_init(bitmap, factory, view, size, desc);
ID3D10ShaderResourceView_Release(view);
return S_OK;
if ((*bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**bitmap))))
{
d2d_bitmap_init(*bitmap, factory, view, size, desc);
TRACE("Created bitmap %p.\n", *bitmap);
}
HRESULT d2d_bitmap_init_shared(struct d2d_bitmap *bitmap, ID2D1Factory *factory, ID3D10Device *target_device,
REFIID iid, void *data, const D2D1_BITMAP_PROPERTIES *desc)
ID3D10ShaderResourceView_Release(view);
return *bitmap ? S_OK : E_OUTOFMEMORY;
}
HRESULT d2d_bitmap_create_shared(ID2D1Factory *factory, ID3D10Device *target_device,
REFIID iid, void *data, const D2D1_BITMAP_PROPERTIES *desc, struct d2d_bitmap **bitmap)
{
if (IsEqualGUID(iid, &IID_ID2D1Bitmap))
{
@ -322,7 +328,11 @@ HRESULT d2d_bitmap_init_shared(struct d2d_bitmap *bitmap, ID2D1Factory *factory,
return D2DERR_UNSUPPORTED_PIXEL_FORMAT;
}
d2d_bitmap_init(bitmap, factory, src_impl->view, src_impl->pixel_size, desc);
if (!(*bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**bitmap))))
return E_OUTOFMEMORY;
d2d_bitmap_init(*bitmap, factory, src_impl->view, src_impl->pixel_size, desc);
TRACE("Created bitmap %p.\n", *bitmap);
return S_OK;
}
@ -332,6 +342,109 @@ HRESULT d2d_bitmap_init_shared(struct d2d_bitmap *bitmap, ID2D1Factory *factory,
return E_INVALIDARG;
}
HRESULT d2d_bitmap_create_from_wic_bitmap(ID2D1Factory *factory, ID3D10Device *device, IWICBitmapSource *bitmap_source,
const D2D1_BITMAP_PROPERTIES *desc, struct d2d_bitmap **bitmap)
{
const D2D1_PIXEL_FORMAT *d2d_format;
D2D1_BITMAP_PROPERTIES bitmap_desc;
WICPixelFormatGUID wic_format;
unsigned int bpp, data_size;
D2D1_SIZE_U size;
unsigned int i;
WICRect rect;
UINT32 pitch;
HRESULT hr;
void *data;
static const struct
{
const WICPixelFormatGUID *wic;
D2D1_PIXEL_FORMAT d2d;
}
format_lookup[] =
{
{&GUID_WICPixelFormat32bppPBGRA, {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED}},
{&GUID_WICPixelFormat32bppBGR, {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE}},
};
if (FAILED(hr = IWICBitmapSource_GetSize(bitmap_source, &size.width, &size.height)))
{
WARN("Failed to get bitmap size, hr %#x.\n", hr);
return hr;
}
if (!desc)
{
bitmap_desc.pixelFormat.format = DXGI_FORMAT_UNKNOWN;
bitmap_desc.pixelFormat.alphaMode = D2D1_ALPHA_MODE_UNKNOWN;
bitmap_desc.dpiX = 0.0f;
bitmap_desc.dpiY = 0.0f;
}
else
{
bitmap_desc = *desc;
}
if (FAILED(hr = IWICBitmapSource_GetPixelFormat(bitmap_source, &wic_format)))
{
WARN("Failed to get bitmap format, hr %#x.\n", hr);
return hr;
}
for (i = 0, d2d_format = NULL; i < sizeof(format_lookup) / sizeof(*format_lookup); ++i)
{
if (IsEqualGUID(&wic_format, format_lookup[i].wic))
{
d2d_format = &format_lookup[i].d2d;
break;
}
}
if (!d2d_format)
{
WARN("Unsupported WIC bitmap format %s.\n", debugstr_guid(&wic_format));
return D2DERR_UNSUPPORTED_PIXEL_FORMAT;
}
if (bitmap_desc.pixelFormat.format == DXGI_FORMAT_UNKNOWN)
bitmap_desc.pixelFormat.format = d2d_format->format;
if (bitmap_desc.pixelFormat.alphaMode == D2D1_ALPHA_MODE_UNKNOWN)
bitmap_desc.pixelFormat.alphaMode = d2d_format->alphaMode;
switch (bitmap_desc.pixelFormat.format)
{
case DXGI_FORMAT_B8G8R8A8_UNORM:
bpp = 4;
break;
default:
FIXME("Unhandled format %#x.\n", bitmap_desc.pixelFormat.format);
return D2DERR_UNSUPPORTED_PIXEL_FORMAT;
}
pitch = ((bpp * size.width) + 15) & ~15;
data_size = pitch * size.height;
if (!(data = HeapAlloc(GetProcessHeap(), 0, data_size)))
return E_OUTOFMEMORY;
rect.X = 0;
rect.Y = 0;
rect.Width = size.width;
rect.Height = size.height;
if (FAILED(hr = IWICBitmapSource_CopyPixels(bitmap_source, &rect, pitch, data_size, data)))
{
WARN("Failed to copy bitmap pixels, hr %#x.\n", hr);
HeapFree(GetProcessHeap(), 0, data);
return hr;
}
hr = d2d_bitmap_create(factory, device, size, data, pitch, &bitmap_desc, bitmap);
HeapFree(GetProcessHeap(), 0, data);
return hr;
}
struct d2d_bitmap *unsafe_impl_from_ID2D1Bitmap(ID2D1Bitmap *iface)
{
if (!iface)

View File

@ -204,10 +204,12 @@ struct d2d_bitmap
float dpi_y;
};
HRESULT d2d_bitmap_init_memory(struct d2d_bitmap *bitmap, ID2D1Factory *factory, ID3D10Device *device,
D2D1_SIZE_U size, const void *src_data, UINT32 pitch, const D2D1_BITMAP_PROPERTIES *desc) DECLSPEC_HIDDEN;
HRESULT d2d_bitmap_init_shared(struct d2d_bitmap *bitmap, ID2D1Factory *factory, ID3D10Device *device,
REFIID iid, void *data, const D2D1_BITMAP_PROPERTIES *desc) DECLSPEC_HIDDEN;
HRESULT d2d_bitmap_create(ID2D1Factory *factory, ID3D10Device *device, D2D1_SIZE_U size, const void *src_data,
UINT32 pitch, const D2D1_BITMAP_PROPERTIES *desc, struct d2d_bitmap **bitmap) DECLSPEC_HIDDEN;
HRESULT d2d_bitmap_create_shared(ID2D1Factory *factory, ID3D10Device *device, REFIID iid, void *data,
const D2D1_BITMAP_PROPERTIES *desc, struct d2d_bitmap **bitmap) DECLSPEC_HIDDEN;
HRESULT d2d_bitmap_create_from_wic_bitmap(ID2D1Factory *factory, ID3D10Device *device, IWICBitmapSource *bitmap_source,
const D2D1_BITMAP_PROPERTIES *desc, struct d2d_bitmap **bitmap) DECLSPEC_HIDDEN;
struct d2d_bitmap *unsafe_impl_from_ID2D1Bitmap(ID2D1Bitmap *iface) DECLSPEC_HIDDEN;
struct d2d_state_block

View File

@ -20,7 +20,6 @@
#include "wine/port.h"
#include "d2d1_private.h"
#include "wincodec.h"
WINE_DEFAULT_DEBUG_CHANNEL(d2d);
@ -309,124 +308,25 @@ static HRESULT STDMETHODCALLTYPE d2d_d3d_render_target_CreateBitmap(ID2D1RenderT
TRACE("iface %p, size {%u, %u}, src_data %p, pitch %u, desc %p, bitmap %p.\n",
iface, size.width, size.height, src_data, pitch, desc, bitmap);
if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
return E_OUTOFMEMORY;
if (FAILED(hr = d2d_bitmap_init_memory(object, render_target->factory, render_target->device, size, src_data, pitch, desc)))
{
WARN("Failed to initialize bitmap, hr %#x.\n", hr);
HeapFree(GetProcessHeap(), 0, object);
return hr;
}
TRACE("Created bitmap %p.\n", object);
if (SUCCEEDED(hr = d2d_bitmap_create(render_target->factory, render_target->device, size, src_data, pitch, desc, &object)))
*bitmap = &object->ID2D1Bitmap_iface;
return S_OK;
return hr;
}
static HRESULT STDMETHODCALLTYPE d2d_d3d_render_target_CreateBitmapFromWicBitmap(ID2D1RenderTarget *iface,
IWICBitmapSource *bitmap_source, const D2D1_BITMAP_PROPERTIES *desc, ID2D1Bitmap **bitmap)
{
const D2D1_PIXEL_FORMAT *d2d_format;
D2D1_BITMAP_PROPERTIES bitmap_desc;
WICPixelFormatGUID wic_format;
unsigned int bpp, data_size;
D2D1_SIZE_U size;
unsigned int i;
WICRect rect;
UINT32 pitch;
struct d2d_d3d_render_target *render_target = impl_from_ID2D1RenderTarget(iface);
struct d2d_bitmap *object;
HRESULT hr;
void *data;
static const struct
{
const WICPixelFormatGUID *wic;
D2D1_PIXEL_FORMAT d2d;
}
format_lookup[] =
{
{&GUID_WICPixelFormat32bppPBGRA, {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED}},
{&GUID_WICPixelFormat32bppBGR, {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE}},
};
TRACE("iface %p, bitmap_source %p, desc %p, bitmap %p.\n",
iface, bitmap_source, desc, bitmap);
if (FAILED(hr = IWICBitmapSource_GetSize(bitmap_source, &size.width, &size.height)))
{
WARN("Failed to get bitmap size, hr %#x.\n", hr);
return hr;
}
if (!desc)
{
bitmap_desc.pixelFormat.format = DXGI_FORMAT_UNKNOWN;
bitmap_desc.pixelFormat.alphaMode = D2D1_ALPHA_MODE_UNKNOWN;
bitmap_desc.dpiX = 0.0f;
bitmap_desc.dpiY = 0.0f;
}
else
{
bitmap_desc = *desc;
}
if (FAILED(hr = IWICBitmapSource_GetPixelFormat(bitmap_source, &wic_format)))
{
WARN("Failed to get bitmap format, hr %#x.\n", hr);
return hr;
}
for (i = 0, d2d_format = NULL; i < sizeof(format_lookup) / sizeof(*format_lookup); ++i)
{
if (IsEqualGUID(&wic_format, format_lookup[i].wic))
{
d2d_format = &format_lookup[i].d2d;
break;
}
}
if (!d2d_format)
{
WARN("Unsupported WIC bitmap format %s.\n", debugstr_guid(&wic_format));
return D2DERR_UNSUPPORTED_PIXEL_FORMAT;
}
if (bitmap_desc.pixelFormat.format == DXGI_FORMAT_UNKNOWN)
bitmap_desc.pixelFormat.format = d2d_format->format;
if (bitmap_desc.pixelFormat.alphaMode == D2D1_ALPHA_MODE_UNKNOWN)
bitmap_desc.pixelFormat.alphaMode = d2d_format->alphaMode;
switch (bitmap_desc.pixelFormat.format)
{
case DXGI_FORMAT_B8G8R8A8_UNORM:
bpp = 4;
break;
default:
FIXME("Unhandled format %#x.\n", bitmap_desc.pixelFormat.format);
return D2DERR_UNSUPPORTED_PIXEL_FORMAT;
}
pitch = ((bpp * size.width) + 15) & ~15;
data_size = pitch * size.height;
if (!(data = HeapAlloc(GetProcessHeap(), 0, data_size)))
return E_OUTOFMEMORY;
rect.X = 0;
rect.Y = 0;
rect.Width = size.width;
rect.Height = size.height;
if (FAILED(hr = IWICBitmapSource_CopyPixels(bitmap_source, &rect, pitch, data_size, data)))
{
WARN("Failed to copy bitmap pixels, hr %#x.\n", hr);
HeapFree(GetProcessHeap(), 0, data);
return hr;
}
hr = d2d_d3d_render_target_CreateBitmap(iface, size, data, pitch, &bitmap_desc, bitmap);
HeapFree(GetProcessHeap(), 0, data);
if (SUCCEEDED(hr = d2d_bitmap_create_from_wic_bitmap(render_target->factory, render_target->device, bitmap_source,
desc, &object)))
*bitmap = &object->ID2D1Bitmap_iface;
return hr;
}
@ -441,20 +341,10 @@ static HRESULT STDMETHODCALLTYPE d2d_d3d_render_target_CreateSharedBitmap(ID2D1R
TRACE("iface %p, iid %s, data %p, desc %p, bitmap %p.\n",
iface, debugstr_guid(iid), data, desc, bitmap);
if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
return E_OUTOFMEMORY;
if (FAILED(hr = d2d_bitmap_init_shared(object, render_target->factory, render_target->device, iid, data, desc)))
{
WARN("Failed to initialize bitmap, hr %#x.\n", hr);
HeapFree(GetProcessHeap(), 0, object);
return hr;
}
TRACE("Created bitmap %p.\n", object);
if (SUCCEEDED(hr = d2d_bitmap_create_shared(render_target->factory, render_target->device, iid, data, desc, &object)))
*bitmap = &object->ID2D1Bitmap_iface;
return S_OK;
return hr;
}
static HRESULT STDMETHODCALLTYPE d2d_d3d_render_target_CreateBitmapBrush(ID2D1RenderTarget *iface,