d3dx10: Check device in d3dx10_sprite_GetDevice().
[wine.git] / dlls / d2d1 / bitmap.c
blob470724e96a9325b72a8af70b96dbe9d66cf50002
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 "d2d1_private.h"
20 #include "wincodec.h"
22 WINE_DEFAULT_DEBUG_CHANNEL(d2d);
24 static inline struct d2d_bitmap *impl_from_ID2D1Bitmap1(ID2D1Bitmap1 *iface)
26 return CONTAINING_RECORD(iface, struct d2d_bitmap, ID2D1Bitmap1_iface);
29 static HRESULT STDMETHODCALLTYPE d2d_bitmap_QueryInterface(ID2D1Bitmap1 *iface, REFIID iid, void **out)
31 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
33 if (IsEqualGUID(iid, &IID_ID2D1Bitmap1)
34 || IsEqualGUID(iid, &IID_ID2D1Bitmap)
35 || IsEqualGUID(iid, &IID_ID2D1Image)
36 || IsEqualGUID(iid, &IID_ID2D1Resource)
37 || IsEqualGUID(iid, &IID_IUnknown))
39 ID2D1Bitmap1_AddRef(iface);
40 *out = iface;
41 return S_OK;
44 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
46 *out = NULL;
47 return E_NOINTERFACE;
50 static ULONG STDMETHODCALLTYPE d2d_bitmap_AddRef(ID2D1Bitmap1 *iface)
52 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap1(iface);
53 ULONG refcount = InterlockedIncrement(&bitmap->refcount);
55 TRACE("%p increasing refcount to %u.\n", iface, refcount);
57 return refcount;
60 static ULONG STDMETHODCALLTYPE d2d_bitmap_Release(ID2D1Bitmap1 *iface)
62 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap1(iface);
63 ULONG refcount = InterlockedDecrement(&bitmap->refcount);
65 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
67 if (!refcount)
69 if (bitmap->srv)
70 ID3D11ShaderResourceView_Release(bitmap->srv);
71 if (bitmap->rtv)
72 ID3D11RenderTargetView_Release(bitmap->rtv);
73 if (bitmap->surface)
74 IDXGISurface_Release(bitmap->surface);
75 ID3D11Resource_Release(bitmap->resource);
76 ID2D1Factory_Release(bitmap->factory);
77 heap_free(bitmap);
80 return refcount;
83 static void STDMETHODCALLTYPE d2d_bitmap_GetFactory(ID2D1Bitmap1 *iface, ID2D1Factory **factory)
85 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap1(iface);
87 TRACE("iface %p, factory %p.\n", iface, factory);
89 ID2D1Factory_AddRef(*factory = bitmap->factory);
92 static D2D1_SIZE_F * STDMETHODCALLTYPE d2d_bitmap_GetSize(ID2D1Bitmap1 *iface, D2D1_SIZE_F *size)
94 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap1(iface);
96 TRACE("iface %p, size %p.\n", iface, size);
98 size->width = bitmap->pixel_size.width / (bitmap->dpi_x / 96.0f);
99 size->height = bitmap->pixel_size.height / (bitmap->dpi_y / 96.0f);
100 return size;
103 static D2D1_SIZE_U * STDMETHODCALLTYPE d2d_bitmap_GetPixelSize(ID2D1Bitmap1 *iface, D2D1_SIZE_U *pixel_size)
105 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap1(iface);
107 TRACE("iface %p, pixel_size %p.\n", iface, pixel_size);
109 *pixel_size = bitmap->pixel_size;
110 return pixel_size;
113 static D2D1_PIXEL_FORMAT * STDMETHODCALLTYPE d2d_bitmap_GetPixelFormat(ID2D1Bitmap1 *iface, D2D1_PIXEL_FORMAT *format)
115 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap1(iface);
117 TRACE("iface %p, format %p.\n", iface, format);
119 *format = bitmap->format;
120 return format;
123 static void STDMETHODCALLTYPE d2d_bitmap_GetDpi(ID2D1Bitmap1 *iface, float *dpi_x, float *dpi_y)
125 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap1(iface);
127 TRACE("iface %p, dpi_x %p, dpi_y %p.\n", iface, dpi_x, dpi_y);
129 *dpi_x = bitmap->dpi_x;
130 *dpi_y = bitmap->dpi_y;
133 static HRESULT STDMETHODCALLTYPE d2d_bitmap_CopyFromBitmap(ID2D1Bitmap1 *iface,
134 const D2D1_POINT_2U *dst_point, ID2D1Bitmap *bitmap, const D2D1_RECT_U *src_rect)
136 FIXME("iface %p, dst_point %p, bitmap %p, src_rect %p stub!\n", iface, dst_point, bitmap, src_rect);
138 return E_NOTIMPL;
141 static HRESULT STDMETHODCALLTYPE d2d_bitmap_CopyFromRenderTarget(ID2D1Bitmap1 *iface,
142 const D2D1_POINT_2U *dst_point, ID2D1RenderTarget *render_target, const D2D1_RECT_U *src_rect)
144 FIXME("iface %p, dst_point %p, render_target %p, src_rect %p stub!\n", iface, dst_point, render_target, src_rect);
146 return E_NOTIMPL;
149 static HRESULT STDMETHODCALLTYPE d2d_bitmap_CopyFromMemory(ID2D1Bitmap1 *iface,
150 const D2D1_RECT_U *dst_rect, const void *src_data, UINT32 pitch)
152 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap1(iface);
153 ID3D11DeviceContext *context;
154 ID3D11Device *device;
155 D3D11_BOX box;
157 TRACE("iface %p, dst_rect %p, src_data %p, pitch %u.\n", iface, dst_rect, src_data, pitch);
159 if (dst_rect)
161 box.left = dst_rect->left;
162 box.top = dst_rect->top;
163 box.front = 0;
164 box.right = dst_rect->right;
165 box.bottom = dst_rect->bottom;
166 box.back = 1;
169 ID3D11Resource_GetDevice(bitmap->resource, &device);
170 ID3D11Device_GetImmediateContext(device, &context);
171 ID3D11DeviceContext_UpdateSubresource(context, bitmap->resource, 0, dst_rect ? &box : NULL, src_data, pitch, 0);
172 ID3D11DeviceContext_Release(context);
173 ID3D11Device_Release(device);
175 return S_OK;
178 static void STDMETHODCALLTYPE d2d_bitmap_GetColorContext(ID2D1Bitmap1 *iface, ID2D1ColorContext **context)
180 FIXME("iface %p, context %p stub!\n", iface, context);
183 static D2D1_BITMAP_OPTIONS STDMETHODCALLTYPE d2d_bitmap_GetOptions(ID2D1Bitmap1 *iface)
185 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap1(iface);
187 TRACE("iface %p.\n", iface);
189 return bitmap->options;
192 static HRESULT STDMETHODCALLTYPE d2d_bitmap_GetSurface(ID2D1Bitmap1 *iface, IDXGISurface **surface)
194 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap1(iface);
196 TRACE("iface %p, surface %p.\n", iface, surface);
198 *surface = bitmap->surface;
199 if (*surface)
200 IDXGISurface_AddRef(*surface);
202 return *surface ? S_OK : D2DERR_INVALID_CALL;
205 static HRESULT STDMETHODCALLTYPE d2d_bitmap_Map(ID2D1Bitmap1 *iface, D2D1_MAP_OPTIONS options,
206 D2D1_MAPPED_RECT *mapped_rect)
208 FIXME("iface %p, options %#x, mapped_rect %p stub!\n", iface, options, mapped_rect);
210 return E_NOTIMPL;
213 static HRESULT STDMETHODCALLTYPE d2d_bitmap_Unmap(ID2D1Bitmap1 *iface)
215 FIXME("iface %p stub!\n", iface);
217 return E_NOTIMPL;
220 static const struct ID2D1Bitmap1Vtbl d2d_bitmap_vtbl =
222 d2d_bitmap_QueryInterface,
223 d2d_bitmap_AddRef,
224 d2d_bitmap_Release,
225 d2d_bitmap_GetFactory,
226 d2d_bitmap_GetSize,
227 d2d_bitmap_GetPixelSize,
228 d2d_bitmap_GetPixelFormat,
229 d2d_bitmap_GetDpi,
230 d2d_bitmap_CopyFromBitmap,
231 d2d_bitmap_CopyFromRenderTarget,
232 d2d_bitmap_CopyFromMemory,
233 d2d_bitmap_GetColorContext,
234 d2d_bitmap_GetOptions,
235 d2d_bitmap_GetSurface,
236 d2d_bitmap_Map,
237 d2d_bitmap_Unmap,
240 static BOOL format_supported(const D2D1_PIXEL_FORMAT *format)
242 unsigned int i;
244 static const D2D1_PIXEL_FORMAT supported_formats[] =
246 {DXGI_FORMAT_R32G32B32A32_FLOAT, D2D1_ALPHA_MODE_PREMULTIPLIED},
247 {DXGI_FORMAT_R32G32B32A32_FLOAT, D2D1_ALPHA_MODE_IGNORE },
248 {DXGI_FORMAT_R16G16B16A16_FLOAT, D2D1_ALPHA_MODE_PREMULTIPLIED},
249 {DXGI_FORMAT_R16G16B16A16_FLOAT, D2D1_ALPHA_MODE_IGNORE },
250 {DXGI_FORMAT_R16G16B16A16_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED},
251 {DXGI_FORMAT_R16G16B16A16_UNORM, D2D1_ALPHA_MODE_IGNORE },
252 {DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED},
253 {DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_IGNORE },
254 {DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, D2D1_ALPHA_MODE_PREMULTIPLIED},
255 {DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, D2D1_ALPHA_MODE_IGNORE },
256 {DXGI_FORMAT_A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED},
257 {DXGI_FORMAT_A8_UNORM, D2D1_ALPHA_MODE_STRAIGHT },
258 {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED},
259 {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE },
260 {DXGI_FORMAT_B8G8R8X8_UNORM, D2D1_ALPHA_MODE_IGNORE },
261 {DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, D2D1_ALPHA_MODE_PREMULTIPLIED},
262 {DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, D2D1_ALPHA_MODE_IGNORE },
265 for (i = 0; i < ARRAY_SIZE(supported_formats); ++i)
267 if (supported_formats[i].format == format->format
268 && supported_formats[i].alphaMode == format->alphaMode)
269 return TRUE;
272 return FALSE;
275 static void d2d_bitmap_init(struct d2d_bitmap *bitmap, struct d2d_device_context *context,
276 ID3D11Resource *resource, D2D1_SIZE_U size, const D2D1_BITMAP_PROPERTIES1 *desc)
278 ID3D11Device *d3d_device;
279 HRESULT hr;
281 bitmap->ID2D1Bitmap1_iface.lpVtbl = &d2d_bitmap_vtbl;
282 bitmap->refcount = 1;
283 ID2D1Factory_AddRef(bitmap->factory = context->factory);
284 ID3D11Resource_AddRef(bitmap->resource = resource);
285 bitmap->pixel_size = size;
286 bitmap->format = desc->pixelFormat;
287 bitmap->dpi_x = desc->dpiX;
288 bitmap->dpi_y = desc->dpiY;
289 bitmap->options = desc->bitmapOptions;
291 if (d2d_device_context_is_dxgi_target(context))
292 ID3D11Resource_QueryInterface(resource, &IID_IDXGISurface, (void **)&bitmap->surface);
294 ID3D11Resource_GetDevice(resource, &d3d_device);
295 if (bitmap->options & D2D1_BITMAP_OPTIONS_TARGET)
297 if (FAILED(hr = ID3D11Device_CreateRenderTargetView(d3d_device, resource, NULL, &bitmap->rtv)))
298 WARN("Failed to create RTV, hr %#x.\n", hr);
301 if (!(bitmap->options & D2D1_BITMAP_OPTIONS_CANNOT_DRAW))
303 if (FAILED(hr = ID3D11Device_CreateShaderResourceView(d3d_device, resource, NULL, &bitmap->srv)))
304 WARN("Failed to create SRV, hr %#x.\n", hr);
306 ID3D11Device_Release(d3d_device);
308 if (bitmap->dpi_x == 0.0f && bitmap->dpi_y == 0.0f)
310 bitmap->dpi_x = 96.0f;
311 bitmap->dpi_y = 96.0f;
315 HRESULT d2d_bitmap_create(struct d2d_device_context *context, D2D1_SIZE_U size, const void *src_data,
316 UINT32 pitch, const D2D1_BITMAP_PROPERTIES1 *desc, struct d2d_bitmap **bitmap)
318 D3D11_SUBRESOURCE_DATA resource_data;
319 D2D1_BITMAP_PROPERTIES1 bitmap_desc;
320 D3D11_TEXTURE2D_DESC texture_desc;
321 ID3D11Texture2D *texture;
322 HRESULT hr;
324 if (!format_supported(&desc->pixelFormat))
326 WARN("Tried to create bitmap with unsupported format {%#x / %#x}.\n",
327 desc->pixelFormat.format, desc->pixelFormat.alphaMode);
328 return D2DERR_UNSUPPORTED_PIXEL_FORMAT;
331 if (desc->dpiX == 0.0f && desc->dpiY == 0.0f)
333 bitmap_desc = *desc;
334 bitmap_desc.dpiX = context->desc.dpiX;
335 bitmap_desc.dpiY = context->desc.dpiY;
336 desc = &bitmap_desc;
338 else if (desc->dpiX <= 0.0f || desc->dpiY <= 0.0f)
340 return E_INVALIDARG;
343 texture_desc.Width = size.width;
344 texture_desc.Height = size.height;
345 if (!texture_desc.Width || !texture_desc.Height)
346 texture_desc.Width = texture_desc.Height = 1;
347 texture_desc.MipLevels = 1;
348 texture_desc.ArraySize = 1;
349 texture_desc.Format = desc->pixelFormat.format;
350 texture_desc.SampleDesc.Count = 1;
351 texture_desc.SampleDesc.Quality = 0;
352 texture_desc.Usage = D3D11_USAGE_DEFAULT;
353 texture_desc.BindFlags = 0;
354 if (desc->bitmapOptions & D2D1_BITMAP_OPTIONS_TARGET)
355 texture_desc.BindFlags |= D3D11_BIND_RENDER_TARGET;
356 if (!(desc->bitmapOptions & D2D1_BITMAP_OPTIONS_CANNOT_DRAW))
357 texture_desc.BindFlags |= D3D11_BIND_SHADER_RESOURCE;
358 texture_desc.CPUAccessFlags = 0;
359 texture_desc.MiscFlags = 0;
360 if (desc->bitmapOptions & D2D1_BITMAP_OPTIONS_GDI_COMPATIBLE)
361 texture_desc.MiscFlags |= D3D11_RESOURCE_MISC_GDI_COMPATIBLE;
363 resource_data.pSysMem = src_data;
364 resource_data.SysMemPitch = pitch;
366 if (FAILED(hr = ID3D11Device1_CreateTexture2D(context->d3d_device, &texture_desc,
367 src_data ? &resource_data : NULL, &texture)))
369 ERR("Failed to create texture, hr %#x.\n", hr);
370 return hr;
373 if ((*bitmap = heap_alloc_zero(sizeof(**bitmap))))
375 d2d_bitmap_init(*bitmap, context, (ID3D11Resource *)texture, size, desc);
376 TRACE("Created bitmap %p.\n", *bitmap);
378 ID3D11Texture2D_Release(texture);
380 return *bitmap ? S_OK : E_OUTOFMEMORY;
383 HRESULT d2d_bitmap_create_shared(struct d2d_device_context *context, REFIID iid, void *data,
384 const D2D1_BITMAP_PROPERTIES1 *desc, struct d2d_bitmap **bitmap)
386 D2D1_BITMAP_PROPERTIES1 d;
388 if (IsEqualGUID(iid, &IID_ID2D1Bitmap))
390 struct d2d_bitmap *src_impl = unsafe_impl_from_ID2D1Bitmap(data);
391 ID3D11Device *device;
392 HRESULT hr = S_OK;
394 if (src_impl->factory != context->factory)
396 hr = D2DERR_WRONG_FACTORY;
397 goto failed;
400 ID3D11Resource_GetDevice(src_impl->resource, &device);
401 ID3D11Device_Release(device);
402 if (device != (ID3D11Device *)context->d3d_device)
404 hr = D2DERR_UNSUPPORTED_OPERATION;
405 goto failed;
408 if (!desc)
410 d.pixelFormat = src_impl->format;
411 d.dpiX = src_impl->dpi_x;
412 d.dpiY = src_impl->dpi_y;
413 d.bitmapOptions = src_impl->options;
414 d.colorContext = NULL;
415 desc = &d;
418 if (!format_supported(&desc->pixelFormat))
420 WARN("Tried to create bitmap with unsupported format {%#x / %#x}.\n",
421 desc->pixelFormat.format, desc->pixelFormat.alphaMode);
422 hr = D2DERR_UNSUPPORTED_PIXEL_FORMAT;
423 goto failed;
426 if (!(*bitmap = heap_alloc_zero(sizeof(**bitmap))))
428 hr = E_OUTOFMEMORY;
429 goto failed;
432 d2d_bitmap_init(*bitmap, context, src_impl->resource, src_impl->pixel_size, desc);
433 TRACE("Created bitmap %p.\n", *bitmap);
435 failed:
436 return hr;
439 if (IsEqualGUID(iid, &IID_IDXGISurface) || IsEqualGUID(iid, &IID_IDXGISurface1))
441 DXGI_SURFACE_DESC surface_desc;
442 IDXGISurface *surface = data;
443 ID3D11Resource *resource;
444 D2D1_SIZE_U pixel_size;
445 ID3D11Device *device;
446 HRESULT hr;
448 if (FAILED(IDXGISurface_QueryInterface(surface, &IID_ID3D11Resource, (void **)&resource)))
450 WARN("Failed to get d3d resource from dxgi surface.\n");
451 return E_FAIL;
454 ID3D11Resource_GetDevice(resource, &device);
455 ID3D11Device_Release(device);
456 if (device != (ID3D11Device *)context->d3d_device)
458 ID3D11Resource_Release(resource);
459 return D2DERR_UNSUPPORTED_OPERATION;
462 if (!(*bitmap = heap_alloc_zero(sizeof(**bitmap))))
464 ID3D11Resource_Release(resource);
465 return E_OUTOFMEMORY;
469 if (FAILED(hr = IDXGISurface_GetDesc(surface, &surface_desc)))
471 WARN("Failed to get surface desc, hr %#x.\n", hr);
472 ID3D11Resource_Release(resource);
473 return hr;
476 if (!desc)
478 memset(&d, 0, sizeof(d));
479 d.pixelFormat.format = surface_desc.Format;
481 else
483 d = *desc;
484 if (d.pixelFormat.format == DXGI_FORMAT_UNKNOWN)
485 d.pixelFormat.format = surface_desc.Format;
488 if (d.dpiX == 0.0f || d.dpiY == 0.0f)
490 if (d.dpiX == 0.0f)
491 d.dpiX = context->desc.dpiX;
492 if (d.dpiY == 0.0f)
493 d.dpiY = context->desc.dpiY;
496 pixel_size.width = surface_desc.Width;
497 pixel_size.height = surface_desc.Height;
499 d2d_bitmap_init(*bitmap, context, resource, pixel_size, &d);
500 ID3D11Resource_Release(resource);
501 TRACE("Created bitmap %p.\n", *bitmap);
503 return S_OK;
506 WARN("Unhandled interface %s.\n", debugstr_guid(iid));
508 return E_INVALIDARG;
511 HRESULT d2d_bitmap_create_from_wic_bitmap(struct d2d_device_context *context, IWICBitmapSource *bitmap_source,
512 const D2D1_BITMAP_PROPERTIES1 *desc, struct d2d_bitmap **bitmap)
514 const D2D1_PIXEL_FORMAT *d2d_format;
515 D2D1_BITMAP_PROPERTIES1 bitmap_desc;
516 WICPixelFormatGUID wic_format;
517 unsigned int bpp, data_size;
518 D2D1_SIZE_U size;
519 unsigned int i;
520 WICRect rect;
521 UINT32 pitch;
522 HRESULT hr;
523 void *data;
525 static const struct
527 const WICPixelFormatGUID *wic;
528 D2D1_PIXEL_FORMAT d2d;
530 format_lookup[] =
532 {&GUID_WICPixelFormat32bppPBGRA, {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED}},
533 {&GUID_WICPixelFormat32bppPRGBA, {DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED}},
534 {&GUID_WICPixelFormat32bppBGR, {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE}},
537 if (FAILED(hr = IWICBitmapSource_GetSize(bitmap_source, &size.width, &size.height)))
539 WARN("Failed to get bitmap size, hr %#x.\n", hr);
540 return hr;
543 if (!desc)
545 bitmap_desc.pixelFormat.format = DXGI_FORMAT_UNKNOWN;
546 bitmap_desc.pixelFormat.alphaMode = D2D1_ALPHA_MODE_UNKNOWN;
547 bitmap_desc.dpiX = 0.0f;
548 bitmap_desc.dpiY = 0.0f;
549 bitmap_desc.bitmapOptions = 0;
550 bitmap_desc.colorContext = NULL;
552 else
554 bitmap_desc = *desc;
557 if (FAILED(hr = IWICBitmapSource_GetPixelFormat(bitmap_source, &wic_format)))
559 WARN("Failed to get bitmap format, hr %#x.\n", hr);
560 return hr;
563 for (i = 0, d2d_format = NULL; i < ARRAY_SIZE(format_lookup); ++i)
565 if (IsEqualGUID(&wic_format, format_lookup[i].wic))
567 d2d_format = &format_lookup[i].d2d;
568 break;
572 if (!d2d_format)
574 WARN("Unsupported WIC bitmap format %s.\n", debugstr_guid(&wic_format));
575 return D2DERR_UNSUPPORTED_PIXEL_FORMAT;
578 if (bitmap_desc.pixelFormat.format == DXGI_FORMAT_UNKNOWN)
579 bitmap_desc.pixelFormat.format = d2d_format->format;
580 if (bitmap_desc.pixelFormat.alphaMode == D2D1_ALPHA_MODE_UNKNOWN)
581 bitmap_desc.pixelFormat.alphaMode = d2d_format->alphaMode;
583 switch (bitmap_desc.pixelFormat.format)
585 case DXGI_FORMAT_B8G8R8A8_UNORM:
586 case DXGI_FORMAT_R8G8B8A8_UNORM:
587 bpp = 4;
588 break;
590 default:
591 FIXME("Unhandled format %#x.\n", bitmap_desc.pixelFormat.format);
592 return D2DERR_UNSUPPORTED_PIXEL_FORMAT;
595 pitch = ((bpp * size.width) + 15) & ~15;
596 if (pitch / bpp < size.width)
597 return E_OUTOFMEMORY;
598 if (!(data = heap_calloc(size.height, pitch)))
599 return E_OUTOFMEMORY;
600 data_size = size.height * pitch;
602 rect.X = 0;
603 rect.Y = 0;
604 rect.Width = size.width;
605 rect.Height = size.height;
606 if (FAILED(hr = IWICBitmapSource_CopyPixels(bitmap_source, &rect, pitch, data_size, data)))
608 WARN("Failed to copy bitmap pixels, hr %#x.\n", hr);
609 heap_free(data);
610 return hr;
613 hr = d2d_bitmap_create(context, size, data, pitch, &bitmap_desc, bitmap);
615 heap_free(data);
617 return hr;
620 struct d2d_bitmap *unsafe_impl_from_ID2D1Bitmap(ID2D1Bitmap *iface)
622 if (!iface)
623 return NULL;
624 assert(iface->lpVtbl == (ID2D1BitmapVtbl *)&d2d_bitmap_vtbl);
625 return CONTAINING_RECORD(iface, struct d2d_bitmap, ID2D1Bitmap1_iface);