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" | |
|
robertphillips
2016/07/27 18:52:58
Alphabetize the Sk* headers
vjiaoblack
2016/07/28 17:17:50
Done.
| |
| 9 #include "SkLights.h" | |
| 10 #include "SkPictureRecorder.h" | |
| 11 #include "SkSurface.h" | |
| 12 #include "SkPaintFilterCanvas.h" | |
| 13 #include "SkMatrix.h" | |
| 14 #include "SkShadowShader.h" | |
|
robertphillips
2016/07/27 18:52:58
Do we need SkBitmapProcShader.h ?
vjiaoblack
2016/07/28 17:17:51
Done.
| |
| 15 #include "SkBitmapProcShader.h" | |
| 16 #include "SkNormalSource.h" | |
| 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 | |
|
robertphillips
2016/07/27 18:52:58
space before the ':'
use INHERITED rather than SkP
vjiaoblack
2016/07/28 17:17:50
Done.
| |
| 81 SkShadowPaintFilterCanvas(SkCanvas* canvas): SkPaintFilterCanvas(canvas) { } ; | |
| 82 | |
| 83 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type type) const override { | |
| 84 if (*paint) { | |
| 85 uint32_t z = this->getZ(); | |
| 86 SkColor color = 0xFF000000; // init color to opaque black | |
|
robertphillips
2016/07/27 18:52:58
assert that z will fit in 8 bits ?
vjiaoblack
2016/07/28 17:17:50
Done.
| |
| 87 color |= z; // Put the index into the blue component | |
| 88 paint->writable()->setColor(color); | |
| 89 } | |
| 90 | |
| 91 return true; | |
| 92 } | |
| 93 | |
|
robertphillips
2016/07/27 18:52:58
Aren't all these onDraw* methods supposed to have
vjiaoblack
2016/07/28 17:17:50
Done.
| |
| 94 void onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, const S kPaint* paint) { | |
|
robertphillips
2016/07/27 18:52:58
It seems like SkPaintFilterCanvas::onDrawPicture d
vjiaoblack
2016/07/28 17:17:51
It doesn't work. I think because SkPaintFilterCanv
robertphillips
2016/07/28 19:47:25
That's odd, it should ultimately wind up calling S
| |
| 95 SkTCopyOnFirstWrite<SkPaint> filteredPaint(paint); | |
|
robertphillips
2016/07/27 18:52:58
this->onFilter
vjiaoblack
2016/07/28 17:17:51
Done.
| |
| 96 if (onFilter(&filteredPaint, kPicture_Type)) | |
|
robertphillips
2016/07/27 18:52:58
Need enclosing brackets here
I think you still nee
vjiaoblack
2016/07/28 17:17:50
Done.
| |
| 97 SkCanvas::onDrawPicture(picture, matrix, filteredPaint); | |
| 98 } | |
| 99 | |
| 100 void updateMatrix() { | |
| 101 this->save(); | |
| 102 | |
| 103 // When we use the SkShadowPaintFilterCanvas, we can only render | |
| 104 // one depth map at a time. Thus, we leave it up to the user to | |
| 105 // set SkLights to only contain (or contain at the first position) | |
| 106 // the light they intend to use for the current depth rendering. | |
| 107 SkVector3 lightDir = this->fLights->light(0).dir(); | |
| 108 SkScalar x = lightDir.fX * this->getZ(); | |
| 109 SkScalar y = lightDir.fY * this->getZ(); | |
| 110 | |
| 111 this->translate(x, y); | |
| 112 } | |
| 113 | |
| 114 void onDrawPaint(const SkPaint& paint) { | |
| 115 this->updateMatrix(); | |
| 116 this->INHERITED::onDrawPaint(paint); | |
| 117 this->restore(); | |
| 118 } | |
| 119 | |
| 120 void onDrawPoints(PointMode mode, size_t count, const SkPoint pts[], const S kPaint& paint) { | |
| 121 this->updateMatrix(); | |
| 122 this->INHERITED::onDrawPoints(mode, count, pts, paint); | |
| 123 this->restore(); | |
| 124 } | |
| 125 | |
| 126 void onDrawRect(const SkRect& rect, const SkPaint& paint) { | |
| 127 this->updateMatrix(); | |
| 128 this->INHERITED::onDrawRect(rect, paint); | |
| 129 this->restore(); | |
| 130 } | |
| 131 | |
| 132 void onDrawRRect(const SkRRect& rrect, const SkPaint& paint) { | |
| 133 this->updateMatrix(); | |
| 134 this->INHERITED::onDrawRRect(rrect, paint); | |
| 135 this->restore(); | |
| 136 } | |
| 137 | |
| 138 void onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) { | |
| 139 this->updateMatrix(); | |
| 140 this->INHERITED::onDrawDRRect(outer, inner, paint); | |
| 141 this->restore(); | |
| 142 } | |
| 143 | |
| 144 void onDrawOval(const SkRect& rect, const SkPaint& paint) { | |
| 145 this->updateMatrix(); | |
| 146 this->INHERITED::onDrawOval(rect, paint); | |
| 147 this->restore(); | |
| 148 } | |
| 149 | |
| 150 void onDrawPath(const SkPath& path, const SkPaint& paint) { | |
| 151 this->updateMatrix(); | |
| 152 this->INHERITED::onDrawPath(path, paint); | |
| 153 this->restore(); | |
| 154 } | |
| 155 | |
| 156 void onDrawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top, const SkP aint* paint) { | |
| 157 this->updateMatrix(); | |
| 158 this->INHERITED::onDrawBitmap(bm, left, top, paint); | |
| 159 this->restore(); | |
| 160 } | |
| 161 | |
| 162 void onDrawBitmapRect(const SkBitmap& bm, const SkRect* src, const SkRect& d st, | |
| 163 const SkPaint* paint, SrcRectConstraint constraint) { | |
| 164 this->updateMatrix(); | |
| 165 this->INHERITED::onDrawBitmapRect(bm, src, dst, paint, constraint); | |
| 166 this->restore(); | |
| 167 } | |
| 168 | |
| 169 void onDrawBitmapNine(const SkBitmap& bm, const SkIRect& center, | |
| 170 const SkRect& dst, const SkPaint* paint) { | |
| 171 this->updateMatrix(); | |
| 172 this->INHERITED::onDrawBitmapNine(bm, center, dst, paint); | |
| 173 this->restore(); | |
| 174 } | |
| 175 | |
| 176 void onDrawImage(const SkImage* image, SkScalar left, SkScalar top, const Sk Paint* paint) { | |
| 177 this->updateMatrix(); | |
| 178 this->INHERITED::onDrawImage(image, left, top, paint); | |
| 179 this->restore(); | |
| 180 } | |
| 181 | |
| 182 void onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst, | |
| 183 const SkPaint* paint, SrcRectConstraint constraint) { | |
| 184 this->updateMatrix(); | |
| 185 this->INHERITED::onDrawImageRect(image, src, dst, paint, constraint); | |
| 186 this->restore(); | |
| 187 } | |
| 188 | |
| 189 void onDrawImageNine(const SkImage* image, const SkIRect& center, | |
| 190 const SkRect& dst, const SkPaint* paint) { | |
| 191 this->updateMatrix(); | |
| 192 this->INHERITED::onDrawImageNine(image, center, dst, paint); | |
| 193 this->restore(); | |
| 194 } | |
| 195 | |
| 196 void onDrawVertices(VertexMode vmode, int vertexCount, const SkPoint vertice s[], | |
| 197 const SkPoint texs[], const SkColor colors[], SkXfermode * xmode, | |
| 198 const uint16_t indices[], int indexCount, const SkPaint& paint) { | |
| 199 this->updateMatrix(); | |
| 200 this->INHERITED::onDrawVertices(vmode, vertexCount, vertices, texs, colo rs, | |
| 201 xmode, indices, indexCount, paint); | |
| 202 this->restore(); | |
| 203 } | |
| 204 | |
| 205 void onDrawPatch(const SkPoint cubics[], const SkColor colors[], const SkPoi nt texCoords[], | |
| 206 SkXfermode* xmode, const SkPaint& paint) { | |
| 207 this->updateMatrix(); | |
| 208 this->INHERITED::onDrawPatch(cubics, colors, texCoords, xmode, paint); | |
| 209 this->restore(); | |
| 210 } | |
| 211 | |
| 212 void onDrawText(const void* text, size_t byteLength, | |
| 213 SkScalar x, SkScalar y, const SkPaint& paint) { | |
| 214 this->updateMatrix(); | |
| 215 this->INHERITED::onDrawText(text, byteLength, x, y, paint); | |
| 216 this->restore(); | |
| 217 } | |
| 218 | |
| 219 void onDrawPosText(const void* text, size_t byteLength, | |
| 220 const SkPoint pos[], const SkPaint& paint) { | |
| 221 this->updateMatrix(); | |
| 222 this->INHERITED::onDrawPosText(text, byteLength, pos, paint); | |
| 223 this->restore(); | |
| 224 } | |
| 225 | |
| 226 void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos [], | |
| 227 SkScalar constY, const SkPaint& paint) { | |
| 228 this->updateMatrix(); | |
| 229 this->INHERITED::onDrawPosTextH(text, byteLength, xpos, constY, paint); | |
| 230 this->restore(); | |
| 231 } | |
| 232 | |
| 233 void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& pat h, | |
| 234 const SkMatrix* matrix, const SkPaint& paint) { | |
| 235 this->updateMatrix(); | |
| 236 this->INHERITED::onDrawTextOnPath(text, byteLength, path, matrix, paint) ; | |
| 237 this->restore(); | |
| 238 } | |
| 239 | |
| 240 void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[], | |
| 241 const SkRect* cull, const SkPaint& paint) { | |
| 242 this->updateMatrix(); | |
| 243 this->INHERITED::onDrawTextRSXform(text, byteLength, xform, cull, paint) ; | |
| 244 this->restore(); | |
| 245 } | |
| 246 | |
| 247 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const Sk Paint& paint) { | |
| 248 this->updateMatrix(); | |
| 249 this->INHERITED::onDrawTextBlob(blob, x, y, paint); | |
| 250 this->restore(); | |
| 251 } | |
| 252 | |
| 253 private: | |
| 254 typedef SkPaintFilterCanvas INHERITED; | |
|
robertphillips
2016/07/27 18:52:58
extra '\n' here
vjiaoblack
2016/07/28 17:17:50
Done.
| |
| 255 | |
| 256 }; | |
| 257 | |
| 258 class ShadowMapsGM : public GM { | |
| 259 public: | |
| 260 ShadowMapsGM() { | |
| 261 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); | |
| 262 } | |
| 263 | |
| 264 void onOnceBeforeDraw() override { | |
| 265 // Create a light set consisting of | |
| 266 // - bluish directional light pointing more right than down | |
| 267 // - reddish directional light pointing more down than right | |
| 268 // - soft white ambient light | |
| 269 | |
| 270 SkLights::Builder builder; | |
| 271 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.3f, 0.4f), | |
| 272 SkVector3::Make(0.2f, 0.1f, 1.0f))); | |
| 273 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.3f, 0.2f), | |
| 274 SkVector3::Make(0.1f, 0.2f, 1.0f))); | |
| 275 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.4f, 0.4f))); | |
| 276 fLights = builder.finish(); | |
| 277 } | |
| 278 | |
| 279 protected: | |
| 280 static const int kWidth = 400; | |
| 281 static const int kHeight = 400; | |
| 282 | |
| 283 SkString onShortName() override { | |
| 284 return SkString("shadowmaps"); | |
| 285 } | |
| 286 | |
| 287 SkISize onISize() override { | |
| 288 return SkISize::Make(kWidth, kHeight); | |
| 289 } | |
| 290 | |
| 291 void onDraw(SkCanvas* canvas) override { | |
| 292 // This picture stores the picture of the scene. | |
| 293 // It's used to generate the depth maps. | |
| 294 sk_sp<SkPicture> pic(make_test_picture(kWidth, kHeight)); | |
| 295 | |
|
robertphillips
2016/07/27 18:52:58
Why do we wrap 'canvas' in an SPFC? AFAICT we neve
vjiaoblack
2016/07/28 17:17:50
Done.
Seemed like SPFCanvas and Canvas had differ
| |
| 296 // Wrapping input canvas in an ShadowPaintFilterCanvas | |
| 297 sk_sp<SkShadowPaintFilterCanvas> SPFCanvas = sk_make_sp<SkShadowPaintFil terCanvas>(canvas); | |
| 298 | |
| 299 for (int i = 0; i < fLights->numLights(); ++i) { | |
| 300 // skip over ambient lights; they don't cast shadows | |
| 301 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type()) { | |
| 302 continue; | |
| 303 } | |
| 304 // TODO: compute the correct size of the depth map from the light pr operties | |
| 305 // TODO: maybe add a kDepth_8_SkColorType | |
| 306 // TODO: change to kIndex_8_SkColorType | |
|
robertphillips
2016/07/27 18:52:58
rm this last TODO or change to A8
vjiaoblack
2016/07/28 17:17:50
Done.
| |
| 307 | |
|
robertphillips
2016/07/27 18:52:58
Maybe change kWidth & kHeight here to be kDefaultS
vjiaoblack
2016/07/28 17:17:50
What do you mean?
The shadow map size should be de
robertphillips
2016/07/28 19:47:25
Right, since they aren't necessarily the same you
| |
| 308 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, | |
| 309 kBGRA_8888_SkColorType, | |
| 310 kOpaque_SkAlphaType); | |
| 311 | |
|
robertphillips
2016/07/27 18:52:58
// Create a new surface (that matches the backend
vjiaoblack
2016/07/28 17:17:50
Done.
| |
| 312 // Get the surface accessor to the SPFCanvas | |
| 313 sk_sp<SkSurface> surf(SPFCanvas->makeSurface(info)); | |
| 314 | |
| 315 // Wrap another SPFCanvas around the surface | |
| 316 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = | |
| 317 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); | |
| 318 | |
| 319 // set the depth map canvas to have the light we're drawing. | |
| 320 SkLights::Builder builder; | |
| 321 builder.add(fLights->light(i)); | |
| 322 sk_sp<SkLights> curLight = builder.finish(); | |
| 323 | |
| 324 depthMapCanvas->setLights(curLight); | |
| 325 depthMapCanvas->drawPicture(pic); | |
| 326 | |
| 327 fLights->light(i).setShadowMap(surf->makeImageSnapshot()); | |
| 328 } | |
| 329 | |
| 330 sk_sp<SkImage> povDepthMap; | |
| 331 sk_sp<SkImage> diffuseMap; | |
| 332 | |
| 333 // TODO: pass the depth to the shader in vertices, or uniforms | |
| 334 // so we don't have to render depth and color separately | |
| 335 | |
| 336 // povDepthMap | |
| 337 { | |
| 338 SkLights::Builder builder; | |
| 339 builder.add(SkLights::Light(SkColor3f::Make(1.0f, 1.0f, 1.0f), | |
| 340 SkVector3::Make(0.0f, 0.0f, 1.0f))); | |
| 341 sk_sp<SkLights> povLight = builder.finish(); | |
| 342 | |
| 343 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, | |
| 344 kBGRA_8888_SkColorType, | |
| 345 kOpaque_SkAlphaType); | |
| 346 | |
|
robertphillips
2016/07/27 18:52:58
// Create a new surface (that matches the backend
vjiaoblack
2016/07/28 17:17:51
Done.
| |
| 347 // Get the surface accessor to the SPFCanvas | |
| 348 sk_sp<SkSurface> surf(SPFCanvas->makeSurface(info)); | |
| 349 | |
| 350 // Wrap another SPFCanvas around the surface | |
| 351 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = | |
| 352 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); | |
| 353 | |
| 354 // set the depth map canvas to have the light as the user's POV | |
| 355 depthMapCanvas->setLights(povLight); | |
| 356 | |
| 357 depthMapCanvas->drawPicture(pic); | |
| 358 | |
| 359 povDepthMap = surf->makeImageSnapshot(); | |
| 360 } | |
| 361 | |
| 362 // diffuseMap | |
| 363 { | |
| 364 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, | |
| 365 kBGRA_8888_SkColorType, | |
| 366 kOpaque_SkAlphaType); | |
| 367 | |
| 368 sk_sp<SkSurface> surf(SPFCanvas->makeSurface(info)); | |
| 369 surf->getCanvas()->drawPicture(pic); | |
| 370 | |
| 371 diffuseMap = surf->makeImageSnapshot(); | |
| 372 } | |
| 373 | |
|
robertphillips
2016/07/27 18:52:58
Make this 2 lines:
SkPaint paint;
paint.setShader
vjiaoblack
2016/07/28 17:17:50
Done.
| |
| 374 sk_sp<SkShader> shadowShader = make_shadow_shader(povDepthMap, diffuseMa p, fLights); | |
| 375 SkPaint paint; | |
| 376 paint.setShader(shadowShader); | |
| 377 | |
| 378 canvas->drawRect(SkRect::MakeIWH(kWidth, kHeight), paint); | |
| 379 } | |
| 380 | |
| 381 private: | |
| 382 sk_sp<SkLights> fLights; | |
| 383 | |
| 384 typedef GM INHERITED; | |
| 385 }; | |
| 386 | |
| 387 ////////////////////////////////////////////////////////////////////////////// | |
| 388 | |
| 389 DEF_GM(return new ShadowMapsGM;) | |
| 390 } | |
| OLD | NEW |