Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2016 Google Inc. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license that can be | |
| 5 * found in the LICENSE file. | |
| 6 */ | |
| 7 | |
| 8 | |
| 9 #include "SkLights.h" | |
| 10 #include "SkReadBuffer.h" | |
| 11 #include "SkShadowShader.h" | |
| 12 #include "SkPoint3.h" | |
| 13 | |
| 14 //////////////////////////////////////////////////////////////////////////// | |
| 15 #ifdef SK_EXPERIMENTAL_SHADOWING | |
| 16 | |
| 17 #define SK_MAX_NON_AMBIENT_LIGHTS 4 | |
| 18 | |
| 19 /** \class SkShadowShaderImpl | |
| 20 This subclass of shader applies shadowing | |
| 21 */ | |
| 22 class SkShadowShaderImpl : public SkShader { | |
| 23 public: | |
| 24 /** Create a new shadowing shader that shadows | |
| 25 @param to do to do | |
| 26 */ | |
| 27 SkShadowShaderImpl(sk_sp<SkShader> povDepthShader, | |
| 28 sk_sp<SkShader> diffuseShader, | |
| 29 sk_sp<SkLights> lights) | |
| 30 : povDepthShader(std::move(povDepthShader)) | |
|
vjiaoblack
2016/08/01 16:33:27
fPovDepthShader
| |
| 31 , diffuseShader(std::move(diffuseShader)) | |
| 32 , fLights(std::move(lights)) { } | |
| 33 | |
| 34 bool isOpaque() const override; | |
| 35 | |
| 36 #if SK_SUPPORT_GPU | |
| 37 sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const overri de; | |
| 38 #endif | |
| 39 | |
| 40 class ShadowShaderContext : public SkShader::Context { | |
| 41 public: | |
| 42 // The context takes ownership of the states. It will call their destruc tors | |
| 43 // but will NOT free the memory. | |
| 44 ShadowShaderContext(const SkShadowShaderImpl&, const ContextRec&, | |
| 45 SkShader::Context* povDepthContext, | |
| 46 SkShader::Context* diffuseContext, | |
| 47 void* heapAllocated); | |
| 48 | |
| 49 ~ShadowShaderContext() override; | |
| 50 | |
| 51 void shadeSpan(int x, int y, SkPMColor[], int count) override; | |
| 52 | |
| 53 uint32_t getFlags() const override { return fFlags; } | |
| 54 | |
| 55 private: | |
| 56 SkShader::Context* fPovDepthContext; | |
| 57 SkShader::Context* fDiffuseContext; | |
| 58 uint32_t fFlags; | |
| 59 | |
| 60 void* fHeapAllocated; | |
| 61 | |
| 62 typedef SkShader::Context INHERITED; | |
| 63 }; | |
| 64 | |
| 65 SK_TO_STRING_OVERRIDE() | |
| 66 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkShadowShaderImpl) | |
| 67 | |
| 68 protected: | |
| 69 void flatten(SkWriteBuffer&) const override; | |
| 70 size_t onContextSize(const ContextRec&) const override; | |
| 71 Context* onCreateContext(const ContextRec&, void*) const override; | |
| 72 | |
| 73 private: | |
| 74 sk_sp<SkShader> povDepthShader; | |
| 75 sk_sp<SkShader> diffuseShader; | |
| 76 sk_sp<SkLights> fLights; | |
| 77 | |
| 78 friend class SkShadowShader; | |
| 79 | |
| 80 typedef SkShader INHERITED; | |
| 81 }; | |
| 82 | |
| 83 //////////////////////////////////////////////////////////////////////////// | |
| 84 | |
| 85 #if SK_SUPPORT_GPU | |
| 86 | |
| 87 #include "GrCoordTransform.h" | |
| 88 #include "GrFragmentProcessor.h" | |
| 89 #include "GrInvariantOutput.h" | |
| 90 #include "glsl/GrGLSLFragmentProcessor.h" | |
| 91 #include "glsl/GrGLSLFragmentShaderBuilder.h" | |
| 92 #include "SkGr.h" | |
| 93 #include "SkGrPriv.h" | |
| 94 #include "SkSpecialImage.h" | |
| 95 #include "SkImage_Base.h" | |
| 96 #include "GrContext.h" | |
| 97 | |
| 98 class ShadowFP : public GrFragmentProcessor { | |
| 99 public: | |
| 100 ShadowFP(sk_sp<GrFragmentProcessor> povDepth, | |
| 101 sk_sp<GrFragmentProcessor> diffuse, | |
| 102 sk_sp<SkLights> lights, | |
| 103 GrContext* context) { | |
| 104 | |
| 105 // fuse all ambient lights into a single one | |
| 106 fAmbientColor.set(0.0f, 0.0f, 0.0f); | |
| 107 | |
| 108 fNumDirLights = 0; // refers to directional lights. | |
| 109 for (int i = 0; i < lights->numLights(); ++i) { | |
| 110 if (SkLights::Light::kAmbient_LightType == lights->light(i).type()) { | |
| 111 fAmbientColor += lights->light(i).color(); | |
| 112 } else if (fNumDirLights < SK_MAX_NON_AMBIENT_LIGHTS){ | |
| 113 fLightColor[fNumDirLights] = lights->light(i).color(); | |
| 114 fLightDir[fNumDirLights] = lights->light(i).dir(); | |
| 115 SkImage_Base* shadowMap = ((SkImage_Base*)lights->light(i).getSh adowMap().get()); | |
| 116 | |
| 117 // this sk_sp gets deleted when the ShadowFP is destroyed, and f rees the GrTexture* | |
| 118 fTexture[fNumDirLights] = sk_sp<GrTexture>(shadowMap->asTextureR ef(context, | |
| 119 GrTextureParams::Clam pNoFilter(), | |
| 120 SkSourceGammaTreatmen t::kIgnore)); | |
| 121 fDepthMapAccess[fNumDirLights].reset(fTexture[fNumDirLights].get ()); | |
| 122 this->addTextureAccess(&fDepthMapAccess[fNumDirLights]); | |
| 123 | |
| 124 fNumDirLights++; | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 this->registerChildProcessor(std::move(povDepth)); | |
| 129 this->registerChildProcessor(std::move(diffuse)); | |
| 130 this->initClassID<ShadowFP>(); | |
| 131 } | |
| 132 | |
| 133 class GLSLShadowFP : public GrGLSLFragmentProcessor { | |
| 134 public: | |
| 135 GLSLShadowFP() { } | |
| 136 | |
| 137 void emitCode(EmitArgs& args) override { | |
| 138 | |
| 139 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; | |
| 140 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; | |
| 141 | |
| 142 // add uniforms | |
| 143 int32_t numLights = args.fFp.cast<ShadowFP>().fNumDirLights; | |
| 144 SkASSERT(numLights <= SK_MAX_NON_AMBIENT_LIGHTS); | |
| 145 | |
| 146 const char* lightDirUniName[SK_MAX_NON_AMBIENT_LIGHTS] = {nullptr}; | |
| 147 const char* lightColorUniName[SK_MAX_NON_AMBIENT_LIGHTS] = {nullptr} ; | |
| 148 | |
| 149 for (int i = 0; i < numLights; i++) { | |
| 150 fLightDirUni[i] = uniformHandler->addUniform(kFragment_GrShaderF lag, | |
| 151 kVec3f_GrSLType, | |
| 152 kDefault_GrSLPrecis ion, | |
| 153 "LightDir" + (i+1), | |
| 154 &lightDirUniName[i] ); | |
| 155 fLightColorUni[i] = uniformHandler->addUniform(kFragment_GrShade rFlag, | |
| 156 kVec3f_GrSLType, | |
| 157 kDefault_GrSLPrec ision, | |
| 158 "LightColor" + (i +1), | |
| 159 &lightColorUniNam e[i]); | |
| 160 } | |
| 161 | |
| 162 SkString povDepth("povDepth"); | |
| 163 this->emitChild(0, nullptr, &povDepth, args); | |
| 164 | |
| 165 SkString diffuseColor("inDiffuseColor"); | |
| 166 this->emitChild(1, nullptr, &diffuseColor, args); | |
| 167 | |
| 168 SkString depthMaps[SK_MAX_NON_AMBIENT_LIGHTS]; | |
| 169 | |
| 170 for (int i = 0; i < numLights; i++) { | |
| 171 SkString povCoord("povCoord"); | |
| 172 povCoord.appendf("%d", i); | |
| 173 | |
| 174 // vMatrixCoord_0_1_Stage0 is the texture sampler coordinates. | |
| 175 // povDepth.b * 255 scales it to 0 - 255, bringing it to world s pace, | |
| 176 // and the / 400 brings it back to a sampler coordinate, 0 - 1 | |
| 177 // The 400 comes from the shadowmaps GM. | |
| 178 // TODO use real shadowmaps size | |
| 179 SkString offset("lol"); | |
| 180 offset.appendf("%d", i); | |
| 181 | |
| 182 fragBuilder->codeAppendf("vec2 %s = (vec2(%s) * povDepth.b * 255 / 400);", | |
| 183 offset.c_str(), lightDirUniName[i]); | |
| 184 | |
| 185 | |
| 186 fragBuilder->codeAppendf("vec2 %s = vec2(vMatrixCoord_0_1_Stage0 .x, " | |
| 187 "vMatrixCoord_0_1_Stage0 .y) + " | |
| 188 "vec2(%s.x, 0 - %s.y);", | |
| 189 povCoord.c_str(), offset.c_str(), offse t.c_str()); | |
| 190 | |
| 191 fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSampler s[i], | |
| 192 povCoord.c_str(), | |
| 193 kVec2f_GrSLType); | |
| 194 } | |
| 195 | |
| 196 const char* ambientColorUniName = nullptr; | |
| 197 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag , | |
| 198 kVec3f_GrSLType, kDefa ult_GrSLPrecision, | |
| 199 "AmbientColor", &ambie ntColorUniName); | |
| 200 | |
| 201 fragBuilder->codeAppendf("vec4 resultDiffuseColor = %s;", diffuseCol or.c_str()); | |
| 202 | |
| 203 // diffColor * (ambientLightTot + foreachDirLight(lightColor * (N . L))) | |
| 204 SkString totalLightColor("totalLightColor"); | |
| 205 fragBuilder->codeAppendf("vec3 %s = vec3(0);", totalLightColor.c_str ()); | |
| 206 | |
| 207 for (int i = 0; i < numLights ; i++) { | |
| 208 fragBuilder->codeAppendf("if (%s.b >= %s.b) {", | |
| 209 povDepth.c_str(), depthMaps[i].c_str()) ; | |
| 210 // dot(vec3(0,0,1), %s) == %s.z * %s | |
| 211 fragBuilder->codeAppendf("%s += %s.z * %s;", | |
| 212 totalLightColor.c_str(), | |
| 213 lightDirUniName[i], | |
| 214 lightColorUniName[i]); | |
| 215 fragBuilder->codeAppendf("}"); | |
| 216 } | |
| 217 | |
| 218 fragBuilder->codeAppendf("%s += %s;", | |
| 219 totalLightColor.c_str(), | |
| 220 ambientColorUniName); | |
| 221 | |
| 222 fragBuilder->codeAppendf("resultDiffuseColor *= vec4(%s, 1);", | |
| 223 totalLightColor.c_str()); | |
| 224 | |
| 225 fragBuilder->codeAppendf("%s = resultDiffuseColor;", args.fOutputCol or); | |
| 226 } | |
| 227 | |
| 228 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | |
| 229 GrProcessorKeyBuilder* b) { | |
| 230 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); | |
| 231 b->add32(shadowFP.fNumDirLights); | |
| 232 } | |
| 233 | |
| 234 protected: | |
| 235 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override { | |
| 236 const ShadowFP &shadowFP = proc.cast<ShadowFP>(); | |
| 237 | |
| 238 fNumDirLights = shadowFP.numLights(); | |
| 239 | |
| 240 for (int i = 0; i < fNumDirLights; i++) { | |
| 241 const SkVector3 &lightDir = shadowFP.lightDir(i); | |
| 242 if (lightDir != fLightDir[i]) { | |
| 243 pdman.set3fv(fLightDirUni[i], 1, &lightDir.fX); | |
| 244 fLightDir[i] = lightDir; | |
| 245 } | |
| 246 const SkColor3f &lightColor = shadowFP.lightColor(i); | |
| 247 if (lightColor != fLightColor[i]) { | |
| 248 pdman.set3fv(fLightColorUni[i], 1, &lightColor.fX); | |
| 249 fLightColor[i] = lightColor; | |
| 250 } | |
| 251 } | |
| 252 | |
| 253 const SkColor3f& ambientColor = shadowFP.ambientColor(); | |
| 254 if (ambientColor != fAmbientColor) { | |
| 255 pdman.set3fv(fAmbientColorUni, 1, &ambientColor.fX); | |
| 256 fAmbientColor = ambientColor; | |
| 257 } | |
| 258 } | |
| 259 | |
| 260 private: | |
| 261 SkVector3 fLightDir[SK_MAX_NON_AMBIENT_LIGHTS]; | |
| 262 GrGLSLProgramDataManager::UniformHandle fLightDirUni[SK_MAX_NON_AMBIENT_ LIGHTS]; | |
| 263 SkColor3f fLightColor[SK_MAX_NON_AMBIENT_LIGHTS]; | |
| 264 GrGLSLProgramDataManager::UniformHandle fLightColorUni[SK_MAX_NON_AMBIEN T_LIGHTS]; | |
| 265 | |
| 266 SkColor3f fAmbientColor; | |
| 267 GrGLSLProgramDataManager::UniformHandle fAmbientColorUni; | |
| 268 | |
| 269 int fNumDirLights; | |
| 270 }; | |
| 271 | |
| 272 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { | |
| 273 GLSLShadowFP::GenKey(*this, caps, b); | |
| 274 } | |
| 275 | |
| 276 const char* name() const override { return "shadowFP"; } | |
| 277 | |
| 278 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { | |
| 279 inout->mulByUnknownFourComponents(); | |
| 280 } | |
| 281 int32_t numLights() const { return fNumDirLights; } | |
| 282 const SkColor3f& ambientColor() const { return fAmbientColor; } | |
| 283 const SkVector3& lightDir(int i) const { | |
| 284 SkASSERT(i < fNumDirLights); | |
| 285 return fLightDir[i]; | |
| 286 } | |
| 287 const SkVector3& lightColor(int i) const { | |
| 288 SkASSERT(i < fNumDirLights); | |
| 289 return fLightColor[i]; | |
| 290 } | |
| 291 | |
| 292 private: | |
| 293 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLSLShadowFP; } | |
| 294 | |
| 295 bool onIsEqual(const GrFragmentProcessor& proc) const override { | |
| 296 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); | |
| 297 if (fAmbientColor != shadowFP.fAmbientColor || fNumDirLights != shadowFP .fNumDirLights) { | |
| 298 return false; | |
| 299 } | |
| 300 | |
| 301 for (int i = 0; i < fNumDirLights; i++) { | |
| 302 if (fLightDir[i] != shadowFP.fLightDir[i] || | |
| 303 fLightColor[i] == shadowFP.fLightColor[i]) { | |
|
vjiaoblack
2016/08/01 16:33:27
This should be a != ... fixing.
| |
| 304 return false; | |
| 305 } | |
| 306 } | |
| 307 | |
| 308 return true; | |
| 309 } | |
| 310 | |
| 311 int fNumDirLights; | |
| 312 | |
| 313 SkVector3 fLightDir[SK_MAX_NON_AMBIENT_LIGHTS]; | |
| 314 SkColor3f fLightColor[SK_MAX_NON_AMBIENT_LIGHTS]; | |
| 315 GrTextureAccess fDepthMapAccess[SK_MAX_NON_AMBIENT_LIGHTS]; | |
| 316 sk_sp<GrTexture> fTexture[SK_MAX_NON_AMBIENT_LIGHTS]; | |
| 317 | |
| 318 SkColor3f fAmbientColor; | |
| 319 }; | |
| 320 | |
| 321 //////////////////////////////////////////////////////////////////////////// | |
| 322 | |
| 323 sk_sp<GrFragmentProcessor> SkShadowShaderImpl::asFragmentProcessor(const AsFPArg s& fpargs) const { | |
| 324 | |
| 325 sk_sp<GrFragmentProcessor> povDepthFP = povDepthShader->asFragmentProcessor( fpargs); | |
| 326 | |
| 327 sk_sp<GrFragmentProcessor> diffuseFP = diffuseShader->asFragmentProcessor(fp args); | |
| 328 | |
| 329 sk_sp<GrFragmentProcessor> shadowfp = sk_make_sp<ShadowFP>(std::move(povDept hFP), | |
| 330 std::move(diffuse FP), | |
| 331 std::move(fLights ), | |
| 332 fpargs.fContext); | |
| 333 return shadowfp; | |
| 334 } | |
| 335 | |
| 336 | |
| 337 #endif | |
| 338 | |
| 339 //////////////////////////////////////////////////////////////////////////// | |
| 340 | |
| 341 bool SkShadowShaderImpl::isOpaque() const { | |
| 342 return diffuseShader->isOpaque(); | |
| 343 } | |
| 344 | |
| 345 SkShadowShaderImpl::ShadowShaderContext::ShadowShaderContext( | |
| 346 const SkShadowShaderImpl& shader, const ContextRec& rec, | |
| 347 SkShader::Context* povDepthContext, | |
| 348 SkShader::Context* diffuseContext, | |
| 349 void* heapAllocated) | |
| 350 : INHERITED(shader, rec) | |
| 351 , fPovDepthContext(povDepthContext) | |
| 352 , fDiffuseContext(diffuseContext) | |
| 353 , fHeapAllocated(heapAllocated) { | |
| 354 bool isOpaque = shader.isOpaque(); | |
| 355 | |
| 356 // update fFlags | |
| 357 uint32_t flags = 0; | |
| 358 if (isOpaque && (255 == this->getPaintAlpha())) { | |
| 359 flags |= kOpaqueAlpha_Flag; | |
| 360 } | |
| 361 | |
| 362 fFlags = flags; | |
| 363 } | |
| 364 | |
| 365 SkShadowShaderImpl::ShadowShaderContext::~ShadowShaderContext() { | |
| 366 // The dependencies have been created outside of the context on memory that was allocated by | |
| 367 // the onCreateContext() method. Call the destructors and free the memory. | |
| 368 fPovDepthContext->~Context(); | |
| 369 fDiffuseContext->~Context(); | |
| 370 | |
| 371 sk_free(fHeapAllocated); | |
| 372 } | |
| 373 | |
| 374 static inline SkPMColor convert(SkColor3f color, U8CPU a) { | |
| 375 if (color.fX <= 0.0f) { | |
| 376 color.fX = 0.0f; | |
| 377 } else if (color.fX >= 255.0f) { | |
| 378 color.fX = 255.0f; | |
| 379 } | |
| 380 | |
| 381 if (color.fY <= 0.0f) { | |
| 382 color.fY = 0.0f; | |
| 383 } else if (color.fY >= 255.0f) { | |
| 384 color.fY = 255.0f; | |
| 385 } | |
| 386 | |
| 387 if (color.fZ <= 0.0f) { | |
| 388 color.fZ = 0.0f; | |
| 389 } else if (color.fZ >= 255.0f) { | |
| 390 color.fZ = 255.0f; | |
| 391 } | |
| 392 | |
| 393 return SkPreMultiplyARGB(a, (int) color.fX, (int) color.fY, (int) color.fZ) ; | |
| 394 } | |
| 395 | |
| 396 // larger is better (fewer times we have to loop), but we shouldn't | |
| 397 // take up too much stack-space (each one here costs 16 bytes) | |
| 398 #define BUFFER_MAX 16 | |
| 399 void SkShadowShaderImpl::ShadowShaderContext::shadeSpan(int x, int y, | |
| 400 SkPMColor result[], int count) { | |
| 401 const SkShadowShaderImpl& lightShader = static_cast<const SkShadowShaderImpl &>(fShader); | |
| 402 | |
| 403 SkPMColor diffuse[BUFFER_MAX]; | |
| 404 | |
| 405 do { | |
| 406 int n = SkTMin(count, BUFFER_MAX); | |
| 407 | |
| 408 fPovDepthContext->shadeSpan(x, y, diffuse, n); | |
| 409 fDiffuseContext->shadeSpan(x, y, diffuse, n); | |
| 410 | |
| 411 for (int i = 0; i < n; ++i) { | |
| 412 | |
| 413 SkColor diffColor = SkUnPreMultiply::PMColorToColor(diffuse[i]); | |
| 414 | |
| 415 SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f); | |
| 416 // This is all done in linear unpremul color space (each component 0 ..255.0f though) | |
| 417 for (int l = 0; l < lightShader.fLights->numLights(); ++l) { | |
| 418 const SkLights::Light& light = lightShader.fLights->light(l); | |
| 419 | |
| 420 if (SkLights::Light::kAmbient_LightType == light.type()) { | |
| 421 accum.fX += light.color().fX * SkColorGetR(diffColor); | |
| 422 accum.fY += light.color().fY * SkColorGetG(diffColor); | |
| 423 accum.fZ += light.color().fZ * SkColorGetB(diffColor); | |
| 424 } else { | |
| 425 // scaling by fZ accounts for lighting direction | |
| 426 accum.fX += light.color().makeScale(light.dir().fZ).fX * SkC olorGetR(diffColor); | |
| 427 accum.fY += light.color().makeScale(light.dir().fZ).fY * SkC olorGetG(diffColor); | |
| 428 accum.fZ += light.color().makeScale(light.dir().fZ).fZ * SkC olorGetB(diffColor); | |
| 429 } | |
| 430 } | |
| 431 | |
| 432 result[i] = convert(accum, SkColorGetA(diffColor)); | |
| 433 } | |
| 434 | |
| 435 result += n; | |
| 436 x += n; | |
| 437 count -= n; | |
| 438 } while (count > 0); | |
| 439 } | |
| 440 | |
| 441 //////////////////////////////////////////////////////////////////////////// | |
| 442 | |
| 443 #ifndef SK_IGNORE_TO_STRING | |
| 444 void SkShadowShaderImpl::toString(SkString* str) const { | |
| 445 str->appendf("ShadowShader: ()"); | |
| 446 } | |
| 447 #endif | |
| 448 | |
| 449 sk_sp<SkFlattenable> SkShadowShaderImpl::CreateProc(SkReadBuffer& buf) { | |
|
vjiaoblack
2016/08/01 16:33:27
Add shadow maps into buf
| |
| 450 | |
| 451 // Discarding SkShader flattenable params | |
| 452 bool hasLocalMatrix = buf.readBool(); | |
| 453 SkAssertResult(!hasLocalMatrix); | |
| 454 | |
| 455 int numLights = buf.readInt(); | |
| 456 | |
| 457 SkLights::Builder builder; | |
| 458 | |
| 459 for (int l = 0; l < numLights; ++l) { | |
| 460 bool isAmbient = buf.readBool(); | |
| 461 | |
| 462 SkColor3f color; | |
| 463 if (!buf.readScalarArray(&color.fX, 3)) { | |
| 464 return nullptr; | |
| 465 } | |
| 466 | |
| 467 if (isAmbient) { | |
| 468 builder.add(SkLights::Light(color)); | |
| 469 } else { | |
| 470 SkVector3 dir; | |
| 471 if (!buf.readScalarArray(&dir.fX, 3)) { | |
| 472 return nullptr; | |
| 473 } | |
| 474 builder.add(SkLights::Light(color, dir)); | |
| 475 } | |
| 476 } | |
| 477 | |
| 478 sk_sp<SkLights> lights(builder.finish()); | |
| 479 | |
| 480 sk_sp<SkShader> povDepthShader(buf.readFlattenable<SkShader>()); | |
| 481 sk_sp<SkShader> diffuseShader(buf.readFlattenable<SkShader>()); | |
| 482 | |
| 483 return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader), | |
| 484 std::move(diffuseShader), | |
| 485 std::move(lights)); | |
| 486 } | |
| 487 | |
| 488 void SkShadowShaderImpl::flatten(SkWriteBuffer& buf) const { | |
| 489 this->INHERITED::flatten(buf); | |
| 490 | |
| 491 buf.writeInt(fLights->numLights()); | |
| 492 for (int l = 0; l < fLights->numLights(); ++l) { | |
| 493 const SkLights::Light& light = fLights->light(l); | |
| 494 | |
| 495 bool isAmbient = SkLights::Light::kAmbient_LightType == light.type(); | |
| 496 | |
| 497 buf.writeBool(isAmbient); | |
| 498 buf.writeScalarArray(&light.color().fX, 3); | |
| 499 if (!isAmbient) { | |
| 500 buf.writeScalarArray(&light.dir().fX, 3); | |
| 501 } | |
| 502 } | |
| 503 | |
| 504 buf.writeFlattenable(povDepthShader.get()); | |
| 505 buf.writeFlattenable(diffuseShader.get()); | |
| 506 } | |
| 507 | |
| 508 size_t SkShadowShaderImpl::onContextSize(const ContextRec& rec) const { | |
| 509 return sizeof(ShadowShaderContext); | |
| 510 } | |
| 511 | |
| 512 SkShader::Context* SkShadowShaderImpl::onCreateContext(const ContextRec& rec, | |
| 513 void* storage) const { | |
| 514 size_t heapRequired = povDepthShader->contextSize(rec) + | |
| 515 diffuseShader->contextSize(rec); | |
| 516 | |
| 517 void* heapAllocated = sk_malloc_throw(heapRequired); | |
| 518 | |
| 519 void* povDepthContextStorage = heapAllocated; | |
| 520 | |
| 521 SkShader::Context* povDepthContext = | |
| 522 povDepthShader->createContext(rec, povDepthContextStorage); | |
| 523 | |
| 524 if (!povDepthContext) { | |
| 525 sk_free(heapAllocated); | |
| 526 return nullptr; | |
| 527 } | |
| 528 | |
| 529 void* diffuseContextStorage = (char*)heapAllocated + povDepthShader->context Size(rec); | |
| 530 | |
| 531 SkShader::Context* diffuseContext = diffuseShader->createContext(rec, diffus eContextStorage); | |
| 532 if (!diffuseContext) { | |
| 533 sk_free(heapAllocated); | |
| 534 return nullptr; | |
| 535 } | |
| 536 | |
| 537 return new (storage) ShadowShaderContext(*this, rec, povDepthContext, diffus eContext, | |
| 538 heapAllocated); | |
| 539 } | |
| 540 | |
| 541 /////////////////////////////////////////////////////////////////////////////// | |
| 542 | |
| 543 sk_sp<SkShader> SkShadowShader::Make(sk_sp<SkShader> povDepthShader, | |
| 544 sk_sp<SkShader> diffuseShader, | |
| 545 sk_sp<SkLights> lights) { | |
| 546 if (!povDepthShader || !diffuseShader) { | |
| 547 // TODO: Use paint's color in absence of a diffuseShader | |
| 548 // TODO: Use a default implementation of normalSource instead | |
| 549 return nullptr; | |
| 550 } | |
| 551 | |
| 552 return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader), | |
| 553 std::move(diffuseShader), | |
| 554 std::move(lights)); | |
| 555 } | |
| 556 | |
| 557 /////////////////////////////////////////////////////////////////////////////// | |
| 558 | |
| 559 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) | |
| 560 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) | |
| 561 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | |
| 562 | |
| 563 /////////////////////////////////////////////////////////////////////////////// | |
| 564 | |
| 565 #endif | |
| OLD | NEW |