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 */ 61 static void DrawShadow(SkCanvas* canvas, const SkPath& path, const SkPoint3& zPlaneParams, 62 const SkPoint3& lightPos, SkScalar lightRadius, 63 SkColor ambientColor, SkColor spotColor, 64 uint32_t flags = SkShadowFlags::kNone_ShadowFlag); 65 66 /** 67 * Generate bounding box for shadows relative to path. Includes both the ambient and spot 68 * shadow bounds. 69 * 70 * @param ctm Current transformation matrix to device space. 71 * @param path The occluder used to generate the shadows. 72 * @param zPlaneParams Values for the plane function which returns the Z offset of the 73 * occluder from the canvas based on local x and y values (the current matrix is not applied). 74 * @param lightPos Generally, the 3D position of the light relative to the canvas plane. 75 * If kDirectionalLight_ShadowFlag is set, this specifies a vector pointing 76 * towards the light. 77 * @param lightRadius Generally, the radius of the disc light. 78 * If DirectionalLight_ShadowFlag is set, this specifies the amount of 79 * blur when the occluder is at Z offset == 1. The blur will grow linearly 80 * as the Z value increases. 81 * @param flags Options controlling opaque occluder optimizations, shadow appearance, 82 * and light position. See SkShadowFlags. 83 * @param bounds Return value for shadow bounding box. 84 * @return Returns true if successful, false otherwise. 85 */ 86 static bool GetLocalBounds(const SkMatrix& ctm, const SkPath& path, 87 const SkPoint3& zPlaneParams, const SkPoint3& lightPos, 88 SkScalar lightRadius, uint32_t flags, SkRect* bounds); 89 90 /** 91 * Helper routine to compute color values for one-pass tonal alpha. 92 * 93 * @param inAmbientColor Original ambient color 94 * @param inSpotColor Original spot color 95 * @param outAmbientColor Modified ambient color 96 * @param outSpotColor Modified spot color 97 */ 98 static void ComputeTonalColors(SkColor inAmbientColor, SkColor inSpotColor, 99 SkColor* outAmbientColor, SkColor* outSpotColor); 100 }; 101 102 #endif 103