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