OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 The Android Open Source Project | 2 * Copyright 2012 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkLightingImageFilter.h" | 8 #include "SkLightingImageFilter.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
(...skipping 665 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
676 class GrGLLight; | 676 class GrGLLight; |
677 | 677 |
678 #endif | 678 #endif |
679 | 679 |
680 }; | 680 }; |
681 | 681 |
682 /////////////////////////////////////////////////////////////////////////////// | 682 /////////////////////////////////////////////////////////////////////////////// |
683 | 683 |
684 class SkImageFilterLight : public SkRefCnt { | 684 class SkImageFilterLight : public SkRefCnt { |
685 public: | 685 public: |
686 | 686 |
687 | 687 |
688 enum LightType { | 688 enum LightType { |
689 kDistant_LightType, | 689 kDistant_LightType, |
690 kPoint_LightType, | 690 kPoint_LightType, |
691 kSpot_LightType, | 691 kSpot_LightType, |
692 }; | 692 }; |
693 virtual LightType type() const = 0; | 693 virtual LightType type() const = 0; |
694 const SkPoint3& color() const { return fColor; } | 694 const SkPoint3& color() const { return fColor; } |
695 virtual GrGLLight* createGLLight() const = 0; | 695 virtual GrGLLight* createGLLight() const = 0; |
696 virtual bool isEqual(const SkImageFilterLight& other) const { | 696 virtual bool isEqual(const SkImageFilterLight& other) const { |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
813 const SkPointLight& o = static_cast<const SkPointLight&>(other); | 813 const SkPointLight& o = static_cast<const SkPointLight&>(other); |
814 return INHERITED::isEqual(other) && | 814 return INHERITED::isEqual(other) && |
815 fLocation == o.fLocation; | 815 fLocation == o.fLocation; |
816 } | 816 } |
817 SkImageFilterLight* transform(const SkMatrix& matrix) const override { | 817 SkImageFilterLight* transform(const SkMatrix& matrix) const override { |
818 SkPoint location2 = SkPoint::Make(fLocation.fX, fLocation.fY); | 818 SkPoint location2 = SkPoint::Make(fLocation.fX, fLocation.fY); |
819 matrix.mapPoints(&location2, 1); | 819 matrix.mapPoints(&location2, 1); |
820 // Use X scale and Y scale on Z and average the result | 820 // Use X scale and Y scale on Z and average the result |
821 SkPoint locationZ = SkPoint::Make(fLocation.fZ, fLocation.fZ); | 821 SkPoint locationZ = SkPoint::Make(fLocation.fZ, fLocation.fZ); |
822 matrix.mapVectors(&locationZ, 1); | 822 matrix.mapVectors(&locationZ, 1); |
823 SkPoint3 location = SkPoint3::Make(location2.fX, | 823 SkPoint3 location = SkPoint3::Make(location2.fX, |
824 location2.fY, | 824 location2.fY, |
825 SkScalarAve(locationZ.fX, locationZ.f
Y)); | 825 SkScalarAve(locationZ.fX, locationZ.f
Y)); |
826 return new SkPointLight(location, color()); | 826 return new SkPointLight(location, color()); |
827 } | 827 } |
828 | 828 |
829 SkPointLight(SkReadBuffer& buffer) : INHERITED(buffer) { | 829 SkPointLight(SkReadBuffer& buffer) : INHERITED(buffer) { |
830 fLocation = readPoint3(buffer); | 830 fLocation = readPoint3(buffer); |
831 } | 831 } |
832 | 832 |
833 protected: | 833 protected: |
834 SkPointLight(const SkPoint3& location, const SkPoint3& color) | 834 SkPointLight(const SkPoint3& location, const SkPoint3& color) |
(...skipping 13 matching lines...) Expand all Loading... |
848 class SkSpotLight : public SkImageFilterLight { | 848 class SkSpotLight : public SkImageFilterLight { |
849 public: | 849 public: |
850 SkSpotLight(const SkPoint3& location, | 850 SkSpotLight(const SkPoint3& location, |
851 const SkPoint3& target, | 851 const SkPoint3& target, |
852 SkScalar specularExponent, | 852 SkScalar specularExponent, |
853 SkScalar cutoffAngle, | 853 SkScalar cutoffAngle, |
854 SkColor color) | 854 SkColor color) |
855 : INHERITED(color), | 855 : INHERITED(color), |
856 fLocation(location), | 856 fLocation(location), |
857 fTarget(target), | 857 fTarget(target), |
858 fSpecularExponent(SkScalarPin(specularExponent, kSpecularExponentMin, kSp
ecularExponentMax)) | 858 fSpecularExponent(specularExponent) |
859 { | 859 { |
860 fS = target - location; | 860 fS = target - location; |
861 fast_normalize(&fS); | 861 fast_normalize(&fS); |
862 fCosOuterConeAngle = SkScalarCos(SkDegreesToRadians(cutoffAngle)); | 862 fCosOuterConeAngle = SkScalarCos(SkDegreesToRadians(cutoffAngle)); |
863 const SkScalar antiAliasThreshold = 0.016f; | 863 const SkScalar antiAliasThreshold = 0.016f; |
864 fCosInnerConeAngle = fCosOuterConeAngle + antiAliasThreshold; | 864 fCosInnerConeAngle = fCosOuterConeAngle + antiAliasThreshold; |
865 fConeScale = SkScalarInvert(antiAliasThreshold); | 865 fConeScale = SkScalarInvert(antiAliasThreshold); |
866 } | 866 } |
867 | 867 |
868 SkImageFilterLight* transform(const SkMatrix& matrix) const override { | 868 SkImageFilterLight* transform(const SkMatrix& matrix) const override { |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
978 | 978 |
979 const SkSpotLight& o = static_cast<const SkSpotLight&>(other); | 979 const SkSpotLight& o = static_cast<const SkSpotLight&>(other); |
980 return INHERITED::isEqual(other) && | 980 return INHERITED::isEqual(other) && |
981 fLocation == o.fLocation && | 981 fLocation == o.fLocation && |
982 fTarget == o.fTarget && | 982 fTarget == o.fTarget && |
983 fSpecularExponent == o.fSpecularExponent && | 983 fSpecularExponent == o.fSpecularExponent && |
984 fCosOuterConeAngle == o.fCosOuterConeAngle; | 984 fCosOuterConeAngle == o.fCosOuterConeAngle; |
985 } | 985 } |
986 | 986 |
987 private: | 987 private: |
988 static const SkScalar kSpecularExponentMin; | |
989 static const SkScalar kSpecularExponentMax; | |
990 | |
991 SkPoint3 fLocation; | 988 SkPoint3 fLocation; |
992 SkPoint3 fTarget; | 989 SkPoint3 fTarget; |
993 SkScalar fSpecularExponent; | 990 SkScalar fSpecularExponent; |
994 SkScalar fCosOuterConeAngle; | 991 SkScalar fCosOuterConeAngle; |
995 SkScalar fCosInnerConeAngle; | 992 SkScalar fCosInnerConeAngle; |
996 SkScalar fConeScale; | 993 SkScalar fConeScale; |
997 SkPoint3 fS; | 994 SkPoint3 fS; |
998 | 995 |
999 typedef SkImageFilterLight INHERITED; | 996 typedef SkImageFilterLight INHERITED; |
1000 }; | 997 }; |
1001 | 998 |
1002 // According to the spec, the specular term should be in the range [1, 128] : | |
1003 // http://www.w3.org/TR/SVG/filters.html#feSpecularLightingSpecularExponentAttri
bute | |
1004 const SkScalar SkSpotLight::kSpecularExponentMin = 1.0f; | |
1005 const SkScalar SkSpotLight::kSpecularExponentMax = 128.0f; | |
1006 | |
1007 /////////////////////////////////////////////////////////////////////////////// | 999 /////////////////////////////////////////////////////////////////////////////// |
1008 | 1000 |
1009 void SkImageFilterLight::flattenLight(SkWriteBuffer& buffer) const { | 1001 void SkImageFilterLight::flattenLight(SkWriteBuffer& buffer) const { |
1010 // Write type first, then baseclass, then subclass. | 1002 // Write type first, then baseclass, then subclass. |
1011 buffer.writeInt(this->type()); | 1003 buffer.writeInt(this->type()); |
1012 writePoint3(fColor, buffer); | 1004 writePoint3(fColor, buffer); |
1013 this->onFlattenLight(buffer); | 1005 this->onFlattenLight(buffer); |
1014 } | 1006 } |
1015 | 1007 |
1016 /*static*/ SkImageFilterLight* SkImageFilterLight::UnflattenLight(SkReadBuffer&
buffer) { | 1008 /*static*/ SkImageFilterLight* SkImageFilterLight::UnflattenLight(SkReadBuffer&
buffer) { |
(...skipping 972 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1989 SkString lightColorBody; | 1981 SkString lightColorBody; |
1990 lightColorBody.appendf("\tfloat cosAngle = -dot(surfaceToLight, %s);\n", s); | 1982 lightColorBody.appendf("\tfloat cosAngle = -dot(surfaceToLight, %s);\n", s); |
1991 lightColorBody.appendf("\tif (cosAngle < %s) {\n", cosOuter); | 1983 lightColorBody.appendf("\tif (cosAngle < %s) {\n", cosOuter); |
1992 lightColorBody.appendf("\t\treturn vec3(0);\n"); | 1984 lightColorBody.appendf("\t\treturn vec3(0);\n"); |
1993 lightColorBody.appendf("\t}\n"); | 1985 lightColorBody.appendf("\t}\n"); |
1994 lightColorBody.appendf("\tfloat scale = pow(cosAngle, %s);\n", exponent); | 1986 lightColorBody.appendf("\tfloat scale = pow(cosAngle, %s);\n", exponent); |
1995 lightColorBody.appendf("\tif (cosAngle < %s) {\n", cosInner); | 1987 lightColorBody.appendf("\tif (cosAngle < %s) {\n", cosInner); |
1996 lightColorBody.appendf("\t\treturn %s * scale * (cosAngle - %s) * %s;\n", | 1988 lightColorBody.appendf("\t\treturn %s * scale * (cosAngle - %s) * %s;\n", |
1997 color, cosOuter, coneScale); | 1989 color, cosOuter, coneScale); |
1998 lightColorBody.appendf("\t}\n"); | 1990 lightColorBody.appendf("\t}\n"); |
1999 lightColorBody.appendf("\treturn %s;\n", color); | 1991 lightColorBody.appendf("\treturn %s * scale;\n", color); |
2000 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); | 1992 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
2001 fsBuilder->emitFunction(kVec3f_GrSLType, | 1993 fsBuilder->emitFunction(kVec3f_GrSLType, |
2002 "lightColor", | 1994 "lightColor", |
2003 SK_ARRAY_COUNT(gLightColorArgs), | 1995 SK_ARRAY_COUNT(gLightColorArgs), |
2004 gLightColorArgs, | 1996 gLightColorArgs, |
2005 lightColorBody.c_str(), | 1997 lightColorBody.c_str(), |
2006 &fLightColorFunc); | 1998 &fLightColorFunc); |
2007 | 1999 |
2008 fsBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); | 2000 fsBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); |
2009 } | 2001 } |
2010 | 2002 |
2011 #endif | 2003 #endif |
2012 | 2004 |
2013 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) | 2005 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) |
2014 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) | 2006 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) |
2015 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) | 2007 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) |
2016 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 2008 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
OLD | NEW |