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