| 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 "SkImagePriv.h" |
| 12 #include "SkImage_Base.h" |
| 11 #include "SkReadBuffer.h" | 13 #include "SkReadBuffer.h" |
| 14 #include "SkReadBuffer.h" |
| 15 #include "SkTypes.h" |
| 12 #include "SkWriteBuffer.h" | 16 #include "SkWriteBuffer.h" |
| 13 #include "SkReadBuffer.h" | |
| 14 #include "SkWriteBuffer.h" | 17 #include "SkWriteBuffer.h" |
| 15 #include "SkTypes.h" | |
| 16 | |
| 17 #if SK_SUPPORT_GPU | 18 #if SK_SUPPORT_GPU |
| 18 #include "GrFragmentProcessor.h" | 19 #include "GrFragmentProcessor.h" |
| 19 #include "GrInvariantOutput.h" | 20 #include "GrInvariantOutput.h" |
| 20 #include "effects/GrSingleTextureEffect.h" | 21 #include "effects/GrSingleTextureEffect.h" |
| 21 #include "gl/GrGLProcessor.h" | 22 #include "gl/GrGLProcessor.h" |
| 22 #include "gl/builders/GrGLProgramBuilder.h" | 23 #include "gl/builders/GrGLProgramBuilder.h" |
| 23 | 24 |
| 24 class GrGLDiffuseLightingEffect; | 25 class GrGLDiffuseLightingEffect; |
| 25 class GrGLSpecularLightingEffect; | 26 class GrGLSpecularLightingEffect; |
| 26 | 27 |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 const CropRect*); | 284 const CropRect*); |
| 284 | 285 |
| 285 SK_TO_STRING_OVERRIDE() | 286 SK_TO_STRING_OVERRIDE() |
| 286 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDiffuseLightingImageFi
lter) | 287 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkDiffuseLightingImageFi
lter) |
| 287 SkScalar kd() const { return fKD; } | 288 SkScalar kd() const { return fKD; } |
| 288 | 289 |
| 289 protected: | 290 protected: |
| 290 SkDiffuseLightingImageFilter(SkLight* light, SkScalar surfaceScale, | 291 SkDiffuseLightingImageFilter(SkLight* light, SkScalar surfaceScale, |
| 291 SkScalar kd, SkImageFilter* input, const CropRe
ct* cropRect); | 292 SkScalar kd, SkImageFilter* input, const CropRe
ct* cropRect); |
| 292 void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE; | 293 void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE; |
| 293 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, | 294 virtual bool onFilterImage(Proxy*, const SkImage* src, const Context&, |
| 294 SkBitmap* result, SkIPoint* offset) const SK_OVER
RIDE; | 295 SkAutoTUnref<const SkImage>&, SkIPoint* offset) c
onst SK_OVERRIDE; |
| 295 #if SK_SUPPORT_GPU | 296 #if SK_SUPPORT_GPU |
| 296 virtual bool asFragmentProcessor(GrFragmentProcessor**, GrTexture*, const Sk
Matrix&, | 297 virtual bool asFragmentProcessor(GrFragmentProcessor**, GrTexture*, const Sk
Matrix&, |
| 297 const SkIRect& bounds) const SK_OVERRIDE; | 298 const SkIRect& bounds) const SK_OVERRIDE; |
| 298 #endif | 299 #endif |
| 299 | 300 |
| 300 private: | 301 private: |
| 301 friend class SkLightingImageFilter; | 302 friend class SkLightingImageFilter; |
| 302 typedef SkLightingImageFilter INHERITED; | 303 typedef SkLightingImageFilter INHERITED; |
| 303 SkScalar fKD; | 304 SkScalar fKD; |
| 304 }; | 305 }; |
| 305 | 306 |
| 306 class SkSpecularLightingImageFilter : public SkLightingImageFilter { | 307 class SkSpecularLightingImageFilter : public SkLightingImageFilter { |
| 307 public: | 308 public: |
| 308 static SkImageFilter* Create(SkLight* light, SkScalar surfaceScale, | 309 static SkImageFilter* Create(SkLight* light, SkScalar surfaceScale, |
| 309 SkScalar ks, SkScalar shininess, SkImageFilter*
, const CropRect*); | 310 SkScalar ks, SkScalar shininess, SkImageFilter*
, const CropRect*); |
| 310 | 311 |
| 311 SK_TO_STRING_OVERRIDE() | 312 SK_TO_STRING_OVERRIDE() |
| 312 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSpecularLightingImageF
ilter) | 313 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkSpecularLightingImageF
ilter) |
| 313 | 314 |
| 314 SkScalar ks() const { return fKS; } | 315 SkScalar ks() const { return fKS; } |
| 315 SkScalar shininess() const { return fShininess; } | 316 SkScalar shininess() const { return fShininess; } |
| 316 | 317 |
| 317 protected: | 318 protected: |
| 318 SkSpecularLightingImageFilter(SkLight* light, SkScalar surfaceScale, SkScala
r ks, | 319 SkSpecularLightingImageFilter(SkLight* light, SkScalar surfaceScale, SkScala
r ks, |
| 319 SkScalar shininess, SkImageFilter* input, cons
t CropRect*); | 320 SkScalar shininess, SkImageFilter* input, cons
t CropRect*); |
| 320 void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE; | 321 void flatten(SkWriteBuffer& buffer) const SK_OVERRIDE; |
| 321 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, | 322 virtual bool onFilterImage(Proxy*, const SkImage* src, const Context&, |
| 322 SkBitmap* result, SkIPoint* offset) const SK_OVER
RIDE; | 323 SkAutoTUnref<const SkImage>& result, SkIPoint* of
fset) const SK_OVERRIDE; |
| 323 #if SK_SUPPORT_GPU | 324 #if SK_SUPPORT_GPU |
| 324 virtual bool asFragmentProcessor(GrFragmentProcessor**, GrTexture*, const Sk
Matrix&, | 325 virtual bool asFragmentProcessor(GrFragmentProcessor**, GrTexture*, const Sk
Matrix&, |
| 325 const SkIRect& bounds) const SK_OVERRIDE; | 326 const SkIRect& bounds) const SK_OVERRIDE; |
| 326 #endif | 327 #endif |
| 327 | 328 |
| 328 private: | 329 private: |
| 329 SkScalar fKS; | 330 SkScalar fKS; |
| 330 SkScalar fShininess; | 331 SkScalar fShininess; |
| 331 friend class SkLightingImageFilter; | 332 friend class SkLightingImageFilter; |
| 332 typedef SkLightingImageFilter INHERITED; | 333 typedef SkLightingImageFilter INHERITED; |
| (...skipping 640 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 973 SkScalar kd = buffer.readScalar(); | 974 SkScalar kd = buffer.readScalar(); |
| 974 return Create(light, surfaceScale, kd, common.getInput(0), &common.cropRect(
)); | 975 return Create(light, surfaceScale, kd, common.getInput(0), &common.cropRect(
)); |
| 975 } | 976 } |
| 976 | 977 |
| 977 void SkDiffuseLightingImageFilter::flatten(SkWriteBuffer& buffer) const { | 978 void SkDiffuseLightingImageFilter::flatten(SkWriteBuffer& buffer) const { |
| 978 this->INHERITED::flatten(buffer); | 979 this->INHERITED::flatten(buffer); |
| 979 buffer.writeScalar(fKD); | 980 buffer.writeScalar(fKD); |
| 980 } | 981 } |
| 981 | 982 |
| 982 bool SkDiffuseLightingImageFilter::onFilterImage(Proxy* proxy, | 983 bool SkDiffuseLightingImageFilter::onFilterImage(Proxy* proxy, |
| 983 const SkBitmap& source, | 984 const SkImage* source, |
| 984 const Context& ctx, | 985 const Context& ctx, |
| 985 SkBitmap* dst, | 986 SkAutoTUnref<const SkImage>& ds
t, |
| 986 SkIPoint* offset) const { | 987 SkIPoint* offset) const { |
| 987 SkImageFilter* input = getInput(0); | 988 SkImageFilter* input = getInput(0); |
| 988 SkBitmap src = source; | 989 SkAutoTUnref<const SkImage> src(SkRef(source)); |
| 989 SkIPoint srcOffset = SkIPoint::Make(0, 0); | 990 SkIPoint srcOffset = SkIPoint::Make(0, 0); |
| 990 if (input && !input->filterImage(proxy, source, ctx, &src, &srcOffset)) { | 991 if (input && !input->filterImage(proxy, source, ctx, src, &srcOffset)) { |
| 991 return false; | |
| 992 } | |
| 993 | |
| 994 if (src.colorType() != kN32_SkColorType) { | |
| 995 return false; | 992 return false; |
| 996 } | 993 } |
| 997 SkIRect bounds; | 994 SkIRect bounds; |
| 998 if (!this->applyCropRect(ctx, proxy, src, &srcOffset, &bounds, &src)) { | 995 if (!this->applyCropRect(ctx, proxy, src, &srcOffset, &bounds, src)) { |
| 999 return false; | 996 return false; |
| 1000 } | 997 } |
| 1001 | 998 |
| 1002 if (bounds.width() < 2 || bounds.height() < 2) { | 999 if (bounds.width() < 2 || bounds.height() < 2) { |
| 1003 return false; | 1000 return false; |
| 1004 } | 1001 } |
| 1005 | 1002 SkBitmap srcBitmap; |
| 1006 SkAutoLockPixels alp(src); | 1003 SkAutoAdoptImageAsN32Bitmap aai(src, &srcBitmap); |
| 1007 if (!src.getPixels()) { | 1004 if (NULL == srcBitmap.getPixels()) { |
| 1008 return false; | 1005 return false; |
| 1009 } | 1006 } |
| 1010 | 1007 |
| 1011 if (!dst->tryAllocPixels(src.info().makeWH(bounds.width(), bounds.height()))
) { | 1008 SkBitmap dstBitmap; |
| 1009 if (!dstBitmap.tryAllocPixels(srcBitmap.info().makeWH(bounds.width(), bounds
.height()))) { |
| 1012 return false; | 1010 return false; |
| 1013 } | 1011 } |
| 1014 | 1012 |
| 1015 SkAutoTUnref<SkLight> transformedLight(light()->transform(ctx.ctm())); | 1013 SkAutoTUnref<SkLight> transformedLight(light()->transform(ctx.ctm())); |
| 1016 | 1014 |
| 1017 DiffuseLightingType lightingType(fKD); | 1015 DiffuseLightingType lightingType(fKD); |
| 1018 offset->fX = bounds.left(); | 1016 int32_t resultX = bounds.left(); |
| 1019 offset->fY = bounds.top(); | 1017 int32_t resultY = bounds.top(); |
| 1018 |
| 1020 bounds.offset(-srcOffset); | 1019 bounds.offset(-srcOffset); |
| 1021 switch (transformedLight->type()) { | 1020 switch (transformedLight->type()) { |
| 1022 case SkLight::kDistant_LightType: | 1021 case SkLight::kDistant_LightType: |
| 1023 lightBitmap<DiffuseLightingType, SkDistantLight>(lightingType, trans
formedLight, src, dst, surfaceScale(), bounds); | 1022 lightBitmap<DiffuseLightingType, SkDistantLight>(lightingType, trans
formedLight, |
| 1023 srcBitmap, &dstBitm
ap, surfaceScale(), |
| 1024 bounds); |
| 1024 break; | 1025 break; |
| 1025 case SkLight::kPoint_LightType: | 1026 case SkLight::kPoint_LightType: |
| 1026 lightBitmap<DiffuseLightingType, SkPointLight>(lightingType, transfo
rmedLight, src, dst, surfaceScale(), bounds); | 1027 lightBitmap<DiffuseLightingType, SkPointLight>(lightingType, transfo
rmedLight, |
| 1028 srcBitmap, &dstBitmap
, surfaceScale(), |
| 1029 bounds); |
| 1027 break; | 1030 break; |
| 1028 case SkLight::kSpot_LightType: | 1031 case SkLight::kSpot_LightType: |
| 1029 lightBitmap<DiffuseLightingType, SkSpotLight>(lightingType, transfor
medLight, src, dst, surfaceScale(), bounds); | 1032 lightBitmap<DiffuseLightingType, SkSpotLight>(lightingType, transfor
medLight, srcBitmap, |
| 1033 &dstBitmap, surfaceSca
le(), bounds); |
| 1030 break; | 1034 break; |
| 1031 } | 1035 } |
| 1032 | 1036 |
| 1037 srcBitmap = SkBitmap(); |
| 1038 |
| 1039 SkImage* image = SkNewImageFromBitmap(dstBitmap, NULL); |
| 1040 if (NULL == image) { |
| 1041 return false; |
| 1042 } |
| 1043 dst.reset(image); |
| 1044 offset->fX = resultX; |
| 1045 offset->fY = resultY; |
| 1033 return true; | 1046 return true; |
| 1034 } | 1047 } |
| 1035 | 1048 |
| 1036 #ifndef SK_IGNORE_TO_STRING | 1049 #ifndef SK_IGNORE_TO_STRING |
| 1037 void SkDiffuseLightingImageFilter::toString(SkString* str) const { | 1050 void SkDiffuseLightingImageFilter::toString(SkString* str) const { |
| 1038 str->appendf("SkDiffuseLightingImageFilter: ("); | 1051 str->appendf("SkDiffuseLightingImageFilter: ("); |
| 1039 str->appendf("kD: %f\n", fKD); | 1052 str->appendf("kD: %f\n", fKD); |
| 1040 str->append(")"); | 1053 str->append(")"); |
| 1041 } | 1054 } |
| 1042 #endif | 1055 #endif |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1089 return Create(light, surfaceScale, ks, shine, common.getInput(0), &common.cr
opRect()); | 1102 return Create(light, surfaceScale, ks, shine, common.getInput(0), &common.cr
opRect()); |
| 1090 } | 1103 } |
| 1091 | 1104 |
| 1092 void SkSpecularLightingImageFilter::flatten(SkWriteBuffer& buffer) const { | 1105 void SkSpecularLightingImageFilter::flatten(SkWriteBuffer& buffer) const { |
| 1093 this->INHERITED::flatten(buffer); | 1106 this->INHERITED::flatten(buffer); |
| 1094 buffer.writeScalar(fKS); | 1107 buffer.writeScalar(fKS); |
| 1095 buffer.writeScalar(fShininess); | 1108 buffer.writeScalar(fShininess); |
| 1096 } | 1109 } |
| 1097 | 1110 |
| 1098 bool SkSpecularLightingImageFilter::onFilterImage(Proxy* proxy, | 1111 bool SkSpecularLightingImageFilter::onFilterImage(Proxy* proxy, |
| 1099 const SkBitmap& source, | 1112 const SkImage* source, |
| 1100 const Context& ctx, | 1113 const Context& ctx, |
| 1101 SkBitmap* dst, | 1114 SkAutoTUnref<const SkImage>& d
st, |
| 1102 SkIPoint* offset) const { | 1115 SkIPoint* offset) const { |
| 1103 SkImageFilter* input = getInput(0); | 1116 SkImageFilter* input = getInput(0); |
| 1104 SkBitmap src = source; | 1117 SkAutoTUnref<const SkImage> src(SkRef(source)); |
| 1105 SkIPoint srcOffset = SkIPoint::Make(0, 0); | 1118 SkIPoint srcOffset = SkIPoint::Make(0, 0); |
| 1106 if (input && !input->filterImage(proxy, source, ctx, &src, &srcOffset)) { | 1119 if (input && !input->filterImage(proxy, source, ctx, src, &srcOffset)) { |
| 1120 return false; |
| 1121 } |
| 1122 SkIRect bounds; |
| 1123 if (!this->applyCropRect(ctx, proxy, src, &srcOffset, &bounds, src)) { |
| 1107 return false; | 1124 return false; |
| 1108 } | 1125 } |
| 1109 | 1126 |
| 1110 if (src.colorType() != kN32_SkColorType) { | |
| 1111 return false; | |
| 1112 } | |
| 1113 | |
| 1114 SkIRect bounds; | |
| 1115 if (!this->applyCropRect(ctx, proxy, src, &srcOffset, &bounds, &src)) { | |
| 1116 return false; | |
| 1117 } | |
| 1118 | |
| 1119 if (bounds.width() < 2 || bounds.height() < 2) { | 1127 if (bounds.width() < 2 || bounds.height() < 2) { |
| 1120 return false; | 1128 return false; |
| 1121 } | 1129 } |
| 1122 | 1130 |
| 1123 SkAutoLockPixels alp(src); | 1131 SkBitmap srcBitmap; |
| 1124 if (!src.getPixels()) { | 1132 SkAutoAdoptImageAsN32Bitmap aai(src, &srcBitmap); |
| 1133 if (NULL == srcBitmap.getPixels()) { |
| 1125 return false; | 1134 return false; |
| 1126 } | 1135 } |
| 1127 | 1136 |
| 1128 if (!dst->tryAllocPixels(src.info().makeWH(bounds.width(), bounds.height()))
) { | 1137 SkBitmap dstBitmap; |
| 1138 if (!dstBitmap.tryAllocPixels(srcBitmap.info().makeWH(bounds.width(), bounds
.height()))) { |
| 1129 return false; | 1139 return false; |
| 1130 } | 1140 } |
| 1131 | 1141 |
| 1132 SpecularLightingType lightingType(fKS, fShininess); | 1142 SpecularLightingType lightingType(fKS, fShininess); |
| 1133 offset->fX = bounds.left(); | 1143 int32_t resultX = bounds.left(); |
| 1134 offset->fY = bounds.top(); | 1144 int32_t resultY = bounds.top(); |
| 1145 |
| 1135 bounds.offset(-srcOffset); | 1146 bounds.offset(-srcOffset); |
| 1136 SkAutoTUnref<SkLight> transformedLight(light()->transform(ctx.ctm())); | 1147 SkAutoTUnref<SkLight> transformedLight(light()->transform(ctx.ctm())); |
| 1137 switch (transformedLight->type()) { | 1148 switch (transformedLight->type()) { |
| 1138 case SkLight::kDistant_LightType: | 1149 case SkLight::kDistant_LightType: |
| 1139 lightBitmap<SpecularLightingType, SkDistantLight>(lightingType, tran
sformedLight, src, dst, surfaceScale(), bounds); | 1150 lightBitmap<SpecularLightingType, SkDistantLight>(lightingType, tran
sformedLight, |
| 1151 srcBitmap, &dstBit
map, surfaceScale(), |
| 1152 bounds); |
| 1140 break; | 1153 break; |
| 1141 case SkLight::kPoint_LightType: | 1154 case SkLight::kPoint_LightType: |
| 1142 lightBitmap<SpecularLightingType, SkPointLight>(lightingType, transf
ormedLight, src, dst, surfaceScale(), bounds); | 1155 lightBitmap<SpecularLightingType, SkPointLight>(lightingType, transf
ormedLight, |
| 1156 srcBitmap, &dstBitma
p, surfaceScale(), |
| 1157 bounds); |
| 1143 break; | 1158 break; |
| 1144 case SkLight::kSpot_LightType: | 1159 case SkLight::kSpot_LightType: |
| 1145 lightBitmap<SpecularLightingType, SkSpotLight>(lightingType, transfo
rmedLight, src, dst, surfaceScale(), bounds); | 1160 lightBitmap<SpecularLightingType, SkSpotLight>(lightingType, transfo
rmedLight, |
| 1161 srcBitmap, &dstBitmap
, surfaceScale(), |
| 1162 bounds); |
| 1146 break; | 1163 break; |
| 1147 } | 1164 } |
| 1165 SkImage* image = SkNewImageFromBitmap(dstBitmap, NULL); |
| 1166 if (NULL == image) { |
| 1167 return false; |
| 1168 } |
| 1169 dst.reset(image); |
| 1170 offset->fX = resultX; |
| 1171 offset->fY = resultY; |
| 1148 return true; | 1172 return true; |
| 1149 } | 1173 } |
| 1150 | 1174 |
| 1151 #ifndef SK_IGNORE_TO_STRING | 1175 #ifndef SK_IGNORE_TO_STRING |
| 1152 void SkSpecularLightingImageFilter::toString(SkString* str) const { | 1176 void SkSpecularLightingImageFilter::toString(SkString* str) const { |
| 1153 str->appendf("SkSpecularLightingImageFilter: ("); | 1177 str->appendf("SkSpecularLightingImageFilter: ("); |
| 1154 str->appendf("kS: %f shininess: %f", fKS, fShininess); | 1178 str->appendf("kS: %f shininess: %f", fKS, fShininess); |
| 1155 str->append(")"); | 1179 str->append(")"); |
| 1156 } | 1180 } |
| 1157 #endif | 1181 #endif |
| (...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1712 | 1736 |
| 1713 fsBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); | 1737 fsBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); |
| 1714 } | 1738 } |
| 1715 | 1739 |
| 1716 #endif | 1740 #endif |
| 1717 | 1741 |
| 1718 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) | 1742 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) |
| 1719 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) | 1743 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) |
| 1720 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) | 1744 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) |
| 1721 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 1745 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| OLD | NEW |