wmvcore: Print the debug string and not the pointer to it.
[wine.git] / dlls / d2d1 / bitmap.c
blob6d9c3529242d3823fd8979cb8fcc60b2ffe94426
1 /*
2 * Copyright 2014 Henri Verbeet for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "config.h"
20 #include "wine/port.h"
22 #include "d2d1_private.h"
23 #include "wincodec.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d2d);
27 static inline struct d2d_bitmap *impl_from_ID2D1Bitmap(ID2D1Bitmap *iface)
29 return CONTAINING_RECORD(iface, struct d2d_bitmap, ID2D1Bitmap_iface);
32 static HRESULT STDMETHODCALLTYPE d2d_bitmap_QueryInterface(ID2D1Bitmap *iface, REFIID iid, void **out)
34 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
36 if (IsEqualGUID(iid, &IID_ID2D1Bitmap)
37 || IsEqualGUID(iid, &IID_ID2D1Resource)
38 || IsEqualGUID(iid, &IID_IUnknown))
40 ID2D1Bitmap_AddRef(iface);
41 *out = iface;
42 return S_OK;
45 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
47 *out = NULL;
48 return E_NOINTERFACE;
51 static ULONG STDMETHODCALLTYPE d2d_bitmap_AddRef(ID2D1Bitmap *iface)
53 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap(iface);
54 ULONG refcount = InterlockedIncrement(&bitmap->refcount);
56 TRACE("%p increasing refcount to %u.\n", iface, refcount);
58 return refcount;
61 static ULONG STDMETHODCALLTYPE d2d_bitmap_Release(ID2D1Bitmap *iface)
63 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap(iface);
64 ULONG refcount = InterlockedDecrement(&bitmap->refcount);
66 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
68 if (!refcount)
70 ID3D10ShaderResourceView_Release(bitmap->view);
71 ID2D1Factory_Release(bitmap->factory);
72 HeapFree(GetProcessHeap(), 0, bitmap);
75 return refcount;
78 static void STDMETHODCALLTYPE d2d_bitmap_GetFactory(ID2D1Bitmap *iface, ID2D1Factory **factory)
80 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap(iface);
82 TRACE("iface %p, factory %p.\n", iface, factory);
84 ID2D1Factory_AddRef(*factory = bitmap->factory);
87 static D2D1_SIZE_F * STDMETHODCALLTYPE d2d_bitmap_GetSize(ID2D1Bitmap *iface, D2D1_SIZE_F *size)
89 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap(iface);
91 TRACE("iface %p, size %p.\n", iface, size);
93 size->width = bitmap->pixel_size.width / (bitmap->dpi_x / 96.0f);
94 size->height = bitmap->pixel_size.height / (bitmap->dpi_y / 96.0f);
95 return size;
98 static D2D1_SIZE_U * STDMETHODCALLTYPE d2d_bitmap_GetPixelSize(ID2D1Bitmap *iface, D2D1_SIZE_U *pixel_size)
100 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap(iface);
102 TRACE("iface %p, pixel_size %p.\n", iface, pixel_size);
104 *pixel_size = bitmap->pixel_size;
105 return pixel_size;
108 static D2D1_PIXEL_FORMAT * STDMETHODCALLTYPE d2d_bitmap_GetPixelFormat(ID2D1Bitmap *iface, D2D1_PIXEL_FORMAT *format)
110 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap(iface);
112 TRACE("iface %p, format %p.\n", iface, format);
114 *format = bitmap->format;
115 return format;
118 static void STDMETHODCALLTYPE d2d_bitmap_GetDpi(ID2D1Bitmap *iface, float *dpi_x, float *dpi_y)
120 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap(iface);
122 TRACE("iface %p, dpi_x %p, dpi_y %p.\n", iface, dpi_x, dpi_y);
124 *dpi_x = bitmap->dpi_x;
125 *dpi_y = bitmap->dpi_y;
128 static HRESULT STDMETHODCALLTYPE d2d_bitmap_CopyFromBitmap(ID2D1Bitmap *iface,
129 const D2D1_POINT_2U *dst_point, ID2D1Bitmap *bitmap, const D2D1_RECT_U *src_rect)
131 FIXME("iface %p, dst_point %p, bitmap %p, src_rect %p stub!\n", iface, dst_point, bitmap, src_rect);
133 return E_NOTIMPL;
136 static HRESULT STDMETHODCALLTYPE d2d_bitmap_CopyFromRenderTarget(ID2D1Bitmap *iface,
137 const D2D1_POINT_2U *dst_point, ID2D1RenderTarget *render_target, const D2D1_RECT_U *src_rect)
139 FIXME("iface %p, dst_point %p, render_target %p, src_rect %p stub!\n", iface, dst_point, render_target, src_rect);
141 return E_NOTIMPL;
144 static HRESULT STDMETHODCALLTYPE d2d_bitmap_CopyFromMemory(ID2D1Bitmap *iface,
145 const D2D1_RECT_U *dst_rect, const void *src_data, UINT32 pitch)
147 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap(iface);
148 ID3D10Device *device;
149 ID3D10Resource *dst;
150 D3D10_BOX box;
152 TRACE("iface %p, dst_rect %p, src_data %p, pitch %u.\n", iface, dst_rect, src_data, pitch);
154 if (dst_rect)
156 box.left = dst_rect->left;
157 box.top = dst_rect->top;
158 box.front = 0;
159 box.right = dst_rect->right;
160 box.bottom = dst_rect->bottom;
161 box.back = 1;
164 ID3D10ShaderResourceView_GetResource(bitmap->view, &dst);
165 ID3D10ShaderResourceView_GetDevice(bitmap->view, &device);
166 ID3D10Device_UpdateSubresource(device, dst, 0, dst_rect ? &box : NULL, src_data, pitch, 0);
167 ID3D10Device_Release(device);
168 ID3D10Resource_Release(dst);
170 return S_OK;
173 static const struct ID2D1BitmapVtbl d2d_bitmap_vtbl =
175 d2d_bitmap_QueryInterface,
176 d2d_bitmap_AddRef,
177 d2d_bitmap_Release,
178 d2d_bitmap_GetFactory,
179 d2d_bitmap_GetSize,
180 d2d_bitmap_GetPixelSize,
181 d2d_bitmap_GetPixelFormat,
182 d2d_bitmap_GetDpi,
183 d2d_bitmap_CopyFromBitmap,
184 d2d_bitmap_CopyFromRenderTarget,
185 d2d_bitmap_CopyFromMemory,
188 static BOOL format_supported(const D2D1_PIXEL_FORMAT *format)
190 unsigned int i;
192 static const D2D1_PIXEL_FORMAT supported_formats[] =
194 {DXGI_FORMAT_R32G32B32A32_FLOAT, D2D1_ALPHA_MODE_PREMULTIPLIED},
195 {DXGI_FORMAT_R32G32B32A32_FLOAT, D2D1_ALPHA_MODE_IGNORE },
196 {DXGI_FORMAT_R16G16B16A16_FLOAT, D2D1_ALPHA_MODE_PREMULTIPLIED},
197 {DXGI_FORMAT_R16G16B16A16_FLOAT, D2D1_ALPHA_MODE_IGNORE },
198 {DXGI_FORMAT_R16G16B16A16_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED},
199 {DXGI_FORMAT_R16G16B16A16_UNORM, D2D1_ALPHA_MODE_IGNORE },
200 {DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED},
201 {DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_IGNORE },
202 {DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, D2D1_ALPHA_MODE_PREMULTIPLIED},
203 {DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, D2D1_ALPHA_MODE_IGNORE },
204 {DXGI_FORMAT_A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED},
205 {DXGI_FORMAT_A8_UNORM, D2D1_ALPHA_MODE_STRAIGHT },
206 {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED},
207 {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE },
208 {DXGI_FORMAT_B8G8R8X8_UNORM, D2D1_ALPHA_MODE_IGNORE },
209 {DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, D2D1_ALPHA_MODE_PREMULTIPLIED},
210 {DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, D2D1_ALPHA_MODE_IGNORE },
213 for (i = 0; i < ARRAY_SIZE(supported_formats); ++i)
215 if (supported_formats[i].format == format->format
216 && supported_formats[i].alphaMode == format->alphaMode)
217 return TRUE;
220 return FALSE;
223 static void d2d_bitmap_init(struct d2d_bitmap *bitmap, ID2D1Factory *factory,
224 ID3D10ShaderResourceView *view, D2D1_SIZE_U size, const D2D1_BITMAP_PROPERTIES *desc)
226 bitmap->ID2D1Bitmap_iface.lpVtbl = &d2d_bitmap_vtbl;
227 bitmap->refcount = 1;
228 ID2D1Factory_AddRef(bitmap->factory = factory);
229 ID3D10ShaderResourceView_AddRef(bitmap->view = view);
230 bitmap->pixel_size = size;
231 bitmap->format = desc->pixelFormat;
232 bitmap->dpi_x = desc->dpiX;
233 bitmap->dpi_y = desc->dpiY;
235 if (bitmap->dpi_x == 0.0f && bitmap->dpi_y == 0.0f)
237 bitmap->dpi_x = 96.0f;
238 bitmap->dpi_y = 96.0f;
242 HRESULT d2d_bitmap_create(ID2D1Factory *factory, ID3D10Device *device, D2D1_SIZE_U size, const void *src_data,
243 UINT32 pitch, const D2D1_BITMAP_PROPERTIES *desc, struct d2d_bitmap **bitmap)
245 D3D10_SUBRESOURCE_DATA resource_data;
246 D3D10_TEXTURE2D_DESC texture_desc;
247 ID3D10ShaderResourceView *view;
248 ID3D10Texture2D *texture;
249 HRESULT hr;
251 if (!format_supported(&desc->pixelFormat))
253 WARN("Tried to create bitmap with unsupported format {%#x / %#x}.\n",
254 desc->pixelFormat.format, desc->pixelFormat.alphaMode);
255 return D2DERR_UNSUPPORTED_PIXEL_FORMAT;
258 texture_desc.Width = size.width;
259 texture_desc.Height = size.height;
260 texture_desc.MipLevels = 1;
261 texture_desc.ArraySize = 1;
262 texture_desc.Format = desc->pixelFormat.format;
263 texture_desc.SampleDesc.Count = 1;
264 texture_desc.SampleDesc.Quality = 0;
265 texture_desc.Usage = D3D10_USAGE_DEFAULT;
266 texture_desc.BindFlags = D3D10_BIND_SHADER_RESOURCE;
267 texture_desc.CPUAccessFlags = 0;
268 texture_desc.MiscFlags = 0;
270 resource_data.pSysMem = src_data;
271 resource_data.SysMemPitch = pitch;
273 if (FAILED(hr = ID3D10Device_CreateTexture2D(device, &texture_desc,
274 src_data ? &resource_data : NULL, &texture)))
276 ERR("Failed to create texture, hr %#x.\n", hr);
277 return hr;
280 hr = ID3D10Device_CreateShaderResourceView(device, (ID3D10Resource *)texture, NULL, &view);
281 ID3D10Texture2D_Release(texture);
282 if (FAILED(hr))
284 ERR("Failed to create view, hr %#x.\n", hr);
285 return hr;
288 if ((*bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**bitmap))))
290 d2d_bitmap_init(*bitmap, factory, view, size, desc);
291 TRACE("Created bitmap %p.\n", *bitmap);
294 ID3D10ShaderResourceView_Release(view);
296 return *bitmap ? S_OK : E_OUTOFMEMORY;
299 HRESULT d2d_bitmap_create_shared(ID2D1RenderTarget *render_target, ID3D10Device *target_device,
300 REFIID iid, void *data, const D2D1_BITMAP_PROPERTIES *desc, struct d2d_bitmap **bitmap)
302 D2D1_BITMAP_PROPERTIES d;
303 ID2D1Factory *factory;
305 if (IsEqualGUID(iid, &IID_ID2D1Bitmap))
307 struct d2d_bitmap *src_impl = unsafe_impl_from_ID2D1Bitmap(data);
308 ID3D10Device *device;
309 HRESULT hr = S_OK;
311 ID2D1RenderTarget_GetFactory(render_target, &factory);
312 if (src_impl->factory != factory)
314 hr = D2DERR_WRONG_FACTORY;
315 goto failed;
318 ID3D10ShaderResourceView_GetDevice(src_impl->view, &device);
319 ID3D10Device_Release(device);
320 if (device != target_device)
322 hr = D2DERR_UNSUPPORTED_OPERATION;
323 goto failed;
326 if (!desc)
328 d.pixelFormat = src_impl->format;
329 d.dpiX = src_impl->dpi_x;
330 d.dpiY = src_impl->dpi_y;
331 desc = &d;
334 if (!format_supported(&desc->pixelFormat))
336 WARN("Tried to create bitmap with unsupported format {%#x / %#x}.\n",
337 desc->pixelFormat.format, desc->pixelFormat.alphaMode);
338 hr = D2DERR_UNSUPPORTED_PIXEL_FORMAT;
339 goto failed;
342 if (!(*bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**bitmap))))
344 hr = E_OUTOFMEMORY;
345 goto failed;
348 d2d_bitmap_init(*bitmap, factory, src_impl->view, src_impl->pixel_size, desc);
349 TRACE("Created bitmap %p.\n", *bitmap);
351 failed:
352 ID2D1Factory_Release(factory);
353 return hr;
356 if (IsEqualGUID(iid, &IID_IDXGISurface) || IsEqualGUID(iid, &IID_IDXGISurface1))
358 ID3D10ShaderResourceView *view;
359 DXGI_SURFACE_DESC surface_desc;
360 IDXGISurface *surface = data;
361 ID3D10Resource *resource;
362 D2D1_SIZE_U pixel_size;
363 ID3D10Device *device;
364 HRESULT hr;
366 if (FAILED(IDXGISurface_QueryInterface(surface, &IID_ID3D10Resource, (void **)&resource)))
368 WARN("Failed to get d3d resource from dxgi surface.\n");
369 return E_FAIL;
372 ID3D10Resource_GetDevice(resource, &device);
373 ID3D10Device_Release(device);
374 if (device != target_device)
376 ID3D10Resource_Release(resource);
377 return D2DERR_UNSUPPORTED_OPERATION;
380 hr = ID3D10Device_CreateShaderResourceView(target_device, resource, NULL, &view);
381 ID3D10Resource_Release(resource);
382 if (FAILED(hr))
384 WARN("Failed to create shader resource view, hr %#x.\n", hr);
385 return hr;
388 if (!(*bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**bitmap))))
390 ID3D10ShaderResourceView_Release(view);
391 return E_OUTOFMEMORY;
394 d = *desc;
395 if (d.dpiX == 0.0f || d.dpiY == 0.0f)
397 float dpi_x, dpi_y;
399 ID2D1RenderTarget_GetDpi(render_target, &dpi_x, &dpi_y);
400 if (d.dpiX == 0.0f)
401 d.dpiX = dpi_x;
402 if (d.dpiY == 0.0f)
403 d.dpiY = dpi_y;
406 if (FAILED(hr = IDXGISurface_GetDesc(surface, &surface_desc)))
408 WARN("Failed to get surface desc, hr %#x.\n", hr);
409 ID3D10ShaderResourceView_Release(view);
410 return hr;
413 pixel_size.width = surface_desc.Width;
414 pixel_size.height = surface_desc.Height;
416 ID2D1RenderTarget_GetFactory(render_target, &factory);
417 d2d_bitmap_init(*bitmap, factory, view, pixel_size, &d);
418 ID3D10ShaderResourceView_Release(view);
419 ID2D1Factory_Release(factory);
420 TRACE("Created bitmap %p.\n", *bitmap);
422 return S_OK;
425 WARN("Unhandled interface %s.\n", debugstr_guid(iid));
427 return E_INVALIDARG;
430 HRESULT d2d_bitmap_create_from_wic_bitmap(ID2D1Factory *factory, ID3D10Device *device, IWICBitmapSource *bitmap_source,
431 const D2D1_BITMAP_PROPERTIES *desc, struct d2d_bitmap **bitmap)
433 const D2D1_PIXEL_FORMAT *d2d_format;
434 D2D1_BITMAP_PROPERTIES bitmap_desc;
435 WICPixelFormatGUID wic_format;
436 unsigned int bpp, data_size;
437 D2D1_SIZE_U size;
438 unsigned int i;
439 WICRect rect;
440 UINT32 pitch;
441 HRESULT hr;
442 void *data;
444 static const struct
446 const WICPixelFormatGUID *wic;
447 D2D1_PIXEL_FORMAT d2d;
449 format_lookup[] =
451 {&GUID_WICPixelFormat32bppPBGRA, {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED}},
452 {&GUID_WICPixelFormat32bppBGR, {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE}},
455 if (FAILED(hr = IWICBitmapSource_GetSize(bitmap_source, &size.width, &size.height)))
457 WARN("Failed to get bitmap size, hr %#x.\n", hr);
458 return hr;
461 if (!desc)
463 bitmap_desc.pixelFormat.format = DXGI_FORMAT_UNKNOWN;
464 bitmap_desc.pixelFormat.alphaMode = D2D1_ALPHA_MODE_UNKNOWN;
465 bitmap_desc.dpiX = 0.0f;
466 bitmap_desc.dpiY = 0.0f;
468 else
470 bitmap_desc = *desc;
473 if (FAILED(hr = IWICBitmapSource_GetPixelFormat(bitmap_source, &wic_format)))
475 WARN("Failed to get bitmap format, hr %#x.\n", hr);
476 return hr;
479 for (i = 0, d2d_format = NULL; i < ARRAY_SIZE(format_lookup); ++i)
481 if (IsEqualGUID(&wic_format, format_lookup[i].wic))
483 d2d_format = &format_lookup[i].d2d;
484 break;
488 if (!d2d_format)
490 WARN("Unsupported WIC bitmap format %s.\n", debugstr_guid(&wic_format));
491 return D2DERR_UNSUPPORTED_PIXEL_FORMAT;
494 if (bitmap_desc.pixelFormat.format == DXGI_FORMAT_UNKNOWN)
495 bitmap_desc.pixelFormat.format = d2d_format->format;
496 if (bitmap_desc.pixelFormat.alphaMode == D2D1_ALPHA_MODE_UNKNOWN)
497 bitmap_desc.pixelFormat.alphaMode = d2d_format->alphaMode;
499 switch (bitmap_desc.pixelFormat.format)
501 case DXGI_FORMAT_B8G8R8A8_UNORM:
502 bpp = 4;
503 break;
505 default:
506 FIXME("Unhandled format %#x.\n", bitmap_desc.pixelFormat.format);
507 return D2DERR_UNSUPPORTED_PIXEL_FORMAT;
510 pitch = ((bpp * size.width) + 15) & ~15;
511 data_size = pitch * size.height;
512 if (!(data = HeapAlloc(GetProcessHeap(), 0, data_size)))
513 return E_OUTOFMEMORY;
515 rect.X = 0;
516 rect.Y = 0;
517 rect.Width = size.width;
518 rect.Height = size.height;
519 if (FAILED(hr = IWICBitmapSource_CopyPixels(bitmap_source, &rect, pitch, data_size, data)))
521 WARN("Failed to copy bitmap pixels, hr %#x.\n", hr);
522 HeapFree(GetProcessHeap(), 0, data);
523 return hr;
526 hr = d2d_bitmap_create(factory, device, size, data, pitch, &bitmap_desc, bitmap);
528 HeapFree(GetProcessHeap(), 0, data);
530 return hr;
533 struct d2d_bitmap *unsafe_impl_from_ID2D1Bitmap(ID2D1Bitmap *iface)
535 if (!iface)
536 return NULL;
537 assert(iface->lpVtbl == &d2d_bitmap_vtbl);
538 return CONTAINING_RECORD(iface, struct d2d_bitmap, ID2D1Bitmap_iface);