Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(834)

Unified Diff: src/effects/SkLightingShader.cpp

Issue 1245883003: Move LightingShader to effects (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: update Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« include/effects/SkLightingShader.h ('K') | « src/core/SkBitmapProcState.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/effects/SkLightingShader.cpp
diff --git a/samplecode/SampleLighting.cpp b/src/effects/SkLightingShader.cpp
old mode 100755
new mode 100644
similarity index 54%
copy from samplecode/SampleLighting.cpp
copy to src/effects/SkLightingShader.cpp
index a002a9320f9843762dae1a492ad84a391ab10d31..507f67387f332ae9e4305031ba8f8ce0b6bff5bb
--- a/samplecode/SampleLighting.cpp
+++ b/src/effects/SkLightingShader.cpp
@@ -5,106 +5,26 @@
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
-#include "SampleCode.h"
-#include "Resources.h"
-#include "SkCanvas.h"
+#include "SkBitmapProcState.h"
+#include "SkColor.h"
+#include "SkEmptyShader.h"
#include "SkErrorInternals.h"
-#include "SkGr.h"
-#include "SkPoint3.h"
+#include "SkLightingShader.h"
+#include "SkMathPriv.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));
-}
+#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:
@@ -163,7 +83,7 @@ public:
fpb->appendTextureLookup(samplers[1], coords[0].c_str(), coords[0].getType());
fpb->codeAppend(";");
- fpb->codeAppend("vec3 normal = normalize(2.0*(normalColor.rgb - vec3(0.5)));");
+ fpb->codeAppend("vec3 normal = normalize(normalColor.rgb - vec3(0.5));");
fpb->codeAppendf("vec3 lightDir = normalize(%s);", lightDirUniName);
jvanverth1 2015/07/23 17:40:14 From external discussion, and for a future change:
fpb->codeAppend("float NdotL = dot(normal, lightDir);");
// diffuse light
@@ -252,10 +172,12 @@ private:
GrColor fAmbientColor;
};
-bool LightingShader::asFragmentProcessor(GrContext* context, const SkPaint& paint,
- const SkMatrix& viewM, const SkMatrix* localMatrix,
- GrColor* color, GrProcessorDataManager*,
- GrFragmentProcessor** fp) const {
+////////////////////////////////////////////////////////////////////////////
+
+bool SkLightingShader::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() &&
@@ -336,83 +258,246 @@ bool LightingShader::asFragmentProcessor(GrContext* context, const SkPaint& pain
*color = GrColorPackA4(paint.getAlpha());
return true;
}
+#else
+
+bool SkLightingShader::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
////////////////////////////////////////////////////////////////////////////
-class LightingView : public SampleView {
-public:
- SkAutoTUnref<LightingShader> fShader;
- SkBitmap fDiffuseBitmap;
- SkBitmap fNormalBitmap;
- SkScalar fLightAngle;
- int fColorFactor;
-
- LightingView() {
- SkString diffusePath = GetResourcePath("brickwork-texture.jpg");
- SkImageDecoder::DecodeFile(diffusePath.c_str(), &fDiffuseBitmap);
- SkString normalPath = GetResourcePath("brickwork_normal-map.jpg");
- SkImageDecoder::DecodeFile(normalPath.c_str(), &fNormalBitmap);
-
- fLightAngle = 0.0f;
- fColorFactor = 0;
-
- LightingShader::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);
-
- fShader.reset(SkNEW_ARGS(LightingShader, (fDiffuseBitmap, fNormalBitmap, light, ambient)));
+bool SkLightingShader::isOpaque() const {
+ return fDiffuseMap.isOpaque();
+}
+
+size_t SkLightingShader::contextSize() const {
+ return 2 * sizeof(SkBitmapProcState) + sizeof(LightingShaderContext);
+}
+
+SkLightingShader::LightingShaderContext::LightingShaderContext(const SkLightingShader& 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;
+}
+
+SkLightingShader::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 SkLightingShader::LightingShaderContext::shadeSpan(int x, int y,
+ SkPMColor result[], int count) {
+ const SkLightingShader& lightShader = static_cast<const SkLightingShader&>(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);
- virtual ~LightingView() {}
+ // 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));
-protected:
- // overrides from SkEventSink
- bool onQuery(SkEvent* evt) override {
- if (SampleCode::TitleQ(*evt)) {
- SampleCode::TitleR(evt, "Lighting");
- return true;
+ result[i] = SkPreMultiplyARGB(SkColorGetA(diffColor), r, g, b);
}
- return this->INHERITED::onQuery(evt);
+
+ result += n;
+ x += n;
+ count -= n;
+ } while (count > 0);
+}
+
+////////////////////////////////////////////////////////////////////////////
+
+#ifndef SK_IGNORE_TO_STRING
+void SkLightingShader::toString(SkString* str) const {
+ str->appendf("LightingShader: ()");
+}
+#endif
+
+SkFlattenable* SkLightingShader::CreateProc(SkReadBuffer& buf) {
+ SkBitmap diffuse;
+ if (!buf.readBitmap(&diffuse)) {
+ return NULL;
+ }
+ diffuse.setImmutable();
+
+ SkBitmap normal;
+ if (!buf.readBitmap(&normal)) {
+ return NULL;
}
+ normal.setImmutable();
- void onDrawContent(SkCanvas* canvas) override {
- fLightAngle += 0.015f;
- fColorFactor++;
+ Light light;
+ if (!buf.readScalarArray(&light.fDirection.fX, 3)) {
+ return NULL;
+ }
+ light.fColor = buf.readColor();
- LightingShader::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);
+ SkColor ambient = buf.readColor();
- fShader.get()->setLight(light);
+ // TODO: this would be nice to enable
+ // return SkCreateLightingShader(diffuse, normal, light, ambient, NULL);
+ return SkNEW_ARGS(SkLightingShader, (diffuse, normal, light, ambient));
+}
- SkPaint paint;
- paint.setShader(fShader);
- paint.setColor(SK_ColorBLACK);
+void SkLightingShader::flatten(SkWriteBuffer& buf) const {
+ buf.writeBitmap(fDiffuseMap);
+ buf.writeBitmap(fNormalMap);
+ buf.writeScalarArray(&fLight.fDirection.fX, 3);
+ buf.writeColor(fLight.fColor);
+ buf.writeColor(fAmbientColor);
+}
- SkRect r = SkRect::MakeWH((SkScalar)fDiffuseBitmap.width(),
- (SkScalar)fDiffuseBitmap.height());
- canvas->drawRect(r, paint);
+SkShader::Context* SkLightingShader::onCreateContext(const ContextRec& rec, void* storage) const {
- // so we're constantly updating
- this->inval(NULL);
+ SkMatrix totalInverse;
+ // Do this first, so we know the matrix can be inverted.
+ if (!this->computeTotalInverse(rec, &totalInverse)) {
+ return NULL;
}
- SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
- this->inval(NULL);
- return this->INHERITED::onFindClickHandler(x, y, modi);
+ 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;
}
-private:
- typedef SampleView INHERITED;
-};
+ 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));
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+// TODO: it would be nice to be able to call this from SkShader but this
+// would need to reside in core.
+#if 0
+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* SkCreateLightingShader(const SkBitmap& diffuse, const SkBitmap& normal,
+ const SkLightingShader::Light& light, const SkColor ambient,
+ SkTBlitterAllocator* allocator) {
+ SkShader* shader;
+
+ if (diffuse.isNull() || bitmap_is_too_big(diffuse) ||
+ normal.isNull() || bitmap_is_too_big(normal) ||
+ diffuse.width() != normal.width() ||
+ diffuse.height() != normal.height()) {
+ if (!allocator) {
+ shader = SkNEW(SkEmptyShader);
+ } else {
+ shader = allocator->createT<SkEmptyShader>();
+ }
+ } else {
+ if (!allocator) {
+ shader = SkNEW_ARGS(SkLightingShader, (diffuse, normal, light, ambient));
+ } else {
+ shader = allocator->createT<SkLightingShader>(diffuse, normal, light, ambient);
+ }
+ }
+ return shader;
+}
+#endif
-static SkView* MyFactory() { return new LightingView; }
-static SkViewRegister reg(MyFactory);
+///////////////////////////////////////////////////////////////////////////////
« include/effects/SkLightingShader.h ('K') | « src/core/SkBitmapProcState.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698