mshtml: Move the MessageEvent construction to a helper.
[wine.git] / dlls / d2d1 / brush.c
blob98aa889aa5fa27cd8a7a849d28da4b5f81a153ae
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 "d2d1_private.h"
21 WINE_DEFAULT_DEBUG_CHANNEL(d2d);
23 static inline struct d2d_gradient *impl_from_ID2D1GradientStopCollection(ID2D1GradientStopCollection *iface)
25 return CONTAINING_RECORD(iface, struct d2d_gradient, ID2D1GradientStopCollection_iface);
28 static HRESULT STDMETHODCALLTYPE d2d_gradient_QueryInterface(ID2D1GradientStopCollection *iface,
29 REFIID iid, void **out)
31 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
33 if (IsEqualGUID(iid, &IID_ID2D1GradientStopCollection)
34 || IsEqualGUID(iid, &IID_ID2D1Resource)
35 || IsEqualGUID(iid, &IID_IUnknown))
37 ID2D1GradientStopCollection_AddRef(iface);
38 *out = iface;
39 return S_OK;
42 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
44 *out = NULL;
45 return E_NOINTERFACE;
48 static ULONG STDMETHODCALLTYPE d2d_gradient_AddRef(ID2D1GradientStopCollection *iface)
50 struct d2d_gradient *gradient = impl_from_ID2D1GradientStopCollection(iface);
51 ULONG refcount = InterlockedIncrement(&gradient->refcount);
53 TRACE("%p increasing refcount to %lu.\n", iface, refcount);
55 return refcount;
58 static ULONG STDMETHODCALLTYPE d2d_gradient_Release(ID2D1GradientStopCollection *iface)
60 struct d2d_gradient *gradient = impl_from_ID2D1GradientStopCollection(iface);
61 ULONG refcount = InterlockedDecrement(&gradient->refcount);
63 TRACE("%p decreasing refcount to %lu.\n", iface, refcount);
65 if (!refcount)
67 free(gradient->stops);
68 ID3D11ShaderResourceView_Release(gradient->view);
69 ID2D1Factory_Release(gradient->factory);
70 free(gradient);
73 return refcount;
76 static void STDMETHODCALLTYPE d2d_gradient_GetFactory(ID2D1GradientStopCollection *iface, ID2D1Factory **factory)
78 struct d2d_gradient *gradient = impl_from_ID2D1GradientStopCollection(iface);
80 TRACE("iface %p, factory %p.\n", iface, factory);
82 ID2D1Factory_AddRef(*factory = gradient->factory);
85 static UINT32 STDMETHODCALLTYPE d2d_gradient_GetGradientStopCount(ID2D1GradientStopCollection *iface)
87 struct d2d_gradient *gradient = impl_from_ID2D1GradientStopCollection(iface);
89 TRACE("iface %p.\n", iface);
91 return gradient->stop_count;
94 static void STDMETHODCALLTYPE d2d_gradient_GetGradientStops(ID2D1GradientStopCollection *iface,
95 D2D1_GRADIENT_STOP *stops, UINT32 stop_count)
97 struct d2d_gradient *gradient = impl_from_ID2D1GradientStopCollection(iface);
99 TRACE("iface %p, stops %p, stop_count %u.\n", iface, stops, stop_count);
101 memcpy(stops, gradient->stops, min(gradient->stop_count, stop_count) * sizeof(*stops));
104 static D2D1_GAMMA STDMETHODCALLTYPE d2d_gradient_GetColorInterpolationGamma(ID2D1GradientStopCollection *iface)
106 FIXME("iface %p stub!\n", iface);
108 return D2D1_GAMMA_1_0;
111 static D2D1_EXTEND_MODE STDMETHODCALLTYPE d2d_gradient_GetExtendMode(ID2D1GradientStopCollection *iface)
113 FIXME("iface %p stub!\n", iface);
115 return D2D1_EXTEND_MODE_CLAMP;
118 static const struct ID2D1GradientStopCollectionVtbl d2d_gradient_vtbl =
120 d2d_gradient_QueryInterface,
121 d2d_gradient_AddRef,
122 d2d_gradient_Release,
123 d2d_gradient_GetFactory,
124 d2d_gradient_GetGradientStopCount,
125 d2d_gradient_GetGradientStops,
126 d2d_gradient_GetColorInterpolationGamma,
127 d2d_gradient_GetExtendMode,
130 HRESULT d2d_gradient_create(ID2D1Factory *factory, ID3D11Device1 *device, const D2D1_GRADIENT_STOP *stops,
131 UINT32 stop_count, D2D1_GAMMA gamma, D2D1_EXTEND_MODE extend_mode, struct d2d_gradient **out)
133 D3D11_SHADER_RESOURCE_VIEW_DESC srv_desc;
134 D3D11_SUBRESOURCE_DATA buffer_data;
135 ID3D11ShaderResourceView *view;
136 struct d2d_gradient *gradient;
137 D3D11_BUFFER_DESC buffer_desc;
138 struct d2d_vec4 *data;
139 ID3D11Buffer *buffer;
140 unsigned int i;
141 HRESULT hr;
143 *out = NULL;
144 if (!(data = calloc(stop_count, 2 * sizeof(*data))))
146 ERR("Failed to allocate data.\n");
147 return E_OUTOFMEMORY;
150 for (i = 0; i < stop_count; ++i)
152 data[i * 2].x = stops[i].position;
153 data[i * 2 + 1].x = stops[i].color.r;
154 data[i * 2 + 1].y = stops[i].color.g;
155 data[i * 2 + 1].z = stops[i].color.b;
156 data[i * 2 + 1].w = stops[i].color.a;
159 buffer_desc.ByteWidth = 2 * stop_count * sizeof(*data);
160 buffer_desc.Usage = D3D11_USAGE_DEFAULT;
161 buffer_desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
162 buffer_desc.CPUAccessFlags = 0;
163 buffer_desc.MiscFlags = 0;
165 buffer_data.pSysMem = data;
166 buffer_data.SysMemPitch = 0;
167 buffer_data.SysMemSlicePitch = 0;
169 hr = ID3D11Device1_CreateBuffer(device, &buffer_desc, &buffer_data, &buffer);
170 free(data);
171 if (FAILED(hr))
173 ERR("Failed to create buffer, hr %#lx.\n", hr);
174 return hr;
177 srv_desc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
178 srv_desc.ViewDimension = D3D11_SRV_DIMENSION_BUFFER;
179 srv_desc.Buffer.ElementOffset = 0;
180 srv_desc.Buffer.ElementWidth = 2 * stop_count;
182 hr = ID3D11Device1_CreateShaderResourceView(device, (ID3D11Resource *)buffer, &srv_desc, &view);
183 ID3D11Buffer_Release(buffer);
184 if (FAILED(hr))
186 ERR("Failed to create view, hr %#lx.\n", hr);
187 return hr;
190 if (!(gradient = calloc(1, sizeof(*gradient))))
192 ID3D11ShaderResourceView_Release(view);
193 return E_OUTOFMEMORY;
196 if (gamma != D2D1_GAMMA_2_2)
197 FIXME("Ignoring gamma %#x.\n", gamma);
198 if (extend_mode != D2D1_EXTEND_MODE_CLAMP)
199 FIXME("Ignoring extend mode %#x.\n", extend_mode);
201 gradient->ID2D1GradientStopCollection_iface.lpVtbl = &d2d_gradient_vtbl;
202 gradient->refcount = 1;
203 ID2D1Factory_AddRef(gradient->factory = factory);
204 gradient->view = view;
206 gradient->stop_count = stop_count;
207 if (!(gradient->stops = calloc(stop_count, sizeof(*stops))))
209 ID3D11ShaderResourceView_Release(view);
210 free(gradient);
211 return E_OUTOFMEMORY;
213 memcpy(gradient->stops, stops, stop_count * sizeof(*stops));
215 TRACE("Created gradient %p.\n", gradient);
216 *out = gradient;
217 return S_OK;
220 static struct d2d_gradient *unsafe_impl_from_ID2D1GradientStopCollection(ID2D1GradientStopCollection *iface)
222 if (!iface)
223 return NULL;
224 assert(iface->lpVtbl == &d2d_gradient_vtbl);
225 return CONTAINING_RECORD(iface, struct d2d_gradient, ID2D1GradientStopCollection_iface);
228 static void d2d_gradient_bind(struct d2d_gradient *gradient, ID3D11Device1 *device, unsigned int brush_idx)
230 ID3D11DeviceContext *context;
231 ID3D11Device1_GetImmediateContext(device, &context);
232 ID3D11DeviceContext_PSSetShaderResources(context, 2 + brush_idx, 1, &gradient->view);
233 ID3D11DeviceContext_Release(context);
236 static void d2d_brush_destroy(struct d2d_brush *brush)
238 ID2D1Factory_Release(brush->factory);
239 free(brush);
242 static void d2d_brush_init(struct d2d_brush *brush, ID2D1Factory *factory,
243 enum d2d_brush_type type, const D2D1_BRUSH_PROPERTIES *desc, const struct ID2D1BrushVtbl *vtbl)
245 static const D2D1_MATRIX_3X2_F identity =
247 1.0f, 0.0f,
248 0.0f, 1.0f,
249 0.0f, 0.0f,
250 }}};
252 brush->ID2D1Brush_iface.lpVtbl = vtbl;
253 brush->refcount = 1;
254 ID2D1Factory_AddRef(brush->factory = factory);
255 brush->opacity = desc ? desc->opacity : 1.0f;
256 brush->transform = desc ? desc->transform : identity;
257 brush->type = type;
260 static inline struct d2d_brush *impl_from_ID2D1SolidColorBrush(ID2D1SolidColorBrush *iface)
262 return CONTAINING_RECORD(iface, struct d2d_brush, ID2D1Brush_iface);
265 static HRESULT STDMETHODCALLTYPE d2d_solid_color_brush_QueryInterface(ID2D1SolidColorBrush *iface,
266 REFIID iid, void **out)
268 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
270 if (IsEqualGUID(iid, &IID_ID2D1SolidColorBrush)
271 || IsEqualGUID(iid, &IID_ID2D1Brush)
272 || IsEqualGUID(iid, &IID_ID2D1Resource)
273 || IsEqualGUID(iid, &IID_IUnknown))
275 ID2D1SolidColorBrush_AddRef(iface);
276 *out = iface;
277 return S_OK;
280 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
282 *out = NULL;
283 return E_NOINTERFACE;
286 static ULONG STDMETHODCALLTYPE d2d_solid_color_brush_AddRef(ID2D1SolidColorBrush *iface)
288 struct d2d_brush *brush = impl_from_ID2D1SolidColorBrush(iface);
289 ULONG refcount = InterlockedIncrement(&brush->refcount);
291 TRACE("%p increasing refcount to %lu.\n", iface, refcount);
293 return refcount;
296 static ULONG STDMETHODCALLTYPE d2d_solid_color_brush_Release(ID2D1SolidColorBrush *iface)
298 struct d2d_brush *brush = impl_from_ID2D1SolidColorBrush(iface);
299 ULONG refcount = InterlockedDecrement(&brush->refcount);
301 TRACE("%p decreasing refcount to %lu.\n", iface, refcount);
303 if (!refcount)
304 d2d_brush_destroy(brush);
306 return refcount;
309 static void STDMETHODCALLTYPE d2d_solid_color_brush_GetFactory(ID2D1SolidColorBrush *iface, ID2D1Factory **factory)
311 struct d2d_brush *brush = impl_from_ID2D1SolidColorBrush(iface);
313 TRACE("iface %p, factory %p.\n", iface, factory);
315 ID2D1Factory_AddRef(*factory = brush->factory);
318 static void STDMETHODCALLTYPE d2d_solid_color_brush_SetOpacity(ID2D1SolidColorBrush *iface, float opacity)
320 struct d2d_brush *brush = impl_from_ID2D1SolidColorBrush(iface);
322 TRACE("iface %p, opacity %.8e.\n", iface, opacity);
324 brush->opacity = opacity;
327 static void STDMETHODCALLTYPE d2d_solid_color_brush_SetTransform(ID2D1SolidColorBrush *iface,
328 const D2D1_MATRIX_3X2_F *transform)
330 struct d2d_brush *brush = impl_from_ID2D1SolidColorBrush(iface);
332 TRACE("iface %p, transform %p.\n", iface, transform);
334 brush->transform = *transform;
337 static float STDMETHODCALLTYPE d2d_solid_color_brush_GetOpacity(ID2D1SolidColorBrush *iface)
339 struct d2d_brush *brush = impl_from_ID2D1SolidColorBrush(iface);
341 TRACE("iface %p.\n", iface);
343 return brush->opacity;
346 static void STDMETHODCALLTYPE d2d_solid_color_brush_GetTransform(ID2D1SolidColorBrush *iface,
347 D2D1_MATRIX_3X2_F *transform)
349 struct d2d_brush *brush = impl_from_ID2D1SolidColorBrush(iface);
351 TRACE("iface %p, transform %p.\n", iface, transform);
353 *transform = brush->transform;
356 static void STDMETHODCALLTYPE d2d_solid_color_brush_SetColor(ID2D1SolidColorBrush *iface, const D2D1_COLOR_F *color)
358 struct d2d_brush *brush = impl_from_ID2D1SolidColorBrush(iface);
360 TRACE("iface %p, color %p.\n", iface, color);
362 brush->u.solid.color = *color;
365 static D2D1_COLOR_F * STDMETHODCALLTYPE d2d_solid_color_brush_GetColor(ID2D1SolidColorBrush *iface, D2D1_COLOR_F *color)
367 struct d2d_brush *brush = impl_from_ID2D1SolidColorBrush(iface);
369 TRACE("iface %p, color %p.\n", iface, color);
371 *color = brush->u.solid.color;
372 return color;
375 static const struct ID2D1SolidColorBrushVtbl d2d_solid_color_brush_vtbl =
377 d2d_solid_color_brush_QueryInterface,
378 d2d_solid_color_brush_AddRef,
379 d2d_solid_color_brush_Release,
380 d2d_solid_color_brush_GetFactory,
381 d2d_solid_color_brush_SetOpacity,
382 d2d_solid_color_brush_SetTransform,
383 d2d_solid_color_brush_GetOpacity,
384 d2d_solid_color_brush_GetTransform,
385 d2d_solid_color_brush_SetColor,
386 d2d_solid_color_brush_GetColor,
389 HRESULT d2d_solid_color_brush_create(ID2D1Factory *factory, const D2D1_COLOR_F *color,
390 const D2D1_BRUSH_PROPERTIES *desc, struct d2d_brush **brush)
392 if (!(*brush = calloc(1, sizeof(**brush))))
393 return E_OUTOFMEMORY;
395 d2d_brush_init(*brush, factory, D2D_BRUSH_TYPE_SOLID, desc,
396 (ID2D1BrushVtbl *)&d2d_solid_color_brush_vtbl);
397 (*brush)->u.solid.color = *color;
399 TRACE("Created brush %p.\n", *brush);
400 return S_OK;
403 static inline struct d2d_brush *impl_from_ID2D1LinearGradientBrush(ID2D1LinearGradientBrush *iface)
405 return CONTAINING_RECORD(iface, struct d2d_brush, ID2D1Brush_iface);
408 static HRESULT STDMETHODCALLTYPE d2d_linear_gradient_brush_QueryInterface(ID2D1LinearGradientBrush *iface,
409 REFIID iid, void **out)
411 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
413 if (IsEqualGUID(iid, &IID_ID2D1LinearGradientBrush)
414 || IsEqualGUID(iid, &IID_ID2D1Brush)
415 || IsEqualGUID(iid, &IID_ID2D1Resource)
416 || IsEqualGUID(iid, &IID_IUnknown))
418 ID2D1LinearGradientBrush_AddRef(iface);
419 *out = iface;
420 return S_OK;
423 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
425 *out = NULL;
426 return E_NOINTERFACE;
429 static ULONG STDMETHODCALLTYPE d2d_linear_gradient_brush_AddRef(ID2D1LinearGradientBrush *iface)
431 struct d2d_brush *brush = impl_from_ID2D1LinearGradientBrush(iface);
432 ULONG refcount = InterlockedIncrement(&brush->refcount);
434 TRACE("%p increasing refcount to %lu.\n", iface, refcount);
436 return refcount;
439 static ULONG STDMETHODCALLTYPE d2d_linear_gradient_brush_Release(ID2D1LinearGradientBrush *iface)
441 struct d2d_brush *brush = impl_from_ID2D1LinearGradientBrush(iface);
442 ULONG refcount = InterlockedDecrement(&brush->refcount);
444 TRACE("%p decreasing refcount to %lu.\n", iface, refcount);
446 if (!refcount)
448 ID2D1GradientStopCollection_Release(&brush->u.linear.gradient->ID2D1GradientStopCollection_iface);
449 d2d_brush_destroy(brush);
452 return refcount;
455 static void STDMETHODCALLTYPE d2d_linear_gradient_brush_GetFactory(ID2D1LinearGradientBrush *iface,
456 ID2D1Factory **factory)
458 struct d2d_brush *brush = impl_from_ID2D1LinearGradientBrush(iface);
460 TRACE("iface %p, factory %p.\n", iface, factory);
462 ID2D1Factory_AddRef(*factory = brush->factory);
465 static void STDMETHODCALLTYPE d2d_linear_gradient_brush_SetOpacity(ID2D1LinearGradientBrush *iface, float opacity)
467 struct d2d_brush *brush = impl_from_ID2D1LinearGradientBrush(iface);
469 TRACE("iface %p, opacity %.8e.\n", iface, opacity);
471 brush->opacity = opacity;
474 static void STDMETHODCALLTYPE d2d_linear_gradient_brush_SetTransform(ID2D1LinearGradientBrush *iface,
475 const D2D1_MATRIX_3X2_F *transform)
477 struct d2d_brush *brush = impl_from_ID2D1LinearGradientBrush(iface);
479 TRACE("iface %p, transform %p.\n", iface, transform);
481 brush->transform = *transform;
484 static float STDMETHODCALLTYPE d2d_linear_gradient_brush_GetOpacity(ID2D1LinearGradientBrush *iface)
486 struct d2d_brush *brush = impl_from_ID2D1LinearGradientBrush(iface);
488 TRACE("iface %p.\n", iface);
490 return brush->opacity;
493 static void STDMETHODCALLTYPE d2d_linear_gradient_brush_GetTransform(ID2D1LinearGradientBrush *iface,
494 D2D1_MATRIX_3X2_F *transform)
496 struct d2d_brush *brush = impl_from_ID2D1LinearGradientBrush(iface);
498 TRACE("iface %p, transform %p.\n", iface, transform);
500 *transform = brush->transform;
503 static void STDMETHODCALLTYPE d2d_linear_gradient_brush_SetStartPoint(ID2D1LinearGradientBrush *iface,
504 D2D1_POINT_2F start_point)
506 struct d2d_brush *brush = impl_from_ID2D1LinearGradientBrush(iface);
508 TRACE("iface %p, start_point %s.\n", iface, debug_d2d_point_2f(&start_point));
510 brush->u.linear.start = start_point;
513 static void STDMETHODCALLTYPE d2d_linear_gradient_brush_SetEndPoint(ID2D1LinearGradientBrush *iface,
514 D2D1_POINT_2F end_point)
516 struct d2d_brush *brush = impl_from_ID2D1LinearGradientBrush(iface);
518 TRACE("iface %p, end_point %s.\n", iface, debug_d2d_point_2f(&end_point));
520 brush->u.linear.end = end_point;
523 static D2D1_POINT_2F * STDMETHODCALLTYPE d2d_linear_gradient_brush_GetStartPoint(ID2D1LinearGradientBrush *iface,
524 D2D1_POINT_2F *point)
526 struct d2d_brush *brush = impl_from_ID2D1LinearGradientBrush(iface);
528 TRACE("iface %p, point %p.\n", iface, point);
530 *point = brush->u.linear.start;
531 return point;
534 static D2D1_POINT_2F * STDMETHODCALLTYPE d2d_linear_gradient_brush_GetEndPoint(ID2D1LinearGradientBrush *iface,
535 D2D1_POINT_2F *point)
537 struct d2d_brush *brush = impl_from_ID2D1LinearGradientBrush(iface);
539 TRACE("iface %p, point %p.\n", iface, point);
541 *point = brush->u.linear.end;
542 return point;
545 static void STDMETHODCALLTYPE d2d_linear_gradient_brush_GetGradientStopCollection(ID2D1LinearGradientBrush *iface,
546 ID2D1GradientStopCollection **gradient)
548 struct d2d_brush *brush = impl_from_ID2D1LinearGradientBrush(iface);
550 TRACE("iface %p, gradient %p.\n", iface, gradient);
552 ID2D1GradientStopCollection_AddRef(*gradient = &brush->u.linear.gradient->ID2D1GradientStopCollection_iface);
555 static const struct ID2D1LinearGradientBrushVtbl d2d_linear_gradient_brush_vtbl =
557 d2d_linear_gradient_brush_QueryInterface,
558 d2d_linear_gradient_brush_AddRef,
559 d2d_linear_gradient_brush_Release,
560 d2d_linear_gradient_brush_GetFactory,
561 d2d_linear_gradient_brush_SetOpacity,
562 d2d_linear_gradient_brush_SetTransform,
563 d2d_linear_gradient_brush_GetOpacity,
564 d2d_linear_gradient_brush_GetTransform,
565 d2d_linear_gradient_brush_SetStartPoint,
566 d2d_linear_gradient_brush_SetEndPoint,
567 d2d_linear_gradient_brush_GetStartPoint,
568 d2d_linear_gradient_brush_GetEndPoint,
569 d2d_linear_gradient_brush_GetGradientStopCollection,
572 HRESULT d2d_linear_gradient_brush_create(ID2D1Factory *factory,
573 const D2D1_LINEAR_GRADIENT_BRUSH_PROPERTIES *gradient_brush_desc, const D2D1_BRUSH_PROPERTIES *brush_desc,
574 ID2D1GradientStopCollection *gradient, struct d2d_brush **brush)
576 if (!(*brush = calloc(1, sizeof(**brush))))
577 return E_OUTOFMEMORY;
579 d2d_brush_init(*brush, factory, D2D_BRUSH_TYPE_LINEAR, brush_desc,
580 (ID2D1BrushVtbl *)&d2d_linear_gradient_brush_vtbl);
581 (*brush)->u.linear.gradient = unsafe_impl_from_ID2D1GradientStopCollection(gradient);
582 ID2D1GradientStopCollection_AddRef(&(*brush)->u.linear.gradient->ID2D1GradientStopCollection_iface);
583 (*brush)->u.linear.start = gradient_brush_desc->startPoint;
584 (*brush)->u.linear.end = gradient_brush_desc->endPoint;
586 TRACE("Created brush %p.\n", *brush);
587 return S_OK;
590 static inline struct d2d_brush *impl_from_ID2D1RadialGradientBrush(ID2D1RadialGradientBrush *iface)
592 return CONTAINING_RECORD(iface, struct d2d_brush, ID2D1Brush_iface);
595 static HRESULT STDMETHODCALLTYPE d2d_radial_gradient_brush_QueryInterface(ID2D1RadialGradientBrush *iface,
596 REFIID iid, void **out)
598 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
600 if (IsEqualGUID(iid, &IID_ID2D1RadialGradientBrush)
601 || IsEqualGUID(iid, &IID_ID2D1Brush)
602 || IsEqualGUID(iid, &IID_ID2D1Resource)
603 || IsEqualGUID(iid, &IID_IUnknown))
605 ID2D1RadialGradientBrush_AddRef(iface);
606 *out = iface;
607 return S_OK;
610 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
612 *out = NULL;
613 return E_NOINTERFACE;
616 static ULONG STDMETHODCALLTYPE d2d_radial_gradient_brush_AddRef(ID2D1RadialGradientBrush *iface)
618 struct d2d_brush *brush = impl_from_ID2D1RadialGradientBrush(iface);
619 ULONG refcount = InterlockedIncrement(&brush->refcount);
621 TRACE("%p increasing refcount to %lu.\n", iface, refcount);
623 return refcount;
626 static ULONG STDMETHODCALLTYPE d2d_radial_gradient_brush_Release(ID2D1RadialGradientBrush *iface)
628 struct d2d_brush *brush = impl_from_ID2D1RadialGradientBrush(iface);
629 ULONG refcount = InterlockedDecrement(&brush->refcount);
631 TRACE("%p decreasing refcount to %lu.\n", iface, refcount);
633 if (!refcount)
635 ID2D1GradientStopCollection_Release(&brush->u.radial.gradient->ID2D1GradientStopCollection_iface);
636 d2d_brush_destroy(brush);
639 return refcount;
642 static void STDMETHODCALLTYPE d2d_radial_gradient_brush_GetFactory(ID2D1RadialGradientBrush *iface,
643 ID2D1Factory **factory)
645 struct d2d_brush *brush = impl_from_ID2D1RadialGradientBrush(iface);
647 TRACE("iface %p, factory %p.\n", iface, factory);
649 ID2D1Factory_AddRef(*factory = brush->factory);
652 static void STDMETHODCALLTYPE d2d_radial_gradient_brush_SetOpacity(ID2D1RadialGradientBrush *iface, float opacity)
654 struct d2d_brush *brush = impl_from_ID2D1RadialGradientBrush(iface);
656 TRACE("iface %p, opacity %.8e.\n", iface, opacity);
658 brush->opacity = opacity;
661 static void STDMETHODCALLTYPE d2d_radial_gradient_brush_SetTransform(ID2D1RadialGradientBrush *iface,
662 const D2D1_MATRIX_3X2_F *transform)
664 struct d2d_brush *brush = impl_from_ID2D1RadialGradientBrush(iface);
666 TRACE("iface %p, transform %p.\n", iface, transform);
668 brush->transform = *transform;
671 static float STDMETHODCALLTYPE d2d_radial_gradient_brush_GetOpacity(ID2D1RadialGradientBrush *iface)
673 struct d2d_brush *brush = impl_from_ID2D1RadialGradientBrush(iface);
675 TRACE("iface %p.\n", iface);
677 return brush->opacity;
680 static void STDMETHODCALLTYPE d2d_radial_gradient_brush_GetTransform(ID2D1RadialGradientBrush *iface,
681 D2D1_MATRIX_3X2_F *transform)
683 struct d2d_brush *brush = impl_from_ID2D1RadialGradientBrush(iface);
685 TRACE("iface %p, transform %p.\n", iface, transform);
687 *transform = brush->transform;
690 static void STDMETHODCALLTYPE d2d_radial_gradient_brush_SetCenter(ID2D1RadialGradientBrush *iface,
691 D2D1_POINT_2F centre)
693 struct d2d_brush *brush = impl_from_ID2D1RadialGradientBrush(iface);
695 TRACE("iface %p, centre %s.\n", iface, debug_d2d_point_2f(&centre));
697 brush->u.radial.centre = centre;
700 static void STDMETHODCALLTYPE d2d_radial_gradient_brush_SetGradientOriginOffset(ID2D1RadialGradientBrush *iface,
701 D2D1_POINT_2F offset)
703 struct d2d_brush *brush = impl_from_ID2D1RadialGradientBrush(iface);
705 TRACE("iface %p, offset %s.\n", iface, debug_d2d_point_2f(&offset));
707 brush->u.radial.offset = offset;
710 static void STDMETHODCALLTYPE d2d_radial_gradient_brush_SetRadiusX(ID2D1RadialGradientBrush *iface, float radius)
712 struct d2d_brush *brush = impl_from_ID2D1RadialGradientBrush(iface);
714 TRACE("iface %p, radius %.8e.\n", iface, radius);
716 brush->u.radial.radius.x = radius;
719 static void STDMETHODCALLTYPE d2d_radial_gradient_brush_SetRadiusY(ID2D1RadialGradientBrush *iface, float radius)
721 struct d2d_brush *brush = impl_from_ID2D1RadialGradientBrush(iface);
723 TRACE("iface %p, radius %.8e.\n", iface, radius);
725 brush->u.radial.radius.y = radius;
728 static D2D1_POINT_2F * STDMETHODCALLTYPE d2d_radial_gradient_brush_GetCenter(ID2D1RadialGradientBrush *iface,
729 D2D1_POINT_2F *centre)
731 struct d2d_brush *brush = impl_from_ID2D1RadialGradientBrush(iface);
733 TRACE("iface %p, centre %p.\n", iface, centre);
735 *centre = brush->u.radial.centre;
736 return centre;
739 static D2D1_POINT_2F * STDMETHODCALLTYPE d2d_radial_gradient_brush_GetGradientOriginOffset(
740 ID2D1RadialGradientBrush *iface, D2D1_POINT_2F *offset)
742 struct d2d_brush *brush = impl_from_ID2D1RadialGradientBrush(iface);
744 TRACE("iface %p, offset %p.\n", iface, offset);
746 *offset = brush->u.radial.offset;
747 return offset;
750 static float STDMETHODCALLTYPE d2d_radial_gradient_brush_GetRadiusX(ID2D1RadialGradientBrush *iface)
752 struct d2d_brush *brush = impl_from_ID2D1RadialGradientBrush(iface);
754 TRACE("iface %p.\n", iface);
756 return brush->u.radial.radius.x;
759 static float STDMETHODCALLTYPE d2d_radial_gradient_brush_GetRadiusY(ID2D1RadialGradientBrush *iface)
761 struct d2d_brush *brush = impl_from_ID2D1RadialGradientBrush(iface);
763 TRACE("iface %p.\n", iface);
765 return brush->u.radial.radius.y;
768 static void STDMETHODCALLTYPE d2d_radial_gradient_brush_GetGradientStopCollection(ID2D1RadialGradientBrush *iface,
769 ID2D1GradientStopCollection **gradient)
771 struct d2d_brush *brush = impl_from_ID2D1RadialGradientBrush(iface);
773 TRACE("iface %p, gradient %p.\n", iface, gradient);
775 ID2D1GradientStopCollection_AddRef(*gradient = &brush->u.radial.gradient->ID2D1GradientStopCollection_iface);
778 static const struct ID2D1RadialGradientBrushVtbl d2d_radial_gradient_brush_vtbl =
780 d2d_radial_gradient_brush_QueryInterface,
781 d2d_radial_gradient_brush_AddRef,
782 d2d_radial_gradient_brush_Release,
783 d2d_radial_gradient_brush_GetFactory,
784 d2d_radial_gradient_brush_SetOpacity,
785 d2d_radial_gradient_brush_SetTransform,
786 d2d_radial_gradient_brush_GetOpacity,
787 d2d_radial_gradient_brush_GetTransform,
788 d2d_radial_gradient_brush_SetCenter,
789 d2d_radial_gradient_brush_SetGradientOriginOffset,
790 d2d_radial_gradient_brush_SetRadiusX,
791 d2d_radial_gradient_brush_SetRadiusY,
792 d2d_radial_gradient_brush_GetCenter,
793 d2d_radial_gradient_brush_GetGradientOriginOffset,
794 d2d_radial_gradient_brush_GetRadiusX,
795 d2d_radial_gradient_brush_GetRadiusY,
796 d2d_radial_gradient_brush_GetGradientStopCollection,
799 HRESULT d2d_radial_gradient_brush_create(ID2D1Factory *factory,
800 const D2D1_RADIAL_GRADIENT_BRUSH_PROPERTIES *gradient_desc, const D2D1_BRUSH_PROPERTIES *brush_desc,
801 ID2D1GradientStopCollection *gradient, struct d2d_brush **brush)
803 struct d2d_brush *b;
805 if (!(b = calloc(1, sizeof(*b))))
806 return E_OUTOFMEMORY;
808 d2d_brush_init(b, factory, D2D_BRUSH_TYPE_RADIAL, brush_desc, (ID2D1BrushVtbl *)&d2d_radial_gradient_brush_vtbl);
809 b->u.radial.gradient = unsafe_impl_from_ID2D1GradientStopCollection(gradient);
810 ID2D1GradientStopCollection_AddRef(&b->u.radial.gradient->ID2D1GradientStopCollection_iface);
811 b->u.radial.centre = gradient_desc->center;
812 b->u.radial.offset = gradient_desc->gradientOriginOffset;
813 b->u.radial.radius.x = gradient_desc->radiusX;
814 b->u.radial.radius.y = gradient_desc->radiusY;
816 TRACE("Created brush %p.\n", b);
817 *brush = b;
819 return S_OK;
822 static inline struct d2d_brush *impl_from_ID2D1BitmapBrush1(ID2D1BitmapBrush1 *iface)
824 return CONTAINING_RECORD(iface, struct d2d_brush, ID2D1Brush_iface);
827 static HRESULT STDMETHODCALLTYPE d2d_bitmap_brush_QueryInterface(ID2D1BitmapBrush1 *iface,
828 REFIID iid, void **out)
830 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
832 if (IsEqualGUID(iid, &IID_ID2D1BitmapBrush1)
833 || IsEqualGUID(iid, &IID_ID2D1BitmapBrush)
834 || IsEqualGUID(iid, &IID_ID2D1Brush)
835 || IsEqualGUID(iid, &IID_ID2D1Resource)
836 || IsEqualGUID(iid, &IID_IUnknown))
838 ID2D1BitmapBrush1_AddRef(iface);
839 *out = iface;
840 return S_OK;
843 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
845 *out = NULL;
846 return E_NOINTERFACE;
849 static ULONG STDMETHODCALLTYPE d2d_bitmap_brush_AddRef(ID2D1BitmapBrush1 *iface)
851 struct d2d_brush *brush = impl_from_ID2D1BitmapBrush1(iface);
852 ULONG refcount = InterlockedIncrement(&brush->refcount);
854 TRACE("%p increasing refcount to %lu.\n", iface, refcount);
856 return refcount;
859 static ULONG STDMETHODCALLTYPE d2d_bitmap_brush_Release(ID2D1BitmapBrush1 *iface)
861 struct d2d_brush *brush = impl_from_ID2D1BitmapBrush1(iface);
862 ULONG refcount = InterlockedDecrement(&brush->refcount);
864 TRACE("%p decreasing refcount to %lu.\n", iface, refcount);
866 if (!refcount)
868 if (brush->u.bitmap.bitmap)
869 ID2D1Bitmap1_Release(&brush->u.bitmap.bitmap->ID2D1Bitmap1_iface);
870 d2d_brush_destroy(brush);
873 return refcount;
876 static void STDMETHODCALLTYPE d2d_bitmap_brush_GetFactory(ID2D1BitmapBrush1 *iface,
877 ID2D1Factory **factory)
879 struct d2d_brush *brush = impl_from_ID2D1BitmapBrush1(iface);
881 TRACE("iface %p, factory %p.\n", iface, factory);
883 ID2D1Factory_AddRef(*factory = brush->factory);
886 static void STDMETHODCALLTYPE d2d_bitmap_brush_SetOpacity(ID2D1BitmapBrush1 *iface, float opacity)
888 struct d2d_brush *brush = impl_from_ID2D1BitmapBrush1(iface);
890 TRACE("iface %p, opacity %.8e.\n", iface, opacity);
892 brush->opacity = opacity;
895 static void STDMETHODCALLTYPE d2d_bitmap_brush_SetTransform(ID2D1BitmapBrush1 *iface,
896 const D2D1_MATRIX_3X2_F *transform)
898 struct d2d_brush *brush = impl_from_ID2D1BitmapBrush1(iface);
900 TRACE("iface %p, transform %p.\n", iface, transform);
902 brush->transform = *transform;
905 static float STDMETHODCALLTYPE d2d_bitmap_brush_GetOpacity(ID2D1BitmapBrush1 *iface)
907 struct d2d_brush *brush = impl_from_ID2D1BitmapBrush1(iface);
909 TRACE("iface %p.\n", iface);
911 return brush->opacity;
914 static void STDMETHODCALLTYPE d2d_bitmap_brush_GetTransform(ID2D1BitmapBrush1 *iface,
915 D2D1_MATRIX_3X2_F *transform)
917 struct d2d_brush *brush = impl_from_ID2D1BitmapBrush1(iface);
919 TRACE("iface %p, transform %p.\n", iface, transform);
921 *transform = brush->transform;
924 static void STDMETHODCALLTYPE d2d_bitmap_brush_SetExtendModeX(ID2D1BitmapBrush1 *iface, D2D1_EXTEND_MODE mode)
926 struct d2d_brush *brush = impl_from_ID2D1BitmapBrush1(iface);
928 TRACE("iface %p, mode %#x.\n", iface, mode);
930 brush->u.bitmap.extend_mode_x = mode;
933 static void STDMETHODCALLTYPE d2d_bitmap_brush_SetExtendModeY(ID2D1BitmapBrush1 *iface, D2D1_EXTEND_MODE mode)
935 struct d2d_brush *brush = impl_from_ID2D1BitmapBrush1(iface);
937 TRACE("iface %p, mode %#x.\n", iface, mode);
939 brush->u.bitmap.extend_mode_y = mode;
942 static void STDMETHODCALLTYPE d2d_bitmap_brush_SetInterpolationMode(ID2D1BitmapBrush1 *iface,
943 D2D1_BITMAP_INTERPOLATION_MODE mode)
945 struct d2d_brush *brush = impl_from_ID2D1BitmapBrush1(iface);
947 TRACE("iface %p, mode %#x.\n", iface, mode);
949 switch (mode)
951 case D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR:
952 case D2D1_BITMAP_INTERPOLATION_MODE_LINEAR:
953 break;
954 default:
955 WARN("Unknown interpolation mode %#x.\n", mode);
956 return;
959 brush->u.bitmap.interpolation_mode = d2d1_1_interp_mode_from_d2d1(mode);
962 static void STDMETHODCALLTYPE d2d_bitmap_brush_SetBitmap(ID2D1BitmapBrush1 *iface, ID2D1Bitmap *bitmap)
964 struct d2d_brush *brush = impl_from_ID2D1BitmapBrush1(iface);
966 TRACE("iface %p, bitmap %p.\n", iface, bitmap);
968 if (bitmap)
969 ID2D1Bitmap_AddRef(bitmap);
970 if (brush->u.bitmap.bitmap)
971 ID2D1Bitmap1_Release(&brush->u.bitmap.bitmap->ID2D1Bitmap1_iface);
972 brush->u.bitmap.bitmap = unsafe_impl_from_ID2D1Bitmap(bitmap);
975 static D2D1_EXTEND_MODE STDMETHODCALLTYPE d2d_bitmap_brush_GetExtendModeX(ID2D1BitmapBrush1 *iface)
977 struct d2d_brush *brush = impl_from_ID2D1BitmapBrush1(iface);
979 TRACE("iface %p.\n", iface);
981 return brush->u.bitmap.extend_mode_x;
984 static D2D1_EXTEND_MODE STDMETHODCALLTYPE d2d_bitmap_brush_GetExtendModeY(ID2D1BitmapBrush1 *iface)
986 struct d2d_brush *brush = impl_from_ID2D1BitmapBrush1(iface);
988 TRACE("iface %p.\n", iface);
990 return brush->u.bitmap.extend_mode_y;
993 static D2D1_BITMAP_INTERPOLATION_MODE STDMETHODCALLTYPE d2d_bitmap_brush_GetInterpolationMode(ID2D1BitmapBrush1 *iface)
995 struct d2d_brush *brush = impl_from_ID2D1BitmapBrush1(iface);
997 TRACE("iface %p.\n", iface);
999 switch (brush->u.bitmap.interpolation_mode)
1001 case D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR:
1002 return D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR;
1003 case D2D1_INTERPOLATION_MODE_LINEAR:
1004 default:
1005 return D2D1_BITMAP_INTERPOLATION_MODE_LINEAR;
1009 static void STDMETHODCALLTYPE d2d_bitmap_brush_GetBitmap(ID2D1BitmapBrush1 *iface, ID2D1Bitmap **bitmap)
1011 struct d2d_brush *brush = impl_from_ID2D1BitmapBrush1(iface);
1013 TRACE("iface %p, bitmap %p.\n", iface, bitmap);
1015 if ((*bitmap = (ID2D1Bitmap *)&brush->u.bitmap.bitmap->ID2D1Bitmap1_iface))
1016 ID2D1Bitmap_AddRef(*bitmap);
1019 static void STDMETHODCALLTYPE d2d_bitmap_brush_SetInterpolationMode1(ID2D1BitmapBrush1 *iface,
1020 D2D1_INTERPOLATION_MODE mode)
1022 struct d2d_brush *brush = impl_from_ID2D1BitmapBrush1(iface);
1024 TRACE("iface %p, mode %#x.\n", iface, mode);
1026 switch (mode)
1028 case D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR:
1029 case D2D1_INTERPOLATION_MODE_LINEAR:
1030 break;
1031 case D2D1_INTERPOLATION_MODE_CUBIC:
1032 case D2D1_INTERPOLATION_MODE_MULTI_SAMPLE_LINEAR:
1033 case D2D1_INTERPOLATION_MODE_ANISOTROPIC:
1034 case D2D1_INTERPOLATION_MODE_HIGH_QUALITY_CUBIC:
1035 FIXME("Unhandled interpolation mode %#x.\n", mode);
1036 break;
1037 default:
1038 WARN("Unknown interpolation mode %#x.\n", mode);
1039 return;
1042 brush->u.bitmap.interpolation_mode = mode;
1045 static D2D1_INTERPOLATION_MODE STDMETHODCALLTYPE d2d_bitmap_brush_GetInterpolationMode1(ID2D1BitmapBrush1 *iface)
1047 struct d2d_brush *brush = impl_from_ID2D1BitmapBrush1(iface);
1049 TRACE("iface %p.\n", iface);
1051 return brush->u.bitmap.interpolation_mode;
1054 static const struct ID2D1BitmapBrush1Vtbl d2d_bitmap_brush_vtbl =
1056 d2d_bitmap_brush_QueryInterface,
1057 d2d_bitmap_brush_AddRef,
1058 d2d_bitmap_brush_Release,
1059 d2d_bitmap_brush_GetFactory,
1060 d2d_bitmap_brush_SetOpacity,
1061 d2d_bitmap_brush_SetTransform,
1062 d2d_bitmap_brush_GetOpacity,
1063 d2d_bitmap_brush_GetTransform,
1064 d2d_bitmap_brush_SetExtendModeX,
1065 d2d_bitmap_brush_SetExtendModeY,
1066 d2d_bitmap_brush_SetInterpolationMode,
1067 d2d_bitmap_brush_SetBitmap,
1068 d2d_bitmap_brush_GetExtendModeX,
1069 d2d_bitmap_brush_GetExtendModeY,
1070 d2d_bitmap_brush_GetInterpolationMode,
1071 d2d_bitmap_brush_GetBitmap,
1072 d2d_bitmap_brush_SetInterpolationMode1,
1073 d2d_bitmap_brush_GetInterpolationMode1,
1076 HRESULT d2d_bitmap_brush_create(ID2D1Factory *factory, ID2D1Bitmap *bitmap,
1077 const D2D1_BITMAP_BRUSH_PROPERTIES1 *bitmap_brush_desc, const D2D1_BRUSH_PROPERTIES *brush_desc,
1078 struct d2d_brush **brush)
1080 if (!(*brush = calloc(1, sizeof(**brush))))
1081 return E_OUTOFMEMORY;
1083 d2d_brush_init(*brush, factory, D2D_BRUSH_TYPE_BITMAP,
1084 brush_desc, (ID2D1BrushVtbl *)&d2d_bitmap_brush_vtbl);
1085 if (((*brush)->u.bitmap.bitmap = unsafe_impl_from_ID2D1Bitmap(bitmap)))
1086 ID2D1Bitmap1_AddRef(&(*brush)->u.bitmap.bitmap->ID2D1Bitmap1_iface);
1087 if (bitmap_brush_desc)
1089 (*brush)->u.bitmap.extend_mode_x = bitmap_brush_desc->extendModeX;
1090 (*brush)->u.bitmap.extend_mode_y = bitmap_brush_desc->extendModeY;
1091 (*brush)->u.bitmap.interpolation_mode = bitmap_brush_desc->interpolationMode;
1093 else
1095 (*brush)->u.bitmap.extend_mode_x = D2D1_EXTEND_MODE_CLAMP;
1096 (*brush)->u.bitmap.extend_mode_y = D2D1_EXTEND_MODE_CLAMP;
1097 (*brush)->u.bitmap.interpolation_mode = D2D1_INTERPOLATION_MODE_LINEAR;
1100 TRACE("Created brush %p.\n", *brush);
1101 return S_OK;
1104 static inline struct d2d_brush *impl_from_ID2D1ImageBrush(ID2D1ImageBrush *iface)
1106 return CONTAINING_RECORD((ID2D1Brush *)iface, struct d2d_brush, ID2D1Brush_iface);
1109 static HRESULT STDMETHODCALLTYPE d2d_image_brush_QueryInterface(ID2D1ImageBrush *iface,
1110 REFIID iid, void **out)
1112 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
1114 if (IsEqualGUID(iid, &IID_ID2D1ImageBrush)
1115 || IsEqualGUID(iid, &IID_ID2D1Brush)
1116 || IsEqualGUID(iid, &IID_ID2D1Resource)
1117 || IsEqualGUID(iid, &IID_IUnknown))
1119 ID2D1ImageBrush_AddRef(iface);
1120 *out = iface;
1121 return S_OK;
1124 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
1126 *out = NULL;
1127 return E_NOINTERFACE;
1130 static ULONG STDMETHODCALLTYPE d2d_image_brush_AddRef(ID2D1ImageBrush *iface)
1132 struct d2d_brush *brush = impl_from_ID2D1ImageBrush(iface);
1133 ULONG refcount = InterlockedIncrement(&brush->refcount);
1135 TRACE("%p increasing refcount to %lu.\n", iface, refcount);
1137 return refcount;
1140 static ULONG STDMETHODCALLTYPE d2d_image_brush_Release(ID2D1ImageBrush *iface)
1142 struct d2d_brush *brush = impl_from_ID2D1ImageBrush(iface);
1143 ULONG refcount = InterlockedDecrement(&brush->refcount);
1145 TRACE("%p decreasing refcount to %lu.\n", iface, refcount);
1147 if (!refcount)
1149 if (brush->u.image.image)
1150 ID2D1Image_Release(brush->u.image.image);
1151 d2d_brush_destroy(brush);
1154 return refcount;
1157 static void STDMETHODCALLTYPE d2d_image_brush_GetFactory(ID2D1ImageBrush *iface,
1158 ID2D1Factory **factory)
1160 struct d2d_brush *brush = impl_from_ID2D1ImageBrush(iface);
1162 TRACE("iface %p, factory %p.\n", iface, factory);
1164 ID2D1Factory_AddRef(*factory = brush->factory);
1167 static void STDMETHODCALLTYPE d2d_image_brush_SetOpacity(ID2D1ImageBrush *iface, float opacity)
1169 struct d2d_brush *brush = impl_from_ID2D1ImageBrush(iface);
1171 TRACE("iface %p, opacity %.8e.\n", iface, opacity);
1173 brush->opacity = opacity;
1176 static void STDMETHODCALLTYPE d2d_image_brush_SetTransform(ID2D1ImageBrush *iface,
1177 const D2D1_MATRIX_3X2_F *transform)
1179 struct d2d_brush *brush = impl_from_ID2D1ImageBrush(iface);
1181 TRACE("iface %p, transform %p.\n", iface, transform);
1183 brush->transform = *transform;
1186 static float STDMETHODCALLTYPE d2d_image_brush_GetOpacity(ID2D1ImageBrush *iface)
1188 struct d2d_brush *brush = impl_from_ID2D1ImageBrush(iface);
1190 TRACE("iface %p.\n", iface);
1192 return brush->opacity;
1195 static void STDMETHODCALLTYPE d2d_image_brush_GetTransform(ID2D1ImageBrush *iface,
1196 D2D1_MATRIX_3X2_F *transform)
1198 struct d2d_brush *brush = impl_from_ID2D1ImageBrush(iface);
1200 TRACE("iface %p, transform %p.\n", iface, transform);
1202 *transform = brush->transform;
1205 static void STDMETHODCALLTYPE d2d_image_brush_SetImage(ID2D1ImageBrush *iface, ID2D1Image *image)
1207 struct d2d_brush *brush = impl_from_ID2D1ImageBrush(iface);
1209 TRACE("iface %p, image %p.\n", iface, image);
1211 if (image)
1212 ID2D1Image_AddRef(image);
1213 if (brush->u.image.image)
1214 ID2D1Image_Release(brush->u.image.image);
1215 brush->u.image.image = image;
1218 static void STDMETHODCALLTYPE d2d_image_brush_SetExtendModeX(ID2D1ImageBrush *iface, D2D1_EXTEND_MODE mode)
1220 struct d2d_brush *brush = impl_from_ID2D1ImageBrush(iface);
1222 TRACE("iface %p, mode %#x.\n", iface, mode);
1224 brush->u.image.extend_mode_x = mode;
1227 static void STDMETHODCALLTYPE d2d_image_brush_SetExtendModeY(ID2D1ImageBrush *iface, D2D1_EXTEND_MODE mode)
1229 struct d2d_brush *brush = impl_from_ID2D1ImageBrush(iface);
1231 TRACE("iface %p, mode %#x.\n", iface, mode);
1233 brush->u.image.extend_mode_y = mode;
1236 static void STDMETHODCALLTYPE d2d_image_brush_SetInterpolationMode(ID2D1ImageBrush *iface,
1237 D2D1_INTERPOLATION_MODE mode)
1239 struct d2d_brush *brush = impl_from_ID2D1ImageBrush(iface);
1241 TRACE("iface %p, mode %#x.\n", iface, mode);
1243 brush->u.image.interpolation_mode = mode;
1246 static void STDMETHODCALLTYPE d2d_image_brush_SetSourceRectangle(ID2D1ImageBrush *iface, const D2D1_RECT_F *rect)
1248 struct d2d_brush *brush = impl_from_ID2D1ImageBrush(iface);
1250 TRACE("iface %p, rect %s.\n", iface, debug_d2d_rect_f(rect));
1252 brush->u.image.source_rect = *rect;
1255 static void STDMETHODCALLTYPE d2d_image_brush_GetImage(ID2D1ImageBrush *iface, ID2D1Image **image)
1257 struct d2d_brush *brush = impl_from_ID2D1ImageBrush(iface);
1259 TRACE("iface %p, image %p.\n", iface, image);
1261 if ((*image = (ID2D1Image *)brush->u.image.image))
1262 ID2D1Image_AddRef(*image);
1265 static D2D1_EXTEND_MODE STDMETHODCALLTYPE d2d_image_brush_GetExtendModeX(ID2D1ImageBrush *iface)
1267 struct d2d_brush *brush = impl_from_ID2D1ImageBrush(iface);
1269 TRACE("iface %p.\n", iface);
1271 return brush->u.image.extend_mode_x;
1274 static D2D1_EXTEND_MODE STDMETHODCALLTYPE d2d_image_brush_GetExtendModeY(ID2D1ImageBrush *iface)
1276 struct d2d_brush *brush = impl_from_ID2D1ImageBrush(iface);
1278 TRACE("iface %p.\n", iface);
1280 return brush->u.image.extend_mode_y;
1283 static D2D1_INTERPOLATION_MODE STDMETHODCALLTYPE d2d_image_brush_GetInterpolationMode(ID2D1ImageBrush *iface)
1285 struct d2d_brush *brush = impl_from_ID2D1ImageBrush(iface);
1287 TRACE("iface %p.\n", iface);
1289 return brush->u.image.interpolation_mode;
1292 static void STDMETHODCALLTYPE d2d_image_brush_GetSourceRectangle(ID2D1ImageBrush *iface, D2D1_RECT_F *rect)
1294 struct d2d_brush *brush = impl_from_ID2D1ImageBrush(iface);
1296 TRACE("iface %p, rect %p.\n", iface, rect);
1298 *rect = brush->u.image.source_rect;
1301 static const struct ID2D1ImageBrushVtbl d2d_image_brush_vtbl =
1303 d2d_image_brush_QueryInterface,
1304 d2d_image_brush_AddRef,
1305 d2d_image_brush_Release,
1306 d2d_image_brush_GetFactory,
1307 d2d_image_brush_SetOpacity,
1308 d2d_image_brush_SetTransform,
1309 d2d_image_brush_GetOpacity,
1310 d2d_image_brush_GetTransform,
1311 d2d_image_brush_SetImage,
1312 d2d_image_brush_SetExtendModeX,
1313 d2d_image_brush_SetExtendModeY,
1314 d2d_image_brush_SetInterpolationMode,
1315 d2d_image_brush_SetSourceRectangle,
1316 d2d_image_brush_GetImage,
1317 d2d_image_brush_GetExtendModeX,
1318 d2d_image_brush_GetExtendModeY,
1319 d2d_image_brush_GetInterpolationMode,
1320 d2d_image_brush_GetSourceRectangle
1323 HRESULT d2d_image_brush_create(ID2D1Factory *factory, ID2D1Image *image,
1324 const D2D1_IMAGE_BRUSH_PROPERTIES *image_brush_desc, const D2D1_BRUSH_PROPERTIES *brush_desc,
1325 struct d2d_brush **brush)
1327 if (!(*brush = calloc(1, sizeof(**brush))))
1328 return E_OUTOFMEMORY;
1330 d2d_brush_init(*brush, factory, D2D_BRUSH_TYPE_IMAGE,
1331 brush_desc, (ID2D1BrushVtbl *)&d2d_image_brush_vtbl);
1332 if (((*brush)->u.image.image = image))
1333 ID2D1Image_AddRef((*brush)->u.image.image);
1335 (*brush)->u.image.source_rect = image_brush_desc->sourceRectangle;
1336 (*brush)->u.image.extend_mode_x = image_brush_desc->extendModeX;
1337 (*brush)->u.image.extend_mode_y = image_brush_desc->extendModeY;
1338 (*brush)->u.image.interpolation_mode = image_brush_desc->interpolationMode;
1340 TRACE("Created brush %p.\n", *brush);
1341 return S_OK;
1344 struct d2d_brush *unsafe_impl_from_ID2D1Brush(ID2D1Brush *iface)
1346 if (!iface)
1347 return NULL;
1348 assert(iface->lpVtbl == (const ID2D1BrushVtbl *)&d2d_solid_color_brush_vtbl
1349 || iface->lpVtbl == (const ID2D1BrushVtbl *)&d2d_linear_gradient_brush_vtbl
1350 || iface->lpVtbl == (const ID2D1BrushVtbl *)&d2d_radial_gradient_brush_vtbl
1351 || iface->lpVtbl == (const ID2D1BrushVtbl *)&d2d_bitmap_brush_vtbl
1352 || iface->lpVtbl == (const ID2D1BrushVtbl *)&d2d_image_brush_vtbl);
1353 return CONTAINING_RECORD(iface, struct d2d_brush, ID2D1Brush_iface);
1356 static D3D11_TEXTURE_ADDRESS_MODE texture_address_mode_from_extend_mode(D2D1_EXTEND_MODE mode)
1358 switch (mode)
1360 case D2D1_EXTEND_MODE_CLAMP:
1361 return D3D11_TEXTURE_ADDRESS_CLAMP;
1362 case D2D1_EXTEND_MODE_WRAP:
1363 return D3D11_TEXTURE_ADDRESS_WRAP;
1364 case D2D1_EXTEND_MODE_MIRROR:
1365 return D3D11_TEXTURE_ADDRESS_MIRROR;
1366 default:
1367 FIXME("Unhandled extend mode %#x.\n", mode);
1368 return D3D11_TEXTURE_ADDRESS_CLAMP;
1372 BOOL d2d_brush_fill_cb(const struct d2d_brush *brush, struct d2d_brush_cb *cb)
1374 float theta, sin_theta, cos_theta;
1375 float dpi_scale, d, s1, s2, t, u;
1376 struct d2d_bitmap *bitmap;
1377 D2D1_POINT_2F v_p, v_q;
1378 D2D1_COLOR_F *colour;
1379 D2D_MATRIX_3X2_F b;
1381 if (!brush)
1383 cb->type = D2D_BRUSH_TYPE_COUNT;
1384 return TRUE;
1387 cb->type = brush->type;
1388 cb->opacity = brush->opacity;
1390 switch (brush->type)
1392 case D2D_BRUSH_TYPE_SOLID:
1393 colour = &cb->u.solid.colour;
1395 *colour = brush->u.solid.color;
1396 colour->r *= colour->a;
1397 colour->g *= colour->a;
1398 colour->b *= colour->a;
1400 return TRUE;
1402 case D2D_BRUSH_TYPE_LINEAR:
1403 b = brush->transform;
1404 d2d_point_transform(&cb->u.linear.start, &b, brush->u.linear.start.x, brush->u.linear.start.y);
1405 d2d_point_transform(&cb->u.linear.end, &b, brush->u.linear.end.x, brush->u.linear.end.y);
1406 cb->u.linear.stop_count = brush->u.linear.gradient->stop_count;
1408 return TRUE;
1410 case D2D_BRUSH_TYPE_RADIAL:
1411 b = brush->transform;
1412 d2d_point_transform(&cb->u.radial.centre, &b, brush->u.radial.centre.x, brush->u.radial.centre.y);
1413 b._31 = b._32 = 0.0f;
1414 d2d_point_transform(&cb->u.radial.offset, &b, brush->u.radial.offset.x, brush->u.radial.offset.y);
1416 /* After a transformation, the axes of the ellipse are no longer
1417 * necessarily orthogonal, but they are conjugate diameters.
1419 * A = ⎡a.x b.x⎤
1420 * ⎣a.y b.y⎦
1422 * = ⎡cos(θ) -sin(θ)⎤⎡σ₁ 0 ⎤⎡cos(φ) -sin(φ)⎤
1423 * ⎣sin(θ) cos(θ)⎦⎣0 σ₂⎦⎣sin(φ) cos(φ)⎦
1425 * a' = [σ₁· cos(θ) σ₁·sin(θ)]
1426 * b' = [σ₂·-sin(θ) σ₂·cos(θ)] */
1428 d2d_point_set(&v_p, brush->u.radial.radius.x * b._11, brush->u.radial.radius.y * b._21);
1429 d2d_point_set(&v_q, brush->u.radial.radius.x * b._12, brush->u.radial.radius.y * b._22);
1431 t = 0.5f * d2d_point_dot(&v_p, &v_p);
1432 u = 0.5f * d2d_point_dot(&v_q, &v_q);
1433 s1 = t + u;
1435 t = t - u;
1436 u = d2d_point_dot(&v_p, &v_q);
1437 s2 = sqrtf(t * t + u * u);
1439 theta = 0.5f * atan2(u, t);
1440 sin_theta = sinf(theta);
1441 cos_theta = cosf(theta);
1443 t = sqrtf(s1 + s2);
1444 d2d_point_set(&cb->u.radial.ra, t * cos_theta, t * sin_theta);
1445 t = sqrtf(s1 - s2);
1446 d2d_point_set(&cb->u.radial.rb, t * -sin_theta, t * cos_theta);
1448 cb->u.radial.stop_count = brush->u.linear.gradient->stop_count;
1450 return TRUE;
1452 case D2D_BRUSH_TYPE_BITMAP:
1453 case D2D_BRUSH_TYPE_IMAGE:
1455 ID2D1Bitmap *image_bitmap = NULL;
1457 if (brush->type == D2D_BRUSH_TYPE_BITMAP)
1458 bitmap = brush->u.bitmap.bitmap;
1459 else
1461 if (FAILED(ID2D1Image_QueryInterface(brush->u.image.image, &IID_ID2D1Bitmap, (void **)&image_bitmap)))
1463 FIXME("Only image brushes with bitmaps are supported.\n");
1464 return FALSE;
1467 bitmap = unsafe_impl_from_ID2D1Bitmap(image_bitmap);
1468 cb->type = D2D_BRUSH_TYPE_BITMAP;
1471 /* Scale for bitmap size and dpi. */
1472 b = brush->transform;
1473 dpi_scale = bitmap->pixel_size.width * (96.0f / bitmap->dpi_x);
1474 b._11 *= dpi_scale;
1475 b._21 *= dpi_scale;
1476 dpi_scale = bitmap->pixel_size.height * (96.0f / bitmap->dpi_y);
1477 b._12 *= dpi_scale;
1478 b._22 *= dpi_scale;
1480 /* Invert the matrix. (Because the matrix is applied to the
1481 * sampling coordinates. I.e., to scale the bitmap by 2 we need to
1482 * divide the coordinates by 2.) */
1483 d = b._11 * b._22 - b._21 * b._12;
1484 if (d != 0.0f)
1486 cb->u.bitmap._11 = b._22 / d;
1487 cb->u.bitmap._21 = -b._21 / d;
1488 cb->u.bitmap._31 = (b._21 * b._32 - b._31 * b._22) / d;
1489 cb->u.bitmap._12 = -b._12 / d;
1490 cb->u.bitmap._22 = b._11 / d;
1491 cb->u.bitmap._32 = -(b._11 * b._32 - b._31 * b._12) / d;
1494 cb->u.bitmap.ignore_alpha = bitmap->format.alphaMode == D2D1_ALPHA_MODE_IGNORE;
1496 if (image_bitmap)
1497 ID2D1Bitmap_Release(image_bitmap);
1499 return TRUE;
1502 default:
1503 FIXME("Unhandled brush type %#x.\n", brush->type);
1504 return FALSE;
1508 static void d2d_brush_bind_bitmap(struct d2d_bitmap *bitmap, struct d2d_device_context *context,
1509 D2D1_EXTEND_MODE extend_mode_x, D2D1_EXTEND_MODE extend_mode_y,
1510 D2D1_INTERPOLATION_MODE interpolation_mode, unsigned int brush_idx)
1512 ID3D11SamplerState **sampler_state;
1513 ID3D11DeviceContext *d3d_context;
1514 HRESULT hr;
1516 ID3D11Device1_GetImmediateContext(context->d3d_device, &d3d_context);
1517 ID3D11DeviceContext_PSSetShaderResources(d3d_context, brush_idx, 1, &bitmap->srv);
1519 sampler_state = &context->sampler_states
1520 [interpolation_mode % D2D_SAMPLER_INTERPOLATION_MODE_COUNT]
1521 [extend_mode_x % D2D_SAMPLER_EXTEND_MODE_COUNT]
1522 [extend_mode_y % D2D_SAMPLER_EXTEND_MODE_COUNT];
1524 if (!*sampler_state)
1526 D3D11_SAMPLER_DESC sampler_desc;
1528 if (interpolation_mode == D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR)
1529 sampler_desc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
1530 else
1531 sampler_desc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
1532 sampler_desc.AddressU = texture_address_mode_from_extend_mode(extend_mode_x);
1533 sampler_desc.AddressV = texture_address_mode_from_extend_mode(extend_mode_y);
1534 sampler_desc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
1535 sampler_desc.MipLODBias = 0.0f;
1536 sampler_desc.MaxAnisotropy = 0;
1537 sampler_desc.ComparisonFunc = D3D11_COMPARISON_NEVER;
1538 sampler_desc.BorderColor[0] = 0.0f;
1539 sampler_desc.BorderColor[1] = 0.0f;
1540 sampler_desc.BorderColor[2] = 0.0f;
1541 sampler_desc.BorderColor[3] = 0.0f;
1542 sampler_desc.MinLOD = 0.0f;
1543 sampler_desc.MaxLOD = 0.0f;
1545 if (FAILED(hr = ID3D11Device1_CreateSamplerState(context->d3d_device, &sampler_desc, sampler_state)))
1546 ERR("Failed to create sampler state, hr %#lx.\n", hr);
1549 ID3D11DeviceContext_PSSetSamplers(d3d_context, brush_idx, 1, sampler_state);
1550 ID3D11DeviceContext_Release(d3d_context);
1553 static void d2d_brush_bind_image(struct d2d_brush *brush, struct d2d_device_context *context,
1554 unsigned int brush_idx)
1556 ID2D1Bitmap *image_bitmap;
1557 struct d2d_bitmap *bitmap;
1559 if (FAILED(ID2D1Image_QueryInterface(brush->u.image.image, &IID_ID2D1Bitmap, (void **)&image_bitmap)))
1561 FIXME("Only image brushes with bitmaps are supported.\n");
1562 return;
1565 bitmap = unsafe_impl_from_ID2D1Bitmap(image_bitmap);
1567 d2d_brush_bind_bitmap(bitmap, context, brush->u.image.extend_mode_x, brush->u.image.extend_mode_y,
1568 brush->u.image.interpolation_mode, brush_idx);
1570 ID2D1Bitmap_Release(image_bitmap);
1573 void d2d_brush_bind_resources(struct d2d_brush *brush, struct d2d_device_context *context, unsigned int brush_idx)
1575 switch (brush->type)
1577 case D2D_BRUSH_TYPE_SOLID:
1578 break;
1580 case D2D_BRUSH_TYPE_LINEAR:
1581 d2d_gradient_bind(brush->u.linear.gradient, context->d3d_device, brush_idx);
1582 break;
1584 case D2D_BRUSH_TYPE_RADIAL:
1585 d2d_gradient_bind(brush->u.radial.gradient, context->d3d_device, brush_idx);
1586 break;
1588 case D2D_BRUSH_TYPE_BITMAP:
1589 d2d_brush_bind_bitmap(brush->u.bitmap.bitmap, context, brush->u.bitmap.extend_mode_x,
1590 brush->u.bitmap.extend_mode_y, brush->u.bitmap.interpolation_mode, brush_idx);
1591 break;
1593 case D2D_BRUSH_TYPE_IMAGE:
1594 d2d_brush_bind_image(brush, context, brush_idx);
1595 break;
1597 default:
1598 FIXME("Unhandled brush type %#x.\n", brush->type);
1599 break;