[PATCH 2/3] d2d1: Implement rounded rectangle drawing.

Giovanni Mascellani gio at debian.org
Tue Jan 21 06:02:06 CST 2020


Currently rounded parts are approximated with quadratic Bézier curves.

Signed-off-by: Giovanni Mascellani <gio at debian.org>
---

The approximation is clearly very crude, but still a better one than
the whole shape missing!

I am also writing a patch to actually display arc segments by
introducing the appropriate shaders, and will submit it as soon as it
is a bit more polished.

---
 dlls/d2d1/d2d1_private.h |  12 ++
 dlls/d2d1/factory.c      |  22 ++-
 dlls/d2d1/geometry.c     | 302 +++++++++++++++++++++++++++++++++++++++
 dlls/d2d1/tests/d2d1.c   |   1 -
 4 files changed, 333 insertions(+), 4 deletions(-)

diff --git a/dlls/d2d1/d2d1_private.h b/dlls/d2d1/d2d1_private.h
index 2e42d6d550..a7b13e914a 100644
--- a/dlls/d2d1/d2d1_private.h
+++ b/dlls/d2d1/d2d1_private.h
@@ -483,6 +483,10 @@ struct d2d_geometry
             D2D1_RECT_F rect;
         } rectangle;
         struct
+        {
+            D2D1_ROUNDED_RECT rounded_rect;
+        } rounded_rectangle;
+        struct
         {
             ID2D1Geometry *src_geometry;
             D2D_MATRIX_3X2_F transform;
@@ -499,6 +503,8 @@ struct d2d_geometry
 void d2d_path_geometry_init(struct d2d_geometry *geometry, ID2D1Factory *factory) DECLSPEC_HIDDEN;
 HRESULT d2d_rectangle_geometry_init(struct d2d_geometry *geometry,
         ID2D1Factory *factory, const D2D1_RECT_F *rect) DECLSPEC_HIDDEN;
+HRESULT d2d_rounded_rectangle_geometry_init(struct d2d_geometry *geometry,
+        ID2D1Factory *factory, const D2D1_ROUNDED_RECT *rounded_rect) DECLSPEC_HIDDEN;
 void d2d_transformed_geometry_init(struct d2d_geometry *geometry, ID2D1Factory *factory,
         ID2D1Geometry *src_geometry, const D2D_MATRIX_3X2_F *transform) DECLSPEC_HIDDEN;
 HRESULT d2d_geometry_group_init(struct d2d_geometry *geometry, ID2D1Factory *factory,
@@ -620,4 +626,10 @@ static inline const char *debug_d2d_rect_f(const D2D1_RECT_F *rect)
     return wine_dbg_sprintf("(%.8e,%.8e)-(%.8e,%.8e)", rect->left, rect->top, rect->right, rect->bottom );
 }
 
+static inline const char *debug_d2d_rounded_rect(const D2D1_ROUNDED_RECT *rounded_rect)
+{
+    if (!rounded_rect) return "(null)";
+    return wine_dbg_sprintf("(%.8e,%.8e)-(%.8e,%.8e)[%.8e,%.8e]", rounded_rect->rect.left, rounded_rect->rect.top, rounded_rect->rect.right, rounded_rect->rect.bottom, rounded_rect->radiusX, rounded_rect->radiusY );
+}
+
 #endif /* __WINE_D2D1_PRIVATE_H */
diff --git a/dlls/d2d1/factory.c b/dlls/d2d1/factory.c
index 48a2efb96b..86284f4ae4 100644
--- a/dlls/d2d1/factory.c
+++ b/dlls/d2d1/factory.c
@@ -152,11 +152,27 @@ static HRESULT STDMETHODCALLTYPE d2d_factory_CreateRectangleGeometry(ID2D1Factor
 }
 
 static HRESULT STDMETHODCALLTYPE d2d_factory_CreateRoundedRectangleGeometry(ID2D1Factory2 *iface,
-        const D2D1_ROUNDED_RECT *rect, ID2D1RoundedRectangleGeometry **geometry)
+        const D2D1_ROUNDED_RECT *rounded_rect, ID2D1RoundedRectangleGeometry **geometry)
 {
-    FIXME("iface %p, rect %p, geometry %p stub!\n", iface, rect, geometry);
+    struct d2d_geometry *object;
+    HRESULT hr;
 
-    return E_NOTIMPL;
+    TRACE("iface %p, rounded_rect %s, geometry %p.\n", iface, debug_d2d_rounded_rect(rounded_rect), geometry);
+
+    if (!(object = heap_alloc_zero(sizeof(*object))))
+        return E_OUTOFMEMORY;
+
+    if (FAILED(hr = d2d_rounded_rectangle_geometry_init(object, (ID2D1Factory *)iface, rounded_rect)))
+    {
+        WARN("Failed to initialize rounded rectangle geometry, hr %#x.\n", hr);
+        heap_free(object);
+        return hr;
+    }
+
+    TRACE("Created rounded rectangle geometry %p.\n", object);
+    *geometry = (ID2D1RoundedRectangleGeometry *)&object->ID2D1Geometry_iface;
+
+    return S_OK;
 }
 
 static HRESULT STDMETHODCALLTYPE d2d_factory_CreateEllipseGeometry(ID2D1Factory2 *iface,
diff --git a/dlls/d2d1/geometry.c b/dlls/d2d1/geometry.c
index 6697eccee4..a8ceb1cf41 100644
--- a/dlls/d2d1/geometry.c
+++ b/dlls/d2d1/geometry.c
@@ -3840,6 +3840,307 @@ fail:
     return E_OUTOFMEMORY;
 }
 
