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

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

Issue 22799007: I'm investigating how to make the IPC transfer a bit more secure on the (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: New fuzzer added Created 7 years, 4 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 | Annotate | Revision Log
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 28 matching lines...) Expand all
39 void setUniformPoint3(const GrGLUniformManager& uman, UniformHandle uni, const S kPoint3& point) { 39 void setUniformPoint3(const GrGLUniformManager& uman, UniformHandle uni, const S kPoint3& point) {
40 GR_STATIC_ASSERT(sizeof(SkPoint3) == 3 * sizeof(GrGLfloat)); 40 GR_STATIC_ASSERT(sizeof(SkPoint3) == 3 * sizeof(GrGLfloat));
41 uman.set3fv(uni, 0, 1, &point.fX); 41 uman.set3fv(uni, 0, 1, &point.fX);
42 } 42 }
43 43
44 void setUniformNormal3(const GrGLUniformManager& uman, UniformHandle uni, const SkPoint3& point) { 44 void setUniformNormal3(const GrGLUniformManager& uman, UniformHandle uni, const SkPoint3& point) {
45 setUniformPoint3(uman, uni, SkPoint3(point.fX, point.fY, point.fZ)); 45 setUniformPoint3(uman, uni, SkPoint3(point.fX, point.fY, point.fZ));
46 } 46 }
47 #endif 47 #endif
48 48
49 void getClampedColor(const SkPoint3 color, U8CPU& r8, U8CPU& g8, U8CPU& b8) {
Stephen White 2013/08/21 20:49:26 Personal style, but I'm not a fan of non-const ref
sugoi 2013/08/21 21:12:09 Ok, will do.
scroggo 2013/08/21 23:25:27 That's actually in the Skia style guide, so it's m
sugoi1 2013/08/22 15:41:00 Done.
50 // Sending negative numbers to SkPackARGB32 would convert them to
51 // unsigned, changing small negative integers into very large positive
52 // unsigned integers. To avoid issues, clamp colors to the [0, 255] range.
53 int r = SkScalarFloorToInt(color.fX);
54 int g = SkScalarFloorToInt(color.fY);
55 int b = SkScalarFloorToInt(color.fZ);
56 r8 = r < 0 ? 0 : (r > 255 ? 255 : r);
57 g8 = g < 0 ? 0 : (g > 255 ? 255 : g);
58 b8 = b < 0 ? 0 : (b > 255 ? 255 : b);
59 }
60
49 // Shift matrix components to the left, as we advance pixels to the right. 61 // Shift matrix components to the left, as we advance pixels to the right.
50 inline void shiftMatrixLeft(int m[9]) { 62 inline void shiftMatrixLeft(int m[9]) {
51 m[0] = m[1]; 63 m[0] = m[1];
52 m[3] = m[4]; 64 m[3] = m[4];
53 m[6] = m[7]; 65 m[6] = m[7];
54 m[1] = m[2]; 66 m[1] = m[2];
55 m[4] = m[5]; 67 m[4] = m[5];
56 m[7] = m[8]; 68 m[7] = m[8];
57 } 69 }
58 70
59 class DiffuseLightingType { 71 class DiffuseLightingType {
60 public: 72 public:
61 DiffuseLightingType(SkScalar kd) 73 DiffuseLightingType(SkScalar kd)
62 : fKD(kd) {} 74 : fKD(kd) {}
63 SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight, cons t SkPoint3& lightColor) const { 75 SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight, cons t SkPoint3& lightColor) const {
64 SkScalar colorScale = SkScalarMul(fKD, normal.dot(surfaceTolight)); 76 SkScalar colorScale = SkScalarMul(fKD, normal.dot(surfaceTolight));
65 colorScale = SkScalarClampMax(colorScale, SK_Scalar1); 77 colorScale = SkScalarClampMax(colorScale, SK_Scalar1);
66 SkPoint3 color(lightColor * colorScale); 78 SkPoint3 color(lightColor * colorScale);
67 return SkPackARGB32(255, 79 U8CPU r(0), g(0), b(0);
68 SkScalarFloorToInt(color.fX), 80 getClampedColor(color, r, g, b);
69 SkScalarFloorToInt(color.fY), 81 return SkPackARGB32(255, r, g, b);
70 SkScalarFloorToInt(color.fZ));
71 } 82 }
72 private: 83 private:
73 SkScalar fKD; 84 SkScalar fKD;
74 }; 85 };
75 86
76 class SpecularLightingType { 87 class SpecularLightingType {
77 public: 88 public:
78 SpecularLightingType(SkScalar ks, SkScalar shininess) 89 SpecularLightingType(SkScalar ks, SkScalar shininess)
79 : fKS(ks), fShininess(shininess) {} 90 : fKS(ks), fShininess(shininess) {}
80 SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight, cons t SkPoint3& lightColor) const { 91 SkPMColor light(const SkPoint3& normal, const SkPoint3& surfaceTolight, cons t SkPoint3& lightColor) const {
81 SkPoint3 halfDir(surfaceTolight); 92 SkPoint3 halfDir(surfaceTolight);
82 halfDir.fZ += SK_Scalar1; // eye position is always (0, 0, 1) 93 halfDir.fZ += SK_Scalar1; // eye position is always (0, 0, 1)
83 halfDir.normalize(); 94 halfDir.normalize();
84 SkScalar colorScale = SkScalarMul(fKS, 95 SkScalar colorScale = SkScalarMul(fKS,
85 SkScalarPow(normal.dot(halfDir), fShininess)); 96 SkScalarPow(normal.dot(halfDir), fShininess));
86 colorScale = SkScalarClampMax(colorScale, SK_Scalar1); 97 colorScale = SkScalarClampMax(colorScale, SK_Scalar1);
87 SkPoint3 color(lightColor * colorScale); 98 SkPoint3 color(lightColor * colorScale);
88 return SkPackARGB32(SkScalarFloorToInt(color.maxComponent()), 99 U8CPU r(0), g(0), b(0);
89 SkScalarFloorToInt(color.fX), 100 getClampedColor(color, r, g, b);
90 SkScalarFloorToInt(color.fY), 101 return SkPackARGB32(r > g ? (r > b ? r : b) : (g > b ? g : b), r, g, b);
Stephen White 2013/08/21 20:49:26 Could we not simply clamp the alpha after doing ma
sugoi 2013/08/21 21:12:09 Sure, I'll just add the clamping code here.
sugoi1 2013/08/22 15:41:00 Done.
91 SkScalarFloorToInt(color.fZ));
92 } 102 }
93 private: 103 private:
94 SkScalar fKS; 104 SkScalar fKS;
95 SkScalar fShininess; 105 SkScalar fShininess;
96 }; 106 };
97 107
98 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) {
99 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);
100 } 110 }
101 111
(...skipping 552 matching lines...) Expand 10 before | Expand all | Expand 10 after
654 }; 664 };
655 665
656 /////////////////////////////////////////////////////////////////////////////// 666 ///////////////////////////////////////////////////////////////////////////////
657 667
658 class SkSpotLight : public SkLight { 668 class SkSpotLight : public SkLight {
659 public: 669 public:
660 SkSpotLight(const SkPoint3& location, const SkPoint3& target, SkScalar specu larExponent, SkScalar cutoffAngle, SkColor color) 670 SkSpotLight(const SkPoint3& location, const SkPoint3& target, SkScalar specu larExponent, SkScalar cutoffAngle, SkColor color)
661 : INHERITED(color), 671 : INHERITED(color),
662 fLocation(location), 672 fLocation(location),
663 fTarget(target), 673 fTarget(target),
664 fSpecularExponent(specularExponent) 674 fSpecularExponent(SkScalarPin(specularExponent, kSpecularExponentMin, kSp ecularExponentMax))
665 { 675 {
666 fS = target - location; 676 fS = target - location;
667 fS.normalize(); 677 fS.normalize();
668 fCosOuterConeAngle = SkScalarCos(SkDegreesToRadians(cutoffAngle)); 678 fCosOuterConeAngle = SkScalarCos(SkDegreesToRadians(cutoffAngle));
669 const SkScalar antiAliasThreshold = SkFloatToScalar(0.016f); 679 const SkScalar antiAliasThreshold = SkFloatToScalar(0.016f);
670 fCosInnerConeAngle = fCosOuterConeAngle + antiAliasThreshold; 680 fCosInnerConeAngle = fCosOuterConeAngle + antiAliasThreshold;
671 fConeScale = SkScalarInvert(antiAliasThreshold); 681 fConeScale = SkScalarInvert(antiAliasThreshold);
672 } 682 }
673 683
674 SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const { 684 SkPoint3 surfaceToLight(int x, int y, int z, SkScalar surfaceScale) const {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
738 748
739 const SkSpotLight& o = static_cast<const SkSpotLight&>(other); 749 const SkSpotLight& o = static_cast<const SkSpotLight&>(other);
740 return INHERITED::isEqual(other) && 750 return INHERITED::isEqual(other) &&
741 fLocation == o.fLocation && 751 fLocation == o.fLocation &&
742 fTarget == o.fTarget && 752 fTarget == o.fTarget &&
743 fSpecularExponent == o.fSpecularExponent && 753 fSpecularExponent == o.fSpecularExponent &&
744 fCosOuterConeAngle == o.fCosOuterConeAngle; 754 fCosOuterConeAngle == o.fCosOuterConeAngle;
745 } 755 }
746 756
747 private: 757 private:
758 // According to the spec, the specular term should be in the range [1, 128] :
759 // http://www.w3.org/TR/SVG/filters.html#feSpecularLightingSpecularExponentA ttribute
760 static const SkScalar kSpecularExponentMin = SkFloatToScalar(1.0f);
761 static const SkScalar kSpecularExponentMax = SkFloatToScalar(128.0f);
762
748 typedef SkLight INHERITED; 763 typedef SkLight INHERITED;
749 SkPoint3 fLocation; 764 SkPoint3 fLocation;
750 SkPoint3 fTarget; 765 SkPoint3 fTarget;
751 SkScalar fSpecularExponent; 766 SkScalar fSpecularExponent;
752 SkScalar fCosOuterConeAngle; 767 SkScalar fCosOuterConeAngle;
753 SkScalar fCosInnerConeAngle; 768 SkScalar fCosInnerConeAngle;
754 SkScalar fConeScale; 769 SkScalar fConeScale;
755 SkPoint3 fS; 770 SkPoint3 fS;
756 }; 771 };
757 772
(...skipping 786 matching lines...) Expand 10 before | Expand all | Expand 10 after
1544 1559
1545 #endif 1560 #endif
1546 1561
1547 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) 1562 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter)
1548 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) 1563 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter)
1549 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) 1564 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter)
1550 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDistantLight) 1565 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDistantLight)
1551 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPointLight) 1566 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPointLight)
1552 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpotLight) 1567 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpotLight)
1553 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END 1568 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
OLDNEW
« src/effects/SkBicubicImageFilter.cpp ('K') | « src/effects/SkBicubicImageFilter.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698