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 15 #include <cstdint> 16 17 class SkCanvas; 18 class SkMatrix; 19 class SkPath; 20 struct SkPoint3; 21 struct SkRect; 22 23 enum SkShadowFlags { 24 kNone_ShadowFlag = 0x00, 25 /** The occluding object is not opaque. Knowing that the occluder is opaque allows 26 * us to cull shadow geometry behind it and improve performance. */ 27 kTransparentOccluder_ShadowFlag = 0x01, 28 /** Don't try to use analytic shadows. */ 29 kGeometricOnly_ShadowFlag = 0x02, 30 /** Light position represents a direction, light radius is blur radius at elevation 1 */ 31 kDirectionalLight_ShadowFlag = 0x04, 32 /** Concave paths will only use blur to generate the shadow */ 33 kConcaveBlurOnly_ShadowFlag = 0x08, 34 /** mask for all shadow flags */ 35 kAll_ShadowFlag = 0x0F 36 }; 37 38 class SK_API SkShadowUtils { 39 public: 40 /** 41 * Draw an offset spot shadow and outlining ambient shadow for the given path using a disc 42 * light. The shadow may be cached, depending on the path type and canvas matrix. If the 43 * matrix is perspective or the path is volatile, it will not be cached. 44 * 45 * @param canvas The canvas on which to draw the shadows. 46 * @param path The occluder used to generate the shadows. 47 * @param zPlaneParams Values for the plane function which returns the Z offset of the 48 * occluder from the canvas based on local x and y values (the current matrix is not applied). 49 * @param lightPos Generally, the 3D position of the light relative to the canvas plane. 50 * If kDirectionalLight_ShadowFlag is set, this specifies a vector pointing 51 * towards the light. 52 * @param lightRadius Generally, the radius of the disc light. 53 * If DirectionalLight_ShadowFlag is set, this specifies the amount of 54 * blur when the occluder is at Z offset == 1. The blur will grow linearly 55 * as the Z value increases. 56 * @param ambientColor The color of the ambient shadow. 57 * @param spotColor The color of the spot shadow. 58 * @param flags Options controlling opaque occluder optimizations, shadow appearance, 59 * and light position. See SkShadowFlags. 60 * @param isLimitElevation Indicates whether to limit the shadow range of the elevation mode. 61 * The default value is FALSE. 62 */ 63 static void DrawShadow(SkCanvas* canvas, const SkPath& path, const SkPoint3& zPlaneParams, 64 const SkPoint3& lightPos, SkScalar lightRadius, 65 SkColor ambientColor, SkColor spotColor, 66 uint32_t flags = SkShadowFlags::kNone_ShadowFlag); 67 68 static void DrawShadowStyle(SkCanvas* canvas, const SkPath& path, const SkPoint3& zPlaneParams, 69 const SkPoint3& lightPos, SkScalar lightRadius, 70 SkColor ambientColor, SkColor spotColor, 71 uint32_t flags = SkShadowFlags::kNone_ShadowFlag, 72 bool isLimitElevation = false); 73 74 /** 75 * Generate bounding box for shadows relative to path. Includes both the ambient and spot 76 * shadow bounds. 77 * 78 * @param ctm Current transformation matrix to device space. 79 * @param path The occluder used to generate the shadows. 80 * @param zPlaneParams Values for the plane function which returns the Z offset of the 81 * occluder from the canvas based on local x and y values (the current matrix is not applied). 82 * @param lightPos Generally, the 3D position of the light relative to the canvas plane. 83 * If kDirectionalLight_ShadowFlag is set, this specifies a vector pointing 84 * towards the light. 85 * @param lightRadius Generally, the radius of the disc light. 86 * If DirectionalLight_ShadowFlag is set, this specifies the amount of 87 * blur when the occluder is at Z offset == 1. The blur will grow linearly 88 * as the Z value increases. 89 * @param flags Options controlling opaque occluder optimizations, shadow appearance, 90 * and light position. See SkShadowFlags. 91 * @param bounds Return value for shadow bounding box. 92 * @return Returns true if successful, false otherwise. 93 */ 94 static bool GetLocalBounds(const SkMatrix& ctm, const SkPath& path, 95 const SkPoint3& zPlaneParams, const SkPoint3& lightPos, 96 SkScalar lightRadius, uint32_t flags, SkRect* bounds); 97 98 /** 99 * Helper routine to compute color values for one-pass tonal alpha. 100 * 101 * @param inAmbientColor Original ambient color 102 * @param inSpotColor Original spot color 103 * @param outAmbientColor Modified ambient color 104 * @param outSpotColor Modified spot color 105 */ 106 static void ComputeTonalColors(SkColor inAmbientColor, SkColor inSpotColor, 107 SkColor* outAmbientColor, SkColor* outSpotColor); 108 }; 109 110 #endif 111