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

Unified Diff: gm/lightingshaderbevel.cpp

Issue 2080993002: Added API for Bevel NormalSource. (Closed) Base URL: https://skia.googlesource.com/skia@dvonbeck-diffuse-api-change
Patch Set: Addressed patch 8 comments 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 | gyp/core.gypi » ('j') | src/core/SkNormalSource.h » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: gm/lightingshaderbevel.cpp
diff --git a/gm/lightingshaderbevel.cpp b/gm/lightingshaderbevel.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..c0c5377296f31a703a04c61cdf35a8b02b9de2a9
--- /dev/null
+++ b/gm/lightingshaderbevel.cpp
@@ -0,0 +1,157 @@
+/*
+ * 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"
+
+
+namespace skiagm {
+
+// This GM exercises lighting shaders when used with bevel SkNormalSource objects.
+class LightingShaderBevelGM : public GM {
+public:
+ LightingShaderBevelGM() {
+ this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
+ }
+
+protected:
+ SkString onShortName() override {
+ return SkString("lightingshaderbevel");
+ }
+
+ SkISize onISize() override {
+ return SkISize::Make(550, 420);
+ }
+
+ 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();
+
+ fRect = SkRect::MakeIWH(kTexSize, kTexSize);
+ SkMatrix matrix;
+ SkRect bitmapBounds = SkRect::MakeIWH(kTexSize, kTexSize);
+ matrix.setRectToRect(bitmapBounds, fRect, SkMatrix::kFill_ScaleToFit);
+
+ SkBitmap diffuseMap = sk_tool_utils::create_checkerboard_bitmap(
+ kTexSize, kTexSize,
+ sk_tool_utils::color_to_565(0x0),
+ sk_tool_utils::color_to_565(0xFF804020),
+ 8);
+ fDiffuse = SkMakeBitmapShader(diffuseMap, SkShader::kClamp_TileMode,
+ SkShader::kClamp_TileMode, &matrix, nullptr);
+ }
+
+ // Scales shape around origin, rotates shape around origin, then translates shape to origin
+ void positionCTM(SkCanvas *canvas, SkScalar scaleX, SkScalar scaleY, SkScalar rotate) const {
+ canvas->translate(kTexSize/2.0f, kTexSize/2.0f);
+ canvas->scale(scaleX, scaleY);
+ canvas->rotate(rotate);
+ canvas->translate(-kTexSize/2.0f, -kTexSize/2.0f);
+ }
+
+ static constexpr int NUM_BOOLEAN_PARAMS = 2;
+ void drawRect(SkCanvas* canvas, SkScalar scaleX, SkScalar scaleY, SkScalar rotate,
+ SkNormalSource::BevelType bevelType, bool useTranslucentPaint,
+ bool useNegativeHeight) {
+ canvas->save();
+
+ this->positionCTM(canvas, scaleX, scaleY, rotate);
+
+ SkPaint paint;
+
+ int width = 10.0f;
+ int height = useNegativeHeight ? -7.0f : 7.0f;
+ sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeBevel(bevelType, width, height);
+
+ if (useTranslucentPaint) {
+ paint.setAlpha(0x99);
+ }
+
+ paint.setShader(SkLightingShader::Make(fDiffuse, std::move(normalSource), fLights));
+ canvas->drawRect(fRect, paint);
+
+ canvas->restore();
+ }
+
+ void onDraw(SkCanvas* canvas) override {
+
+ constexpr SkScalar LABEL_SIZE = 10.0f;
+ SkPaint labelPaint;
+ labelPaint.setTypeface(sk_tool_utils::create_portable_typeface("sans-serif",
+ SkFontStyle()));
+ labelPaint.setAntiAlias(true);
+ labelPaint.setTextSize(LABEL_SIZE);
+
+ constexpr int GRID_COLUMN_NUM = 4;
+ constexpr SkScalar GRID_CELL_WIDTH = kTexSize + 20.0f + NUM_BOOLEAN_PARAMS * LABEL_SIZE;
+
+ int gridNum = 0;
+
+ // Running through all possible parameter combinations
+ for (auto bevelType : {SkNormalSource::BevelType::kLinear,
+ SkNormalSource::BevelType::kRoundedIn,
+ SkNormalSource::BevelType::kRoundedOut}) {
+ for (bool useTranslucentPaint : {true, false}) {
egdaniel 2016/07/26 19:54:41 Is there anything special with the bevel shader wh
dvonbeck 2016/07/27 17:44:57 At the time I was thinking the normal could get pr
+ for (bool useNegativeHeight : {true, false}) {
+
+ // Determining position
+ SkScalar xPos = (gridNum % GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
+ SkScalar yPos = (gridNum / GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
+
+ canvas->save();
+
+ canvas->translate(xPos, yPos);
+ this->drawRect(canvas, 1.0f, 1.0f, 0.f, bevelType, useTranslucentPaint,
+ useNegativeHeight);
+ // Drawing labels
+ canvas->translate(0.0f, SkIntToScalar(kTexSize));
+ {
+ canvas->translate(0.0f, LABEL_SIZE);
+ SkString label;
+ label.appendf("useTranslucentPaint: %d", useTranslucentPaint);
+ canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f, labelPaint);
+ }
+ {
+ canvas->translate(0.0f, LABEL_SIZE);
+ SkString label;
+ label.appendf("useNegativeHeight: %d", useNegativeHeight);
+ canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f, labelPaint);
+ }
+
+ canvas->restore();
+
+ gridNum++;
+ }
+ }
+ }
+ }
+
+private:
+ static const int kTexSize = 96;
+
+ sk_sp<SkShader> fDiffuse;
+
+ SkRect fRect;
+ sk_sp<SkLights> fLights;
+
+ typedef GM INHERITED;
+};
+
+//////////////////////////////////////////////////////////////////////////////
+
+DEF_GM(return new LightingShaderBevelGM;)
+}
« no previous file with comments | « no previous file | gyp/core.gypi » ('j') | src/core/SkNormalSource.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698