stroke.c 7.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
/*
 * Copyright 2014 Henri Verbeet for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "config.h"
#include "wine/port.h"

#include "d2d1_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(d2d);

static inline struct d2d_stroke_style *impl_from_ID2D1StrokeStyle(ID2D1StrokeStyle *iface)
{
    return CONTAINING_RECORD(iface, struct d2d_stroke_style, ID2D1StrokeStyle_iface);
}

static HRESULT STDMETHODCALLTYPE d2d_stroke_style_QueryInterface(ID2D1StrokeStyle *iface, REFIID iid, void **out)
{
    TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);

    if (IsEqualGUID(iid, &IID_ID2D1StrokeStyle)
            || IsEqualGUID(iid, &IID_ID2D1Resource)
            || IsEqualGUID(iid, &IID_IUnknown))
    {
        ID2D1StrokeStyle_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_stroke_style_AddRef(ID2D1StrokeStyle *iface)
{
    struct d2d_stroke_style *style = impl_from_ID2D1StrokeStyle(iface);
    ULONG refcount = InterlockedIncrement(&style->refcount);

    TRACE("%p increasing refcount to %u.\n", iface, refcount);

    return refcount;
}

static ULONG STDMETHODCALLTYPE d2d_stroke_style_Release(ID2D1StrokeStyle *iface)
{
    struct d2d_stroke_style *style = impl_from_ID2D1StrokeStyle(iface);
    ULONG refcount = InterlockedDecrement(&style->refcount);

    TRACE("%p decreasing refcount to %u.\n", iface, refcount);

    if (!refcount)
68 69
    {
        ID2D1Factory_Release(style->factory);
70 71
        if (style->desc.dashStyle == D2D1_DASH_STYLE_CUSTOM)
            HeapFree(GetProcessHeap(), 0, style->dashes);
72
        HeapFree(GetProcessHeap(), 0, style);
73
    }
74 75 76 77 78 79

    return refcount;
}

static void STDMETHODCALLTYPE d2d_stroke_style_GetFactory(ID2D1StrokeStyle *iface, ID2D1Factory **factory)
{
80 81 82
    struct d2d_stroke_style *style = impl_from_ID2D1StrokeStyle(iface);

    TRACE("iface %p, factory %p.\n", iface, factory);
83

84
    ID2D1Factory_AddRef(*factory = style->factory);
85 86 87 88
}

static D2D1_CAP_STYLE STDMETHODCALLTYPE d2d_stroke_style_GetStartCap(ID2D1StrokeStyle *iface)
{
89 90 91
    struct d2d_stroke_style *style = impl_from_ID2D1StrokeStyle(iface);

    TRACE("iface %p.\n", iface);
92

93
    return style->desc.startCap;
94 95 96 97
}

static D2D1_CAP_STYLE STDMETHODCALLTYPE d2d_stroke_style_GetEndCap(ID2D1StrokeStyle *iface)
{
98 99 100
    struct d2d_stroke_style *style = impl_from_ID2D1StrokeStyle(iface);

    TRACE("iface %p.\n", iface);
101

102
    return style->desc.endCap;
103 104 105 106
}

static D2D1_CAP_STYLE STDMETHODCALLTYPE d2d_stroke_style_GetDashCap(ID2D1StrokeStyle *iface)
{
107 108 109
    struct d2d_stroke_style *style = impl_from_ID2D1StrokeStyle(iface);

    TRACE("iface %p.\n", iface);
110

111
    return style->desc.dashCap;
112 113 114 115
}

static float STDMETHODCALLTYPE d2d_stroke_style_GetMiterLimit(ID2D1StrokeStyle *iface)
{
116
    struct d2d_stroke_style *style = impl_from_ID2D1StrokeStyle(iface);
117

118 119 120
    TRACE("iface %p.\n", iface);

    return style->desc.miterLimit;
121 122 123 124
}

static D2D1_LINE_JOIN STDMETHODCALLTYPE d2d_stroke_style_GetLineJoin(ID2D1StrokeStyle *iface)
{
125
    struct d2d_stroke_style *style = impl_from_ID2D1StrokeStyle(iface);
126

127 128 129
    TRACE("iface %p.\n", iface);

    return style->desc.lineJoin;
130 131 132 133
}

static float STDMETHODCALLTYPE d2d_stroke_style_GetDashOffset(ID2D1StrokeStyle *iface)
{
134 135 136
    struct d2d_stroke_style *style = impl_from_ID2D1StrokeStyle(iface);

    TRACE("iface %p.\n", iface);
137

138
    return style->desc.dashOffset;
139 140 141 142
}

static D2D1_DASH_STYLE STDMETHODCALLTYPE d2d_stroke_style_GetDashStyle(ID2D1StrokeStyle *iface)
{
143 144 145
    struct d2d_stroke_style *style = impl_from_ID2D1StrokeStyle(iface);

    TRACE("iface %p.\n", iface);
146

147
    return style->desc.dashStyle;
148 149 150 151
}

static UINT32 STDMETHODCALLTYPE d2d_stroke_style_GetDashesCount(ID2D1StrokeStyle *iface)
{
152 153 154
    struct d2d_stroke_style *style = impl_from_ID2D1StrokeStyle(iface);

    TRACE("iface %p.\n", iface);
155

156
    return style->dash_count;
157 158
}

159
static void STDMETHODCALLTYPE d2d_stroke_style_GetDashes(ID2D1StrokeStyle *iface, float *dashes, UINT32 dash_count)
160
{
161 162 163 164 165 166 167
    struct d2d_stroke_style *style = impl_from_ID2D1StrokeStyle(iface);

    TRACE("iface %p, dashes %p, count %u.\n", iface, dashes, dash_count);

    memcpy(dashes, style->dashes, min(style->dash_count, dash_count) * sizeof(*dashes));
    if (dash_count > style->dash_count)
        memset(dashes + style->dash_count, 0, (dash_count - style->dash_count) * sizeof(*dashes));
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
}

static const struct ID2D1StrokeStyleVtbl d2d_stroke_style_vtbl =
{
    d2d_stroke_style_QueryInterface,
    d2d_stroke_style_AddRef,
    d2d_stroke_style_Release,
    d2d_stroke_style_GetFactory,
    d2d_stroke_style_GetStartCap,
    d2d_stroke_style_GetEndCap,
    d2d_stroke_style_GetDashCap,
    d2d_stroke_style_GetMiterLimit,
    d2d_stroke_style_GetLineJoin,
    d2d_stroke_style_GetDashOffset,
    d2d_stroke_style_GetDashStyle,
    d2d_stroke_style_GetDashesCount,
    d2d_stroke_style_GetDashes,
};

187
HRESULT d2d_stroke_style_init(struct d2d_stroke_style *style, ID2D1Factory *factory,
188 189
        const D2D1_STROKE_STYLE_PROPERTIES *desc, const float *dashes, UINT32 dash_count)
{
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    static const struct
    {
        UINT32 dash_count;
        float dashes[6];
    }
    builtin_dash_styles[] =
    {
        /* D2D1_DASH_STYLE_SOLID */        { 0 },
        /* D2D1_DASH_STYLE_DASH */         { 2, {2.0f, 2.0f}},
        /* D2D1_DASH_STYLE_DOT */          { 2, {0.0f, 2.0f}},
        /* D2D1_DASH_STYLE_DASH_DOT */     { 4, {2.0f, 2.0f, 0.0f, 2.0f}},
        /* D2D1_DASH_STYLE_DASH_DOT_DOT */ { 6, {2.0f, 2.0f, 0.0f, 2.0f, 0.0f, 2.0f}},
    };

    if (desc->dashStyle > D2D1_DASH_STYLE_CUSTOM)
        return E_INVALIDARG;
206 207 208

    style->ID2D1StrokeStyle_iface.lpVtbl = &d2d_stroke_style_vtbl;
    style->refcount = 1;
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

    if (desc->dashStyle == D2D1_DASH_STYLE_CUSTOM)
    {
        if (!dashes || !dash_count)
            return E_INVALIDARG;

        if (!(style->dashes = HeapAlloc(GetProcessHeap(), 0, dash_count * sizeof(*style->dashes))))
            return E_OUTOFMEMORY;
        memcpy(style->dashes, dashes, dash_count * sizeof(*style->dashes));
        style->dash_count = dash_count;
    }
    else
    {
        if (dashes)
            return E_INVALIDARG;

        style->dashes = (float *)builtin_dash_styles[desc->dashStyle].dashes;
        style->dash_count = builtin_dash_styles[desc->dashStyle].dash_count;
    }

229
    ID2D1Factory_AddRef(style->factory = factory);
230
    style->desc = *desc;
231 232

    return S_OK;
233
}