d2d1: Take the recommended rendering mode into account in d2d_d3d_render_target_DrawG...
[wine.git] / dlls / d2d1 / bitmap.c
blob8d22eac613ba7f46f8ada7f581e3fca8ac661b97
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"
24 WINE_DEFAULT_DEBUG_CHANNEL(d2d);
26 static inline struct d2d_bitmap *impl_from_ID2D1Bitmap(ID2D1Bitmap *iface)
28 return CONTAINING_RECORD(iface, struct d2d_bitmap, ID2D1Bitmap_iface);
31 static HRESULT STDMETHODCALLTYPE d2d_bitmap_QueryInterface(ID2D1Bitmap *iface, REFIID iid, void **out)
33 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
35 if (IsEqualGUID(iid, &IID_ID2D1Bitmap)
36 || IsEqualGUID(iid, &IID_ID2D1Resource)
37 || IsEqualGUID(iid, &IID_IUnknown))
39 ID2D1Bitmap_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(ID2D1Bitmap *iface)
52 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap(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(ID2D1Bitmap *iface)
62 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap(iface);
63 ULONG refcount = InterlockedDecrement(&bitmap->refcount);
65 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
67 if (!refcount)
69 ID3D10ShaderResourceView_Release(bitmap->view);
70 ID2D1Factory_Release(bitmap->factory);
71 HeapFree(GetProcessHeap(), 0, bitmap);
74 return refcount;
77 static void STDMETHODCALLTYPE d2d_bitmap_GetFactory(ID2D1Bitmap *iface, ID2D1Factory **factory)
79 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap(iface);
81 TRACE("iface %p, factory %p.\n", iface, factory);
83 ID2D1Factory_AddRef(*factory = bitmap->factory);
86 static D2D1_SIZE_F * STDMETHODCALLTYPE d2d_bitmap_GetSize(ID2D1Bitmap *iface, D2D1_SIZE_F *size)
88 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap(iface);
90 TRACE("iface %p, size %p.\n", iface, size);
92 size->width = bitmap->pixel_size.width / (bitmap->dpi_x / 96.0f);
93 size->height = bitmap->pixel_size.height / (bitmap->dpi_y / 96.0f);
94 return size;
97 static D2D1_SIZE_U * STDMETHODCALLTYPE d2d_bitmap_GetPixelSize(ID2D1Bitmap *iface, D2D1_SIZE_U *pixel_size)
99 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap(iface);
101 TRACE("iface %p, pixel_size %p.\n", iface, pixel_size);
103 *pixel_size = bitmap->pixel_size;
104 return pixel_size;
107 static D2D1_PIXEL_FORMAT * STDMETHODCALLTYPE d2d_bitmap_GetPixelFormat(ID2D1Bitmap *iface, D2D1_PIXEL_FORMAT *format)
109 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap(iface);
111 TRACE("iface %p, format %p.\n", iface, format);
113 *format = bitmap->format;
114 return format;
117 static void STDMETHODCALLTYPE d2d_bitmap_GetDpi(ID2D1Bitmap *iface, float *dpi_x, float *dpi_y)
119 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap(iface);
121 TRACE("iface %p, dpi_x %p, dpi_y %p.\n", iface, dpi_x, dpi_y);
123 *dpi_x = bitmap->dpi_x;
124 *dpi_y = bitmap->dpi_y;
127 static HRESULT STDMETHODCALLTYPE d2d_bitmap_CopyFromBitmap(ID2D1Bitmap *iface,
128 const D2D1_POINT_2U *dst_point, ID2D1Bitmap *bitmap, const D2D1_RECT_U *src_rect)
130 FIXME("iface %p, dst_point %p, bitmap %p, src_rect %p stub!\n", iface, dst_point, bitmap, src_rect);
132 return E_NOTIMPL;
135 static HRESULT STDMETHODCALLTYPE d2d_bitmap_CopyFromRenderTarget(ID2D1Bitmap *iface,
136 const D2D1_POINT_2U *dst_point, ID2D1RenderTarget *render_target, const D2D1_RECT_U *src_rect)
138 FIXME("iface %p, dst_point %p, render_target %p, src_rect %p stub!\n", iface, dst_point, render_target, src_rect);
140 return E_NOTIMPL;
143 static HRESULT STDMETHODCALLTYPE d2d_bitmap_CopyFromMemory(ID2D1Bitmap *iface,
144 const D2D1_RECT_U *dst_rect, const void *src_data, UINT32 pitch)
146 struct d2d_bitmap *bitmap = impl_from_ID2D1Bitmap(iface);
147 ID3D10Device *device;
148 ID3D10Resource *dst;
149 D3D10_BOX box;
151 TRACE("iface %p, dst_rect %p, src_data %p, pitch %u.\n", iface, dst_rect, src_data, pitch);
153 if (dst_rect)
155 box.left = dst_rect->left;
156 box.top = dst_rect->top;
157 box.front = 0;
158 box.right = dst_rect->right;
159 box.bottom = dst_rect->bottom;
160 box.back = 1;
163 ID3D10ShaderResourceView_GetResource(bitmap->view, &dst);
164 ID3D10ShaderResourceView_GetDevice(bitmap->view, &device);
165 ID3D10Device_UpdateSubresource(device, dst, 0, dst_rect ? &box : NULL, src_data, pitch, 0);
166 ID3D10Device_Release(device);
167 ID3D10Resource_Release(dst);
169 return S_OK;
172 static const struct ID2D1BitmapVtbl d2d_bitmap_vtbl =
174 d2d_bitmap_QueryInterface,
175 d2d_bitmap_AddRef,
176 d2d_bitmap_Release,
177 d2d_bitmap_GetFactory,
178 d2d_bitmap_GetSize,
179 d2d_bitmap_GetPixelSize,
180 d2d_bitmap_GetPixelFormat,
181 d2d_bitmap_GetDpi,
182 d2d_bitmap_CopyFromBitmap,
183 d2d_bitmap_CopyFromRenderTarget,
184 d2d_bitmap_CopyFromMemory,
187 static BOOL format_supported(const D2D1_PIXEL_FORMAT *format)
189 unsigned int i;
191 static const D2D1_PIXEL_FORMAT supported_formats[] =
193 {DXGI_FORMAT_R32G32B32A32_FLOAT, D2D1_ALPHA_MODE_PREMULTIPLIED},
194 {DXGI_FORMAT_R32G32B32A32_FLOAT, D2D1_ALPHA_MODE_IGNORE },
195 {DXGI_FORMAT_R16G16B16A16_FLOAT, D2D1_ALPHA_MODE_PREMULTIPLIED},
196 {DXGI_FORMAT_R16G16B16A16_FLOAT, D2D1_ALPHA_MODE_IGNORE },
197 {DXGI_FORMAT_R16G16B16A16_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED},
198 {DXGI_FORMAT_R16G16B16A16_UNORM, D2D1_ALPHA_MODE_IGNORE },
199 {DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED},
200 {DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_IGNORE },
201 {DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, D2D1_ALPHA_MODE_PREMULTIPLIED},
202 {DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, D2D1_ALPHA_MODE_IGNORE },
203 {DXGI_FORMAT_A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED},
204 {DXGI_FORMAT_A8_UNORM, D2D1_ALPHA_MODE_STRAIGHT },
205 {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED},
206 {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE },
207 {DXGI_FORMAT_B8G8R8X8_UNORM, D2D1_ALPHA_MODE_IGNORE },
208 {DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, D2D1_ALPHA_MODE_PREMULTIPLIED},
209 {DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, D2D1_ALPHA_MODE_IGNORE },
212 for (i = 0; i < sizeof(supported_formats) / sizeof(*supported_formats); ++i)
214 if (supported_formats[i].format == format->format
215 && supported_formats[i].alphaMode == format->alphaMode)
216 return TRUE;
219 return FALSE;
222 static void d2d_bitmap_init(struct d2d_bitmap *bitmap, ID2D1Factory *factory,
223 ID3D10ShaderResourceView *view, D2D1_SIZE_U size, const D2D1_BITMAP_PROPERTIES *desc)
225 bitmap->ID2D1Bitmap_iface.lpVtbl = &d2d_bitmap_vtbl;
226 bitmap->refcount = 1;
227 ID2D1Factory_AddRef(bitmap->factory = factory);
228 ID3D10ShaderResourceView_AddRef(bitmap->view = view);
229 bitmap->pixel_size = size;
230 bitmap->format = desc->pixelFormat;
231 bitmap->dpi_x = desc->dpiX;
232 bitmap->dpi_y = desc->dpiY;
234 if (bitmap->dpi_x == 0.0f && bitmap->dpi_y == 0.0f)
236 bitmap->dpi_x = 96.0f;
237 bitmap->dpi_y = 96.0f;
241 HRESULT d2d_bitmap_init_memory(struct d2d_bitmap *bitmap, struct d2d_d3d_render_target *render_target,
242 D2D1_SIZE_U size, const void *src_data, UINT32 pitch, const D2D1_BITMAP_PROPERTIES *desc)
244 D3D10_SUBRESOURCE_DATA resource_data;
245 D3D10_TEXTURE2D_DESC texture_desc;
246 ID3D10ShaderResourceView *view;
247 ID3D10Texture2D *texture;
248 HRESULT hr;
250 if (!format_supported(&desc->pixelFormat))
252 WARN("Tried to create bitmap with unsupported format {%#x / %#x}.\n",
253 desc->pixelFormat.format, desc->pixelFormat.alphaMode);
254 return D2DERR_UNSUPPORTED_PIXEL_FORMAT;
257 texture_desc.Width = size.width;
258 texture_desc.Height = size.height;
259 texture_desc.MipLevels = 1;
260 texture_desc.ArraySize = 1;
261 texture_desc.Format = desc->pixelFormat.format;
262 texture_desc.SampleDesc.Count = 1;
263 texture_desc.SampleDesc.Quality = 0;
264 texture_desc.Usage = D3D10_USAGE_DEFAULT;
265 texture_desc.BindFlags = D3D10_BIND_SHADER_RESOURCE;
266 texture_desc.CPUAccessFlags = 0;
267 texture_desc.MiscFlags = 0;
269 resource_data.pSysMem = src_data;
270 resource_data.SysMemPitch = pitch;
272 if (FAILED(hr = ID3D10Device_CreateTexture2D(render_target->device, &texture_desc,
273 src_data ? &resource_data : NULL, &texture)))
275 ERR("Failed to create texture, hr %#x.\n", hr);
276 return hr;
279 hr = ID3D10Device_CreateShaderResourceView(render_target->device, (ID3D10Resource *)texture, NULL, &view);
280 ID3D10Texture2D_Release(texture);
281 if (FAILED(hr))
283 ERR("Failed to create view, hr %#x.\n", hr);
284 return hr;
287 d2d_bitmap_init(bitmap, render_target->factory, view, size, desc);
288 ID3D10ShaderResourceView_Release(view);
290 return S_OK;
293 HRESULT d2d_bitmap_init_shared(struct d2d_bitmap *bitmap, struct d2d_d3d_render_target *render_target,
294 REFIID iid, void *data, const D2D1_BITMAP_PROPERTIES *desc)
296 if (IsEqualGUID(iid, &IID_ID2D1Bitmap))
298 struct d2d_bitmap *src_impl = unsafe_impl_from_ID2D1Bitmap(data);
299 D2D1_BITMAP_PROPERTIES d;
300 ID3D10Device *device;
302 if (src_impl->factory != render_target->factory)
303 return D2DERR_WRONG_FACTORY;
305 ID3D10ShaderResourceView_GetDevice(src_impl->view, &device);
306 ID3D10Device_Release(device);
307 if (device != render_target->device)
308 return D2DERR_UNSUPPORTED_OPERATION;
310 if (!desc)
312 d.pixelFormat = src_impl->format;
313 d.dpiX = src_impl->dpi_x;
314 d.dpiY = src_impl->dpi_y;
315 desc = &d;
318 if (!format_supported(&desc->pixelFormat))
320 WARN("Tried to create bitmap with unsupported format {%#x / %#x}.\n",
321 desc->pixelFormat.format, desc->pixelFormat.alphaMode);
322 return D2DERR_UNSUPPORTED_PIXEL_FORMAT;
325 d2d_bitmap_init(bitmap, render_target->factory, src_impl->view, src_impl->pixel_size, desc);
327 return S_OK;
330 WARN("Unhandled interface %s.\n", debugstr_guid(iid));
332 return E_INVALIDARG;
335 struct d2d_bitmap *unsafe_impl_from_ID2D1Bitmap(ID2D1Bitmap *iface)
337 if (!iface)
338 return NULL;
339 assert(iface->lpVtbl == &d2d_bitmap_vtbl);
340 return CONTAINING_RECORD(iface, struct d2d_bitmap, ID2D1Bitmap_iface);