winex11: Use NtUserBuildHwndList for has_owned_popup implementation.
[wine.git] / dlls / d2d1 / bitmap.c
blob971e3c7ff6bb65b1947c0f33d02699f2462be3c3
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 %lu.\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 %lu.\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 struct d2d_bitmap *src_bitmap = unsafe_impl_from_ID2D1Bitmap(bitmap);
137 struct d2d_bitmap *dst_bitmap = impl_from_ID2D1Bitmap1(iface);
138 ID3D11DeviceContext *context;
139 ID3D11Device *device;
140 D3D11_BOX box;
142 TRACE("iface %p, dst_point %p, bitmap %p, src_rect %p.\n", iface, dst_point, bitmap, src_rect);
144 if (src_rect)
146 box.left = src_rect->left;
147 box.top = src_rect->top;
148 box.front = 0;
149 box.right = src_rect->right;
150 box.bottom = src_rect->bottom;
151 box.back = 1;
154 ID3D11Resource_GetDevice(dst_bitmap->resource, &device);
155 ID3D11Device_GetImmediateContext(device, &context);
156 ID3D11DeviceContext_CopySubresourceRegion(context, dst_bitmap->resource, 0,
157 dst_point ? dst_point->x : 0, dst_point ? dst_point->y : 0, 0,
158 src_bitmap->resource, 0, src_rect ? &box : NULL);
159 ID3D11DeviceContext_Release(context);
160 ID3D11Device_Release(device);
162 return S_OK;
165 static HRESULT STDMETHODCALLTYPE d2d_bitmap_CopyFromRenderTarget(ID2D1Bitmap1 *iface,
166 const D2D1_POINT_2U *dst_point, ID2D1RenderTarget *render_target, const D2D1_RECT_U *src_rect)
168 FIXME("iface %p, dst_point %p, render_target %p, src_rect %p stub!\n", iface, dst_point, render_target, src_rect);
170 return E_NOTIMPL;
173 static HRESULT STDMETHODCALLTYPE d2d_bitmap_CopyFromMemory(ID2D1Bitmap1 *iface,
174 const D2D1_RECT_U *dst_rect, const void *src_data, UINT32 pitch)
176 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap1(iface);
177 ID3D11DeviceContext *context;
178 ID3D11Device *device;
179 D3D11_BOX box;
181 TRACE("iface %p, dst_rect %p, src_data %p, pitch %u.\n", iface, dst_rect, src_data, pitch);
183 if (dst_rect)
185 box.left = dst_rect->left;
186 box.top = dst_rect->top;
187 box.front = 0;
188 box.right = dst_rect->right;
189 box.bottom = dst_rect->bottom;
190 box.back = 1;
193 ID3D11Resource_GetDevice(bitmap->resource, &device);
194 ID3D11Device_GetImmediateContext(device, &context);
195 ID3D11DeviceContext_UpdateSubresource(context, bitmap->resource, 0, dst_rect ? &box : NULL, src_data, pitch, 0);
196 ID3D11DeviceContext_Release(context);
197 ID3D11Device_Release(device);
199 return S_OK;
202 static void STDMETHODCALLTYPE d2d_bitmap_GetColorContext(ID2D1Bitmap1 *iface, ID2D1ColorContext **context)
204 FIXME("iface %p, context %p stub!\n", iface, context);
207 static D2D1_BITMAP_OPTIONS STDMETHODCALLTYPE d2d_bitmap_GetOptions(ID2D1Bitmap1 *iface)
209 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap1(iface);
211 TRACE("iface %p.\n", iface);
213 return bitmap->options;
216 static HRESULT STDMETHODCALLTYPE d2d_bitmap_GetSurface(ID2D1Bitmap1 *iface, IDXGISurface **surface)
218 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap1(iface);
220 TRACE("iface %p, surface %p.\n", iface, surface);
222 *surface = bitmap->surface;
223 if (*surface)
224 IDXGISurface_AddRef(*surface);
226 return *surface ? S_OK : D2DERR_INVALID_CALL;
229 static HRESULT STDMETHODCALLTYPE d2d_bitmap_Map(ID2D1Bitmap1 *iface, D2D1_MAP_OPTIONS options,
230 D2D1_MAPPED_RECT *mapped_rect)
232 FIXME("iface %p, options %#x, mapped_rect %p stub!\n", iface, options, mapped_rect);
234 return E_NOTIMPL;
237 static HRESULT STDMETHODCALLTYPE d2d_bitmap_Unmap(ID2D1Bitmap1 *iface)
239 FIXME("iface %p stub!\n", iface);
241 return E_NOTIMPL;
244 static const struct ID2D1Bitmap1Vtbl d2d_bitmap_vtbl =
246 d2d_bitmap_QueryInterface,
247 d2d_bitmap_AddRef,
248 d2d_bitmap_Release,
249 d2d_bitmap_GetFactory,
250 d2d_bitmap_GetSize,
251 d2d_bitmap_GetPixelSize,
252 d2d_bitmap_GetPixelFormat,
253 d2d_bitmap_GetDpi,
254 d2d_bitmap_CopyFromBitmap,
255 d2d_bitmap_CopyFromRenderTarget,
256 d2d_bitmap_CopyFromMemory,
257 d2d_bitmap_GetColorContext,
258 d2d_bitmap_GetOptions,
259 d2d_bitmap_GetSurface,
260 d2d_bitmap_Map,
261 d2d_bitmap_Unmap,
264 static BOOL format_supported(const D2D1_PIXEL_FORMAT *format)
266 unsigned int i;
268 static const D2D1_PIXEL_FORMAT supported_formats[] =
270 {DXGI_FORMAT_R32G32B32A32_FLOAT, D2D1_ALPHA_MODE_PREMULTIPLIED},
271 {DXGI_FORMAT_R32G32B32A32_FLOAT, D2D1_ALPHA_MODE_IGNORE },
272 {DXGI_FORMAT_R16G16B16A16_FLOAT, D2D1_ALPHA_MODE_PREMULTIPLIED},
273 {DXGI_FORMAT_R16G16B16A16_FLOAT, D2D1_ALPHA_MODE_IGNORE },
274 {DXGI_FORMAT_R16G16B16A16_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED},
275 {DXGI_FORMAT_R16G16B16A16_UNORM, D2D1_ALPHA_MODE_IGNORE },
276 {DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED},
277 {DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_IGNORE },
278 {DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, D2D1_ALPHA_MODE_PREMULTIPLIED},
279 {DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, D2D1_ALPHA_MODE_IGNORE },
280 {DXGI_FORMAT_A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED},
281 {DXGI_FORMAT_A8_UNORM, D2D1_ALPHA_MODE_STRAIGHT },
282 {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED},
283 {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE },
284 {DXGI_FORMAT_B8G8R8X8_UNORM, D2D1_ALPHA_MODE_IGNORE },
285 {DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, D2D1_ALPHA_MODE_PREMULTIPLIED},
286 {DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, D2D1_ALPHA_MODE_IGNORE },
289 for (i = 0; i < ARRAY_SIZE(supported_formats); ++i)
291 if (supported_formats[i].format == format->format
292 && supported_formats[i].alphaMode == format->alphaMode)
293 return TRUE;
296 return FALSE;
299 static void d2d_bitmap_init(struct d2d_bitmap *bitmap, struct d2d_device_context *context,
300 ID3D11Resource *resource, D2D1_SIZE_U size, const D2D1_BITMAP_PROPERTIES1 *desc)
302 ID3D11Device *d3d_device;
303 HRESULT hr;
305 bitmap->ID2D1Bitmap1_iface.lpVtbl = &d2d_bitmap_vtbl;
306 bitmap->refcount = 1;
307 ID2D1Factory_AddRef(bitmap->factory = context->factory);
308 ID3D11Resource_AddRef(bitmap->resource = resource);
309 bitmap->pixel_size = size;
310 bitmap->format = desc->pixelFormat;
311 bitmap->dpi_x = desc->dpiX;
312 bitmap->dpi_y = desc->dpiY;
313 bitmap->options = desc->bitmapOptions;
315 if (d2d_device_context_is_dxgi_target(context))
316 ID3D11Resource_QueryInterface(resource, &IID_IDXGISurface, (void **)&bitmap->surface);
318 ID3D11Resource_GetDevice(resource, &d3d_device);
319 if (bitmap->options & D2D1_BITMAP_OPTIONS_TARGET)
321 if (FAILED(hr = ID3D11Device_CreateRenderTargetView(d3d_device, resource, NULL, &bitmap->rtv)))
322 WARN("Failed to create RTV, hr %#lx.\n", hr);
325 if (!(bitmap->options & D2D1_BITMAP_OPTIONS_CANNOT_DRAW))
327 if (FAILED(hr = ID3D11Device_CreateShaderResourceView(d3d_device, resource, NULL, &bitmap->srv)))
328 WARN("Failed to create SRV, hr %#lx.\n", hr);
330 ID3D11Device_Release(d3d_device);
332 if (bitmap->dpi_x == 0.0f && bitmap->dpi_y == 0.0f)
334 bitmap->dpi_x = 96.0f;
335 bitmap->dpi_y = 96.0f;
339 HRESULT d2d_bitmap_create(struct d2d_device_context *context, D2D1_SIZE_U size, const void *src_data,
340 UINT32 pitch, const D2D1_BITMAP_PROPERTIES1 *desc, struct d2d_bitmap **bitmap)
342 D3D11_SUBRESOURCE_DATA resource_data;
343 D2D1_BITMAP_PROPERTIES1 bitmap_desc;
344 D3D11_TEXTURE2D_DESC texture_desc;
345 ID3D11Texture2D *texture;
346 HRESULT hr;
348 if (!format_supported(&desc->pixelFormat))
350 WARN("Tried to create bitmap with unsupported format {%#x / %#x}.\n",
351 desc->pixelFormat.format, desc->pixelFormat.alphaMode);
352 return D2DERR_UNSUPPORTED_PIXEL_FORMAT;
355 if (desc->dpiX == 0.0f && desc->dpiY == 0.0f)
357 bitmap_desc = *desc;
358 bitmap_desc.dpiX = context->desc.dpiX;
359 bitmap_desc.dpiY = context->desc.dpiY;
360 desc = &bitmap_desc;
362 else if (desc->dpiX <= 0.0f || desc->dpiY <= 0.0f)
364 return E_INVALIDARG;
367 texture_desc.Width = size.width;
368 texture_desc.Height = size.height;
369 if (!texture_desc.Width || !texture_desc.Height)
370 texture_desc.Width = texture_desc.Height = 1;
371 texture_desc.MipLevels = 1;
372 texture_desc.ArraySize = 1;
373 texture_desc.Format = desc->pixelFormat.format;
374 texture_desc.SampleDesc.Count = 1;
375 texture_desc.SampleDesc.Quality = 0;
376 texture_desc.Usage = D3D11_USAGE_DEFAULT;
377 texture_desc.BindFlags = 0;
378 if (desc->bitmapOptions & D2D1_BITMAP_OPTIONS_TARGET)
379 texture_desc.BindFlags |= D3D11_BIND_RENDER_TARGET;
380 if (!(desc->bitmapOptions & D2D1_BITMAP_OPTIONS_CANNOT_DRAW))
381 texture_desc.BindFlags |= D3D11_BIND_SHADER_RESOURCE;
382 texture_desc.CPUAccessFlags = 0;
383 texture_desc.MiscFlags = 0;
384 if (desc->bitmapOptions & D2D1_BITMAP_OPTIONS_GDI_COMPATIBLE)
385 texture_desc.MiscFlags |= D3D11_RESOURCE_MISC_GDI_COMPATIBLE;
387 resource_data.pSysMem = src_data;
388 resource_data.SysMemPitch = pitch;
390 if (FAILED(hr = ID3D11Device1_CreateTexture2D(context->d3d_device, &texture_desc,
391 src_data ? &resource_data : NULL, &texture)))
393 ERR("Failed to create texture, hr %#lx.\n", hr);
394 return hr;
397 if ((*bitmap = heap_alloc_zero(sizeof(**bitmap))))
399 d2d_bitmap_init(*bitmap, context, (ID3D11Resource *)texture, size, desc);
400 TRACE("Created bitmap %p.\n", *bitmap);
402 ID3D11Texture2D_Release(texture);
404 return *bitmap ? S_OK : E_OUTOFMEMORY;
407 HRESULT d2d_bitmap_create_shared(struct d2d_device_context *context, REFIID iid, void *data,
408 const D2D1_BITMAP_PROPERTIES1 *desc, struct d2d_bitmap **bitmap)
410 D2D1_BITMAP_PROPERTIES1 d;
412 if (IsEqualGUID(iid, &IID_ID2D1Bitmap))
414 struct d2d_bitmap *src_impl = unsafe_impl_from_ID2D1Bitmap(data);
415 ID3D11Device *device;
416 HRESULT hr = S_OK;
418 if (src_impl->factory != context->factory)
420 hr = D2DERR_WRONG_FACTORY;
421 goto failed;
424 ID3D11Resource_GetDevice(src_impl->resource, &device);
425 ID3D11Device_Release(device);
426 if (device != (ID3D11Device *)context->d3d_device)
428 hr = D2DERR_UNSUPPORTED_OPERATION;
429 goto failed;
432 if (!desc)
434 d.pixelFormat = src_impl->format;
435 d.dpiX = src_impl->dpi_x;
436 d.dpiY = src_impl->dpi_y;
437 d.bitmapOptions = src_impl->options;
438 d.colorContext = NULL;
439 desc = &d;
442 if (!format_supported(&desc->pixelFormat))
444 WARN("Tried to create bitmap with unsupported format {%#x / %#x}.\n",
445 desc->pixelFormat.format, desc->pixelFormat.alphaMode);
446 hr = D2DERR_UNSUPPORTED_PIXEL_FORMAT;
447 goto failed;
450 if (!(*bitmap = heap_alloc_zero(sizeof(**bitmap))))
452 hr = E_OUTOFMEMORY;
453 goto failed;
456 d2d_bitmap_init(*bitmap, context, src_impl->resource, src_impl->pixel_size, desc);
457 TRACE("Created bitmap %p.\n", *bitmap);
459 failed:
460 return hr;
463 if (IsEqualGUID(iid, &IID_IDXGISurface) || IsEqualGUID(iid, &IID_IDXGISurface1))
465 DXGI_SURFACE_DESC surface_desc;
466 IDXGISurface *surface = data;
467 ID3D11Resource *resource;
468 D2D1_SIZE_U pixel_size;
469 ID3D11Device *device;
470 HRESULT hr;
472 if (FAILED(IDXGISurface_QueryInterface(surface, &IID_ID3D11Resource, (void **)&resource)))
474 WARN("Failed to get d3d resource from dxgi surface.\n");
475 return E_FAIL;
478 ID3D11Resource_GetDevice(resource, &device);
479 ID3D11Device_Release(device);
480 if (device != (ID3D11Device *)context->d3d_device)
482 ID3D11Resource_Release(resource);
483 return D2DERR_UNSUPPORTED_OPERATION;
486 if (!(*bitmap = heap_alloc_zero(sizeof(**bitmap))))
488 ID3D11Resource_Release(resource);
489 return E_OUTOFMEMORY;
493 if (FAILED(hr = IDXGISurface_GetDesc(surface, &surface_desc)))
495 WARN("Failed to get surface desc, hr %#lx.\n", hr);
496 ID3D11Resource_Release(resource);
497 return hr;
500 if (!desc)
502 memset(&d, 0, sizeof(d));
503 d.pixelFormat.format = surface_desc.Format;
505 else
507 d = *desc;
508 if (d.pixelFormat.format == DXGI_FORMAT_UNKNOWN)
509 d.pixelFormat.format = surface_desc.Format;
512 if (d.dpiX == 0.0f || d.dpiY == 0.0f)
514 if (d.dpiX == 0.0f)
515 d.dpiX = context->desc.dpiX;
516 if (d.dpiY == 0.0f)
517 d.dpiY = context->desc.dpiY;
520 pixel_size.width = surface_desc.Width;
521 pixel_size.height = surface_desc.Height;
523 d2d_bitmap_init(*bitmap, context, resource, pixel_size, &d);
524 ID3D11Resource_Release(resource);
525 TRACE("Created bitmap %p.\n", *bitmap);
527 return S_OK;
530 WARN("Unhandled interface %s.\n", debugstr_guid(iid));
532 return E_INVALIDARG;
535 HRESULT d2d_bitmap_create_from_wic_bitmap(struct d2d_device_context *context, IWICBitmapSource *bitmap_source,
536 const D2D1_BITMAP_PROPERTIES1 *desc, struct d2d_bitmap **bitmap)
538 const D2D1_PIXEL_FORMAT *d2d_format;
539 D2D1_BITMAP_PROPERTIES1 bitmap_desc;
540 WICPixelFormatGUID wic_format;
541 unsigned int bpp, data_size;
542 D2D1_SIZE_U size;
543 unsigned int i;
544 WICRect rect;
545 UINT32 pitch;
546 HRESULT hr;
547 void *data;
549 static const struct
551 const WICPixelFormatGUID *wic;
552 D2D1_PIXEL_FORMAT d2d;
554 format_lookup[] =
556 {&GUID_WICPixelFormat32bppPBGRA, {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED}},
557 {&GUID_WICPixelFormat32bppPRGBA, {DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED}},
558 {&GUID_WICPixelFormat32bppBGR, {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE}},
561 if (FAILED(hr = IWICBitmapSource_GetSize(bitmap_source, &size.width, &size.height)))
563 WARN("Failed to get bitmap size, hr %#lx.\n", hr);
564 return hr;
567 if (!desc)
569 bitmap_desc.pixelFormat.format = DXGI_FORMAT_UNKNOWN;
570 bitmap_desc.pixelFormat.alphaMode = D2D1_ALPHA_MODE_UNKNOWN;
571 bitmap_desc.dpiX = 0.0f;
572 bitmap_desc.dpiY = 0.0f;
573 bitmap_desc.bitmapOptions = 0;
574 bitmap_desc.colorContext = NULL;
576 else
578 bitmap_desc = *desc;
581 if (FAILED(hr = IWICBitmapSource_GetPixelFormat(bitmap_source, &wic_format)))
583 WARN("Failed to get bitmap format, hr %#lx.\n", hr);
584 return hr;
587 for (i = 0, d2d_format = NULL; i < ARRAY_SIZE(format_lookup); ++i)
589 if (IsEqualGUID(&wic_format, format_lookup[i].wic))
591 d2d_format = &format_lookup[i].d2d;
592 break;
596 if (!d2d_format)
598 WARN("Unsupported WIC bitmap format %s.\n", debugstr_guid(&wic_format));
599 return D2DERR_UNSUPPORTED_PIXEL_FORMAT;
602 if (bitmap_desc.pixelFormat.format == DXGI_FORMAT_UNKNOWN)
603 bitmap_desc.pixelFormat.format = d2d_format->format;
604 if (bitmap_desc.pixelFormat.alphaMode == D2D1_ALPHA_MODE_UNKNOWN)
605 bitmap_desc.pixelFormat.alphaMode = d2d_format->alphaMode;
607 switch (bitmap_desc.pixelFormat.format)
609 case DXGI_FORMAT_B8G8R8A8_UNORM:
610 case DXGI_FORMAT_R8G8B8A8_UNORM:
611 bpp = 4;
612 break;
614 default:
615 FIXME("Unhandled format %#x.\n", bitmap_desc.pixelFormat.format);
616 return D2DERR_UNSUPPORTED_PIXEL_FORMAT;
619 pitch = ((bpp * size.width) + 15) & ~15;
620 if (pitch / bpp < size.width)
621 return E_OUTOFMEMORY;
622 if (!(data = heap_calloc(size.height, pitch)))
623 return E_OUTOFMEMORY;
624 data_size = size.height * pitch;
626 rect.X = 0;
627 rect.Y = 0;
628 rect.Width = size.width;
629 rect.Height = size.height;
630 if (FAILED(hr = IWICBitmapSource_CopyPixels(bitmap_source, &rect, pitch, data_size, data)))
632 WARN("Failed to copy bitmap pixels, hr %#lx.\n", hr);
633 heap_free(data);
634 return hr;
637 hr = d2d_bitmap_create(context, size, data, pitch, &bitmap_desc, bitmap);
639 heap_free(data);
641 return hr;
644 struct d2d_bitmap *unsafe_impl_from_ID2D1Bitmap(ID2D1Bitmap *iface)
646 if (!iface)
647 return NULL;
648 assert(iface->lpVtbl == (ID2D1BitmapVtbl *)&d2d_bitmap_vtbl);
649 return CONTAINING_RECORD(iface, struct d2d_bitmap, ID2D1Bitmap1_iface);