Coverage Report

Created: 2021-08-22 09:07

/src/skia/src/core/SkDrawShadowInfo.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2017 Google Inc.
3
 *
4
 * Use of this source code is governed by a BSD-style license that can be
5
 * found in the LICENSE file.
6
 */
7
8
#ifndef SkDrawShadowInfo_DEFINED
9
#define SkDrawShadowInfo_DEFINED
10
11
#include "include/core/SkColor.h"
12
#include "include/core/SkPoint.h"
13
#include "include/core/SkPoint3.h"
14
#include "include/private/SkTPin.h"
15
16
class SkMatrix;
17
class SkPath;
18
struct SkRect;
19
20
struct SkDrawShadowRec {
21
    SkPoint3    fZPlaneParams;
22
    SkPoint3    fLightPos;
23
    SkScalar    fLightRadius;
24
    SkColor     fAmbientColor;
25
    SkColor     fSpotColor;
26
    uint32_t    fFlags;
27
};
28
29
namespace SkDrawShadowMetrics {
30
31
static constexpr auto kAmbientHeightFactor = 1.0f / 128.0f;
32
static constexpr auto kAmbientGeomFactor = 64.0f;
33
// Assuming that we have a light height of 600 for the spot shadow,
34
// the spot values will reach their maximum at a height of approximately 292.3077.
35
// We'll round up to 300 to keep it simple.
36
static constexpr auto kMaxAmbientRadius = 300*kAmbientHeightFactor*kAmbientGeomFactor;
37
38
0
static inline float divide_and_pin(float numer, float denom, float min, float max) {
39
0
    float result = SkTPin(sk_ieee_float_divide(numer, denom), min, max);
40
    // ensure that SkTPin handled non-finites correctly
41
0
    SkASSERT(result >= min && result <= max);
42
0
    return result;
43
0
}
Unexecuted instantiation: SkPictureRecorder.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: SkRecordedDrawable.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: SkRecorder.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: SkShadowUtils.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: SkGlobalInitialization_default.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: Device.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: SurfaceDrawContext.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: SkDrawShadowInfo.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: SkMiniRecorder.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: SkRecord.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: SkRecordDraw.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: SkRecordOpts.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: SkRecords.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: SkBigPicture.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: SkPicture.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: SkPicturePlayback.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: SkPictureRecord.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: SkShadowTessellator.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: SkShadowUtils.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: SurfaceDrawContext.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: SkDrawShadowInfo.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: FuzzCanvas.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: DebugCanvas.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: DebugLayerManager.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: DrawCommand.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
Unexecuted instantiation: JsonWriteBuffer.cpp:SkDrawShadowMetrics::divide_and_pin(float, float, float, float)
44
45
0
inline SkScalar AmbientBlurRadius(SkScalar height) {
46
0
    return std::min(height*kAmbientHeightFactor*kAmbientGeomFactor, kMaxAmbientRadius);
47
0
}
48
49
0
inline SkScalar AmbientRecipAlpha(SkScalar height) {
50
0
    return 1.0f + std::max(height*kAmbientHeightFactor, 0.0f);
51
0
}
52
53
0
inline SkScalar SpotBlurRadius(SkScalar occluderZ, SkScalar lightZ, SkScalar lightRadius) {
54
0
    return lightRadius*divide_and_pin(occluderZ, lightZ - occluderZ, 0.0f, 0.95f);
55
0
}
56
57
inline void GetSpotParams(SkScalar occluderZ, SkScalar lightX, SkScalar lightY, SkScalar lightZ,
58
                          SkScalar lightRadius,
59
0
                          SkScalar* blurRadius, SkScalar* scale, SkVector* translate) {
60
0
    SkScalar zRatio = divide_and_pin(occluderZ, lightZ - occluderZ, 0.0f, 0.95f);
61
0
    *blurRadius = lightRadius*zRatio;
62
0
    *scale = divide_and_pin(lightZ, lightZ - occluderZ, 1.0f, 1.95f);
63
0
    *translate = SkVector::Make(-zRatio * lightX, -zRatio * lightY);
64
0
}
65
66
inline void GetDirectionalParams(SkScalar occluderZ, SkScalar lightX, SkScalar lightY,
67
                                 SkScalar lightZ, SkScalar lightRadius,
68
0
                                 SkScalar* blurRadius, SkScalar* scale, SkVector* translate) {
69
0
    *blurRadius = lightRadius*occluderZ;
70
0
    *scale = 1;
71
    // Max z-ratio is "max expected elevation"/"min allowable z"
72
0
    constexpr SkScalar kMaxZRatio = 64/SK_ScalarNearlyZero;
73
0
    SkScalar zRatio = divide_and_pin(occluderZ, lightZ, 0.0f, kMaxZRatio);
74
0
    *translate = SkVector::Make(-zRatio * lightX, -zRatio * lightY);
75
0
}
76
77
// Create the transformation to apply to a path to get its base shadow outline, given the light
78
// parameters and the path's 3D transformation (given by ctm and zPlaneParams).
79
// Also computes the blur radius to apply the transformed outline.
80
bool GetSpotShadowTransform(const SkPoint3& lightPos, SkScalar lightRadius,
81
                            const SkMatrix& ctm, const SkPoint3& zPlaneParams,
82
                            const SkRect& pathBounds, bool directional,
83
                            SkMatrix* shadowTransform, SkScalar* radius);
84
85
// get bounds prior to the ctm being applied
86
void GetLocalBounds(const SkPath&, const SkDrawShadowRec&, const SkMatrix& ctm, SkRect* bounds);
87
88
}  // namespace SkDrawShadowMetrics
89
90
#endif