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