blob: b675aca2b3aea402a1b98db58305c2c5142b1d2b [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)));
vjiaoblacka8eabc42016-08-29 10:22:09 -070077 builder.setAmbientLightColor(SkColor3f::Make(0.4f, 0.4f, 0.4f));
vjiaoblack53da5ba2016-08-01 10:02:31 -070078 fLights = builder.finish();
vjiaoblacke6f5d562016-08-25 06:30:23 -070079
80 fShadowParams.fShadowRadius = 4.0f;
81 fShadowParams.fBiasingConstant = 0.3f;
82 fShadowParams.fMinVariance = 1024;
83 fShadowParams.fType = SkShadowParams::kVariance_ShadowType;
vjiaoblack53da5ba2016-08-01 10:02:31 -070084 }
85
86protected:
vjiaoblackb2796fd2016-09-09 09:22:39 -070087 static constexpr int kWidth = 400;
88 static constexpr int kHeight = 400;
vjiaoblack53da5ba2016-08-01 10:02:31 -070089
90 SkString onShortName() override {
91 return SkString("shadowmaps");
92 }
93
94 SkISize onISize() override {
95 return SkISize::Make(kWidth, kHeight);
96 }
97
98 void onDraw(SkCanvas* canvas) override {
99 // This picture stores the picture of the scene.
100 // It's used to generate the depth maps.
101 sk_sp<SkPicture> pic(make_test_picture(kWidth, kHeight));
vjiaoblack904527d2016-08-09 09:32:09 -0700102 canvas->setLights(fLights);
vjiaoblacke6f5d562016-08-25 06:30:23 -0700103 canvas->drawShadowedPicture(pic, nullptr, nullptr, fShadowParams);
vjiaoblack53da5ba2016-08-01 10:02:31 -0700104 }
105
106private:
107 sk_sp<SkLights> fLights;
vjiaoblacke6f5d562016-08-25 06:30:23 -0700108 SkShadowParams fShadowParams;
vjiaoblack53da5ba2016-08-01 10:02:31 -0700109 typedef GM INHERITED;
110};
111
112//////////////////////////////////////////////////////////////////////////////
113
114DEF_GM(return new ShadowMapsGM;)
115}
116
117#endif