winex11: Use NtUserBuildHwndList for has_owned_popup implementation.
[wine.git] / dlls / d2d1 / factory.c
blobf0f616748894ca25f3d6fa06e5b2528f6e3e39a4
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 #define D2D1_INIT_GUID
20 #include "d2d1_private.h"
22 WINE_DECLARE_DEBUG_CHANNEL(winediag);
23 WINE_DEFAULT_DEBUG_CHANNEL(d2d);
25 struct d2d_settings d2d_settings =
27 ~0u, /* No ID2D1Factory version limit by default. */
30 struct d2d_factory
32 ID2D1Factory3 ID2D1Factory3_iface;
33 ID2D1Multithread ID2D1Multithread_iface;
34 LONG refcount;
36 ID3D10Device1 *device;
38 float dpi_x;
39 float dpi_y;
41 CRITICAL_SECTION cs;
44 static inline struct d2d_factory *impl_from_ID2D1Factory3(ID2D1Factory3 *iface)
46 return CONTAINING_RECORD(iface, struct d2d_factory, ID2D1Factory3_iface);
49 static inline struct d2d_factory *impl_from_ID2D1Multithread(ID2D1Multithread *iface)
51 return CONTAINING_RECORD(iface, struct d2d_factory, ID2D1Multithread_iface);
54 static HRESULT d2d_factory_reload_sysmetrics(struct d2d_factory *factory)
56 HDC hdc;
58 if (!(hdc = GetDC(NULL)))
60 factory->dpi_x = factory->dpi_y = 96.0f;
61 return E_FAIL;
64 factory->dpi_x = GetDeviceCaps(hdc, LOGPIXELSX);
65 factory->dpi_y = GetDeviceCaps(hdc, LOGPIXELSY);
67 ReleaseDC(NULL, hdc);
69 return S_OK;
72 static HRESULT STDMETHODCALLTYPE d2d_factory_QueryInterface(ID2D1Factory3 *iface, REFIID iid, void **out)
74 struct d2d_factory *factory = impl_from_ID2D1Factory3(iface);
76 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
78 if ((IsEqualGUID(iid, &IID_ID2D1Factory3) && d2d_settings.max_version_factory >= 3)
79 || (IsEqualGUID(iid, &IID_ID2D1Factory2) && d2d_settings.max_version_factory >= 2)
80 || (IsEqualGUID(iid, &IID_ID2D1Factory1) && d2d_settings.max_version_factory >= 1)
81 || IsEqualGUID(iid, &IID_ID2D1Factory)
82 || IsEqualGUID(iid, &IID_IUnknown))
84 ID2D1Factory3_AddRef(iface);
85 *out = iface;
86 return S_OK;
88 else if (IsEqualGUID(iid, &IID_ID2D1Multithread))
90 ID2D1Factory3_AddRef(iface);
91 *out = &factory->ID2D1Multithread_iface;
92 return S_OK;
95 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
97 *out = NULL;
98 return E_NOINTERFACE;
101 static ULONG STDMETHODCALLTYPE d2d_factory_AddRef(ID2D1Factory3 *iface)
103 struct d2d_factory *factory = impl_from_ID2D1Factory3(iface);
104 ULONG refcount = InterlockedIncrement(&factory->refcount);
106 TRACE("%p increasing refcount to %lu.\n", iface, refcount);
108 return refcount;
111 static ULONG STDMETHODCALLTYPE d2d_factory_Release(ID2D1Factory3 *iface)
113 struct d2d_factory *factory = impl_from_ID2D1Factory3(iface);
114 ULONG refcount = InterlockedDecrement(&factory->refcount);
116 TRACE("%p decreasing refcount to %lu.\n", iface, refcount);
118 if (!refcount)
120 if (factory->device)
121 ID3D10Device1_Release(factory->device);
122 DeleteCriticalSection(&factory->cs);
123 heap_free(factory);
126 return refcount;
129 static HRESULT STDMETHODCALLTYPE d2d_factory_ReloadSystemMetrics(ID2D1Factory3 *iface)
131 struct d2d_factory *factory = impl_from_ID2D1Factory3(iface);
133 TRACE("iface %p.\n", iface);
135 return d2d_factory_reload_sysmetrics(factory);
138 static void STDMETHODCALLTYPE d2d_factory_GetDesktopDpi(ID2D1Factory3 *iface, float *dpi_x, float *dpi_y)
140 struct d2d_factory *factory = impl_from_ID2D1Factory3(iface);
142 TRACE("iface %p, dpi_x %p, dpi_y %p.\n", iface, dpi_x, dpi_y);
144 *dpi_x = factory->dpi_x;
145 *dpi_y = factory->dpi_y;
148 static HRESULT STDMETHODCALLTYPE d2d_factory_CreateRectangleGeometry(ID2D1Factory3 *iface,
149 const D2D1_RECT_F *rect, ID2D1RectangleGeometry **geometry)
151 struct d2d_geometry *object;
152 HRESULT hr;
154 TRACE("iface %p, rect %s, geometry %p.\n", iface, debug_d2d_rect_f(rect), geometry);
156 if (!(object = heap_alloc_zero(sizeof(*object))))
157 return E_OUTOFMEMORY;
159 if (FAILED(hr = d2d_rectangle_geometry_init(object, (ID2D1Factory *)iface, rect)))
161 WARN("Failed to initialise rectangle geometry, hr %#lx.\n", hr);
162 heap_free(object);
163 return hr;
166 TRACE("Created rectangle geometry %p.\n", object);
167 *geometry = (ID2D1RectangleGeometry *)&object->ID2D1Geometry_iface;
169 return S_OK;
172 static HRESULT STDMETHODCALLTYPE d2d_factory_CreateRoundedRectangleGeometry(ID2D1Factory3 *iface,
173 const D2D1_ROUNDED_RECT *rounded_rect, ID2D1RoundedRectangleGeometry **geometry)
175 struct d2d_geometry *object;
176 HRESULT hr;
178 TRACE("iface %p, rounded_rect %s, geometry %p.\n", iface, debug_d2d_rounded_rect(rounded_rect), geometry);
180 if (!(object = heap_alloc_zero(sizeof(*object))))
181 return E_OUTOFMEMORY;
183 if (FAILED(hr = d2d_rounded_rectangle_geometry_init(object, (ID2D1Factory *)iface, rounded_rect)))
185 WARN("Failed to initialise rounded rectangle geometry, hr %#lx.\n", hr);
186 heap_free(object);
187 return hr;
190 TRACE("Created rounded rectangle geometry %p.\n", object);
191 *geometry = (ID2D1RoundedRectangleGeometry *)&object->ID2D1Geometry_iface;
193 return S_OK;
196 static HRESULT STDMETHODCALLTYPE d2d_factory_CreateEllipseGeometry(ID2D1Factory3 *iface,
197 const D2D1_ELLIPSE *ellipse, ID2D1EllipseGeometry **geometry)
199 struct d2d_geometry *object;
200 HRESULT hr;
202 TRACE("iface %p, ellipse %s, geometry %p.\n", iface, debug_d2d_ellipse(ellipse), geometry);
204 if (!(object = heap_alloc_zero(sizeof(*object))))
205 return E_OUTOFMEMORY;
207 if (FAILED(hr = d2d_ellipse_geometry_init(object, (ID2D1Factory *)iface, ellipse)))
209 WARN("Failed to initialise ellipse geometry, hr %#lx.\n", hr);
210 heap_free(object);
211 return hr;
214 TRACE("Created ellipse geometry %p.\n", object);
215 *geometry = (ID2D1EllipseGeometry *)&object->ID2D1Geometry_iface;
217 return S_OK;
220 static HRESULT STDMETHODCALLTYPE d2d_factory_CreateGeometryGroup(ID2D1Factory3 *iface,
221 D2D1_FILL_MODE fill_mode, ID2D1Geometry **geometries, UINT32 geometry_count, ID2D1GeometryGroup **group)
223 struct d2d_geometry *object;
224 HRESULT hr;
226 TRACE("iface %p, fill_mode %#x, geometries %p, geometry_count %u, group %p.\n",
227 iface, fill_mode, geometries, geometry_count, group);
229 if (!(object = heap_alloc_zero(sizeof(*object))))
230 return E_OUTOFMEMORY;
232 if (FAILED(hr = d2d_geometry_group_init(object, (ID2D1Factory *)iface, fill_mode, geometries, geometry_count)))
234 WARN("Failed to initialise geometry group, hr %#lx.\n", hr);
235 heap_free(object);
236 return hr;
239 TRACE("Created geometry group %p.\n", object);
240 *group = (ID2D1GeometryGroup *)&object->ID2D1Geometry_iface;
242 return S_OK;
245 static HRESULT STDMETHODCALLTYPE d2d_factory_CreateTransformedGeometry(ID2D1Factory3 *iface,
246 ID2D1Geometry *src_geometry, const D2D1_MATRIX_3X2_F *transform,
247 ID2D1TransformedGeometry **transformed_geometry)
249 struct d2d_geometry *object;
251 TRACE("iface %p, src_geometry %p, transform %p, transformed_geometry %p.\n",
252 iface, src_geometry, transform, transformed_geometry);
254 if (!(object = heap_alloc_zero(sizeof(*object))))
255 return E_OUTOFMEMORY;
257 d2d_transformed_geometry_init(object, (ID2D1Factory *)iface, src_geometry, transform);
259 TRACE("Created transformed geometry %p.\n", object);
260 *transformed_geometry = (ID2D1TransformedGeometry *)&object->ID2D1Geometry_iface;
262 return S_OK;
265 static HRESULT STDMETHODCALLTYPE d2d_factory_CreatePathGeometry(ID2D1Factory3 *iface, ID2D1PathGeometry **geometry)
267 struct d2d_geometry *object;
269 TRACE("iface %p, geometry %p.\n", iface, geometry);
271 if (!(object = heap_alloc_zero(sizeof(*object))))
272 return E_OUTOFMEMORY;
274 d2d_path_geometry_init(object, (ID2D1Factory *)iface);
276 TRACE("Created path geometry %p.\n", object);
277 *geometry = (ID2D1PathGeometry *)&object->ID2D1Geometry_iface;
279 return S_OK;
282 static HRESULT STDMETHODCALLTYPE d2d_factory_CreateStrokeStyle(ID2D1Factory3 *iface,
283 const D2D1_STROKE_STYLE_PROPERTIES *desc, const float *dashes, UINT32 dash_count,
284 ID2D1StrokeStyle **stroke_style)
286 struct d2d_stroke_style *object;
287 D2D1_STROKE_STYLE_PROPERTIES1 desc1;
288 HRESULT hr;
290 TRACE("iface %p, desc %p, dashes %p, dash_count %u, stroke_style %p.\n",
291 iface, desc, dashes, dash_count, stroke_style);
293 if (!(object = heap_alloc_zero(sizeof(*object))))
294 return E_OUTOFMEMORY;
296 desc1.startCap = desc->startCap;
297 desc1.endCap = desc->endCap;
298 desc1.dashCap = desc->dashCap;
299 desc1.lineJoin = desc->lineJoin;
300 desc1.miterLimit = desc->miterLimit;
301 desc1.dashStyle = desc->dashStyle;
302 desc1.dashOffset = desc->dashOffset;
303 desc1.transformType = D2D1_STROKE_TRANSFORM_TYPE_NORMAL;
305 if (FAILED(hr = d2d_stroke_style_init(object, (ID2D1Factory *)iface, &desc1, dashes, dash_count)))
307 WARN("Failed to initialise stroke style, hr %#lx.\n", hr);
308 heap_free(object);
309 return hr;
312 TRACE("Created stroke style %p.\n", object);
313 *stroke_style = (ID2D1StrokeStyle *)&object->ID2D1StrokeStyle1_iface;
315 return S_OK;
318 static HRESULT STDMETHODCALLTYPE d2d_factory_CreateDrawingStateBlock(ID2D1Factory3 *iface,
319 const D2D1_DRAWING_STATE_DESCRIPTION *desc, IDWriteRenderingParams *text_rendering_params,
320 ID2D1DrawingStateBlock **state_block)
322 D2D1_DRAWING_STATE_DESCRIPTION1 state_desc;
323 struct d2d_state_block *object;
325 TRACE("iface %p, desc %p, text_rendering_params %p, state_block %p.\n",
326 iface, desc, text_rendering_params, state_block);
328 if (!(object = heap_alloc_zero(sizeof(*object))))
329 return E_OUTOFMEMORY;
331 if (desc)
333 memcpy(&state_desc, desc, sizeof(*desc));
334 state_desc.primitiveBlend = D2D1_PRIMITIVE_BLEND_SOURCE_OVER;
335 state_desc.unitMode = D2D1_UNIT_MODE_DIPS;
338 d2d_state_block_init(object, (ID2D1Factory *)iface, desc ? &state_desc : NULL, text_rendering_params);
340 TRACE("Created state block %p.\n", object);
341 *state_block = (ID2D1DrawingStateBlock *)&object->ID2D1DrawingStateBlock1_iface;
343 return S_OK;
346 static HRESULT d2d_factory_get_device(struct d2d_factory *factory, ID3D10Device1 **device)
348 HRESULT hr = S_OK;
350 if (!factory->device && FAILED(hr = D3D10CreateDevice1(NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL, D3D10_CREATE_DEVICE_BGRA_SUPPORT,
351 D3D10_FEATURE_LEVEL_10_0, D3D10_1_SDK_VERSION, &factory->device)))
352 WARN("Failed to create device, hr %#lx.\n", hr);
354 *device = factory->device;
355 return hr;
358 static HRESULT STDMETHODCALLTYPE d2d_factory_CreateWicBitmapRenderTarget(ID2D1Factory3 *iface,
359 IWICBitmap *target, const D2D1_RENDER_TARGET_PROPERTIES *desc, ID2D1RenderTarget **render_target)
361 struct d2d_factory *factory = impl_from_ID2D1Factory3(iface);
362 struct d2d_wic_render_target *object;
363 ID3D10Device1 *device;
364 HRESULT hr;
366 TRACE("iface %p, target %p, desc %p, render_target %p.\n", iface, target, desc, render_target);
368 if (!(object = heap_alloc_zero(sizeof(*object))))
369 return E_OUTOFMEMORY;
371 if (FAILED(hr = d2d_factory_get_device(factory, &device)))
373 heap_free(object);
374 return hr;
377 if (FAILED(hr = d2d_wic_render_target_init(object, (ID2D1Factory1 *)iface, device, target, desc)))
379 WARN("Failed to initialise render target, hr %#lx.\n", hr);
380 heap_free(object);
381 return hr;
384 TRACE("Created render target %p.\n", object);
385 *render_target = object->dxgi_target;
387 return S_OK;
390 static HRESULT STDMETHODCALLTYPE d2d_factory_CreateHwndRenderTarget(ID2D1Factory3 *iface,
391 const D2D1_RENDER_TARGET_PROPERTIES *desc, const D2D1_HWND_RENDER_TARGET_PROPERTIES *hwnd_rt_desc,
392 ID2D1HwndRenderTarget **render_target)
394 struct d2d_factory *factory = impl_from_ID2D1Factory3(iface);
395 struct d2d_hwnd_render_target *object;
396 ID3D10Device1 *device;
397 HRESULT hr;
399 TRACE("iface %p, desc %p, hwnd_rt_desc %p, render_target %p.\n", iface, desc, hwnd_rt_desc, render_target);
401 if (FAILED(hr = d2d_factory_get_device(factory, &device)))
402 return hr;
404 if (!(object = heap_alloc_zero(sizeof(*object))))
405 return E_OUTOFMEMORY;
407 if (FAILED(hr = d2d_hwnd_render_target_init(object, (ID2D1Factory1 *)iface, device, desc, hwnd_rt_desc)))
409 WARN("Failed to initialise render target, hr %#lx.\n", hr);
410 heap_free(object);
411 return hr;
414 TRACE("Created render target %p.\n", object);
415 *render_target = &object->ID2D1HwndRenderTarget_iface;
417 return S_OK;
420 static HRESULT STDMETHODCALLTYPE d2d_factory_CreateDxgiSurfaceRenderTarget(ID2D1Factory3 *iface,
421 IDXGISurface *surface, const D2D1_RENDER_TARGET_PROPERTIES *desc, ID2D1RenderTarget **render_target)
423 IDXGIDevice *dxgi_device;
424 ID2D1Device *device;
425 HRESULT hr;
427 TRACE("iface %p, surface %p, desc %p, render_target %p.\n", iface, surface, desc, render_target);
429 if (FAILED(hr = IDXGISurface_GetDevice(surface, &IID_IDXGIDevice, (void **)&dxgi_device)))
431 WARN("Failed to get DXGI device, hr %#lx.\n", hr);
432 return hr;
435 hr = ID2D1Factory1_CreateDevice((ID2D1Factory1 *)iface, dxgi_device, &device);
436 IDXGIDevice_Release(dxgi_device);
437 if (FAILED(hr))
439 WARN("Failed to create D2D device, hr %#lx.\n", hr);
440 return hr;
443 hr = d2d_d3d_create_render_target(device, surface, NULL, NULL, desc, (void **)render_target);
444 ID2D1Device_Release(device);
445 return hr;
448 static HRESULT STDMETHODCALLTYPE d2d_factory_CreateDCRenderTarget(ID2D1Factory3 *iface,
449 const D2D1_RENDER_TARGET_PROPERTIES *desc, ID2D1DCRenderTarget **render_target)
451 struct d2d_factory *factory = impl_from_ID2D1Factory3(iface);
452 struct d2d_dc_render_target *object;
453 ID3D10Device1 *device;
454 HRESULT hr;
456 TRACE("iface %p, desc %p, render_target %p.\n", iface, desc, render_target);
458 if (FAILED(hr = d2d_factory_get_device(factory, &device)))
459 return hr;
461 if (!(object = heap_alloc_zero(sizeof(*object))))
462 return E_OUTOFMEMORY;
464 if (FAILED(hr = d2d_dc_render_target_init(object, (ID2D1Factory1 *)iface, device, desc)))
466 WARN("Failed to initialise render target, hr %#lx.\n", hr);
467 heap_free(object);
468 return hr;
471 TRACE("Created render target %p.\n", object);
472 *render_target = &object->ID2D1DCRenderTarget_iface;
474 return S_OK;
477 static HRESULT STDMETHODCALLTYPE d2d_factory_CreateDevice(ID2D1Factory3 *iface,
478 IDXGIDevice *dxgi_device, ID2D1Device **device)
480 struct d2d_device *object;
482 TRACE("iface %p, dxgi_device %p, device %p.\n", iface, dxgi_device, device);
484 if (!(object = heap_alloc_zero(sizeof(*object))))
485 return E_OUTOFMEMORY;
487 d2d_device_init(object, (ID2D1Factory1 *)iface, dxgi_device);
489 TRACE("Create device %p.\n", object);
490 *device = &object->ID2D1Device_iface;
492 return S_OK;
495 static HRESULT STDMETHODCALLTYPE d2d_factory_CreateStrokeStyle1(ID2D1Factory3 *iface,
496 const D2D1_STROKE_STYLE_PROPERTIES1 *desc, const float *dashes, UINT32 dash_count,
497 ID2D1StrokeStyle1 **stroke_style)
499 struct d2d_stroke_style *object;
500 HRESULT hr;
502 TRACE("iface %p, desc %p, dashes %p, dash_count %u, stroke_style %p.\n",
503 iface, desc, dashes, dash_count, stroke_style);
505 if (!(object = heap_alloc_zero(sizeof(*object))))
506 return E_OUTOFMEMORY;
508 if (FAILED(hr = d2d_stroke_style_init(object, (ID2D1Factory *)iface,
509 desc, dashes, dash_count)))
511 WARN("Failed to initialise stroke style, hr %#lx.\n", hr);
512 heap_free(object);
513 return hr;
516 TRACE("Created stroke style %p.\n", object);
517 *stroke_style = &object->ID2D1StrokeStyle1_iface;
519 return S_OK;
522 static HRESULT STDMETHODCALLTYPE d2d_factory_CreatePathGeometry1(ID2D1Factory3 *iface, ID2D1PathGeometry1 **geometry)
524 FIXME("iface %p, geometry %p stub!\n", iface, geometry);
526 return E_NOTIMPL;
529 static HRESULT STDMETHODCALLTYPE d2d_factory_CreateDrawingStateBlock1(ID2D1Factory3 *iface,
530 const D2D1_DRAWING_STATE_DESCRIPTION1 *desc, IDWriteRenderingParams *text_rendering_params,
531 ID2D1DrawingStateBlock1 **state_block)
533 struct d2d_state_block *object;
535 TRACE("iface %p, desc %p, text_rendering_params %p, state_block %p.\n",
536 iface, desc, text_rendering_params, state_block);
538 if (!(object = heap_alloc_zero(sizeof(*object))))
539 return E_OUTOFMEMORY;
541 d2d_state_block_init(object, (ID2D1Factory *)iface, desc, text_rendering_params);
543 TRACE("Created state block %p.\n", object);
544 *state_block = &object->ID2D1DrawingStateBlock1_iface;
546 return S_OK;
549 static HRESULT STDMETHODCALLTYPE d2d_factory_CreateGdiMetafile(ID2D1Factory3 *iface,
550 IStream *stream, ID2D1GdiMetafile **metafile)
552 FIXME("iface %p, stream %p, metafile %p stub!\n", iface, stream, metafile);
554 return E_NOTIMPL;
557 static HRESULT STDMETHODCALLTYPE d2d_factory_RegisterEffectFromStream(ID2D1Factory3 *iface,
558 REFCLSID effect_id, IStream *property_xml, const D2D1_PROPERTY_BINDING *bindings,
559 UINT32 binding_count, PD2D1_EFFECT_FACTORY effect_factory)
561 FIXME("iface %p, effect_id %s, property_xml %p, bindings %p, binding_count %u, effect_factory %p stub!\n",
562 iface, debugstr_guid(effect_id), property_xml, bindings, binding_count, effect_factory);
564 return E_NOTIMPL;
567 static HRESULT STDMETHODCALLTYPE d2d_factory_RegisterEffectFromString(ID2D1Factory3 *iface,
568 REFCLSID effect_id, const WCHAR *property_xml, const D2D1_PROPERTY_BINDING *bindings,
569 UINT32 binding_count, PD2D1_EFFECT_FACTORY effect_factory)
571 FIXME("iface %p, effect_id %s, property_xml %s, bindings %p, binding_count %u, effect_factory %p stub!\n",
572 iface, debugstr_guid(effect_id), debugstr_w(property_xml), bindings, binding_count, effect_factory);
574 return S_OK;
577 static HRESULT STDMETHODCALLTYPE d2d_factory_UnregisterEffect(ID2D1Factory3 *iface, REFCLSID effect_id)
579 FIXME("iface %p, effect_id %s stub!\n", iface, debugstr_guid(effect_id));
581 return E_NOTIMPL;
584 static HRESULT STDMETHODCALLTYPE d2d_factory_GetRegisteredEffects(ID2D1Factory3 *iface,
585 CLSID *effects, UINT32 effect_count, UINT32 *returned, UINT32 *registered)
587 FIXME("iface %p, effects %p, effect_count %u, returned %p, registered %p stub!\n",
588 iface, effects, effect_count, returned, registered);
590 return E_NOTIMPL;
593 static HRESULT STDMETHODCALLTYPE d2d_factory_GetEffectProperties(ID2D1Factory3 *iface,
594 REFCLSID effect_id, ID2D1Properties **props)
596 FIXME("iface %p, effect_id %s, props %p stub!\n", iface, debugstr_guid(effect_id), props);
598 return E_NOTIMPL;
601 static HRESULT STDMETHODCALLTYPE d2d_factory_ID2D1Factory2_CreateDevice(ID2D1Factory3 *iface, IDXGIDevice *dxgi_device,
602 ID2D1Device1 **device)
604 FIXME("iface %p, dxgi_device %p, device %p stub!\n", iface, dxgi_device, device);
606 return E_NOTIMPL;
609 static HRESULT STDMETHODCALLTYPE d2d_factory_ID2D1Factory3_CreateDevice(ID2D1Factory3 *iface, IDXGIDevice *dxgi_device,
610 ID2D1Device2 **device)
612 FIXME("iface %p, dxgi_device %p, device %p stub!\n", iface, dxgi_device, device);
614 return E_NOTIMPL;
617 static const struct ID2D1Factory3Vtbl d2d_factory_vtbl =
619 d2d_factory_QueryInterface,
620 d2d_factory_AddRef,
621 d2d_factory_Release,
622 d2d_factory_ReloadSystemMetrics,
623 d2d_factory_GetDesktopDpi,
624 d2d_factory_CreateRectangleGeometry,
625 d2d_factory_CreateRoundedRectangleGeometry,
626 d2d_factory_CreateEllipseGeometry,
627 d2d_factory_CreateGeometryGroup,
628 d2d_factory_CreateTransformedGeometry,
629 d2d_factory_CreatePathGeometry,
630 d2d_factory_CreateStrokeStyle,
631 d2d_factory_CreateDrawingStateBlock,
632 d2d_factory_CreateWicBitmapRenderTarget,
633 d2d_factory_CreateHwndRenderTarget,
634 d2d_factory_CreateDxgiSurfaceRenderTarget,
635 d2d_factory_CreateDCRenderTarget,
636 d2d_factory_CreateDevice,
637 d2d_factory_CreateStrokeStyle1,
638 d2d_factory_CreatePathGeometry1,
639 d2d_factory_CreateDrawingStateBlock1,
640 d2d_factory_CreateGdiMetafile,
641 d2d_factory_RegisterEffectFromStream,
642 d2d_factory_RegisterEffectFromString,
643 d2d_factory_UnregisterEffect,
644 d2d_factory_GetRegisteredEffects,
645 d2d_factory_GetEffectProperties,
646 d2d_factory_ID2D1Factory2_CreateDevice,
647 d2d_factory_ID2D1Factory3_CreateDevice,
650 static HRESULT STDMETHODCALLTYPE d2d_factory_mt_QueryInterface(ID2D1Multithread *iface, REFIID iid, void **out)
652 struct d2d_factory *factory = impl_from_ID2D1Multithread(iface);
653 return d2d_factory_QueryInterface(&factory->ID2D1Factory3_iface, iid, out);
656 static ULONG STDMETHODCALLTYPE d2d_factory_mt_AddRef(ID2D1Multithread *iface)
658 struct d2d_factory *factory = impl_from_ID2D1Multithread(iface);
659 return d2d_factory_AddRef(&factory->ID2D1Factory3_iface);
662 static ULONG STDMETHODCALLTYPE d2d_factory_mt_Release(ID2D1Multithread *iface)
664 struct d2d_factory *factory = impl_from_ID2D1Multithread(iface);
665 return d2d_factory_Release(&factory->ID2D1Factory3_iface);
668 static BOOL STDMETHODCALLTYPE d2d_factory_mt_GetMultithreadProtected(ID2D1Multithread *iface)
670 return TRUE;
673 static void STDMETHODCALLTYPE d2d_factory_mt_Enter(ID2D1Multithread *iface)
675 struct d2d_factory *factory = impl_from_ID2D1Multithread(iface);
677 TRACE("%p.\n", iface);
679 EnterCriticalSection(&factory->cs);
682 static void STDMETHODCALLTYPE d2d_factory_mt_Leave(ID2D1Multithread *iface)
684 struct d2d_factory *factory = impl_from_ID2D1Multithread(iface);
686 TRACE("%p.\n", iface);
688 LeaveCriticalSection(&factory->cs);
691 static BOOL STDMETHODCALLTYPE d2d_factory_st_GetMultithreadProtected(ID2D1Multithread *iface)
693 return FALSE;
696 static void STDMETHODCALLTYPE d2d_factory_st_Enter(ID2D1Multithread *iface)
700 static void STDMETHODCALLTYPE d2d_factory_st_Leave(ID2D1Multithread *iface)
704 static const struct ID2D1MultithreadVtbl d2d_factory_multithread_vtbl =
706 d2d_factory_mt_QueryInterface,
707 d2d_factory_mt_AddRef,
708 d2d_factory_mt_Release,
709 d2d_factory_mt_GetMultithreadProtected,
710 d2d_factory_mt_Enter,
711 d2d_factory_mt_Leave,
714 static const struct ID2D1MultithreadVtbl d2d_factory_multithread_noop_vtbl =
716 d2d_factory_mt_QueryInterface,
717 d2d_factory_mt_AddRef,
718 d2d_factory_mt_Release,
719 d2d_factory_st_GetMultithreadProtected,
720 d2d_factory_st_Enter,
721 d2d_factory_st_Leave,
724 static void d2d_factory_init(struct d2d_factory *factory, D2D1_FACTORY_TYPE factory_type,
725 const D2D1_FACTORY_OPTIONS *factory_options)
727 if (factory_options && factory_options->debugLevel != D2D1_DEBUG_LEVEL_NONE)
728 WARN("Ignoring debug level %#x.\n", factory_options->debugLevel);
730 factory->ID2D1Factory3_iface.lpVtbl = &d2d_factory_vtbl;
731 factory->ID2D1Multithread_iface.lpVtbl = factory_type == D2D1_FACTORY_TYPE_SINGLE_THREADED ?
732 &d2d_factory_multithread_noop_vtbl : &d2d_factory_multithread_vtbl;
733 factory->refcount = 1;
734 d2d_factory_reload_sysmetrics(factory);
735 InitializeCriticalSection(&factory->cs);
738 HRESULT WINAPI D2D1CreateFactory(D2D1_FACTORY_TYPE factory_type, REFIID iid,
739 const D2D1_FACTORY_OPTIONS *factory_options, void **factory)
741 struct d2d_factory *object;
742 HRESULT hr;
744 TRACE("factory_type %#x, iid %s, factory_options %p, factory %p.\n",
745 factory_type, debugstr_guid(iid), factory_options, factory);
747 if (factory_type != D2D1_FACTORY_TYPE_SINGLE_THREADED &&
748 factory_type != D2D1_FACTORY_TYPE_MULTI_THREADED)
750 return E_INVALIDARG;
753 if (!(object = heap_alloc_zero(sizeof(*object))))
754 return E_OUTOFMEMORY;
756 d2d_factory_init(object, factory_type, factory_options);
758 TRACE("Created factory %p.\n", object);
760 hr = ID2D1Factory3_QueryInterface(&object->ID2D1Factory3_iface, iid, factory);
761 ID2D1Factory3_Release(&object->ID2D1Factory3_iface);
763 return hr;
766 void WINAPI D2D1MakeRotateMatrix(float angle, D2D1_POINT_2F center, D2D1_MATRIX_3X2_F *matrix)
768 float theta, sin_theta, cos_theta;
770 TRACE("angle %.8e, center %s, matrix %p.\n", angle, debug_d2d_point_2f(&center), matrix);
772 theta = angle * (M_PI / 180.0f);
773 sin_theta = sinf(theta);
774 cos_theta = cosf(theta);
776 /* translate(center) * rotate(theta) * translate(-center) */
777 matrix->_11 = cos_theta;
778 matrix->_12 = sin_theta;
779 matrix->_21 = -sin_theta;
780 matrix->_22 = cos_theta;
781 matrix->_31 = center.x - center.x * cos_theta + center.y * sin_theta;
782 matrix->_32 = center.y - center.x * sin_theta - center.y * cos_theta;
785 void WINAPI D2D1MakeSkewMatrix(float angle_x, float angle_y, D2D1_POINT_2F center, D2D1_MATRIX_3X2_F *matrix)
787 float tan_x, tan_y;
789 TRACE("angle_x %.8e, angle_y %.8e, center %s, matrix %p.\n", angle_x, angle_y, debug_d2d_point_2f(&center), matrix);
791 tan_x = tan(angle_x * (M_PI / 180.0f));
792 tan_y = tan(angle_y * (M_PI / 180.0f));
794 /* translate(-center) * skew() * translate(center) */
795 matrix->_11 = 1.0f;
796 matrix->_12 = tan_y;
797 matrix->_21 = tan_x;
798 matrix->_22 = 1.0f;
799 matrix->_31 = -tan_x * center.y;
800 matrix->_32 = -tan_y * center.x;
803 BOOL WINAPI D2D1IsMatrixInvertible(const D2D1_MATRIX_3X2_F *matrix)
805 TRACE("matrix %p.\n", matrix);
807 return (matrix->_11 * matrix->_22 - matrix->_21 * matrix->_12) != 0.0f;
810 BOOL WINAPI D2D1InvertMatrix(D2D1_MATRIX_3X2_F *matrix)
812 D2D1_MATRIX_3X2_F m = *matrix;
814 TRACE("matrix %p.\n", matrix);
816 return d2d_matrix_invert(matrix, &m);
819 HRESULT WINAPI D2D1CreateDevice(IDXGIDevice *dxgi_device,
820 const D2D1_CREATION_PROPERTIES *properties, ID2D1Device **device)
822 D2D1_CREATION_PROPERTIES default_properties = {0};
823 D2D1_FACTORY_OPTIONS factory_options;
824 ID3D11Device *d3d_device;
825 ID2D1Factory1 *factory;
826 HRESULT hr;
828 TRACE("dxgi_device %p, properties %p, device %p.\n", dxgi_device, properties, device);
830 if (!properties)
832 if (SUCCEEDED(IDXGIDevice_QueryInterface(dxgi_device, &IID_ID3D11Device, (void **)&d3d_device)))
834 if (!(ID3D11Device_GetCreationFlags(d3d_device) & D3D11_CREATE_DEVICE_SINGLETHREADED))
835 default_properties.threadingMode = D2D1_THREADING_MODE_MULTI_THREADED;
836 ID3D11Device_Release(d3d_device);
838 properties = &default_properties;
841 factory_options.debugLevel = properties->debugLevel;
842 if (FAILED(hr = D2D1CreateFactory(properties->threadingMode,
843 &IID_ID2D1Factory1, &factory_options, (void **)&factory)))
844 return hr;
846 hr = ID2D1Factory1_CreateDevice(factory, dxgi_device, device);
847 ID2D1Factory1_Release(factory);
848 return hr;
851 void WINAPI D2D1SinCos(float angle, float *s, float *c)
853 TRACE("angle %.8e, s %p, c %p.\n", angle, s, c);
855 *s = sinf(angle);
856 *c = cosf(angle);
859 float WINAPI D2D1Tan(float angle)
861 TRACE("angle %.8e.\n", angle);
863 return tanf(angle);
866 float WINAPI D2D1Vec3Length(float x, float y, float z)
868 TRACE("x %.8e, y %.8e, z %.8e.\n", x, y, z);
870 return sqrtf(x * x + y * y + z * z);
873 /* See IEC 61966-2-1:1999; also described in the EXT_texture_sRGB OpenGL
874 * extension, among others. */
875 static float srgb_transfer_function(float x)
877 if (x <= 0.0f)
878 return 0.0f;
879 else if (x >= 1.0f)
880 return 1.0f;
881 else if (x <= 0.0031308f)
882 return 12.92f * x;
883 else
884 return 1.055f * powf(x, 1.0f / 2.4f) - 0.055f;
887 static float srgb_inverse_transfer_function(float x)
889 if (x <= 0.0f)
890 return 0.0f;
891 else if (x >= 1.0f)
892 return 1.0f;
893 else if (x <= 0.04045f)
894 return x / 12.92f;
895 else
896 return powf((x + 0.055f) / 1.055f, 2.4f);
899 D2D1_COLOR_F WINAPI D2D1ConvertColorSpace(D2D1_COLOR_SPACE src_colour_space,
900 D2D1_COLOR_SPACE dst_colour_space, const D2D1_COLOR_F *colour)
902 D2D1_COLOR_F ret;
904 TRACE("src_colour_space %#x, dst_colour_space %#x, colour %s.\n",
905 src_colour_space, dst_colour_space, debug_d2d_color_f(colour));
907 if (src_colour_space == D2D1_COLOR_SPACE_CUSTOM || dst_colour_space == D2D1_COLOR_SPACE_CUSTOM)
909 ret.r = 0.0f;
910 ret.g = 0.0f;
911 ret.b = 0.0f;
912 ret.a = 0.0f;
914 return ret;
917 if (src_colour_space == dst_colour_space)
918 return *colour;
920 if (src_colour_space == D2D1_COLOR_SPACE_SRGB && dst_colour_space == D2D1_COLOR_SPACE_SCRGB)
922 ret.r = srgb_inverse_transfer_function(colour->r);
923 ret.g = srgb_inverse_transfer_function(colour->g);
924 ret.b = srgb_inverse_transfer_function(colour->b);
925 ret.a = colour->a;
927 return ret;
930 if (src_colour_space == D2D1_COLOR_SPACE_SCRGB && dst_colour_space == D2D1_COLOR_SPACE_SRGB)
932 ret.r = srgb_transfer_function(colour->r);
933 ret.g = srgb_transfer_function(colour->g);
934 ret.b = srgb_transfer_function(colour->b);
935 ret.a = colour->a;
937 return ret;
940 FIXME("Unhandled conversion from source colour space %#x to destination colour space %#x.\n",
941 src_colour_space, dst_colour_space);
942 ret.r = 0.0f;
943 ret.g = 0.0f;
944 ret.b = 0.0f;
945 ret.a = 0.0f;
947 return ret;
950 static bool get_config_key_u32(HKEY default_key, HKEY application_key, const char *name, uint32_t *value)
952 DWORD type, size;
953 uint32_t data;
955 size = sizeof(data);
956 if (application_key && !RegQueryValueExA(application_key,
957 name, 0, &type, (BYTE *)&data, &size) && type == REG_DWORD)
958 goto success;
960 size = sizeof(data);
961 if (default_key && !RegQueryValueExA(default_key,
962 name, 0, &type, (BYTE *)&data, &size) && type == REG_DWORD)
963 goto success;
965 return false;
967 success:
968 *value = data;
969 return true;
972 static void d2d_settings_init(void)
974 HKEY default_key, tmp_key, application_key = NULL;
975 char buffer[MAX_PATH + 10];
976 DWORD len;
978 if (RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Direct2D", &default_key))
979 default_key = NULL;
981 len = GetModuleFileNameA(0, buffer, MAX_PATH);
982 if (len && len < MAX_PATH)
984 char *p, *appname = buffer;
986 if ((p = strrchr(appname, '/')))
987 appname = p + 1;
988 if ((p = strrchr(appname, '\\')))
989 appname = p + 1;
990 strcat(appname, "\\Direct2D");
992 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmp_key))
994 if (RegOpenKeyA(tmp_key, appname, &application_key))
995 application_key = NULL;
996 RegCloseKey(tmp_key);
1000 if (!default_key && !application_key)
1001 return;
1003 if (get_config_key_u32(default_key, application_key, "max_version_factory", &d2d_settings.max_version_factory))
1004 ERR_(winediag)("Limiting maximum Direct2D factory version to %#x.\n", d2d_settings.max_version_factory);
1006 if (application_key)
1007 RegCloseKey(application_key);
1008 if (default_key)
1009 RegCloseKey(default_key);
1012 BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, void *reserved)
1014 if (reason == DLL_PROCESS_ATTACH)
1015 d2d_settings_init();
1016 return TRUE;