aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/effects/shadows/SkSpotShadowMaskFilter.cpp
blob: 0716d9b8701f1519df809639366dc58d4c8dc62d (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkSpotShadowMaskFilter.h"
#include "SkReadBuffer.h"
#include "SkStringUtils.h"
#include "SkWriteBuffer.h"

#if SK_SUPPORT_GPU
#include "GrContext.h"
#include "GrRenderTargetContext.h"
#include "GrFragmentProcessor.h"
#include "GrStyle.h"
#include "GrTexture.h"
#include "GrTextureProxy.h"
#include "SkStrokeRec.h"
#endif

class SkSpotShadowMaskFilterImpl : public SkMaskFilter {
public:
    SkSpotShadowMaskFilterImpl(SkScalar occluderHeight, const SkPoint3& lightPos,
                               SkScalar lightRadius, SkScalar spotAlpha, uint32_t flags);

    // overrides from SkMaskFilter
    SkMask::Format getFormat() const override;
    bool filterMask(SkMask* dst, const SkMask& src, const SkMatrix&,
                    SkIPoint* margin) const override;

#if SK_SUPPORT_GPU
    bool canFilterMaskGPU(const SkRRect& devRRect,
                          const SkIRect& clipBounds,
                          const SkMatrix& ctm,
                          SkRect* maskRect) const override;
    bool directFilterMaskGPU(GrContext*,
                             GrRenderTargetContext* drawContext,
                             GrPaint&&,
                             const GrClip&,
                             const SkMatrix& viewMatrix,
                             const SkStrokeRec& strokeRec,
                             const SkPath& path) const override;
    bool directFilterRRectMaskGPU(GrContext*,
                                  GrRenderTargetContext* drawContext,
                                  GrPaint&&,
                                  const GrClip&,
                                  const SkMatrix& viewMatrix,
                                  const SkStrokeRec& strokeRec,
                                  const SkRRect& rrect,
                                  const SkRRect& devRRect) const override;
    sk_sp<GrTextureProxy> filterMaskGPU(GrContext*,
                                        sk_sp<GrTextureProxy> srcProxy,
                                        const SkMatrix& ctm,
                                        const SkIRect& maskRect) const override;
#endif

    void computeFastBounds(const SkRect&, SkRect*) const override;

    SK_TO_STRING_OVERRIDE()
    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSpotShadowMaskFilterImpl)

private:
    SkScalar fOccluderHeight;
    SkPoint3 fLightPos;
    SkScalar fLightRadius;
    SkScalar fSpotAlpha;
    uint32_t fFlags;

    SkSpotShadowMaskFilterImpl(SkReadBuffer&);
    void flatten(SkWriteBuffer&) const override;

    friend class SkSpotShadowMaskFilter;

    typedef SkMaskFilter INHERITED;
};

sk_sp<SkMaskFilter> SkSpotShadowMaskFilter::Make(SkScalar occluderHeight, const SkPoint3& lightPos,
                                                 SkScalar lightRadius, SkScalar spotAlpha,
                                                 uint32_t flags) {
    // add some param checks here for early exit

    return sk_sp<SkMaskFilter>(new SkSpotShadowMaskFilterImpl(occluderHeight, lightPos,
                                                              lightRadius, spotAlpha, flags));
}

///////////////////////////////////////////////////////////////////////////////////////////////////

SkSpotShadowMaskFilterImpl::SkSpotShadowMaskFilterImpl(SkScalar occluderHeight,
                                                       const SkPoint3& lightPos,
                                                       SkScalar lightRadius,
                                                       SkScalar spotAlpha,
                                                       uint32_t flags)
    : fOccluderHeight(occluderHeight)
    , fLightPos(lightPos)
    , fLightRadius(lightRadius)
    , fSpotAlpha(spotAlpha)
    , fFlags(flags) {
    SkASSERT(fOccluderHeight > 0);
    SkASSERT(fLightPos.z() > 0 && fLightPos.z() > fOccluderHeight);
    SkASSERT(fLightRadius > 0);
    SkASSERT(fSpotAlpha >= 0);
}

