Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(705)

Side by Side Diff: src/effects/SkLightingImageFilter.cpp

Issue 1244523002: Revert of Add fast normalize for SkLightingImageFilter. (Closed) Base URL: https://chromium.googlesource.com/skia.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 // 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.
54 inline void shiftMatrixLeft(int m[9]) { 54 inline void shiftMatrixLeft(int m[9]) {
55 m[0] = m[1]; 55 m[0] = m[1];
56 m[3] = m[4]; 56 m[3] = m[4];
57 m[6] = m[7]; 57 m[6] = m[7];
58 m[1] = m[2]; 58 m[1] = m[2];
59 m[4] = m[5]; 59 m[4] = m[5];
60 m[7] = m[8]; 60 m[7] = m[8];
61 } 61 }
62 62
63 static inline void fast_normalize(SkPoint3* vector) {
64 // add a tiny bit so we don't have to worry about divide-by-zero
65 SkScalar magSq = vector->dot(*vector) + SK_ScalarNearlyZero;
66 SkScalar scale = sk_float_rsqrt(magSq);
67 vector->fX *= scale;
68 vector->fY *= scale;
69 vector->fZ *= scale;
70 }
71
72 class DiffuseLightingType { 63 class DiffuseLightingType {
73 public: 64 public:
74 DiffuseLightingType(SkScalar kd) 65 DiffuseLightingType(SkScalar kd)
75 : fKD(kd) {} 66 : fKD(kd) {}
76 SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight, 67 SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight,
77 const SkPoint3& lightColor) const { 68 const SkPoint3& lightColor) const {
78 SkScalar colorScale = SkScalarMul(fKD, normal.dot(surfaceTolight)); 69 SkScalar colorScale = SkScalarMul(fKD, normal.dot(surfaceTolight));
79 colorScale = SkScalarClampMax(colorScale, SK_Scalar1); 70 colorScale = SkScalarClampMax(colorScale, SK_Scalar1);
80 SkPoint3 color = lightColor.makeScale(colorScale); 71 SkPoint3 color = lightColor.makeScale(colorScale);
81 return SkPackARGB32(255, 72 return SkPackARGB32(255,
(...skipping 10 matching lines...) Expand all
92 } 83 }
93 84
94 class SpecularLightingType { 85 class SpecularLightingType {
95 public: 86 public:
96 SpecularLightingType(SkScalar ks, SkScalar shininess) 87 SpecularLightingType(SkScalar ks, SkScalar shininess)
97 : fKS(ks), fShininess(shininess) {} 88 : fKS(ks), fShininess(shininess) {}
98 SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight, 89 SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight,
99 const SkPoint3& lightColor) const { 90 const SkPoint3& lightColor) const {
100 SkPoint3 halfDir(surfaceTolight); 91 SkPoint3 halfDir(surfaceTolight);
101 halfDir.fZ += SK_Scalar1; // eye position is always (0, 0, 1) 92 halfDir.fZ += SK_Scalar1; // eye position is always (0, 0, 1)
102 fast_normalize(&halfDir); 93 halfDir.normalize();
103 SkScalar colorScale = SkScalarMul(fKS, 94 SkScalar colorScale = SkScalarMul(fKS,
104 SkScalarPow(normal.dot(halfDir), fShininess)); 95 SkScalarPow(normal.dot(halfDir), fShininess));
105 colorScale = SkScalarClampMax(colorScale, SK_Scalar1); 96 colorScale = SkScalarClampMax(colorScale, SK_Scalar1);
106 SkPoint3 color = lightColor.makeScale(colorScale); 97 SkPoint3 color = lightColor.makeScale(colorScale);
107 return SkPackARGB32(SkClampMax(SkScalarRoundToInt(max_component(color)), 255), 98 return SkPackARGB32(SkClampMax(SkScalarRoundToInt(max_component(color)), 255),
108 SkClampMax(SkScalarRoundToInt(color.fX), 255), 99 SkClampMax(SkScalarRoundToInt(color.fX), 255),
109 SkClampMax(SkScalarRoundToInt(color.fY), 255), 100 SkClampMax(SkScalarRoundToInt(color.fY), 255),
110 SkClampMax(SkScalarRoundToInt(color.fZ), 255)); 101 SkClampMax(SkScalarRoundToInt(color.fZ), 255));
111 } 102 }
112 private: 103 private:
113 SkScalar fKS; 104 SkScalar fKS;
114 SkScalar fShininess; 105 SkScalar fShininess;
115 }; 106 };
116 107
117 inline SkScalar sobel(int a, int b, int c, int d, int e, int f, SkScalar scale) { 108 inline SkScalar sobel(int a, int b, int c, int d, int e, int f, SkScalar scale) {
118 return SkScalarMul(SkIntToScalar(-a + b - 2 * c + 2 * d -e + f), scale); 109 return SkScalarMul(SkIntToScalar(-a + b - 2 * c + 2 * d -e + f), scale);
119 } 110 }
120 111
121 inline SkPoint3 pointToNormal(SkScalar x, SkScalar y, SkScalar surfaceScale) { 112 inline SkPoint3 pointToNormal(SkScalar x, SkScalar y, SkScalar surfaceScale) {
122 SkPoint3 vector = SkPoint3::Make(SkScalarMul(-x, surfaceScale), 113 SkPoint3 vector = SkPoint3::Make(SkScalarMul(-x, surfaceScale),
123 SkScalarMul(-y, surfaceScale), 114 SkScalarMul(-y, surfaceScale),
124 SK_Scalar1); 115 SK_Scalar1);
125 fast_normalize(&vector); 116 vector.normalize();
126 return vector; 117 return vector;
127 } 118 }
128 119
129 inline SkPoint3 topLeftNormal(int m[9], SkScalar surfaceScale) { 120 inline SkPoint3 topLeftNormal(int m[9], SkScalar surfaceScale) {
130 return pointToNormal(sobel(0, 0, m[4], m[5], m[7], m[8], gTwoThirds), 121 return pointToNormal(sobel(0, 0, m[4], m[5], m[7], m[8], gTwoThirds),
131 sobel(0, 0, m[4], m[7], m[5], m[8], gTwoThirds), 122 sobel(0, 0, m[4], m[7], m[5], m[8], gTwoThirds),
132 surfaceScale); 123 surfaceScale);
133 } 124 }
134 125
135 inline SkPoint3 topNormal(int m[9], SkScalar surfaceScale) { 126 inline SkPoint3 topNormal(int m[9], SkScalar surfaceScale) {
(...skipping 666 matching lines...) Expand 10 before | Expand all | Expand 10 after
802 class SkPointLight : public SkLight { 793 class SkPointLight : public SkLight {
803 public: 794 public:
804 SkPointLight(const SkPoint3& location, SkColor color) 795 SkPointLight(const SkPoint3& location, SkColor color)
805 : INHERITED(color), fLocation(location) {} 796 : INHERITED(color), fLocation(location) {}
806 797
807 SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const { 798 SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const {
808 SkPoint3 direction = SkPoint3::Make(fLocation.fX - SkIntToScalar(x), 799 SkPoint3 direction = SkPoint3::Make(fLocation.fX - SkIntToScalar(x),
809 fLocation.fY - SkIntToScalar(y), 800 fLocation.fY - SkIntToScalar(y),
810 fLocation.fZ - SkScalarMul(SkIntToSc alar(z), 801 fLocation.fZ - SkScalarMul(SkIntToSc alar(z),
811 surfaceSc ale)); 802 surfaceSc ale));
812 fast_normalize(&direction); 803 direction.normalize();
813 return direction; 804 return direction;
814 }; 805 };
815 const SkPoint3& lightColor(const SkPoint3&) const { return this->color(); } 806 const SkPoint3& lightColor(const SkPoint3&) const { return this->color(); }
816 LightType type() const override { return kPoint_LightType; } 807 LightType type() const override { return kPoint_LightType; }
817 const SkPoint3& location() const { return fLocation; } 808 const SkPoint3& location() const { return fLocation; }
818 GrGLLight* createGLLight() const override { 809 GrGLLight* createGLLight() const override {
819 #if SK_SUPPORT_GPU 810 #if SK_SUPPORT_GPU
820 return SkNEW(GrGLPointLight); 811 return SkNEW(GrGLPointLight);
821 #else 812 #else
822 SkDEBUGFAIL("Should not call in GPU-less build"); 813 SkDEBUGFAIL("Should not call in GPU-less build");
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
868 const SkPoint3& target, 859 const SkPoint3& target,
869 SkScalar specularExponent, 860 SkScalar specularExponent,
870 SkScalar cutoffAngle, 861 SkScalar cutoffAngle,
871 SkColor color) 862 SkColor color)
872 : INHERITED(color), 863 : INHERITED(color),
873 fLocation(location), 864 fLocation(location),
874 fTarget(target), 865 fTarget(target),
875 fSpecularExponent(SkScalarPin(specularExponent, kSpecularExponentMin, kSp ecularExponentMax)) 866 fSpecularExponent(SkScalarPin(specularExponent, kSpecularExponentMin, kSp ecularExponentMax))
876 { 867 {
877 fS = target - location; 868 fS = target - location;
878 fast_normalize(&fS); 869 fS.normalize();
879 fCosOuterConeAngle = SkScalarCos(SkDegreesToRadians(cutoffAngle)); 870 fCosOuterConeAngle = SkScalarCos(SkDegreesToRadians(cutoffAngle));
880 const SkScalar antiAliasThreshold = 0.016f; 871 const SkScalar antiAliasThreshold = 0.016f;
881 fCosInnerConeAngle = fCosOuterConeAngle + antiAliasThreshold; 872 fCosInnerConeAngle = fCosOuterConeAngle + antiAliasThreshold;
882 fConeScale = SkScalarInvert(antiAliasThreshold); 873 fConeScale = SkScalarInvert(antiAliasThreshold);
883 } 874 }
884 875
885 SkLight* transform(const SkMatrix& matrix) const override { 876 SkLight* transform(const SkMatrix& matrix) const override {
886 SkPoint location2 = SkPoint::Make(fLocation.fX, fLocation.fY); 877 SkPoint location2 = SkPoint::Make(fLocation.fX, fLocation.fY);
887 matrix.mapPoints(&location2, 1); 878 matrix.mapPoints(&location2, 1);
888 // Use X scale and Y scale on Z and average the result 879 // Use X scale and Y scale on Z and average the result
889 SkPoint locationZ = SkPoint::Make(fLocation.fZ, fLocation.fZ); 880 SkPoint locationZ = SkPoint::Make(fLocation.fZ, fLocation.fZ);
890 matrix.mapVectors(&locationZ, 1); 881 matrix.mapVectors(&locationZ, 1);
891 SkPoint3 location = SkPoint3::Make(location2.fX, location2.fY, 882 SkPoint3 location = SkPoint3::Make(location2.fX, location2.fY,
892 SkScalarAve(locationZ.fX, locationZ.f Y)); 883 SkScalarAve(locationZ.fX, locationZ.f Y));
893 SkPoint target2 = SkPoint::Make(fTarget.fX, fTarget.fY); 884 SkPoint target2 = SkPoint::Make(fTarget.fX, fTarget.fY);
894 matrix.mapPoints(&target2, 1); 885 matrix.mapPoints(&target2, 1);
895 SkPoint targetZ = SkPoint::Make(fTarget.fZ, fTarget.fZ); 886 SkPoint targetZ = SkPoint::Make(fTarget.fZ, fTarget.fZ);
896 matrix.mapVectors(&targetZ, 1); 887 matrix.mapVectors(&targetZ, 1);
897 SkPoint3 target = SkPoint3::Make(target2.fX, target2.fY, 888 SkPoint3 target = SkPoint3::Make(target2.fX, target2.fY,
898 SkScalarAve(targetZ.fX, targetZ.fY)); 889 SkScalarAve(targetZ.fX, targetZ.fY));
899 SkPoint3 s = target - location; 890 SkPoint3 s = target - location;
900 fast_normalize(&s); 891 s.normalize();
901 return new SkSpotLight(location, 892 return new SkSpotLight(location,
902 target, 893 target,
903 fSpecularExponent, 894 fSpecularExponent,
904 fCosOuterConeAngle, 895 fCosOuterConeAngle,
905 fCosInnerConeAngle, 896 fCosInnerConeAngle,
906 fConeScale, 897 fConeScale,
907 s, 898 s,
908 color()); 899 color());
909 } 900 }
910 901
911 SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const { 902 SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const {
912 SkPoint3 direction = SkPoint3::Make(fLocation.fX - SkIntToScalar(x), 903 SkPoint3 direction = SkPoint3::Make(fLocation.fX - SkIntToScalar(x),
913 fLocation.fY - SkIntToScalar(y), 904 fLocation.fY - SkIntToScalar(y),
914 fLocation.fZ - SkScalarMul(SkIntToSc alar(z), 905 fLocation.fZ - SkScalarMul(SkIntToSc alar(z),
915 surfaceSc ale)); 906 surfaceSc ale));
916 fast_normalize(&direction); 907 direction.normalize();
917 return direction; 908 return direction;
918 }; 909 };
919 SkPoint3 lightColor(const SkPoint3& surfaceToLight) const { 910 SkPoint3 lightColor(const SkPoint3& surfaceToLight) const {
920 SkScalar cosAngle = -surfaceToLight.dot(fS); 911 SkScalar cosAngle = -surfaceToLight.dot(fS);
921 SkScalar scale = 0; 912 SkScalar scale = 0;
922 if (cosAngle >= fCosOuterConeAngle) { 913 if (cosAngle >= fCosOuterConeAngle) {
923 scale = SkScalarPow(cosAngle, fSpecularExponent); 914 scale = SkScalarPow(cosAngle, fSpecularExponent);
924 if (cosAngle < fCosInnerConeAngle) { 915 if (cosAngle < fCosInnerConeAngle) {
925 scale = SkScalarMul(scale, cosAngle - fCosOuterConeAngle); 916 scale = SkScalarMul(scale, cosAngle - fCosOuterConeAngle);
926 scale *= fConeScale; 917 scale *= fConeScale;
(...skipping 1097 matching lines...) Expand 10 before | Expand all | Expand 10 after
2024 2015
2025 fsBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); 2016 fsBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight);
2026 } 2017 }
2027 2018
2028 #endif 2019 #endif
2029 2020
2030 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) 2021 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter)
2031 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) 2022 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter)
2032 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) 2023 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter)
2033 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END 2024 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698