d2d1: Add D2D1Crop.
[wine.git] / dlls / d2d1 / brush.c
blob38afbe837521786dee4361c9633eabcdb13688ce
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 %u.\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 %u.\n", iface, refcount);
65 if (!refcount)
67 heap_free(gradient->stops);
68 ID3D11ShaderResourceView_Release(gradient->view);
69 ID2D1Factory_Release(gradient->factory);
70 heap_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 = heap_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 heap_free(data);
171 if (FAILED(hr))
173 ERR("Failed to create buffer, hr %#x.\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 %#x.\n", hr);
187 return hr;
190 if (!(gradient = heap_alloc_zero(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 = heap_calloc(stop_count, sizeof(*stops))))
209 ID3D11ShaderResourceView_Release(view);
210 heap_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 heap_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 %u.\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 %u.\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 = heap_alloc_zero(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 %u.\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 %u.\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 = heap_alloc_zero(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 %u.\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 %u.\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 = heap_alloc_zero(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 %u.\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 %u.\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 = heap_alloc_zero(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 struct d2d_brush *unsafe_impl_from_ID2D1Brush(ID2D1Brush *iface)
1106 if (!iface)
1107 return NULL;
1108 assert(iface->lpVtbl == (const ID2D1BrushVtbl *)&d2d_solid_color_brush_vtbl
1109 || iface->lpVtbl == (const ID2D1BrushVtbl *)&d2d_linear_gradient_brush_vtbl
1110 || iface->lpVtbl == (const ID2D1BrushVtbl *)&d2d_radial_gradient_brush_vtbl
1111 || iface->lpVtbl == (const ID2D1BrushVtbl *)&d2d_bitmap_brush_vtbl);
1112 return CONTAINING_RECORD(iface, struct d2d_brush, ID2D1Brush_iface);
1115 static D3D11_TEXTURE_ADDRESS_MODE texture_address_mode_from_extend_mode(D2D1_EXTEND_MODE mode)
1117 switch (mode)
1119 case D2D1_EXTEND_MODE_CLAMP:
1120 return D3D11_TEXTURE_ADDRESS_CLAMP;
1121 case D2D1_EXTEND_MODE_WRAP:
1122 return D3D11_TEXTURE_ADDRESS_WRAP;
1123 case D2D1_EXTEND_MODE_MIRROR:
1124 return D3D11_TEXTURE_ADDRESS_MIRROR;
1125 default:
1126 FIXME("Unhandled extend mode %#x.\n", mode);
1127 return D3D11_TEXTURE_ADDRESS_CLAMP;
1131 BOOL d2d_brush_fill_cb(const struct d2d_brush *brush, struct d2d_brush_cb *cb)
1133 float theta, sin_theta, cos_theta;
1134 float dpi_scale, d, s1, s2, t, u;
1135 struct d2d_bitmap *bitmap;
1136 D2D1_POINT_2F v_p, v_q;
1137 D2D1_COLOR_F *colour;
1138 D2D_MATRIX_3X2_F b;
1140 if (!brush)
1142 cb->type = D2D_BRUSH_TYPE_COUNT;
1143 return TRUE;
1146 cb->type = brush->type;
1147 cb->opacity = brush->opacity;
1149 switch (brush->type)
1151 case D2D_BRUSH_TYPE_SOLID:
1152 colour = &cb->u.solid.colour;
1154 *colour = brush->u.solid.color;
1155 colour->r *= colour->a;
1156 colour->g *= colour->a;
1157 colour->b *= colour->a;
1159 return TRUE;
1161 case D2D_BRUSH_TYPE_LINEAR:
1162 b = brush->transform;
1163 d2d_point_transform(&cb->u.linear.start, &b, brush->u.linear.start.x, brush->u.linear.start.y);
1164 d2d_point_transform(&cb->u.linear.end, &b, brush->u.linear.end.x, brush->u.linear.end.y);
1165 cb->u.linear.stop_count = brush->u.linear.gradient->stop_count;
1167 return TRUE;
1169 case D2D_BRUSH_TYPE_RADIAL:
1170 b = brush->transform;
1171 d2d_point_transform(&cb->u.radial.centre, &b, brush->u.radial.centre.x, brush->u.radial.centre.y);
1172 b._31 = b._32 = 0.0f;
1173 d2d_point_transform(&cb->u.radial.offset, &b, brush->u.radial.offset.x, brush->u.radial.offset.y);
1175 /* After a transformation, the axes of the ellipse are no longer
1176 * necessarily orthogonal, but they are conjugate diameters.
1178 * A = ⎡a.x b.x⎤
1179 * ⎣a.y b.y⎦
1181 * = ⎡cos(θ) -sin(θ)⎤⎡σ₁ 0 ⎤⎡cos(φ) -sin(φ)⎤
1182 * ⎣sin(θ) cos(θ)⎦⎣0 σ₂⎦⎣sin(φ) cos(φ)⎦
1184 * a' = [σ₁· cos(θ) σ₁·sin(θ)]
1185 * b' = [σ₂·-sin(θ) σ₂·cos(θ)] */
1187 d2d_point_set(&v_p, brush->u.radial.radius.x * b._11, brush->u.radial.radius.y * b._21);
1188 d2d_point_set(&v_q, brush->u.radial.radius.x * b._12, brush->u.radial.radius.y * b._22);
1190 t = 0.5f * d2d_point_dot(&v_p, &v_p);
1191 u = 0.5f * d2d_point_dot(&v_q, &v_q);
1192 s1 = t + u;
1194 t = t - u;
1195 u = d2d_point_dot(&v_p, &v_q);
1196 s2 = sqrtf(t * t + u * u);
1198 theta = 0.5f * atan2(u, t);
1199 sin_theta = sinf(theta);
1200 cos_theta = cosf(theta);
1202 t = sqrtf(s1 + s2);
1203 d2d_point_set(&cb->u.radial.ra, t * cos_theta, t * sin_theta);
1204 t = sqrtf(s1 - s2);
1205 d2d_point_set(&cb->u.radial.rb, t * -sin_theta, t * cos_theta);
1207 cb->u.radial.stop_count = brush->u.linear.gradient->stop_count;
1209 return TRUE;
1211 case D2D_BRUSH_TYPE_BITMAP:
1212 bitmap = brush->u.bitmap.bitmap;
1214 /* Scale for bitmap size and dpi. */
1215 b = brush->transform;
1216 dpi_scale = bitmap->pixel_size.width * (96.0f / bitmap->dpi_x);
1217 b._11 *= dpi_scale;
1218 b._21 *= dpi_scale;
1219 dpi_scale = bitmap->pixel_size.height * (96.0f / bitmap->dpi_y);
1220 b._12 *= dpi_scale;
1221 b._22 *= dpi_scale;
1223 /* Invert the matrix. (Because the matrix is applied to the
1224 * sampling coordinates. I.e., to scale the bitmap by 2 we need to
1225 * divide the coordinates by 2.) */
1226 d = b._11 * b._22 - b._21 * b._12;
1227 if (d != 0.0f)
1229 cb->u.bitmap._11 = b._22 / d;
1230 cb->u.bitmap._21 = -b._21 / d;
1231 cb->u.bitmap._31 = (b._21 * b._32 - b._31 * b._22) / d;
1232 cb->u.bitmap._12 = -b._12 / d;
1233 cb->u.bitmap._22 = b._11 / d;
1234 cb->u.bitmap._32 = -(b._11 * b._32 - b._31 * b._12) / d;
1237 cb->u.bitmap.ignore_alpha = bitmap->format.alphaMode == D2D1_ALPHA_MODE_IGNORE;
1239 return TRUE;
1241 default:
1242 FIXME("Unhandled brush type %#x.\n", brush->type);
1243 return FALSE;
1247 static void d2d_brush_bind_bitmap(struct d2d_brush *brush, struct d2d_device_context *context,
1248 unsigned int brush_idx)
1250 ID3D11SamplerState **sampler_state;
1251 ID3D11DeviceContext *d3d_context;
1252 HRESULT hr;
1254 ID3D11Device1_GetImmediateContext(context->d3d_device, &d3d_context);
1255 ID3D11DeviceContext_PSSetShaderResources(d3d_context, brush_idx, 1, &brush->u.bitmap.bitmap->srv);
1257 sampler_state = &context->sampler_states
1258 [brush->u.bitmap.interpolation_mode % D2D_SAMPLER_INTERPOLATION_MODE_COUNT]
1259 [brush->u.bitmap.extend_mode_x % D2D_SAMPLER_EXTEND_MODE_COUNT]
1260 [brush->u.bitmap.extend_mode_y % D2D_SAMPLER_EXTEND_MODE_COUNT];
1262 if (!*sampler_state)
1264 D3D11_SAMPLER_DESC sampler_desc;
1266 if (brush->u.bitmap.interpolation_mode == D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR)
1267 sampler_desc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
1268 else
1269 sampler_desc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
1270 sampler_desc.AddressU = texture_address_mode_from_extend_mode(brush->u.bitmap.extend_mode_x);
1271 sampler_desc.AddressV = texture_address_mode_from_extend_mode(brush->u.bitmap.extend_mode_y);
1272 sampler_desc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
1273 sampler_desc.MipLODBias = 0.0f;
1274 sampler_desc.MaxAnisotropy = 0;
1275 sampler_desc.ComparisonFunc = D3D11_COMPARISON_NEVER;
1276 sampler_desc.BorderColor[0] = 0.0f;
1277 sampler_desc.BorderColor[1] = 0.0f;
1278 sampler_desc.BorderColor[2] = 0.0f;
1279 sampler_desc.BorderColor[3] = 0.0f;
1280 sampler_desc.MinLOD = 0.0f;
1281 sampler_desc.MaxLOD = 0.0f;
1283 if (FAILED(hr = ID3D11Device1_CreateSamplerState(context->d3d_device, &sampler_desc, sampler_state)))
1284 ERR("Failed to create sampler state, hr %#x.\n", hr);
1287 ID3D11DeviceContext_PSSetSamplers(d3d_context, brush_idx, 1, sampler_state);
1288 ID3D11DeviceContext_Release(d3d_context);
1291 void d2d_brush_bind_resources(struct d2d_brush *brush, struct d2d_device_context *context, unsigned int brush_idx)
1293 switch (brush->type)
1295 case D2D_BRUSH_TYPE_SOLID:
1296 break;
1298 case D2D_BRUSH_TYPE_LINEAR:
1299 d2d_gradient_bind(brush->u.linear.gradient, context->d3d_device, brush_idx);
1300 break;
1302 case D2D_BRUSH_TYPE_RADIAL:
1303 d2d_gradient_bind(brush->u.radial.gradient, context->d3d_device, brush_idx);
1304 break;
1306 case D2D_BRUSH_TYPE_BITMAP:
1307 d2d_brush_bind_bitmap(brush, context, brush_idx);
1308 break;
1310 default:
1311 FIXME("Unhandled brush type %#x.\n", brush->type);
1312 break;