SkMask::Format SkSpotShadowMaskFilterImpl::getFormat() const {
    return SkMask::kA8_Format;
}

bool SkSpotShadowMaskFilterImpl::filterMask(SkMask* dst, const SkMask& src,
                                            const SkMatrix& matrix,
                                            SkIPoint* margin) const {
    // TODO something
    return false;
}

void SkSpotShadowMaskFilterImpl::computeFastBounds(const SkRect& src, SkRect* dst) const {
    // TODO compute based on ambient + spot data
    dst->set(src.fLeft, src.fTop, src.fRight, src.fBottom);
}

sk_sp<SkFlattenable> SkSpotShadowMaskFilterImpl::CreateProc(SkReadBuffer& buffer) {
    const SkScalar occluderHeight = buffer.readScalar();
    const SkScalar lightX = buffer.readScalar();
    const SkScalar lightY = buffer.readScalar();
    const SkScalar lightZ = buffer.readScalar();
    const SkPoint3 lightPos = SkPoint3::Make(lightX, lightY, lightZ);
    const SkScalar lightRadius = buffer.readScalar();
    const SkScalar spotAlpha = buffer.readScalar();
    const uint32_t flags = buffer.readUInt();

    return SkSpotShadowMaskFilter::Make(occluderHeight, lightPos, lightRadius,
                                        spotAlpha, flags);
}

void SkSpotShadowMaskFilterImpl::flatten(SkWriteBuffer& buffer) const {
    buffer.writeScalar(fOccluderHeight);
    buffer.writeScalar(fLightPos.fX);
    buffer.writeScalar(fLightPos.fY);
    buffer.writeScalar(fLightPos.fZ);
    buffer.writeScalar(fLightRadius);
    buffer.writeScalar(fSpotAlpha);
    buffer.writeUInt(fFlags);
}

#if SK_SUPPORT_GPU

///////////////////////////////////////////////////////////////////////////////////////////////////

bool SkSpotShadowMaskFilterImpl::canFilterMaskGPU(const SkRRect& devRRect,
                                                  const SkIRect& clipBounds,
                                                  const SkMatrix& ctm,
                                                  SkRect* maskRect) const {
    // TODO
    *maskRect = devRRect.rect();
    return true;
}

bool SkSpotShadowMaskFilterImpl::directFilterMaskGPU(GrContext* context,
                                                     GrRenderTargetContext* rtContext,
                                                     GrPaint&& paint,
                                                     const GrClip& clip,
                                                     const SkMatrix& viewMatrix,
                                                     const SkStrokeRec& strokeRec,
                                                     const SkPath& path) const {
    SkASSERT(rtContext);
    // TODO: this will not handle local coordinates properly

    SkRect rect;
    SkRRect rrect;
    if (path.isOval(&rect) && SkScalarNearlyEqual(rect.width(),
                                                  rect.height())) {
        rrect = SkRRect::MakeOval(rect);
        return this->directFilterRRectMaskGPU(context, rtContext, std::move(paint), clip,
                                              SkMatrix::I(), strokeRec, rrect, rrect);
    } else if (path.isRect(&rect)) {
        rrect = SkRRect::MakeRect(rect);
        return this->directFilterRRectMaskGPU(context, rtContext, std::move(paint), clip,
                                              SkMatrix::I(), strokeRec, rrect, rrect);
    } else if (path.isRRect(&rrect) && rrect.isSimpleCircular()) {
        return this->directFilterRRectMaskGPU(context, rtContext, std::move(paint), clip,
                                              SkMatrix::I(), strokeRec, rrect, rrect);
    }

    return false;
}

