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 "SkColor.h"
12#include "SkScalar.h"
13#include "../private/SkShadowFlags.h"
14#include <functional>
15
16class SkCanvas;
17class SkPath;
18class SkResourceCache;
19
20class SkShadowUtils {
21public:
22    /**
23     * Draw an offset spot shadow and outlining ambient shadow for the given path using a disc
24     * light.
25     *
26     * @param canvas  The canvas on which to draw the shadows.
27     * @param path  The occluder used to generate the shadows.
28     * @param occluderHeight  The vertical offset of the occluder from the canvas. This is
29     *  independent of the canvas's current matrix.
30     * @param lightPos  The 3D position of the light relative to the canvas plane. This is
31     *  independent of the canvas's current matrix.
32     * @param lightRadius  The radius of the disc light.
33     * @param ambientAlpha  The maximum alpha of the ambient shadow.
34     * @param spotAlpha  The maxium alpha of the spot shadow.
35     * @param color  The shadow color.
36     * @param flags  Options controlling opaque occluder optimizations and shadow appearance. See
37     *               SkShadowFlags.
38     * @param cache  Used for testing purposes. Clients should pass nullptr (default).
39     */
40    static void DrawShadow(SkCanvas* canvas, const SkPath& path, SkScalar occluderHeight,
41                           const SkPoint3& lightPos, SkScalar lightRadius, SkScalar ambientAlpha,
42                           SkScalar spotAlpha, SkColor color,
43                           uint32_t flags = SkShadowFlags::kNone_ShadowFlag,
44                           SkResourceCache* cache = nullptr);
45
46   /**
47    * Draw an offset spot shadow and outlining ambient shadow for the given path using a disc
48    * light. Takes a function to vary the z value based on the transformed x and y position.
49    * This shadow will not be cached, as the assumption is that this will be used for animation.
50    *
51    * @param canvas  The canvas on which to draw the shadows.
52    * @param path  The occluder used to generate the shadows.
53    * @param heightFunc  A function which returns the vertical offset of the occluder from the
54    *  canvas based on local x and y values (the current matrix is not applied).
55    * @param lightPos  The 3D position of the light relative to the canvas plane. This is
56    *  independent of the canvas's current matrix.
57    * @param lightRadius  The radius of the disc light.
58    * @param ambientAlpha  The maximum alpha of the ambient shadow.
59    * @param spotAlpha  The maxium alpha of the spot shadow.
60    * @param color  The shadow color.
61    * @param flags  Options controlling opaque occluder optimizations and shadow appearance. See
62    *               SkShadowFlags.
63    */
64    static void DrawUncachedShadow(SkCanvas* canvas, const SkPath& path,
65                                   std::function<SkScalar(SkScalar, SkScalar)> heightFunc,
66                                   const SkPoint3& lightPos, SkScalar lightRadius,
67                                   SkScalar ambientAlpha, SkScalar spotAlpha, SkColor color,
68                                   uint32_t flags = SkShadowFlags::kNone_ShadowFlag);
69};
70
71#endif
72