winebus.sys: Add stub driver.
[wine.git] / dlls / d2d1 / bitmap.c
blobb2f2ad7ca843eda4057891b7f8d9c19d47c6047d
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 < sizeof(supported_formats) / sizeof(*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(ID2D1Factory *factory, ID3D10Device *target_device,
300 REFIID iid, void *data, const D2D1_BITMAP_PROPERTIES *desc, struct d2d_bitmap **bitmap)
302 if (IsEqualGUID(iid, &IID_ID2D1Bitmap))
304 struct d2d_bitmap *src_impl = unsafe_impl_from_ID2D1Bitmap(data);
305 D2D1_BITMAP_PROPERTIES d;
306 ID3D10Device *device;
308 if (src_impl->factory != factory)
309 return D2DERR_WRONG_FACTORY;
311 ID3D10ShaderResourceView_GetDevice(src_impl->view, &device);
312 ID3D10Device_Release(device);
313 if (device != target_device)
314 return D2DERR_UNSUPPORTED_OPERATION;
316 if (!desc)
318 d.pixelFormat = src_impl->format;
319 d.dpiX = src_impl->dpi_x;
320 d.dpiY = src_impl->dpi_y;
321 desc = &d;
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 (!(*bitmap = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**bitmap))))
332 return E_OUTOFMEMORY;
334 d2d_bitmap_init(*bitmap, factory, src_impl->view, src_impl->pixel_size, desc);
335 TRACE("Created bitmap %p.\n", *bitmap);
337 return S_OK;
340 WARN("Unhandled interface %s.\n", debugstr_guid(iid));
342 return E_INVALIDARG;
345 HRESULT d2d_bitmap_create_from_wic_bitmap(ID2D1Factory *factory, ID3D10Device *device, IWICBitmapSource *bitmap_source,
346 const D2D1_BITMAP_PROPERTIES *desc, struct d2d_bitmap **bitmap)
348 const D2D1_PIXEL_FORMAT *d2d_format;
349 D2D1_BITMAP_PROPERTIES bitmap_desc;
350 WICPixelFormatGUID wic_format;
351 unsigned int bpp, data_size;
352 D2D1_SIZE_U size;
353 unsigned int i;
354 WICRect rect;
355 UINT32 pitch;
356 HRESULT hr;
357 void *data;
359 static const struct
361 const WICPixelFormatGUID *wic;
362 D2D1_PIXEL_FORMAT d2d;
364 format_lookup[] =
366 {&GUID_WICPixelFormat32bppPBGRA, {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED}},
367 {&GUID_WICPixelFormat32bppBGR, {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE}},
370 if (FAILED(hr = IWICBitmapSource_GetSize(bitmap_source, &size.width, &size.height)))
372 WARN("Failed to get bitmap size, hr %#x.\n", hr);
373 return hr;
376 if (!desc)
378 bitmap_desc.pixelFormat.format = DXGI_FORMAT_UNKNOWN;
379 bitmap_desc.pixelFormat.alphaMode = D2D1_ALPHA_MODE_UNKNOWN;
380 bitmap_desc.dpiX = 0.0f;
381 bitmap_desc.dpiY = 0.0f;
383 else
385 bitmap_desc = *desc;
388 if (FAILED(hr = IWICBitmapSource_GetPixelFormat(bitmap_source, &wic_format)))
390 WARN("Failed to get bitmap format, hr %#x.\n", hr);
391 return hr;
394 for (i = 0, d2d_format = NULL; i < sizeof(format_lookup) / sizeof(*format_lookup); ++i)
396 if (IsEqualGUID(&wic_format, format_lookup[i].wic))
398 d2d_format = &format_lookup[i].d2d;
399 break;
403 if (!d2d_format)
405 WARN("Unsupported WIC bitmap format %s.\n", debugstr_guid(&wic_format));
406 return D2DERR_UNSUPPORTED_PIXEL_FORMAT;
409 if (bitmap_desc.pixelFormat.format == DXGI_FORMAT_UNKNOWN)
410 bitmap_desc.pixelFormat.format = d2d_format->format;
411 if (bitmap_desc.pixelFormat.alphaMode == D2D1_ALPHA_MODE_UNKNOWN)
412 bitmap_desc.pixelFormat.alphaMode = d2d_format->alphaMode;
414 switch (bitmap_desc.pixelFormat.format)
416 case DXGI_FORMAT_B8G8R8A8_UNORM:
417 bpp = 4;
418 break;
420 default:
421 FIXME("Unhandled format %#x.\n", bitmap_desc.pixelFormat.format);
422 return D2DERR_UNSUPPORTED_PIXEL_FORMAT;
425 pitch = ((bpp * size.width) + 15) & ~15;
426 data_size = pitch * size.height;
427 if (!(data = HeapAlloc(GetProcessHeap(), 0, data_size)))
428 return E_OUTOFMEMORY;
430 rect.X = 0;
431 rect.Y = 0;
432 rect.Width = size.width;
433 rect.Height = size.height;
434 if (FAILED(hr = IWICBitmapSource_CopyPixels(bitmap_source, &rect, pitch, data_size, data)))
436 WARN("Failed to copy bitmap pixels, hr %#x.\n", hr);
437 HeapFree(GetProcessHeap(), 0, data);
438 return hr;
441 hr = d2d_bitmap_create(factory, device, size, data, pitch, &bitmap_desc, bitmap);
443 HeapFree(GetProcessHeap(), 0, data);
445 return hr;
448 struct d2d_bitmap *unsafe_impl_from_ID2D1Bitmap(ID2D1Bitmap *iface)
450 if (!iface)
451 return NULL;
452 assert(iface->lpVtbl == &d2d_bitmap_vtbl);
453 return CONTAINING_RECORD(iface, struct d2d_bitmap, ID2D1Bitmap_iface);