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

Unified Diff: gm/lightingshader2.cpp

Issue 2132113002: SkLS accepts nullptr for pointer args, handles alpha accurately, has new GM (Closed) Base URL: https://skia.googlesource.com/skia@dvonbeck-diffuse-api-change
Patch Set: comment I forgot about diffuseshader nullptr Created 4 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
« no previous file with comments | « no previous file | src/core/SkLightingShader.h » ('j') | src/core/SkLightingShader.cpp » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: gm/lightingshader2.cpp
diff --git a/gm/lightingshader2.cpp b/gm/lightingshader2.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..65f7dacfac7a377313ae4a7c780a334d86f54528
--- /dev/null
+++ b/gm/lightingshader2.cpp
@@ -0,0 +1,179 @@
+/*
robertphillips 2016/07/11 14:02:04 2016
dvonbeck 2016/07/11 19:37:29 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 "gm.h"
+
+#include "SkBitmapProcShader.h"
+#include "SkLightingShader.h"
+#include "SkNormalSource.h"
+#include "SkPoint3.h"
+#include "SkShader.h"
+
+// Create a truncated pyramid normal map
+static SkBitmap make_frustum_normalmap(int texSize) {
+ SkBitmap frustum;
+ frustum.allocN32Pixels(texSize, texSize);
+
+ sk_tool_utils::create_frustum_normal_map(&frustum, SkIRect::MakeWH(texSize, texSize));
+ return frustum;
+}
+
+namespace skiagm {
+
+// This GM exercises lighting shaders.
robertphillips 2016/07/11 14:02:04 // more ... // what does it do besides what the or
dvonbeck 2016/07/11 19:37:29 Done.
+class LightingShader2GM : public GM {
+public:
+ LightingShader2GM() {
+ this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
+
+ SkLights::Builder builder;
+
+ builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f),
robertphillips 2016/07/11 14:02:04 normalize this so the reader has a better expectat
dvonbeck 2016/07/11 19:37:29 Done.
+ SkVector3::Make(1.0f, 0.5f, 0.6f)));
+ builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f)));
+
+ fLights = builder.finish();
+ }
+
+protected:
robertphillips 2016/07/11 14:02:04 extra spaces ?
dvonbeck 2016/07/11 19:37:29 Done.
+
+
+ SkString onShortName() override {
+ return SkString("lightingshader2");
+ }
+
+ SkISize onISize() override {
+ return SkISize::Make(kGMSize, kGMSize);
+ }
+
+ void onOnceBeforeDraw() override {
robertphillips 2016/07/11 14:02:04 Can we create the lights in here too ?
dvonbeck 2016/07/11 19:37:28 Done. Why?
robertphillips 2016/07/11 20:40:55 The problem with doing stuff in the constructor is
+ fDiffuse = sk_tool_utils::create_checkerboard_bitmap(
+ kTexSize, kTexSize,
+ sk_tool_utils::color_to_565(0x0),
+ sk_tool_utils::color_to_565(0xFF804020),
+ 8);
+
+ fNormalMap = make_frustum_normalmap(kTexSize);
+ }
+
robertphillips 2016/07/11 14:02:04 // ... ?
dvonbeck 2016/07/11 19:37:29 Done.
+ void positionCTM(SkCanvas *canvas, SkScalar scaleX, SkScalar scaleY, SkScalar rotate) const {
+ canvas->translate(kGMSize / 2.0f, kGMSize / 2.0f);
+ canvas->scale(scaleX, scaleY);
+ canvas->rotate(rotate);
+ canvas->translate(-kTexSize/2.0f, -kTexSize/2.0f);
+ }
+
robertphillips 2016/07/11 14:02:03 Hmmm, there is a lot of duplicate code here. Can w
dvonbeck 2016/07/11 19:37:29 Done.
+ void drawRect(SkCanvas* canvas, const SkRect& r, SkScalar scaleX, SkScalar scaleY,
+ SkScalar rotate) {
robertphillips 2016/07/11 14:02:04 this->
dvonbeck 2016/07/11 19:37:29 Done.
+ positionCTM(canvas, scaleX, scaleY, rotate);
+
+ SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height());
+
+ SkMatrix matrix;
+ matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit);
+
+ const SkMatrix& ctm = canvas->getTotalMatrix();
+
+ SkPaint paint;
+ sk_sp<SkShader> normalMap = SkMakeBitmapShader(fNormalMap, SkShader::kClamp_TileMode,
+ SkShader::kClamp_TileMode, &matrix, nullptr);
+ sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeFromNormalMap(std::move(normalMap),
+ ctm);
+ sk_sp<SkShader> diffuseShader = SkMakeBitmapShader(fDiffuse, SkShader::kClamp_TileMode,
+ SkShader::kClamp_TileMode, &matrix, nullptr);
+ paint.setShader(SkLightingShader::Make(std::move(diffuseShader), std::move(normalSource),
+ fLights));
+
+ canvas->drawRect(r, paint);
+ }
+
+ void drawRectFromNullDiffuse(SkCanvas* canvas, const SkRect& r, SkScalar scaleX,
+ SkScalar scaleY, SkScalar rotate) {
+ positionCTM(canvas, scaleX, scaleY, rotate);
+
+ SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height());
+
+ SkMatrix matrix;
+ matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit);
+
+ const SkMatrix& ctm = canvas->getTotalMatrix();
+
+ SkPaint paint;
+ sk_sp<SkShader> normalMap = SkMakeBitmapShader(fNormalMap, SkShader::kClamp_TileMode,
+ SkShader::kClamp_TileMode, &matrix, nullptr);
+ sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeFromNormalMap(std::move(normalMap),
+ ctm);
+ paint.setShader(SkLightingShader::Make(nullptr, std::move(normalSource),
+ fLights));
+ paint.setColor(SK_ColorGREEN);
+
+ canvas->drawRect(r, paint);
+ }
+
+ void drawRectFromNullNormalSource(SkCanvas* canvas, const SkRect& r, SkScalar scaleX,
+ SkScalar scaleY, SkScalar rotate) {
+ positionCTM(canvas, scaleX, scaleY, rotate);
+
+ SkRect bitmapBounds = SkRect::MakeIWH(fDiffuse.width(), fDiffuse.height());
+
+ SkMatrix matrix;
+ matrix.setRectToRect(bitmapBounds, r, SkMatrix::kFill_ScaleToFit);
+
+ SkPaint paint;
+ sk_sp<SkShader> diffuseShader = SkMakeBitmapShader(fDiffuse, SkShader::kClamp_TileMode,
+ SkShader::kClamp_TileMode, &matrix, nullptr);
+ paint.setShader(SkLightingShader::Make(std::move(diffuseShader), nullptr, fLights));
+
+ canvas->drawRect(r, paint);
+ }
+
+ void onDraw(SkCanvas* canvas) override {
robertphillips 2016/07/11 14:02:04 one line ?
dvonbeck 2016/07/11 19:37:28 Done.
+ SkRect r;
+ r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize));
+
+ canvas->save();
robertphillips 2016/07/11 14:02:04 SK_ScalarSqrt2 ?
dvonbeck 2016/07/11 19:37:29 Done.
+ canvas->translate(0.0f, -SkScalarSqrt(2.0f) * kTexSize);
+ this->drawRect(canvas, r, 1.0f, 1.0f, 45.f);
+ canvas->restore();
+
+ canvas->save();
+ canvas->translate(-SkScalarSqrt(2.0f) * kTexSize, -SkScalarSqrt(2.0f) * kTexSize);
+ this->drawRect(canvas, r, 1.2f, 1.2f, 0.f);
+ canvas->restore();
+
+ canvas->save();
+ canvas->translate(SkScalarSqrt(2.0f) * kTexSize, -SkScalarSqrt(2.0f) * kTexSize);
+ this->drawRect(canvas, r, 0.5f, 0.5f, 0.f);
+ canvas->restore();
+
+ canvas->save();
+ canvas->translate(-SkScalarSqrt(2.0f) * kTexSize, 0.0f);
+ this->drawRectFromNullDiffuse(canvas, r, 1.0f, 1.0f, 0.f);
+ canvas->restore();
+
+ canvas->save();
+ canvas->translate(SkScalarSqrt(2.0f) * kTexSize, 0.0f);
+ this->drawRectFromNullNormalSource(canvas, r, 1.0f, 1.0f, 0.f);
+ canvas->restore();
+ }
+
+private:
+ static const int kTexSize = 128;
+ static const int kGMSize = 512;
+
+ SkBitmap fDiffuse;
+ SkBitmap fNormalMap;
+
+ sk_sp<SkLights> fLights;
+
+ typedef GM INHERITED;
+};
+
+//////////////////////////////////////////////////////////////////////////////
+
+DEF_GM(return new LightingShader2GM;)
+}
« no previous file with comments | « no previous file | src/core/SkLightingShader.h » ('j') | src/core/SkLightingShader.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698