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(specularExponent) | 858 fSpecularExponent(SkScalarPin(specularExponent, kSpecularExponentMin, kSp
ecularExponentMax)) |
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 |
988 SkPoint3 fLocation; | 991 SkPoint3 fLocation; |
989 SkPoint3 fTarget; | 992 SkPoint3 fTarget; |
990 SkScalar fSpecularExponent; | 993 SkScalar fSpecularExponent; |
991 SkScalar fCosOuterConeAngle; | 994 SkScalar fCosOuterConeAngle; |
992 SkScalar fCosInnerConeAngle; | 995 SkScalar fCosInnerConeAngle; |
993 SkScalar fConeScale; | 996 SkScalar fConeScale; |
994 SkPoint3 fS; | 997 SkPoint3 fS; |
995 | 998 |
996 typedef SkImageFilterLight INHERITED; | 999 typedef SkImageFilterLight INHERITED; |
997 }; | 1000 }; |
998 | 1001 |
| 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 |
999 /////////////////////////////////////////////////////////////////////////////// | 1007 /////////////////////////////////////////////////////////////////////////////// |
1000 | 1008 |
1001 void SkImageFilterLight::flattenLight(SkWriteBuffer& buffer) const { | 1009 void SkImageFilterLight::flattenLight(SkWriteBuffer& buffer) const { |
1002 // Write type first, then baseclass, then subclass. | 1010 // Write type first, then baseclass, then subclass. |
1003 buffer.writeInt(this->type()); | 1011 buffer.writeInt(this->type()); |
1004 writePoint3(fColor, buffer); | 1012 writePoint3(fColor, buffer); |
1005 this->onFlattenLight(buffer); | 1013 this->onFlattenLight(buffer); |
1006 } | 1014 } |
1007 | 1015 |
1008 /*static*/ SkImageFilterLight* SkImageFilterLight::UnflattenLight(SkReadBuffer&
buffer) { | 1016 /*static*/ SkImageFilterLight* SkImageFilterLight::UnflattenLight(SkReadBuffer&
buffer) { |
(...skipping 972 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1981 SkString lightColorBody; | 1989 SkString lightColorBody; |
1982 lightColorBody.appendf("\tfloat cosAngle = -dot(surfaceToLight, %s);\n", s); | 1990 lightColorBody.appendf("\tfloat cosAngle = -dot(surfaceToLight, %s);\n", s); |
1983 lightColorBody.appendf("\tif (cosAngle < %s) {\n", cosOuter); | 1991 lightColorBody.appendf("\tif (cosAngle < %s) {\n", cosOuter); |
1984 lightColorBody.appendf("\t\treturn vec3(0);\n"); | 1992 lightColorBody.appendf("\t\treturn vec3(0);\n"); |
1985 lightColorBody.appendf("\t}\n"); | 1993 lightColorBody.appendf("\t}\n"); |
1986 lightColorBody.appendf("\tfloat scale = pow(cosAngle, %s);\n", exponent); | 1994 lightColorBody.appendf("\tfloat scale = pow(cosAngle, %s);\n", exponent); |
1987 lightColorBody.appendf("\tif (cosAngle < %s) {\n", cosInner); | 1995 lightColorBody.appendf("\tif (cosAngle < %s) {\n", cosInner); |
1988 lightColorBody.appendf("\t\treturn %s * scale * (cosAngle - %s) * %s;\n", | 1996 lightColorBody.appendf("\t\treturn %s * scale * (cosAngle - %s) * %s;\n", |
1989 color, cosOuter, coneScale); | 1997 color, cosOuter, coneScale); |
1990 lightColorBody.appendf("\t}\n"); | 1998 lightColorBody.appendf("\t}\n"); |
1991 lightColorBody.appendf("\treturn %s * scale;\n", color); | 1999 lightColorBody.appendf("\treturn %s;\n", color); |
1992 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); | 2000 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
1993 fsBuilder->emitFunction(kVec3f_GrSLType, | 2001 fsBuilder->emitFunction(kVec3f_GrSLType, |
1994 "lightColor", | 2002 "lightColor", |
1995 SK_ARRAY_COUNT(gLightColorArgs), | 2003 SK_ARRAY_COUNT(gLightColorArgs), |
1996 gLightColorArgs, | 2004 gLightColorArgs, |
1997 lightColorBody.c_str(), | 2005 lightColorBody.c_str(), |
1998 &fLightColorFunc); | 2006 &fLightColorFunc); |
1999 | 2007 |
2000 fsBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); | 2008 fsBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); |
2001 } | 2009 } |
2002 | 2010 |
2003 #endif | 2011 #endif |
2004 | 2012 |
2005 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) | 2013 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) |
2006 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) | 2014 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) |
2007 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) | 2015 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) |
2008 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 2016 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
OLD | NEW |