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 |
| 9 #include "gm.h" |
| 10 #include "SkPaintFilterCanvas.h" |
| 11 #include "SkPathEffect.h" |
| 12 #include "SkPictureRecorder.h" |
| 13 #include "SkShadowShader.h" |
| 14 #include "SkSurface.h" |
| 15 |
| 16 #ifdef SK_EXPERIMENTAL_SHADOWING |
| 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 } |
| 32 |
| 33 static sk_sp<SkPicture> make_test_picture(int width, int height) { |
| 34 SkPictureRecorder recorder; |
| 35 |
| 36 // LONG RANGE TODO: eventually add SkBBHFactory (bounding box factory) |
| 37 SkCanvas* canvas = recorder.beginRecording(SkRect::MakeIWH(width, height)); |
| 38 |
| 39 SkASSERT(canvas->getTotalMatrix().isIdentity()); |
| 40 SkPaint paint; |
| 41 paint.setColor(SK_ColorGRAY); |
| 42 |
| 43 // LONG RANGE TODO: tag occluders |
| 44 // LONG RANGE TODO: track number of IDs we need (hopefully less than 256) |
| 45 // and determinate the mapping from z to id |
| 46 |
| 47 // universal receiver, "ground" |
| 48 canvas->drawRect(SkRect::MakeIWH(width, height), paint); |
| 49 |
| 50 // TODO: Maybe add the ID here along with the depth |
| 51 |
| 52 paint.setColor(0xFFEE8888); |
| 53 |
| 54 canvas->translateZ(80); |
| 55 canvas->drawRect(SkRect::MakeLTRB(200,150,350,300), paint); |
| 56 |
| 57 paint.setColor(0xFF88EE88); |
| 58 |
| 59 canvas->translateZ(80); |
| 60 canvas->drawRect(SkRect::MakeLTRB(150,200,300,350), paint); |
| 61 |
| 62 paint.setColor(0xFF8888EE); |
| 63 |
| 64 canvas->translateZ(80); |
| 65 canvas->drawRect(SkRect::MakeLTRB(100,100,250,250), paint); |
| 66 // TODO: Add an assert that Z order matches painter's order |
| 67 // TODO: think about if the Z-order always matching painting order is too st
rict |
| 68 |
| 69 return recorder.finishRecordingAsPicture(); |
| 70 } |
| 71 |
| 72 namespace skiagm { |
| 73 |
| 74 /* We override the onFilter method to draw depths into the canvas |
| 75 * depending on the current draw depth of the canvas, throwing out |
| 76 * the actual draw color. |
| 77 */ |
| 78 class SkShadowPaintFilterCanvas : public SkPaintFilterCanvas { |
| 79 public: |
| 80 |
| 81 SkShadowPaintFilterCanvas(SkCanvas* canvas) : INHERITED(canvas) { } |
| 82 |
| 83 // TODO use a shader instead |
| 84 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type type) const override
{ |
| 85 if (*paint) { |
| 86 int z = this->getZ(); |
| 87 SkASSERT(z <= 0xFF && z >= 0x00); |
| 88 |
| 89 SkPaint newPaint; |
| 90 newPaint.setPathEffect(sk_ref_sp<SkPathEffect>((*paint)->getPathEffe
ct())); |
| 91 |
| 92 SkColor color = 0xFF000000; // init color to opaque black |
| 93 color |= z; // Put the index into the blue component |
| 94 newPaint.setColor(color); |
| 95 |
| 96 *paint->writable() = newPaint; |
| 97 } |
| 98 |
| 99 return true; |
| 100 } |
| 101 |
| 102 void onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, const S
kPaint* paint) { |
| 103 SkTCopyOnFirstWrite<SkPaint> filteredPaint(paint); |
| 104 if (this->onFilter(&filteredPaint, kPicture_Type)) { |
| 105 // we directly call SkCanvas's onDrawPicture because calling the one |
| 106 // that INHERITED has (SkPaintFilterCanvas) leads to wrong behavior |
| 107 this->SkCanvas::onDrawPicture(picture, matrix, filteredPaint); |
| 108 } |
| 109 } |
| 110 |
| 111 void updateMatrix() { |
| 112 this->save(); |
| 113 |
| 114 // When we use the SkShadowPaintFilterCanvas, we can only render |
| 115 // one depth map at a time. Thus, we leave it up to the user to |
| 116 // set SkLights to only contain (or contain at the first position) |
| 117 // the light they intend to use for the current depth rendering. |
| 118 |
| 119 if (fLights->numLights() > 0 && |
| 120 this->fLights->light(0).type() == SkLights::Light::kDirectional_Ligh
tType) { |
| 121 SkVector3 lightDir = this->fLights->light(0).dir(); |
| 122 SkScalar x = lightDir.fX * this->getZ(); |
| 123 SkScalar y = lightDir.fY * this->getZ(); |
| 124 |
| 125 this->translate(x, y); |
| 126 } |
| 127 |
| 128 } |
| 129 |
| 130 void onDrawPaint(const SkPaint& paint) override { |
| 131 this->updateMatrix(); |
| 132 this->INHERITED::onDrawPaint(paint); |
| 133 this->restore(); |
| 134 } |
| 135 |
| 136 void onDrawPoints(PointMode mode, size_t count, const SkPoint pts[], |
| 137 const SkPaint& paint) override { |
| 138 this->updateMatrix(); |
| 139 this->INHERITED::onDrawPoints(mode, count, pts, paint); |
| 140 this->restore(); |
| 141 } |
| 142 |
| 143 void onDrawRect(const SkRect& rect, const SkPaint& paint) override { |
| 144 this->updateMatrix(); |
| 145 this->INHERITED::onDrawRect(rect, paint); |
| 146 this->restore(); |
| 147 } |
| 148 |
| 149 void onDrawRRect(const SkRRect& rrect, const SkPaint& paint) override { |
| 150 this->updateMatrix(); |
| 151 this->INHERITED::onDrawRRect(rrect, paint); |
| 152 this->restore(); |
| 153 } |
| 154 |
| 155 void onDrawDRRect(const SkRRect& outer, const SkRRect& inner, |
| 156 const SkPaint& paint) override { |
| 157 this->updateMatrix(); |
| 158 this->INHERITED::onDrawDRRect(outer, inner, paint); |
| 159 this->restore(); |
| 160 } |
| 161 |
| 162 void onDrawOval(const SkRect& rect, const SkPaint& paint) override { |
| 163 this->updateMatrix(); |
| 164 this->INHERITED::onDrawOval(rect, paint); |
| 165 this->restore(); |
| 166 } |
| 167 |
| 168 void onDrawPath(const SkPath& path, const SkPaint& paint) override { |
| 169 this->updateMatrix(); |
| 170 this->INHERITED::onDrawPath(path, paint); |
| 171 this->restore(); |
| 172 } |
| 173 |
| 174 void onDrawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top, |
| 175 const SkPaint* paint) override { |
| 176 this->updateMatrix(); |
| 177 this->INHERITED::onDrawBitmap(bm, left, top, paint); |
| 178 this->restore(); |
| 179 } |
| 180 |
| 181 void onDrawBitmapRect(const SkBitmap& bm, const SkRect* src, const SkRect& d
st, |
| 182 const SkPaint* paint, SrcRectConstraint constraint) ov
erride { |
| 183 this->updateMatrix(); |
| 184 this->INHERITED::onDrawBitmapRect(bm, src, dst, paint, constraint); |
| 185 this->restore(); |
| 186 } |
| 187 |
| 188 void onDrawBitmapNine(const SkBitmap& bm, const SkIRect& center, |
| 189 const SkRect& dst, const SkPaint* paint) { |
| 190 this->updateMatrix(); |
| 191 this->INHERITED::onDrawBitmapNine(bm, center, dst, paint); |
| 192 this->restore(); |
| 193 } |
| 194 |
| 195 void onDrawImage(const SkImage* image, SkScalar left, SkScalar top, |
| 196 const SkPaint* paint) override { |
| 197 this->updateMatrix(); |
| 198 this->INHERITED::onDrawImage(image, left, top, paint); |
| 199 this->restore(); |
| 200 } |
| 201 |
| 202 void onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect&
dst, |
| 203 const SkPaint* paint, SrcRectConstraint constraint) ove
rride { |
| 204 this->updateMatrix(); |
| 205 this->INHERITED::onDrawImageRect(image, src, dst, paint, constraint); |
| 206 this->restore(); |
| 207 } |
| 208 |
| 209 void onDrawImageNine(const SkImage* image, const SkIRect& center, |
| 210 const SkRect& dst, const SkPaint* paint) { |
| 211 this->updateMatrix(); |
| 212 this->INHERITED::onDrawImageNine(image, center, dst, paint); |
| 213 this->restore(); |
| 214 } |
| 215 |
| 216 void onDrawVertices(VertexMode vmode, int vertexCount, const SkPoint vertice
s[], |
| 217 const SkPoint texs[], const SkColor colors[], SkXfermode
* xmode, |
| 218 const uint16_t indices[], int indexCount, const SkPaint&
paint) override { |
| 219 this->updateMatrix(); |
| 220 this->INHERITED::onDrawVertices(vmode, vertexCount, vertices, texs, colo
rs, |
| 221 xmode, indices, indexCount, paint); |
| 222 this->restore(); |
| 223 } |
| 224 |
| 225 void onDrawPatch(const SkPoint cubics[], const SkColor colors[], const SkPoi
nt texCoords[], |
| 226 SkXfermode* xmode, const SkPaint& paint) override { |
| 227 this->updateMatrix(); |
| 228 this->INHERITED::onDrawPatch(cubics, colors, texCoords, xmode, paint); |
| 229 this->restore(); |
| 230 } |
| 231 |
| 232 void onDrawText(const void* text, size_t byteLength, |
| 233 SkScalar x, SkScalar y, const SkPaint& paint) override { |
| 234 this->updateMatrix(); |
| 235 this->INHERITED::onDrawText(text, byteLength, x, y, paint); |
| 236 this->restore(); |
| 237 } |
| 238 |
| 239 void onDrawPosText(const void* text, size_t byteLength, |
| 240 const SkPoint pos[], const SkPaint& paint) override { |
| 241 this->updateMatrix(); |
| 242 this->INHERITED::onDrawPosText(text, byteLength, pos, paint); |
| 243 this->restore(); |
| 244 } |
| 245 |
| 246 void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos
[], |
| 247 SkScalar constY, const SkPaint& paint) override { |
| 248 this->updateMatrix(); |
| 249 this->INHERITED::onDrawPosTextH(text, byteLength, xpos, constY, paint); |
| 250 this->restore(); |
| 251 } |
| 252 |
| 253 void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& pat
h, |
| 254 const SkMatrix* matrix, const SkPaint& paint) override
{ |
| 255 this->updateMatrix(); |
| 256 this->INHERITED::onDrawTextOnPath(text, byteLength, path, matrix, paint)
; |
| 257 this->restore(); |
| 258 } |
| 259 |
| 260 void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform
xform[], |
| 261 const SkRect* cull, const SkPaint& paint) override { |
| 262 this->updateMatrix(); |
| 263 this->INHERITED::onDrawTextRSXform(text, byteLength, xform, cull, paint)
; |
| 264 this->restore(); |
| 265 } |
| 266 |
| 267 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 268 const SkPaint& paint) override { |
| 269 this->updateMatrix(); |
| 270 this->INHERITED::onDrawTextBlob(blob, x, y, paint); |
| 271 this->restore(); |
| 272 } |
| 273 |
| 274 private: |
| 275 typedef SkPaintFilterCanvas INHERITED; |
| 276 }; |
| 277 |
| 278 class ShadowMapsGM : public GM { |
| 279 public: |
| 280 ShadowMapsGM() { |
| 281 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
| 282 } |
| 283 |
| 284 void onOnceBeforeDraw() override { |
| 285 // Create a light set consisting of |
| 286 // - bluish directional light pointing more right than down |
| 287 // - reddish directional light pointing more down than right |
| 288 // - soft white ambient light |
| 289 |
| 290 SkLights::Builder builder; |
| 291 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.3f, 0.4f), |
| 292 SkVector3::Make(0.2f, 0.1f, 1.0f))); |
| 293 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.3f, 0.2f), |
| 294 SkVector3::Make(0.1f, 0.2f, 1.0f))); |
| 295 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.4f, 0.4f))); |
| 296 fLights = builder.finish(); |
| 297 } |
| 298 |
| 299 protected: |
| 300 static const int kWidth = 400; |
| 301 static const int kHeight = 400; |
| 302 |
| 303 SkString onShortName() override { |
| 304 return SkString("shadowmaps"); |
| 305 } |
| 306 |
| 307 SkISize onISize() override { |
| 308 return SkISize::Make(kWidth, kHeight); |
| 309 } |
| 310 |
| 311 void onDraw(SkCanvas* canvas) override { |
| 312 // This picture stores the picture of the scene. |
| 313 // It's used to generate the depth maps. |
| 314 sk_sp<SkPicture> pic(make_test_picture(kWidth, kHeight)); |
| 315 |
| 316 for (int i = 0; i < fLights->numLights(); ++i) { |
| 317 // skip over ambient lights; they don't cast shadows |
| 318 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type())
{ |
| 319 continue; |
| 320 } |
| 321 // TODO: compute the correct size of the depth map from the light pr
operties |
| 322 // TODO: maybe add a kDepth_8_SkColorType |
| 323 |
| 324 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, |
| 325 kBGRA_8888_SkColorType, |
| 326 kOpaque_SkAlphaType); |
| 327 |
| 328 // Create a new surface (that matches the backend of canvas) |
| 329 // for each shadow map |
| 330 sk_sp<SkSurface> surf(canvas->makeSurface(info)); |
| 331 |
| 332 // Wrap another SPFCanvas around the surface |
| 333 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = |
| 334 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); |
| 335 |
| 336 // set the depth map canvas to have the light we're drawing. |
| 337 SkLights::Builder builder; |
| 338 builder.add(fLights->light(i)); |
| 339 sk_sp<SkLights> curLight = builder.finish(); |
| 340 |
| 341 depthMapCanvas->setLights(std::move(curLight)); |
| 342 depthMapCanvas->drawPicture(pic); |
| 343 |
| 344 fLights->light(i).setShadowMap(surf->makeImageSnapshot()); |
| 345 } |
| 346 |
| 347 sk_sp<SkImage> povDepthMap; |
| 348 sk_sp<SkImage> diffuseMap; |
| 349 |
| 350 // TODO: pass the depth to the shader in vertices, or uniforms |
| 351 // so we don't have to render depth and color separately |
| 352 |
| 353 // povDepthMap |
| 354 { |
| 355 SkLights::Builder builder; |
| 356 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f), |
| 357 SkVector3::Make(0.0f, 0.0f, 1.0f))); |
| 358 sk_sp<SkLights> povLight = builder.finish(); |
| 359 |
| 360 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, |
| 361 kBGRA_8888_SkColorType, |
| 362 kOpaque_SkAlphaType); |
| 363 |
| 364 // Create a new surface (that matches the backend of canvas) |
| 365 // to create the povDepthMap |
| 366 sk_sp<SkSurface> surf(canvas->makeSurface(info)); |
| 367 |
| 368 // Wrap another SPFCanvas around the surface |
| 369 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = |
| 370 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); |
| 371 |
| 372 // set the depth map canvas to have the light as the user's POV |
| 373 depthMapCanvas->setLights(std::move(povLight)); |
| 374 |
| 375 depthMapCanvas->drawPicture(pic); |
| 376 |
| 377 povDepthMap = surf->makeImageSnapshot(); |
| 378 } |
| 379 |
| 380 // diffuseMap |
| 381 { |
| 382 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, |
| 383 kBGRA_8888_SkColorType, |
| 384 kOpaque_SkAlphaType); |
| 385 |
| 386 sk_sp<SkSurface> surf(canvas->makeSurface(info)); |
| 387 surf->getCanvas()->drawPicture(pic); |
| 388 |
| 389 diffuseMap = surf->makeImageSnapshot(); |
| 390 } |
| 391 |
| 392 SkPaint paint; |
| 393 paint.setShader(make_shadow_shader(std::move(povDepthMap), std::move(dif
fuseMap), fLights)); |
| 394 |
| 395 canvas->drawRect(SkRect::MakeIWH(kWidth, kHeight), paint); |
| 396 } |
| 397 |
| 398 private: |
| 399 sk_sp<SkLights> fLights; |
| 400 |
| 401 typedef GM INHERITED; |
| 402 }; |
| 403 |
| 404 ////////////////////////////////////////////////////////////////////////////// |
| 405 |
| 406 DEF_GM(return new ShadowMapsGM;) |
| 407 } |
| 408 |
| 409 #endif |
OLD | NEW |