1
2/*
3 * Copyright 2017 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#ifndef SkShadowUtils_DEFINED
9#define SkShadowUtils_DEFINED
10
11#include "include/core/SkColor.h"
12#include "include/core/SkPoint3.h"
13#include "include/core/SkScalar.h"
14#include "include/private/SkShadowFlags.h"
15
16class SkCanvas;
17class SkMatrix;
18class SkPath;
19class SkResourceCache;
20
21class SK_API SkShadowUtils {
22public:
23    /**
24     * Draw an offset spot shadow and outlining ambient shadow for the given path using a disc
25     * light. The shadow may be cached, depending on the path type and canvas matrix. If the
26     * matrix is perspective or the path is volatile, it will not be cached.
27     *
28     * @param canvas  The canvas on which to draw the shadows.
29     * @param path  The occluder used to generate the shadows.
30     * @param zPlaneParams  Values for the plane function which returns the Z offset of the
31     *  occluder from the canvas based on local x and y values (the current matrix is not applied).
32     * @param lightPos  Generally, the 3D position of the light relative to the canvas plane.
33     *                  If kDirectionalLight_ShadowFlag is set, this specifies a vector pointing
34     *                  towards the light.
35     * @param lightRadius  Generally, the radius of the disc light.
36     *                     If DirectionalLight_ShadowFlag is set, this specifies the amount of
37     *                     blur when the occluder is at Z offset == 1. The blur will grow linearly
38     *                     as the Z value increases.
39     * @param ambientColor  The color of the ambient shadow.
40     * @param spotColor  The color of the spot shadow.
41     * @param flags  Options controlling opaque occluder optimizations, shadow appearance,
42     *               and light position. See SkShadowFlags.
43     * @param isLimitElevation  Indicates whether to limit the shadow range of the elevation mode.
44     *                          The default value is FALSE.
45     */
46    static void DrawShadow(SkCanvas* canvas, const SkPath& path, const SkPoint3& zPlaneParams,
47                           const SkPoint3& lightPos, SkScalar lightRadius,
48                           SkColor ambientColor, SkColor spotColor,
49                           uint32_t flags = SkShadowFlags::kNone_ShadowFlag);
50
51    static void DrawShadowStyle(SkCanvas* canvas, const SkPath& path, const SkPoint3& zPlaneParams,
52                                const SkPoint3& lightPos, SkScalar lightRadius,
53                                SkColor ambientColor, SkColor spotColor,
54                                uint32_t flags = SkShadowFlags::kNone_ShadowFlag,
55                                bool isLimitElevation = false);
56
57    /**
58     * Generate bounding box for shadows relative to path. Includes both the ambient and spot
59     * shadow bounds.
60     *
61     * @param ctm  Current transformation matrix to device space.
62     * @param path  The occluder used to generate the shadows.
63     * @param zPlaneParams  Values for the plane function which returns the Z offset of the
64     *  occluder from the canvas based on local x and y values (the current matrix is not applied).
65     * @param lightPos  Generally, the 3D position of the light relative to the canvas plane.
66     *                  If kDirectionalLight_ShadowFlag is set, this specifies a vector pointing
67     *                  towards the light.
68     * @param lightRadius  Generally, the radius of the disc light.
69     *                     If DirectionalLight_ShadowFlag is set, this specifies the amount of
70     *                     blur when the occluder is at Z offset == 1. The blur will grow linearly
71     *                     as the Z value increases.
72     * @param flags  Options controlling opaque occluder optimizations, shadow appearance,
73     *               and light position. See SkShadowFlags.
74     * @param bounds Return value for shadow bounding box.
75     * @return Returns true if successful, false otherwise.
76     */
77    static bool GetLocalBounds(const SkMatrix& ctm, const SkPath& path,
78                               const SkPoint3& zPlaneParams, const SkPoint3& lightPos,
79                               SkScalar lightRadius, uint32_t flags, SkRect* bounds);
80
81    /**
82     * Helper routine to compute color values for one-pass tonal alpha.
83     *
84     * @param inAmbientColor  Original ambient color
85     * @param inSpotColor  Original spot color
86     * @param outAmbientColor  Modified ambient color
87     * @param outSpotColor  Modified spot color
88     */
89    static void ComputeTonalColors(SkColor inAmbientColor, SkColor inSpotColor,
90                                   SkColor* outAmbientColor, SkColor* outSpotColor);
91};
92
93#endif
94