1 /* 2 * Copyright 2012 The Android Open Source Project 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 9 #ifndef SkLightingImageFilter_DEFINED 10 #define SkLightingImageFilter_DEFINED 11 12 #include "SkImageFilter.h" 13 #include "SkColor.h" 14 15 class SK_API SkPoint3 { 16 public: SkPoint3()17 SkPoint3() {} SkPoint3(SkScalar x,SkScalar y,SkScalar z)18 SkPoint3(SkScalar x, SkScalar y, SkScalar z) 19 : fX(x), fY(y), fZ(z) {} dot(const SkPoint3 & other)20 SkScalar dot(const SkPoint3& other) const { 21 return SkScalarMul(fX, other.fX) 22 + SkScalarMul(fY, other.fY) 23 + SkScalarMul(fZ, other.fZ); 24 } maxComponent()25 SkScalar maxComponent() const { 26 return fX > fY ? (fX > fZ ? fX : fZ) : (fY > fZ ? fY : fZ); 27 } normalize()28 void normalize() { 29 // Small epsilon is added to prevent division by 0. 30 SkScalar scale = SkScalarInvert(SkScalarSqrt(dot(*this)) + SK_ScalarNearlyZero); 31 fX = SkScalarMul(fX, scale); 32 fY = SkScalarMul(fY, scale); 33 fZ = SkScalarMul(fZ, scale); 34 } 35 SkPoint3 operator*(SkScalar scalar) const { 36 return SkPoint3(SkScalarMul(fX, scalar), 37 SkScalarMul(fY, scalar), 38 SkScalarMul(fZ, scalar)); 39 } 40 SkPoint3 operator-(const SkPoint3& other) const { 41 return SkPoint3(fX - other.fX, fY - other.fY, fZ - other.fZ); 42 } 43 bool operator==(const SkPoint3& other) const { 44 return fX == other.fX && fY == other.fY && fZ == other.fZ; 45 } 46 SkScalar fX, fY, fZ; 47 }; 48 49 class SkLight; 50 51 class SK_API SkLightingImageFilter : public SkImageFilter { 52 public: 53 static SkImageFilter* CreateDistantLitDiffuse(const SkPoint3& direction, 54 SkColor lightColor, SkScalar surfaceScale, SkScalar kd, 55 SkImageFilter* input = NULL, const CropRect* cropRect = NULL); 56 static SkImageFilter* CreatePointLitDiffuse(const SkPoint3& location, 57 SkColor lightColor, SkScalar surfaceScale, SkScalar kd, 58 SkImageFilter* input = NULL, const CropRect* cropRect = NULL); 59 static SkImageFilter* CreateSpotLitDiffuse(const SkPoint3& location, 60 const SkPoint3& target, SkScalar specularExponent, SkScalar cutoffAngle, 61 SkColor lightColor, SkScalar surfaceScale, SkScalar kd, 62 SkImageFilter* input = NULL, const CropRect* cropRect = NULL); 63 static SkImageFilter* CreateDistantLitSpecular(const SkPoint3& direction, 64 SkColor lightColor, SkScalar surfaceScale, SkScalar ks, 65 SkScalar shininess, SkImageFilter* input = NULL, const CropRect* cropRect = NULL); 66 static SkImageFilter* CreatePointLitSpecular(const SkPoint3& location, 67 SkColor lightColor, SkScalar surfaceScale, SkScalar ks, 68 SkScalar shininess, SkImageFilter* input = NULL, const CropRect* cropRect = NULL); 69 static SkImageFilter* CreateSpotLitSpecular(const SkPoint3& location, 70 const SkPoint3& target, SkScalar specularExponent, SkScalar cutoffAngle, 71 SkColor lightColor, SkScalar surfaceScale, SkScalar ks, 72 SkScalar shininess, SkImageFilter* input = NULL, const CropRect* cropRect = NULL); 73 ~SkLightingImageFilter(); 74 75 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() 76 77 protected: 78 SkLightingImageFilter(SkLight* light, 79 SkScalar surfaceScale, 80 SkImageFilter* input, 81 const CropRect* cropRect = NULL); 82 explicit SkLightingImageFilter(SkFlattenableReadBuffer& buffer); 83 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; light()84 const SkLight* light() const { return fLight; } surfaceScale()85 SkScalar surfaceScale() const { return fSurfaceScale; } 86 87 private: 88 typedef SkImageFilter INHERITED; 89 SkLight* fLight; 90 SkScalar fSurfaceScale; 91 }; 92 93 #endif 94