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 "sk_tool_utils.h" | |
| 9 #include "SampleCode.h" | |
| 10 #include "SkPictureRecorder.h" | |
| 11 #include "SkSurface.h" | |
|
robertphillips
2016/08/04 15:17:35
out of order here
vjiaoblack
2016/08/04 18:03:58
Done.
| |
| 12 #include "SkShadowPaintFilterCanvas.h" | |
| 13 #include "SkShadowShader.h" | |
| 14 | |
| 15 #ifdef SK_EXPERIMENTAL_SHADOWING | |
| 16 | |
| 17 | |
| 18 static sk_sp<SkShader> make_shadow_shader(sk_sp<SkImage> povDepth, | |
| 19 sk_sp<SkImage> diffuse, | |
| 20 sk_sp<SkLights> lights) { | |
| 21 | |
| 22 sk_sp<SkShader> povDepthShader = povDepth->makeShader(SkShader::kClamp_TileM ode, | |
| 23 SkShader::kClamp_TileM ode); | |
| 24 | |
| 25 sk_sp<SkShader> diffuseShader = diffuse->makeShader(SkShader::kClamp_TileMod e, | |
| 26 SkShader::kClamp_TileMod e); | |
| 27 | |
| 28 return SkShadowShader::Make(std::move(povDepthShader), | |
| 29 std::move(diffuseShader), | |
| 30 std::move(lights), | |
| 31 diffuse->width(), diffuse->height()); | |
| 32 } | |
| 33 | |
| 34 | |
| 35 class IndexClick : public SkView::Click { | |
| 36 public: | |
| 37 IndexClick(SkView* v, int index) : INHERITED(v), fIndex(index) {} | |
| 38 | |
|
robertphillips
2016/08/04 15:17:35
We don't seem to use this entry point.
Do we ever
vjiaoblack
2016/08/04 18:03:58
Done.
| |
| 39 static int GetIndex(SkView::Click* click) { | |
| 40 return ((IndexClick*)click)->fIndex; | |
| 41 } | |
| 42 | |
| 43 private: | |
| 44 int fIndex; | |
| 45 typedef SkView::Click INHERITED; | |
| 46 }; | |
| 47 | |
| 48 class ShadowingView : public SampleView { | |
| 49 public: | |
| 50 ShadowingView() { | |
| 51 | |
| 52 this->setBGColor(0xFFCCCCCC); | |
| 53 SkLights::Builder builder; | |
| 54 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.3f, 0.4f), | |
| 55 SkVector3::Make(0.27f, 0.07f, 1.0f))); | |
| 56 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.3f, 0.2f), | |
| 57 SkVector3::Make(0.03f, 0.27f, 1.0f))); | |
| 58 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.4f, 0.4f))); | |
| 59 fLights = builder.finish(); | |
| 60 | |
| 61 fTestRects[0].fColor = 0xFFEE8888; | |
| 62 fTestRects[0].fDepth = 80; | |
| 63 fTestRects[0].fGeometry = SkRect::MakeLTRB(200,150,350,300); | |
| 64 | |
| 65 fTestRects[1].fColor = 0xFF88EE88; | |
| 66 fTestRects[1].fDepth = 160; | |
| 67 fTestRects[1].fGeometry = SkRect::MakeLTRB(150,200,300,350); | |
| 68 | |
| 69 fTestRects[2].fColor = 0xFF8888EE; | |
| 70 fTestRects[2].fDepth = 240; | |
| 71 fTestRects[2].fGeometry = SkRect::MakeLTRB(100,100,250,250); | |
| 72 | |
| 73 fSceneChanged = true; | |
| 74 fLightsChanged = true; | |
| 75 | |
| 76 fSelectedRect = -1; | |
| 77 fMoveLight = false; | |
| 78 } | |
| 79 | |
| 80 protected: | |
| 81 bool onQuery(SkEvent *evt) override { | |
| 82 if (SampleCode::TitleQ(*evt)) { | |
| 83 SampleCode::TitleR(evt, "shadowing"); | |
| 84 return true; | |
| 85 } | |
| 86 | |
| 87 SkUnichar uni; | |
| 88 if (SampleCode::CharQ(*evt, &uni)) { | |
| 89 switch (uni) { | |
| 90 case 'L': | |
| 91 fMoveLight = !fMoveLight; | |
| 92 break; | |
| 93 default: | |
| 94 break; | |
| 95 } | |
| 96 } | |
| 97 return this->INHERITED::onQuery(evt); | |
| 98 } | |
| 99 | |
|
robertphillips
2016/08/04 15:17:35
This is file scope syntax. Either move to file sco
vjiaoblack
2016/08/04 18:03:59
Done.
| |
| 100 static SkISize compute_dmap_size(int maxDepth, const SkLights::Light& light, | |
| 101 int width, int height) { | |
|
robertphillips
2016/08/04 15:17:35
assert it isn't an ambient light?
vjiaoblack
2016/08/04 18:03:58
Done.
| |
| 102 int dMapWidth = SkMin32(maxDepth * fabs(light.dir().fX) + kWidth, | |
| 103 kWidth * 2); | |
| 104 int dMapHeight = SkMin32(maxDepth * fabs(light.dir().fY) + kHeight, | |
| 105 kHeight * 2); | |
| 106 | |
| 107 return SkISize::Make(dMapWidth, dMapHeight); | |
| 108 } | |
| 109 | |
| 110 sk_sp<SkPicture> makeTestPicture(int width, int height) { | |
| 111 SkPictureRecorder recorder; | |
| 112 | |
| 113 // LONG RANGE TODO: eventually add SkBBHFactory (bounding box factory) | |
| 114 SkCanvas* canvas = recorder.beginRecording(SkRect::MakeIWH(width, height )); | |
| 115 | |
| 116 SkASSERT(canvas->getTotalMatrix().isIdentity()); | |
| 117 SkPaint paint; | |
| 118 paint.setColor(SK_ColorGRAY); | |
| 119 | |
| 120 // LONG RANGE TODO: tag occluders | |
| 121 // LONG RANGE TODO: track number of IDs we need (hopefully less than 256 ) | |
| 122 // and determinate the mapping from z to id | |
| 123 | |
| 124 // universal receiver, "ground" | |
| 125 canvas->drawRect(SkRect::MakeIWH(width, height), paint); | |
| 126 | |
| 127 for (int i = 0; i < kNumTestRects; i++) { | |
| 128 paint.setColor(fTestRects[i].fColor); | |
| 129 if (i == 0) { | |
| 130 canvas->translateZ(fTestRects[0].fDepth); | |
| 131 } else { | |
| 132 canvas->translateZ(fTestRects[i].fDepth - fTestRects[i-1].fDepth ); | |
| 133 } | |
| 134 canvas->drawRect(fTestRects[i].fGeometry, paint); | |
| 135 } | |
| 136 | |
| 137 return recorder.finishRecordingAsPicture(); | |
| 138 } | |
| 139 | |
| 140 void updateDepthMaps(SkCanvas *canvas) { | |
| 141 for (int i = 0; i < fLights->numLights(); ++i) { | |
| 142 // skip over ambient lights; they don't cast shadows | |
| 143 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type()) { | |
| 144 continue; | |
| 145 } | |
| 146 | |
| 147 // TODO: maybe add a kDepth_8_SkColorType when vertices have depth | |
| 148 // assume max depth is 255. | |
| 149 // TODO: find actual max depth of picture | |
| 150 SkISize shMapSize = compute_dmap_size(255, fLights->light(i), kWidth , kHeight); | |
| 151 | |
| 152 SkImageInfo info = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHe ight, | |
| 153 kBGRA_8888_SkColorType, | |
| 154 kOpaque_SkAlphaType); | |
| 155 | |
| 156 // Create a new surface (that matches the backend of canvas) | |
| 157 // for each shadow map | |
| 158 sk_sp<SkSurface> surf(canvas->makeSurface(info)); | |
| 159 | |
| 160 // Wrap another SPFCanvas around the surface | |
| 161 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = | |
| 162 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); | |
| 163 | |
| 164 // set the depth map canvas to have the light we're drawing. | |
| 165 SkLights::Builder builder; | |
| 166 builder.add(fLights->light(i)); | |
| 167 sk_sp<SkLights> curLight = builder.finish(); | |
| 168 | |
| 169 depthMapCanvas->setLights(std::move(curLight)); | |
| 170 depthMapCanvas->drawPicture(fPicture); | |
| 171 | |
| 172 fLights->light(i).setShadowMap(surf->makeImageSnapshot()); | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 void updatePovDepthMap(SkCanvas* canvas) { | |
| 177 // TODO: pass the depth to the shader in vertices, or uniforms | |
| 178 // so we don't have to render depth and color separately | |
| 179 | |
| 180 SkLights::Builder builder; | |
| 181 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f), | |
| 182 SkVector3::Make(0.0f, 0.0f, 1.0f))); | |
| 183 sk_sp<SkLights> povLight = builder.finish(); | |
| 184 | |
| 185 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, | |
| 186 kBGRA_8888_SkColorType, | |
| 187 kOpaque_SkAlphaType); | |
| 188 | |
| 189 // Create a new surface (that matches the backend of canvas) | |
| 190 // to create the povDepthMap | |
| 191 sk_sp<SkSurface> surf(canvas->makeSurface(info)); | |
| 192 | |
| 193 // Wrap another SPFCanvas around the surface | |
| 194 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = | |
| 195 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); | |
| 196 | |
| 197 // set the depth map canvas to have the light as the user's POV | |
| 198 depthMapCanvas->setLights(std::move(povLight)); | |
| 199 | |
| 200 depthMapCanvas->drawPicture(fPicture); | |
| 201 | |
| 202 fPovDepthMap = surf->makeImageSnapshot(); | |
| 203 } | |
| 204 | |
| 205 void updateDiffuseMap(SkCanvas* canvas) { | |
| 206 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, | |
| 207 kBGRA_8888_SkColorType, | |
| 208 kOpaque_SkAlphaType); | |
| 209 | |
| 210 sk_sp<SkSurface> surf(canvas->makeSurface(info)); | |
| 211 surf->getCanvas()->drawPicture(fPicture); | |
| 212 | |
| 213 fDiffuseMap = surf->makeImageSnapshot(); | |
| 214 } | |
| 215 | |
| 216 void onDrawContent(SkCanvas *canvas) override { | |
| 217 if (fSceneChanged || !fPovDepthMap) { | |
| 218 fPicture = this->makeTestPicture(kWidth, kHeight); | |
| 219 this->updateDepthMaps(canvas); | |
| 220 this->updatePovDepthMap(canvas); | |
| 221 this->updateDiffuseMap(canvas); | |
| 222 } | |
| 223 | |
| 224 if (fLightsChanged) { | |
| 225 this->updateDepthMaps(canvas); | |
| 226 } | |
| 227 | |
|
robertphillips
2016/08/04 15:17:35
[Optional] It looks like we keep recreating the sh
vjiaoblack
2016/08/04 18:03:58
Done.
vjiaoblack
2016/08/04 18:03:58
Done.
| |
| 228 SkPaint paint; | |
| 229 paint.setShader(make_shadow_shader(fPovDepthMap, | |
| 230 fDiffuseMap, | |
| 231 fLights)); | |
| 232 | |
| 233 canvas->drawRect(SkRect::MakeIWH(kWidth, kHeight), paint); | |
| 234 } | |
| 235 | |
|
robertphillips
2016/08/04 15:17:35
What does the base class' implementation do?
vjiaoblack
2016/08/04 18:03:58
I don't think there's a base class' implementation
vjiaoblack
2016/08/04 18:03:58
Done.
| |
| 236 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) ove rride { | |
| 237 int index = 0; | |
| 238 | |
| 239 return new IndexClick(this, index); | |
| 240 } | |
| 241 | |
| 242 bool onClick(Click *click) override { | |
| 243 SkScalar x = click->fCurr.fX; | |
| 244 SkScalar y = click->fCurr.fY; | |
| 245 | |
| 246 SkScalar dx = x - click->fPrev.fX; | |
| 247 SkScalar dy = y - click->fPrev.fY; | |
| 248 | |
| 249 if (fMoveLight) { | |
|
robertphillips
2016/08/04 15:17:35
Shouldn't this be an || ?
vjiaoblack
2016/08/04 18:03:58
Done.
| |
| 250 if (dx != 0 && dy != 0) { | |
| 251 float recipX = 1.0f / kWidth; | |
| 252 float recipY = 1.0f / kHeight; | |
| 253 | |
| 254 SkLights::Builder builder; | |
| 255 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.3f, 0.4f), | |
| 256 SkVector3::Make(0.12f + (200.0f - x) * recipX, | |
| 257 -0.08f + (200.0f - y ) * recipY, | |
| 258 1.0f))); | |
| 259 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.3f, 0.2f), | |
| 260 SkVector3::Make(-0.12f + (200.0f - x ) * recipX, | |
| 261 0.12f + (200.0f - y) * recipY, | |
| 262 1.0f))); | |
| 263 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.4f, 0.4f))); | |
| 264 fLights = builder.finish(); | |
| 265 | |
| 266 fLightsChanged = true; | |
| 267 } | |
| 268 | |
| 269 return true; | |
| 270 } | |
| 271 | |
| 272 if (click->fState == Click::State::kUp_State) { | |
| 273 fSelectedRect = -1; | |
| 274 return true; | |
| 275 } | |
| 276 | |
| 277 if (fSelectedRect > -1) { | |
|
robertphillips
2016/08/04 15:17:35
Didn't we already store off/compute these above ?
vjiaoblack
2016/08/04 18:03:58
Done.
| |
| 278 SkScalar x = click->fCurr.fX; | |
| 279 SkScalar y = click->fCurr.fY; | |
| 280 | |
| 281 SkScalar dx = x - click->fPrev.fX; | |
| 282 SkScalar dy = y - click->fPrev.fY; | |
| 283 | |
|
robertphillips
2016/08/04 15:17:35
fTestRects[fSelectedRect].offset(dx, dy); ?
vjiaoblack
2016/08/04 18:03:58
Done.
| |
| 284 fTestRects[fSelectedRect].fGeometry.fLeft += dx; | |
| 285 fTestRects[fSelectedRect].fGeometry.fRight += dx; | |
| 286 fTestRects[fSelectedRect].fGeometry.fTop += dy; | |
| 287 fTestRects[fSelectedRect].fGeometry.fBottom += dy; | |
| 288 | |
| 289 fSceneChanged = true; | |
| 290 | |
| 291 return true; | |
| 292 } | |
| 293 | |
| 294 // assume last elements are highest | |
| 295 for (int i = kNumTestRects - 1; i >= 0; i--) { | |
| 296 if (fTestRects[i].fGeometry.contains(SkRect::MakeXYWH(x, y, 1, 1))) { | |
| 297 fSelectedRect = i; | |
|
robertphillips
2016/08/04 15:17:35
fTestRects[i].offset(dx, dy); ?
vjiaoblack
2016/08/04 18:03:58
Done.
| |
| 298 fTestRects[i].fGeometry.fLeft += dx; | |
| 299 fTestRects[i].fGeometry.fRight += dx; | |
| 300 fTestRects[i].fGeometry.fTop += dy; | |
| 301 fTestRects[i].fGeometry.fBottom += dy; | |
| 302 | |
| 303 fSceneChanged = true; | |
| 304 | |
| 305 break; | |
| 306 } | |
| 307 } | |
| 308 | |
| 309 return true; | |
| 310 } | |
| 311 | |
| 312 private: | |
| 313 static constexpr int kNumTestRects = 3; | |
| 314 | |
| 315 static const int kWidth = 400; | |
| 316 static const int kHeight = 400; | |
| 317 | |
| 318 struct { | |
| 319 SkRect fGeometry; | |
| 320 int fDepth; | |
| 321 SkColor fColor; | |
| 322 } fTestRects[kNumTestRects]; | |
| 323 | |
| 324 int fSelectedRect; | |
| 325 bool fMoveLight; | |
| 326 | |
| 327 sk_sp<SkImage> fPovDepthMap; | |
| 328 sk_sp<SkImage> fDiffuseMap; | |
| 329 sk_sp<SkPicture> fPicture; | |
| 330 | |
| 331 bool fSceneChanged; | |
| 332 bool fLightsChanged; | |
| 333 | |
| 334 sk_sp<SkLights> fLights; | |
| 335 | |
| 336 typedef SampleView INHERITED; | |
| 337 }; | |
| 338 | |
| 339 ////////////////////////////////////////////////////////////////////////////// | |
| 340 static SkView* MyFactory() { return new ShadowingView; } | |
| 341 static SkViewRegister reg(MyFactory); | |
| 342 | |
| 343 #endif | |
| OLD | NEW |