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
20 #include "wine/port.h"
22 #define D2D1_INIT_GUID
23 #include "d2d1_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d2d
);
29 ID2D1Factory ID2D1Factory_iface
;
32 ID3D10Device1
*device
;
38 static inline struct d2d_factory
*impl_from_ID2D1Factory(ID2D1Factory
*iface
)
40 return CONTAINING_RECORD(iface
, struct d2d_factory
, ID2D1Factory_iface
);
43 static HRESULT
d2d_factory_reload_sysmetrics(struct d2d_factory
*factory
)
47 if (!(hdc
= GetDC(NULL
)))
49 factory
->dpi_x
= factory
->dpi_y
= 96.0f
;
53 factory
->dpi_x
= GetDeviceCaps(hdc
, LOGPIXELSX
);
54 factory
->dpi_y
= GetDeviceCaps(hdc
, LOGPIXELSY
);
61 static HRESULT STDMETHODCALLTYPE
d2d_factory_QueryInterface(ID2D1Factory
*iface
, REFIID iid
, void **out
)
63 TRACE("iface %p, iid %s, out %p.\n", iface
, debugstr_guid(iid
), out
);
65 if (IsEqualGUID(iid
, &IID_ID2D1Factory
)
66 || IsEqualGUID(iid
, &IID_IUnknown
))
68 ID2D1Factory_AddRef(iface
);
73 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid
));
79 static ULONG STDMETHODCALLTYPE
d2d_factory_AddRef(ID2D1Factory
*iface
)
81 struct d2d_factory
*factory
= impl_from_ID2D1Factory(iface
);
82 ULONG refcount
= InterlockedIncrement(&factory
->refcount
);
84 TRACE("%p increasing refcount to %u.\n", iface
, refcount
);
89 static ULONG STDMETHODCALLTYPE
d2d_factory_Release(ID2D1Factory
*iface
)
91 struct d2d_factory
*factory
= impl_from_ID2D1Factory(iface
);
92 ULONG refcount
= InterlockedDecrement(&factory
->refcount
);
94 TRACE("%p decreasing refcount to %u.\n", iface
, refcount
);
99 ID3D10Device1_Release(factory
->device
);
100 HeapFree(GetProcessHeap(), 0, factory
);
106 static HRESULT STDMETHODCALLTYPE
d2d_factory_ReloadSystemMetrics(ID2D1Factory
*iface
)
108 struct d2d_factory
*factory
= impl_from_ID2D1Factory(iface
);
110 TRACE("iface %p.\n", iface
);
112 return d2d_factory_reload_sysmetrics(factory
);
115 static void STDMETHODCALLTYPE
d2d_factory_GetDesktopDpi(ID2D1Factory
*iface
, float *dpi_x
, float *dpi_y
)
117 struct d2d_factory
*factory
= impl_from_ID2D1Factory(iface
);
119 TRACE("iface %p, dpi_x %p, dpi_y %p.\n", iface
, dpi_x
, dpi_y
);
121 *dpi_x
= factory
->dpi_x
;
122 *dpi_y
= factory
->dpi_y
;
125 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateRectangleGeometry(ID2D1Factory
*iface
,
126 const D2D1_RECT_F
*rect
, ID2D1RectangleGeometry
**geometry
)
128 struct d2d_geometry
*object
;
131 TRACE("iface %p, rect %s, geometry %p.\n", iface
, debug_d2d_rect_f(rect
), geometry
);
133 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
134 return E_OUTOFMEMORY
;
136 if (FAILED(hr
= d2d_rectangle_geometry_init(object
, iface
, rect
)))
138 WARN("Failed to initialize rectangle geometry, hr %#x.\n", hr
);
139 HeapFree(GetProcessHeap(), 0, object
);
143 TRACE("Created rectangle geometry %p.\n", object
);
144 *geometry
= (ID2D1RectangleGeometry
*)&object
->ID2D1Geometry_iface
;
149 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateRoundedRectangleGeometry(ID2D1Factory
*iface
,
150 const D2D1_ROUNDED_RECT
*rect
, ID2D1RoundedRectangleGeometry
**geometry
)
152 FIXME("iface %p, rect %p, geometry %p stub!\n", iface
, rect
, geometry
);
157 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateEllipseGeometry(ID2D1Factory
*iface
,
158 const D2D1_ELLIPSE
*ellipse
, ID2D1EllipseGeometry
**geometry
)
160 FIXME("iface %p, ellipse %p, geometry %p stub!\n", iface
, ellipse
, geometry
);
165 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateGeometryGroup(ID2D1Factory
*iface
,
166 D2D1_FILL_MODE fill_mode
, ID2D1Geometry
**geometries
, UINT32 geometry_count
, ID2D1GeometryGroup
**group
)
168 FIXME("iface %p, fill_mode %#x, geometries %p, geometry_count %u, group %p stub!\n",
169 iface
, fill_mode
, geometries
, geometry_count
, group
);
174 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateTransformedGeometry(ID2D1Factory
*iface
,
175 ID2D1Geometry
*src_geometry
, const D2D1_MATRIX_3X2_F
*transform
,
176 ID2D1TransformedGeometry
**transformed_geometry
)
178 struct d2d_geometry
*object
;
180 TRACE("iface %p, src_geometry %p, transform %p, transformed_geometry %p.\n",
181 iface
, src_geometry
, transform
, transformed_geometry
);
183 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
184 return E_OUTOFMEMORY
;
186 d2d_transformed_geometry_init(object
, iface
, src_geometry
, transform
);
188 TRACE("Created transformed geometry %p.\n", object
);
189 *transformed_geometry
= (ID2D1TransformedGeometry
*)&object
->ID2D1Geometry_iface
;
194 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreatePathGeometry(ID2D1Factory
*iface
, ID2D1PathGeometry
**geometry
)
196 struct d2d_geometry
*object
;
198 TRACE("iface %p, geometry %p.\n", iface
, geometry
);
200 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
201 return E_OUTOFMEMORY
;
203 d2d_path_geometry_init(object
, iface
);
205 TRACE("Created path geometry %p.\n", object
);
206 *geometry
= (ID2D1PathGeometry
*)&object
->ID2D1Geometry_iface
;
211 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateStrokeStyle(ID2D1Factory
*iface
,
212 const D2D1_STROKE_STYLE_PROPERTIES
*desc
, const float *dashes
, UINT32 dash_count
,
213 ID2D1StrokeStyle
**stroke_style
)
215 struct d2d_stroke_style
*object
;
218 TRACE("iface %p, desc %p, dashes %p, dash_count %u, stroke_style %p.\n",
219 iface
, desc
, dashes
, dash_count
, stroke_style
);
221 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
222 return E_OUTOFMEMORY
;
224 if (FAILED(hr
= d2d_stroke_style_init(object
, iface
, desc
, dashes
, dash_count
)))
226 WARN("Failed to initialize stroke style, hr %#x.\n", hr
);
227 HeapFree(GetProcessHeap(), 0, object
);
231 TRACE("Created stroke style %p.\n", object
);
232 *stroke_style
= &object
->ID2D1StrokeStyle_iface
;
237 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateDrawingStateBlock(ID2D1Factory
*iface
,
238 const D2D1_DRAWING_STATE_DESCRIPTION
*desc
, IDWriteRenderingParams
*text_rendering_params
,
239 ID2D1DrawingStateBlock
**state_block
)
241 struct d2d_state_block
*object
;
243 TRACE("iface %p, desc %p, text_rendering_params %p, state_block %p.\n",
244 iface
, desc
, text_rendering_params
, state_block
);
246 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
247 return E_OUTOFMEMORY
;
249 d2d_state_block_init(object
, iface
, desc
, text_rendering_params
);
251 TRACE("Created state block %p.\n", object
);
252 *state_block
= &object
->ID2D1DrawingStateBlock_iface
;
257 static HRESULT
d2d_factory_get_device(struct d2d_factory
*factory
, ID3D10Device1
**device
)
261 if (!factory
->device
&& FAILED(hr
= D3D10CreateDevice1(NULL
, D3D10_DRIVER_TYPE_HARDWARE
, NULL
, D3D10_CREATE_DEVICE_BGRA_SUPPORT
,
262 D3D10_FEATURE_LEVEL_10_0
, D3D10_1_SDK_VERSION
, &factory
->device
)))
263 WARN("Failed to create device, hr %#x.\n", hr
);
265 *device
= factory
->device
;
269 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateWicBitmapRenderTarget(ID2D1Factory
*iface
,
270 IWICBitmap
*target
, const D2D1_RENDER_TARGET_PROPERTIES
*desc
, ID2D1RenderTarget
**render_target
)
272 struct d2d_factory
*factory
= impl_from_ID2D1Factory(iface
);
273 struct d2d_wic_render_target
*object
;
274 ID3D10Device1
*device
;
277 TRACE("iface %p, target %p, desc %p, render_target %p.\n", iface
, target
, desc
, render_target
);
279 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
280 return E_OUTOFMEMORY
;
282 if (FAILED(hr
= d2d_factory_get_device(factory
, &device
)))
284 HeapFree(GetProcessHeap(), 0, object
);
288 if (FAILED(hr
= d2d_wic_render_target_init(object
, iface
, device
, target
, desc
)))
290 WARN("Failed to initialize render target, hr %#x.\n", hr
);
291 HeapFree(GetProcessHeap(), 0, object
);
295 TRACE("Created render target %p.\n", object
);
296 *render_target
= &object
->ID2D1RenderTarget_iface
;
301 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateHwndRenderTarget(ID2D1Factory
*iface
,
302 const D2D1_RENDER_TARGET_PROPERTIES
*desc
, const D2D1_HWND_RENDER_TARGET_PROPERTIES
*hwnd_rt_desc
,
303 ID2D1HwndRenderTarget
**render_target
)
305 struct d2d_factory
*factory
= impl_from_ID2D1Factory(iface
);
306 struct d2d_hwnd_render_target
*object
;
307 ID3D10Device1
*device
;
310 TRACE("iface %p, desc %p, hwnd_rt_desc %p, render_target %p.\n", iface
, desc
, hwnd_rt_desc
, render_target
);
312 if (FAILED(hr
= d2d_factory_get_device(factory
, &device
)))
315 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
316 return E_OUTOFMEMORY
;
318 if (FAILED(hr
= d2d_hwnd_render_target_init(object
, iface
, device
, desc
, hwnd_rt_desc
)))
320 WARN("Failed to initialize render target, hr %#x.\n", hr
);
321 HeapFree(GetProcessHeap(), 0, object
);
325 TRACE("Created render target %p.\n", object
);
326 *render_target
= &object
->ID2D1HwndRenderTarget_iface
;
331 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateDxgiSurfaceRenderTarget(ID2D1Factory
*iface
,
332 IDXGISurface
*surface
, const D2D1_RENDER_TARGET_PROPERTIES
*desc
, ID2D1RenderTarget
**render_target
)
334 TRACE("iface %p, surface %p, desc %p, render_target %p.\n", iface
, surface
, desc
, render_target
);
336 return d2d_d3d_create_render_target(iface
, surface
, NULL
, desc
, render_target
);
339 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateDCRenderTarget(ID2D1Factory
*iface
,
340 const D2D1_RENDER_TARGET_PROPERTIES
*desc
, ID2D1DCRenderTarget
**render_target
)
342 struct d2d_factory
*factory
= impl_from_ID2D1Factory(iface
);
343 struct d2d_dc_render_target
*object
;
344 ID3D10Device1
*device
;
347 TRACE("iface %p, desc %p, render_target %p.\n", iface
, desc
, render_target
);
349 if (FAILED(hr
= d2d_factory_get_device(factory
, &device
)))
352 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
353 return E_OUTOFMEMORY
;
355 if (FAILED(hr
= d2d_dc_render_target_init(object
, iface
, device
, desc
)))
357 WARN("Failed to initialize render target, hr %#x.\n", hr
);
358 HeapFree(GetProcessHeap(), 0, object
);
362 TRACE("Created render target %p.\n", object
);
363 *render_target
= &object
->ID2D1DCRenderTarget_iface
;
368 static const struct ID2D1FactoryVtbl d2d_factory_vtbl
=
370 d2d_factory_QueryInterface
,
373 d2d_factory_ReloadSystemMetrics
,
374 d2d_factory_GetDesktopDpi
,
375 d2d_factory_CreateRectangleGeometry
,
376 d2d_factory_CreateRoundedRectangleGeometry
,
377 d2d_factory_CreateEllipseGeometry
,
378 d2d_factory_CreateGeometryGroup
,
379 d2d_factory_CreateTransformedGeometry
,
380 d2d_factory_CreatePathGeometry
,
381 d2d_factory_CreateStrokeStyle
,
382 d2d_factory_CreateDrawingStateBlock
,
383 d2d_factory_CreateWicBitmapRenderTarget
,
384 d2d_factory_CreateHwndRenderTarget
,
385 d2d_factory_CreateDxgiSurfaceRenderTarget
,
386 d2d_factory_CreateDCRenderTarget
,
389 static void d2d_factory_init(struct d2d_factory
*factory
, D2D1_FACTORY_TYPE factory_type
,
390 const D2D1_FACTORY_OPTIONS
*factory_options
)
392 if (factory_type
!= D2D1_FACTORY_TYPE_SINGLE_THREADED
)
393 FIXME("Ignoring factory type %#x.\n", factory_type
);
394 if (factory_options
&& factory_options
->debugLevel
!= D2D1_DEBUG_LEVEL_NONE
)
395 WARN("Ignoring debug level %#x.\n", factory_options
->debugLevel
);
397 factory
->ID2D1Factory_iface
.lpVtbl
= &d2d_factory_vtbl
;
398 factory
->refcount
= 1;
399 d2d_factory_reload_sysmetrics(factory
);
402 HRESULT WINAPI
D2D1CreateFactory(D2D1_FACTORY_TYPE factory_type
, REFIID iid
,
403 const D2D1_FACTORY_OPTIONS
*factory_options
, void **factory
)
405 struct d2d_factory
*object
;
408 TRACE("factory_type %#x, iid %s, factory_options %p, factory %p.\n",
409 factory_type
, debugstr_guid(iid
), factory_options
, factory
);
411 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
412 return E_OUTOFMEMORY
;
414 d2d_factory_init(object
, factory_type
, factory_options
);
416 TRACE("Created factory %p.\n", object
);
418 hr
= ID2D1Factory_QueryInterface(&object
->ID2D1Factory_iface
, iid
, factory
);
419 ID2D1Factory_Release(&object
->ID2D1Factory_iface
);
424 void WINAPI
D2D1MakeRotateMatrix(float angle
, D2D1_POINT_2F center
, D2D1_MATRIX_3X2_F
*matrix
)
426 float theta
, sin_theta
, cos_theta
;
428 TRACE("angle %.8e, center {%.8e, %.8e}, matrix %p.\n", angle
, center
.x
, center
.y
, matrix
);
430 theta
= angle
* (M_PI
/ 180.0f
);
431 sin_theta
= sinf(theta
);
432 cos_theta
= cosf(theta
);
434 /* translate(center) * rotate(theta) * translate(-center) */
435 matrix
->_11
= cos_theta
;
436 matrix
->_12
= sin_theta
;
437 matrix
->_21
= -sin_theta
;
438 matrix
->_22
= cos_theta
;
439 matrix
->_31
= center
.x
- center
.x
* cos_theta
+ center
.y
* sin_theta
;
440 matrix
->_32
= center
.y
- center
.x
* sin_theta
- center
.y
* cos_theta
;