Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
|
jvanverth1
2016/07/26 15:01:24
I'd rename this to something more generic, like sh
vjiaoblack
2016/07/27 15:26:26
Done.
| |
| 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/26 16:15:04
Do you still need SkDrawFilter.h ?
vjiaoblack
2016/07/27 15:26:26
Done.
| |
| 9 #include "SkDrawFilter.h" | |
| 10 #include "SkLights.h" | |
| 11 #include "SkPictureRecorder.h" | |
| 12 #include "SkSurface.h" | |
| 13 #include "SkPaintFilterCanvas.h" | |
| 14 #include "SkMatrix.h" | |
| 15 #include "SkShadowShader.h" | |
| 16 #include "SkBitmapProcShader.h" | |
| 17 #include "SkNormalSource.h" | |
| 18 | |
|
robertphillips
2016/07/26 16:15:04
overlength line
file static methods are make_shado
vjiaoblack
2016/07/27 15:26:26
Done.
| |
| 19 static sk_sp<SkShader> makeShadowShader(sk_sp<SkImage> povDepth, sk_sp<SkImage> diffuse, sk_sp<SkLights> lights) { | |
| 20 // TODO: Victor continues to figure this out | |
| 21 SkMatrix matrix1 = SkMatrix::I(); | |
| 22 SkMatrix matrix2 = SkMatrix::I(); | |
| 23 | |
| 24 // this is the 0, 0, 1 light | |
|
jvanverth1
2016/07/26 15:01:24
It's unclear based on this comment what this means
vjiaoblack
2016/07/27 15:26:27
Is that necessarily better? Asking because IDK. Th
| |
| 25 sk_sp<SkShader> povDepthShader = povDepth->makeShader( | |
| 26 SkShader::kClamp_TileMode, | |
| 27 SkShader::kClamp_TileMode, | |
| 28 &matrix1); | |
|
jvanverth1
2016/07/26 15:01:24
If the matrix is identity, you don't need to pass
vjiaoblack
2016/07/27 15:26:26
Done.
| |
| 29 | |
| 30 sk_sp<SkShader> diffuseShader = diffuse->makeShader( | |
| 31 SkShader::kClamp_TileMode, | |
| 32 SkShader::kClamp_TileMode, | |
| 33 &matrix2); | |
| 34 | |
|
robertphillips
2016/07/26 16:15:05
std::move(lights) ?
vjiaoblack
2016/07/27 15:26:26
Done.
| |
| 35 return SkShadowShader::Make(std::move(povDepthShader), std::move(diffuseShad er), lights); | |
| 36 } | |
| 37 | |
|
robertphillips
2016/07/26 16:15:05
If it is named this way it should be file static
/
vjiaoblack
2016/07/27 15:26:26
Done.
| |
| 38 sk_sp<SkPicture> make_picture(int width, int height, sk_sp<SkLights> lights) { | |
|
jvanverth1
2016/07/26 15:01:24
This name seems a little generic. Maybe make_test_
vjiaoblack
2016/07/27 15:26:26
Done.
| |
| 39 SkPictureRecorder recorder; | |
| 40 | |
| 41 // LONG RANGE TODO: eventually add SkBBHFactory (bounding box factory) | |
| 42 SkCanvas* canvas = recorder.beginRecording(SkRect::MakeIWH(width, height)); | |
| 43 | |
| 44 SkASSERT(canvas->getTotalMatrix().isIdentity()); | |
| 45 SkPaint paint; | |
| 46 paint.setColor(SK_ColorGRAY); | |
| 47 | |
| 48 // LONG RANGE TODO: tag occluders | |
| 49 // LONG RANGE TODO: track number of IDs we need (hopefully less than 256) | |
| 50 // and determinate the mapping from z to id | |
| 51 | |
| 52 // universal receiver, "ground" | |
| 53 canvas->drawRect(SkRect::MakeIWH(width, height), paint); | |
| 54 | |
| 55 // TODO: Maybe add the ID here along with the depth | |
| 56 | |
| 57 paint.setColor(0xFFEE8888); | |
| 58 | |
| 59 canvas->translateZ(80); | |
| 60 canvas->drawRect(SkRect::MakeLTRB(200,150,350,300), paint); | |
| 61 | |
| 62 paint.setColor(0xFF88EE88); | |
| 63 | |
| 64 canvas->translateZ(80); | |
| 65 canvas->drawRect(SkRect::MakeLTRB(150,200,300,350), paint); | |
| 66 | |
| 67 paint.setColor(0xFF8888EE); | |
| 68 | |
| 69 canvas->translateZ(80); | |
| 70 canvas->drawRect(SkRect::MakeLTRB(100,100,250,250), paint); | |
| 71 // TODO: Add an assert that Z order matches painter's order | |
| 72 // TODO: think about if the Z-order always matching painting order is too st rict | |
| 73 | |
| 74 return recorder.finishRecordingAsPicture(); | |
| 75 } | |
| 76 | |
| 77 | |
| 78 namespace skiagm { | |
| 79 | |
|
robertphillips
2016/07/26 16:15:04
// Why does this class exist
vjiaoblack
2016/07/27 15:26:27
Done.
| |
| 80 class SkShadowPaintFilterCanvas : public SkPaintFilterCanvas { | |
| 81 public: | |
| 82 | |
|
robertphillips
2016/07/26 16:15:04
Do you need to this entry point ?
It isn't virtual
vjiaoblack
2016/07/27 15:26:27
Done.
| |
| 83 sk_sp<SkLights> getLights() { | |
| 84 return INHERITED::getLights(); | |
| 85 } | |
| 86 | |
|
robertphillips
2016/07/26 16:15:05
Can you fit this all on one line. Also no spaces i
vjiaoblack
2016/07/27 15:26:26
Done.
| |
| 87 SkShadowPaintFilterCanvas(SkCanvas* canvas) | |
| 88 : SkPaintFilterCanvas( canvas ) { } | |
| 89 | |
| 90 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type type) const override { | |
| 91 if (*paint) { | |
| 92 uint32_t z = this->getZ(); | |
| 93 SkColor color = 0xFF000000; // init color to opaque white | |
| 94 color |= z; // Put the index into the blue component | |
| 95 paint->writable()->setColor(color); | |
| 96 } | |
| 97 | |
| 98 return true; | |
| 99 } | |
| 100 | |
| 101 void onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, const S kPaint* paint) { | |
|
robertphillips
2016/07/26 16:15:04
Maybe some words here about how the updating of th
vjiaoblack
2016/07/27 15:26:27
Done.
| |
| 102 SkTCopyOnFirstWrite<SkPaint> filteredPaint(paint); | |
|
robertphillips
2016/07/26 16:15:05
Calls to member functions are preceded with "this-
vjiaoblack
2016/07/27 15:26:25
Filtering is necessary for calling onFilter, which
| |
| 103 if (onFilter(&filteredPaint, kPicture_Type)) | |
|
robertphillips
2016/07/26 16:15:04
Need {}s around this line
Not "this->INHERITED::on
vjiaoblack
2016/07/27 15:26:27
Doing that didn't work.
| |
| 104 SkCanvas::onDrawPicture(picture, matrix, filteredPaint); | |
| 105 } | |
| 106 | |
| 107 void updateMatrix() { | |
|
robertphillips
2016/07/26 16:15:04
this->
vjiaoblack
2016/07/27 15:26:27
Done.
| |
| 108 save(); | |
| 109 | |
|
robertphillips
2016/07/26 16:15:04
Shouldn't we only ever have one light when we get
vjiaoblack
2016/07/27 15:26:27
Done.
| |
| 110 // TODO which light do we set? | |
| 111 SkVector3 lightDir = this->fLights->light(0).dir(); | |
|
jvanverth1
2016/07/26 15:01:24
Some sort of comment here that describes what you'
vjiaoblack
2016/07/27 15:26:25
I did!
I can't really pass into these override-ed
| |
| 112 SkScalar x = lightDir.fX * this->getZ(); | |
| 113 SkScalar y = lightDir.fY * this->getZ(); | |
| 114 | |
| 115 this->translate(x, y); | |
| 116 } | |
| 117 | |
| 118 void onDrawPaint(const SkPaint& paint) { | |
|
robertphillips
2016/07/26 16:15:05
this-> & all following instances too
vjiaoblack
2016/07/27 15:26:26
Done.
| |
| 119 updateMatrix(); | |
| 120 this->INHERITED::onDrawPaint(paint); | |
|
robertphillips
2016/07/26 16:15:04
this-> & all following instances too
vjiaoblack
2016/07/27 15:26:26
Done.
| |
| 121 restore(); | |
| 122 } | |
| 123 | |
| 124 void onDrawPoints(PointMode mode, size_t count, const SkPoint pts[], | |
|
robertphillips
2016/07/26 16:15:05
align the "const SkPint& paint" somehow
vjiaoblack
2016/07/27 15:26:27
Done.
| |
| 125 const SkPaint& paint) { | |
| 126 updateMatrix(); | |
| 127 this->INHERITED::onDrawPoints(mode, count, pts, paint); | |
| 128 restore(); | |
| 129 } | |
| 130 | |
| 131 void onDrawRect(const SkRect& rect, const SkPaint& paint) { | |
| 132 updateMatrix(); | |
| 133 this->INHERITED::onDrawRect(rect, paint); | |
| 134 restore(); | |
| 135 } | |
| 136 | |
| 137 void onDrawRRect(const SkRRect& rrect, const SkPaint& paint) { | |
| 138 updateMatrix(); | |
| 139 this->INHERITED::onDrawRRect(rrect, paint); | |
| 140 restore(); | |
| 141 } | |
| 142 | |
| 143 void onDrawDRRect(const SkRRect& outer, const SkRRect& inner, | |
| 144 const SkPaint& paint) { | |
| 145 updateMatrix(); | |
| 146 this->INHERITED::onDrawDRRect(outer, inner, paint); | |
| 147 restore(); | |
| 148 } | |
| 149 | |
| 150 void onDrawOval(const SkRect& rect, const SkPaint& paint) { | |
| 151 updateMatrix(); | |
| 152 this->INHERITED::onDrawOval(rect, paint); | |
| 153 restore(); | |
| 154 } | |
| 155 | |
| 156 void onDrawPath(const SkPath& path, const SkPaint& paint) { | |
| 157 updateMatrix(); | |
| 158 this->INHERITED::onDrawPath(path, paint); | |
| 159 restore(); | |
| 160 } | |
| 161 | |
| 162 void onDrawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top, | |
| 163 const SkPaint* paint) { | |
| 164 updateMatrix(); | |
| 165 this->INHERITED::onDrawBitmap(bm, left, top, paint); | |
| 166 restore(); | |
| 167 } | |
| 168 | |
| 169 void onDrawBitmapRect(const SkBitmap& bm, const SkRect* src, const SkRect& d st, | |
| 170 const SkPaint* paint, SrcRectCons traint constraint) { | |
| 171 updateMatrix(); | |
| 172 this->INHERITED::onDrawBitmapRect(bm, src, dst, paint, constraint); | |
| 173 restore(); | |
| 174 } | |
| 175 | |
| 176 void onDrawBitmapNine(const SkBitmap& bm, const SkIRect& center, | |
| 177 const SkRect& dst, const SkPaint* paint) { | |
| 178 updateMatrix(); | |
| 179 this->INHERITED::onDrawBitmapNine(bm, center, dst, paint); | |
| 180 restore(); | |
| 181 } | |
| 182 | |
| 183 void onDrawImage(const SkImage* image, SkScalar left, SkScalar top, | |
| 184 const SkPaint* paint) { | |
| 185 updateMatrix(); | |
| 186 this->INHERITED::onDrawImage(image, left, top, paint); | |
| 187 restore(); | |
| 188 } | |
| 189 | |
| 190 void onDrawImageRect(const SkImage* image, const SkRect* src, | |
| 191 const SkRect& dst, const SkPaint* paint, | |
| 192 SrcRectConstraint constraint) { | |
| 193 updateMatrix(); | |
| 194 this->INHERITED::onDrawImageRect(image, src, dst, paint, constraint); | |
| 195 restore(); | |
| 196 } | |
| 197 | |
| 198 void onDrawImageNine(const SkImage* image, const SkIRect& center, | |
| 199 const SkRect& dst, const SkPaint* paint) { | |
| 200 updateMatrix(); | |
| 201 this->INHERITED::onDrawImageNine(image, center, dst, paint); | |
| 202 restore(); | |
| 203 } | |
| 204 | |
| 205 void onDrawVertices(VertexMode vmode, int vertexCount, | |
| 206 const SkPoint vertices[], const SkP oint texs[], | |
| 207 const SkColor colors[], SkXfermode* xmode, | |
| 208 const uint16_t indices[], int index Count, | |
| 209 const SkPaint& paint) { | |
| 210 updateMatrix(); | |
| 211 this->INHERITED::onDrawVertices(vmode, vertexCount, vertices, texs, colo rs, | |
| 212 xmode, indices, indexCount, paint); | |
| 213 restore(); | |
| 214 } | |
| 215 | |
| 216 void onDrawPatch(const SkPoint cubics[], const SkColor colors[], | |
| 217 const SkPoint texCoords[], SkXfermode* xmode, | |
| 218 const SkPaint& paint) { | |
| 219 updateMatrix(); | |
| 220 this->INHERITED::onDrawPatch(cubics, colors, texCoords, xmode, paint); | |
| 221 restore(); | |
| 222 } | |
| 223 | |
| 224 void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, | |
| 225 const SkPaint& paint) { | |
| 226 updateMatrix(); | |
| 227 this->INHERITED::onDrawText(text, byteLength, x, y, paint); | |
| 228 restore(); | |
| 229 } | |
| 230 | |
| 231 void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], | |
| 232 const SkPaint& paint) { | |
| 233 updateMatrix(); | |
| 234 this->INHERITED::onDrawPosText(text, byteLength, pos, paint); | |
| 235 restore(); | |
| 236 } | |
| 237 | |
| 238 void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos [], | |
| 239 SkScalar constY, const SkPaint& pai nt) { | |
| 240 updateMatrix(); | |
| 241 this->INHERITED::onDrawPosTextH(text, byteLength, xpos, constY, paint); | |
| 242 restore(); | |
| 243 } | |
| 244 | |
| 245 void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& pat h, | |
| 246 const SkMatrix* matrix, const SkP aint& paint) { | |
| 247 updateMatrix(); | |
| 248 this->INHERITED::onDrawTextOnPath(text, byteLength, path, matrix, paint) ; | |
| 249 restore(); | |
| 250 } | |
| 251 | |
| 252 void onDrawTextRSXform(const void* text, size_t byteLength, | |
| 253 const SkRSXform xform[], const S kRect* cull, | |
| 254 const SkPaint& paint) { | |
| 255 updateMatrix(); | |
| 256 this->INHERITED::onDrawTextRSXform(text, byteLength, xform, cull, paint) ; | |
| 257 restore(); | |
| 258 } | |
| 259 | |
| 260 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const Sk Paint& paint) { | |
| 261 updateMatrix(); | |
| 262 this->INHERITED::onDrawTextBlob(blob, x, y, paint); | |
| 263 restore(); | |
| 264 } | |
| 265 | |
| 266 private: | |
| 267 typedef SkPaintFilterCanvas INHERITED; | |
| 268 | |
| 269 }; | |
| 270 | |
| 271 class VictorShadowsGM : public GM { | |
| 272 public: | |
| 273 VictorShadowsGM() { | |
| 274 | |
| 275 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); | |
|
robertphillips
2016/07/26 16:15:04
move the construction of the lights into onOnceBef
vjiaoblack
2016/07/27 15:26:26
Done.
| |
| 276 SkLights::Builder builder; | |
| 277 builder.add(SkLights::Light(SkColor3f::Make(0.2f, 0.3f, 0.4f), | |
| 278 SkVector3::Make(0.22f, 0.12f, 0.9747f))); | |
| 279 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.3f, 0.2f), | |
| 280 SkVector3::Make(0.12f, 0.22f, 0.9747f))); | |
| 281 builder.add(SkLights::Light(SkColor3f::Make(0.4f, 0.4f, 0.4f))); | |
| 282 fLights = builder.finish(); | |
| 283 } | |
| 284 | |
| 285 protected: | |
| 286 static const int kWidth = 400; | |
| 287 static const int kHeight = 400; | |
| 288 | |
| 289 SkString onShortName() override { | |
|
robertphillips
2016/07/26 16:15:05
Probably rename to shadowmap-shadows or something
vjiaoblack
2016/07/27 15:26:26
Done.
| |
| 290 return SkString("victor-shadows"); | |
| 291 } | |
| 292 | |
| 293 SkISize onISize() override { | |
| 294 return SkISize::Make(kWidth, kHeight); | |
| 295 } | |
| 296 | |
| 297 void onDraw(SkCanvas* canvas) override { | |
| 298 // This picture stores the picture of the scene. | |
|
robertphillips
2016/07/26 16:15:04
depth maps or shadow maps ?
vjiaoblack
2016/07/27 15:26:26
Technically, shadow maps === depth maps, right?
(a
| |
| 299 // It's used to generate the depth maps. | |
| 300 sk_sp<SkPicture> pic(make_picture(kWidth, kHeight, fLights)); | |
| 301 | |
|
robertphillips
2016/07/26 16:15:04
Unless I'm missing something this isn't a cast. Do
vjiaoblack
2016/07/27 15:26:26
Done.
| |
| 302 // The general ShadowPaintFilterCanvas cast of input canvas | |
| 303 sk_sp<SkShadowPaintFilterCanvas> SPFCanvas = sk_make_sp<SkShadowPaintFil terCanvas>(canvas); | |
| 304 | |
| 305 for (int i = 0; i < fLights->numLights(); ++i) { | |
| 306 // skip over ambient lights; they don't cast shadows | |
| 307 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type()) { | |
| 308 continue; | |
| 309 } | |
| 310 // TODO: compute the correct size of the shadow map from the light p roperties | |
| 311 // TODO: maybe add a kDepth_8_SkColorType | |
| 312 // TODO: change to kIndex_8_SkColorType | |
|
jvanverth1
2016/07/26 15:01:24
Or use kAlpha_8_SkColorType?
robertphillips
2016/07/26 16:15:04
We should probably avoid trying to render to Index
vjiaoblack
2016/07/27 15:26:26
Is that possible through SkPaint?
vjiaoblack
2016/07/27 15:26:27
I don't think this is actually possible with the c
| |
| 313 | |
| 314 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, | |
| 315 kBGRA_8888_SkColorType, | |
| 316 kOpaque_SkAlphaType); | |
| 317 | |
| 318 sk_sp<SkSurface> surf(SPFCanvas->makeSurface(info)); | |
|
robertphillips
2016/07/26 16:15:04
// Wrap the new canvas in a ShadowPaintFilterCanva
vjiaoblack
2016/07/27 15:26:25
Done.
| |
| 319 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = | |
| 320 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); | |
| 321 | |
| 322 // set the depth map canvas to have the light we're drawing. | |
| 323 SkLights::Builder builder; | |
| 324 builder.add(fLights->light(i)); | |
| 325 sk_sp<SkLights> curLight = builder.finish(); | |
| 326 | |
| 327 depthMapCanvas->setLights(curLight); | |
| 328 depthMapCanvas->drawPicture(pic); | |
| 329 | |
| 330 fLights->light(i).setShadowMap(surf->makeImageSnapshot()); | |
| 331 } | |
| 332 | |
|
robertphillips
2016/07/26 16:15:05
// TODO: plumb down each primitive's Z so we don't
vjiaoblack
2016/07/27 15:26:25
Done.
| |
| 333 sk_sp<SkImage> povDepthMap; | |
| 334 sk_sp<SkImage> diffuseMap; | |
| 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 | |
| 347 sk_sp<SkSurface> surf(SPFCanvas->makeSurface(info)); | |
|
robertphillips
2016/07/26 16:15:04
// Wrap ...
vjiaoblack
2016/07/27 15:26:26
Done.
| |
| 348 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = | |
| 349 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); | |
| 350 | |
| 351 // set the depth map canvas to have the light we're drawing. | |
|
robertphillips
2016/07/26 16:15:05
How does curLight differ from povLight ?
vjiaoblack
2016/07/27 15:26:27
Done.
| |
| 352 sk_sp<SkLights> curLight = builder.finish(); | |
| 353 depthMapCanvas->setLights(curLight); | |
| 354 | |
| 355 depthMapCanvas->drawPicture(pic); | |
| 356 | |
| 357 povDepthMap = surf->makeImageSnapshot(); | |
| 358 } | |
| 359 | |
| 360 // diffuseMap | |
| 361 { | |
| 362 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, | |
| 363 kBGRA_8888_SkColorType, | |
| 364 kOpaque_SkAlphaType); | |
| 365 | |
| 366 sk_sp<SkSurface> surf(SPFCanvas->makeSurface(info)); | |
| 367 surf->getCanvas()->drawPicture(pic); | |
| 368 | |
| 369 diffuseMap = surf->makeImageSnapshot(); | |
| 370 } | |
| 371 | |
| 372 sk_sp<SkShader> shadowShader = makeShadowShader(povDepthMap, diffuseMap, fLights); | |
| 373 SkPaint paint; | |
| 374 paint.setShader(shadowShader); | |
| 375 | |
| 376 canvas->drawRect(SkRect::MakeIWH(400, 400), paint); | |
|
jvanverth1
2016/07/26 15:01:24
kWidth, kHeight?
vjiaoblack
2016/07/27 15:26:26
Done.
| |
| 377 } | |
| 378 | |
| 379 private: | |
| 380 sk_sp<SkLights> fLights; | |
| 381 | |
| 382 typedef GM INHERITED; | |
| 383 }; | |
| 384 | |
| 385 ////////////////////////////////////////////////////////////////////////////// | |
| 386 | |
| 387 DEF_GM(return new VictorShadowsGM;) | |
| 388 } | |
| OLD | NEW |