bool SkSpotShadowMaskFilterImpl::directFilterRRectMaskGPU(GrContext*,
                                                          GrRenderTargetContext* rtContext,
                                                          GrPaint&& paint,
                                                          const GrClip& clip,
                                                          const SkMatrix& viewMatrix,
                                                          const SkStrokeRec& strokeRec,
                                                          const SkRRect& rrect,
                                                          const SkRRect& devRRect) const {
    // Fast path only supports filled rrects for now.
    // TODO: fill and stroke as well.
    if (SkStrokeRec::kFill_Style != strokeRec.getStyle()) {
        return false;
    }
    // These should have been checked by the caller.
    // Fast path only supports simple rrects with circular corners.
    SkASSERT(devRRect.isRect() || devRRect.isCircle() ||
        (devRRect.isSimple() && devRRect.allCornersCircular()));
    // Fast path only supports uniform scale.
    SkASSERT(viewMatrix.isSimilarity());
    // Assume we have positive alpha
    SkASSERT(fSpotAlpha > 0);

    // 1/scale
    SkScalar devToSrcScale = viewMatrix.isScaleTranslate() ?
        SkScalarInvert(viewMatrix[SkMatrix::kMScaleX]) :
        sk_float_rsqrt(viewMatrix[SkMatrix::kMScaleX] * viewMatrix[SkMatrix::kMScaleX] +
                       viewMatrix[SkMatrix::kMSkewX] * viewMatrix[SkMatrix::kMSkewX]);

    float zRatio = SkTPin(fOccluderHeight / (fLightPos.fZ - fOccluderHeight), 0.0f, 0.95f);

    SkScalar devSpaceSpotBlur = 2.0f * fLightRadius * zRatio;
    // handle scale of radius and pad due to CTM
    const SkScalar srcSpaceSpotBlur = devSpaceSpotBlur * devToSrcScale;

    // Compute the scale and translation for the spot shadow.
    const SkScalar spotScale = fLightPos.fZ / (fLightPos.fZ - fOccluderHeight);
    SkPoint spotOffset = SkPoint::Make(zRatio*(-fLightPos.fX), zRatio*(-fLightPos.fY));
    // Adjust translate for the effect of the scale.
    spotOffset.fX += spotScale*viewMatrix[SkMatrix::kMTransX];
    spotOffset.fY += spotScale*viewMatrix[SkMatrix::kMTransY];
    // This offset is in dev space, need to transform it into source space.
    SkMatrix ctmInverse;
    if (!viewMatrix.invert(&ctmInverse)) {
        // Since the matrix is a similarity, this should never happen, but just in case...
        SkDebugf("Matrix is degenerate. Will not render spot shadow!\n");
        return true;
    }
    ctmInverse.mapPoints(&spotOffset, 1);

    // Compute the transformed shadow rrect
    SkRRect spotShadowRRect;
    SkMatrix shadowTransform;
    shadowTransform.setScaleTranslate(spotScale, spotScale, spotOffset.fX, spotOffset.fY);
    rrect.transform(shadowTransform, &spotShadowRRect);
    SkScalar spotRadius = spotShadowRRect.getSimpleRadii().fX;

    // Compute the insetWidth
    SkScalar blurOutset = 0.5f*srcSpaceSpotBlur;
    SkScalar insetWidth = blurOutset;
    if (fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag) {
        // If transparent, just do a fill
        insetWidth += spotShadowRRect.width();
    } else {
        // For shadows, instead of using a stroke we specify an inset from the penumbra
        // border. We want to extend this inset area so that it meets up with the caster
        // geometry. The inset geometry will by default already be inset by the blur width.
        //
        // We compare the min and max corners inset by the radius between the original
        // rrect and the shadow rrect. The distance between the two plus the difference
        // between the scaled radius and the original radius gives the distance from the
        // transformed shadow shape to the original shape in that corner. The max
        // of these gives the maximum distance we need to cover.
        //
        // Since we are outsetting by 1/2 the blur distance, we just add the maxOffset to
        // that to get the full insetWidth.
        SkScalar maxOffset;
        if (rrect.isRect()) {
            // Manhattan distance works better for rects
            maxOffset = SkTMax(SkTMax(SkTAbs(spotShadowRRect.rect().fLeft -
                                             rrect.rect().fLeft),
                                      SkTAbs(spotShadowRRect.rect().fTop -
                                             rrect.rect().fTop)),
                               SkTMax(SkTAbs(spotShadowRRect.rect().fRight -
                                             rrect.rect().fRight),
                                      SkTAbs(spotShadowRRect.rect().fBottom -
                                             rrect.rect().fBottom)));
        } else {
            SkScalar dr = spotRadius - rrect.getSimpleRadii().fX;
            SkPoint upperLeftOffset = SkPoint::Make(spotShadowRRect.rect().fLeft -
                                                    rrect.rect().fLeft + dr,
                                                    spotShadowRRect.rect().fTop -
                                                    rrect.rect().fTop + dr);
            SkPoint lowerRightOffset = SkPoint::Make(spotShadowRRect.rect().fRight -
                                                     rrect.rect().fRight - dr,
                                                     spotShadowRRect.rect().fBottom -
                                                     rrect.rect().fBottom - dr);
            maxOffset = SkScalarSqrt(SkTMax(upperLeftOffset.lengthSqd(),
                                            lowerRightOffset.lengthSqd())) + dr;
        }
        insetWidth += maxOffset;
    }

    // Outset the shadow rrect to the border of the penumbra
    SkRect outsetRect = spotShadowRRect.rect().makeOutset(blurOutset, blurOutset);
    if (spotShadowRRect.isOval()) {
        spotShadowRRect = SkRRect::MakeOval(outsetRect);
    } else {
        SkScalar outsetRad = spotRadius + blurOutset;
        spotShadowRRect = SkRRect::MakeRectXY(outsetRect, outsetRad, outsetRad);
    }

    GrColor4f color = paint.getColor4f();
    color.fRGBA[3] *= fSpotAlpha;
    paint.setColor4f(color);
    rtContext->drawShadowRRect(clip, std::move(paint), viewMatrix, spotShadowRRect,
                               devSpaceSpotBlur, insetWidth);

    return true;
}

