| 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" |
| 11 #include "SkPoint3.h" |
| 11 #include "SkReadBuffer.h" | 12 #include "SkReadBuffer.h" |
| 13 #include "SkTypes.h" |
| 12 #include "SkWriteBuffer.h" | 14 #include "SkWriteBuffer.h" |
| 13 #include "SkReadBuffer.h" | |
| 14 #include "SkWriteBuffer.h" | |
| 15 #include "SkTypes.h" | |
| 16 | 15 |
| 17 #if SK_SUPPORT_GPU | 16 #if SK_SUPPORT_GPU |
| 18 #include "GrContext.h" | 17 #include "GrContext.h" |
| 19 #include "GrDrawContext.h" | 18 #include "GrDrawContext.h" |
| 20 #include "GrFragmentProcessor.h" | 19 #include "GrFragmentProcessor.h" |
| 21 #include "GrInvariantOutput.h" | 20 #include "GrInvariantOutput.h" |
| 22 #include "GrPaint.h" | 21 #include "GrPaint.h" |
| 23 #include "effects/GrSingleTextureEffect.h" | 22 #include "effects/GrSingleTextureEffect.h" |
| 24 #include "gl/GrGLProcessor.h" | 23 #include "gl/GrGLProcessor.h" |
| 25 #include "gl/builders/GrGLProgramBuilder.h" | 24 #include "gl/builders/GrGLProgramBuilder.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 40 | 39 |
| 41 #if SK_SUPPORT_GPU | 40 #if SK_SUPPORT_GPU |
| 42 void setUniformPoint3(const GrGLProgramDataManager& pdman, UniformHandle uni, | 41 void setUniformPoint3(const GrGLProgramDataManager& pdman, UniformHandle uni, |
| 43 const SkPoint3& point) { | 42 const SkPoint3& point) { |
| 44 GR_STATIC_ASSERT(sizeof(SkPoint3) == 3 * sizeof(GrGLfloat)); | 43 GR_STATIC_ASSERT(sizeof(SkPoint3) == 3 * sizeof(GrGLfloat)); |
| 45 pdman.set3fv(uni, 1, &point.fX); | 44 pdman.set3fv(uni, 1, &point.fX); |
| 46 } | 45 } |
| 47 | 46 |
| 48 void setUniformNormal3(const GrGLProgramDataManager& pdman, UniformHandle uni, | 47 void setUniformNormal3(const GrGLProgramDataManager& pdman, UniformHandle uni, |
| 49 const SkPoint3& point) { | 48 const SkPoint3& point) { |
| 50 setUniformPoint3(pdman, uni, SkPoint3(point.fX, point.fY, point.fZ)); | 49 setUniformPoint3(pdman, uni, point); |
| 51 } | 50 } |
| 52 #endif | 51 #endif |
| 53 | 52 |
| 54 // Shift matrix components to the left, as we advance pixels to the right. | 53 // Shift matrix components to the left, as we advance pixels to the right. |
| 55 inline void shiftMatrixLeft(int m[9]) { | 54 inline void shiftMatrixLeft(int m[9]) { |
| 56 m[0] = m[1]; | 55 m[0] = m[1]; |
| 57 m[3] = m[4]; | 56 m[3] = m[4]; |
| 58 m[6] = m[7]; | 57 m[6] = m[7]; |
| 59 m[1] = m[2]; | 58 m[1] = m[2]; |
| 60 m[4] = m[5]; | 59 m[4] = m[5]; |
| 61 m[7] = m[8]; | 60 m[7] = m[8]; |
| 62 } | 61 } |
| 63 | 62 |
| 64 class DiffuseLightingType { | 63 class DiffuseLightingType { |
| 65 public: | 64 public: |
| 66 DiffuseLightingType(SkScalar kd) | 65 DiffuseLightingType(SkScalar kd) |
| 67 : fKD(kd) {} | 66 : fKD(kd) {} |
| 68 SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight, | 67 SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight, |
| 69 const SkPoint3& lightColor) const { | 68 const SkPoint3& lightColor) const { |
| 70 SkScalar colorScale = SkScalarMul(fKD, normal.dot(surfaceTolight)); | 69 SkScalar colorScale = SkScalarMul(fKD, normal.dot(surfaceTolight)); |
| 71 colorScale = SkScalarClampMax(colorScale, SK_Scalar1); | 70 colorScale = SkScalarClampMax(colorScale, SK_Scalar1); |
| 72 SkPoint3 color(lightColor * colorScale); | 71 SkPoint3 color; |
| 72 lightColor.scale(colorScale, &color); |
| 73 return SkPackARGB32(255, | 73 return SkPackARGB32(255, |
| 74 SkClampMax(SkScalarRoundToInt(color.fX), 255), | 74 SkClampMax(SkScalarRoundToInt(color.fX), 255), |
| 75 SkClampMax(SkScalarRoundToInt(color.fY), 255), | 75 SkClampMax(SkScalarRoundToInt(color.fY), 255), |
| 76 SkClampMax(SkScalarRoundToInt(color.fZ), 255)); | 76 SkClampMax(SkScalarRoundToInt(color.fZ), 255)); |
| 77 } | 77 } |
| 78 private: | 78 private: |
| 79 SkScalar fKD; | 79 SkScalar fKD; |
| 80 }; | 80 }; |
| 81 | 81 |
| 82 static SkScalar max_component(const SkPoint3& p) { |
| 83 return p.x() > p.y() ? (p.x() > p.z() ? p.x() : p.z()) : (p.y() > p.z() ? p.
y() : p.z()); |
| 84 } |
| 85 |
| 82 class SpecularLightingType { | 86 class SpecularLightingType { |
| 83 public: | 87 public: |
| 84 SpecularLightingType(SkScalar ks, SkScalar shininess) | 88 SpecularLightingType(SkScalar ks, SkScalar shininess) |
| 85 : fKS(ks), fShininess(shininess) {} | 89 : fKS(ks), fShininess(shininess) {} |
| 86 SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight, | 90 SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight, |
| 87 const SkPoint3& lightColor) const { | 91 const SkPoint3& lightColor) const { |
| 88 SkPoint3 halfDir(surfaceTolight); | 92 SkPoint3 halfDir(surfaceTolight); |
| 89 halfDir.fZ += SK_Scalar1; // eye position is always (0, 0, 1) | 93 halfDir.fZ += SK_Scalar1; // eye position is always (0, 0, 1) |
| 90 halfDir.normalize(); | 94 halfDir.normalize(); |
| 91 SkScalar colorScale = SkScalarMul(fKS, | 95 SkScalar colorScale = SkScalarMul(fKS, |
| 92 SkScalarPow(normal.dot(halfDir), fShininess)); | 96 SkScalarPow(normal.dot(halfDir), fShininess)); |
| 93 colorScale = SkScalarClampMax(colorScale, SK_Scalar1); | 97 colorScale = SkScalarClampMax(colorScale, SK_Scalar1); |
| 94 SkPoint3 color(lightColor * colorScale); | 98 SkPoint3 color; |
| 95 return SkPackARGB32(SkClampMax(SkScalarRoundToInt(color.maxComponent()),
255), | 99 lightColor.scale(colorScale, &color); |
| 100 return SkPackARGB32(SkClampMax(SkScalarRoundToInt(max_component(color)),
255), |
| 96 SkClampMax(SkScalarRoundToInt(color.fX), 255), | 101 SkClampMax(SkScalarRoundToInt(color.fX), 255), |
| 97 SkClampMax(SkScalarRoundToInt(color.fY), 255), | 102 SkClampMax(SkScalarRoundToInt(color.fY), 255), |
| 98 SkClampMax(SkScalarRoundToInt(color.fZ), 255)); | 103 SkClampMax(SkScalarRoundToInt(color.fZ), 255)); |
| 99 } | 104 } |
| 100 private: | 105 private: |
| 101 SkScalar fKS; | 106 SkScalar fKS; |
| 102 SkScalar fShininess; | 107 SkScalar fShininess; |
| 103 }; | 108 }; |
| 104 | 109 |
| 105 inline SkScalar sobel(int a, int b, int c, int d, int e, int f, SkScalar scale)
{ | 110 inline SkScalar sobel(int a, int b, int c, int d, int e, int f, SkScalar scale)
{ |
| 106 return SkScalarMul(SkIntToScalar(-a + b - 2 * c + 2 * d -e + f), scale); | 111 return SkScalarMul(SkIntToScalar(-a + b - 2 * c + 2 * d -e + f), scale); |
| 107 } | 112 } |
| 108 | 113 |
| 109 inline SkPoint3 pointToNormal(SkScalar x, SkScalar y, SkScalar surfaceScale) { | 114 inline SkPoint3 pointToNormal(SkScalar x, SkScalar y, SkScalar surfaceScale) { |
| 110 SkPoint3 vector(SkScalarMul(-x, surfaceScale), | 115 SkPoint3 vector = SkPoint3::Make(SkScalarMul(-x, surfaceScale), |
| 111 SkScalarMul(-y, surfaceScale), | 116 SkScalarMul(-y, surfaceScale), |
| 112 SK_Scalar1); | 117 SK_Scalar1); |
| 113 vector.normalize(); | 118 vector.normalize(); |
| 114 return vector; | 119 return vector; |
| 115 } | 120 } |
| 116 | 121 |
| 117 inline SkPoint3 topLeftNormal(int m[9], SkScalar surfaceScale) { | 122 inline SkPoint3 topLeftNormal(int m[9], SkScalar surfaceScale) { |
| 118 return pointToNormal(sobel(0, 0, m[4], m[5], m[7], m[8], gTwoThirds), | 123 return pointToNormal(sobel(0, 0, m[4], m[5], m[7], m[8], gTwoThirds), |
| 119 sobel(0, 0, m[4], m[7], m[5], m[8], gTwoThirds), | 124 sobel(0, 0, m[4], m[7], m[5], m[8], gTwoThirds), |
| 120 surfaceScale); | 125 surfaceScale); |
| 121 } | 126 } |
| 122 | 127 |
| (...skipping 582 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 705 } | 710 } |
| 706 // Called to know whether the generated GrGLLight will require access to the
fragment position. | 711 // Called to know whether the generated GrGLLight will require access to the
fragment position. |
| 707 virtual bool requiresFragmentPosition() const = 0; | 712 virtual bool requiresFragmentPosition() const = 0; |
| 708 virtual SkLight* transform(const SkMatrix& matrix) const = 0; | 713 virtual SkLight* transform(const SkMatrix& matrix) const = 0; |
| 709 | 714 |
| 710 // Defined below SkLight's subclasses. | 715 // Defined below SkLight's subclasses. |
| 711 void flattenLight(SkWriteBuffer& buffer) const; | 716 void flattenLight(SkWriteBuffer& buffer) const; |
| 712 static SkLight* UnflattenLight(SkReadBuffer& buffer); | 717 static SkLight* UnflattenLight(SkReadBuffer& buffer); |
| 713 | 718 |
| 714 protected: | 719 protected: |
| 715 SkLight(SkColor color) | 720 SkLight(SkColor color) { |
| 716 : fColor(SkIntToScalar(SkColorGetR(color)), | 721 fColor = SkPoint3::Make(SkIntToScalar(SkColorGetR(color)), |
| 717 SkIntToScalar(SkColorGetG(color)), | 722 SkIntToScalar(SkColorGetG(color)), |
| 718 SkIntToScalar(SkColorGetB(color))) {} | 723 SkIntToScalar(SkColorGetB(color))); |
| 724 } |
| 719 SkLight(const SkPoint3& color) | 725 SkLight(const SkPoint3& color) |
| 720 : fColor(color) {} | 726 : fColor(color) {} |
| 721 SkLight(SkReadBuffer& buffer) { | 727 SkLight(SkReadBuffer& buffer) { |
| 722 fColor = readPoint3(buffer); | 728 fColor = readPoint3(buffer); |
| 723 } | 729 } |
| 724 | 730 |
| 725 virtual void onFlattenLight(SkWriteBuffer& buffer) const = 0; | 731 virtual void onFlattenLight(SkWriteBuffer& buffer) const = 0; |
| 726 | 732 |
| 727 | 733 |
| 728 private: | 734 private: |
| 729 typedef SkRefCnt INHERITED; | 735 typedef SkRefCnt INHERITED; |
| 730 SkPoint3 fColor; | 736 SkPoint3 fColor; |
| 731 }; | 737 }; |
| 732 | 738 |
| 733 /////////////////////////////////////////////////////////////////////////////// | 739 /////////////////////////////////////////////////////////////////////////////// |
| 734 | 740 |
| 735 class SkDistantLight : public SkLight { | 741 class SkDistantLight : public SkLight { |
| 736 public: | 742 public: |
| 737 SkDistantLight(const SkPoint3& direction, SkColor color) | 743 SkDistantLight(const SkPoint3& direction, SkColor color) |
| 738 : INHERITED(color), fDirection(direction) { | 744 : INHERITED(color), fDirection(direction) { |
| 739 } | 745 } |
| 740 | 746 |
| 741 SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const { | 747 SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const { |
| 742 return fDirection; | 748 return fDirection; |
| 743 }; | 749 }; |
| 744 SkPoint3 lightColor(const SkPoint3&) const { return color(); } | 750 const SkPoint3& lightColor(const SkPoint3&) const { return this->color(); } |
| 745 LightType type() const override { return kDistant_LightType; } | 751 LightType type() const override { return kDistant_LightType; } |
| 746 const SkPoint3& direction() const { return fDirection; } | 752 const SkPoint3& direction() const { return fDirection; } |
| 747 GrGLLight* createGLLight() const override { | 753 GrGLLight* createGLLight() const override { |
| 748 #if SK_SUPPORT_GPU | 754 #if SK_SUPPORT_GPU |
| 749 return SkNEW(GrGLDistantLight); | 755 return SkNEW(GrGLDistantLight); |
| 750 #else | 756 #else |
| 751 SkDEBUGFAIL("Should not call in GPU-less build"); | 757 SkDEBUGFAIL("Should not call in GPU-less build"); |
| 752 return NULL; | 758 return NULL; |
| 753 #endif | 759 #endif |
| 754 } | 760 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 785 }; | 791 }; |
| 786 | 792 |
| 787 /////////////////////////////////////////////////////////////////////////////// | 793 /////////////////////////////////////////////////////////////////////////////// |
| 788 | 794 |
| 789 class SkPointLight : public SkLight { | 795 class SkPointLight : public SkLight { |
| 790 public: | 796 public: |
| 791 SkPointLight(const SkPoint3& location, SkColor color) | 797 SkPointLight(const SkPoint3& location, SkColor color) |
| 792 : INHERITED(color), fLocation(location) {} | 798 : INHERITED(color), fLocation(location) {} |
| 793 | 799 |
| 794 SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const { | 800 SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const { |
| 795 SkPoint3 direction(fLocation.fX - SkIntToScalar(x), | 801 SkPoint3 direction = SkPoint3::Make(fLocation.fX - SkIntToScalar(x), |
| 796 fLocation.fY - SkIntToScalar(y), | 802 fLocation.fY - SkIntToScalar(y), |
| 797 fLocation.fZ - SkScalarMul(SkIntToScalar(z), surfaceS
cale)); | 803 fLocation.fZ - SkScalarMul(SkIntToSc
alar(z), |
| 804 surfaceSc
ale)); |
| 798 direction.normalize(); | 805 direction.normalize(); |
| 799 return direction; | 806 return direction; |
| 800 }; | 807 }; |
| 801 SkPoint3 lightColor(const SkPoint3&) const { return color(); } | 808 const SkPoint3& lightColor(const SkPoint3&) const { return this->color(); } |
| 802 LightType type() const override { return kPoint_LightType; } | 809 LightType type() const override { return kPoint_LightType; } |
| 803 const SkPoint3& location() const { return fLocation; } | 810 const SkPoint3& location() const { return fLocation; } |
| 804 GrGLLight* createGLLight() const override { | 811 GrGLLight* createGLLight() const override { |
| 805 #if SK_SUPPORT_GPU | 812 #if SK_SUPPORT_GPU |
| 806 return SkNEW(GrGLPointLight); | 813 return SkNEW(GrGLPointLight); |
| 807 #else | 814 #else |
| 808 SkDEBUGFAIL("Should not call in GPU-less build"); | 815 SkDEBUGFAIL("Should not call in GPU-less build"); |
| 809 return NULL; | 816 return NULL; |
| 810 #endif | 817 #endif |
| 811 } | 818 } |
| 812 bool requiresFragmentPosition() const override { return true; } | 819 bool requiresFragmentPosition() const override { return true; } |
| 813 bool isEqual(const SkLight& other) const override { | 820 bool isEqual(const SkLight& other) const override { |
| 814 if (other.type() != kPoint_LightType) { | 821 if (other.type() != kPoint_LightType) { |
| 815 return false; | 822 return false; |
| 816 } | 823 } |
| 817 const SkPointLight& o = static_cast<const SkPointLight&>(other); | 824 const SkPointLight& o = static_cast<const SkPointLight&>(other); |
| 818 return INHERITED::isEqual(other) && | 825 return INHERITED::isEqual(other) && |
| 819 fLocation == o.fLocation; | 826 fLocation == o.fLocation; |
| 820 } | 827 } |
| 821 SkLight* transform(const SkMatrix& matrix) const override { | 828 SkLight* transform(const SkMatrix& matrix) const override { |
| 822 SkPoint location2 = SkPoint::Make(fLocation.fX, fLocation.fY); | 829 SkPoint location2 = SkPoint::Make(fLocation.fX, fLocation.fY); |
| 823 matrix.mapPoints(&location2, 1); | 830 matrix.mapPoints(&location2, 1); |
| 824 // Use X scale and Y scale on Z and average the result | 831 // Use X scale and Y scale on Z and average the result |
| 825 SkPoint locationZ = SkPoint::Make(fLocation.fZ, fLocation.fZ); | 832 SkPoint locationZ = SkPoint::Make(fLocation.fZ, fLocation.fZ); |
| 826 matrix.mapVectors(&locationZ, 1); | 833 matrix.mapVectors(&locationZ, 1); |
| 827 SkPoint3 location(location2.fX, location2.fY, SkScalarAve(locationZ.fX,
locationZ.fY)); | 834 SkPoint3 location = SkPoint3::Make(location2.fX, |
| 835 location2.fY, |
| 836 SkScalarAve(locationZ.fX, locationZ.f
Y)); |
| 828 return new SkPointLight(location, color()); | 837 return new SkPointLight(location, color()); |
| 829 } | 838 } |
| 830 | 839 |
| 831 SkPointLight(SkReadBuffer& buffer) : INHERITED(buffer) { | 840 SkPointLight(SkReadBuffer& buffer) : INHERITED(buffer) { |
| 832 fLocation = readPoint3(buffer); | 841 fLocation = readPoint3(buffer); |
| 833 } | 842 } |
| 834 | 843 |
| 835 protected: | 844 protected: |
| 836 SkPointLight(const SkPoint3& location, const SkPoint3& color) | 845 SkPointLight(const SkPoint3& location, const SkPoint3& color) |
| 837 : INHERITED(color), fLocation(location) {} | 846 : INHERITED(color), fLocation(location) {} |
| (...skipping 27 matching lines...) Expand all Loading... |
| 865 fCosInnerConeAngle = fCosOuterConeAngle + antiAliasThreshold; | 874 fCosInnerConeAngle = fCosOuterConeAngle + antiAliasThreshold; |
| 866 fConeScale = SkScalarInvert(antiAliasThreshold); | 875 fConeScale = SkScalarInvert(antiAliasThreshold); |
| 867 } | 876 } |
| 868 | 877 |
| 869 SkLight* transform(const SkMatrix& matrix) const override { | 878 SkLight* transform(const SkMatrix& matrix) const override { |
| 870 SkPoint location2 = SkPoint::Make(fLocation.fX, fLocation.fY); | 879 SkPoint location2 = SkPoint::Make(fLocation.fX, fLocation.fY); |
| 871 matrix.mapPoints(&location2, 1); | 880 matrix.mapPoints(&location2, 1); |
| 872 // Use X scale and Y scale on Z and average the result | 881 // Use X scale and Y scale on Z and average the result |
| 873 SkPoint locationZ = SkPoint::Make(fLocation.fZ, fLocation.fZ); | 882 SkPoint locationZ = SkPoint::Make(fLocation.fZ, fLocation.fZ); |
| 874 matrix.mapVectors(&locationZ, 1); | 883 matrix.mapVectors(&locationZ, 1); |
| 875 SkPoint3 location(location2.fX, location2.fY, SkScalarAve(locationZ.fX,
locationZ.fY)); | 884 SkPoint3 location = SkPoint3::Make(location2.fX, location2.fY, |
| 885 SkScalarAve(locationZ.fX, locationZ.f
Y)); |
| 876 SkPoint target2 = SkPoint::Make(fTarget.fX, fTarget.fY); | 886 SkPoint target2 = SkPoint::Make(fTarget.fX, fTarget.fY); |
| 877 matrix.mapPoints(&target2, 1); | 887 matrix.mapPoints(&target2, 1); |
| 878 SkPoint targetZ = SkPoint::Make(fTarget.fZ, fTarget.fZ); | 888 SkPoint targetZ = SkPoint::Make(fTarget.fZ, fTarget.fZ); |
| 879 matrix.mapVectors(&targetZ, 1); | 889 matrix.mapVectors(&targetZ, 1); |
| 880 SkPoint3 target(target2.fX, target2.fY, SkScalarAve(targetZ.fX, targetZ.
fY)); | 890 SkPoint3 target = SkPoint3::Make(target2.fX, target2.fY, |
| 891 SkScalarAve(targetZ.fX, targetZ.fY)); |
| 881 SkPoint3 s = target - location; | 892 SkPoint3 s = target - location; |
| 882 s.normalize(); | 893 s.normalize(); |
| 883 return new SkSpotLight(location, | 894 return new SkSpotLight(location, |
| 884 target, | 895 target, |
| 885 fSpecularExponent, | 896 fSpecularExponent, |
| 886 fCosOuterConeAngle, | 897 fCosOuterConeAngle, |
| 887 fCosInnerConeAngle, | 898 fCosInnerConeAngle, |
| 888 fConeScale, | 899 fConeScale, |
| 889 s, | 900 s, |
| 890 color()); | 901 color()); |
| 891 } | 902 } |
| 892 | 903 |
| 893 SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const { | 904 SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const { |
| 894 SkPoint3 direction(fLocation.fX - SkIntToScalar(x), | 905 SkPoint3 direction = SkPoint3::Make(fLocation.fX - SkIntToScalar(x), |
| 895 fLocation.fY - SkIntToScalar(y), | 906 fLocation.fY - SkIntToScalar(y), |
| 896 fLocation.fZ - SkScalarMul(SkIntToScalar(z), surfaceS
cale)); | 907 fLocation.fZ - SkScalarMul(SkIntToSc
alar(z), |
| 908 surfaceSc
ale)); |
| 897 direction.normalize(); | 909 direction.normalize(); |
| 898 return direction; | 910 return direction; |
| 899 }; | 911 }; |
| 900 SkPoint3 lightColor(const SkPoint3& surfaceToLight) const { | 912 SkPoint3 lightColor(const SkPoint3& surfaceToLight) const { |
| 901 SkScalar cosAngle = -surfaceToLight.dot(fS); | 913 SkScalar cosAngle = -surfaceToLight.dot(fS); |
| 914 SkPoint3 result; |
| 902 if (cosAngle < fCosOuterConeAngle) { | 915 if (cosAngle < fCosOuterConeAngle) { |
| 903 return SkPoint3(0, 0, 0); | 916 result = SkPoint3::Make(0, 0, 0); |
| 917 } else { |
| 918 SkScalar scale = SkScalarPow(cosAngle, fSpecularExponent); |
| 919 if (cosAngle < fCosInnerConeAngle) { |
| 920 scale = SkScalarMul(scale, cosAngle - fCosOuterConeAngle); |
| 921 scale *= fConeScale; |
| 922 } |
| 923 this->color().scale(scale, &result); |
| 904 } | 924 } |
| 905 SkScalar scale = SkScalarPow(cosAngle, fSpecularExponent); | 925 return result; |
| 906 if (cosAngle < fCosInnerConeAngle) { | |
| 907 scale = SkScalarMul(scale, cosAngle - fCosOuterConeAngle); | |
| 908 return color() * SkScalarMul(scale, fConeScale); | |
| 909 } | |
| 910 return color() * scale; | |
| 911 } | 926 } |
| 912 GrGLLight* createGLLight() const override { | 927 GrGLLight* createGLLight() const override { |
| 913 #if SK_SUPPORT_GPU | 928 #if SK_SUPPORT_GPU |
| 914 return SkNEW(GrGLSpotLight); | 929 return SkNEW(GrGLSpotLight); |
| 915 #else | 930 #else |
| 916 SkDEBUGFAIL("Should not call in GPU-less build"); | 931 SkDEBUGFAIL("Should not call in GPU-less build"); |
| 917 return NULL; | 932 return NULL; |
| 918 #endif | 933 #endif |
| 919 } | 934 } |
| 920 bool requiresFragmentPosition() const override { return true; } | 935 bool requiresFragmentPosition() const override { return true; } |
| (...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1379 this->ks(), this->shininess(), bound
aryMode); | 1394 this->ks(), this->shininess(), bound
aryMode); |
| 1380 } | 1395 } |
| 1381 #endif | 1396 #endif |
| 1382 | 1397 |
| 1383 /////////////////////////////////////////////////////////////////////////////// | 1398 /////////////////////////////////////////////////////////////////////////////// |
| 1384 | 1399 |
| 1385 #if SK_SUPPORT_GPU | 1400 #if SK_SUPPORT_GPU |
| 1386 | 1401 |
| 1387 namespace { | 1402 namespace { |
| 1388 SkPoint3 random_point3(SkRandom* random) { | 1403 SkPoint3 random_point3(SkRandom* random) { |
| 1389 return SkPoint3(SkScalarToFloat(random->nextSScalar1()), | 1404 return SkPoint3::Make(SkScalarToFloat(random->nextSScalar1()), |
| 1390 SkScalarToFloat(random->nextSScalar1()), | 1405 SkScalarToFloat(random->nextSScalar1()), |
| 1391 SkScalarToFloat(random->nextSScalar1())); | 1406 SkScalarToFloat(random->nextSScalar1())); |
| 1392 } | 1407 } |
| 1393 | 1408 |
| 1394 SkLight* create_random_light(SkRandom* random) { | 1409 SkLight* create_random_light(SkRandom* random) { |
| 1395 int type = random->nextULessThan(3); | 1410 int type = random->nextULessThan(3); |
| 1396 switch (type) { | 1411 switch (type) { |
| 1397 case 0: { | 1412 case 0: { |
| 1398 return SkNEW_ARGS(SkDistantLight, (random_point3(random), random->ne
xtU())); | 1413 return SkNEW_ARGS(SkDistantLight, (random_point3(random), random->ne
xtU())); |
| 1399 } | 1414 } |
| 1400 case 1: { | 1415 case 1: { |
| 1401 return SkNEW_ARGS(SkPointLight, (random_point3(random), random->next
U())); | 1416 return SkNEW_ARGS(SkPointLight, (random_point3(random), random->next
U())); |
| (...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1876 pdman.set1f(fShininessUni, spec.shininess()); | 1891 pdman.set1f(fShininessUni, spec.shininess()); |
| 1877 } | 1892 } |
| 1878 | 1893 |
| 1879 /////////////////////////////////////////////////////////////////////////////// | 1894 /////////////////////////////////////////////////////////////////////////////// |
| 1880 void GrGLLight::emitLightColorUniform(GrGLFPBuilder* builder) { | 1895 void GrGLLight::emitLightColorUniform(GrGLFPBuilder* builder) { |
| 1881 fColorUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, | 1896 fColorUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
| 1882 kVec3f_GrSLType, kDefault_GrSLPrecision, | 1897 kVec3f_GrSLType, kDefault_GrSLPrecision, |
| 1883 "LightColor"); | 1898 "LightColor"); |
| 1884 } | 1899 } |
| 1885 | 1900 |
| 1886 void GrGLLight::emitLightColor(GrGLFPBuilder* builder, | 1901 void GrGLLight::emitLightColor(GrGLFPBuilder* builder, const char *surfaceToLigh
t) { |
| 1887 const char *surfaceToLight) { | |
| 1888 builder->getFragmentShaderBuilder()->codeAppend(builder->getUniformCStr(this
->lightColorUni())); | 1902 builder->getFragmentShaderBuilder()->codeAppend(builder->getUniformCStr(this
->lightColorUni())); |
| 1889 } | 1903 } |
| 1890 | 1904 |
| 1891 void GrGLLight::setData(const GrGLProgramDataManager& pdman, | 1905 void GrGLLight::setData(const GrGLProgramDataManager& pdman, const SkLight* ligh
t) const { |
| 1892 const SkLight* light) const { | 1906 SkPoint3 tmp; |
| 1893 setUniformPoint3(pdman, fColorUni, light->color() * SkScalarInvert(SkIntToSc
alar(255))); | 1907 light->color().scale(SkScalarInvert(SkIntToScalar(255)), &tmp); |
| 1908 setUniformPoint3(pdman, fColorUni, tmp); |
| 1894 } | 1909 } |
| 1895 | 1910 |
| 1896 /////////////////////////////////////////////////////////////////////////////// | 1911 /////////////////////////////////////////////////////////////////////////////// |
| 1897 | 1912 |
| 1898 void GrGLDistantLight::setData(const GrGLProgramDataManager& pdman, | 1913 void GrGLDistantLight::setData(const GrGLProgramDataManager& pdman, |
| 1899 const SkLight* light) const { | 1914 const SkLight* light) const { |
| 1900 INHERITED::setData(pdman, light); | 1915 INHERITED::setData(pdman, light); |
| 1901 SkASSERT(light->type() == SkLight::kDistant_LightType); | 1916 SkASSERT(light->type() == SkLight::kDistant_LightType); |
| 1902 const SkDistantLight* distantLight = static_cast<const SkDistantLight*>(ligh
t); | 1917 const SkDistantLight* distantLight = static_cast<const SkDistantLight*>(ligh
t); |
| 1903 setUniformNormal3(pdman, fDirectionUni, distantLight->direction()); | 1918 setUniformNormal3(pdman, fDirectionUni, distantLight->direction()); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2006 | 2021 |
| 2007 fsBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); | 2022 fsBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); |
| 2008 } | 2023 } |
| 2009 | 2024 |
| 2010 #endif | 2025 #endif |
| 2011 | 2026 |
| 2012 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) | 2027 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) |
| 2013 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) | 2028 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) |
| 2014 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) | 2029 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) |
| 2015 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 2030 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| OLD | NEW |