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_DECLARE_DEBUG_CHANNEL(winediag
);
26 WINE_DEFAULT_DEBUG_CHANNEL(d2d
);
28 struct d2d_settings d2d_settings
=
30 ~0u, /* No ID2D1Factory version limit by default. */
35 ID2D1Factory1 ID2D1Factory1_iface
;
38 ID3D10Device1
*device
;
44 static inline struct d2d_factory
*impl_from_ID2D1Factory1(ID2D1Factory1
*iface
)
46 return CONTAINING_RECORD(iface
, struct d2d_factory
, ID2D1Factory1_iface
);
49 static HRESULT
d2d_factory_reload_sysmetrics(struct d2d_factory
*factory
)
53 if (!(hdc
= GetDC(NULL
)))
55 factory
->dpi_x
= factory
->dpi_y
= 96.0f
;
59 factory
->dpi_x
= GetDeviceCaps(hdc
, LOGPIXELSX
);
60 factory
->dpi_y
= GetDeviceCaps(hdc
, LOGPIXELSY
);
67 static HRESULT STDMETHODCALLTYPE
d2d_factory_QueryInterface(ID2D1Factory1
*iface
, REFIID iid
, void **out
)
69 TRACE("iface %p, iid %s, out %p.\n", iface
, debugstr_guid(iid
), out
);
71 if ((IsEqualGUID(iid
, &IID_ID2D1Factory1
) && d2d_settings
.max_version_factory
>= 1)
72 || IsEqualGUID(iid
, &IID_ID2D1Factory
)
73 || IsEqualGUID(iid
, &IID_IUnknown
))
75 ID2D1Factory1_AddRef(iface
);
80 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid
));
86 static ULONG STDMETHODCALLTYPE
d2d_factory_AddRef(ID2D1Factory1
*iface
)
88 struct d2d_factory
*factory
= impl_from_ID2D1Factory1(iface
);
89 ULONG refcount
= InterlockedIncrement(&factory
->refcount
);
91 TRACE("%p increasing refcount to %u.\n", iface
, refcount
);
96 static ULONG STDMETHODCALLTYPE
d2d_factory_Release(ID2D1Factory1
*iface
)
98 struct d2d_factory
*factory
= impl_from_ID2D1Factory1(iface
);
99 ULONG refcount
= InterlockedDecrement(&factory
->refcount
);
101 TRACE("%p decreasing refcount to %u.\n", iface
, refcount
);
106 ID3D10Device1_Release(factory
->device
);
113 static HRESULT STDMETHODCALLTYPE
d2d_factory_ReloadSystemMetrics(ID2D1Factory1
*iface
)
115 struct d2d_factory
*factory
= impl_from_ID2D1Factory1(iface
);
117 TRACE("iface %p.\n", iface
);
119 return d2d_factory_reload_sysmetrics(factory
);
122 static void STDMETHODCALLTYPE
d2d_factory_GetDesktopDpi(ID2D1Factory1
*iface
, float *dpi_x
, float *dpi_y
)
124 struct d2d_factory
*factory
= impl_from_ID2D1Factory1(iface
);
126 TRACE("iface %p, dpi_x %p, dpi_y %p.\n", iface
, dpi_x
, dpi_y
);
128 *dpi_x
= factory
->dpi_x
;
129 *dpi_y
= factory
->dpi_y
;
132 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateRectangleGeometry(ID2D1Factory1
*iface
,
133 const D2D1_RECT_F
*rect
, ID2D1RectangleGeometry
**geometry
)
135 struct d2d_geometry
*object
;
138 TRACE("iface %p, rect %s, geometry %p.\n", iface
, debug_d2d_rect_f(rect
), geometry
);
140 if (!(object
= heap_alloc_zero(sizeof(*object
))))
141 return E_OUTOFMEMORY
;
143 if (FAILED(hr
= d2d_rectangle_geometry_init(object
, (ID2D1Factory
*)iface
, rect
)))
145 WARN("Failed to initialize rectangle geometry, hr %#x.\n", hr
);
150 TRACE("Created rectangle geometry %p.\n", object
);
151 *geometry
= (ID2D1RectangleGeometry
*)&object
->ID2D1Geometry_iface
;
156 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateRoundedRectangleGeometry(ID2D1Factory1
*iface
,
157 const D2D1_ROUNDED_RECT
*rect
, ID2D1RoundedRectangleGeometry
**geometry
)
159 FIXME("iface %p, rect %p, geometry %p stub!\n", iface
, rect
, geometry
);
164 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateEllipseGeometry(ID2D1Factory1
*iface
,
165 const D2D1_ELLIPSE
*ellipse
, ID2D1EllipseGeometry
**geometry
)
167 FIXME("iface %p, ellipse %p, geometry %p stub!\n", iface
, ellipse
, geometry
);
172 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateGeometryGroup(ID2D1Factory1
*iface
,
173 D2D1_FILL_MODE fill_mode
, ID2D1Geometry
**geometries
, UINT32 geometry_count
, ID2D1GeometryGroup
**group
)
175 FIXME("iface %p, fill_mode %#x, geometries %p, geometry_count %u, group %p stub!\n",
176 iface
, fill_mode
, geometries
, geometry_count
, group
);
181 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateTransformedGeometry(ID2D1Factory1
*iface
,
182 ID2D1Geometry
*src_geometry
, const D2D1_MATRIX_3X2_F
*transform
,
183 ID2D1TransformedGeometry
**transformed_geometry
)
185 struct d2d_geometry
*object
;
187 TRACE("iface %p, src_geometry %p, transform %p, transformed_geometry %p.\n",
188 iface
, src_geometry
, transform
, transformed_geometry
);
190 if (!(object
= heap_alloc_zero(sizeof(*object
))))
191 return E_OUTOFMEMORY
;
193 d2d_transformed_geometry_init(object
, (ID2D1Factory
*)iface
, src_geometry
, transform
);
195 TRACE("Created transformed geometry %p.\n", object
);
196 *transformed_geometry
= (ID2D1TransformedGeometry
*)&object
->ID2D1Geometry_iface
;
201 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreatePathGeometry(ID2D1Factory1
*iface
, ID2D1PathGeometry
**geometry
)
203 struct d2d_geometry
*object
;
205 TRACE("iface %p, geometry %p.\n", iface
, geometry
);
207 if (!(object
= heap_alloc_zero(sizeof(*object
))))
208 return E_OUTOFMEMORY
;
210 d2d_path_geometry_init(object
, (ID2D1Factory
*)iface
);
212 TRACE("Created path geometry %p.\n", object
);
213 *geometry
= (ID2D1PathGeometry
*)&object
->ID2D1Geometry_iface
;
218 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateStrokeStyle(ID2D1Factory1
*iface
,
219 const D2D1_STROKE_STYLE_PROPERTIES
*desc
, const float *dashes
, UINT32 dash_count
,
220 ID2D1StrokeStyle
**stroke_style
)
222 struct d2d_stroke_style
*object
;
225 TRACE("iface %p, desc %p, dashes %p, dash_count %u, stroke_style %p.\n",
226 iface
, desc
, dashes
, dash_count
, stroke_style
);
228 if (!(object
= heap_alloc_zero(sizeof(*object
))))
229 return E_OUTOFMEMORY
;
231 if (FAILED(hr
= d2d_stroke_style_init(object
, (ID2D1Factory
*)iface
, desc
, dashes
, dash_count
)))
233 WARN("Failed to initialize stroke style, hr %#x.\n", hr
);
238 TRACE("Created stroke style %p.\n", object
);
239 *stroke_style
= &object
->ID2D1StrokeStyle_iface
;
244 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateDrawingStateBlock(ID2D1Factory1
*iface
,
245 const D2D1_DRAWING_STATE_DESCRIPTION
*desc
, IDWriteRenderingParams
*text_rendering_params
,
246 ID2D1DrawingStateBlock
**state_block
)
248 struct d2d_state_block
*object
;
250 TRACE("iface %p, desc %p, text_rendering_params %p, state_block %p.\n",
251 iface
, desc
, text_rendering_params
, state_block
);
253 if (!(object
= heap_alloc_zero(sizeof(*object
))))
254 return E_OUTOFMEMORY
;
256 d2d_state_block_init(object
, (ID2D1Factory
*)iface
, desc
, text_rendering_params
);
258 TRACE("Created state block %p.\n", object
);
259 *state_block
= &object
->ID2D1DrawingStateBlock_iface
;
264 static HRESULT
d2d_factory_get_device(struct d2d_factory
*factory
, ID3D10Device1
**device
)
268 if (!factory
->device
&& FAILED(hr
= D3D10CreateDevice1(NULL
, D3D10_DRIVER_TYPE_HARDWARE
, NULL
, D3D10_CREATE_DEVICE_BGRA_SUPPORT
,
269 D3D10_FEATURE_LEVEL_10_0
, D3D10_1_SDK_VERSION
, &factory
->device
)))
270 WARN("Failed to create device, hr %#x.\n", hr
);
272 *device
= factory
->device
;
276 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateWicBitmapRenderTarget(ID2D1Factory1
*iface
,
277 IWICBitmap
*target
, const D2D1_RENDER_TARGET_PROPERTIES
*desc
, ID2D1RenderTarget
**render_target
)
279 struct d2d_factory
*factory
= impl_from_ID2D1Factory1(iface
);
280 struct d2d_wic_render_target
*object
;
281 ID3D10Device1
*device
;
284 TRACE("iface %p, target %p, desc %p, render_target %p.\n", iface
, target
, desc
, render_target
);
286 if (!(object
= heap_alloc_zero(sizeof(*object
))))
287 return E_OUTOFMEMORY
;
289 if (FAILED(hr
= d2d_factory_get_device(factory
, &device
)))
295 if (FAILED(hr
= d2d_wic_render_target_init(object
, (ID2D1Factory
*)iface
, device
, target
, desc
)))
297 WARN("Failed to initialize render target, hr %#x.\n", hr
);
302 TRACE("Created render target %p.\n", object
);
303 *render_target
= &object
->ID2D1RenderTarget_iface
;
308 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateHwndRenderTarget(ID2D1Factory1
*iface
,
309 const D2D1_RENDER_TARGET_PROPERTIES
*desc
, const D2D1_HWND_RENDER_TARGET_PROPERTIES
*hwnd_rt_desc
,
310 ID2D1HwndRenderTarget
**render_target
)
312 struct d2d_factory
*factory
= impl_from_ID2D1Factory1(iface
);
313 struct d2d_hwnd_render_target
*object
;
314 ID3D10Device1
*device
;
317 TRACE("iface %p, desc %p, hwnd_rt_desc %p, render_target %p.\n", iface
, desc
, hwnd_rt_desc
, render_target
);
319 if (FAILED(hr
= d2d_factory_get_device(factory
, &device
)))
322 if (!(object
= heap_alloc_zero(sizeof(*object
))))
323 return E_OUTOFMEMORY
;
325 if (FAILED(hr
= d2d_hwnd_render_target_init(object
, (ID2D1Factory
*)iface
, device
, desc
, hwnd_rt_desc
)))
327 WARN("Failed to initialize render target, hr %#x.\n", hr
);
332 TRACE("Created render target %p.\n", object
);
333 *render_target
= &object
->ID2D1HwndRenderTarget_iface
;
338 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateDxgiSurfaceRenderTarget(ID2D1Factory1
*iface
,
339 IDXGISurface
*surface
, const D2D1_RENDER_TARGET_PROPERTIES
*desc
, ID2D1RenderTarget
**render_target
)
341 TRACE("iface %p, surface %p, desc %p, render_target %p.\n", iface
, surface
, desc
, render_target
);
343 return d2d_d3d_create_render_target((ID2D1Factory
*)iface
, surface
, NULL
, desc
, render_target
);
346 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateDCRenderTarget(ID2D1Factory1
*iface
,
347 const D2D1_RENDER_TARGET_PROPERTIES
*desc
, ID2D1DCRenderTarget
**render_target
)
349 struct d2d_factory
*factory
= impl_from_ID2D1Factory1(iface
);
350 struct d2d_dc_render_target
*object
;
351 ID3D10Device1
*device
;
354 TRACE("iface %p, desc %p, render_target %p.\n", iface
, desc
, render_target
);
356 if (FAILED(hr
= d2d_factory_get_device(factory
, &device
)))
359 if (!(object
= heap_alloc_zero(sizeof(*object
))))
360 return E_OUTOFMEMORY
;
362 if (FAILED(hr
= d2d_dc_render_target_init(object
, (ID2D1Factory
*)iface
, device
, desc
)))
364 WARN("Failed to initialize render target, hr %#x.\n", hr
);
369 TRACE("Created render target %p.\n", object
);
370 *render_target
= &object
->ID2D1DCRenderTarget_iface
;
375 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateDevice(ID2D1Factory1
*iface
,
376 IDXGIDevice
*dxgi_device
, ID2D1Device
**device
)
378 FIXME("iface %p, dxgi_device %p, device %p stub!\n", iface
, dxgi_device
, device
);
383 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateStrokeStyle1(ID2D1Factory1
*iface
,
384 const D2D1_STROKE_STYLE_PROPERTIES1
*desc
, const float *dashes
, UINT32 dash_count
,
385 ID2D1StrokeStyle1
**stroke_style
)
387 FIXME("iface %p, desc %p, dashes %p, dash_count %u, stroke_style %p stub!\n",
388 iface
, desc
, dashes
, dash_count
, stroke_style
);
393 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreatePathGeometry1(ID2D1Factory1
*iface
, ID2D1PathGeometry1
**geometry
)
395 FIXME("iface %p, geometry %p stub!\n", iface
, geometry
);
400 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateDrawingStateBlock1(ID2D1Factory1
*iface
,
401 const D2D1_DRAWING_STATE_DESCRIPTION1
*desc
, IDWriteRenderingParams
*text_rendering_params
,
402 ID2D1DrawingStateBlock1
**state_block
)
404 FIXME("iface %p, desc %p, text_rendering_params %p, state_block %p stub!\n",
405 iface
, desc
, text_rendering_params
, state_block
);
410 static HRESULT STDMETHODCALLTYPE
d2d_factory_CreateGdiMetafile(ID2D1Factory1
*iface
,
411 IStream
*stream
, ID2D1GdiMetafile
**metafile
)
413 FIXME("iface %p, stream %p, metafile %p stub!\n", iface
, stream
, metafile
);
418 static HRESULT STDMETHODCALLTYPE
d2d_factory_RegisterEffectFromStream(ID2D1Factory1
*iface
,
419 REFCLSID effect_id
, IStream
*property_xml
, const D2D1_PROPERTY_BINDING
*bindings
,
420 UINT32 binding_count
, PD2D1_EFFECT_FACTORY effect_factory
)
422 FIXME("iface %p, effect_id %s, property_xml %p, bindings %p, binding_count %u, effect_factory %p stub!\n",
423 iface
, debugstr_guid(effect_id
), property_xml
, bindings
, binding_count
, effect_factory
);
428 static HRESULT STDMETHODCALLTYPE
d2d_factory_RegisterEffectFromString(ID2D1Factory1
*iface
,
429 REFCLSID effect_id
, const WCHAR
*property_xml
, const D2D1_PROPERTY_BINDING
*bindings
,
430 UINT32 binding_count
, PD2D1_EFFECT_FACTORY effect_factory
)
432 FIXME("iface %p, effect_id %s, property_xml %s, bindings %p, binding_count %u, effect_factory %p stub!\n",
433 iface
, debugstr_guid(effect_id
), debugstr_w(property_xml
), bindings
, binding_count
, effect_factory
);
438 static HRESULT STDMETHODCALLTYPE
d2d_factory_UnregisterEffect(ID2D1Factory1
*iface
, REFCLSID effect_id
)
440 FIXME("iface %p, effect_id %s stub!\n", iface
, debugstr_guid(effect_id
));
445 static HRESULT STDMETHODCALLTYPE
d2d_factory_GetRegisteredEffects(ID2D1Factory1
*iface
,
446 CLSID
*effects
, UINT32 effect_count
, UINT32
*returned
, UINT32
*registered
)
448 FIXME("iface %p, effects %p, effect_count %u, returned %p, registered %p stub!\n",
449 iface
, effects
, effect_count
, returned
, registered
);
454 static HRESULT STDMETHODCALLTYPE
d2d_factory_GetEffectProperties(ID2D1Factory1
*iface
,
455 REFCLSID effect_id
, ID2D1Properties
**props
)
457 FIXME("iface %p, effect_id %s, props %p stub!\n", iface
, debugstr_guid(effect_id
), props
);
462 static const struct ID2D1Factory1Vtbl d2d_factory_vtbl
=
464 d2d_factory_QueryInterface
,
467 d2d_factory_ReloadSystemMetrics
,
468 d2d_factory_GetDesktopDpi
,
469 d2d_factory_CreateRectangleGeometry
,
470 d2d_factory_CreateRoundedRectangleGeometry
,
471 d2d_factory_CreateEllipseGeometry
,
472 d2d_factory_CreateGeometryGroup
,
473 d2d_factory_CreateTransformedGeometry
,
474 d2d_factory_CreatePathGeometry
,
475 d2d_factory_CreateStrokeStyle
,
476 d2d_factory_CreateDrawingStateBlock
,
477 d2d_factory_CreateWicBitmapRenderTarget
,
478 d2d_factory_CreateHwndRenderTarget
,
479 d2d_factory_CreateDxgiSurfaceRenderTarget
,
480 d2d_factory_CreateDCRenderTarget
,
481 d2d_factory_CreateDevice
,
482 d2d_factory_CreateStrokeStyle1
,
483 d2d_factory_CreatePathGeometry1
,
484 d2d_factory_CreateDrawingStateBlock1
,
485 d2d_factory_CreateGdiMetafile
,
486 d2d_factory_RegisterEffectFromStream
,
487 d2d_factory_RegisterEffectFromString
,
488 d2d_factory_UnregisterEffect
,
489 d2d_factory_GetRegisteredEffects
,
490 d2d_factory_GetEffectProperties
,
493 static void d2d_factory_init(struct d2d_factory
*factory
, D2D1_FACTORY_TYPE factory_type
,
494 const D2D1_FACTORY_OPTIONS
*factory_options
)
496 if (factory_type
!= D2D1_FACTORY_TYPE_SINGLE_THREADED
)
497 FIXME("Ignoring factory type %#x.\n", factory_type
);
498 if (factory_options
&& factory_options
->debugLevel
!= D2D1_DEBUG_LEVEL_NONE
)
499 WARN("Ignoring debug level %#x.\n", factory_options
->debugLevel
);
501 factory
->ID2D1Factory1_iface
.lpVtbl
= &d2d_factory_vtbl
;
502 factory
->refcount
= 1;
503 d2d_factory_reload_sysmetrics(factory
);
506 HRESULT WINAPI
D2D1CreateFactory(D2D1_FACTORY_TYPE factory_type
, REFIID iid
,
507 const D2D1_FACTORY_OPTIONS
*factory_options
, void **factory
)
509 struct d2d_factory
*object
;
512 TRACE("factory_type %#x, iid %s, factory_options %p, factory %p.\n",
513 factory_type
, debugstr_guid(iid
), factory_options
, factory
);
515 if (!(object
= heap_alloc_zero(sizeof(*object
))))
516 return E_OUTOFMEMORY
;
518 d2d_factory_init(object
, factory_type
, factory_options
);
520 TRACE("Created factory %p.\n", object
);
522 hr
= ID2D1Factory1_QueryInterface(&object
->ID2D1Factory1_iface
, iid
, factory
);
523 ID2D1Factory1_Release(&object
->ID2D1Factory1_iface
);
528 void WINAPI
D2D1MakeRotateMatrix(float angle
, D2D1_POINT_2F center
, D2D1_MATRIX_3X2_F
*matrix
)
530 float theta
, sin_theta
, cos_theta
;
532 TRACE("angle %.8e, center {%.8e, %.8e}, matrix %p.\n", angle
, center
.x
, center
.y
, matrix
);
534 theta
= angle
* (M_PI
/ 180.0f
);
535 sin_theta
= sinf(theta
);
536 cos_theta
= cosf(theta
);
538 /* translate(center) * rotate(theta) * translate(-center) */
539 matrix
->_11
= cos_theta
;
540 matrix
->_12
= sin_theta
;
541 matrix
->_21
= -sin_theta
;
542 matrix
->_22
= cos_theta
;
543 matrix
->_31
= center
.x
- center
.x
* cos_theta
+ center
.y
* sin_theta
;
544 matrix
->_32
= center
.y
- center
.x
* sin_theta
- center
.y
* cos_theta
;
547 static BOOL
get_config_key_dword(HKEY default_key
, HKEY application_key
, const char *name
, DWORD
*value
)
549 DWORD type
, data
, size
;
552 if (application_key
&& !RegQueryValueExA(application_key
,
553 name
, 0, &type
, (BYTE
*)&data
, &size
) && type
== REG_DWORD
)
557 if (default_key
&& !RegQueryValueExA(default_key
,
558 name
, 0, &type
, (BYTE
*)&data
, &size
) && type
== REG_DWORD
)
568 static void d2d_settings_init(void)
570 HKEY default_key
, tmp_key
, application_key
= NULL
;
571 char buffer
[MAX_PATH
+ 10];
574 if (RegOpenKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\Direct2D", &default_key
))
577 len
= GetModuleFileNameA(0, buffer
, MAX_PATH
);
578 if (len
&& len
< MAX_PATH
)
580 char *p
, *appname
= buffer
;
582 if ((p
= strrchr(appname
, '/')))
584 if ((p
= strrchr(appname
, '\\')))
586 strcat(appname
, "\\Direct2D");
588 if (!RegOpenKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\AppDefaults", &tmp_key
))
590 if (RegOpenKeyA(tmp_key
, appname
, &application_key
))
591 application_key
= NULL
;
592 RegCloseKey(tmp_key
);
596 if (!default_key
&& !application_key
)
599 if (get_config_key_dword(default_key
, application_key
, "max_version_factory", &d2d_settings
.max_version_factory
))
600 ERR_(winediag
)("Limiting maximum Direct2D factory version to %#x.\n", d2d_settings
.max_version_factory
);
603 RegCloseKey(application_key
);
605 RegCloseKey(default_key
);
608 BOOL WINAPI
DllMain(HINSTANCE inst
, DWORD reason
, void *reserved
)
610 if (reason
== DLL_PROCESS_ATTACH
)