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

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: Overridden paint now gets destructed, clearer comments, GM now tests texture + transparent paint, a… 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 | include/gpu/GrFragmentProcessor.h » ('j') | include/gpu/GrFragmentProcessor.h » ('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..701d4b34837f6de5b6abc581f2e8994415711b6d
--- /dev/null
+++ b/gm/lightingshader2.cpp
@@ -0,0 +1,153 @@
+/*
+ * Copyright 2016 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. Specifically, nullptr arguments, scaling when using
+// normal maps, and paint transparency.
+class LightingShader2GM : public GM {
+public:
+ LightingShader2GM() {
+ this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
+ }
+
+protected:
+ SkString onShortName() override {
+ return SkString("lightingshader2");
+ }
+
+ SkISize onISize() override {
+ return SkISize::Make(kGMSize, kGMSize);
+ }
+
+ void onOnceBeforeDraw() override {
+ SkLights::Builder builder;
+ const SkVector3 kLightFromUpperRight = SkVector3::Make(0.788f, 0.394f, 0.473f);
+
+ builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f),
+ kLightFromUpperRight));
+ builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f)));
+ fLights = builder.finish();
+
+ 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);
+ }
+
+ // Scales shape around origin, rotates shape around origin, then translates shape to GM center
+ 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);
+ }
+
+ void drawRect(SkCanvas* canvas, const SkRect& r, SkScalar scaleX, SkScalar scaleY,
+ SkScalar rotate, bool useNormalSource = true, bool useDiffuseShader = true,
egdaniel 2016/07/14 03:22:47 not sure there is much advantage to the defaults h
dvonbeck 2016/07/14 14:30:33 Done.
+ bool useTransparentPaint = false) {
+ this->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<SkNormalSource> normalSource = nullptr;
+ sk_sp<SkShader> diffuseShader = nullptr;
+
+ if (useNormalSource) {
+ sk_sp <SkShader> normalMap = SkMakeBitmapShader(fNormalMap, SkShader::kClamp_TileMode,
+ SkShader::kClamp_TileMode, &matrix,
+ nullptr);
+ normalSource = SkNormalSource::MakeFromNormalMap(
+ std::move(normalMap),
+ ctm);
+ }
+ if (useDiffuseShader) {
+ diffuseShader = SkMakeBitmapShader(fDiffuse, SkShader::kClamp_TileMode,
+ SkShader::kClamp_TileMode, &matrix, nullptr);
+ } else {
+ paint.setColor(0xFF00FF00);
+ }
+ if (useTransparentPaint) {
+ paint.setAlpha(0x55);
+ }
+ paint.setShader(SkLightingShader::Make(std::move(diffuseShader), std::move(normalSource),
+ fLights));
+
+ canvas->drawRect(r, paint);
+ }
+
+ void onDraw(SkCanvas* canvas) override {
+ SkRect r = SkRect::MakeWH(SkIntToScalar(kTexSize), SkIntToScalar(kTexSize));
+
+ canvas->save();
+ canvas->translate(0.0f, -SkScalarSqrt(2.0f) * kTexSize);
+ this->drawRect(canvas, r, 1.0f, 1.0f, 45.f, true, true, true);
egdaniel 2016/07/14 03:22:47 Would there be value to drawing all these combinat
dvonbeck 2016/07/14 14:30:33 I don't think so, since I am varying different par
+ canvas->restore();
+
+ canvas->save();
+ canvas->translate(-SK_ScalarSqrt2 * kTexSize, -SK_ScalarSqrt2 * kTexSize);
+ this->drawRect(canvas, r, 1.2f, 1.2f, 0.f);
+ canvas->restore();
+
+ canvas->save();
+ canvas->translate(SK_ScalarSqrt2 * kTexSize, -SK_ScalarSqrt2 * kTexSize);
+ this->drawRect(canvas, r, 0.5f, 0.5f, 0.f);
+ canvas->restore();
+
+ canvas->save();
+ canvas->translate(-SK_ScalarSqrt2 * kTexSize, 0.0f);
+ this->drawRect(canvas, r, 1.0f, 1.0f, 0.f, true, false, true);
+ canvas->restore();
+
+ canvas->save();
+ canvas->translate(SK_ScalarSqrt2 * kTexSize, 0.0f);
+ this->drawRect(canvas, r, 1.0f, 1.0f, 0.f, false, true);
+ 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 | include/gpu/GrFragmentProcessor.h » ('j') | include/gpu/GrFragmentProcessor.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698