143475ad9dc43c4cbe13d924d8caff8916dcbbc06Jim Van Verth
243475ad9dc43c4cbe13d924d8caff8916dcbbc06Jim Van Verth/*
343475ad9dc43c4cbe13d924d8caff8916dcbbc06Jim Van Verth * Copyright 2017 Google Inc.
443475ad9dc43c4cbe13d924d8caff8916dcbbc06Jim Van Verth *
543475ad9dc43c4cbe13d924d8caff8916dcbbc06Jim Van Verth * Use of this source code is governed by a BSD-style license that can be
643475ad9dc43c4cbe13d924d8caff8916dcbbc06Jim Van Verth * found in the LICENSE file.
743475ad9dc43c4cbe13d924d8caff8916dcbbc06Jim Van Verth */
843475ad9dc43c4cbe13d924d8caff8916dcbbc06Jim Van Verth#ifndef SkShadowUtils_DEFINED
943475ad9dc43c4cbe13d924d8caff8916dcbbc06Jim Van Verth#define SkShadowUtils_DEFINED
1043475ad9dc43c4cbe13d924d8caff8916dcbbc06Jim Van Verth
1143475ad9dc43c4cbe13d924d8caff8916dcbbc06Jim Van Verth#include "SkColor.h"
1243475ad9dc43c4cbe13d924d8caff8916dcbbc06Jim Van Verth#include "SkScalar.h"
132103cf0ff09763aeaa35508734f765aec9b75665Jim Van Verth#include "../private/SkShadowFlags.h"
14b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth#include <functional>
1543475ad9dc43c4cbe13d924d8caff8916dcbbc06Jim Van Verth
1643475ad9dc43c4cbe13d924d8caff8916dcbbc06Jim Van Verthclass SkCanvas;
1743475ad9dc43c4cbe13d924d8caff8916dcbbc06Jim Van Verthclass SkPath;
18804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomonclass SkResourceCache;
1943475ad9dc43c4cbe13d924d8caff8916dcbbc06Jim Van Verth
2043475ad9dc43c4cbe13d924d8caff8916dcbbc06Jim Van Verthclass SkShadowUtils {
2143475ad9dc43c4cbe13d924d8caff8916dcbbc06Jim Van Verthpublic:
22804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon    /**
23804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon     * Draw an offset spot shadow and outlining ambient shadow for the given path using a disc
24804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon     * light.
25804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon     *
26804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon     * @param canvas  The canvas on which to draw the shadows.
27804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon     * @param path  The occluder used to generate the shadows.
28804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon     * @param occluderHeight  The vertical offset of the occluder from the canvas. This is
29b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth     *  independent of the canvas's current matrix.
30804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon     * @param lightPos  The 3D position of the light relative to the canvas plane. This is
31804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon     *  independent of the canvas's current matrix.
32804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon     * @param lightRadius  The radius of the disc light.
33804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon     * @param ambientAlpha  The maximum alpha of the ambient shadow.
34804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon     * @param spotAlpha  The maxium alpha of the spot shadow.
35804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon     * @param color  The shadow color.
36804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon     * @param flags  Options controlling opaque occluder optimizations and shadow appearance. See
37804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon     *               SkShadowFlags.
38804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon     * @param cache  Used for testing purposes. Clients should pass nullptr (default).
39804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon     */
40804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon    static void DrawShadow(SkCanvas* canvas, const SkPath& path, SkScalar occluderHeight,
41804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon                           const SkPoint3& lightPos, SkScalar lightRadius, SkScalar ambientAlpha,
42804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon                           SkScalar spotAlpha, SkColor color,
43804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon                           uint32_t flags = SkShadowFlags::kNone_ShadowFlag,
44804e091df9879ad0f83a9b2f2216ed8ea8951489Brian Salomon                           SkResourceCache* cache = nullptr);
45b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth
46b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth   /**
47b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth    * Draw an offset spot shadow and outlining ambient shadow for the given path using a disc
48b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth    * light. Takes a function to vary the z value based on the transformed x and y position.
49b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth    * This shadow will not be cached, as the assumption is that this will be used for animation.
50b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth    *
51b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth    * @param canvas  The canvas on which to draw the shadows.
52b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth    * @param path  The occluder used to generate the shadows.
53b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth    * @param heightFunc  A function which returns the vertical offset of the occluder from the
54b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth    *  canvas based on local x and y values (the current matrix is not applied).
55b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth    * @param lightPos  The 3D position of the light relative to the canvas plane. This is
56b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth    *  independent of the canvas's current matrix.
57b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth    * @param lightRadius  The radius of the disc light.
58b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth    * @param ambientAlpha  The maximum alpha of the ambient shadow.
59b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth    * @param spotAlpha  The maxium alpha of the spot shadow.
60b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth    * @param color  The shadow color.
61b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth    * @param flags  Options controlling opaque occluder optimizations and shadow appearance. See
62b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth    *               SkShadowFlags.
63b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth    */
64b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth    static void DrawUncachedShadow(SkCanvas* canvas, const SkPath& path,
65b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth                                   std::function<SkScalar(SkScalar, SkScalar)> heightFunc,
66b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth                                   const SkPoint3& lightPos, SkScalar lightRadius,
67b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth                                   SkScalar ambientAlpha, SkScalar spotAlpha, SkColor color,
68b436655ad5c40a04b65c1642d0a0e781ce296c96Jim Van Verth                                   uint32_t flags = SkShadowFlags::kNone_ShadowFlag);
6943475ad9dc43c4cbe13d924d8caff8916dcbbc06Jim Van Verth};
7043475ad9dc43c4cbe13d924d8caff8916dcbbc06Jim Van Verth
7143475ad9dc43c4cbe13d924d8caff8916dcbbc06Jim Van Verth#endif
72