d2d1: Implement d2d_bitmap_GetFactory().
[wine.git] / dlls / d2d1 / bitmap.c
blob2627501ec1e6678b976030245cce5026e2b99008
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 FIXME("iface %p, dst_rect %p, src_data %p, pitch %u stub!\n", iface, dst_rect, src_data, pitch);
148 return E_NOTIMPL;
151 static const struct ID2D1BitmapVtbl d2d_bitmap_vtbl =
153 d2d_bitmap_QueryInterface,
154 d2d_bitmap_AddRef,
155 d2d_bitmap_Release,
156 d2d_bitmap_GetFactory,
157 d2d_bitmap_GetSize,
158 d2d_bitmap_GetPixelSize,
159 d2d_bitmap_GetPixelFormat,
160 d2d_bitmap_GetDpi,
161 d2d_bitmap_CopyFromBitmap,
162 d2d_bitmap_CopyFromRenderTarget,
163 d2d_bitmap_CopyFromMemory,
166 HRESULT d2d_bitmap_init(struct d2d_bitmap *bitmap, struct d2d_d3d_render_target *render_target,
167 D2D1_SIZE_U size, const void *src_data, UINT32 pitch, const D2D1_BITMAP_PROPERTIES *desc)
169 D3D10_SUBRESOURCE_DATA resource_data;
170 D3D10_TEXTURE2D_DESC texture_desc;
171 ID3D10Texture2D *texture;
172 BOOL supported = FALSE;
173 unsigned int i;
174 HRESULT hr;
176 static const D2D1_PIXEL_FORMAT supported_formats[] =
178 {DXGI_FORMAT_R32G32B32A32_FLOAT, D2D1_ALPHA_MODE_PREMULTIPLIED},
179 {DXGI_FORMAT_R32G32B32A32_FLOAT, D2D1_ALPHA_MODE_IGNORE },
180 {DXGI_FORMAT_R16G16B16A16_FLOAT, D2D1_ALPHA_MODE_PREMULTIPLIED},
181 {DXGI_FORMAT_R16G16B16A16_FLOAT, D2D1_ALPHA_MODE_IGNORE },
182 {DXGI_FORMAT_R16G16B16A16_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED},
183 {DXGI_FORMAT_R16G16B16A16_UNORM, D2D1_ALPHA_MODE_IGNORE },
184 {DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED},
185 {DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_IGNORE },
186 {DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, D2D1_ALPHA_MODE_PREMULTIPLIED},
187 {DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, D2D1_ALPHA_MODE_IGNORE },
188 {DXGI_FORMAT_A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED},
189 {DXGI_FORMAT_A8_UNORM, D2D1_ALPHA_MODE_STRAIGHT },
190 {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED},
191 {DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE },
192 {DXGI_FORMAT_B8G8R8X8_UNORM, D2D1_ALPHA_MODE_IGNORE },
193 {DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, D2D1_ALPHA_MODE_PREMULTIPLIED},
194 {DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, D2D1_ALPHA_MODE_IGNORE },
197 for (i = 0; i < sizeof(supported_formats) / sizeof(*supported_formats); ++i)
199 if (supported_formats[i].format == desc->pixelFormat.format
200 && supported_formats[i].alphaMode == desc->pixelFormat.alphaMode)
202 supported = TRUE;
203 break;
207 if (!supported)
209 WARN("Tried to create bitmap with unsupported format {%#x / %#x}.\n",
210 desc->pixelFormat.format, desc->pixelFormat.alphaMode);
211 return D2DERR_UNSUPPORTED_PIXEL_FORMAT;
214 FIXME("Ignoring bitmap properties.\n");
216 bitmap->ID2D1Bitmap_iface.lpVtbl = &d2d_bitmap_vtbl;
217 bitmap->refcount = 1;
218 ID2D1Factory_AddRef(bitmap->factory = render_target->factory);
220 texture_desc.Width = size.width;
221 texture_desc.Height = size.height;
222 texture_desc.MipLevels = 1;
223 texture_desc.ArraySize = 1;
224 texture_desc.Format = desc->pixelFormat.format;
225 texture_desc.SampleDesc.Count = 1;
226 texture_desc.SampleDesc.Quality = 0;
227 texture_desc.Usage = D3D10_USAGE_DEFAULT;
228 texture_desc.BindFlags = D3D10_BIND_SHADER_RESOURCE;
229 texture_desc.CPUAccessFlags = 0;
230 texture_desc.MiscFlags = 0;
232 resource_data.pSysMem = src_data;
233 resource_data.SysMemPitch = pitch;
235 if (FAILED(hr = ID3D10Device_CreateTexture2D(render_target->device, &texture_desc,
236 src_data ? &resource_data : NULL, &texture)))
238 ERR("Failed to create texture, hr %#x.\n", hr);
239 return hr;
242 hr = ID3D10Device_CreateShaderResourceView(render_target->device, (ID3D10Resource *)texture, NULL, &bitmap->view);
243 ID3D10Texture2D_Release(texture);
244 if (FAILED(hr))
246 ERR("Failed to create view, hr %#x.\n", hr);
247 return hr;
250 bitmap->pixel_size = size;
251 bitmap->format = desc->pixelFormat;
252 bitmap->dpi_x = desc->dpiX;
253 bitmap->dpi_y = desc->dpiY;
255 if (bitmap->dpi_x == 0.0f && bitmap->dpi_y == 0.0f)
257 bitmap->dpi_x = 96.0f;
258 bitmap->dpi_y = 96.0f;
261 return S_OK;
264 struct d2d_bitmap *unsafe_impl_from_ID2D1Bitmap(ID2D1Bitmap *iface)
266 if (!iface)
267 return NULL;
268 assert(iface->lpVtbl == &d2d_bitmap_vtbl);
269 return CONTAINING_RECORD(iface, struct d2d_bitmap, ID2D1Bitmap_iface);