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

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

Powered by Google App Engine
This is Rietveld 408576698