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 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 | 258 |
259 void writePoint3(const SkPoint3& point, SkWriteBuffer& buffer) { | 259 void writePoint3(const SkPoint3& point, SkWriteBuffer& buffer) { |
260 buffer.writeScalar(point.fX); | 260 buffer.writeScalar(point.fX); |
261 buffer.writeScalar(point.fY); | 261 buffer.writeScalar(point.fY); |
262 buffer.writeScalar(point.fZ); | 262 buffer.writeScalar(point.fZ); |
263 }; | 263 }; |
264 | 264 |
265 class SkDiffuseLightingImageFilter : public SkLightingImageFilter { | 265 class SkDiffuseLightingImageFilter : public SkLightingImageFilter { |
266 public: | 266 public: |
267 static SkImageFilter* Create(SkLight* light, SkScalar surfaceScale, SkScalar
kd, SkImageFilter*, | 267 static SkImageFilter* Create(SkLight* light, SkScalar surfaceScale, SkScalar
kd, SkImageFilter*, |
268 const CropRect*); | 268 const CropRect*, uint32_t uniqueID = 0); |
269 | 269 |
270 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDiffuseLightingImageFi
lter) | 270 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDiffuseLightingImageFi
lter) |
271 SkScalar kd() const { return fKD; } | 271 SkScalar kd() const { return fKD; } |
272 | 272 |
273 protected: | 273 protected: |
274 SkDiffuseLightingImageFilter(SkLight* light, SkScalar surfaceScale, | 274 SkDiffuseLightingImageFilter(SkLight* light, SkScalar surfaceScale, |
275 SkScalar kd, SkImageFilter* input, const CropRe
ct* cropRect); | 275 SkScalar kd, SkImageFilter* input, const CropRe
ct* cropRect, |
| 276 uint32_t uniqueID); |
276 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING | 277 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
277 explicit SkDiffuseLightingImageFilter(SkReadBuffer& buffer); | 278 explicit SkDiffuseLightingImageFilter(SkReadBuffer& buffer); |
278 #endif | 279 #endif |
279 virtual void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE; | 280 virtual void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE; |
280 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, | 281 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, |
281 SkBitmap* result, SkIPoint* offset) const SK_OVER
RIDE; | 282 SkBitmap* result, SkIPoint* offset) const SK_OVER
RIDE; |
282 #if SK_SUPPORT_GPU | 283 #if SK_SUPPORT_GPU |
283 virtual bool asNewEffect(GrEffect** effect, GrTexture*, const SkMatrix& matr
ix, | 284 virtual bool asNewEffect(GrEffect** effect, GrTexture*, const SkMatrix& matr
ix, |
284 const SkIRect& bounds) const SK_OVERRIDE; | 285 const SkIRect& bounds) const SK_OVERRIDE; |
285 #endif | 286 #endif |
286 | 287 |
287 private: | 288 private: |
288 friend class SkLightingImageFilter; | 289 friend class SkLightingImageFilter; |
289 typedef SkLightingImageFilter INHERITED; | 290 typedef SkLightingImageFilter INHERITED; |
290 SkScalar fKD; | 291 SkScalar fKD; |
291 }; | 292 }; |
292 | 293 |
293 class SkSpecularLightingImageFilter : public SkLightingImageFilter { | 294 class SkSpecularLightingImageFilter : public SkLightingImageFilter { |
294 public: | 295 public: |
295 static SkImageFilter* Create(SkLight* light, SkScalar surfaceScale, | 296 static SkImageFilter* Create(SkLight* light, SkScalar surfaceScale, |
296 SkScalar ks, SkScalar shininess, SkImageFilter*
, const CropRect*); | 297 SkScalar ks, SkScalar shininess, SkImageFilter*
, const CropRect*, uint32_t uniqueID = 0); |
297 | 298 |
298 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSpecularLightingImageF
ilter) | 299 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSpecularLightingImageF
ilter) |
299 | 300 |
300 SkScalar ks() const { return fKS; } | 301 SkScalar ks() const { return fKS; } |
301 SkScalar shininess() const { return fShininess; } | 302 SkScalar shininess() const { return fShininess; } |
302 | 303 |
303 protected: | 304 protected: |
304 SkSpecularLightingImageFilter(SkLight* light, SkScalar surfaceScale, SkScala
r ks, | 305 SkSpecularLightingImageFilter(SkLight* light, SkScalar surfaceScale, SkScala
r ks, |
305 SkScalar shininess, SkImageFilter* input, cons
t CropRect*); | 306 SkScalar shininess, SkImageFilter* input, cons
t CropRect*, |
| 307 uint32_t uniqueID); |
306 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING | 308 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
307 explicit SkSpecularLightingImageFilter(SkReadBuffer& buffer); | 309 explicit SkSpecularLightingImageFilter(SkReadBuffer& buffer); |
308 #endif | 310 #endif |
309 virtual void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE; | 311 virtual void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE; |
310 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, | 312 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, |
311 SkBitmap* result, SkIPoint* offset) const SK_OVER
RIDE; | 313 SkBitmap* result, SkIPoint* offset) const SK_OVER
RIDE; |
312 #if SK_SUPPORT_GPU | 314 #if SK_SUPPORT_GPU |
313 virtual bool asNewEffect(GrEffect** effect, GrTexture*, const SkMatrix& matr
ix, | 315 virtual bool asNewEffect(GrEffect** effect, GrTexture*, const SkMatrix& matr
ix, |
314 const SkIRect& bounds) const SK_OVERRIDE; | 316 const SkIRect& bounds) const SK_OVERRIDE; |
315 #endif | 317 #endif |
(...skipping 521 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
837 case SkLight::kSpot_LightType: return SkNEW_ARGS(SkSpotLight, (buffer
)); | 839 case SkLight::kSpot_LightType: return SkNEW_ARGS(SkSpotLight, (buffer
)); |
838 default: | 840 default: |
839 SkDEBUGFAIL("Unknown LightType."); | 841 SkDEBUGFAIL("Unknown LightType."); |
840 buffer.validate(false); | 842 buffer.validate(false); |
841 return NULL; | 843 return NULL; |
842 } | 844 } |
843 } | 845 } |
844 /////////////////////////////////////////////////////////////////////////////// | 846 /////////////////////////////////////////////////////////////////////////////// |
845 | 847 |
846 SkLightingImageFilter::SkLightingImageFilter(SkLight* light, SkScalar surfaceSca
le, | 848 SkLightingImageFilter::SkLightingImageFilter(SkLight* light, SkScalar surfaceSca
le, |
847 SkImageFilter* input, const CropRec
t* cropRect) | 849 SkImageFilter* input, const CropRec
t* cropRect, |
848 : INHERITED(1, &input, cropRect) | 850 uint32_t uniqueID) |
| 851 : INHERITED(1, &input, cropRect, uniqueID) |
849 , fLight(SkRef(light)) | 852 , fLight(SkRef(light)) |
850 , fSurfaceScale(surfaceScale / 255) | 853 , fSurfaceScale(surfaceScale / 255) |
851 {} | 854 {} |
852 | 855 |
853 SkImageFilter* SkLightingImageFilter::CreateDistantLitDiffuse(const SkPoint3& di
rection, | 856 SkImageFilter* SkLightingImageFilter::CreateDistantLitDiffuse(const SkPoint3& di
rection, |
854 SkColor lightColor
, | 857 SkColor lightColor
, |
855 SkScalar surfaceSc
ale, | 858 SkScalar surfaceSc
ale, |
856 SkScalar kd, | 859 SkScalar kd, |
857 SkImageFilter* inp
ut, | 860 SkImageFilter* inp
ut, |
858 const CropRect* cr
opRect) { | 861 const CropRect* cr
opRect) { |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
934 | 937 |
935 void SkLightingImageFilter::flatten(SkWriteBuffer& buffer) const { | 938 void SkLightingImageFilter::flatten(SkWriteBuffer& buffer) const { |
936 this->INHERITED::flatten(buffer); | 939 this->INHERITED::flatten(buffer); |
937 fLight->flattenLight(buffer); | 940 fLight->flattenLight(buffer); |
938 buffer.writeScalar(fSurfaceScale * 255); | 941 buffer.writeScalar(fSurfaceScale * 255); |
939 } | 942 } |
940 | 943 |
941 /////////////////////////////////////////////////////////////////////////////// | 944 /////////////////////////////////////////////////////////////////////////////// |
942 | 945 |
943 SkImageFilter* SkDiffuseLightingImageFilter::Create(SkLight* light, SkScalar sur
faceScale, | 946 SkImageFilter* SkDiffuseLightingImageFilter::Create(SkLight* light, SkScalar sur
faceScale, |
944 SkScalar kd, SkImageFilter* input, const Cro
pRect* cropRect) { | 947 SkScalar kd, SkImageFilter* input, const Cro
pRect* cropRect, uint32_t uniqueID) { |
945 if (NULL == light) { | 948 if (NULL == light) { |
946 return NULL; | 949 return NULL; |
947 } | 950 } |
948 if (!SkScalarIsFinite(surfaceScale) || !SkScalarIsFinite(kd)) { | 951 if (!SkScalarIsFinite(surfaceScale) || !SkScalarIsFinite(kd)) { |
949 return NULL; | 952 return NULL; |
950 } | 953 } |
951 // According to the spec, kd can be any non-negative number : | 954 // According to the spec, kd can be any non-negative number : |
952 // http://www.w3.org/TR/SVG/filters.html#feDiffuseLightingElement | 955 // http://www.w3.org/TR/SVG/filters.html#feDiffuseLightingElement |
953 if (kd < 0) { | 956 if (kd < 0) { |
954 return NULL; | 957 return NULL; |
955 } | 958 } |
956 return SkNEW_ARGS(SkDiffuseLightingImageFilter, (light, surfaceScale, kd, in
put, cropRect)); | 959 return SkNEW_ARGS(SkDiffuseLightingImageFilter, (light, surfaceScale, kd, in
put, cropRect, uniqueID)); |
957 } | 960 } |
958 | 961 |
959 SkDiffuseLightingImageFilter::SkDiffuseLightingImageFilter(SkLight* light, SkSca
lar surfaceScale, SkScalar kd, SkImageFilter* input, const CropRect* cropRect =
NULL) | 962 SkDiffuseLightingImageFilter::SkDiffuseLightingImageFilter(SkLight* light, SkSca
lar surfaceScale, SkScalar kd, SkImageFilter* input, const CropRect* cropRect, u
int32_t uniqueID) |
960 : SkLightingImageFilter(light, surfaceScale, input, cropRect), | 963 : SkLightingImageFilter(light, surfaceScale, input, cropRect, uniqueID), |
961 fKD(kd) | 964 fKD(kd) |
962 { | 965 { |
963 } | 966 } |
964 | 967 |
965 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING | 968 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
966 SkDiffuseLightingImageFilter::SkDiffuseLightingImageFilter(SkReadBuffer& buffer) | 969 SkDiffuseLightingImageFilter::SkDiffuseLightingImageFilter(SkReadBuffer& buffer) |
967 : INHERITED(buffer) | 970 : INHERITED(buffer) |
968 { | 971 { |
969 fKD = buffer.readScalar(); | 972 fKD = buffer.readScalar(); |
970 buffer.validate(SkScalarIsFinite(fKD) && (fKD >= 0)); | 973 buffer.validate(SkScalarIsFinite(fKD) && (fKD >= 0)); |
971 } | 974 } |
972 #endif | 975 #endif |
973 | 976 |
974 SkFlattenable* SkDiffuseLightingImageFilter::CreateProc(SkReadBuffer& buffer) { | 977 SkFlattenable* SkDiffuseLightingImageFilter::CreateProc(SkReadBuffer& buffer) { |
975 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); | 978 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
976 SkAutoTUnref<SkLight> light(SkLight::UnflattenLight(buffer)); | 979 SkAutoTUnref<SkLight> light(SkLight::UnflattenLight(buffer)); |
977 SkScalar surfaceScale = buffer.readScalar(); | 980 SkScalar surfaceScale = buffer.readScalar(); |
978 SkScalar kd = buffer.readScalar(); | 981 SkScalar kd = buffer.readScalar(); |
979 return Create(light, surfaceScale, kd, common.getInput(0), &common.cropRect(
)); | 982 return Create(light, surfaceScale, kd, common.getInput(0), &common.cropRect(
), common.uniqueID()); |
980 } | 983 } |
981 | 984 |
982 void SkDiffuseLightingImageFilter::flatten(SkWriteBuffer& buffer) const { | 985 void SkDiffuseLightingImageFilter::flatten(SkWriteBuffer& buffer) const { |
983 this->INHERITED::flatten(buffer); | 986 this->INHERITED::flatten(buffer); |
984 buffer.writeScalar(fKD); | 987 buffer.writeScalar(fKD); |
985 } | 988 } |
986 | 989 |
987 bool SkDiffuseLightingImageFilter::onFilterImage(Proxy* proxy, | 990 bool SkDiffuseLightingImageFilter::onFilterImage(Proxy* proxy, |
988 const SkBitmap& source, | 991 const SkBitmap& source, |
989 const Context& ctx, | 992 const Context& ctx, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1045 SkScalar scale = SkScalarMul(surfaceScale(), SkIntToScalar(255)); | 1048 SkScalar scale = SkScalarMul(surfaceScale(), SkIntToScalar(255)); |
1046 *effect = GrDiffuseLightingEffect::Create(texture, light(), scale, matri
x, kd()); | 1049 *effect = GrDiffuseLightingEffect::Create(texture, light(), scale, matri
x, kd()); |
1047 } | 1050 } |
1048 return true; | 1051 return true; |
1049 } | 1052 } |
1050 #endif | 1053 #endif |
1051 | 1054 |
1052 /////////////////////////////////////////////////////////////////////////////// | 1055 /////////////////////////////////////////////////////////////////////////////// |
1053 | 1056 |
1054 SkImageFilter* SkSpecularLightingImageFilter::Create(SkLight* light, SkScalar su
rfaceScale, | 1057 SkImageFilter* SkSpecularLightingImageFilter::Create(SkLight* light, SkScalar su
rfaceScale, |
1055 SkScalar ks, SkScalar shininess, SkImageFilter* input, const Cro
pRect* cropRect) { | 1058 SkScalar ks, SkScalar shininess, SkImageFilter* input, const Cro
pRect* cropRect, uint32_t uniqueID) { |
1056 if (NULL == light) { | 1059 if (NULL == light) { |
1057 return NULL; | 1060 return NULL; |
1058 } | 1061 } |
1059 if (!SkScalarIsFinite(surfaceScale) || !SkScalarIsFinite(ks) || !SkScalarIsF
inite(shininess)) { | 1062 if (!SkScalarIsFinite(surfaceScale) || !SkScalarIsFinite(ks) || !SkScalarIsF
inite(shininess)) { |
1060 return NULL; | 1063 return NULL; |
1061 } | 1064 } |
1062 // According to the spec, ks can be any non-negative number : | 1065 // According to the spec, ks can be any non-negative number : |
1063 // http://www.w3.org/TR/SVG/filters.html#feSpecularLightingElement | 1066 // http://www.w3.org/TR/SVG/filters.html#feSpecularLightingElement |
1064 if (ks < 0) { | 1067 if (ks < 0) { |
1065 return NULL; | 1068 return NULL; |
1066 } | 1069 } |
1067 return SkNEW_ARGS(SkSpecularLightingImageFilter, | 1070 return SkNEW_ARGS(SkSpecularLightingImageFilter, |
1068 (light, surfaceScale, ks, shininess, input, cropRect)); | 1071 (light, surfaceScale, ks, shininess, input, cropRect, uniq
ueID)); |
1069 } | 1072 } |
1070 | 1073 |
1071 SkSpecularLightingImageFilter::SkSpecularLightingImageFilter(SkLight* light, SkS
calar surfaceScale, SkScalar ks, SkScalar shininess, SkImageFilter* input, const
CropRect* cropRect) | 1074 SkSpecularLightingImageFilter::SkSpecularLightingImageFilter(SkLight* light, SkS
calar surfaceScale, SkScalar ks, SkScalar shininess, SkImageFilter* input, const
CropRect* cropRect, uint32_t uniqueID) |
1072 : SkLightingImageFilter(light, surfaceScale, input, cropRect), | 1075 : SkLightingImageFilter(light, surfaceScale, input, cropRect, uniqueID), |
1073 fKS(ks), | 1076 fKS(ks), |
1074 fShininess(shininess) | 1077 fShininess(shininess) |
1075 { | 1078 { |
1076 } | 1079 } |
1077 | 1080 |
1078 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING | 1081 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
1079 SkSpecularLightingImageFilter::SkSpecularLightingImageFilter(SkReadBuffer& buffe
r) | 1082 SkSpecularLightingImageFilter::SkSpecularLightingImageFilter(SkReadBuffer& buffe
r) |
1080 : INHERITED(buffer) | 1083 : INHERITED(buffer) |
1081 { | 1084 { |
1082 fKS = buffer.readScalar(); | 1085 fKS = buffer.readScalar(); |
1083 fShininess = buffer.readScalar(); | 1086 fShininess = buffer.readScalar(); |
1084 buffer.validate(SkScalarIsFinite(fKS) && (fKS >= 0) && | 1087 buffer.validate(SkScalarIsFinite(fKS) && (fKS >= 0) && |
1085 SkScalarIsFinite(fShininess)); | 1088 SkScalarIsFinite(fShininess)); |
1086 } | 1089 } |
1087 #endif | 1090 #endif |
1088 | 1091 |
1089 SkFlattenable* SkSpecularLightingImageFilter::CreateProc(SkReadBuffer& buffer) { | 1092 SkFlattenable* SkSpecularLightingImageFilter::CreateProc(SkReadBuffer& buffer) { |
1090 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); | 1093 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
1091 SkAutoTUnref<SkLight> light(SkLight::UnflattenLight(buffer)); | 1094 SkAutoTUnref<SkLight> light(SkLight::UnflattenLight(buffer)); |
1092 SkScalar surfaceScale = buffer.readScalar(); | 1095 SkScalar surfaceScale = buffer.readScalar(); |
1093 SkScalar ks = buffer.readScalar(); | 1096 SkScalar ks = buffer.readScalar(); |
1094 SkScalar shine = buffer.readScalar(); | 1097 SkScalar shine = buffer.readScalar(); |
1095 return Create(light, surfaceScale, ks, shine, common.getInput(0), &common.cr
opRect()); | 1098 return Create(light, surfaceScale, ks, shine, common.getInput(0), &common.cr
opRect(), common.uniqueID()); |
1096 } | 1099 } |
1097 | 1100 |
1098 void SkSpecularLightingImageFilter::flatten(SkWriteBuffer& buffer) const { | 1101 void SkSpecularLightingImageFilter::flatten(SkWriteBuffer& buffer) const { |
1099 this->INHERITED::flatten(buffer); | 1102 this->INHERITED::flatten(buffer); |
1100 buffer.writeScalar(fKS); | 1103 buffer.writeScalar(fKS); |
1101 buffer.writeScalar(fShininess); | 1104 buffer.writeScalar(fShininess); |
1102 } | 1105 } |
1103 | 1106 |
1104 bool SkSpecularLightingImageFilter::onFilterImage(Proxy* proxy, | 1107 bool SkSpecularLightingImageFilter::onFilterImage(Proxy* proxy, |
1105 const SkBitmap& source, | 1108 const SkBitmap& source, |
(...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1700 | 1703 |
1701 fsBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); | 1704 fsBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); |
1702 } | 1705 } |
1703 | 1706 |
1704 #endif | 1707 #endif |
1705 | 1708 |
1706 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) | 1709 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) |
1707 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) | 1710 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) |
1708 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) | 1711 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) |
1709 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 1712 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
OLD | NEW |