+static inline struct d2d_geometry *impl_from_ID2D1RoundedRectangleGeometry(ID2D1RoundedRectangleGeometry *iface)
+{
+    return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);
+}
+
+static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_QueryInterface(ID2D1RoundedRectangleGeometry *iface,
+        REFIID iid, void **out)
+{
+    TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
+
+    if (IsEqualGUID(iid, &IID_ID2D1RoundedRectangleGeometry)
+            || IsEqualGUID(iid, &IID_ID2D1Geometry)
+            || IsEqualGUID(iid, &IID_ID2D1Resource)
+            || IsEqualGUID(iid, &IID_IUnknown))
+    {
+        ID2D1RoundedRectangleGeometry_AddRef(iface);
+        *out = iface;
+        return S_OK;
+    }
+
+    WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
+
+    *out = NULL;
+    return E_NOINTERFACE;
+}
+
+static ULONG STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_AddRef(ID2D1RoundedRectangleGeometry *iface)
+{
+    struct d2d_geometry *geometry = impl_from_ID2D1RoundedRectangleGeometry(iface);
+    ULONG refcount = InterlockedIncrement(&geometry->refcount);
+
+    TRACE("%p increasing refcount to %u.\n", iface, refcount);
+
+    return refcount;
+}
+
+static ULONG STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_Release(ID2D1RoundedRectangleGeometry *iface)
+{
+    struct d2d_geometry *geometry = impl_from_ID2D1RoundedRectangleGeometry(iface);
+    ULONG refcount = InterlockedDecrement(&geometry->refcount);
+
+    TRACE("%p decreasing refcount to %u.\n", iface, refcount);
+
+    if (!refcount)
+    {
+        d2d_geometry_cleanup(geometry);
+        heap_free(geometry);
+    }
+
+    return refcount;
+}
+
+static void STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_GetFactory(ID2D1RoundedRectangleGeometry *iface, ID2D1Factory **factory)
+{
+    struct d2d_geometry *geometry = impl_from_ID2D1RoundedRectangleGeometry(iface);
+
+    TRACE("iface %p, factory %p.\n", iface, factory);
+
+    ID2D1Factory_AddRef(*factory = geometry->factory);
+}
+
+static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_GetBounds(ID2D1RoundedRectangleGeometry *iface,
+        const D2D1_MATRIX_3X2_F *transform, D2D1_RECT_F *bounds)
+{
+    FIXME("iface %p, transform %p, bounds %p stub!\n", iface, transform, bounds);
+
+    return E_NOTIMPL;
+}
+
+static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_GetWidenedBounds(ID2D1RoundedRectangleGeometry *iface,
+        float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
+        float tolerance, D2D1_RECT_F *bounds)
+{
+    FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, bounds %p stub!\n",
+            iface, stroke_width, stroke_style, transform, tolerance, bounds);
+
+    return E_NOTIMPL;
+}
+
+static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_StrokeContainsPoint(ID2D1RoundedRectangleGeometry *iface,
+        D2D1_POINT_2F point, float stroke_width, ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform,
+        float tolerance, BOOL *contains)
+{
+    FIXME("iface %p, point %s, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, contains %p stub!\n",
+            iface, debug_d2d_point_2f(&point), stroke_width, stroke_style, transform, tolerance, contains);
+
+    return E_NOTIMPL;
+}
+
+static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_FillContainsPoint(ID2D1RoundedRectangleGeometry *iface,
+        D2D1_POINT_2F point, const D2D1_MATRIX_3X2_F *transform, float tolerance, BOOL *contains)
+{
+    FIXME("iface %p, point %s, transform %p, tolerance %.8e, contains %p stub!\n",
+            iface, debug_d2d_point_2f(&point), transform, tolerance, contains);
+
+    return E_NOTIMPL;
+}
+
+static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_CompareWithGeometry(ID2D1RoundedRectangleGeometry *iface,
+        ID2D1Geometry *geometry, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_GEOMETRY_RELATION *relation)
+{
+    FIXME("iface %p, geometry %p, transform %p, tolerance %.8e, relation %p stub!\n",
+            iface, geometry, transform, tolerance, relation);
+
+    return E_NOTIMPL;
+}
+
+static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_Simplify(ID2D1RoundedRectangleGeometry *iface,
+        D2D1_GEOMETRY_SIMPLIFICATION_OPTION option, const D2D1_MATRIX_3X2_F *transform, float tolerance,
+        ID2D1SimplifiedGeometrySink *sink)
+{
+    FIXME("iface %p, option %#x, transform %p, tolerance %.8e, sink %p stub!\n",
+            iface, option, transform, tolerance, sink);
+
+    return E_NOTIMPL;
+}
+
+static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_Tessellate(ID2D1RoundedRectangleGeometry *iface,
+        const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1TessellationSink *sink)
+{
+    FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
+
+    return E_NOTIMPL;
+}
+
+static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_CombineWithGeometry(ID2D1RoundedRectangleGeometry *iface,
+        ID2D1Geometry *geometry, D2D1_COMBINE_MODE combine_mode, const D2D1_MATRIX_3X2_F *transform,
+        float tolerance, ID2D1SimplifiedGeometrySink *sink)
+{
+    FIXME("iface %p, geometry %p, combine_mode %#x, transform %p, tolerance %.8e, sink %p stub!\n",
+            iface, geometry, combine_mode, transform, tolerance, sink);
+
+    return E_NOTIMPL;
+}
+
+static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_Outline(ID2D1RoundedRectangleGeometry *iface,
+        const D2D1_MATRIX_3X2_F *transform, float tolerance, ID2D1SimplifiedGeometrySink *sink)
+{
+    FIXME("iface %p, transform %p, tolerance %.8e, sink %p stub!\n", iface, transform, tolerance, sink);
+
+    return E_NOTIMPL;
+}
+
+static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_ComputeArea(ID2D1RoundedRectangleGeometry *iface,
+        const D2D1_MATRIX_3X2_F *transform, float tolerance, float *area)
+{
+    FIXME("iface %p, transform %p, tolerance %.8e, area %p stub!\n", iface, transform, tolerance, area);
+
+    return E_NOTIMPL;
+}
+
+static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_ComputeLength(ID2D1RoundedRectangleGeometry *iface,
+        const D2D1_MATRIX_3X2_F *transform, float tolerance, float *length)
+{
+    FIXME("iface %p, transform %p, tolerance %.8e, length %p stub!\n", iface, transform, tolerance, length);
+
+    return E_NOTIMPL;
+}
+
+static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_ComputePointAtLength(ID2D1RoundedRectangleGeometry *iface,
+        float length, const D2D1_MATRIX_3X2_F *transform, float tolerance, D2D1_POINT_2F *point,
+        D2D1_POINT_2F *tangent)
+{
+    FIXME("iface %p, length %.8e, transform %p, tolerance %.8e, point %p, tangent %p stub!\n",
+            iface, length, transform, tolerance, point, tangent);
+
+    return E_NOTIMPL;
+}
+
+static HRESULT STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_Widen(ID2D1RoundedRectangleGeometry *iface, float stroke_width,
+        ID2D1StrokeStyle *stroke_style, const D2D1_MATRIX_3X2_F *transform, float tolerance,
+        ID2D1SimplifiedGeometrySink *sink)
+{
+    FIXME("iface %p, stroke_width %.8e, stroke_style %p, transform %p, tolerance %.8e, sink %p stub!\n",
+            iface, stroke_width, stroke_style, transform, tolerance, sink);
+
+    return E_NOTIMPL;
+}
+
+static void STDMETHODCALLTYPE d2d_rounded_rectangle_geometry_GetRoundedRect(ID2D1RoundedRectangleGeometry *iface, D2D1_ROUNDED_RECT *rounded_rect)
+{
+    struct d2d_geometry *geometry = impl_from_ID2D1RoundedRectangleGeometry(iface);
+
+    TRACE("iface %p, rounded_rect %p.\n", iface, rounded_rect);
+
+    *rounded_rect = geometry->u.rounded_rectangle.rounded_rect;
+}
+
+static const struct ID2D1RoundedRectangleGeometryVtbl d2d_rounded_rectangle_geometry_vtbl =
+{
+    d2d_rounded_rectangle_geometry_QueryInterface,
+    d2d_rounded_rectangle_geometry_AddRef,
+    d2d_rounded_rectangle_geometry_Release,
+    d2d_rounded_rectangle_geometry_GetFactory,
+    d2d_rounded_rectangle_geometry_GetBounds,
+    d2d_rounded_rectangle_geometry_GetWidenedBounds,
+    d2d_rounded_rectangle_geometry_StrokeContainsPoint,
+    d2d_rounded_rectangle_geometry_FillContainsPoint,
+    d2d_rounded_rectangle_geometry_CompareWithGeometry,
+    d2d_rounded_rectangle_geometry_Simplify,
+    d2d_rounded_rectangle_geometry_Tessellate,
+    d2d_rounded_rectangle_geometry_CombineWithGeometry,
+    d2d_rounded_rectangle_geometry_Outline,
+    d2d_rounded_rectangle_geometry_ComputeArea,
+    d2d_rounded_rectangle_geometry_ComputeLength,
+    d2d_rounded_rectangle_geometry_ComputePointAtLength,
+    d2d_rounded_rectangle_geometry_Widen,
+    d2d_rounded_rectangle_geometry_GetRoundedRect,
+};
+
+HRESULT d2d_rounded_rectangle_geometry_init(struct d2d_geometry *geometry, ID2D1Factory *factory, const D2D1_ROUNDED_RECT *rounded_rect)
+{
+    struct d2d_face *f;
+    struct d2d_bezier_vertex *bv;
+    D2D1_POINT_2F *v, v1, v2, v3, v4;
+    float l, r, t, b;
+    float rX, rY;
+
+    d2d_geometry_init(geometry, factory, &identity, (ID2D1GeometryVtbl *)&d2d_rounded_rectangle_geometry_vtbl);
+    geometry->u.rounded_rectangle.rounded_rect = *rounded_rect;
+
+    if (!(geometry->fill.vertices = heap_alloc(8 * sizeof(*geometry->fill.vertices))))
+        goto fail;
+    if (!d2d_array_reserve((void **)&geometry->fill.faces,
+            &geometry->fill.faces_size, 6, sizeof(*geometry->fill.faces)))
+        goto fail;
+    if (!(geometry->fill.bezier_vertices = heap_alloc(12 * sizeof(*geometry->fill.bezier_vertices))))
+        goto fail;
+
+    l = min(rounded_rect->rect.left, rounded_rect->rect.right);
+    r = max(rounded_rect->rect.left, rounded_rect->rect.right);
+    t = min(rounded_rect->rect.top, rounded_rect->rect.bottom);
+    b = max(rounded_rect->rect.top, rounded_rect->rect.bottom);
+
+    rX = min(rounded_rect->radiusX, 0.5f*(r-l));
+    rY = min(rounded_rect->radiusY, 0.5f*(b-t));
+
+    d2d_point_set(&v1, r, t);
+    d2d_point_set(&v2, r, b);
+    d2d_point_set(&v3, l, b);
+    d2d_point_set(&v4, l, t);
+
+    v = geometry->fill.vertices;
+    d2d_point_set(&v[0], l+rX, t);
+    d2d_point_set(&v[1], r-rX, t);
+    d2d_point_set(&v[2], r, t+rY);
+    d2d_point_set(&v[3], r, b-rY);
+    d2d_point_set(&v[4], r-rX, b);
+    d2d_point_set(&v[5], l+rX, b);
+    d2d_point_set(&v[6], l, b-rY);
+    d2d_point_set(&v[7], l, t+rY);
+    geometry->fill.vertex_count = 8;
+
+    f = geometry->fill.faces;
+    d2d_face_set(&f[0], 0, 7, 6);
+    d2d_face_set(&f[1], 0, 6, 5);
+    d2d_face_set(&f[2], 0, 5, 4);
+    d2d_face_set(&f[3], 0, 4, 1);
+    d2d_face_set(&f[4], 1, 4, 3);
+    d2d_face_set(&f[5], 1, 3, 2);
+    geometry->fill.face_count = 6;
+
+    bv = geometry->fill.bezier_vertices;
+    d2d_bezier_vertex_set(&bv[0], &v[1], 0.0f, 0.0f, -1.0f);
+    d2d_bezier_vertex_set(&bv[1], &v1, 0.5f, 0.0f, -1.0f);
+    d2d_bezier_vertex_set(&bv[2], &v[2], 1.0f, 1.0f, -1.0f);
+    d2d_bezier_vertex_set(&bv[3], &v[3], 0.0f, 0.0f, -1.0f);
+    d2d_bezier_vertex_set(&bv[4], &v2, 0.5f, 0.0f, -1.0f);
+    d2d_bezier_vertex_set(&bv[5], &v[4], 1.0f, 1.0f, -1.0f);
+    d2d_bezier_vertex_set(&bv[6], &v[5], 0.0f, 0.0f, -1.0f);
+    d2d_bezier_vertex_set(&bv[7], &v3, 0.5f, 0.0f, -1.0f);
+    d2d_bezier_vertex_set(&bv[8], &v[6], 1.0f, 1.0f, -1.0f);
+    d2d_bezier_vertex_set(&bv[9], &v[7], 0.0f, 0.0f, -1.0f);
+    d2d_bezier_vertex_set(&bv[10], &v4, 0.5f, 0.0f, -1.0f);
+    d2d_bezier_vertex_set(&bv[11], &v[0], 1.0f, 1.0f, -1.0f);
+    geometry->fill.bezier_vertex_count = 12;
+
+    if (!d2d_geometry_outline_add_line_segment(geometry, &v[0], &v[1]))
+        goto fail;
+    if (!d2d_geometry_outline_add_bezier_segment(geometry, &v[1], &v1, &v[2]))
+        goto fail;
+    if (!d2d_geometry_outline_add_line_segment(geometry, &v[2], &v[3]))
+        goto fail;
+    if (!d2d_geometry_outline_add_bezier_segment(geometry, &v[3], &v2, &v[4]))
+        goto fail;
+    if (!d2d_geometry_outline_add_line_segment(geometry, &v[4], &v[5]))
+        goto fail;
+    if (!d2d_geometry_outline_add_bezier_segment(geometry, &v[5], &v3, &v[6]))
+        goto fail;
+    if (!d2d_geometry_outline_add_line_segment(geometry, &v[6], &v[7]))
+        goto fail;
+    if (!d2d_geometry_outline_add_bezier_segment(geometry, &v[7], &v4, &v[0]))
+        goto fail;
+
+    return S_OK;
+
+fail:
+    d2d_geometry_cleanup(geometry);
+    return E_OUTOFMEMORY;
+}
+
 static inline struct d2d_geometry *impl_from_ID2D1TransformedGeometry(ID2D1TransformedGeometry *iface)
 {
     return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);
