aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--gm/lighting.cpp3
-rw-r--r--gm/lightingshader.cpp111
-rw-r--r--gyp/effects.gypi2
-rwxr-xr-xsamplecode/SampleLighting.cpp348
-rw-r--r--src/core/SkBitmapProcShader.cpp27
-rw-r--r--src/core/SkBitmapProcShader.h6
-rw-r--r--src/core/SkBitmapProcState.h1
-rw-r--r--src/effects/SkLightingShader.cpp579
-rw-r--r--src/effects/SkLightingShader.h45
9 files changed, 762 insertions, 360 deletions
diff --git a/gm/lighting.cpp b/gm/lighting.cpp
index b721fef6c3..f6d236c382 100644
--- a/gm/lighting.cpp
+++ b/gm/lighting.cpp
@@ -170,7 +170,6 @@ private:
//////////////////////////////////////////////////////////////////////////////
-static GM* MyFactory(void*) { return new ImageLightingGM; }
-static GMRegistry reg(MyFactory);
+DEF_GM( return SkNEW(ImageLightingGM); )
}
diff --git a/gm/lightingshader.cpp b/gm/lightingshader.cpp
new file mode 100644
index 0000000000..e920fb5cc3
--- /dev/null
+++ b/gm/lightingshader.cpp
@@ -0,0 +1,111 @@
+/*
+ * 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 "gm.h"
+
+#include "SkColorPriv.h"
+#include "SkLightingShader.h"
+
+static SkBitmap make_checkerboard(int texSize) {
+ SkBitmap bitmap;
+ bitmap.allocN32Pixels(texSize, texSize);
+
+ SkCanvas canvas(bitmap);
+ sk_tool_utils::draw_checkerboard(&canvas,
+ sk_tool_utils::color_to_565(0x0),
+ sk_tool_utils::color_to_565(0xFF804020),
+ 16);
+ return bitmap;
+}
+
+// Create a hemispherical normal map
+static SkBitmap make_normalmap(int texSize) {
+ SkBitmap hemi;
+ hemi.allocN32Pixels(texSize, texSize);
+
+ for (int y = 0; y < texSize; ++y) {
+ for (int x = 0; x < texSize; ++x) {
+ SkScalar locX = (x + 0.5f - texSize/2.0f) / (texSize/2.0f);
+ SkScalar locY = (y + 0.5f - texSize/2.0f) / (texSize/2.0f);
+
+ SkScalar locZ = locX * locX + locY * locY;
+ if (locZ >= 1.0f) {
+ locX = 0.0f;
+ locY = 0.0f;
+ locZ = 0.0f;
+ }
+ locZ = sqrt(1.0f - locZ);
+ unsigned char r = static_cast<unsigned char>((0.5f * locX + 0.5f) * 255);
+ unsigned char g = static_cast<unsigned char>((-0.5f * locY + 0.5f) * 255);
+ unsigned char b = static_cast<unsigned char>((0.5f * locZ + 0.5f) * 255);
+ *hemi.getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b);
+ }
+ }
+
+ return hemi;
+}
+
+
+namespace skiagm {
+
+// This GM exercises lighting shaders.
+class LightingShaderGM : public GM {
+public:
+ LightingShaderGM() {
+ this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
+ }
+
+protected:
+
+ SkString onShortName() override {
+ return SkString("lightingshader");
+ }
+
+ SkISize onISize() override {
+ return SkISize::Make(kTexSize, kTexSize);
+ }
+
+ void onOnceBeforeDraw() override {
+ fDiffuse = make_checkerboard(kTexSize);
+ fNormalMap = make_normalmap(kTexSize);
+ }
+
+ void onDraw(SkCanvas* canvas) override {
+
+ SkColor ambient = SkColorSetRGB(0x1f, 0x1f, 0x1f);
+
+ SkLightingShader::Light light;
+ light.fColor = SkColorSetRGB(0xff, 0xff, 0xff);
+ light.fDirection.fX = 0.0f;
+ light.fDirection.fY = 0.0f;
+ light.fDirection.fZ = 1.0f;
+
+ SkAutoTUnref<SkShader> fShader(SkLightingShader::Create(fDiffuse, fNormalMap,
+ light, ambient));
+
+ SkPaint paint;
+ paint.setShader(fShader);
+
+ SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize));
+
+ canvas->drawRect(r, paint);
+ }
+
+private:
+ static const int kTexSize = 128;
+
+ SkBitmap fDiffuse;
+ SkBitmap fNormalMap;
+
+ typedef GM INHERITED;
+};
+
+//////////////////////////////////////////////////////////////////////////////
+
+DEF_GM( return SkNEW(LightingShaderGM); )
+
+}
diff --git a/gyp/effects.gypi b/gyp/effects.gypi
index e0ed2c43da..04dcd08875 100644
--- a/gyp/effects.gypi
+++ b/gyp/effects.gypi
@@ -45,6 +45,8 @@
'<(skia_src_path)/effects/SkLayerRasterizer.cpp',
'<(skia_src_path)/effects/SkLerpXfermode.cpp',
'<(skia_src_path)/effects/SkLightingImageFilter.cpp',
+ '<(skia_src_path)/effects/SkLightingShader.h',
+ '<(skia_src_path)/effects/SkLightingShader.cpp',
'<(skia_src_path)/effects/SkLumaColorFilter.cpp',
'<(skia_src_path)/effects/SkMagnifierImageFilter.cpp',
'<(skia_src_path)/effects/SkMatrixConvolutionImageFilter.cpp',
diff --git a/samplecode/SampleLighting.cpp b/samplecode/SampleLighting.cpp
index 53a542ec2e..079cb68708 100755
--- a/samplecode/SampleLighting.cpp
+++ b/samplecode/SampleLighting.cpp
@@ -9,339 +9,19 @@
#include "Resources.h"
#include "SkCanvas.h"
-#include "SkErrorInternals.h"
-#include "SkGr.h"
-#include "SkPoint3.h"
-#include "SkReadBuffer.h"
-#include "SkShader.h"
-#include "SkWriteBuffer.h"
-#include "GrFragmentProcessor.h"
-#include "GrCoordTransform.h"
-#include "gl/GrGLProcessor.h"
-#include "gl/builders/GrGLProgramBuilder.h"
-
-///////////////////////////////////////////////////////////////////////////////
-
-class LightingShader : public SkShader {
-public:
- struct Light {
- SkVector3 fDirection;
- SkColor fColor; // assumed to be linear color
- };
-
- LightingShader(const SkBitmap& diffuse, const SkBitmap& normal, const Light& light,
- const SkColor ambient)
- : fDiffuseMap(diffuse)
- , fNormalMap(normal)
- , fLight(light)
- , fAmbientColor(ambient) {}
-
- SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(LightingShader);
-
- void flatten(SkWriteBuffer& buf) const override {
- buf.writeBitmap(fDiffuseMap);
- buf.writeBitmap(fNormalMap);
- buf.writeScalarArray(&fLight.fDirection.fX, 3);
- buf.writeColor(fLight.fColor);
- buf.writeColor(fAmbientColor);
- }
-
- bool asFragmentProcessor(GrContext*, const SkPaint& paint, const SkMatrix& viewM,
- const SkMatrix* localMatrix, GrColor* color,
- GrProcessorDataManager*, GrFragmentProcessor** fp) const override;
-
- SkShader::BitmapType asABitmap(SkBitmap* bitmap, SkMatrix* matrix,
- SkShader::TileMode* xy) const override {
- if (bitmap) {
- *bitmap = fDiffuseMap;
- }
- if (matrix) {
- matrix->reset();
- }
- if (xy) {
- xy[0] = kClamp_TileMode;
- xy[1] = kClamp_TileMode;
- }
- return kDefault_BitmapType;
- }
-
-#ifndef SK_IGNORE_TO_STRING
- void toString(SkString* str) const override {
- str->appendf("LightingShader: ()");
- }
-#endif
-
- void setLight(const Light& light) { fLight = light; }
-
-private:
- SkBitmap fDiffuseMap;
- SkBitmap fNormalMap;
- Light fLight;
- SkColor fAmbientColor;
-};
-
-SkFlattenable* LightingShader::CreateProc(SkReadBuffer& buf) {
- SkBitmap diffuse;
- if (!buf.readBitmap(&diffuse)) {
- return NULL;
- }
- diffuse.setImmutable();
-
- SkBitmap normal;
- if (!buf.readBitmap(&normal)) {
- return NULL;
- }
- normal.setImmutable();
-
- Light light;
- if (!buf.readScalarArray(&light.fDirection.fX, 3)) {
- return NULL;
- }
- light.fColor = buf.readColor();
-
- SkColor ambient = buf.readColor();
-
- return SkNEW_ARGS(LightingShader, (diffuse, normal, light, ambient));
-}
-
-////////////////////////////////////////////////////////////////////////////
-
-class LightingFP : public GrFragmentProcessor {
-public:
- LightingFP(GrTexture* diffuse, GrTexture* normal, const SkMatrix& matrix,
- SkVector3 lightDir, GrColor lightColor, GrColor ambientColor)
- : fDeviceTransform(kDevice_GrCoordSet, matrix)
- , fDiffuseTextureAccess(diffuse)
- , fNormalTextureAccess(normal)
- , fLightDir(lightDir)
- , fLightColor(lightColor)
- , fAmbientColor(ambientColor) {
- this->addCoordTransform(&fDeviceTransform);
- this->addTextureAccess(&fDiffuseTextureAccess);
- this->addTextureAccess(&fNormalTextureAccess);
-
- this->initClassID<LightingFP>();
- }
-
- class LightingGLFP : public GrGLFragmentProcessor {
- public:
- LightingGLFP() : fLightColor(GrColor_ILLEGAL) {
- fLightDir.fX = 10000.0f;
- }
-
- void emitCode(EmitArgs& args) override {
-
- GrGLFragmentBuilder* fpb = args.fBuilder->getFragmentShaderBuilder();
-
- // add uniforms
- const char* lightDirUniName = NULL;
- fLightDirUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
- kVec3f_GrSLType, kDefault_GrSLPrecision,
- "LightDir", &lightDirUniName);
-
- const char* lightColorUniName = NULL;
- fLightColorUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
- kVec4f_GrSLType, kDefault_GrSLPrecision,
- "LightColor", &lightColorUniName);
-
- const char* ambientColorUniName = NULL;
- fAmbientColorUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
- kVec4f_GrSLType, kDefault_GrSLPrecision,
- "AmbientColor", &ambientColorUniName);
-
- fpb->codeAppend("vec4 diffuseColor = ");
- fpb->appendTextureLookupAndModulate(args.fInputColor, args.fSamplers[0],
- args.fCoords[0].c_str(), args.fCoords[0].getType());
- fpb->codeAppend(";");
-
- fpb->codeAppend("vec4 normalColor = ");
- fpb->appendTextureLookup(args.fSamplers[1], args.fCoords[0].c_str(),
- args.fCoords[0].getType());
- fpb->codeAppend(";");
-
- fpb->codeAppend("vec3 normal = normalize(2.0*(normalColor.rgb - vec3(0.5)));");
- fpb->codeAppendf("vec3 lightDir = normalize(%s);", lightDirUniName);
- fpb->codeAppend("float NdotL = dot(normal, lightDir);");
- // diffuse light
- fpb->codeAppendf("vec3 result = %s.rgb*diffuseColor.rgb*NdotL;", lightColorUniName);
- // ambient light
- fpb->codeAppendf("result += %s.rgb;", ambientColorUniName);
- fpb->codeAppendf("%s = vec4(result.rgb, diffuseColor.a);", args.fOutputColor);
- }
-
- void setData(const GrGLProgramDataManager& pdman, const GrProcessor& proc) override {
- const LightingFP& lightingFP = proc.cast<LightingFP>();
-
- SkVector3 lightDir = lightingFP.lightDir();
- if (lightDir != fLightDir) {
- pdman.set3fv(fLightDirUni, 1, &lightDir.fX);
- fLightDir = lightDir;
- }
-
- GrColor lightColor = lightingFP.lightColor();
- if (lightColor != fLightColor) {
- GrGLfloat c[4];
- GrColorToRGBAFloat(lightColor, c);
- pdman.set4fv(fLightColorUni, 1, c);
- fLightColor = lightColor;
- }
-
- GrColor ambientColor = lightingFP.ambientColor();
- if (ambientColor != fAmbientColor) {
- GrGLfloat c[4];
- GrColorToRGBAFloat(ambientColor, c);
- pdman.set4fv(fAmbientColorUni, 1, c);
- fAmbientColor = ambientColor;
- }
- }
-
- static void GenKey(const GrProcessor& proc, const GrGLSLCaps&,
- GrProcessorKeyBuilder* b) {
-// const LightingFP& lightingFP = proc.cast<LightingFP>();
- // only one shader generated currently
- b->add32(0x0);
- }
-
- private:
- SkVector3 fLightDir;
- GrGLProgramDataManager::UniformHandle fLightDirUni;
-
- GrColor fLightColor;
- GrGLProgramDataManager::UniformHandle fLightColorUni;
-
- GrColor fAmbientColor;
- GrGLProgramDataManager::UniformHandle fAmbientColorUni;
- };
-
- GrGLFragmentProcessor* createGLInstance() const override { return SkNEW(LightingGLFP); }
-
- void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {
- LightingGLFP::GenKey(*this, caps, b);
- }
-
- const char* name() const override { return "LightingFP"; }
-
- void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
- inout->mulByUnknownFourComponents();
- }
-
- SkVector3 lightDir() const { return fLightDir; }
- GrColor lightColor() const { return fLightColor; }
- GrColor ambientColor() const { return fAmbientColor; }
-
-private:
- bool onIsEqual(const GrFragmentProcessor& proc) const override {
- const LightingFP& lightingFP = proc.cast<LightingFP>();
- return fDeviceTransform == lightingFP.fDeviceTransform &&
- fDiffuseTextureAccess == lightingFP.fDiffuseTextureAccess &&
- fNormalTextureAccess == lightingFP.fNormalTextureAccess &&
- fLightDir == lightingFP.fLightDir &&
- fLightColor == lightingFP.fLightColor &&
- fAmbientColor == lightingFP.fAmbientColor;
- }
-
- GrCoordTransform fDeviceTransform;
- GrTextureAccess fDiffuseTextureAccess;
- GrTextureAccess fNormalTextureAccess;
- SkVector3 fLightDir;
- GrColor fLightColor;
- GrColor fAmbientColor;
-};
-
-bool LightingShader::asFragmentProcessor(GrContext* context, const SkPaint& paint,
- const SkMatrix& viewM, const SkMatrix* localMatrix,
- GrColor* color, GrProcessorDataManager*,
- GrFragmentProcessor** fp) const {
- // we assume diffuse and normal maps have same width and height
- // TODO: support different sizes
- SkASSERT(fDiffuseMap.width() == fNormalMap.width() &&
- fDiffuseMap.height() == fNormalMap.height());
- SkMatrix matrix;
- matrix.setIDiv(fDiffuseMap.width(), fDiffuseMap.height());
-
- SkMatrix lmInverse;
- if (!this->getLocalMatrix().invert(&lmInverse)) {
- return false;
- }
- if (localMatrix) {
- SkMatrix inv;
- if (!localMatrix->invert(&inv)) {
- return false;
- }
- lmInverse.postConcat(inv);
- }
- matrix.preConcat(lmInverse);
-
- // Must set wrap and filter on the sampler before requesting a texture. In two places below
- // we check the matrix scale factors to determine how to interpret the filter quality setting.
- // This completely ignores the complexity of the drawVertices case where explicit local coords
- // are provided by the caller.
- GrTextureParams::FilterMode textureFilterMode = GrTextureParams::kBilerp_FilterMode;
- switch (paint.getFilterQuality()) {
- case kNone_SkFilterQuality:
- textureFilterMode = GrTextureParams::kNone_FilterMode;
- break;
- case kLow_SkFilterQuality:
- textureFilterMode = GrTextureParams::kBilerp_FilterMode;
- break;
- case kMedium_SkFilterQuality:{
- SkMatrix matrix;
- matrix.setConcat(viewM, this->getLocalMatrix());
- if (matrix.getMinScale() < SK_Scalar1) {
- textureFilterMode = GrTextureParams::kMipMap_FilterMode;
- } else {
- // Don't trigger MIP level generation unnecessarily.
- textureFilterMode = GrTextureParams::kBilerp_FilterMode;
- }
- break;
- }
- case kHigh_SkFilterQuality:
- default:
- SkErrorInternals::SetError(kInvalidPaint_SkError,
- "Sorry, I don't understand the filtering "
- "mode you asked for. Falling back to "
- "MIPMaps.");
- textureFilterMode = GrTextureParams::kMipMap_FilterMode;
- break;
-
- }
-
- // TODO: support other tile modes
- GrTextureParams params(kClamp_TileMode, textureFilterMode);
- SkAutoTUnref<GrTexture> diffuseTexture(GrRefCachedBitmapTexture(context, fDiffuseMap, &params));
- if (!diffuseTexture) {
- SkErrorInternals::SetError(kInternalError_SkError,
- "Couldn't convert bitmap to texture.");
- return false;
- }
-
- SkAutoTUnref<GrTexture> normalTexture(GrRefCachedBitmapTexture(context, fNormalMap, &params));
- if (!normalTexture) {
- SkErrorInternals::SetError(kInternalError_SkError,
- "Couldn't convert bitmap to texture.");
- return false;
- }
-
- GrColor lightColor = GrColorPackRGBA(SkColorGetR(fLight.fColor), SkColorGetG(fLight.fColor),
- SkColorGetB(fLight.fColor), SkColorGetA(fLight.fColor));
- GrColor ambientColor = GrColorPackRGBA(SkColorGetR(fAmbientColor), SkColorGetG(fAmbientColor),
- SkColorGetB(fAmbientColor), SkColorGetA(fAmbientColor));
-
- *fp = SkNEW_ARGS(LightingFP, (diffuseTexture, normalTexture, matrix,
- fLight.fDirection, lightColor, ambientColor));
- *color = GrColorPackA4(paint.getAlpha());
- return true;
-}
+#include "SkImageDecoder.h"
+#include "SkLightingShader.h"
////////////////////////////////////////////////////////////////////////////
class LightingView : public SampleView {
public:
- SkAutoTUnref<LightingShader> fShader;
- SkBitmap fDiffuseBitmap;
- SkBitmap fNormalBitmap;
- SkScalar fLightAngle;
- int fColorFactor;
+ SkAutoTUnref<SkShader> fShader;
+ SkBitmap fDiffuseBitmap;
+ SkBitmap fNormalBitmap;
+ SkScalar fLightAngle;
+ int fColorFactor;
+ SkColor fAmbientColor;
LightingView() {
SkString diffusePath = GetResourcePath("brickwork-texture.jpg");
@@ -352,15 +32,16 @@ public:
fLightAngle = 0.0f;
fColorFactor = 0;
- LightingShader::Light light;
+ SkLightingShader::Light light;
light.fColor = SkColorSetRGB(0xff, 0xff, 0xff);
light.fDirection.fX = SkScalarSin(fLightAngle)*SkScalarSin(SK_ScalarPI*0.25f);
light.fDirection.fY = SkScalarCos(fLightAngle)*SkScalarSin(SK_ScalarPI*0.25f);
light.fDirection.fZ = SkScalarCos(SK_ScalarPI*0.25f);
- SkColor ambient = SkColorSetRGB(0x1f, 0x1f, 0x1f);
+ fAmbientColor = SkColorSetRGB(0x1f, 0x1f, 0x1f);
- fShader.reset(SkNEW_ARGS(LightingShader, (fDiffuseBitmap, fNormalBitmap, light, ambient)));
+ fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap,
+ light, fAmbientColor));
}
virtual ~LightingView() {}
@@ -379,13 +60,14 @@ protected:
fLightAngle += 0.015f;
fColorFactor++;
- LightingShader::Light light;
+ SkLightingShader::Light light;
light.fColor = SkColorSetRGB(0xff, 0xff, (fColorFactor >> 1) & 0xff);
light.fDirection.fX = SkScalarSin(fLightAngle)*SkScalarSin(SK_ScalarPI*0.25f);
light.fDirection.fY = SkScalarCos(fLightAngle)*SkScalarSin(SK_ScalarPI*0.25f);
light.fDirection.fZ = SkScalarCos(SK_ScalarPI*0.25f);
- fShader.get()->setLight(light);
+ fShader.reset(SkLightingShader::Create(fDiffuseBitmap, fNormalBitmap,
+ light, fAmbientColor));
SkPaint paint;
paint.setShader(fShader);
diff --git a/src/core/SkBitmapProcShader.cpp b/src/core/SkBitmapProcShader.cpp
index eeb43b4886..5a33af55ba 100644
--- a/src/core/SkBitmapProcShader.cpp
+++ b/src/core/SkBitmapProcShader.cpp
@@ -17,20 +17,6 @@
#include "effects/GrBicubicEffect.h"
#endif
-bool SkBitmapProcShader::CanDo(const SkBitmap& bm, TileMode tx, TileMode ty) {
- switch (bm.colorType()) {
- case kAlpha_8_SkColorType:
- case kRGB_565_SkColorType:
- case kIndex_8_SkColorType:
- case kN32_SkColorType:
- // if (tx == ty && (kClamp_TileMode == tx || kRepeat_TileMode == tx))
- return true;
- default:
- break;
- }
- return false;
-}
-
SkBitmapProcShader::SkBitmapProcShader(const SkBitmap& src, TileMode tmx, TileMode tmy,
const SkMatrix* localMatrix)
: INHERITED(localMatrix) {
@@ -267,7 +253,7 @@ void SkBitmapProcShader::BitmapProcShaderContext::shadeSpan16(int x, int y, uint
// returns true and set color if the bitmap can be drawn as a single color
// (for efficiency)
-static bool canUseColorShader(const SkBitmap& bm, SkColor* color) {
+static bool can_use_color_shader(const SkBitmap& bm, SkColor* color) {
#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
// HWUI does not support color shaders (see b/22390304)
return false;
@@ -298,14 +284,14 @@ static bool canUseColorShader(const SkBitmap& bm, SkColor* color) {
return false;
}
-static bool bitmapIsTooBig(const SkBitmap& bm) {
+static bool bitmap_is_too_big(const SkBitmap& bm) {
// SkBitmapProcShader stores bitmap coordinates in a 16bit buffer, as it
// communicates between its matrix-proc and its sampler-proc. Until we can
// widen that, we have to reject bitmaps that are larger.
//
- const int maxSize = 65535;
+ static const int kMaxSize = 65535;
- return bm.width() > maxSize || bm.height() > maxSize;
+ return bm.width() > kMaxSize || bm.height() > kMaxSize;
}
SkShader* SkCreateBitmapShader(const SkBitmap& src, SkShader::TileMode tmx,
@@ -313,14 +299,13 @@ SkShader* SkCreateBitmapShader(const SkBitmap& src, SkShader::TileMode tmx,
SkTBlitterAllocator* allocator) {
SkShader* shader;
SkColor color;
- if (src.isNull() || bitmapIsTooBig(src)) {
+ if (src.isNull() || bitmap_is_too_big(src)) {
if (NULL == allocator) {
shader = SkNEW(SkEmptyShader);
} else {
shader = allocator->createT<SkEmptyShader>();
}
- }
- else if (canUseColorShader(src, &color)) {
+ } else if (can_use_color_shader(src, &color)) {
if (NULL == allocator) {
shader = SkNEW_ARGS(SkColorShader, (color));
} else {
diff --git a/src/core/SkBitmapProcShader.h b/src/core/SkBitmapProcShader.h
index b76a6fe774..ea16d1fa9f 100644
--- a/src/core/SkBitmapProcShader.h
+++ b/src/core/SkBitmapProcShader.h
@@ -25,8 +25,6 @@ public:
size_t contextSize() const override;
- static bool CanDo(const SkBitmap&, TileMode tx, TileMode ty);
-
SK_TO_STRING_OVERRIDE()
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkBitmapProcShader)
@@ -40,7 +38,7 @@ public:
// The context takes ownership of the state. It will call its destructor
// but will NOT free the memory.
BitmapProcShaderContext(const SkBitmapProcShader&, const ContextRec&, SkBitmapProcState*);
- virtual ~BitmapProcShaderContext();
+ ~BitmapProcShaderContext() override;
void shadeSpan(int x, int y, SkPMColor dstC[], int count) override;
ShadeProc asAShadeProc(void** ctx) override;
@@ -71,7 +69,7 @@ private:
// an Sk3DBlitter in SkDraw.cpp
// Note that some contexts may contain other contexts (e.g. for compose shaders), but we've not
// yet found a situation where the size below isn't big enough.
-typedef SkSmallAllocator<3, 1024> SkTBlitterAllocator;
+typedef SkSmallAllocator<3, 1152> SkTBlitterAllocator;
// If alloc is non-NULL, it will be used to allocate the returned SkShader, and MUST outlive
// the SkShader.
diff --git a/src/core/SkBitmapProcState.h b/src/core/SkBitmapProcState.h
index 31d114291c..af39004ef2 100644
--- a/src/core/SkBitmapProcState.h
+++ b/src/core/SkBitmapProcState.h
@@ -118,6 +118,7 @@ struct SkBitmapProcState {
private:
friend class SkBitmapProcShader;
+ friend class SkLightingShaderImpl;
ShaderProc32 fShaderProc32; // chooseProcs
ShaderProc16 fShaderProc16; // chooseProcs
diff --git a/src/effects/SkLightingShader.cpp b/src/effects/SkLightingShader.cpp
new file mode 100644
index 0000000000..be92ccf361
--- /dev/null
+++ b/src/effects/SkLightingShader.cpp
@@ -0,0 +1,579 @@
+
+/*
+ * 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 "SkBitmapProcState.h"
+#include "SkColor.h"
+#include "SkEmptyShader.h"
+#include "SkErrorInternals.h"
+#include "SkLightingShader.h"
+#include "SkMathPriv.h"
+#include "SkReadBuffer.h"
+#include "SkWriteBuffer.h"
+
+///////////////////////////////////////////////////////////////////////////////////////////
+
+/*
+ SkLightingShader TODOs:
+ support other than clamp mode
+ allow 'diffuse' & 'normal' to be of different dimensions?
+ support different light types
+ support multiple lights
+ enforce normal map is 4 channel
+ use SkImages instead if SkBitmaps
+ vec3 for ambient and light-color
+ add dox for both lighting equation, and how we compute normal from bitmap
+
+ To Test:
+ non-opaque diffuse textures
+ A8 diffuse textures
+ down & upsampled draws
+*/
+
+
+
+/** \class SkLightingShaderImpl
+ This subclass of shader applies lighting.
+*/
+class SK_API SkLightingShaderImpl : public SkShader {
+public:
+
+ /** Create a new lighting shader that use the provided normal map, light
+ and ambient color to light the diffuse bitmap.
+ @param diffuse the diffuse bitmap
+ @param normal the normal map
+ @param light the light applied to the normal map
+ @param ambient the linear (unpremul) ambient light color
+ */
+ SkLightingShaderImpl(const SkBitmap& diffuse, const SkBitmap& normal,
+ const SkLightingShader::Light& light,
+ const SkColor ambient)
+ : fDiffuseMap(diffuse)
+ , fNormalMap(normal)
+ , fLight(light)
+ , fAmbientColor(ambient) {
+ if (!fLight.fDirection.normalize()) {
+ fLight.fDirection = SkPoint3::Make(0.0f, 0.0f, 1.0f);
+ }
+ SkColorSetA(fLight.fColor, 0xFF);
+ SkColorSetA(fAmbientColor, 0xFF);
+ }
+
+ bool isOpaque() const override;
+
+ bool asFragmentProcessor(GrContext*, const SkPaint& paint, const SkMatrix& viewM,
+ const SkMatrix* localMatrix, GrColor* color,
+ GrProcessorDataManager*, GrFragmentProcessor** fp) const override;
+
+ size_t contextSize() const override;
+
+ class LightingShaderContext : public SkShader::Context {
+ public:
+ // The context takes ownership of the states. It will call their destructors
+ // but will NOT free the memory.
+ LightingShaderContext(const SkLightingShaderImpl&, const ContextRec&,
+ SkBitmapProcState* diffuseState, SkBitmapProcState* normalState);
+ ~LightingShaderContext() override;
+
+ void shadeSpan(int x, int y, SkPMColor[], int count) override;
+
+ uint32_t getFlags() const override { return fFlags; }
+
+ private:
+ SkBitmapProcState* fDiffuseState;
+ SkBitmapProcState* fNormalState;
+ uint32_t fFlags;
+
+ typedef SkShader::Context INHERITED;
+ };
+
+ SK_TO_STRING_OVERRIDE()
+ SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(LightingShader)
+
+protected:
+ void flatten(SkWriteBuffer&) const override;
+ Context* onCreateContext(const ContextRec&, void*) const override;
+
+private:
+ SkBitmap fDiffuseMap;
+ SkBitmap fNormalMap;
+ SkLightingShader::Light fLight;
+ SkColor fAmbientColor; // linear (unpremul) color
+
+ typedef SkShader INHERITED;
+};
+
+////////////////////////////////////////////////////////////////////////////
+
+#if SK_SUPPORT_GPU
+
+#include "GrCoordTransform.h"
+#include "GrFragmentProcessor.h"
+#include "GrTextureAccess.h"
+#include "gl/GrGLProcessor.h"
+#include "gl/builders/GrGLProgramBuilder.h"
+#include "SkGr.h"
+
+class LightingFP : public GrFragmentProcessor {
+public:
+ LightingFP(GrTexture* diffuse, GrTexture* normal, const SkMatrix& matrix,
+ SkVector3 lightDir, GrColor lightColor, GrColor ambientColor)
+ : fDeviceTransform(kDevice_GrCoordSet, matrix)
+ , fDiffuseTextureAccess(diffuse)
+ , fNormalTextureAccess(normal)
+ , fLightDir(lightDir)
+ , fLightColor(lightColor)
+ , fAmbientColor(ambientColor) {
+ this->addCoordTransform(&fDeviceTransform);
+ this->addTextureAccess(&fDiffuseTextureAccess);
+ this->addTextureAccess(&fNormalTextureAccess);
+
+ this->initClassID<LightingFP>();
+ }
+
+ class LightingGLFP : public GrGLFragmentProcessor {
+ public:
+ LightingGLFP() : fLightColor(GrColor_ILLEGAL), fAmbientColor(GrColor_ILLEGAL) {
+ fLightDir.fX = 10000.0f;
+ }
+
+ void emitCode(EmitArgs& args) override {
+
+ GrGLFragmentBuilder* fpb = args.fBuilder->getFragmentShaderBuilder();
+
+ // add uniforms
+ const char* lightDirUniName = NULL;
+ fLightDirUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
+ kVec3f_GrSLType, kDefault_GrSLPrecision,
+ "LightDir", &lightDirUniName);
+
+ const char* lightColorUniName = NULL;
+ fLightColorUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
+ kVec4f_GrSLType, kDefault_GrSLPrecision,
+ "LightColor", &lightColorUniName);
+
+ const char* ambientColorUniName = NULL;
+ fAmbientColorUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
+ kVec4f_GrSLType, kDefault_GrSLPrecision,
+ "AmbientColor", &ambientColorUniName);
+
+ fpb->codeAppend("vec4 diffuseColor = ");
+ fpb->appendTextureLookupAndModulate(args.fInputColor, args.fSamplers[0],
+ args.fCoords[0].c_str(),
+ args.fCoords[0].getType());
+ fpb->codeAppend(";");
+
+ fpb->codeAppend("vec4 normalColor = ");
+ fpb->appendTextureLookup(args.fSamplers[1],
+ args.fCoords[0].c_str(),
+ args.fCoords[0].getType());
+ fpb->codeAppend(";");
+
+ fpb->codeAppend("vec3 normal = normalize(normalColor.rgb - vec3(0.5));");
+ fpb->codeAppendf("vec3 lightDir = normalize(%s);", lightDirUniName);
+ fpb->codeAppend("float NdotL = dot(normal, lightDir);");
+ // diffuse light
+ fpb->codeAppendf("vec3 result = %s.rgb*diffuseColor.rgb*NdotL;", lightColorUniName);
+ // ambient light
+ fpb->codeAppendf("result += %s.rgb;", ambientColorUniName);
+ fpb->codeAppendf("%s = vec4(result.rgb, diffuseColor.a);", args.fOutputColor);
+ }
+
+ void setData(const GrGLProgramDataManager& pdman, const GrProcessor& proc) override {
+ const LightingFP& lightingFP = proc.cast<LightingFP>();
+
+ SkVector3 lightDir = lightingFP.lightDir();
+ if (lightDir != fLightDir) {
+ pdman.set3fv(fLightDirUni, 1, &lightDir.fX);
+ fLightDir = lightDir;
+ }
+
+ GrColor lightColor = lightingFP.lightColor();
+ if (lightColor != fLightColor) {
+ GrGLfloat c[4];
+ GrColorToRGBAFloat(lightColor, c);
+ pdman.set4fv(fLightColorUni, 1, c);
+ fLightColor = lightColor;
+ }
+
+ GrColor ambientColor = lightingFP.ambientColor();
+ if (ambientColor != fAmbientColor) {
+ GrGLfloat c[4];
+ GrColorToRGBAFloat(ambientColor, c);
+ pdman.set4fv(fAmbientColorUni, 1, c);
+ fAmbientColor = ambientColor;
+ }
+ }
+
+ static void GenKey(const GrProcessor& proc, const GrGLSLCaps&,
+ GrProcessorKeyBuilder* b) {
+// const LightingFP& lightingFP = proc.cast<LightingFP>();
+ // only one shader generated currently
+ b->add32(0x0);
+ }
+
+ private:
+ SkVector3 fLightDir;
+ GrGLProgramDataManager::UniformHandle fLightDirUni;
+
+ GrColor fLightColor;
+ GrGLProgramDataManager::UniformHandle fLightColorUni;
+
+ GrColor fAmbientColor;
+ GrGLProgramDataManager::UniformHandle fAmbientColorUni;
+ };
+
+ GrGLFragmentProcessor* createGLInstance() const override { return SkNEW(LightingGLFP); }
+
+ void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {
+ LightingGLFP::GenKey(*this, caps, b);
+ }
+
+ const char* name() const override { return "LightingFP"; }
+
+ void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
+ inout->mulByUnknownFourComponents();
+ }
+
+ SkVector3 lightDir() const { return fLightDir; }
+ GrColor lightColor() const { return fLightColor; }
+ GrColor ambientColor() const { return fAmbientColor; }
+
+private:
+ bool onIsEqual(const GrFragmentProcessor& proc) const override {
+ const LightingFP& lightingFP = proc.cast<LightingFP>();
+ return fDeviceTransform == lightingFP.fDeviceTransform &&
+ fDiffuseTextureAccess == lightingFP.fDiffuseTextureAccess &&
+ fNormalTextureAccess == lightingFP.fNormalTextureAccess &&
+ fLightDir == lightingFP.fLightDir &&
+ fLightColor == lightingFP.fLightColor &&
+ fAmbientColor == lightingFP.fAmbientColor;
+ }
+
+ GrCoordTransform fDeviceTransform;
+ GrTextureAccess fDiffuseTextureAccess;
+ GrTextureAccess fNormalTextureAccess;
+ SkVector3 fLightDir;
+ GrColor fLightColor;
+ GrColor fAmbientColor;
+};
+
+////////////////////////////////////////////////////////////////////////////
+
+bool SkLightingShaderImpl::asFragmentProcessor(GrContext* context, const SkPaint& paint,
+ const SkMatrix& viewM, const SkMatrix* localMatrix,
+ GrColor* color, GrProcessorDataManager*,
+ GrFragmentProcessor** fp) const {
+ // we assume diffuse and normal maps have same width and height
+ // TODO: support different sizes
+ SkASSERT(fDiffuseMap.width() == fNormalMap.width() &&
+ fDiffuseMap.height() == fNormalMap.height());
+ SkMatrix matrix;
+ matrix.setIDiv(fDiffuseMap.width(), fDiffuseMap.height());
+
+ SkMatrix lmInverse;
+ if (!this->getLocalMatrix().invert(&lmInverse)) {
+ return false;
+ }
+ if (localMatrix) {
+ SkMatrix inv;
+ if (!localMatrix->invert(&inv)) {
+ return false;
+ }
+ lmInverse.postConcat(inv);
+ }
+ matrix.preConcat(lmInverse);
+
+ // Must set wrap and filter on the sampler before requesting a texture. In two places below
+ // we check the matrix scale factors to determine how to interpret the filter quality setting.
+ // This completely ignores the complexity of the drawVertices case where explicit local coords
+ // are provided by the caller.
+ GrTextureParams::FilterMode textureFilterMode = GrTextureParams::kBilerp_FilterMode;
+ switch (paint.getFilterQuality()) {
+ case kNone_SkFilterQuality:
+ textureFilterMode = GrTextureParams::kNone_FilterMode;
+ break;
+ case kLow_SkFilterQuality:
+ textureFilterMode = GrTextureParams::kBilerp_FilterMode;
+ break;
+ case kMedium_SkFilterQuality:{
+ SkMatrix matrix;
+ matrix.setConcat(viewM, this->getLocalMatrix());
+ if (matrix.getMinScale() < SK_Scalar1) {
+ textureFilterMode = GrTextureParams::kMipMap_FilterMode;
+ } else {
+ // Don't trigger MIP level generation unnecessarily.
+ textureFilterMode = GrTextureParams::kBilerp_FilterMode;
+ }
+ break;
+ }
+ case kHigh_SkFilterQuality:
+ default:
+ SkErrorInternals::SetError(kInvalidPaint_SkError,
+ "Sorry, I don't understand the filtering "
+ "mode you asked for. Falling back to "
+ "MIPMaps.");
+ textureFilterMode = GrTextureParams::kMipMap_FilterMode;
+ break;
+
+ }
+
+ // TODO: support other tile modes
+ GrTextureParams params(kClamp_TileMode, textureFilterMode);
+ SkAutoTUnref<GrTexture> diffuseTexture(GrRefCachedBitmapTexture(context, fDiffuseMap, &params));
+ if (!diffuseTexture) {
+ SkErrorInternals::SetError(kInternalError_SkError,
+ "Couldn't convert bitmap to texture.");
+ return false;
+ }
+
+ SkAutoTUnref<GrTexture> normalTexture(GrRefCachedBitmapTexture(context, fNormalMap, &params));
+ if (!normalTexture) {
+ SkErrorInternals::SetError(kInternalError_SkError,
+ "Couldn't convert bitmap to texture.");
+ return false;
+ }
+
+ GrColor lightColor = GrColorPackRGBA(SkColorGetR(fLight.fColor), SkColorGetG(fLight.fColor),
+ SkColorGetB(fLight.fColor), SkColorGetA(fLight.fColor));
+ GrColor ambientColor = GrColorPackRGBA(SkColorGetR(fAmbientColor), SkColorGetG(fAmbientColor),
+ SkColorGetB(fAmbientColor), SkColorGetA(fAmbientColor));
+
+ *fp = SkNEW_ARGS(LightingFP, (diffuseTexture, normalTexture, matrix,
+ fLight.fDirection, lightColor, ambientColor));
+ *color = GrColorPackA4(paint.getAlpha());
+ return true;
+}
+#else
+
+bool SkLightingShaderImpl::asFragmentProcessor(GrContext* context, const SkPaint& paint,
+ const SkMatrix& viewM, const SkMatrix* localMatrix,
+ GrColor* color, GrProcessorDataManager*,
+ GrFragmentProcessor** fp) const {
+ SkDEBUGFAIL("Should not call in GPU-less build");
+ return false;
+}
+
+#endif
+
+////////////////////////////////////////////////////////////////////////////
+
+bool SkLightingShaderImpl::isOpaque() const {
+ return fDiffuseMap.isOpaque();
+}
+
+size_t SkLightingShaderImpl::contextSize() const {
+ return 2 * sizeof(SkBitmapProcState) + sizeof(LightingShaderContext);
+}
+
+SkLightingShaderImpl::LightingShaderContext::LightingShaderContext(const SkLightingShaderImpl& shader,
+ const ContextRec& rec,
+ SkBitmapProcState* diffuseState,
+ SkBitmapProcState* normalState)
+ : INHERITED(shader, rec)
+ , fDiffuseState(diffuseState)
+ , fNormalState(normalState)
+{
+ const SkPixmap& pixmap = fDiffuseState->fPixmap;
+ bool isOpaque = pixmap.isOpaque();
+
+ // update fFlags
+ uint32_t flags = 0;
+ if (isOpaque && (255 == this->getPaintAlpha())) {
+ flags |= kOpaqueAlpha_Flag;
+ }
+
+ fFlags = flags;
+}
+
+SkLightingShaderImpl::LightingShaderContext::~LightingShaderContext() {
+ // The bitmap proc states have been created outside of the context on memory that will be freed
+ // elsewhere. Call the destructors but leave the freeing of the memory to the caller.
+ fDiffuseState->~SkBitmapProcState();
+ fNormalState->~SkBitmapProcState();
+}
+
+static inline int light(int light, int diff, SkScalar NdotL, int ambient) {
+ int color = int(light * diff * NdotL + 255 * ambient);
+ if (color <= 0) {
+ return 0;
+ } else if (color >= 255*255) {
+ return 255;
+ } else {
+ return SkDiv255Round(color);
+ }
+}
+
+// larger is better (fewer times we have to loop), but we shouldn't
+// take up too much stack-space (each could here costs 16 bytes)
+#define TMP_COUNT 16
+
+void SkLightingShaderImpl::LightingShaderContext::shadeSpan(int x, int y,
+ SkPMColor result[], int count) {
+ const SkLightingShaderImpl& lightShader = static_cast<const SkLightingShaderImpl&>(fShader);
+
+ SkPMColor tmpColor[TMP_COUNT], tmpColor2[TMP_COUNT];
+ SkPMColor tmpNormal[TMP_COUNT], tmpNormal2[TMP_COUNT];
+
+ SkBitmapProcState::MatrixProc diffMProc = fDiffuseState->getMatrixProc();
+ SkBitmapProcState::SampleProc32 diffSProc = fDiffuseState->getSampleProc32();
+
+ SkBitmapProcState::MatrixProc normalMProc = fNormalState->getMatrixProc();
+ SkBitmapProcState::SampleProc32 normalSProc = fNormalState->getSampleProc32();
+
+ SkASSERT(fDiffuseState->fPixmap.addr());
+ SkASSERT(fNormalState->fPixmap.addr());
+
+ SkPoint3 norm;
+ SkScalar NdotL;
+ int r, g, b;
+
+ do {
+ int n = count;
+ if (n > TMP_COUNT) {
+ n = TMP_COUNT;
+ }
+
+ diffMProc(*fDiffuseState, tmpColor, n, x, y);
+ diffSProc(*fDiffuseState, tmpColor, n, tmpColor2);
+
+ normalMProc(*fNormalState, tmpNormal, n, x, y);
+ normalSProc(*fNormalState, tmpNormal, n, tmpNormal2);
+
+ for (int i = 0; i < n; ++i) {
+ SkASSERT(0xFF == SkColorGetA(tmpNormal2[i])); // opaque -> unpremul
+ norm.set(SkIntToScalar(SkColorGetR(tmpNormal2[i]))-127.0f,
+ SkIntToScalar(SkColorGetG(tmpNormal2[i]))-127.0f,
+ SkIntToScalar(SkColorGetB(tmpNormal2[i]))-127.0f);
+ norm.normalize();
+
+ SkColor diffColor = SkUnPreMultiply::PMColorToColor(tmpColor2[i]);
+ NdotL = norm.dot(lightShader.fLight.fDirection);
+
+ // This is all done in linear unpremul color space
+ r = light(SkColorGetR(lightShader.fLight.fColor), SkColorGetR(diffColor), NdotL,
+ SkColorGetR(lightShader.fAmbientColor));
+ g = light(SkColorGetG(lightShader.fLight.fColor), SkColorGetG(diffColor), NdotL,
+ SkColorGetG(lightShader.fAmbientColor));
+ b = light(SkColorGetB(lightShader.fLight.fColor), SkColorGetB(diffColor), NdotL,
+ SkColorGetB(lightShader.fAmbientColor));
+
+ result[i] = SkPreMultiplyARGB(SkColorGetA(diffColor), r, g, b);
+ }
+
+ result += n;
+ x += n;
+ count -= n;
+ } while (count > 0);
+}
+
+////////////////////////////////////////////////////////////////////////////
+
+#ifndef SK_IGNORE_TO_STRING
+void SkLightingShaderImpl::toString(SkString* str) const {
+ str->appendf("LightingShader: ()");
+}
+#endif
+
+SkFlattenable* SkLightingShaderImpl::CreateProc(SkReadBuffer& buf) {
+ SkBitmap diffuse;
+ if (!buf.readBitmap(&diffuse)) {
+ return NULL;
+ }
+ diffuse.setImmutable();
+
+ SkBitmap normal;
+ if (!buf.readBitmap(&normal)) {
+ return NULL;
+ }
+ normal.setImmutable();
+
+ SkLightingShader::Light light;
+ if (!buf.readScalarArray(&light.fDirection.fX, 3)) {
+ return NULL;
+ }
+ light.fColor = buf.readColor();
+
+ SkColor ambient = buf.readColor();
+
+ // TODO: this would be nice to enable
+ // return SkCreateLightingShader(diffuse, normal, light, ambient, NULL);
+ return SkNEW_ARGS(SkLightingShaderImpl, (diffuse, normal, light, ambient));
+}
+
+void SkLightingShaderImpl::flatten(SkWriteBuffer& buf) const {
+ buf.writeBitmap(fDiffuseMap);
+ buf.writeBitmap(fNormalMap);
+ buf.writeScalarArray(&fLight.fDirection.fX, 3);
+ buf.writeColor(fLight.fColor);
+ buf.writeColor(fAmbientColor);
+}
+
+SkShader::Context* SkLightingShaderImpl::onCreateContext(const ContextRec& rec,
+ void* storage) const {
+
+ SkMatrix totalInverse;
+ // Do this first, so we know the matrix can be inverted.
+ if (!this->computeTotalInverse(rec, &totalInverse)) {
+ return NULL;
+ }
+
+ void* diffuseStateStorage = (char*)storage + sizeof(LightingShaderContext);
+ SkBitmapProcState* diffuseState = SkNEW_PLACEMENT(diffuseStateStorage, SkBitmapProcState);
+ SkASSERT(diffuseState);
+
+ diffuseState->fTileModeX = SkShader::kClamp_TileMode;
+ diffuseState->fTileModeY = SkShader::kClamp_TileMode;
+ diffuseState->fOrigBitmap = fDiffuseMap;
+ if (!diffuseState->chooseProcs(totalInverse, *rec.fPaint)) {
+ diffuseState->~SkBitmapProcState();
+ return NULL;
+ }
+
+ void* normalStateStorage = (char*)storage + sizeof(LightingShaderContext) + sizeof(SkBitmapProcState);
+ SkBitmapProcState* normalState = SkNEW_PLACEMENT(normalStateStorage, SkBitmapProcState);
+ SkASSERT(normalState);
+
+ normalState->fTileModeX = SkShader::kClamp_TileMode;
+ normalState->fTileModeY = SkShader::kClamp_TileMode;
+ normalState->fOrigBitmap = fNormalMap;
+ if (!normalState->chooseProcs(totalInverse, *rec.fPaint)) {
+ diffuseState->~SkBitmapProcState();
+ normalState->~SkBitmapProcState();
+ return NULL;
+ }
+
+ return SkNEW_PLACEMENT_ARGS(storage, LightingShaderContext, (*this, rec,
+ diffuseState, normalState));
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+static bool bitmap_is_too_big(const SkBitmap& bm) {
+ // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer, as it
+ // communicates between its matrix-proc and its sampler-proc. Until we can
+ // widen that, we have to reject bitmaps that are larger.
+ //
+ static const int kMaxSize = 65535;
+
+ return bm.width() > kMaxSize || bm.height() > kMaxSize;
+}
+
+SkShader* SkLightingShader::Create(const SkBitmap& diffuse, const SkBitmap& normal,
+ const SkLightingShader::Light& light,
+ const SkColor ambient) {
+ if (diffuse.isNull() || bitmap_is_too_big(diffuse) ||
+ normal.isNull() || bitmap_is_too_big(normal) ||
+ diffuse.width() != normal.width() ||
+ diffuse.height() != normal.height()) {
+ return nullptr;
+ }
+
+ return SkNEW_ARGS(SkLightingShaderImpl, (diffuse, normal, light, ambient));
+}
+
+///////////////////////////////////////////////////////////////////////////////
diff --git a/src/effects/SkLightingShader.h b/src/effects/SkLightingShader.h
new file mode 100644
index 0000000000..d3fe3f66ac
--- /dev/null
+++ b/src/effects/SkLightingShader.h
@@ -0,0 +1,45 @@
+
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+
+#ifndef SkLightingShader_DEFINED
+#define SkLightingShader_DEFINED
+
+#include "SkPoint3.h"
+#include "SkShader.h"
+
+class SK_API SkLightingShader {
+public:
+ struct Light {
+ SkVector3 fDirection; // direction towards the light (+Z is out of the screen).
+ // If degenerate, it will be replaced with (0, 0, 1).
+ SkColor fColor; // linear (unpremul) color. Note: alpha assumed to be 255.
+ };
+
+ /** Returns a shader that lights the diffuse and normal maps with a single light.
+
+ It returns a shader with a reference count of 1.
+ The caller should decrement the shader's reference count when done with the shader.
+ It is an error for count to be < 2.
+ @param diffuse the diffuse bitmap
+ @param normal the normal map
+ @param light the light applied to the normal map
+ @param ambient the linear (unpremul) ambient light color. Note: alpha assumed to be 255.
+
+ NULL will be returned if:
+ either 'diffuse' or 'normal' are empty
+ either 'diffuse' or 'normal' are too big (> 65535 on a side)
+ 'diffuse' and 'normal' aren't the same size
+ */
+ static SkShader* Create(const SkBitmap& diffuse, const SkBitmap& normal,
+ const SkLightingShader::Light& light, const SkColor ambient);
+
+ SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
+};
+
+#endif