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

Side by Side Diff: samplecode/SampleShadowing.cpp

Issue 2220633002: moved code into onDrawShadowedPic, only renders into shadow maps if needed (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: fixed small crumbs Created 4 years, 4 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
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SampleCode.h" 8 #include "SampleCode.h"
9 #include "SkPictureRecorder.h" 9 #include "SkPictureRecorder.h"
10 #include "SkShadowPaintFilterCanvas.h" 10 #include "SkShadowPaintFilterCanvas.h"
11 #include "SkShadowShader.h" 11 #include "SkShadowShader.h"
12 #include "SkSurface.h" 12 #include "SkSurface.h"
13 13
14 #ifdef SK_EXPERIMENTAL_SHADOWING 14 #ifdef SK_EXPERIMENTAL_SHADOWING
15 15
16 static sk_sp<SkShader> make_shadow_shader(sk_sp<SkImage> povDepth,
17 sk_sp<SkImage> diffuse,
18 sk_sp<SkLights> lights) {
19
20 sk_sp<SkShader> povDepthShader = povDepth->makeShader(SkShader::kClamp_TileM ode,
21 SkShader::kClamp_TileM ode);
22
23 sk_sp<SkShader> diffuseShader = diffuse->makeShader(SkShader::kClamp_TileMod e,
24 SkShader::kClamp_TileMod e);
25
26 return SkShadowShader::Make(std::move(povDepthShader),
27 std::move(diffuseShader),
28 std::move(lights),
29 diffuse->width(), diffuse->height());
30 }
31
32 class ShadowingView : public SampleView { 16 class ShadowingView : public SampleView {
33 public: 17 public:
34 ShadowingView() { 18 ShadowingView() {
35 19
36 this->setBGColor(0xFFCCCCCC); 20 this->setBGColor(0xFFCCCCCC);
37 SkLights::Builder builder; 21 SkLights::Builder builder;
38 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.3f, 0.4f), 22 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.3f, 0.4f),
39 SkVector3::Make(0.27f, 0.07f, 1.0f))); 23 SkVector3::Make(0.27f, 0.07f, 1.0f)));
40 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.3f, 0.2f), 24 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.3f, 0.2f),
41 SkVector3::Make(0.03f, 0.27f, 1.0f))); 25 SkVector3::Make(0.03f, 0.27f, 1.0f)));
(...skipping 24 matching lines...) Expand all
66 if (SampleCode::TitleQ(*evt)) { 50 if (SampleCode::TitleQ(*evt)) {
67 SampleCode::TitleR(evt, "shadowing"); 51 SampleCode::TitleR(evt, "shadowing");
68 return true; 52 return true;
69 } 53 }
70 54
71 SkUnichar uni; 55 SkUnichar uni;
72 if (SampleCode::CharQ(*evt, &uni)) { 56 if (SampleCode::CharQ(*evt, &uni)) {
73 switch (uni) { 57 switch (uni) {
74 case 'L': 58 case 'L':
75 fMoveLight = !fMoveLight; 59 fMoveLight = !fMoveLight;
76 break; 60 break;
robertphillips 2016/08/08 14:18:31 // Comment about why you need this here ?
vjiaoblack 2016/08/08 15:48:31 Done.
61 case 'd':
62 fClearShadowMaps = true;
jvanverth1 2016/08/08 14:15:47 Should this be a toggle? How do you set it to fals
vjiaoblack 2016/08/08 15:48:31 When the if (fClearShadowMaps) triggers, I edited
63 break;
77 default: 64 default:
78 break; 65 break;
79 } 66 }
80 } 67 }
81 return this->INHERITED::onQuery(evt); 68 return this->INHERITED::onQuery(evt);
82 } 69 }
83 70
84 sk_sp<SkPicture> makeTestPicture(int width, int height) { 71 sk_sp<SkPicture> makeTestPicture(int width, int height) {
85 SkPictureRecorder recorder; 72 SkPictureRecorder recorder;
86 73
(...skipping 17 matching lines...) Expand all
104 canvas->translateZ(fTestRects[0].fDepth); 91 canvas->translateZ(fTestRects[0].fDepth);
105 } else { 92 } else {
106 canvas->translateZ(fTestRects[i].fDepth - fTestRects[i-1].fDepth ); 93 canvas->translateZ(fTestRects[i].fDepth - fTestRects[i-1].fDepth );
107 } 94 }
108 canvas->drawRect(fTestRects[i].fGeometry, paint); 95 canvas->drawRect(fTestRects[i].fGeometry, paint);
109 } 96 }
110 97
111 return recorder.finishRecordingAsPicture(); 98 return recorder.finishRecordingAsPicture();
112 } 99 }
113 100
114 void updateDepthMaps(SkCanvas *canvas) {
115 for (int i = 0; i < fLights->numLights(); ++i) {
116 // skip over ambient lights; they don't cast shadows
117 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type()) {
118 continue;
119 }
120
121 // TODO: maybe add a kDepth_8_SkColorType when vertices have depth
122 // assume max depth is 255.
123 // TODO: find actual max depth of picture
124 SkISize shMapSize = SkShadowPaintFilterCanvas::
125 ComputeDepthMapSize(fLights->light(i), 255, kWid th, kHeight);
126
127 SkImageInfo info = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHe ight,
128 kBGRA_8888_SkColorType,
129 kOpaque_SkAlphaType);
130
131 // Create a new surface (that matches the backend of canvas)
132 // for each shadow map
133 sk_sp<SkSurface> surf(canvas->makeSurface(info));
134
135 // Wrap another SPFCanvas around the surface
136 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas =
137 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas());
138
139 // set the depth map canvas to have the light we're drawing.
140 SkLights::Builder builder;
141 builder.add(fLights->light(i));
142 sk_sp<SkLights> curLight = builder.finish();
143
144 depthMapCanvas->setLights(std::move(curLight));
145 depthMapCanvas->drawPicture(fPicture);
146
147 fLights->light(i).setShadowMap(surf->makeImageSnapshot());
148 }
149 }
150
151 void updatePovDepthMap(SkCanvas* canvas) {
152 // TODO: pass the depth to the shader in vertices, or uniforms
153 // so we don't have to render depth and color separately
154
155 SkLights::Builder builder;
156 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f),
157 SkVector3::Make(0.0f, 0.0f, 1.0f)));
158 sk_sp<SkLights> povLight = builder.finish();
159
160 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight,
161 kBGRA_8888_SkColorType,
162 kOpaque_SkAlphaType);
163
164 // Create a new surface (that matches the backend of canvas)
165 // to create the povDepthMap
166 sk_sp<SkSurface> surf(canvas->makeSurface(info));
167
168 // Wrap another SPFCanvas around the surface
169 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas =
170 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas());
171
172 // set the depth map canvas to have the light as the user's POV
173 depthMapCanvas->setLights(std::move(povLight));
174
175 depthMapCanvas->drawPicture(fPicture);
176
177 fPovDepthMap = surf->makeImageSnapshot();
178 }
179
180 void updateDiffuseMap(SkCanvas* canvas) {
181 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight,
182 kBGRA_8888_SkColorType,
183 kOpaque_SkAlphaType);
184
185 sk_sp<SkSurface> surf(canvas->makeSurface(info));
186 surf->getCanvas()->drawPicture(fPicture);
187
188 fDiffuseMap = surf->makeImageSnapshot();
189 }
190
191 void onDrawContent(SkCanvas *canvas) override { 101 void onDrawContent(SkCanvas *canvas) override {
192 if (fSceneChanged || !fPovDepthMap) { 102 if (fSceneChanged || !fPovDepthMap) {
193 fPicture = this->makeTestPicture(kWidth, kHeight); 103 fPicture = this->makeTestPicture(kWidth, kHeight);
194 this->updateDepthMaps(canvas);
195 this->updatePovDepthMap(canvas);
196 this->updateDiffuseMap(canvas);
197 }
198
199 if (fLightsChanged) {
200 this->updateDepthMaps(canvas);
jvanverth1 2016/08/08 14:15:47 Any thoughts on how you'd manage this caching with
vjiaoblack 2016/08/08 15:48:31 yeah, we just only cache when there is no change a
201 } 104 }
202 105
203 if (fSceneChanged || fLightsChanged || !fShadowShader) { 106 if (fSceneChanged || fLightsChanged || !fShadowShader) {
204 fShadowShader = make_shadow_shader(fPovDepthMap, fDiffuseMap, fLight s); 107 if (fClearShadowMaps) {
108 for (int i = 0; i < fLights->numLights(); i++) {
109 fLights->light(i).setShadowMap(nullptr);
110 }
111 }
112
113 canvas->setLights(fLights);
robertphillips 2016/08/08 14:18:31 pass in nullptr for paint argument ?
vjiaoblack 2016/08/08 15:48:31 Done.
114 SkPaint paint;
115 canvas->drawShadowedPicture(fPicture, nullptr, &paint);
205 } 116 }
206
207 SkPaint paint;
208 paint.setShader(fShadowShader);
209
210 canvas->drawRect(SkRect::MakeIWH(kWidth, kHeight), paint);
211 } 117 }
212 118
213 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove rride { 119 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove rride {
214 return new SkView::Click(this); 120 return new SkView::Click(this);
215 } 121 }
216 122
217 bool onClick(Click *click) override { 123 bool onClick(Click *click) override {
218 SkScalar x = click->fCurr.fX; 124 SkScalar x = click->fCurr.fX;
219 SkScalar y = click->fCurr.fY; 125 SkScalar y = click->fCurr.fY;
220 126
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 } 176 }
271 177
272 return true; 178 return true;
273 } 179 }
274 180
275 private: 181 private:
276 static constexpr int kNumTestRects = 3; 182 static constexpr int kNumTestRects = 3;
277 183
278 static const int kWidth = 400; 184 static const int kWidth = 400;
279 static const int kHeight = 400; 185 static const int kHeight = 400;
186 bool fClearShadowMaps;
jvanverth1 2016/08/08 14:15:47 This is never initialized.
vjiaoblack 2016/08/08 15:48:31 Done.
280 187
281 struct { 188 struct {
282 SkRect fGeometry; 189 SkRect fGeometry;
283 int fDepth; 190 int fDepth;
284 SkColor fColor; 191 SkColor fColor;
285 } fTestRects[kNumTestRects]; 192 } fTestRects[kNumTestRects];
286 193
287 int fSelectedRect; 194 int fSelectedRect;
288 bool fMoveLight; 195 bool fMoveLight;
289 196
robertphillips 2016/08/08 14:18:31 rm fPovDepthMap, fDiffuseMap & fShadowShader ?
vjiaoblack 2016/08/08 15:48:31 Done.
290 sk_sp<SkImage> fPovDepthMap; 197 sk_sp<SkImage> fPovDepthMap;
291 sk_sp<SkImage> fDiffuseMap; 198 sk_sp<SkImage> fDiffuseMap;
292 sk_sp<SkPicture> fPicture; 199 sk_sp<SkPicture> fPicture;
293 sk_sp<SkShader> fShadowShader; 200 sk_sp<SkShader> fShadowShader;
294 201
295 bool fSceneChanged; 202 bool fSceneChanged;
296 bool fLightsChanged; 203 bool fLightsChanged;
297 204
298 sk_sp<SkLights> fLights; 205 sk_sp<SkLights> fLights;
299 206
300 typedef SampleView INHERITED; 207 typedef SampleView INHERITED;
301 }; 208 };
302 209
303 ////////////////////////////////////////////////////////////////////////////// 210 //////////////////////////////////////////////////////////////////////////////
304 static SkView* MyFactory() { return new ShadowingView; } 211 static SkView* MyFactory() { return new ShadowingView; }
305 static SkViewRegister reg(MyFactory); 212 static SkViewRegister reg(MyFactory);
306 213
307 #endif 214 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698