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 16 class SkCanvas; 17 class SkMatrix; 18 class SkPath; 19 class SkResourceCache; 20 21 class SK_API SkShadowUtils { 22 public: 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 */ 44 static void DrawShadow(SkCanvas* canvas, const SkPath& path, const SkPoint3& zPlaneParams, 45 const SkPoint3& lightPos, SkScalar lightRadius, 46 SkColor ambientColor, SkColor spotColor, 47 uint32_t flags = SkShadowFlags::kNone_ShadowFlag); 48 49 /** 50 * Generate bounding box for shadows relative to path. Includes both the ambient and spot 51 * shadow bounds. 52 * 53 * @param ctm Current transformation matrix to device space. 54 * @param path The occluder used to generate the shadows. 55 * @param zPlaneParams Values for the plane function which returns the Z offset of the 56 * occluder from the canvas based on local x and y values (the current matrix is not applied). 57 * @param lightPos Generally, the 3D position of the light relative to the canvas plane. 58 * If kDirectionalLight_ShadowFlag is set, this specifies a vector pointing 59 * towards the light. 60 * @param lightRadius Generally, the radius of the disc light. 61 * If DirectionalLight_ShadowFlag is set, this specifies the amount of 62 * blur when the occluder is at Z offset == 1. The blur will grow linearly 63 * as the Z value increases. 64 * @param flags Options controlling opaque occluder optimizations, shadow appearance, 65 * and light position. See SkShadowFlags. 66 * @param bounds Return value for shadow bounding box. 67 * @return Returns true if successful, false otherwise. 68 */ 69 static bool GetLocalBounds(const SkMatrix& ctm, const SkPath& path, 70 const SkPoint3& zPlaneParams, const SkPoint3& lightPos, 71 SkScalar lightRadius, uint32_t flags, SkRect* bounds); 72 73 /** 74 * Helper routine to compute color values for one-pass tonal alpha. 75 * 76 * @param inAmbientColor Original ambient color 77 * @param inSpotColor Original spot color 78 * @param outAmbientColor Modified ambient color 79 * @param outSpotColor Modified spot color 80 */ 81 static void ComputeTonalColors(SkColor inAmbientColor, SkColor inSpotColor, 82 SkColor* outAmbientColor, SkColor* outSpotColor); 83 }; 84 85 #endif 86