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