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))); |
| 77 | builder.add(SkLights::Light::MakeAmbient(SkColor3f::Make(0.4f, 0.4f, 0.4f))); |
vjiaoblack | 53da5ba | 2016-08-01 10:02:31 -0700 | [diff] [blame] | 78 | fLights = builder.finish(); |
| 79 | } |
| 80 | |
| 81 | protected: |
| 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)); |
vjiaoblack | 904527d | 2016-08-09 09:32:09 -0700 | [diff] [blame] | 97 | canvas->setLights(fLights); |
| 98 | canvas->drawShadowedPicture(pic, nullptr, nullptr); |
vjiaoblack | 53da5ba | 2016-08-01 10:02:31 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | private: |
| 102 | sk_sp<SkLights> fLights; |
vjiaoblack | 53da5ba | 2016-08-01 10:02:31 -0700 | [diff] [blame] | 103 | typedef GM INHERITED; |
| 104 | }; |
| 105 | |
| 106 | ////////////////////////////////////////////////////////////////////////////// |
| 107 | |
| 108 | DEF_GM(return new ShadowMapsGM;) |
| 109 | } |
| 110 | |
| 111 | #endif |