@@ -4385,6 +4686,7 @@ struct d2d_geometry *unsafe_impl_from_ID2D1Geometry(ID2D1Geometry *iface)
         return NULL;
     assert(iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_path_geometry_vtbl
             || iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_rectangle_geometry_vtbl
+            || iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_rounded_rectangle_geometry_vtbl
             || iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_transformed_geometry_vtbl
             || iface->lpVtbl == (const ID2D1GeometryVtbl *)&d2d_geometry_group_vtbl);
     return CONTAINING_RECORD(iface, struct d2d_geometry, ID2D1Geometry_iface);
diff --git a/dlls/d2d1/tests/d2d1.c b/dlls/d2d1/tests/d2d1.c
index 801334f2ad..42cf524deb 100644
--- a/dlls/d2d1/tests/d2d1.c
+++ b/dlls/d2d1/tests/d2d1.c
@@ -3815,7 +3815,6 @@ static void test_rounded_rectangle_geometry(void)
 
     set_rounded_rect(&rect, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
     hr = ID2D1Factory_CreateRoundedRectangleGeometry(factory, &rect, &geometry);
-todo_wine
     ok(SUCCEEDED(hr), "Failed to create geometry, hr %#x.\n", hr);
     if (FAILED(hr))
     {
-- 
2.25.0




More information about the wine-devel mailing list