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

Side by Side 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: Fixed more windows warnings 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 | include/gpu/GrFragmentProcessor.h » ('j') | no next file with comments »
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 // Create a truncated pyramid normal map
17 static SkBitmap make_frustum_normalmap(int texSize) {
18 SkBitmap frustum;
19 frustum.allocN32Pixels(texSize, texSize);
20
21 sk_tool_utils::create_frustum_normal_map(&frustum, SkIRect::MakeWH(texSize, texSize));
22 return frustum;
23 }
24
25 namespace skiagm {
26
27 // This GM exercises lighting shaders. Specifically, nullptr arguments, scaling when using
28 // normal maps, and paint transparency.
29 class LightingShader2GM : public GM {
30 public:
31 LightingShader2GM() {
32 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
33 }
34
35 protected:
36 SkString onShortName() override {
37 return SkString("lightingshader2");
38 }
39
40 SkISize onISize() override {
41 return SkISize::Make(kGMSize, kGMSize);
42 }
43
44 void onOnceBeforeDraw() override {
45 SkLights::Builder builder;
46 const SkVector3 kLightFromUpperRight = SkVector3::Make(0.788f, 0.394f, 0 .473f);
47
48 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f),
49 kLightFromUpperRight));
50 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f)));
51 fLights = builder.finish();
52
53 fRect = SkRect::MakeIWH(kTexSize, kTexSize);
54 SkMatrix matrix;
55 SkRect bitmapBounds = SkRect::MakeIWH(kTexSize, kTexSize);
56 matrix.setRectToRect(bitmapBounds, fRect, SkMatrix::kFill_ScaleToFit);
57
58 SkBitmap opaqueDiffuseMap = sk_tool_utils::create_checkerboard_bitmap(
59 kTexSize, kTexSize,
60 sk_tool_utils::color_to_565(0x0),
61 sk_tool_utils::color_to_565(0xFF804020),
62 8);
63 fOpaqueDiffuse = SkMakeBitmapShader(opaqueDiffuseMap, SkShader::kClamp_T ileMode,
64 SkShader::kClamp_TileMode, &matrix, nullptr);
65
66 SkBitmap translucentDiffuseMap = sk_tool_utils::create_checkerboard_bitm ap(
67 kTexSize, kTexSize,
68 SkColorSetARGB(0x55, 0x00, 0x00, 0x00),
69 SkColorSetARGB(0x55, 0x80, 0x40, 0x20),
70 8);
71 fTranslucentDiffuse = SkMakeBitmapShader(translucentDiffuseMap, SkShader ::kClamp_TileMode,
72 SkShader::kClamp_TileMode, &mat rix, nullptr);
73
74 SkBitmap normalMap = make_frustum_normalmap(kTexSize);
75 fNormalMapShader = SkMakeBitmapShader(normalMap, SkShader::kClamp_TileMo de,
76 SkShader::kClamp_TileMode, &matrix , nullptr);
77
78 }
79
80 // Scales shape around origin, rotates shape around origin, then translates shape to origin
81 void positionCTM(SkCanvas *canvas, SkScalar scaleX, SkScalar scaleY, SkScala r rotate) const {
82 canvas->translate(kTexSize/2.0f, kTexSize/2.0f);
83 canvas->scale(scaleX, scaleY);
84 canvas->rotate(rotate);
85 canvas->translate(-kTexSize/2.0f, -kTexSize/2.0f);
86 }
87
88 static constexpr int NUM_BOOLEAN_PARAMS = 4;
89 void drawRect(SkCanvas* canvas, SkScalar scaleX, SkScalar scaleY,
90 SkScalar rotate, bool useNormalSource, bool useDiffuseShader,
91 bool useTranslucentPaint, bool useTranslucentShader) {
92 canvas->save();
93
94 this->positionCTM(canvas, scaleX, scaleY, rotate);
95
96 const SkMatrix& ctm = canvas->getTotalMatrix();
97
98 SkPaint paint;
99 sk_sp<SkNormalSource> normalSource = nullptr;
100 sk_sp<SkShader> diffuseShader = nullptr;
101
102 if (useNormalSource) {
103 normalSource = SkNormalSource::MakeFromNormalMap(fNormalMapShader, c tm);
104 }
105
106 if (useDiffuseShader) {
107 diffuseShader = (useTranslucentShader) ? fTranslucentDiffuse : fOpaq ueDiffuse;
108 } else {
109 paint.setColor(0xFF00FF00);
110 }
111
112 if (useTranslucentPaint) {
113 paint.setAlpha(0x99);
114 }
115
116 paint.setShader(SkLightingShader::Make(std::move(diffuseShader), std::mo ve(normalSource),
117 fLights));
118 canvas->drawRect(fRect, paint);
119
120 canvas->restore();
121 }
122
123 void onDraw(SkCanvas* canvas) override {
124
125 constexpr SkScalar LABEL_SIZE = 10.0f;
126 SkPaint labelPaint;
127 labelPaint.setTypeface(sk_tool_utils::create_portable_typeface("sans-ser if",
128 SkFontSty le()));
129 labelPaint.setAntiAlias(true);
130 labelPaint.setTextSize(LABEL_SIZE);
131
132 constexpr int GRID_COLUMN_NUM = 4;
133 constexpr SkScalar GRID_CELL_WIDTH = kTexSize + 20.0f + NUM_BOOLEAN_PARA MS * LABEL_SIZE;
134
135 int gridNum = 0;
136
137 // Running through all possible bool parameter combinations
138 for (bool useNormalSource : {true, false}) {
139 for (bool useDiffuseShader : {true, false}) {
140 for (bool useTranslucentPaint : {true, false}) {
141 for (bool useTranslucentShader : {true, false}) {
142
143 // Determining position
144 SkScalar xPos = (gridNum % GRID_COLUMN_NUM) * GRID_CELL_ WIDTH;
145 SkScalar yPos = (gridNum / GRID_COLUMN_NUM) * GRID_CELL_ WIDTH;
146
147 canvas->save();
148
149 canvas->translate(xPos, yPos);
150 this->drawRect(canvas, 1.0f, 1.0f, 0.f, useNormalSource, useDiffuseShader,
151 useTranslucentPaint, useTranslucentShader );
152 // Drawing labels
153 canvas->translate(0.0f, SkIntToScalar(kTexSize));
154 {
155 canvas->translate(0.0f, LABEL_SIZE);
156 SkString label;
157 label.appendf("useNormalSource: %d", useNormalSource );
158 canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f, labelPaint);
159 }
160 {
161 canvas->translate(0.0f, LABEL_SIZE);
162 SkString label;
163 label.appendf("useDiffuseShader: %d", useDiffuseShad er);
164 canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f, labelPaint);
165 }
166 {
167 canvas->translate(0.0f, LABEL_SIZE);
168 SkString label;
169 label.appendf("useTranslucentPaint: %d", useTransluc entPaint);
170 canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f, labelPaint);
171 }
172 {
173 canvas->translate(0.0f, LABEL_SIZE);
174 SkString label;
175 label.appendf("useTranslucentShader: %d", useTranslu centShader);
176 canvas->drawText(label.c_str(), label.size(), 0.0f, 0.0f, labelPaint);
177 }
178
179 canvas->restore();
180
181 gridNum++;
182 }
183 }
184 }
185 }
186
187
188 // Rotation/scale test
189 {
190 SkScalar xPos = (gridNum % GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
191 SkScalar yPos = (gridNum / GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
192
193 canvas->save();
194 canvas->translate(xPos, yPos);
195 this->drawRect(canvas, 0.6f, 0.6f, 45.0f, true, true, true, true);
196 canvas->restore();
197
198 gridNum++;
199 }
200
201 // Anisotropic scale test
202 {
203 SkScalar xPos = (gridNum % GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
204 SkScalar yPos = (gridNum / GRID_COLUMN_NUM) * GRID_CELL_WIDTH;
205
206 canvas->save();
207 canvas->translate(xPos, yPos);
208 this->drawRect(canvas, 0.6f, 0.4f, 30.0f, true, true, true, true);
209 canvas->restore();
210
211 gridNum++;
212 }
213 }
214
215 private:
216 static const int kTexSize = 96;
217 static const int kGMSize = 512;
218
219 sk_sp<SkShader> fOpaqueDiffuse;
220 sk_sp<SkShader> fTranslucentDiffuse;
221 sk_sp<SkShader> fNormalMapShader;
222
223 SkRect fRect;
224 sk_sp<SkLights> fLights;
225
226 typedef GM INHERITED;
227 };
228
229 //////////////////////////////////////////////////////////////////////////////
230
231 DEF_GM(return new LightingShader2GM;)
232 }
OLDNEW
« no previous file with comments | « no previous file | include/gpu/GrFragmentProcessor.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698