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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | gyp/core.gypi » ('j') | src/core/SkNormalSource.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "gm.h"
9
10 #include "SkBitmapProcShader.h"
11 #include "SkLightingShader.h"
12 #include "SkNormalSource.h"
13 #include "SkPoint3.h"
14 #include "SkShader.h"
15
16
17 namespace skiagm {
18
19 // This GM exercises lighting shaders when used with bevel SkNormalSource object s.
20 class LightingShaderBevelGM : public GM {
21 public:
22 LightingShaderBevelGM() {
23 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
24 }
25
26 protected:
27 SkString onShortName() override {
28 return SkString("lightingshaderbevel");
29 }
30
31 SkISize onISize() override {
32 return SkISize::Make(550, 420);
33 }
34
35 void onOnceBeforeDraw() override {
36 SkLights::Builder builder;
37 const SkVector3 kLightFromUpperRight = SkVector3::Make(0.788f, 0.394f, 0 .473f);
38
39 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f),
40 kLightFromUpperRight));
41 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f)));
42 fLights = builder.finish();
43
44 fRect = SkRect::MakeIWH(kTexSize, kTexSize);
45 SkMatrix matrix;
46 SkRect bitmapBounds = SkRect::MakeIWH(kTexSize, kTexSize);
47 matrix.setRectToRect(bitmapBounds, fRect, SkMatrix::kFill_ScaleToFit);
48
49 SkBitmap diffuseMap = sk_tool_utils::create_checkerboard_bitmap(
50 kTexSize, kTexSize,
51 sk_tool_utils::color_to_565(0x0),
52 sk_tool_utils::color_to_565(0xFF804020),
53 8);
54 fDiffuse = SkMakeBitmapShader(diffuseMap, SkShader::kClamp_TileMode,
55 SkShader::kClamp_TileMode, &matrix, nullpt r);
56 }
57
58 // Scales shape around origin, rotates shape around origin, then translates shape to origin
59 void positionCTM(SkCanvas *canvas, SkScalar scaleX, SkScalar scaleY, SkScala r rotate) const {
60 canvas->translate(kTexSize/2.0f, kTexSize/2.0f);
61 canvas->scale(scaleX, scaleY);
62 canvas->rotate(rotate);
63 canvas->translate(-kTexSize/2.0f, -kTexSize/2.0f);
64 }
65
66 static constexpr int NUM_BOOLEAN_PARAMS = 2;
67 void drawRect(SkCanvas* canvas, SkScalar scaleX, SkScalar scaleY, SkScalar r otate,
68 SkNormalSource::BevelType bevelType, bool useTranslucentPaint,
69 bool useNegativeHeight) {
70 canvas->save();
71
72 this->positionCTM(canvas, scaleX, scaleY, rotate);
73
74 SkPaint paint;
75
76 int width = 10.0f;
77 int height = useNegativeHeight ? -7.0f : 7.0f;
78 sk_sp<SkNormalSource> normalSource = SkNormalSource::MakeBevel(bevelType , width, height);
79
80 if (useTranslucentPaint) {
81 paint.setAlpha(0x99);
82 }
83
84 paint.setShader(SkLightingShader::Make(fDiffuse, std::move(normalSource) , fLights));
85 canvas->drawRect(fRect, paint);
86
87 canvas->restore();
88 }
89
90 void onDraw(SkCanvas* canvas) override {
91
92 constexpr SkScalar LABEL_SIZE = 10.0f;
93 SkPaint labelPaint;
94 labelPaint.setTypeface(sk_tool_utils::create_portable_typeface("sans-ser if",
95 SkFontSty le()));
96 labelPaint.setAntiAlias(true);
97 labelPaint.setTextSize(LABEL_SIZE);
98
99 constexpr int GRID_COLUMN_NUM = 4;
100 constexpr SkScalar GRID_CELL_WIDTH = kTexSize + 20.0f + NUM_BOOLEAN_PARA MS * LABEL_SIZE;
101
102 int gridNum = 0;
103
104 // Running through all possible parameter combinations
105 for (auto bevelType : {SkNormalSource::BevelType::kLinear,
106 SkNormalSource::BevelType::kRoundedIn,
107 SkNormalSource::BevelType::kRoundedOut}) {
108 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
109 for (bool useNegativeHeight : {true, false}) {
110
111 // Determining position
112 SkScalar xPos = (gridNum % GRID_COLUMN_NUM) * GRID_CELL_WIDT H;
113 SkScalar yPos = (gridNum / GRID_COLUMN_NUM) * GRID_CELL_WIDT H;
114
115 canvas->save();
116
117 canvas->translate(xPos, yPos);
118 this->drawRect(canvas, 1.0f, 1.0f, 0.f, bevelType, useTransl ucentPaint,
119 useNegativeHeight);
120 // Drawing labels
121 canvas->translate(0.0f, SkIntToScalar(kTexSize));
122 {
123 canvas->translate(0.0f, LABEL_SIZE);
124 SkString label;
125 label.appendf("useTranslucentPaint: %d", useTranslucentP aint);
126 canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f , labelPaint);
127 }
128 {
129 canvas->translate(0.0f, LABEL_SIZE);
130 SkString label;
131 label.appendf("useNegativeHeight: %d", useNegativeHeight );
132 canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f , labelPaint);
133 }
134
135 canvas->restore();
136
137 gridNum++;
138 }
139 }
140 }
141 }
142
143 private:
144 static const int kTexSize = 96;
145
146 sk_sp<SkShader> fDiffuse;
147
148 SkRect fRect;
149 sk_sp<SkLights> fLights;
150
151 typedef GM INHERITED;
152 };
153
154 //////////////////////////////////////////////////////////////////////////////
155
156 DEF_GM(return new LightingShaderBevelGM;)
157 }
OLDNEW
« 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