Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 "SkDrawFilter.h" | |
| 10 #include "SkLights.h" | |
| 11 #include "SkLightingShader.h" | |
| 12 #include "SkPictureRecorder.h" | |
| 13 #include "SkSurface.h" | |
| 14 | |
| 15 | |
| 16 sk_sp<SkShader> make_lighting_shader(const SkMatrix& ctm, sk_sp<SkLights> lights ) { | |
| 17 // TODO: Victor figures this out | |
| 18 #if 0 | |
| 19 // TODO: correctly pull out the pure rotation | |
| 20 SkVector invNormRotation = { ctm[SkMatrix::kMScaleX], ctm[SkMatrix::kMSkewY] }; | |
| 21 | |
| 22 return SkLightingShader::Make(nullptr, | |
| 23 nullptr, | |
| 24 std::move(lights), | |
| 25 invNormRotation, | |
| 26 nullptr, | |
| 27 nullptr); | |
| 28 #else | |
| 29 return nullptr; | |
| 30 #endif | |
| 31 } | |
| 32 | |
| 33 sk_sp<SkPicture> make_picture(int width, int height, sk_sp<SkLights> lights) { | |
| 34 SkPictureRecorder recorder; | |
| 35 | |
| 36 // eventually add SkBBHFactory (bounding box factory) | |
| 37 SkCanvas* canvas = recorder.beginRecording(SkRect::MakeIWH(width, height)); | |
| 38 | |
| 39 SkASSERT(canvas->getTotalMatrix().isIdentity()); | |
| 40 sk_sp<SkShader> identityLights(make_lighting_shader(canvas->getTotalMatrix() , lights)); | |
| 41 SkPaint paint; | |
| 42 paint.setShader(identityLights); | |
| 43 paint.setColor(SK_ColorWHITE); | |
| 44 | |
| 45 // LONG RANGE TODO: tag occluders | |
| 46 // LONG RANGE TODO: track number of IDs we need (hopefully less than 256) | |
| 47 // and determinate the mapping from z to id | |
| 48 | |
| 49 // universal receiver, "ground" | |
| 50 canvas->drawRect(SkRect::MakeIWH(width, height), paint); | |
| 51 | |
| 52 // Maybe add the ID here along with the depth | |
|
robertphillips
2016/07/01 15:17:54
Note that you're also going to need to add a SkCan
| |
| 53 // canvas->setZ(10); | |
| 54 | |
| 55 canvas->save(); | |
| 56 canvas->translate(50, 50); | |
| 57 canvas->clipRect(SkRect::MakeLTRB(475, 475, 525, 525)); | |
| 58 | |
| 59 // Add an assert that Z order matches painter's order | |
| 60 /// | |
| 61 // canvas->setZ(20); | |
| 62 | |
| 63 SkASSERT(!canvas->getTotalMatrix().isIdentity()); | |
| 64 paint.setShader(make_lighting_shader(canvas->getTotalMatrix(), lights)); | |
| 65 paint.setColor(SK_ColorBLUE); | |
| 66 | |
| 67 // use an IDE | |
| 68 canvas->drawRect(SkRect::MakeLTRB(0, 0, 1000, 1000), paint); | |
| 69 canvas->restore(); | |
| 70 | |
| 71 // TODO: think about if the Z-order always matching painting order is too st rict | |
| 72 SkASSERT(canvas->getTotalMatrix().isIdentity()); | |
| 73 paint.setShader(identityLights); | |
| 74 paint.setColor(SK_ColorRED); | |
| 75 canvas->drawRect(SkRect::MakeLTRB(700, 700, 710, 710), paint); | |
| 76 | |
| 77 return recorder.finishRecordingAsPicture(); | |
| 78 } | |
| 79 | |
| 80 namespace skiagm { | |
| 81 | |
|
robertphillips
2016/07/01 15:17:54
Thinking about this a bit more I think we should u
jvanverth1
2016/07/01 15:31:04
After talking to Robert about this, when we create
| |
| 82 class SkFoo : public SkDrawFilter { | |
| 83 public: | |
| 84 SkFoo(/*SkTDynamicHash<int, int> *hash*/) { | |
| 85 | |
| 86 } | |
| 87 | |
| 88 bool filter(SkPaint* paint, Type) override { | |
| 89 SkPaint newPaint; | |
| 90 | |
| 91 SkColor color = 0xFF000000; | |
| 92 | |
| 93 color |= 0x000000AA; // Put the index into the blue component | |
| 94 newPaint.setColor(color); | |
| 95 // Map the individual draw to a specific index | |
| 96 *paint = newPaint; | |
| 97 return true; | |
| 98 } | |
| 99 }; | |
| 100 | |
| 101 // This GM ... | |
| 102 class Shadows1GM : public GM { | |
| 103 public: | |
| 104 Shadows1GM() { | |
| 105 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); | |
| 106 | |
| 107 SkLights::Builder builder; | |
| 108 | |
| 109 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f), | |
| 110 SkVector3::Make(1.0f, 0.0f, 0.0f))); | |
| 111 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.2f, 0.2f))); | |
| 112 | |
| 113 fLights = builder.finish(); | |
| 114 } | |
| 115 | |
| 116 protected: | |
| 117 static const int kWidth = 1024; | |
| 118 static const int kHeight = 1024; | |
| 119 | |
| 120 | |
| 121 SkString onShortName() override { | |
| 122 return SkString("victor-shadows"); | |
| 123 } | |
| 124 | |
| 125 SkISize onISize() override { | |
| 126 return SkISize::Make(kWidth, kHeight); | |
| 127 } | |
| 128 | |
| 129 void onDraw(SkCanvas* canvas) override { | |
| 130 | |
| 131 sk_sp<SkPicture> pic(make_picture(kWidth, kHeight, fLights)); | |
| 132 | |
| 133 sk_sp<SkDrawFilter> filter(new SkFoo()); | |
| 134 | |
| 135 sk_sp<SkImage> img; | |
| 136 for (int i = 0; i < fLights->numLights(); ++i) { | |
| 137 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type()) { | |
| 138 continue; | |
| 139 } | |
| 140 | |
| 141 // generate shadow map | |
| 142 | |
| 143 // TODO: compute the correct size of the shadow map from the light p roperties | |
| 144 // TODO: maybe add a kDepth_8_SkColorType | |
| 145 // TODO: change to kIndex_8_SkColorType | |
|
robertphillips
2016/07/01 15:17:55
We could probably start off with this being kAlpha
| |
| 146 SkImageInfo info = SkImageInfo::Make(256, 256, kBGRA_8888_SkColorTyp e, kOpaque_SkAlphaType); | |
| 147 | |
| 148 sk_sp<SkSurface> surf(canvas->makeSurface(info)); | |
| 149 SkCanvas* shadowMapCanvas = surf->getCanvas(); | |
| 150 | |
| 151 // TODO: add a matrix to map the scene to the viewpoint of the light | |
| 152 fLights->light(i); | |
| 153 | |
| 154 SkMatrix mat; | |
| 155 mat.setRectToRect(SkRect::MakeIWH(kWidth, kHeight), SkRect::MakeIWH( 256, 256), | |
| 156 SkMatrix::kFill_ScaleToFit); | |
| 157 | |
| 158 shadowMapCanvas->setMatrix(mat); | |
| 159 shadowMapCanvas->setDrawFilter(filter.get()); | |
| 160 shadowMapCanvas->drawPicture(pic); | |
| 161 | |
| 162 img = surf->makeImageSnapshot(); | |
| 163 | |
| 164 // TODO: Add something like this | |
| 165 fLights->light(i).setShadowMap(img); | |
| 166 } | |
| 167 | |
| 168 // perform final pass | |
| 169 canvas->drawPicture(std::move(pic)); | |
| 170 //canvas->drawImage(img, 0, 0); | |
| 171 } | |
| 172 | |
| 173 private: | |
| 174 sk_sp<SkLights> fLights; | |
| 175 | |
| 176 typedef GM INHERITED; | |
| 177 }; | |
| 178 | |
| 179 ////////////////////////////////////////////////////////////////////////////// | |
| 180 | |
| 181 DEF_GM(return new Shadows1GM;) | |
| 182 } | |
| OLD | NEW |