sk_sp<GrTextureProxy> SkSpotShadowMaskFilterImpl::filterMaskGPU(GrContext*,
                                                                sk_sp<GrTextureProxy> srcProxy,
                                                                const SkMatrix& ctm,
                                                                const SkIRect& maskRect) const {
    // This filter is generative and doesn't operate on pre-existing masks
    return nullptr;
}

#endif

#ifndef SK_IGNORE_TO_STRING
void SkSpotShadowMaskFilterImpl::toString(SkString* str) const {
    str->append("SkSpotShadowMaskFilterImpl: (");

    str->append("occluderHeight: ");
    str->appendScalar(fOccluderHeight);
    str->append(" ");

    str->append("lightPos: (");
    str->appendScalar(fLightPos.fX);
    str->append(", ");
    str->appendScalar(fLightPos.fY);
    str->append(", ");
    str->appendScalar(fLightPos.fZ);
    str->append(") ");

    str->append("lightRadius: ");
    str->appendScalar(fLightRadius);
    str->append(" ");

    str->append("spotAlpha: ");
    str->appendScalar(fSpotAlpha);
    str->append(" ");

    str->append("flags: (");
    if (fFlags) {
        bool needSeparator = false;
        SkAddFlagToString(str,
                          SkToBool(fFlags & SkShadowFlags::kTransparentOccluder_ShadowFlag),
                          "TransparentOccluder", &needSeparator);
    } else {
        str->append("None");
    }
    str->append("))");
}
#endif

SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkSpotShadowMaskFilter)
SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpotShadowMaskFilterImpl)
SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END