vjiaoblack | 53da5ba | 2016-08-01 10:02:31 -0700 | [diff] [blame] | 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" |
vjiaoblack | 53da5ba | 2016-08-01 10:02:31 -0700 | [diff] [blame] | 10 | #include "SkPathEffect.h" |
| 11 | #include "SkPictureRecorder.h" |
vjiaoblack | 955e879 | 2016-08-05 07:55:01 -0700 | [diff] [blame] | 12 | #include "SkShadowPaintFilterCanvas.h" |
vjiaoblack | 53da5ba | 2016-08-01 10:02:31 -0700 | [diff] [blame] | 13 | #include "SkShadowShader.h" |
| 14 | #include "SkSurface.h" |
| 15 | |
| 16 | #ifdef SK_EXPERIMENTAL_SHADOWING |
| 17 | |
vjiaoblack | 53da5ba | 2016-08-01 10:02:31 -0700 | [diff] [blame] | 18 | |
| 19 | static 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 | |
| 58 | namespace skiagm { |
| 59 | |
vjiaoblack | 53da5ba | 2016-08-01 10:02:31 -0700 | [diff] [blame] | 60 | class ShadowMapsGM : public GM { |
| 61 | public: |
| 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; |
vjiaoblack | 772b5ee | 2016-08-12 11:38:47 -0700 | [diff] [blame] | 73 | 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))); |
vjiaoblack | a8eabc4 | 2016-08-29 10:22:09 -0700 | [diff] [blame] | 77 | builder.setAmbientLightColor(SkColor3f::Make(0.4f, 0.4f, 0.4f)); |
vjiaoblack | 53da5ba | 2016-08-01 10:02:31 -0700 | [diff] [blame] | 78 | fLights = builder.finish(); |
vjiaoblack | e6f5d56 | 2016-08-25 06:30:23 -0700 | [diff] [blame] | 79 | |
| 80 | fShadowParams.fShadowRadius = 4.0f; |
| 81 | fShadowParams.fBiasingConstant = 0.3f; |
| 82 | fShadowParams.fMinVariance = 1024; |
| 83 | fShadowParams.fType = SkShadowParams::kVariance_ShadowType; |
vjiaoblack | 53da5ba | 2016-08-01 10:02:31 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | protected: |
vjiaoblack | b2796fd | 2016-09-09 09:22:39 -0700 | [diff] [blame] | 87 | static constexpr int kWidth = 400; |
| 88 | static constexpr int kHeight = 400; |
vjiaoblack | 53da5ba | 2016-08-01 10:02:31 -0700 | [diff] [blame] | 89 | |
| 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)); |
vjiaoblack | 904527d | 2016-08-09 09:32:09 -0700 | [diff] [blame] | 102 | canvas->setLights(fLights); |
vjiaoblack | e6f5d56 | 2016-08-25 06:30:23 -0700 | [diff] [blame] | 103 | canvas->drawShadowedPicture(pic, nullptr, nullptr, fShadowParams); |
vjiaoblack | 53da5ba | 2016-08-01 10:02:31 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | private: |
| 107 | sk_sp<SkLights> fLights; |
vjiaoblack | e6f5d56 | 2016-08-25 06:30:23 -0700 | [diff] [blame] | 108 | SkShadowParams fShadowParams; |
vjiaoblack | 53da5ba | 2016-08-01 10:02:31 -0700 | [diff] [blame] | 109 | typedef GM INHERITED; |
| 110 | }; |
| 111 | |
| 112 | ////////////////////////////////////////////////////////////////////////////// |
| 113 | |
| 114 | DEF_GM(return new ShadowMapsGM;) |
| 115 | } |
| 116 | |
| 117 | #endif |