| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SampleCode.h" | 8 #include "SampleCode.h" |
| 9 #include "SkPictureRecorder.h" | 9 #include "SkPictureRecorder.h" |
| 10 #include "SkShadowPaintFilterCanvas.h" | 10 #include "SkShadowPaintFilterCanvas.h" |
| 11 #include "SkShadowShader.h" | 11 #include "SkShadowShader.h" |
| 12 #include "SkSurface.h" | 12 #include "SkSurface.h" |
| 13 | 13 |
| 14 #ifdef SK_EXPERIMENTAL_SHADOWING | 14 #ifdef SK_EXPERIMENTAL_SHADOWING |
| 15 | 15 |
| 16 static sk_sp<SkShader> make_shadow_shader(sk_sp<SkImage> povDepth, | |
| 17 sk_sp<SkImage> diffuse, | |
| 18 sk_sp<SkLights> lights) { | |
| 19 | |
| 20 sk_sp<SkShader> povDepthShader = povDepth->makeShader(SkShader::kClamp_TileM
ode, | |
| 21 SkShader::kClamp_TileM
ode); | |
| 22 | |
| 23 sk_sp<SkShader> diffuseShader = diffuse->makeShader(SkShader::kClamp_TileMod
e, | |
| 24 SkShader::kClamp_TileMod
e); | |
| 25 | |
| 26 return SkShadowShader::Make(std::move(povDepthShader), | |
| 27 std::move(diffuseShader), | |
| 28 std::move(lights), | |
| 29 diffuse->width(), diffuse->height()); | |
| 30 } | |
| 31 | |
| 32 class ShadowingView : public SampleView { | 16 class ShadowingView : public SampleView { |
| 33 public: | 17 public: |
| 34 ShadowingView() { | 18 ShadowingView() { |
| 35 | 19 |
| 36 this->setBGColor(0xFFCCCCCC); | 20 this->setBGColor(0xFFCCCCCC); |
| 37 SkLights::Builder builder; | 21 SkLights::Builder builder; |
| 38 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.3f, 0.4f), | 22 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.3f, 0.4f), |
| 39 SkVector3::Make(0.27f, 0.07f, 1.0f))); | 23 SkVector3::Make(0.2f, 0.05f, 1.0f))); |
| 40 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.3f, 0.2f), | 24 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.3f, 0.2f), |
| 41 SkVector3::Make(0.03f, 0.27f, 1.0f))); | 25 SkVector3::Make(0.05f, 0.2f, 1.0f))); |
| 42 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.4f, 0.4f))); | 26 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.4f, 0.4f))); |
| 43 fLights = builder.finish(); | 27 fLights = builder.finish(); |
| 44 | 28 |
| 45 fTestRects[0].fColor = 0xFFEE8888; | 29 fTestRects[0].fColor = 0xFFEE8888; |
| 46 fTestRects[0].fDepth = 80; | 30 fTestRects[0].fDepth = 80; |
| 47 fTestRects[0].fGeometry = SkRect::MakeLTRB(200,150,350,300); | 31 fTestRects[0].fGeometry = SkRect::MakeLTRB(200,150,350,300); |
| 48 | 32 |
| 49 fTestRects[1].fColor = 0xFF88EE88; | 33 fTestRects[1].fColor = 0xFF88EE88; |
| 50 fTestRects[1].fDepth = 160; | 34 fTestRects[1].fDepth = 160; |
| 51 fTestRects[1].fGeometry = SkRect::MakeLTRB(150,200,300,350); | 35 fTestRects[1].fGeometry = SkRect::MakeLTRB(150,200,300,350); |
| 52 | 36 |
| 53 fTestRects[2].fColor = 0xFF8888EE; | 37 fTestRects[2].fColor = 0xFF8888EE; |
| 54 fTestRects[2].fDepth = 240; | 38 fTestRects[2].fDepth = 240; |
| 55 fTestRects[2].fGeometry = SkRect::MakeLTRB(100,100,250,250); | 39 fTestRects[2].fGeometry = SkRect::MakeLTRB(100,100,250,250); |
| 56 | 40 |
| 57 fSceneChanged = true; | 41 fSceneChanged = true; |
| 58 fLightsChanged = true; | 42 fLightsChanged = true; |
| 59 | 43 |
| 60 fSelectedRect = -1; | 44 fSelectedRect = -1; |
| 61 fMoveLight = false; | 45 fMoveLight = false; |
| 46 |
| 47 fClearShadowMaps = false; |
| 62 } | 48 } |
| 63 | 49 |
| 64 protected: | 50 protected: |
| 65 bool onQuery(SkEvent *evt) override { | 51 bool onQuery(SkEvent *evt) override { |
| 66 if (SampleCode::TitleQ(*evt)) { | 52 if (SampleCode::TitleQ(*evt)) { |
| 67 SampleCode::TitleR(evt, "shadowing"); | 53 SampleCode::TitleR(evt, "shadowing"); |
| 68 return true; | 54 return true; |
| 69 } | 55 } |
| 70 | 56 |
| 71 SkUnichar uni; | 57 SkUnichar uni; |
| 72 if (SampleCode::CharQ(*evt, &uni)) { | 58 if (SampleCode::CharQ(*evt, &uni)) { |
| 73 switch (uni) { | 59 switch (uni) { |
| 74 case 'L': | 60 case 'L': |
| 75 fMoveLight = !fMoveLight; | 61 fMoveLight = !fMoveLight; |
| 76 break; | 62 break; |
| 63 case 'd': |
| 64 // Raster generated shadow maps have their origin in the UL
corner |
| 65 // GPU shadow maps can have an arbitrary origin. |
| 66 // We override the 'd' keypress so that when the device is c
ycled, |
| 67 // the shadow maps will be re-generated according to the new
backend. |
| 68 fClearShadowMaps = true; |
| 69 break; |
| 77 default: | 70 default: |
| 78 break; | 71 break; |
| 79 } | 72 } |
| 80 } | 73 } |
| 81 return this->INHERITED::onQuery(evt); | 74 return this->INHERITED::onQuery(evt); |
| 82 } | 75 } |
| 83 | 76 |
| 84 sk_sp<SkPicture> makeTestPicture(int width, int height) { | 77 sk_sp<SkPicture> makeTestPicture(int width, int height) { |
| 85 SkPictureRecorder recorder; | 78 SkPictureRecorder recorder; |
| 86 | 79 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 104 canvas->translateZ(fTestRects[0].fDepth); | 97 canvas->translateZ(fTestRects[0].fDepth); |
| 105 } else { | 98 } else { |
| 106 canvas->translateZ(fTestRects[i].fDepth - fTestRects[i-1].fDepth
); | 99 canvas->translateZ(fTestRects[i].fDepth - fTestRects[i-1].fDepth
); |
| 107 } | 100 } |
| 108 canvas->drawRect(fTestRects[i].fGeometry, paint); | 101 canvas->drawRect(fTestRects[i].fGeometry, paint); |
| 109 } | 102 } |
| 110 | 103 |
| 111 return recorder.finishRecordingAsPicture(); | 104 return recorder.finishRecordingAsPicture(); |
| 112 } | 105 } |
| 113 | 106 |
| 114 void updateDepthMaps(SkCanvas *canvas) { | 107 void onDrawContent(SkCanvas *canvas) override { |
| 115 for (int i = 0; i < fLights->numLights(); ++i) { | 108 if (fSceneChanged) { |
| 116 // skip over ambient lights; they don't cast shadows | 109 fPicture = this->makeTestPicture(kWidth, kHeight); |
| 117 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type())
{ | 110 } |
| 118 continue; | 111 |
| 112 if (fSceneChanged || fLightsChanged || fClearShadowMaps) { |
| 113 for (int i = 0; i < fLights->numLights(); i++) { |
| 114 fLights->light(i).setShadowMap(nullptr); |
| 119 } | 115 } |
| 116 fSceneChanged = false; |
| 117 fLightsChanged = false; |
| 118 fClearShadowMaps = false; |
| 120 | 119 |
| 121 // TODO: maybe add a kDepth_8_SkColorType when vertices have depth | 120 canvas->setLights(fLights); |
| 122 // assume max depth is 255. | 121 canvas->drawShadowedPicture(fPicture, nullptr, nullptr); |
| 123 // TODO: find actual max depth of picture | |
| 124 SkISize shMapSize = SkShadowPaintFilterCanvas:: | |
| 125 ComputeDepthMapSize(fLights->light(i), 255, kWid
th, kHeight); | |
| 126 | |
| 127 SkImageInfo info = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHe
ight, | |
| 128 kBGRA_8888_SkColorType, | |
| 129 kOpaque_SkAlphaType); | |
| 130 | |
| 131 // Create a new surface (that matches the backend of canvas) | |
| 132 // for each shadow map | |
| 133 sk_sp<SkSurface> surf(canvas->makeSurface(info)); | |
| 134 | |
| 135 // Wrap another SPFCanvas around the surface | |
| 136 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = | |
| 137 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); | |
| 138 | |
| 139 // set the depth map canvas to have the light we're drawing. | |
| 140 SkLights::Builder builder; | |
| 141 builder.add(fLights->light(i)); | |
| 142 sk_sp<SkLights> curLight = builder.finish(); | |
| 143 | |
| 144 depthMapCanvas->setLights(std::move(curLight)); | |
| 145 depthMapCanvas->drawPicture(fPicture); | |
| 146 | |
| 147 fLights->light(i).setShadowMap(surf->makeImageSnapshot()); | |
| 148 } | 122 } |
| 149 } | 123 } |
| 150 | 124 |
| 151 void updatePovDepthMap(SkCanvas* canvas) { | |
| 152 // TODO: pass the depth to the shader in vertices, or uniforms | |
| 153 // so we don't have to render depth and color separately | |
| 154 | |
| 155 SkLights::Builder builder; | |
| 156 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f), | |
| 157 SkVector3::Make(0.0f, 0.0f, 1.0f))); | |
| 158 sk_sp<SkLights> povLight = builder.finish(); | |
| 159 | |
| 160 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, | |
| 161 kBGRA_8888_SkColorType, | |
| 162 kOpaque_SkAlphaType); | |
| 163 | |
| 164 // Create a new surface (that matches the backend of canvas) | |
| 165 // to create the povDepthMap | |
| 166 sk_sp<SkSurface> surf(canvas->makeSurface(info)); | |
| 167 | |
| 168 // Wrap another SPFCanvas around the surface | |
| 169 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = | |
| 170 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); | |
| 171 | |
| 172 // set the depth map canvas to have the light as the user's POV | |
| 173 depthMapCanvas->setLights(std::move(povLight)); | |
| 174 | |
| 175 depthMapCanvas->drawPicture(fPicture); | |
| 176 | |
| 177 fPovDepthMap = surf->makeImageSnapshot(); | |
| 178 } | |
| 179 | |
| 180 void updateDiffuseMap(SkCanvas* canvas) { | |
| 181 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, | |
| 182 kBGRA_8888_SkColorType, | |
| 183 kOpaque_SkAlphaType); | |
| 184 | |
| 185 sk_sp<SkSurface> surf(canvas->makeSurface(info)); | |
| 186 surf->getCanvas()->drawPicture(fPicture); | |
| 187 | |
| 188 fDiffuseMap = surf->makeImageSnapshot(); | |
| 189 } | |
| 190 | |
| 191 void onDrawContent(SkCanvas *canvas) override { | |
| 192 if (fSceneChanged || !fPovDepthMap) { | |
| 193 fPicture = this->makeTestPicture(kWidth, kHeight); | |
| 194 this->updateDepthMaps(canvas); | |
| 195 this->updatePovDepthMap(canvas); | |
| 196 this->updateDiffuseMap(canvas); | |
| 197 } | |
| 198 | |
| 199 if (fLightsChanged) { | |
| 200 this->updateDepthMaps(canvas); | |
| 201 } | |
| 202 | |
| 203 if (fSceneChanged || fLightsChanged || !fShadowShader) { | |
| 204 fShadowShader = make_shadow_shader(fPovDepthMap, fDiffuseMap, fLight
s); | |
| 205 } | |
| 206 | |
| 207 SkPaint paint; | |
| 208 paint.setShader(fShadowShader); | |
| 209 | |
| 210 canvas->drawRect(SkRect::MakeIWH(kWidth, kHeight), paint); | |
| 211 } | |
| 212 | |
| 213 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove
rride { | 125 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove
rride { |
| 214 return new SkView::Click(this); | 126 return new SkView::Click(this); |
| 215 } | 127 } |
| 216 | 128 |
| 217 bool onClick(Click *click) override { | 129 bool onClick(Click *click) override { |
| 218 SkScalar x = click->fCurr.fX; | 130 SkScalar x = click->fCurr.fX; |
| 219 SkScalar y = click->fCurr.fY; | 131 SkScalar y = click->fCurr.fY; |
| 220 | 132 |
| 221 SkScalar dx = x - click->fPrev.fX; | 133 SkScalar dx = x - click->fPrev.fX; |
| 222 SkScalar dy = y - click->fPrev.fY; | 134 SkScalar dy = y - click->fPrev.fY; |
| 223 | 135 |
| 224 if (fMoveLight) { | 136 if (fMoveLight) { |
| 225 if (dx != 0 || dy != 0) { | 137 if (dx != 0 || dy != 0) { |
| 226 float recipX = 1.0f / kWidth; | 138 float recipX = 1.0f / kWidth; |
| 227 float recipY = 1.0f / kHeight; | 139 float recipY = 1.0f / kHeight; |
| 228 | 140 |
| 229 SkLights::Builder builder; | 141 SkLights::Builder builder; |
| 230 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.3f, 0.4f), | 142 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.3f, 0.4f), |
| 231 SkVector3::Make(0.12f + (200.0f - x)
* recipX, | 143 SkVector3::Make(0.2f + (200.0f - x)
* recipX, |
| 232 -0.08f + (200.0f - y
) * recipY, | 144 0.05f + (200.0f - y)
* recipY, |
| 233 1.0f))); | 145 1.0f))); |
| 234 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.3f, 0.2f), | 146 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.3f, 0.2f), |
| 235 SkVector3::Make(-0.12f + (200.0f - x
) * recipX, | 147 SkVector3::Make(0.05f + (200.0f - x)
* recipX, |
| 236 0.12f + (200.0f - y)
* recipY, | 148 0.2f + (200.0f - y)
* recipY, |
| 237 1.0f))); | 149 1.0f))); |
| 238 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.4f, 0.4f))); | 150 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.4f, 0.4f))); |
| 239 fLights = builder.finish(); | 151 fLights = builder.finish(); |
| 240 | 152 |
| 241 fLightsChanged = true; | 153 fLightsChanged = true; |
| 242 this->inval(nullptr); | 154 this->inval(nullptr); |
| 243 } | 155 } |
| 244 return true; | 156 return true; |
| 245 } | 157 } |
| 246 | 158 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 270 } | 182 } |
| 271 | 183 |
| 272 return true; | 184 return true; |
| 273 } | 185 } |
| 274 | 186 |
| 275 private: | 187 private: |
| 276 static constexpr int kNumTestRects = 3; | 188 static constexpr int kNumTestRects = 3; |
| 277 | 189 |
| 278 static const int kWidth = 400; | 190 static const int kWidth = 400; |
| 279 static const int kHeight = 400; | 191 static const int kHeight = 400; |
| 192 bool fClearShadowMaps; |
| 280 | 193 |
| 281 struct { | 194 struct { |
| 282 SkRect fGeometry; | 195 SkRect fGeometry; |
| 283 int fDepth; | 196 int fDepth; |
| 284 SkColor fColor; | 197 SkColor fColor; |
| 285 } fTestRects[kNumTestRects]; | 198 } fTestRects[kNumTestRects]; |
| 286 | 199 |
| 287 int fSelectedRect; | 200 int fSelectedRect; |
| 288 bool fMoveLight; | 201 bool fMoveLight; |
| 289 | 202 |
| 290 sk_sp<SkImage> fPovDepthMap; | |
| 291 sk_sp<SkImage> fDiffuseMap; | |
| 292 sk_sp<SkPicture> fPicture; | 203 sk_sp<SkPicture> fPicture; |
| 293 sk_sp<SkShader> fShadowShader; | |
| 294 | 204 |
| 295 bool fSceneChanged; | 205 bool fSceneChanged; |
| 296 bool fLightsChanged; | 206 bool fLightsChanged; |
| 297 | 207 |
| 298 sk_sp<SkLights> fLights; | 208 sk_sp<SkLights> fLights; |
| 299 | 209 |
| 300 typedef SampleView INHERITED; | 210 typedef SampleView INHERITED; |
| 301 }; | 211 }; |
| 302 | 212 |
| 303 ////////////////////////////////////////////////////////////////////////////// | 213 ////////////////////////////////////////////////////////////////////////////// |
| 304 static SkView* MyFactory() { return new ShadowingView; } | 214 static SkView* MyFactory() { return new ShadowingView; } |
| 305 static SkViewRegister reg(MyFactory); | 215 static SkViewRegister reg(MyFactory); |
| 306 | 216 |
| 307 #endif | 217 #endif |
| OLD | NEW |