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

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: 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 canvas->translateZ(fTestRects[0].fDepth); 88 canvas->translateZ(fTestRects[0].fDepth);
105 } else { 89 } else {
106 canvas->translateZ(fTestRects[i].fDepth - fTestRects[i-1].fDepth ); 90 canvas->translateZ(fTestRects[i].fDepth - fTestRects[i-1].fDepth );
107 } 91 }
108 canvas->drawRect(fTestRects[i].fGeometry, paint); 92 canvas->drawRect(fTestRects[i].fGeometry, paint);
109 } 93 }
110 94
111 return recorder.finishRecordingAsPicture(); 95 return recorder.finishRecordingAsPicture();
112 } 96 }
113 97
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 { 98 void onDrawContent(SkCanvas *canvas) override {
192 if (fSceneChanged || !fPovDepthMap) { 99 if (fSceneChanged || !fPovDepthMap) {
193 fPicture = this->makeTestPicture(kWidth, kHeight); 100 fPicture = this->makeTestPicture(kWidth, kHeight);
194 this->updateDepthMaps(canvas); 101 for (int i = 0; i < fLights->numLights(); i++) {
195 this->updatePovDepthMap(canvas); 102 fLights->light(0).setShadowMap(nullptr);
196 this->updateDiffuseMap(canvas); 103 }
197 }
198
199 if (fLightsChanged) {
200 this->updateDepthMaps(canvas);
201 } 104 }
202 105
203 if (fSceneChanged || fLightsChanged || !fShadowShader) { 106 if (fSceneChanged || fLightsChanged || !fShadowShader) {
204 fShadowShader = make_shadow_shader(fPovDepthMap, fDiffuseMap, fLight s); 107 canvas->setLights(fLights);
108 SkPaint paint;
109 canvas->drawShadowedPicture(fPicture, nullptr, &paint);
205 } 110 }
206 111
207 SkPaint paint;
208 paint.setShader(fShadowShader);
209
210 canvas->drawRect(SkRect::MakeIWH(kWidth, kHeight), paint);
211 } 112 }
212 113
213 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove rride { 114 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove rride {
214 return new SkView::Click(this); 115 return new SkView::Click(this);
215 } 116 }
216 117
217 bool onClick(Click *click) override { 118 bool onClick(Click *click) override {
218 SkScalar x = click->fCurr.fX; 119 SkScalar x = click->fCurr.fX;
219 SkScalar y = click->fCurr.fY; 120 SkScalar y = click->fCurr.fY;
220 121
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 sk_sp<SkLights> fLights; 199 sk_sp<SkLights> fLights;
299 200
300 typedef SampleView INHERITED; 201 typedef SampleView INHERITED;
301 }; 202 };
302 203
303 ////////////////////////////////////////////////////////////////////////////// 204 //////////////////////////////////////////////////////////////////////////////
304 static SkView* MyFactory() { return new ShadowingView; } 205 static SkView* MyFactory() { return new ShadowingView; }
305 static SkViewRegister reg(MyFactory); 206 static SkViewRegister reg(MyFactory);
306 207
307 #endif 208 #endif
OLDNEW
« no previous file with comments | « gyp/common_variables.gypi ('k') | src/core/SkCanvas.cpp » ('j') | src/core/SkCanvas.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698