Chromium Code Reviews| Index: src/core/SkShadowShader.cpp |
| diff --git a/src/core/SkShadowShader.cpp b/src/core/SkShadowShader.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e9ff4fd950b60a34322dc6eee26c599493e3564a |
| --- /dev/null |
| +++ b/src/core/SkShadowShader.cpp |
| @@ -0,0 +1,558 @@ |
| +/* |
|
robertphillips
2016/07/26 16:15:05
2016
vjiaoblack
2016/07/27 15:26:27
Done.
|
| + * Copyright 2015 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#include "SkBitmapProcShader.h" |
| +#include "SkBitmapProcState.h" |
| +#include "SkColor.h" |
| +#include "SkEmptyShader.h" |
| +#include "SkErrorInternals.h" |
| +#include "SkShadowShader.h" |
| +#include "SkMathPriv.h" |
|
robertphillips
2016/07/26 16:15:05
Do we need SkNormalSource.h ?
vjiaoblack
2016/07/27 15:26:27
Done.
|
| +#include "SkNormalSource.h" |
| +#include "SkPoint3.h" |
| +#include "SkReadBuffer.h" |
| +#include "SkWriteBuffer.h" |
| + |
| +//////////////////////////////////////////////////////////////////////////// |
| + |
| +/** \class SkShadowShaderImpl |
| + This subclass of shader applies shadowing |
| +*/ |
| +class SkShadowShaderImpl : public SkShader { |
| +public: |
| + /** Create a new shadowing shader that shadows |
| + @param to do to do |
| + */ |
| + SkShadowShaderImpl(sk_sp<SkShader> povDepthShader, |
| + sk_sp<SkShader> diffuseShader, |
| + sk_sp<SkLights> lights) |
| + : povDepthShader(std::move(povDepthShader)) |
| + , diffuseShader(std::move(diffuseShader)) |
| + , fLights(std::move(lights)) { } |
| + |
| + bool isOpaque() const override; |
| + |
| +#if SK_SUPPORT_GPU |
| + sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, |
| + const SkMatrix& viewM, |
| + const SkMatrix* localMatrix, |
| + SkFilterQuality, |
| + SkSourceGammaTreatment) const override; |
| +#endif |
| + |
| + class ShadowShaderContext : public SkShader::Context { |
| + public: |
| + // The context takes ownership of the states. It will call their destructors |
| + // but will NOT free the memory. |
| + ShadowShaderContext(const SkShadowShaderImpl&, const ContextRec&, |
| + SkShader::Context* povDepthContext, |
| + SkShader::Context* diffuseContext, |
| + void* heapAllocated); |
| + |
| + ~ShadowShaderContext() override; |
| + |
| + void shadeSpan(int x, int y, SkPMColor[], int count) override; |
| + |
| + uint32_t getFlags() const override { return fFlags; } |
| + |
| + private: |
| + SkShader::Context* fDiffuseContext1; |
| + SkShader::Context* fDiffuseContext2; |
| + uint32_t fFlags; |
| + |
| + void* fHeapAllocated; |
| + |
| + typedef SkShader::Context INHERITED; |
| + }; |
| + |
| + SK_TO_STRING_OVERRIDE() |
|
robertphillips
2016/07/26 16:15:05
Tab this over ?
vjiaoblack
2016/07/27 15:26:27
Done.
|
| +SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkShadowShaderImpl) |
| + |
| +protected: |
| + void flatten(SkWriteBuffer&) const override; |
| + size_t onContextSize(const ContextRec&) const override; |
| + Context* onCreateContext(const ContextRec&, void*) const override; |
| + |
| +private: |
| + sk_sp<SkShader> povDepthShader; |
| + sk_sp<SkShader> diffuseShader; |
| + sk_sp<SkLights> fLights; |
| + |
| + friend class SkShadowShader; |
| + |
| + typedef SkShader INHERITED; |
| +}; |
| + |
| +//////////////////////////////////////////////////////////////////////////// |
| + |
| +#if SK_SUPPORT_GPU |
| + |
| +#include "GrCoordTransform.h" |
| +#include "GrFragmentProcessor.h" |
| +#include "GrInvariantOutput.h" |
| +#include "GrTextureAccess.h" |
| +#include "glsl/GrGLSLFragmentProcessor.h" |
| +#include "glsl/GrGLSLFragmentShaderBuilder.h" |
| +#include "glsl/GrGLSLProgramDataManager.h" |
| +#include "glsl/GrGLSLUniformHandler.h" |
| +#include "SkGr.h" |
| +#include "SkGrPriv.h" |
| +#include "SkSpecialImage.h" |
| +#include "SkImage_Base.h" |
| +#include "SkImageInfo.h" |
| +#include "GrContext.h" |
| +#include "GrContextOptions.h" |
| + |
| +class ShadowFP : public GrFragmentProcessor { |
| +public: |
| + ShadowFP(sk_sp<GrFragmentProcessor> povDepth, |
| + sk_sp<GrFragmentProcessor> diffuse, |
| + sk_sp<SkLights> lights) { |
| + |
| + // fuse all ambient lights into a single one |
| + // TODO do we support 4 directional lights, or just 4 lights? |
|
robertphillips
2016/07/26 16:15:05
Probably any number of ambient + 4 "real" ones.
M
vjiaoblack
2016/07/27 15:26:27
Done.
|
| + fAmbientColor.set(0.0f, 0.0f, 0.0f); |
| + |
|
robertphillips
2016/07/26 16:15:05
This doesn't seem quite right. fNumLights needs to
vjiaoblack
2016/07/27 15:26:28
I'll change fNumLights to fNumDirLights.
|
| + fNumLights = lights->numLights(); // refers to directional lights. |
|
robertphillips
2016/07/26 16:15:05
You probably can't have this assert but keep a sep
vjiaoblack
2016/07/27 15:26:27
Done.
|
| + SkASSERT(fNumLights <= 4 && fNumLights >= 0); |
| + for (int i = 0; i < lights->numLights(); ++i) { |
| + if (SkLights::Light::kAmbient_LightType == lights->light(i).type()) { |
| + fAmbientColor += lights->light(i).color(); |
| + } else { |
| + fLightColor[i] = lights->light(i).color(); |
| + fLightDir[i] = lights->light(i).dir(); |
| + fDepthMapAccess[i].reset(((SkImage_Base*)lights->light(i).getShadowMap().get())-> |
|
jvanverth1
2016/07/26 15:01:25
You might want to break this line up -- it's a lit
vjiaoblack
2016/07/27 15:26:28
Done.
|
| + asTextureRef(GrContext::Create(kOpenGL_GrBackend, (GrBackendContext) nullptr, GrContextOptions()), |
|
jvanverth1
2016/07/26 15:01:24
Lines too long (must be <= 100 chars)
vjiaoblack
2016/07/27 15:26:28
Done.
|
| + GrTextureParams::ClampNoFilter(), SkSourceGammaTreatment::kIgnore)); |
| + this->addTextureAccess(&fDepthMapAccess[i]); |
| +// // TODO: Call unref on the GrTexture somewhere. |
|
jvanverth1
2016/07/26 15:01:25
Is this still needed? Otherwise you have a memory
vjiaoblack
2016/07/27 15:26:28
Hm. I'm trying to fix this (and it seems like a pr
|
| + } |
| + } |
| + |
| + this->registerChildProcessor(std::move(povDepth)); |
| + this->registerChildProcessor(std::move(diffuse)); |
| + this->initClassID<ShadowFP>(); |
| + } |
| + |
| + class GLSLShadowFP : public GrGLSLFragmentProcessor { |
| + public: |
| + GLSLShadowFP() { } |
| + |
| + void emitCode(EmitArgs& args) override { |
| + |
| + GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| + GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| + |
| + // add uniforms |
| + int32_t numLights = args.fFp.cast<ShadowFP>().fNumLights; |
| + |
| + const char* lightDirUniName[4] = {nullptr}; |
| + const char* lightColorUniName[4] = {nullptr}; |
| + |
| + for (int i = 0; i < numLights; i++) { |
| + lightDirUniName[i] = nullptr; |
| + fLightDirUni[i] = uniformHandler->addUniform(kFragment_GrShaderFlag, |
| + kVec3f_GrSLType, kDefault_GrSLPrecision, |
|
jvanverth1
2016/07/26 15:01:25
Line up indents next to parens.
vjiaoblack
2016/07/27 15:26:28
Done.
|
| + "LightDir" + (i+1), &lightDirUniName[i]); |
| + |
| + lightColorUniName[i] = nullptr; |
| + fLightColorUni[i] = uniformHandler->addUniform(kFragment_GrShaderFlag, |
| + kVec3f_GrSLType, kDefault_GrSLPrecision, |
|
jvanverth1
2016/07/26 15:01:24
Lines too long.
vjiaoblack
2016/07/27 15:26:28
Done.
|
| + "LightColor" + (i+1), &lightColorUniName[i]); |
| + } |
| + |
| + SkString povDepth("povDepth"); |
| + this->emitChild(0, nullptr, &povDepth, args); |
| + |
| + SkString diffuseColor("inDiffuseColor"); |
| + this->emitChild(1, nullptr, &diffuseColor, args); |
| + |
| + SkString depthMaps[4]; |
| + |
| + for (int i = 0; i < numLights; i++) { |
| + const char* povCoord = "povCoord" + i; |
| + |
| + fragBuilder->codeAppendf("vec2 %s = vMatrixCoord_0_1_Stage0 + (vec2(%s) * povDepth.b * 255 / 400);", |
|
jvanverth1
2016/07/26 15:01:25
Line too long. Also, it's not clear to me what thi
vjiaoblack
2016/07/27 15:26:28
Done.
|
| + povCoord, lightDirUniName[i]); |
| + |
| + fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSamplers[i], |
| + povCoord, |
| + kVec2f_GrSLType); |
| + } |
| + |
| + const char* ambientColorUniName = nullptr; |
| + fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
| + kVec3f_GrSLType, kDefault_GrSLPrecision, |
| + "AmbientColor", &ambientColorUniName); |
| + |
| + fragBuilder->codeAppendf("vec4 resultDiffuseColor = %s;", diffuseColor.c_str()); |
| + |
| + // all the normal vectors point straight up |
| + SkString totalLightColor("totalLightColor"); |
| + fragBuilder->codeAppendf("vec3 %s = vec3(0,0,0);", totalLightColor.c_str()); |
| + |
| + for (int i = 0; i < numLights; i++) { |
| + fragBuilder->codeAppendf("if (%s.b >= %s.b) {", |
| + povDepth.c_str(), depthMaps[i].c_str()); |
| + fragBuilder->codeAppendf("%s += dot(vec3(0,0,1), %s) * %s;", totalLightColor.c_str(), |
|
jvanverth1
2016/07/26 15:01:24
Line too long.
vjiaoblack
2016/07/27 15:26:28
Done.
|
| + lightDirUniName[i], lightColorUniName[i]); |
| + fragBuilder->codeAppendf("}"); |
| + } |
| + |
| + |
| + fragBuilder->codeAppendf("%s += %s;", totalLightColor.c_str(), ambientColorUniName); |
| + fragBuilder->codeAppendf("resultDiffuseColor *= vec4(%s, 1);", totalLightColor.c_str()); |
| + |
| + fragBuilder->codeAppendf("%s = resultDiffuseColor;", args.fOutputColor); |
| + } |
| + |
| + static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, |
| + GrProcessorKeyBuilder* b) { |
| + const ShadowFP& shadowFP = proc.cast<ShadowFP>(); |
| + b->add32(shadowFP.fNumLights); |
| + } |
| + |
| + protected: |
| + void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override { |
| + const ShadowFP &shadowFP = proc.cast<ShadowFP>(); |
| + |
|
robertphillips
2016/07/26 16:15:05
We set fNumLights in the ctor. Is it correct to re
vjiaoblack
2016/07/27 15:26:28
We set fNumLights in the ShadowFP constructor. We
|
| + fNumLights = shadowFP.numLights(); |
| + |
| + SkASSERT(fNumLights <= 4); |
| + |
| + for (int i = 0; i < fNumLights; i++) { |
|
robertphillips
2016/07/26 16:15:05
Do we need the "1" suffixes?
vjiaoblack
2016/07/27 15:26:28
Done.
|
| + const SkVector3 &lightDir1 = shadowFP.lightDir(i); |
| + if (lightDir1 != fLightDir[i]) { |
| + pdman.set3fv(fLightDirUni[i], 1, &lightDir1.fX); |
| + fLightDir[i] = lightDir1; |
| + } |
| + const SkColor3f &lightColor1 = shadowFP.lightColor(i); |
| + if (lightColor1 != fLightColor[i]) { |
| + pdman.set3fv(fLightColorUni[i], 1, &lightColor1.fX); |
| + fLightColor[i] = lightColor1; |
| + } |
| + } |
| + |
| + const SkColor3f& ambientColor = shadowFP.ambientColor(); |
| + if (ambientColor != fAmbientColor) { |
| + pdman.set3fv(fAmbientColorUni, 1, &ambientColor.fX); |
| + fAmbientColor = ambientColor; |
| + } |
| + } |
| + |
| + private: |
| + SkVector3 fLightDir[4]; |
| + GrGLSLProgramDataManager::UniformHandle fLightDirUni[4]; |
| + SkColor3f fLightColor[4]; |
| + GrGLSLProgramDataManager::UniformHandle fLightColorUni[4]; |
| + |
| + SkColor3f fAmbientColor; |
| + GrGLSLProgramDataManager::UniformHandle fAmbientColorUni; |
| + |
|
robertphillips
2016/07/26 16:15:05
can this just be an int?
vjiaoblack
2016/07/27 15:26:27
Done.
|
| + int32_t fNumLights; |
| + }; |
| + |
| + void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override { |
| + GLSLShadowFP::GenKey(*this, caps, b); |
| + } |
| + |
| + const char* name() const override { return "shadowFP"; } |
| + |
| + void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
| + inout->mulByUnknownFourComponents(); |
| + } |
| + int32_t numLights() const { return fNumLights; } |
| + const SkColor3f& ambientColor() const { return fAmbientColor; } |
| + const SkVector3& lightDir(int i) const { |
| + SkASSERT(i < fNumLights); |
| + return fLightDir[i]; |
| + } |
| + const SkVector3& lightColor(int i) const { |
| + SkASSERT(i < fNumLights); |
| + return fLightColor[i]; |
| + } |
| + |
| +private: |
| + GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLSLShadowFP; } |
| + |
| + bool onIsEqual(const GrFragmentProcessor& proc) const override { |
| + const ShadowFP& shadowFP = proc.cast<ShadowFP>(); |
|
robertphillips
2016/07/26 16:15:05
Shouldn't we check if the number of lights is the
vjiaoblack
2016/07/27 15:26:27
Done.
|
| + return fLightDir[0] == shadowFP.fLightDir[0] && |
| + fLightColor[0] == shadowFP.fLightColor[0] && |
| + fLightDir[1] == shadowFP.fLightDir[1] && |
| + fLightColor[1] == shadowFP.fLightColor[1] && |
| + fLightDir[2] == shadowFP.fLightDir[2] && |
| + fLightColor[2] == shadowFP.fLightColor[2] && |
| + fLightDir[3] == shadowFP.fLightDir[3] && |
| + fLightColor[3] == shadowFP.fLightColor[3] && |
| + fAmbientColor == shadowFP.fAmbientColor; |
| + } |
| + |
|
robertphillips
2016/07/26 16:15:05
line this guy up with the rest ?
vjiaoblack
2016/07/27 15:26:27
Done.
|
| + int fNumLights; |
| + |
| + SkVector3 fLightDir[4]; |
| + SkColor3f fLightColor[4]; |
| + GrTextureAccess fDepthMapAccess[4]; |
| + |
| + SkColor3f fAmbientColor; |
| +}; |
| + |
| +//////////////////////////////////////////////////////////////////////////// |
| + |
| +sk_sp<GrFragmentProcessor> SkShadowShaderImpl::asFragmentProcessor( |
| + GrContext* context, |
| + const SkMatrix& viewM, |
| + const SkMatrix* localMatrix, |
| + SkFilterQuality filterQuality, |
| + SkSourceGammaTreatment gammaTreatment) const { |
| + |
|
robertphillips
2016/07/26 16:15:05
I'm not in love with the D_FP* names
vjiaoblack
2016/07/27 15:26:28
Done.
|
| + sk_sp<GrFragmentProcessor> D_FP1 = povDepthShader->asFragmentProcessor(context, viewM, |
| + localMatrix, filterQuality, |
| + gammaTreatment); |
| + |
| + sk_sp<GrFragmentProcessor> D_FP2 = diffuseShader->asFragmentProcessor(context, viewM, |
| + localMatrix, filterQuality, |
|
jvanverth1
2016/07/26 15:01:25
Line too long.
vjiaoblack
2016/07/27 15:26:27
Done.
|
| + gammaTreatment); |
| + |
| + sk_sp<GrFragmentProcessor> shadowfp = sk_make_sp<ShadowFP>(D_FP1, D_FP2, fLights); |
| + return shadowfp; |
| +} |
| + |
| + |
| +#endif |
| + |
| +//////////////////////////////////////////////////////////////////////////// |
| + |
| +bool SkShadowShaderImpl::isOpaque() const { |
| + return povDepthShader->isOpaque() || diffuseShader->isOpaque(); |
| +} |
| + |
| +SkShadowShaderImpl::ShadowShaderContext::ShadowShaderContext( |
| + const SkShadowShaderImpl& shader, const ContextRec& rec, |
| + SkShader::Context* povDepthContext, |
| + SkShader::Context* diffuseContext, |
| + void* heapAllocated) |
| + : INHERITED(shader, rec) |
| + , fDiffuseContext1(povDepthContext) |
| + , fDiffuseContext2(diffuseContext) |
| + , fHeapAllocated(heapAllocated) { |
| + bool isOpaque = shader.isOpaque(); |
| + |
| + // update fFlags |
| + uint32_t flags = 0; |
| + if (isOpaque && (255 == this->getPaintAlpha())) { |
| + flags |= kOpaqueAlpha_Flag; |
| + } |
| + |
| + fFlags = flags; |
| +} |
| + |
| +SkShadowShaderImpl::ShadowShaderContext::~ShadowShaderContext() { |
| + // The dependencies have been created outside of the context on memory that was allocated by |
| + // the onCreateContext() method. Call the destructors and free the memory. |
| + fDiffuseContext1->~Context(); |
| + fDiffuseContext2->~Context(); |
| + |
| + sk_free(fHeapAllocated); |
| +} |
| + |
| +static inline SkPMColor convert(SkColor3f color, U8CPU a) { |
| + if (color.fX <= 0.0f) { |
| + color.fX = 0.0f; |
| + } else if (color.fX >= 255.0f) { |
| + color.fX = 255.0f; |
| + } |
| + |
| + if (color.fY <= 0.0f) { |
| + color.fY = 0.0f; |
| + } else if (color.fY >= 255.0f) { |
| + color.fY = 255.0f; |
| + } |
| + |
| + if (color.fZ <= 0.0f) { |
| + color.fZ = 0.0f; |
| + } else if (color.fZ >= 255.0f) { |
| + color.fZ = 255.0f; |
| + } |
| +// printf(); |
| + return SkPreMultiplyARGB(a, (int) color.fX, (int) color.fY, (int) color.fZ); |
| +} |
| + |
| +// larger is better (fewer times we have to loop), but we shouldn't |
| +// take up too much stack-space (each one here costs 16 bytes) |
| +#define BUFFER_MAX 16 |
| +void SkShadowShaderImpl::ShadowShaderContext::shadeSpan(int x, int y, |
| + SkPMColor result[], int count) { |
| + const SkShadowShaderImpl& lightShader = static_cast<const SkShadowShaderImpl&>(fShader); |
| + |
| + SkPMColor diffuse[BUFFER_MAX]; |
| + SkPoint3 normals[BUFFER_MAX]; |
| + |
| + do { |
| + int n = SkTMin(count, BUFFER_MAX); |
| + |
| + fDiffuseContext1->shadeSpan(x, y, diffuse, n); |
| + fDiffuseContext2->shadeSpan(x, y, diffuse, n); |
| + |
| + for (int i = 0; i < n; ++i) { |
| + |
| + SkColor diffColor = SkUnPreMultiply::PMColorToColor(diffuse[i]); |
| + |
| + SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f); |
| + // This is all done in linear unpremul color space (each component 0..255.0f though) |
| + for (int l = 0; l < lightShader.fLights->numLights(); ++l) { |
| + const SkLights::Light& light = lightShader.fLights->light(l); |
| + |
| + if (SkLights::Light::kAmbient_LightType == light.type()) { |
| + accum += light.color().makeScale(255.0f); |
| + } else { |
| + SkScalar NdotL = normals[i].dot(light.dir()); |
| + if (NdotL < 0.0f) { |
| + NdotL = 0.0f; |
| + } |
| + |
| + NdotL = 1.0f; |
| + |
| + accum.fX += light.color().fX * SkColorGetR(diffColor) * NdotL; |
| + accum.fY += light.color().fY * SkColorGetG(diffColor) * NdotL; |
| + accum.fZ += light.color().fZ * SkColorGetB(diffColor) * NdotL; |
| + } |
| + } |
| + |
| + result[i] = convert(accum, SkColorGetA(diffColor)); |
| + } |
| + |
| + result += n; |
| + x += n; |
| + count -= n; |
| + } while (count > 0); |
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////// |
| + |
| +#ifndef SK_IGNORE_TO_STRING |
| +void SkShadowShaderImpl::toString(SkString* str) const { |
| + str->appendf("ShadowShader: ()"); |
| +} |
| +#endif |
| + |
| +sk_sp<SkFlattenable> SkShadowShaderImpl::CreateProc(SkReadBuffer& buf) { |
| + |
| + // Discarding SkShader flattenable params |
| + bool hasLocalMatrix = buf.readBool(); |
| + SkAssertResult(!hasLocalMatrix); |
| + |
| + int numLights = buf.readInt(); |
| + |
| + SkLights::Builder builder; |
| + |
| + for (int l = 0; l < numLights; ++l) { |
| + bool isAmbient = buf.readBool(); |
| + |
| + SkColor3f color; |
| + if (!buf.readScalarArray(&color.fX, 3)) { |
| + return nullptr; |
| + } |
| + |
| + if (isAmbient) { |
| + builder.add(SkLights::Light(color)); |
| + } else { |
| + SkVector3 dir; |
| + if (!buf.readScalarArray(&dir.fX, 3)) { |
| + return nullptr; |
| + } |
| + builder.add(SkLights::Light(color, dir)); |
| + } |
| + } |
| + |
| + sk_sp<SkLights> lights(builder.finish()); |
| + |
| + sk_sp<SkShader> povDepthShader(buf.readFlattenable<SkShader>()); |
| + sk_sp<SkShader> diffuseShader(buf.readFlattenable<SkShader>()); |
| + |
| + return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader), |
| + std::move(diffuseShader), |
| + std::move(lights)); |
| +} |
| + |
| +void SkShadowShaderImpl::flatten(SkWriteBuffer& buf) const { |
| + this->INHERITED::flatten(buf); |
| + |
| + buf.writeInt(fLights->numLights()); |
| + for (int l = 0; l < fLights->numLights(); ++l) { |
| + const SkLights::Light& light = fLights->light(l); |
| + |
| + bool isAmbient = SkLights::Light::kAmbient_LightType == light.type(); |
| + |
| + buf.writeBool(isAmbient); |
| + buf.writeScalarArray(&light.color().fX, 3); |
| + if (!isAmbient) { |
| + buf.writeScalarArray(&light.dir().fX, 3); |
| + } |
| + } |
| + |
| + buf.writeFlattenable(povDepthShader.get()); |
| + buf.writeFlattenable(diffuseShader.get()); |
| +} |
| + |
| +size_t SkShadowShaderImpl::onContextSize(const ContextRec& rec) const { |
| + return sizeof(ShadowShaderContext); |
| +} |
| + |
| +SkShader::Context* SkShadowShaderImpl::onCreateContext(const ContextRec& rec, |
| + void* storage) const { |
| + size_t heapRequired = povDepthShader->contextSize(rec) + |
| + diffuseShader->contextSize(rec); |
| + |
| + void* heapAllocated = sk_malloc_throw(heapRequired); |
| + |
| + void* povDepthContextStorage = heapAllocated; |
| + |
| + SkShader::Context* povDepthContext = povDepthShader->createContext(rec, povDepthContextStorage); |
| + if (!povDepthContext) { |
| + sk_free(heapAllocated); |
| + return nullptr; |
| + } |
| + |
| + void* diffuseContextStorage = (char*)heapAllocated + povDepthShader->contextSize(rec); |
| + |
| + SkShader::Context* diffuseContext2 = diffuseShader->createContext(rec, diffuseContextStorage); |
| + if (!diffuseContext2) { |
| + sk_free(heapAllocated); |
| + return nullptr; |
| + } |
| + |
| + |
| + return new (storage) ShadowShaderContext(*this, rec, povDepthContext, diffuseContext2, |
| + heapAllocated); |
| +} |
| + |
| +/////////////////////////////////////////////////////////////////////////////// |
| + |
| +sk_sp<SkShader> SkShadowShader::Make(sk_sp<SkShader> povDepthShader, |
| + sk_sp<SkShader> diffuseShader, |
| + sk_sp<SkLights> lights) { |
| + if (!povDepthShader || !diffuseShader) { |
| + // TODO: Use paint's color in absence of a diffuseShader |
| + // TODO: Use a default implementation of normalSource instead |
| + return nullptr; |
| + } |
| + |
| + SkASSERT(lights->numLights() <= 4); |
| + |
| + return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader), |
| + std::move(diffuseShader), |
| + std::move(lights)); |
| +} |
| + |
| +/////////////////////////////////////////////////////////////////////////////// |
| + |
| +SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) |
| + SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) |
| +SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| + |
| +/////////////////////////////////////////////////////////////////////////////// |