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*, SkImage& src, const Context&, |
295 SkBitmap* result, SkIPoint* offset) const SK_OVER
RIDE; | 296 SkAutoTUnref<SkImage>&, SkIPoint* offset) const S
K_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*, SkImage& src, const Context&, |
325 SkBitmap* result, SkIPoint* offset) const SK_OVER
RIDE; | 326 SkAutoTUnref<SkImage>& result, SkIPoint* offset)
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 SkImage& source, |
988 const Context& ctx, | 989 const Context& ctx, |
989 SkBitmap* dst, | 990 SkAutoTUnref<SkImage>& dst, |
990 SkIPoint* offset) const { | 991 SkIPoint* offset) const { |
991 SkImageFilter* input = getInput(0); | 992 SkImageFilter* input = getInput(0); |
992 SkBitmap src = source; | 993 SkAutoTUnref<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 return false; |
996 } | 997 } |
997 | 998 #if 0 |
998 if (src.colorType() != kN32_SkColorType) { | 999 if (src.colorType() != kN32_SkColorType) { |
999 return false; | 1000 return false; |
1000 } | 1001 } |
| 1002 #endif |
1001 SkIRect bounds; | 1003 SkIRect bounds; |
1002 if (!this->applyCropRect(ctx, proxy, src, &srcOffset, &bounds, &src)) { | 1004 if (!this->applyCropRect(ctx, proxy, *src, &srcOffset, &bounds, src)) { |
1003 return false; | 1005 return false; |
1004 } | 1006 } |
1005 | 1007 |
1006 if (bounds.width() < 2 || bounds.height() < 2) { | 1008 if (bounds.width() < 2 || bounds.height() < 2) { |
1007 return false; | 1009 return false; |
1008 } | 1010 } |
| 1011 SkBitmap srcBitmap; |
| 1012 if (!as_IB(src)->getROPixels(&srcBitmap)) { |
| 1013 return false; |
| 1014 } |
| 1015 src.reset(NULL); |
1009 | 1016 |
1010 SkAutoLockPixels alp(src); | 1017 SkAutoLockPixels alp(srcBitmap); |
1011 if (!src.getPixels()) { | 1018 if (!srcBitmap.getPixels()) { |
1012 return false; | 1019 return false; |
1013 } | 1020 } |
1014 | 1021 |
1015 if (!dst->tryAllocPixels(src.info().makeWH(bounds.width(), bounds.height()))
) { | 1022 SkBitmap dstBitmap; |
| 1023 if (!dstBitmap.tryAllocPixels(srcBitmap.info().makeWH(bounds.width(), bounds
.height()))) { |
1016 return false; | 1024 return false; |
1017 } | 1025 } |
1018 | 1026 |
1019 SkAutoTUnref<SkLight> transformedLight(light()->transform(ctx.ctm())); | 1027 SkAutoTUnref<SkLight> transformedLight(light()->transform(ctx.ctm())); |
1020 | 1028 |
1021 DiffuseLightingType lightingType(fKD); | 1029 DiffuseLightingType lightingType(fKD); |
1022 offset->fX = bounds.left(); | 1030 int32_t resultX = bounds.left(); |
1023 offset->fY = bounds.top(); | 1031 int32_t resultY = bounds.top(); |
| 1032 |
1024 bounds.offset(-srcOffset); | 1033 bounds.offset(-srcOffset); |
1025 switch (transformedLight->type()) { | 1034 switch (transformedLight->type()) { |
1026 case SkLight::kDistant_LightType: | 1035 case SkLight::kDistant_LightType: |
1027 lightBitmap<DiffuseLightingType, SkDistantLight>(lightingType, trans
formedLight, src, dst, surfaceScale(), bounds); | 1036 lightBitmap<DiffuseLightingType, SkDistantLight>(lightingType, trans
formedLight, |
| 1037 srcBitmap, &dstBitm
ap, surfaceScale(), |
| 1038 bounds); |
1028 break; | 1039 break; |
1029 case SkLight::kPoint_LightType: | 1040 case SkLight::kPoint_LightType: |
1030 lightBitmap<DiffuseLightingType, SkPointLight>(lightingType, transfo
rmedLight, src, dst, surfaceScale(), bounds); | 1041 lightBitmap<DiffuseLightingType, SkPointLight>(lightingType, transfo
rmedLight, |
| 1042 srcBitmap, &dstBitmap
, surfaceScale(), |
| 1043 bounds); |
1031 break; | 1044 break; |
1032 case SkLight::kSpot_LightType: | 1045 case SkLight::kSpot_LightType: |
1033 lightBitmap<DiffuseLightingType, SkSpotLight>(lightingType, transfor
medLight, src, dst, surfaceScale(), bounds); | 1046 lightBitmap<DiffuseLightingType, SkSpotLight>(lightingType, transfor
medLight, srcBitmap, |
| 1047 &dstBitmap, surfaceSca
le(), bounds); |
1034 break; | 1048 break; |
1035 } | 1049 } |
1036 | 1050 |
| 1051 srcBitmap = SkBitmap(); |
| 1052 |
| 1053 SkImage* image = SkNewImageFromBitmap(dstBitmap, true, NULL); |
| 1054 if (NULL == image) { |
| 1055 return false; |
| 1056 } |
| 1057 dst.reset(image); |
| 1058 offset->fX = resultX; |
| 1059 offset->fY = resultY; |
1037 return true; | 1060 return true; |
1038 } | 1061 } |
1039 | 1062 |
1040 #ifndef SK_IGNORE_TO_STRING | 1063 #ifndef SK_IGNORE_TO_STRING |
1041 void SkDiffuseLightingImageFilter::toString(SkString* str) const { | 1064 void SkDiffuseLightingImageFilter::toString(SkString* str) const { |
1042 str->appendf("SkDiffuseLightingImageFilter: ("); | 1065 str->appendf("SkDiffuseLightingImageFilter: ("); |
1043 str->appendf("kD: %f\n", fKD); | 1066 str->appendf("kD: %f\n", fKD); |
1044 str->append(")"); | 1067 str->append(")"); |
1045 } | 1068 } |
1046 #endif | 1069 #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()); | 1116 return Create(light, surfaceScale, ks, shine, common.getInput(0), &common.cr
opRect(), common.uniqueID()); |
1094 } | 1117 } |
1095 | 1118 |
1096 void SkSpecularLightingImageFilter::flatten(SkWriteBuffer& buffer) const { | 1119 void SkSpecularLightingImageFilter::flatten(SkWriteBuffer& buffer) const { |
1097 this->INHERITED::flatten(buffer); | 1120 this->INHERITED::flatten(buffer); |
1098 buffer.writeScalar(fKS); | 1121 buffer.writeScalar(fKS); |
1099 buffer.writeScalar(fShininess); | 1122 buffer.writeScalar(fShininess); |
1100 } | 1123 } |
1101 | 1124 |
1102 bool SkSpecularLightingImageFilter::onFilterImage(Proxy* proxy, | 1125 bool SkSpecularLightingImageFilter::onFilterImage(Proxy* proxy, |
1103 const SkBitmap& source, | 1126 SkImage& source, |
1104 const Context& ctx, | 1127 const Context& ctx, |
1105 SkBitmap* dst, | 1128 SkAutoTUnref<SkImage>& dst, |
1106 SkIPoint* offset) const { | 1129 SkIPoint* offset) const { |
1107 SkImageFilter* input = getInput(0); | 1130 SkImageFilter* input = getInput(0); |
1108 SkBitmap src = source; | 1131 SkAutoTUnref<SkImage> src(SkRef(&source)); |
1109 SkIPoint srcOffset = SkIPoint::Make(0, 0); | 1132 SkIPoint srcOffset = SkIPoint::Make(0, 0); |
1110 if (input && !input->filterImage(proxy, source, ctx, &src, &srcOffset)) { | 1133 if (input && !input->filterImage(proxy, source, ctx, src, &srcOffset)) { |
| 1134 return false; |
| 1135 } |
| 1136 #if 0 |
| 1137 if (src.colorType() != kN32_SkColorType) { |
| 1138 return false; |
| 1139 } |
| 1140 #endif |
| 1141 SkIRect bounds; |
| 1142 if (!this->applyCropRect(ctx, proxy, *src, &srcOffset, &bounds, src)) { |
1111 return false; | 1143 return false; |
1112 } | 1144 } |
1113 | 1145 |
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) { | 1146 if (bounds.width() < 2 || bounds.height() < 2) { |
1124 return false; | 1147 return false; |
1125 } | 1148 } |
1126 | 1149 |
1127 SkAutoLockPixels alp(src); | 1150 SkBitmap srcBitmap; |
1128 if (!src.getPixels()) { | 1151 if (!as_IB(src)->getROPixels(&srcBitmap)) { |
| 1152 return false; |
| 1153 } |
| 1154 src.reset(NULL); |
| 1155 |
| 1156 SkAutoLockPixels alp(srcBitmap); |
| 1157 if (!srcBitmap.getPixels()) { |
1129 return false; | 1158 return false; |
1130 } | 1159 } |
1131 | 1160 |
1132 if (!dst->tryAllocPixels(src.info().makeWH(bounds.width(), bounds.height()))
) { | 1161 SkBitmap dstBitmap; |
| 1162 if (!dstBitmap.tryAllocPixels(srcBitmap.info().makeWH(bounds.width(), bounds
.height()))) { |
1133 return false; | 1163 return false; |
1134 } | 1164 } |
1135 | 1165 |
1136 SpecularLightingType lightingType(fKS, fShininess); | 1166 SpecularLightingType lightingType(fKS, fShininess); |
1137 offset->fX = bounds.left(); | 1167 int32_t resultX = bounds.left(); |
1138 offset->fY = bounds.top(); | 1168 int32_t resultY = bounds.top(); |
| 1169 |
1139 bounds.offset(-srcOffset); | 1170 bounds.offset(-srcOffset); |
1140 SkAutoTUnref<SkLight> transformedLight(light()->transform(ctx.ctm())); | 1171 SkAutoTUnref<SkLight> transformedLight(light()->transform(ctx.ctm())); |
1141 switch (transformedLight->type()) { | 1172 switch (transformedLight->type()) { |
1142 case SkLight::kDistant_LightType: | 1173 case SkLight::kDistant_LightType: |
1143 lightBitmap<SpecularLightingType, SkDistantLight>(lightingType, tran
sformedLight, src, dst, surfaceScale(), bounds); | 1174 lightBitmap<SpecularLightingType, SkDistantLight>(lightingType, tran
sformedLight, |
| 1175 srcBitmap, &dstBit
map, surfaceScale(), |
| 1176 bounds); |
1144 break; | 1177 break; |
1145 case SkLight::kPoint_LightType: | 1178 case SkLight::kPoint_LightType: |
1146 lightBitmap<SpecularLightingType, SkPointLight>(lightingType, transf
ormedLight, src, dst, surfaceScale(), bounds); | 1179 lightBitmap<SpecularLightingType, SkPointLight>(lightingType, transf
ormedLight, |
| 1180 srcBitmap, &dstBitma
p, surfaceScale(), |
| 1181 bounds); |
1147 break; | 1182 break; |
1148 case SkLight::kSpot_LightType: | 1183 case SkLight::kSpot_LightType: |
1149 lightBitmap<SpecularLightingType, SkSpotLight>(lightingType, transfo
rmedLight, src, dst, surfaceScale(), bounds); | 1184 lightBitmap<SpecularLightingType, SkSpotLight>(lightingType, transfo
rmedLight, |
| 1185 srcBitmap, &dstBitmap
, surfaceScale(), |
| 1186 bounds); |
1150 break; | 1187 break; |
1151 } | 1188 } |
| 1189 SkImage* image = SkNewImageFromBitmap(dstBitmap, true, NULL); |
| 1190 if (NULL == image) { |
| 1191 return false; |
| 1192 } |
| 1193 dst.reset(image); |
| 1194 offset->fX = resultX; |
| 1195 offset->fY = resultY; |
1152 return true; | 1196 return true; |
1153 } | 1197 } |
1154 | 1198 |
1155 #ifndef SK_IGNORE_TO_STRING | 1199 #ifndef SK_IGNORE_TO_STRING |
1156 void SkSpecularLightingImageFilter::toString(SkString* str) const { | 1200 void SkSpecularLightingImageFilter::toString(SkString* str) const { |
1157 str->appendf("SkSpecularLightingImageFilter: ("); | 1201 str->appendf("SkSpecularLightingImageFilter: ("); |
1158 str->appendf("kS: %f shininess: %f", fKS, fShininess); | 1202 str->appendf("kS: %f shininess: %f", fKS, fShininess); |
1159 str->append(")"); | 1203 str->append(")"); |
1160 } | 1204 } |
1161 #endif | 1205 #endif |
(...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1716 | 1760 |
1717 fsBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); | 1761 fsBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); |
1718 } | 1762 } |
1719 | 1763 |
1720 #endif | 1764 #endif |
1721 | 1765 |
1722 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) | 1766 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) |
1723 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) | 1767 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) |
1724 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) | 1768 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) |
1725 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 1769 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
OLD | NEW |