blob: f953550d4bb55ac74b169d96dc64a8c261f6d4d7 [file] [log] [blame]
vjiaoblack53da5ba2016-08-01 10:02:31 -07001/*
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"
vjiaoblack53da5ba2016-08-01 10:02:31 -070010#include "SkPathEffect.h"
11#include "SkPictureRecorder.h"
vjiaoblack955e8792016-08-05 07:55:01 -070012#include "SkShadowPaintFilterCanvas.h"
vjiaoblack53da5ba2016-08-01 10:02:31 -070013#include "SkShadowShader.h"
14#include "SkSurface.h"
15
16#ifdef SK_EXPERIMENTAL_SHADOWING
17
vjiaoblack53da5ba2016-08-01 10:02:31 -070018
19static sk_sp<SkPicture> make_test_picture(int width, int height) {
20 SkPictureRecorder recorder;
21
22 // LONG RANGE TODO: eventually add SkBBHFactory (bounding box factory)
23 SkCanvas* canvas = recorder.beginRecording(SkRect::MakeIWH(width, height));
24
25 SkASSERT(canvas->getTotalMatrix().isIdentity());
26 SkPaint paint;
27 paint.setColor(SK_ColorGRAY);
28
29 // LONG RANGE TODO: tag occluders
30 // LONG RANGE TODO: track number of IDs we need (hopefully less than 256)
31 // and determinate the mapping from z to id
32
33 // universal receiver, "ground"
34 canvas->drawRect(SkRect::MakeIWH(width, height), paint);
35
36 // TODO: Maybe add the ID here along with the depth
37
38 paint.setColor(0xFFEE8888);
39
40 canvas->translateZ(80);
41 canvas->drawRect(SkRect::MakeLTRB(200,150,350,300), paint);
42
43 paint.setColor(0xFF88EE88);
44
45 canvas->translateZ(80);
46 canvas->drawRect(SkRect::MakeLTRB(150,200,300,350), paint);
47
48 paint.setColor(0xFF8888EE);
49
50 canvas->translateZ(80);
51 canvas->drawRect(SkRect::MakeLTRB(100,100,250,250), paint);
52 // TODO: Add an assert that Z order matches painter's order
53 // TODO: think about if the Z-order always matching painting order is too strict
54
55 return recorder.finishRecordingAsPicture();
56}
57
58namespace skiagm {
59
vjiaoblack53da5ba2016-08-01 10:02:31 -070060class ShadowMapsGM : public GM {
61public:
62 ShadowMapsGM() {
63 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
64 }
65
66 void onOnceBeforeDraw() override {
67 // Create a light set consisting of
68 // - bluish directional light pointing more right than down
69 // - reddish directional light pointing more down than right
70 // - soft white ambient light
71
72 SkLights::Builder builder;
vjiaoblack772b5ee2016-08-12 11:38:47 -070073 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(0.2f, 0.3f, 0.4f),
74 SkVector3::Make(0.2f, 0.1f, 1.0f)));
75 builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(0.4f, 0.3f, 0.2f),
76 SkVector3::Make(0.1f, 0.2f, 1.0f)));
77 builder.add(SkLights::Light::MakeAmbient(SkColor3f::Make(0.4f, 0.4f, 0.4f)));
vjiaoblack53da5ba2016-08-01 10:02:31 -070078 fLights = builder.finish();
79 }
80
81protected:
82 static const int kWidth = 400;
83 static const int kHeight = 400;
84
85 SkString onShortName() override {
86 return SkString("shadowmaps");
87 }
88
89 SkISize onISize() override {
90 return SkISize::Make(kWidth, kHeight);
91 }
92
93 void onDraw(SkCanvas* canvas) override {
94 // This picture stores the picture of the scene.
95 // It's used to generate the depth maps.
96 sk_sp<SkPicture> pic(make_test_picture(kWidth, kHeight));
vjiaoblack904527d2016-08-09 09:32:09 -070097 canvas->setLights(fLights);
98 canvas->drawShadowedPicture(pic, nullptr, nullptr);
vjiaoblack53da5ba2016-08-01 10:02:31 -070099 }
100
101private:
102 sk_sp<SkLights> fLights;
vjiaoblack53da5ba2016-08-01 10:02:31 -0700103 typedef GM INHERITED;
104};
105
106//////////////////////////////////////////////////////////////////////////////
107
108DEF_GM(return new ShadowMapsGM;)
109}
110
111#endif