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

Side by Side Diff: gm/shadowmaps.cpp

Issue 2118553002: adding new GM to demostrate new shadows (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: Lines too long 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
« no previous file with comments | « no previous file | gyp/core.gypi » ('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
9 #include "gm.h"
10 #include "SkPaintFilterCanvas.h"
11 #include "SkPathEffect.h"
12 #include "SkPictureRecorder.h"
13 #include "SkShadowShader.h"
14 #include "SkSurface.h"
15
16 #ifdef SK_EXPERIMENTAL_SHADOWING
17
18 static sk_sp<SkShader> make_shadow_shader(sk_sp<SkImage> povDepth,
19 sk_sp<SkImage> diffuse,
20 sk_sp<SkLights> lights) {
21
22 sk_sp<SkShader> povDepthShader = povDepth->makeShader(SkShader::kClamp_TileM ode,
23 SkShader::kClamp_TileM ode);
24
25 sk_sp<SkShader> diffuseShader = diffuse->makeShader(SkShader::kClamp_TileMod e,
26 SkShader::kClamp_TileMod e);
27
28 return SkShadowShader::Make(std::move(povDepthShader),
29 std::move(diffuseShader),
30 std::move(lights),
31 diffuse->width(), diffuse->height());
32 }
33
34 static sk_sp<SkPicture> make_test_picture(int width, int height) {
35 SkPictureRecorder recorder;
36
37 // LONG RANGE TODO: eventually add SkBBHFactory (bounding box factory)
38 SkCanvas* canvas = recorder.beginRecording(SkRect::MakeIWH(width, height));
39
40 SkASSERT(canvas->getTotalMatrix().isIdentity());
41 SkPaint paint;
42 paint.setColor(SK_ColorGRAY);
43
44 // LONG RANGE TODO: tag occluders
45 // LONG RANGE TODO: track number of IDs we need (hopefully less than 256)
46 // and determinate the mapping from z to id
47
48 // universal receiver, "ground"
49 canvas->drawRect(SkRect::MakeIWH(width, height), paint);
50
51 // TODO: Maybe add the ID here along with the depth
52
53 paint.setColor(0xFFEE8888);
54
55 canvas->translateZ(80);
56 canvas->drawRect(SkRect::MakeLTRB(200,150,350,300), paint);
57
58 paint.setColor(0xFF88EE88);
59
60 canvas->translateZ(80);
61 canvas->drawRect(SkRect::MakeLTRB(150,200,300,350), paint);
62
63 paint.setColor(0xFF8888EE);
64
65 canvas->translateZ(80);
66 canvas->drawRect(SkRect::MakeLTRB(100,100,250,250), paint);
67 // TODO: Add an assert that Z order matches painter's order
68 // TODO: think about if the Z-order always matching painting order is too st rict
69
70 return recorder.finishRecordingAsPicture();
71 }
72
73 namespace skiagm {
74
75 /* We override the onFilter method to draw depths into the canvas
76 * depending on the current draw depth of the canvas, throwing out
77 * the actual draw color.
78 */
79 class SkShadowPaintFilterCanvas : public SkPaintFilterCanvas {
80 public:
81
82 SkShadowPaintFilterCanvas(SkCanvas* canvas) : INHERITED(canvas) { }
83
84 // TODO use a shader instead
85 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type type) const override {
86 if (*paint) {
87 int z = this->getZ();
88 SkASSERT(z <= 0xFF && z >= 0x00);
89
90 SkPaint newPaint;
91 newPaint.setPathEffect(sk_ref_sp<SkPathEffect>((*paint)->getPathEffe ct()));
92
93 SkColor color = 0xFF000000; // init color to opaque black
94 color |= z; // Put the index into the blue component
95 newPaint.setColor(color);
96
97 *paint->writable() = newPaint;
98 }
99
100 return true;
101 }
102
103 void onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, const S kPaint* paint) {
104 SkTCopyOnFirstWrite<SkPaint> filteredPaint(paint);
105 if (this->onFilter(&filteredPaint, kPicture_Type)) {
106 // we directly call SkCanvas's onDrawPicture because calling the one
107 // that INHERITED has (SkPaintFilterCanvas) leads to wrong behavior
108 this->SkCanvas::onDrawPicture(picture, matrix, filteredPaint);
109 }
110 }
111
112 void updateMatrix() {
113 this->save();
114
115 // When we use the SkShadowPaintFilterCanvas, we can only render
116 // one depth map at a time. Thus, we leave it up to the user to
117 // set SkLights to only contain (or contain at the first position)
118 // the light they intend to use for the current depth rendering.
119
120 if (fLights->numLights() > 0 &&
121 this->fLights->light(0).type() == SkLights::Light::kDirectional_Ligh tType) {
122 SkVector3 lightDir = this->fLights->light(0).dir();
123 SkScalar x = lightDir.fX * this->getZ();
124 SkScalar y = lightDir.fY * this->getZ();
125
126 this->translate(x, y);
127 }
128
129 }
130
131 void onDrawPaint(const SkPaint& paint) override {
132 this->updateMatrix();
133 this->INHERITED::onDrawPaint(paint);
134 this->restore();
135 }
136
137 void onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
138 const SkPaint& paint) override {
139 this->updateMatrix();
140 this->INHERITED::onDrawPoints(mode, count, pts, paint);
141 this->restore();
142 }
143
144 void onDrawRect(const SkRect& rect, const SkPaint& paint) override {
145 this->updateMatrix();
146 this->INHERITED::onDrawRect(rect, paint);
147 this->restore();
148 }
149
150 void onDrawRRect(const SkRRect& rrect, const SkPaint& paint) override {
151 this->updateMatrix();
152 this->INHERITED::onDrawRRect(rrect, paint);
153 this->restore();
154 }
155
156 void onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
157 const SkPaint& paint) override {
158 this->updateMatrix();
159 this->INHERITED::onDrawDRRect(outer, inner, paint);
160 this->restore();
161 }
162
163 void onDrawOval(const SkRect& rect, const SkPaint& paint) override {
164 this->updateMatrix();
165 this->INHERITED::onDrawOval(rect, paint);
166 this->restore();
167 }
168
169 void onDrawPath(const SkPath& path, const SkPaint& paint) override {
170 this->updateMatrix();
171 this->INHERITED::onDrawPath(path, paint);
172 this->restore();
173 }
174
175 void onDrawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top,
176 const SkPaint* paint) override {
177 this->updateMatrix();
178 this->INHERITED::onDrawBitmap(bm, left, top, paint);
179 this->restore();
180 }
181
182 void onDrawBitmapRect(const SkBitmap& bm, const SkRect* src, const SkRect& d st,
183 const SkPaint* paint, SrcRectConstraint constraint) ov erride {
184 this->updateMatrix();
185 this->INHERITED::onDrawBitmapRect(bm, src, dst, paint, constraint);
186 this->restore();
187 }
188
189 void onDrawBitmapNine(const SkBitmap& bm, const SkIRect& center,
190 const SkRect& dst, const SkPaint* paint) {
191 this->updateMatrix();
192 this->INHERITED::onDrawBitmapNine(bm, center, dst, paint);
193 this->restore();
194 }
195
196 void onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
197 const SkPaint* paint) override {
198 this->updateMatrix();
199 this->INHERITED::onDrawImage(image, left, top, paint);
200 this->restore();
201 }
202
203 void onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
204 const SkPaint* paint, SrcRectConstraint constraint) ove rride {
205 this->updateMatrix();
206 this->INHERITED::onDrawImageRect(image, src, dst, paint, constraint);
207 this->restore();
208 }
209
210 void onDrawImageNine(const SkImage* image, const SkIRect& center,
211 const SkRect& dst, const SkPaint* paint) {
212 this->updateMatrix();
213 this->INHERITED::onDrawImageNine(image, center, dst, paint);
214 this->restore();
215 }
216
217 void onDrawVertices(VertexMode vmode, int vertexCount, const SkPoint vertice s[],
218 const SkPoint texs[], const SkColor colors[], SkXfermode * xmode,
219 const uint16_t indices[], int indexCount, const SkPaint& paint) override {
220 this->updateMatrix();
221 this->INHERITED::onDrawVertices(vmode, vertexCount, vertices, texs, colo rs,
222 xmode, indices, indexCount, paint);
223 this->restore();
224 }
225
226 void onDrawPatch(const SkPoint cubics[], const SkColor colors[], const SkPoi nt texCoords[],
227 SkXfermode* xmode, const SkPaint& paint) override {
228 this->updateMatrix();
229 this->INHERITED::onDrawPatch(cubics, colors, texCoords, xmode, paint);
230 this->restore();
231 }
232
233 void onDrawText(const void* text, size_t byteLength,
234 SkScalar x, SkScalar y, const SkPaint& paint) override {
235 this->updateMatrix();
236 this->INHERITED::onDrawText(text, byteLength, x, y, paint);
237 this->restore();
238 }
239
240 void onDrawPosText(const void* text, size_t byteLength,
241 const SkPoint pos[], const SkPaint& paint) override {
242 this->updateMatrix();
243 this->INHERITED::onDrawPosText(text, byteLength, pos, paint);
244 this->restore();
245 }
246
247 void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos [],
248 SkScalar constY, const SkPaint& paint) override {
249 this->updateMatrix();
250 this->INHERITED::onDrawPosTextH(text, byteLength, xpos, constY, paint);
251 this->restore();
252 }
253
254 void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& pat h,
255 const SkMatrix* matrix, const SkPaint& paint) override {
256 this->updateMatrix();
257 this->INHERITED::onDrawTextOnPath(text, byteLength, path, matrix, paint) ;
258 this->restore();
259 }
260
261 void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
262 const SkRect* cull, const SkPaint& paint) override {
263 this->updateMatrix();
264 this->INHERITED::onDrawTextRSXform(text, byteLength, xform, cull, paint) ;
265 this->restore();
266 }
267
268 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
269 const SkPaint& paint) override {
270 this->updateMatrix();
271 this->INHERITED::onDrawTextBlob(blob, x, y, paint);
272 this->restore();
273 }
274
275 private:
276 typedef SkPaintFilterCanvas INHERITED;
277 };
278
279 class ShadowMapsGM : public GM {
280 public:
281 ShadowMapsGM() {
282 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
283 }
284
285 void onOnceBeforeDraw() override {
286 // Create a light set consisting of
287 // - bluish directional light pointing more right than down
288 // - reddish directional light pointing more down than right
289 // - soft white ambient light
290
291 SkLights::Builder builder;
292 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.3f, 0.4f),
293 SkVector3::Make(0.2f, 0.1f, 1.0f)));
294 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.3f, 0.2f),
295 SkVector3::Make(0.1f, 0.2f, 1.0f)));
296 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.4f, 0.4f)));
297 fLights = builder.finish();
298 }
299
300 protected:
301 static const int kWidth = 400;
302 static const int kHeight = 400;
303
304 SkString onShortName() override {
305 return SkString("shadowmaps");
306 }
307
308 SkISize onISize() override {
309 return SkISize::Make(kWidth, kHeight);
310 }
311
312 void onDraw(SkCanvas* canvas) override {
313 // This picture stores the picture of the scene.
314 // It's used to generate the depth maps.
315 sk_sp<SkPicture> pic(make_test_picture(kWidth, kHeight));
316
317 for (int i = 0; i < fLights->numLights(); ++i) {
318 // skip over ambient lights; they don't cast shadows
319 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type()) {
320 continue;
321 }
322 // TODO: compute the correct size of the depth map from the light pr operties
323 // TODO: maybe add a kDepth_8_SkColorType
324
325 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight,
326 kBGRA_8888_SkColorType,
327 kOpaque_SkAlphaType);
328
329 // Create a new surface (that matches the backend of canvas)
330 // for each shadow map
331 sk_sp<SkSurface> surf(canvas->makeSurface(info));
332
333 // Wrap another SPFCanvas around the surface
334 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas =
335 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas());
336
337 // set the depth map canvas to have the light we're drawing.
338 SkLights::Builder builder;
339 builder.add(fLights->light(i));
340 sk_sp<SkLights> curLight = builder.finish();
341
342 depthMapCanvas->setLights(std::move(curLight));
343 depthMapCanvas->drawPicture(pic);
344
345 fLights->light(i).setShadowMap(surf->makeImageSnapshot());
346 }
347
348 sk_sp<SkImage> povDepthMap;
349 sk_sp<SkImage> diffuseMap;
350
351 // TODO: pass the depth to the shader in vertices, or uniforms
352 // so we don't have to render depth and color separately
353
354 // povDepthMap
355 {
356 SkLights::Builder builder;
357 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f),
358 SkVector3::Make(0.0f, 0.0f, 1.0f)));
359 sk_sp<SkLights> povLight = builder.finish();
360
361 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight,
362 kBGRA_8888_SkColorType,
363 kOpaque_SkAlphaType);
364
365 // Create a new surface (that matches the backend of canvas)
366 // to create the povDepthMap
367 sk_sp<SkSurface> surf(canvas->makeSurface(info));
368
369 // Wrap another SPFCanvas around the surface
370 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas =
371 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas());
372
373 // set the depth map canvas to have the light as the user's POV
374 depthMapCanvas->setLights(std::move(povLight));
375
376 depthMapCanvas->drawPicture(pic);
377
378 povDepthMap = surf->makeImageSnapshot();
379 }
380
381 // diffuseMap
382 {
383 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight,
384 kBGRA_8888_SkColorType,
385 kOpaque_SkAlphaType);
386
387 sk_sp<SkSurface> surf(canvas->makeSurface(info));
388 surf->getCanvas()->drawPicture(pic);
389
390 diffuseMap = surf->makeImageSnapshot();
391 }
392
393 SkPaint paint;
394 paint.setShader(make_shadow_shader(std::move(povDepthMap), std::move(dif fuseMap), fLights));
395
396 canvas->drawRect(SkRect::MakeIWH(kWidth, kHeight), paint);
397 }
398
399 private:
400 sk_sp<SkLights> fLights;
401
402 typedef GM INHERITED;
403 };
404
405 //////////////////////////////////////////////////////////////////////////////
406
407 DEF_GM(return new ShadowMapsGM;)
408 }
409
410 #endif
OLDNEW
« no previous file with comments | « no previous file | gyp/core.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698