| OLD | NEW |
| (Empty) |
| 1 | |
| 2 /* | |
| 3 * Copyright 2015 Google Inc. | |
| 4 * | |
| 5 * Use of this source code is governed by a BSD-style license that can be | |
| 6 * found in the LICENSE file. | |
| 7 */ | |
| 8 | |
| 9 #include "SkBitmapProcState.h" | |
| 10 #include "SkColor.h" | |
| 11 #include "SkEmptyShader.h" | |
| 12 #include "SkErrorInternals.h" | |
| 13 #include "SkLightingShader.h" | |
| 14 #include "SkMathPriv.h" | |
| 15 #include "SkReadBuffer.h" | |
| 16 #include "SkWriteBuffer.h" | |
| 17 | |
| 18 //////////////////////////////////////////////////////////////////////////// | |
| 19 | |
| 20 /* | |
| 21 SkLightingShader TODOs: | |
| 22 support other than clamp mode | |
| 23 allow 'diffuse' & 'normal' to be of different dimensions? | |
| 24 support different light types | |
| 25 support multiple lights | |
| 26 enforce normal map is 4 channel | |
| 27 use SkImages instead if SkBitmaps | |
| 28 | |
| 29 To Test: | |
| 30 non-opaque diffuse textures | |
| 31 A8 diffuse textures | |
| 32 down & upsampled draws | |
| 33 */ | |
| 34 | |
| 35 | |
| 36 | |
| 37 /** \class SkLightingShaderImpl | |
| 38 This subclass of shader applies lighting. | |
| 39 */ | |
| 40 class SK_API SkLightingShaderImpl : public SkShader { | |
| 41 public: | |
| 42 | |
| 43 /** Create a new lighting shader that use the provided normal map, light | |
| 44 and ambient color to light the diffuse bitmap. | |
| 45 @param diffuse the diffuse bitmap | |
| 46 @param normal the normal map | |
| 47 @param light the light applied to the normal map | |
| 48 @param ambient the linear (unpremul) ambient light color | |
| 49 */ | |
| 50 SkLightingShaderImpl(const SkBitmap& diffuse, const SkBitmap& normal, | |
| 51 const SkLightingShader::Light& light, | |
| 52 const SkColor3f& ambient, const SkMatrix* localMatrix) | |
| 53 : INHERITED(localMatrix) | |
| 54 , fDiffuseMap(diffuse) | |
| 55 , fNormalMap(normal) | |
| 56 , fLight(light) | |
| 57 , fAmbientColor(ambient) { | |
| 58 if (!fLight.fDirection.normalize()) { | |
| 59 fLight.fDirection = SkPoint3::Make(0.0f, 0.0f, 1.0f); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 bool isOpaque() const override; | |
| 64 | |
| 65 bool asFragmentProcessor(GrContext*, const SkPaint& paint, const SkMatrix& v
iewM, | |
| 66 const SkMatrix* localMatrix, GrColor* color, | |
| 67 GrProcessorDataManager*, GrFragmentProcessor** fp)
const override; | |
| 68 | |
| 69 size_t contextSize() const override; | |
| 70 | |
| 71 class LightingShaderContext : public SkShader::Context { | |
| 72 public: | |
| 73 // The context takes ownership of the states. It will call their destruc
tors | |
| 74 // but will NOT free the memory. | |
| 75 LightingShaderContext(const SkLightingShaderImpl&, const ContextRec&, | |
| 76 SkBitmapProcState* diffuseState, SkBitmapProcState
* normalState); | |
| 77 ~LightingShaderContext() override; | |
| 78 | |
| 79 void shadeSpan(int x, int y, SkPMColor[], int count) override; | |
| 80 | |
| 81 uint32_t getFlags() const override { return fFlags; } | |
| 82 | |
| 83 private: | |
| 84 SkBitmapProcState* fDiffuseState; | |
| 85 SkBitmapProcState* fNormalState; | |
| 86 uint32_t fFlags; | |
| 87 | |
| 88 typedef SkShader::Context INHERITED; | |
| 89 }; | |
| 90 | |
| 91 SK_TO_STRING_OVERRIDE() | |
| 92 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLightingShaderImpl) | |
| 93 | |
| 94 protected: | |
| 95 void flatten(SkWriteBuffer&) const override; | |
| 96 Context* onCreateContext(const ContextRec&, void*) const override; | |
| 97 | |
| 98 private: | |
| 99 SkBitmap fDiffuseMap; | |
| 100 SkBitmap fNormalMap; | |
| 101 SkLightingShader::Light fLight; | |
| 102 SkColor3f fAmbientColor; // linear (unpremul) color. Range is
0..1/channel. | |
| 103 | |
| 104 friend class SkLightingShader; | |
| 105 | |
| 106 typedef SkShader INHERITED; | |
| 107 }; | |
| 108 | |
| 109 //////////////////////////////////////////////////////////////////////////// | |
| 110 | |
| 111 #if SK_SUPPORT_GPU | |
| 112 | |
| 113 #include "GrCoordTransform.h" | |
| 114 #include "GrFragmentProcessor.h" | |
| 115 #include "GrTextureAccess.h" | |
| 116 #include "gl/GrGLProcessor.h" | |
| 117 #include "gl/builders/GrGLProgramBuilder.h" | |
| 118 #include "SkGr.h" | |
| 119 | |
| 120 class LightingFP : public GrFragmentProcessor { | |
| 121 public: | |
| 122 LightingFP(GrTexture* diffuse, GrTexture* normal, const SkMatrix& matrix, | |
| 123 const SkVector3& lightDir, const SkColor3f& lightColor, | |
| 124 const SkColor3f& ambientColor) | |
| 125 : fDeviceTransform(kDevice_GrCoordSet, matrix) | |
| 126 , fDiffuseTextureAccess(diffuse) | |
| 127 , fNormalTextureAccess(normal) | |
| 128 , fLightDir(lightDir) | |
| 129 , fLightColor(lightColor) | |
| 130 , fAmbientColor(ambientColor) { | |
| 131 this->addCoordTransform(&fDeviceTransform); | |
| 132 this->addTextureAccess(&fDiffuseTextureAccess); | |
| 133 this->addTextureAccess(&fNormalTextureAccess); | |
| 134 | |
| 135 this->initClassID<LightingFP>(); | |
| 136 } | |
| 137 | |
| 138 class LightingGLFP : public GrGLFragmentProcessor { | |
| 139 public: | |
| 140 LightingGLFP() { | |
| 141 fLightDir.fX = 10000.0f; | |
| 142 fLightColor.fX = 0.0f; | |
| 143 fAmbientColor.fX = 0.0f; | |
| 144 } | |
| 145 | |
| 146 void emitCode(EmitArgs& args) override { | |
| 147 | |
| 148 GrGLFragmentBuilder* fpb = args.fBuilder->getFragmentShaderBuilder()
; | |
| 149 | |
| 150 // add uniforms | |
| 151 const char* lightDirUniName = NULL; | |
| 152 fLightDirUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragme
nt_Visibility, | |
| 153 kVec3f_GrSLType, kDefault_G
rSLPrecision, | |
| 154 "LightDir", &lightDirUniNam
e); | |
| 155 | |
| 156 const char* lightColorUniName = NULL; | |
| 157 fLightColorUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFrag
ment_Visibility, | |
| 158 kVec3f_GrSLType, kDefault
_GrSLPrecision, | |
| 159 "LightColor", &lightColor
UniName); | |
| 160 | |
| 161 const char* ambientColorUniName = NULL; | |
| 162 fAmbientColorUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFr
agment_Visibility, | |
| 163 kVec3f_GrSLType, kDefau
lt_GrSLPrecision, | |
| 164 "AmbientColor", &ambien
tColorUniName); | |
| 165 | |
| 166 fpb->codeAppend("vec4 diffuseColor = "); | |
| 167 fpb->appendTextureLookupAndModulate(args.fInputColor, args.fSamplers
[0], | |
| 168 args.fCoords[0].c_str(), | |
| 169 args.fCoords[0].getType()); | |
| 170 fpb->codeAppend(";"); | |
| 171 | |
| 172 fpb->codeAppend("vec4 normalColor = "); | |
| 173 fpb->appendTextureLookup(args.fSamplers[1], | |
| 174 args.fCoords[0].c_str(), | |
| 175 args.fCoords[0].getType()); | |
| 176 fpb->codeAppend(";"); | |
| 177 | |
| 178 fpb->codeAppend("vec3 normal = normalize(normalColor.rgb - vec3(0.5)
);"); | |
| 179 fpb->codeAppendf("vec3 lightDir = normalize(%s);", lightDirUniName); | |
| 180 fpb->codeAppend("float NdotL = dot(normal, lightDir);"); | |
| 181 // diffuse light | |
| 182 fpb->codeAppendf("vec3 result = %s*diffuseColor.rgb*NdotL;", lightCo
lorUniName); | |
| 183 // ambient light | |
| 184 fpb->codeAppendf("result += %s;", ambientColorUniName); | |
| 185 fpb->codeAppendf("%s = vec4(result.rgb, diffuseColor.a);", args.fOut
putColor); | |
| 186 } | |
| 187 | |
| 188 void setData(const GrGLProgramDataManager& pdman, const GrProcessor& pro
c) override { | |
| 189 const LightingFP& lightingFP = proc.cast<LightingFP>(); | |
| 190 | |
| 191 const SkVector3& lightDir = lightingFP.lightDir(); | |
| 192 if (lightDir != fLightDir) { | |
| 193 pdman.set3fv(fLightDirUni, 1, &lightDir.fX); | |
| 194 fLightDir = lightDir; | |
| 195 } | |
| 196 | |
| 197 const SkColor3f& lightColor = lightingFP.lightColor(); | |
| 198 if (lightColor != fLightColor) { | |
| 199 pdman.set3fv(fLightColorUni, 1, &lightColor.fX); | |
| 200 fLightColor = lightColor; | |
| 201 } | |
| 202 | |
| 203 const SkColor3f& ambientColor = lightingFP.ambientColor(); | |
| 204 if (ambientColor != fAmbientColor) { | |
| 205 pdman.set3fv(fAmbientColorUni, 1, &ambientColor.fX); | |
| 206 fAmbientColor = ambientColor; | |
| 207 } | |
| 208 } | |
| 209 | |
| 210 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | |
| 211 GrProcessorKeyBuilder* b) { | |
| 212 // const LightingFP& lightingFP = proc.cast<LightingFP>(); | |
| 213 // only one shader generated currently | |
| 214 b->add32(0x0); | |
| 215 } | |
| 216 | |
| 217 private: | |
| 218 SkVector3 fLightDir; | |
| 219 GrGLProgramDataManager::UniformHandle fLightDirUni; | |
| 220 | |
| 221 SkColor3f fLightColor; | |
| 222 GrGLProgramDataManager::UniformHandle fLightColorUni; | |
| 223 | |
| 224 SkColor3f fAmbientColor; | |
| 225 GrGLProgramDataManager::UniformHandle fAmbientColorUni; | |
| 226 }; | |
| 227 | |
| 228 GrGLFragmentProcessor* createGLInstance() const override { return SkNEW(Ligh
tingGLFP); } | |
| 229 | |
| 230 void onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) c
onst override { | |
| 231 LightingGLFP::GenKey(*this, caps, b); | |
| 232 } | |
| 233 | |
| 234 const char* name() const override { return "LightingFP"; } | |
| 235 | |
| 236 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { | |
| 237 inout->mulByUnknownFourComponents(); | |
| 238 } | |
| 239 | |
| 240 const SkVector3& lightDir() const { return fLightDir; } | |
| 241 const SkColor3f& lightColor() const { return fLightColor; } | |
| 242 const SkColor3f& ambientColor() const { return fAmbientColor; } | |
| 243 | |
| 244 private: | |
| 245 bool onIsEqual(const GrFragmentProcessor& proc) const override { | |
| 246 const LightingFP& lightingFP = proc.cast<LightingFP>(); | |
| 247 return fDeviceTransform == lightingFP.fDeviceTransform && | |
| 248 fDiffuseTextureAccess == lightingFP.fDiffuseTextureAccess && | |
| 249 fNormalTextureAccess == lightingFP.fNormalTextureAccess && | |
| 250 fLightDir == lightingFP.fLightDir && | |
| 251 fLightColor == lightingFP.fLightColor && | |
| 252 fAmbientColor == lightingFP.fAmbientColor; | |
| 253 } | |
| 254 | |
| 255 GrCoordTransform fDeviceTransform; | |
| 256 GrTextureAccess fDiffuseTextureAccess; | |
| 257 GrTextureAccess fNormalTextureAccess; | |
| 258 SkVector3 fLightDir; | |
| 259 SkColor3f fLightColor; | |
| 260 SkColor3f fAmbientColor; | |
| 261 }; | |
| 262 | |
| 263 //////////////////////////////////////////////////////////////////////////// | |
| 264 | |
| 265 bool SkLightingShaderImpl::asFragmentProcessor(GrContext* context, const SkPaint
& paint, | |
| 266 const SkMatrix& viewM, const SkMa
trix* localMatrix, | |
| 267 GrColor* color, GrProcessorDataMa
nager*, | |
| 268 GrFragmentProcessor** fp) const { | |
| 269 // we assume diffuse and normal maps have same width and height | |
| 270 // TODO: support different sizes | |
| 271 SkASSERT(fDiffuseMap.width() == fNormalMap.width() && | |
| 272 fDiffuseMap.height() == fNormalMap.height()); | |
| 273 SkMatrix matrix; | |
| 274 matrix.setIDiv(fDiffuseMap.width(), fDiffuseMap.height()); | |
| 275 | |
| 276 SkMatrix lmInverse; | |
| 277 if (!this->getLocalMatrix().invert(&lmInverse)) { | |
| 278 return false; | |
| 279 } | |
| 280 if (localMatrix) { | |
| 281 SkMatrix inv; | |
| 282 if (!localMatrix->invert(&inv)) { | |
| 283 return false; | |
| 284 } | |
| 285 lmInverse.postConcat(inv); | |
| 286 } | |
| 287 matrix.preConcat(lmInverse); | |
| 288 | |
| 289 // Must set wrap and filter on the sampler before requesting a texture. In t
wo places below | |
| 290 // we check the matrix scale factors to determine how to interpret the filte
r quality setting. | |
| 291 // This completely ignores the complexity of the drawVertices case where exp
licit local coords | |
| 292 // are provided by the caller. | |
| 293 GrTextureParams::FilterMode textureFilterMode = GrTextureParams::kBilerp_Fil
terMode; | |
| 294 switch (paint.getFilterQuality()) { | |
| 295 case kNone_SkFilterQuality: | |
| 296 textureFilterMode = GrTextureParams::kNone_FilterMode; | |
| 297 break; | |
| 298 case kLow_SkFilterQuality: | |
| 299 textureFilterMode = GrTextureParams::kBilerp_FilterMode; | |
| 300 break; | |
| 301 case kMedium_SkFilterQuality:{ | |
| 302 SkMatrix matrix; | |
| 303 matrix.setConcat(viewM, this->getLocalMatrix()); | |
| 304 if (matrix.getMinScale() < SK_Scalar1) { | |
| 305 textureFilterMode = GrTextureParams::kMipMap_FilterMode; | |
| 306 } else { | |
| 307 // Don't trigger MIP level generation unnecessarily. | |
| 308 textureFilterMode = GrTextureParams::kBilerp_FilterMode; | |
| 309 } | |
| 310 break; | |
| 311 } | |
| 312 case kHigh_SkFilterQuality: | |
| 313 default: | |
| 314 SkErrorInternals::SetError(kInvalidPaint_SkError, | |
| 315 "Sorry, I don't understand the filtering " | |
| 316 "mode you asked for. Falling back to " | |
| 317 "MIPMaps."); | |
| 318 textureFilterMode = GrTextureParams::kMipMap_FilterMode; | |
| 319 break; | |
| 320 | |
| 321 } | |
| 322 | |
| 323 // TODO: support other tile modes | |
| 324 GrTextureParams params(kClamp_TileMode, textureFilterMode); | |
| 325 SkAutoTUnref<GrTexture> diffuseTexture(GrRefCachedBitmapTexture(context, fDi
ffuseMap, ¶ms)); | |
| 326 if (!diffuseTexture) { | |
| 327 SkErrorInternals::SetError(kInternalError_SkError, | |
| 328 "Couldn't convert bitmap to texture."); | |
| 329 return false; | |
| 330 } | |
| 331 | |
| 332 SkAutoTUnref<GrTexture> normalTexture(GrRefCachedBitmapTexture(context, fNor
malMap, ¶ms)); | |
| 333 if (!normalTexture) { | |
| 334 SkErrorInternals::SetError(kInternalError_SkError, | |
| 335 "Couldn't convert bitmap to texture."); | |
| 336 return false; | |
| 337 } | |
| 338 | |
| 339 *fp = SkNEW_ARGS(LightingFP, (diffuseTexture, normalTexture, matrix, | |
| 340 fLight.fDirection, fLight.fColor, fAmbientColo
r)); | |
| 341 *color = GrColorPackA4(paint.getAlpha()); | |
| 342 return true; | |
| 343 } | |
| 344 #else | |
| 345 | |
| 346 bool SkLightingShaderImpl::asFragmentProcessor(GrContext* context, const SkPaint
& paint, | |
| 347 const SkMatrix& viewM, const SkMa
trix* localMatrix, | |
| 348 GrColor* color, GrProcessorDataMa
nager*, | |
| 349 GrFragmentProcessor** fp) const { | |
| 350 SkDEBUGFAIL("Should not call in GPU-less build"); | |
| 351 return false; | |
| 352 } | |
| 353 | |
| 354 #endif | |
| 355 | |
| 356 //////////////////////////////////////////////////////////////////////////// | |
| 357 | |
| 358 bool SkLightingShaderImpl::isOpaque() const { | |
| 359 return fDiffuseMap.isOpaque(); | |
| 360 } | |
| 361 | |
| 362 size_t SkLightingShaderImpl::contextSize() const { | |
| 363 return 2 * sizeof(SkBitmapProcState) + sizeof(LightingShaderContext); | |
| 364 } | |
| 365 | |
| 366 SkLightingShaderImpl::LightingShaderContext::LightingShaderContext(const SkLight
ingShaderImpl& shader, | |
| 367 const Context
Rec& rec, | |
| 368 SkBitmapProcS
tate* diffuseState, | |
| 369 SkBitmapProcS
tate* normalState) | |
| 370 : INHERITED(shader, rec) | |
| 371 , fDiffuseState(diffuseState) | |
| 372 , fNormalState(normalState) | |
| 373 { | |
| 374 const SkPixmap& pixmap = fDiffuseState->fPixmap; | |
| 375 bool isOpaque = pixmap.isOpaque(); | |
| 376 | |
| 377 // update fFlags | |
| 378 uint32_t flags = 0; | |
| 379 if (isOpaque && (255 == this->getPaintAlpha())) { | |
| 380 flags |= kOpaqueAlpha_Flag; | |
| 381 } | |
| 382 | |
| 383 fFlags = flags; | |
| 384 } | |
| 385 | |
| 386 SkLightingShaderImpl::LightingShaderContext::~LightingShaderContext() { | |
| 387 // The bitmap proc states have been created outside of the context on memory
that will be freed | |
| 388 // elsewhere. Call the destructors but leave the freeing of the memory to th
e caller. | |
| 389 fDiffuseState->~SkBitmapProcState(); | |
| 390 fNormalState->~SkBitmapProcState(); | |
| 391 } | |
| 392 | |
| 393 static inline int light(SkScalar light, int diff, SkScalar NdotL, SkScalar ambie
nt) { | |
| 394 SkScalar color = light * diff * NdotL + 255 * ambient; | |
| 395 if (color <= 0.0f) { | |
| 396 return 0; | |
| 397 } else if (color >= 255.0f) { | |
| 398 return 255; | |
| 399 } else { | |
| 400 return (int) color; | |
| 401 } | |
| 402 } | |
| 403 | |
| 404 // larger is better (fewer times we have to loop), but we shouldn't | |
| 405 // take up too much stack-space (each could here costs 16 bytes) | |
| 406 #define TMP_COUNT 16 | |
| 407 | |
| 408 void SkLightingShaderImpl::LightingShaderContext::shadeSpan(int x, int y, | |
| 409 SkPMColor result[],
int count) { | |
| 410 const SkLightingShaderImpl& lightShader = static_cast<const SkLightingShader
Impl&>(fShader); | |
| 411 | |
| 412 SkPMColor tmpColor[TMP_COUNT], tmpColor2[TMP_COUNT]; | |
| 413 SkPMColor tmpNormal[TMP_COUNT], tmpNormal2[TMP_COUNT]; | |
| 414 | |
| 415 SkBitmapProcState::MatrixProc diffMProc = fDiffuseState->getMatrixProc(); | |
| 416 SkBitmapProcState::SampleProc32 diffSProc = fDiffuseState->getSampleProc32()
; | |
| 417 | |
| 418 SkBitmapProcState::MatrixProc normalMProc = fNormalState->getMatrixProc(); | |
| 419 SkBitmapProcState::SampleProc32 normalSProc = fNormalState->getSampleProc32(
); | |
| 420 | |
| 421 SkASSERT(fDiffuseState->fPixmap.addr()); | |
| 422 SkASSERT(fNormalState->fPixmap.addr()); | |
| 423 | |
| 424 SkPoint3 norm; | |
| 425 SkScalar NdotL; | |
| 426 int r, g, b; | |
| 427 | |
| 428 do { | |
| 429 int n = count; | |
| 430 if (n > TMP_COUNT) { | |
| 431 n = TMP_COUNT; | |
| 432 } | |
| 433 | |
| 434 diffMProc(*fDiffuseState, tmpColor, n, x, y); | |
| 435 diffSProc(*fDiffuseState, tmpColor, n, tmpColor2); | |
| 436 | |
| 437 normalMProc(*fNormalState, tmpNormal, n, x, y); | |
| 438 normalSProc(*fNormalState, tmpNormal, n, tmpNormal2); | |
| 439 | |
| 440 for (int i = 0; i < n; ++i) { | |
| 441 SkASSERT(0xFF == SkColorGetA(tmpNormal2[i])); // opaque -> unpremul | |
| 442 norm.set(SkIntToScalar(SkGetPackedR32(tmpNormal2[i]))-127.0f, | |
| 443 SkIntToScalar(SkGetPackedG32(tmpNormal2[i]))-127.0f, | |
| 444 SkIntToScalar(SkGetPackedB32(tmpNormal2[i]))-127.0f); | |
| 445 norm.normalize(); | |
| 446 | |
| 447 SkColor diffColor = SkUnPreMultiply::PMColorToColor(tmpColor2[i]); | |
| 448 NdotL = norm.dot(lightShader.fLight.fDirection); | |
| 449 | |
| 450 // This is all done in linear unpremul color space | |
| 451 r = light(lightShader.fLight.fColor.fX, SkColorGetR(diffColor), Ndot
L, | |
| 452 lightShader.fAmbientColor.fX); | |
| 453 g = light(lightShader.fLight.fColor.fY, SkColorGetG(diffColor), Ndot
L, | |
| 454 lightShader.fAmbientColor.fY); | |
| 455 b = light(lightShader.fLight.fColor.fZ, SkColorGetB(diffColor), Ndot
L, | |
| 456 lightShader.fAmbientColor.fZ); | |
| 457 | |
| 458 result[i] = SkPreMultiplyARGB(SkColorGetA(diffColor), r, g, b); | |
| 459 } | |
| 460 | |
| 461 result += n; | |
| 462 x += n; | |
| 463 count -= n; | |
| 464 } while (count > 0); | |
| 465 } | |
| 466 | |
| 467 //////////////////////////////////////////////////////////////////////////// | |
| 468 | |
| 469 #ifndef SK_IGNORE_TO_STRING | |
| 470 void SkLightingShaderImpl::toString(SkString* str) const { | |
| 471 str->appendf("LightingShader: ()"); | |
| 472 } | |
| 473 #endif | |
| 474 | |
| 475 SkFlattenable* SkLightingShaderImpl::CreateProc(SkReadBuffer& buf) { | |
| 476 SkMatrix localMatrix; | |
| 477 buf.readMatrix(&localMatrix); | |
| 478 | |
| 479 SkBitmap diffuse; | |
| 480 if (!buf.readBitmap(&diffuse)) { | |
| 481 return NULL; | |
| 482 } | |
| 483 diffuse.setImmutable(); | |
| 484 | |
| 485 SkBitmap normal; | |
| 486 if (!buf.readBitmap(&normal)) { | |
| 487 return NULL; | |
| 488 } | |
| 489 normal.setImmutable(); | |
| 490 | |
| 491 SkLightingShader::Light light; | |
| 492 if (!buf.readScalarArray(&light.fDirection.fX, 3)) { | |
| 493 return NULL; | |
| 494 } | |
| 495 if (!buf.readScalarArray(&light.fColor.fX, 3)) { | |
| 496 return NULL; | |
| 497 } | |
| 498 | |
| 499 SkColor3f ambient; | |
| 500 if (!buf.readScalarArray(&ambient.fX, 3)) { | |
| 501 return NULL; | |
| 502 } | |
| 503 | |
| 504 return SkNEW_ARGS(SkLightingShaderImpl, (diffuse, normal, light, ambient, &l
ocalMatrix)); | |
| 505 } | |
| 506 | |
| 507 void SkLightingShaderImpl::flatten(SkWriteBuffer& buf) const { | |
| 508 buf.writeMatrix(this->getLocalMatrix()); | |
| 509 | |
| 510 buf.writeBitmap(fDiffuseMap); | |
| 511 buf.writeBitmap(fNormalMap); | |
| 512 buf.writeScalarArray(&fLight.fDirection.fX, 3); | |
| 513 buf.writeScalarArray(&fLight.fColor.fX, 3); | |
| 514 buf.writeScalarArray(&fAmbientColor.fX, 3); | |
| 515 } | |
| 516 | |
| 517 SkShader::Context* SkLightingShaderImpl::onCreateContext(const ContextRec& rec, | |
| 518 void* storage) const { | |
| 519 | |
| 520 SkMatrix totalInverse; | |
| 521 // Do this first, so we know the matrix can be inverted. | |
| 522 if (!this->computeTotalInverse(rec, &totalInverse)) { | |
| 523 return NULL; | |
| 524 } | |
| 525 | |
| 526 void* diffuseStateStorage = (char*)storage + sizeof(LightingShaderContext); | |
| 527 SkBitmapProcState* diffuseState = SkNEW_PLACEMENT(diffuseStateStorage, SkBit
mapProcState); | |
| 528 SkASSERT(diffuseState); | |
| 529 | |
| 530 diffuseState->fTileModeX = SkShader::kClamp_TileMode; | |
| 531 diffuseState->fTileModeY = SkShader::kClamp_TileMode; | |
| 532 diffuseState->fOrigBitmap = fDiffuseMap; | |
| 533 if (!diffuseState->chooseProcs(totalInverse, *rec.fPaint)) { | |
| 534 diffuseState->~SkBitmapProcState(); | |
| 535 return NULL; | |
| 536 } | |
| 537 | |
| 538 void* normalStateStorage = (char*)storage + sizeof(LightingShaderContext) +
sizeof(SkBitmapProcState); | |
| 539 SkBitmapProcState* normalState = SkNEW_PLACEMENT(normalStateStorage, SkBitma
pProcState); | |
| 540 SkASSERT(normalState); | |
| 541 | |
| 542 normalState->fTileModeX = SkShader::kClamp_TileMode; | |
| 543 normalState->fTileModeY = SkShader::kClamp_TileMode; | |
| 544 normalState->fOrigBitmap = fNormalMap; | |
| 545 if (!normalState->chooseProcs(totalInverse, *rec.fPaint)) { | |
| 546 diffuseState->~SkBitmapProcState(); | |
| 547 normalState->~SkBitmapProcState(); | |
| 548 return NULL; | |
| 549 } | |
| 550 | |
| 551 return SkNEW_PLACEMENT_ARGS(storage, LightingShaderContext, (*this, rec, | |
| 552 diffuseState, n
ormalState)); | |
| 553 } | |
| 554 | |
| 555 /////////////////////////////////////////////////////////////////////////////// | |
| 556 | |
| 557 static bool bitmap_is_too_big(const SkBitmap& bm) { | |
| 558 // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer, as it | |
| 559 // communicates between its matrix-proc and its sampler-proc. Until we can | |
| 560 // widen that, we have to reject bitmaps that are larger. | |
| 561 // | |
| 562 static const int kMaxSize = 65535; | |
| 563 | |
| 564 return bm.width() > kMaxSize || bm.height() > kMaxSize; | |
| 565 } | |
| 566 | |
| 567 SkShader* SkLightingShader::Create(const SkBitmap& diffuse, const SkBitmap& norm
al, | |
| 568 const SkLightingShader::Light& light, | |
| 569 const SkColor3f& ambient, | |
| 570 const SkMatrix* localMatrix) { | |
| 571 if (diffuse.isNull() || bitmap_is_too_big(diffuse) || | |
| 572 normal.isNull() || bitmap_is_too_big(normal) || | |
| 573 diffuse.width() != normal.width() || | |
| 574 diffuse.height() != normal.height()) { | |
| 575 return nullptr; | |
| 576 } | |
| 577 | |
| 578 return SkNEW_ARGS(SkLightingShaderImpl, (diffuse, normal, light, ambient, lo
calMatrix)); | |
| 579 } | |
| 580 | |
| 581 /////////////////////////////////////////////////////////////////////////////// | |
| 582 | |
| 583 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader) | |
| 584 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl) | |
| 585 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | |
| 586 | |
| 587 /////////////////////////////////////////////////////////////////////////////// | |
| OLD | NEW |