Commit 24dc5c74 authored by Henri Verbeet's avatar Henri Verbeet Committed by Alexandre Julliard

d2d1: Implement radial gradient brushes.

parent 370cbf48
......@@ -225,6 +225,11 @@ static struct d2d_gradient *unsafe_impl_from_ID2D1GradientStopCollection(ID2D1Gr
return CONTAINING_RECORD(iface, struct d2d_gradient, ID2D1GradientStopCollection_iface);
}
static void d2d_gradient_bind(struct d2d_gradient *gradient, ID3D10Device *device, unsigned int brush_idx)
{
ID3D10Device_PSSetShaderResources(device, 2 + brush_idx, 1, &gradient->view);
}
static void d2d_brush_destroy(struct d2d_brush *brush)
{
ID2D1Factory_Release(brush->factory);
......@@ -1083,10 +1088,12 @@ static D3D10_TEXTURE_ADDRESS_MODE texture_address_mode_from_extend_mode(D2D1_EXT
static BOOL d2d_brush_fill_cb(const struct d2d_brush *brush,
const struct d2d_d3d_render_target *render_target, struct d2d_brush_cb *cb)
{
float theta, sin_theta, cos_theta;
float dpi_scale, d, s1, s2, t, u;
struct d2d_bitmap *bitmap;
D2D1_POINT_2F v_p, v_q;
D2D1_COLOR_F *colour;
D2D_MATRIX_3X2_F b;
float dpi_scale, d;
if (!brush)
{
......@@ -1117,6 +1124,48 @@ static BOOL d2d_brush_fill_cb(const struct d2d_brush *brush,
return TRUE;
case D2D_BRUSH_TYPE_RADIAL:
b = brush->transform;
d2d_point_transform(&cb->u.radial.centre, &b, brush->u.radial.centre.x, brush->u.radial.centre.y);
b._31 = b._32 = 0.0f;
d2d_point_transform(&cb->u.radial.offset, &b, brush->u.radial.offset.x, brush->u.radial.offset.y);
/* After a transformation, the axes of the ellipse are no longer
* necessarily orthogonal, but they are conjugate diameters.
*
* A = ⎡a.x b.x⎤
* ⎣a.y b.y⎦
*
* = ⎡cos(θ) -sin(θ)⎤⎡σ₁ 0 ⎤⎡cos(φ) -sin(φ)⎤
* ⎣sin(θ) cos(θ)⎦⎣0 σ₂⎦⎣sin(φ) cos(φ)⎦
*
* a' = [σ₁· cos(θ) σ₁·sin(θ)]
* b' = [σ₂·-sin(θ) σ₂·cos(θ)] */
d2d_point_set(&v_p, brush->u.radial.radius.x * b._11, brush->u.radial.radius.y * b._21);
d2d_point_set(&v_q, brush->u.radial.radius.x * b._12, brush->u.radial.radius.y * b._22);
t = 0.5f * d2d_point_dot(&v_p, &v_p);
u = 0.5f * d2d_point_dot(&v_q, &v_q);
s1 = t + u;
t = t - u;
u = d2d_point_dot(&v_p, &v_q);
s2 = sqrtf(t * t + u * u);
theta = 0.5f * atan2(u, t);
sin_theta = sinf(theta);
cos_theta = cosf(theta);
t = sqrtf(s1 + s2);
d2d_point_set(&cb->u.radial.ra, t * cos_theta, t * sin_theta);
t = sqrtf(s1 - s2);
d2d_point_set(&cb->u.radial.rb, t * -sin_theta, t * cos_theta);
cb->u.radial.stop_count = brush->u.linear.gradient->stop_count;
return TRUE;
case D2D_BRUSH_TYPE_BITMAP:
bitmap = brush->u.bitmap.bitmap;
......@@ -1183,11 +1232,6 @@ HRESULT d2d_brush_get_ps_cb(struct d2d_brush *brush, struct d2d_brush *opacity_b
return hr;
}
static void d2d_brush_bind_gradient(struct d2d_brush *brush, ID3D10Device *device, unsigned int brush_idx)
{
ID3D10Device_PSSetShaderResources(device, 2 + brush_idx, 1, &brush->u.linear.gradient->view);
}
static void d2d_brush_bind_bitmap(struct d2d_brush *brush, ID3D10Device *device, unsigned int brush_idx)
{
HRESULT hr;
......@@ -1229,7 +1273,11 @@ void d2d_brush_bind_resources(struct d2d_brush *brush, ID3D10Device *device, uns
break;
case D2D_BRUSH_TYPE_LINEAR:
d2d_brush_bind_gradient(brush, device, brush_idx);
d2d_gradient_bind(brush->u.linear.gradient, device, brush_idx);
break;
case D2D_BRUSH_TYPE_RADIAL:
d2d_gradient_bind(brush->u.radial.gradient, device, brush_idx);
break;
case D2D_BRUSH_TYPE_BITMAP:
......
......@@ -90,6 +90,15 @@ struct d2d_brush_cb
} linear;
struct
{
D2D1_POINT_2F centre;
D2D1_POINT_2F offset;
D2D1_POINT_2F ra;
D2D1_POINT_2F rb;
unsigned int stop_count;
float pad[3];
} radial;
struct
{
float _11, _21, _31, pad;
float _12, _22, _32;
BOOL ignore_alpha;
......@@ -496,6 +505,11 @@ static inline void d2d_point_set(D2D1_POINT_2F *dst, float x, float y)
dst->y = y;
}
static inline float d2d_point_dot(const D2D1_POINT_2F *p0, const D2D1_POINT_2F *p1)
{
return p0->x * p1->x + p0->y * p1->y;
}
static inline void d2d_point_transform(D2D1_POINT_2F *dst, const D2D1_MATRIX_3X2_F *matrix, float x, float y)
{
dst->x = x * matrix->_11 + y * matrix->_21 + matrix->_31;
......
......@@ -406,11 +406,6 @@ static void d2d_point_calculate_bezier(D2D1_POINT_2F *out, const D2D1_POINT_2F *
out->y = t_c * (t_c * p0->y + t * p1->y) + t * (t_c * p1->y + t * p2->y);
}
static float d2d_point_dot(const D2D1_POINT_2F *p0, const D2D1_POINT_2F *p1)
{
return p0->x * p1->x + p0->y * p1->y;
}
static void d2d_point_normalise(D2D1_POINT_2F *p)
{
float l;
......
......@@ -1901,6 +1901,220 @@ static void test_linear_brush(void)
DestroyWindow(window);
}
static void test_radial_brush(void)
{
D2D1_RADIAL_GRADIENT_BRUSH_PROPERTIES gradient_properties;
ID2D1GradientStopCollection *gradient, *tmp_gradient;
ID2D1TransformedGeometry *transformed_geometry;
ID2D1RectangleGeometry *rectangle_geometry;
D2D1_MATRIX_3X2_F matrix, tmp_matrix;
ID2D1RadialGradientBrush *brush;
struct resource_readback rb;
IDXGISwapChain *swapchain;
ID2D1RenderTarget *rt;
ID3D10Device1 *device;
IDXGISurface *surface;
ID2D1Factory *factory;
D2D1_COLOR_F colour;
D2D1_POINT_2F p;
unsigned int i;
ULONG refcount;
D2D1_RECT_F r;
HWND window;
HRESULT hr;
float f;
static const D2D1_GRADIENT_STOP stops[] =
{
{0.0f, {1.0f, 0.0f, 0.0f, 1.0f}},
{0.5f, {0.0f, 1.0f, 0.0f, 1.0f}},
{1.0f, {0.0f, 0.0f, 1.0f, 1.0f}},
};
static const struct
{
unsigned int x, y;
DWORD colour;
}
test1[] =
{
{80, 80, 0xff0000ff}, {240, 80, 0xff00a857}, {400, 80, 0xff00d728}, {560, 80, 0xff0000ff},
{80, 240, 0xff006699}, {240, 240, 0xff29d600}, {400, 240, 0xff966900}, {560, 240, 0xff00a55a},
{80, 400, 0xff0000ff}, {240, 400, 0xff006e91}, {400, 400, 0xff007d82}, {560, 400, 0xff0000ff},
},
test2[] =
{
{ 40, 30, 0xff000df2}, {120, 30, 0xffffffff}, { 40, 60, 0xffffffff}, { 80, 60, 0xff00b04f},
{120, 60, 0xff007689}, {200, 60, 0xffffffff}, { 40, 90, 0xffffffff}, {120, 90, 0xff47b800},
{160, 90, 0xff00c13e}, {200, 90, 0xffffffff}, { 80, 120, 0xffffffff}, {120, 120, 0xff0000ff},
{160, 120, 0xff6f9000}, {200, 120, 0xff00718e}, {240, 120, 0xffffffff}, {160, 150, 0xffffffff},
{200, 150, 0xff00609f}, {240, 150, 0xffffffff}, {280, 150, 0xffffffff}, {320, 150, 0xffffffff},
{240, 180, 0xffffffff}, {280, 180, 0xff4040ff}, {320, 180, 0xff40b788}, {380, 180, 0xffffffff},
{200, 210, 0xffffffff}, {240, 210, 0xff4040ff}, {280, 210, 0xff4040ff}, {320, 210, 0xff76c940},
{360, 210, 0xff40cc73}, {400, 210, 0xffffffff}, {200, 240, 0xffffffff}, {280, 240, 0xff4061de},
{320, 240, 0xff9fa040}, {360, 240, 0xff404af5}, {440, 240, 0xffffffff}, {240, 270, 0xffffffff},
{280, 270, 0xff40aa95}, {320, 270, 0xff4ef140}, {360, 270, 0xff4040ff}, {440, 270, 0xffffffff},
{280, 300, 0xffffffff}, {320, 300, 0xff4093ac}, {360, 300, 0xff4040ff}, {400, 300, 0xff4040ff},
{440, 300, 0xff404af5}, {480, 300, 0xff4045fa}, {520, 300, 0xff4040ff}, {280, 330, 0xffffffff},
{360, 330, 0xffffffff}, {400, 330, 0xff4069d6}, {440, 330, 0xff40c579}, {480, 330, 0xff40e956},
{520, 330, 0xff4072cd}, {400, 360, 0xff408ab4}, {440, 360, 0xff49f540}, {480, 360, 0xffb98640},
{520, 360, 0xff40dc62}, {400, 390, 0xff405ee1}, {440, 390, 0xff40d56a}, {480, 390, 0xff62dd40},
{520, 390, 0xff4059e6},
};
if (!(device = create_device()))
{
skip("Failed to create device, skipping tests.\n");
return;
}
window = create_window();
swapchain = create_swapchain(device, window, TRUE);
hr = IDXGISwapChain_GetBuffer(swapchain, 0, &IID_IDXGISurface, (void **)&surface);
ok(SUCCEEDED(hr), "Failed to get buffer, hr %#x.\n", hr);
rt = create_render_target(surface);
ok(!!rt, "Failed to create render target.\n");
ID2D1RenderTarget_SetDpi(rt, 192.0f, 48.0f);
ID2D1RenderTarget_SetAntialiasMode(rt, D2D1_ANTIALIAS_MODE_ALIASED);
hr = ID2D1RenderTarget_CreateGradientStopCollection(rt, stops, sizeof(stops) / sizeof(*stops),
D2D1_GAMMA_2_2, D2D1_EXTEND_MODE_CLAMP, &gradient);
ok(SUCCEEDED(hr), "Failed to create stop collection, hr %#x.\n", hr);
set_point(&gradient_properties.center, 160.0f, 480.0f);
set_point(&gradient_properties.gradientOriginOffset, 40.0f, -120.0f);
gradient_properties.radiusX = 160.0f;
gradient_properties.radiusY = 480.0f;
hr = ID2D1RenderTarget_CreateRadialGradientBrush(rt, &gradient_properties, NULL, gradient, &brush);
ok(SUCCEEDED(hr), "Failed to create brush, hr %#x.\n", hr);
f = ID2D1RadialGradientBrush_GetOpacity(brush);
ok(f == 1.0f, "Got unexpected opacity %.8e.\n", f);
set_matrix_identity(&matrix);
ID2D1RadialGradientBrush_GetTransform(brush, &tmp_matrix);
ok(!memcmp(&tmp_matrix, &matrix, sizeof(matrix)),
"Got unexpected matrix {%.8e, %.8e, %.8e, %.8e, %.8e, %.8e}.\n",
tmp_matrix._11, tmp_matrix._12, tmp_matrix._21,
tmp_matrix._22, tmp_matrix._31, tmp_matrix._32);
p = ID2D1RadialGradientBrush_GetCenter(brush);
ok(compare_point(&p, 160.0f, 480.0f, 0), "Got unexpected center {%.8e, %.8e}.\n", p.x, p.y);
p = ID2D1RadialGradientBrush_GetGradientOriginOffset(brush);
ok(compare_point(&p, 40.0f, -120.0f, 0), "Got unexpected origin offset {%.8e, %.8e}.\n", p.x, p.y);
f = ID2D1RadialGradientBrush_GetRadiusX(brush);
ok(compare_float(f, 160.0f, 0), "Got unexpected x-radius %.8e.\n", f);
f = ID2D1RadialGradientBrush_GetRadiusY(brush);
ok(compare_float(f, 480.0f, 0), "Got unexpected y-radius %.8e.\n", f);
ID2D1RadialGradientBrush_GetGradientStopCollection(brush, &tmp_gradient);
ok(tmp_gradient == gradient, "Got unexpected gradient %p, expected %p.\n", tmp_gradient, gradient);
ID2D1GradientStopCollection_Release(tmp_gradient);
ID2D1RenderTarget_BeginDraw(rt);
set_color(&colour, 1.0f, 1.0f, 1.0f, 1.0f);
ID2D1RenderTarget_Clear(rt, &colour);
set_rect(&r, 0.0f, 0.0f, 320.0f, 960.0f);
ID2D1RenderTarget_FillRectangle(rt, &r, (ID2D1Brush *)brush);
hr = ID2D1RenderTarget_EndDraw(rt, NULL, NULL);
ok(SUCCEEDED(hr), "Failed to end draw, hr %#x.\n", hr);
get_surface_readback(surface, &rb);
for (i = 0; i < sizeof(test1) / sizeof(*test1); ++i)
{
DWORD colour;
colour = get_readback_colour(&rb, test1[i].x, test1[i].y);
ok(compare_colour(colour, test1[i].colour, 1),
"Got unexpected colour 0x%08x at position {%u, %u}.\n",
colour, test1[i].x, test1[i].y);
}
release_resource_readback(&rb);
ID2D1RenderTarget_BeginDraw(rt);
ID2D1RenderTarget_Clear(rt, &colour);
set_matrix_identity(&matrix);
skew_matrix(&matrix, 0.2146f, 1.575f);
ID2D1RenderTarget_SetTransform(rt, &matrix);
set_matrix_identity(&matrix);
translate_matrix(&matrix, 0.0f, 240.0f);
scale_matrix(&matrix, 0.25f, -0.25f);
ID2D1RadialGradientBrush_SetTransform(brush, &matrix);
set_rect(&r, 0.0f, 0.0f, 80.0f, 240.0f);
ID2D1RenderTarget_FillRectangle(rt, &r, (ID2D1Brush *)brush);
set_matrix_identity(&matrix);
scale_matrix(&matrix, 0.5f, 2.0f);
translate_matrix(&matrix, 320.0f, 240.0f);
rotate_matrix(&matrix, M_PI / 4.0f);
ID2D1RenderTarget_SetTransform(rt, &matrix);
set_matrix_identity(&matrix);
translate_matrix(&matrix, -75.0f, -50.0f);
scale_matrix(&matrix, 0.15f, 0.5f);
rotate_matrix(&matrix, -M_PI / 3.0f);
ID2D1RadialGradientBrush_SetTransform(brush, &matrix);
ID2D1RadialGradientBrush_SetOpacity(brush, 0.75f);
set_rect(&r, -80.0f, -60.0f, 80.0f, 60.0f);
ID2D1RenderTarget_FillRectangle(rt, &r, (ID2D1Brush *)brush);
ID2D1RenderTarget_GetFactory(rt, &factory);
set_rect(&r, -1.0f, -1.0f, 1.0f, 1.0f);
hr = ID2D1Factory_CreateRectangleGeometry(factory, &r, &rectangle_geometry);
ok(SUCCEEDED(hr), "Failed to create geometry, hr %#x.\n", hr);
set_matrix_identity(&matrix);
translate_matrix(&matrix, 228.5f, 714.0f);
scale_matrix(&matrix, 40.0f, 120.0f);
hr = ID2D1Factory_CreateTransformedGeometry(factory, (ID2D1Geometry *)rectangle_geometry,
&matrix, &transformed_geometry);
ok(SUCCEEDED(hr), "Failed to create geometry, hr %#x.\n", hr);
ID2D1RectangleGeometry_Release(rectangle_geometry);
set_matrix_identity(&matrix);
ID2D1RenderTarget_SetTransform(rt, &matrix);
ID2D1RadialGradientBrush_SetTransform(brush, &matrix);
set_point(&p, 228.5f, 714.0f);
ID2D1RadialGradientBrush_SetCenter(brush, p);
ID2D1RadialGradientBrush_SetRadiusX(brush, -40.0f);
ID2D1RadialGradientBrush_SetRadiusY(brush, 120.0f);
set_point(&p, 20.0f, 30.0f);
ID2D1RadialGradientBrush_SetGradientOriginOffset(brush, p);
ID2D1RenderTarget_FillGeometry(rt, (ID2D1Geometry *)transformed_geometry, (ID2D1Brush *)brush, NULL);
ID2D1TransformedGeometry_Release(transformed_geometry);
ID2D1Factory_Release(factory);
hr = ID2D1RenderTarget_EndDraw(rt, NULL, NULL);
ok(SUCCEEDED(hr), "Failed to end draw, hr %#x.\n", hr);
get_surface_readback(surface, &rb);
for (i = 0; i < sizeof(test2) / sizeof(*test2); ++i)
{
DWORD colour;
colour = get_readback_colour(&rb, test2[i].x, test2[i].y);
ok(compare_colour(colour, test2[i].colour, 1),
"Got unexpected colour 0x%08x at position {%u, %u}.\n",
colour, test2[i].x, test2[i].y);
}
release_resource_readback(&rb);
ID2D1RadialGradientBrush_Release(brush);
refcount = ID2D1GradientStopCollection_Release(gradient);
ok(!refcount, "Gradient has %u references left.\n", refcount);
ID2D1RenderTarget_Release(rt);
IDXGISurface_Release(surface);
IDXGISwapChain_Release(swapchain);
ID3D10Device1_Release(device);
DestroyWindow(window);
}
static void fill_geometry_sink(ID2D1GeometrySink *sink)
{
D2D1_POINT_2F point;
......@@ -6177,6 +6391,7 @@ START_TEST(d2d1)
test_color_brush();
test_bitmap_brush();
test_linear_brush();
test_radial_brush();
test_path_geometry();
test_rectangle_geometry();
test_rounded_rectangle_geometry();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment