| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 | 8 |
| 9 #include "gm.h" | 9 #include "gm.h" |
| 10 #include "SkPaintFilterCanvas.h" | |
| 11 #include "SkPathEffect.h" | 10 #include "SkPathEffect.h" |
| 12 #include "SkPictureRecorder.h" | 11 #include "SkPictureRecorder.h" |
| 12 #include "SkShadowPaintFilterCanvas.h" |
| 13 #include "SkShadowShader.h" | 13 #include "SkShadowShader.h" |
| 14 #include "SkSurface.h" | 14 #include "SkSurface.h" |
| 15 | 15 |
| 16 #ifdef SK_EXPERIMENTAL_SHADOWING | 16 #ifdef SK_EXPERIMENTAL_SHADOWING |
| 17 | 17 |
| 18 static sk_sp<SkShader> make_shadow_shader(sk_sp<SkImage> povDepth, | 18 static sk_sp<SkShader> make_shadow_shader(sk_sp<SkImage> povDepth, |
| 19 sk_sp<SkImage> diffuse, | 19 sk_sp<SkImage> diffuse, |
| 20 sk_sp<SkLights> lights) { | 20 sk_sp<SkLights> lights) { |
| 21 | 21 |
| 22 sk_sp<SkShader> povDepthShader = povDepth->makeShader(SkShader::kClamp_TileM
ode, | 22 sk_sp<SkShader> povDepthShader = povDepth->makeShader(SkShader::kClamp_TileM
ode, |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 canvas->translateZ(80); | 65 canvas->translateZ(80); |
| 66 canvas->drawRect(SkRect::MakeLTRB(100,100,250,250), paint); | 66 canvas->drawRect(SkRect::MakeLTRB(100,100,250,250), paint); |
| 67 // TODO: Add an assert that Z order matches painter's order | 67 // TODO: Add an assert that Z order matches painter's order |
| 68 // TODO: think about if the Z-order always matching painting order is too st
rict | 68 // TODO: think about if the Z-order always matching painting order is too st
rict |
| 69 | 69 |
| 70 return recorder.finishRecordingAsPicture(); | 70 return recorder.finishRecordingAsPicture(); |
| 71 } | 71 } |
| 72 | 72 |
| 73 namespace skiagm { | 73 namespace skiagm { |
| 74 | 74 |
| 75 /* We override the onFilter method to draw depths into the canvas | |
| 76 * depending on the current draw depth of the canvas, throwing out | |
| 77 * the actual draw color. | |
| 78 */ | |
| 79 class SkShadowPaintFilterCanvas : public SkPaintFilterCanvas { | |
| 80 public: | |
| 81 | |
| 82 SkShadowPaintFilterCanvas(SkCanvas* canvas) : INHERITED(canvas) { } | |
| 83 | |
| 84 // TODO use a shader instead | |
| 85 bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type type) const override
{ | |
| 86 if (*paint) { | |
| 87 int z = this->getZ(); | |
| 88 SkASSERT(z <= 0xFF && z >= 0x00); | |
| 89 | |
| 90 SkPaint newPaint; | |
| 91 newPaint.setPathEffect(sk_ref_sp<SkPathEffect>((*paint)->getPathEffe
ct())); | |
| 92 | |
| 93 SkColor color = 0xFF000000; // init color to opaque black | |
| 94 color |= z; // Put the index into the blue component | |
| 95 newPaint.setColor(color); | |
| 96 | |
| 97 *paint->writable() = newPaint; | |
| 98 } | |
| 99 | |
| 100 return true; | |
| 101 } | |
| 102 | |
| 103 void onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, const S
kPaint* paint) { | |
| 104 SkTCopyOnFirstWrite<SkPaint> filteredPaint(paint); | |
| 105 if (this->onFilter(&filteredPaint, kPicture_Type)) { | |
| 106 // we directly call SkCanvas's onDrawPicture because calling the one | |
| 107 // that INHERITED has (SkPaintFilterCanvas) leads to wrong behavior | |
| 108 this->SkCanvas::onDrawPicture(picture, matrix, filteredPaint); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 void updateMatrix() { | |
| 113 this->save(); | |
| 114 | |
| 115 // When we use the SkShadowPaintFilterCanvas, we can only render | |
| 116 // one depth map at a time. Thus, we leave it up to the user to | |
| 117 // set SkLights to only contain (or contain at the first position) | |
| 118 // the light they intend to use for the current depth rendering. | |
| 119 | |
| 120 if (fLights->numLights() > 0 && | |
| 121 this->fLights->light(0).type() == SkLights::Light::kDirectional_Ligh
tType) { | |
| 122 SkVector3 lightDir = this->fLights->light(0).dir(); | |
| 123 SkScalar x = lightDir.fX * this->getZ(); | |
| 124 SkScalar y = lightDir.fY * this->getZ(); | |
| 125 | |
| 126 this->translate(x, y); | |
| 127 } | |
| 128 | |
| 129 } | |
| 130 | |
| 131 void onDrawPaint(const SkPaint& paint) override { | |
| 132 this->updateMatrix(); | |
| 133 this->INHERITED::onDrawPaint(paint); | |
| 134 this->restore(); | |
| 135 } | |
| 136 | |
| 137 void onDrawPoints(PointMode mode, size_t count, const SkPoint pts[], | |
| 138 const SkPaint& paint) override { | |
| 139 this->updateMatrix(); | |
| 140 this->INHERITED::onDrawPoints(mode, count, pts, paint); | |
| 141 this->restore(); | |
| 142 } | |
| 143 | |
| 144 void onDrawRect(const SkRect& rect, const SkPaint& paint) override { | |
| 145 this->updateMatrix(); | |
| 146 this->INHERITED::onDrawRect(rect, paint); | |
| 147 this->restore(); | |
| 148 } | |
| 149 | |
| 150 void onDrawRRect(const SkRRect& rrect, const SkPaint& paint) override { | |
| 151 this->updateMatrix(); | |
| 152 this->INHERITED::onDrawRRect(rrect, paint); | |
| 153 this->restore(); | |
| 154 } | |
| 155 | |
| 156 void onDrawDRRect(const SkRRect& outer, const SkRRect& inner, | |
| 157 const SkPaint& paint) override { | |
| 158 this->updateMatrix(); | |
| 159 this->INHERITED::onDrawDRRect(outer, inner, paint); | |
| 160 this->restore(); | |
| 161 } | |
| 162 | |
| 163 void onDrawOval(const SkRect& rect, const SkPaint& paint) override { | |
| 164 this->updateMatrix(); | |
| 165 this->INHERITED::onDrawOval(rect, paint); | |
| 166 this->restore(); | |
| 167 } | |
| 168 | |
| 169 void onDrawPath(const SkPath& path, const SkPaint& paint) override { | |
| 170 this->updateMatrix(); | |
| 171 this->INHERITED::onDrawPath(path, paint); | |
| 172 this->restore(); | |
| 173 } | |
| 174 | |
| 175 void onDrawBitmap(const SkBitmap& bm, SkScalar left, SkScalar top, | |
| 176 const SkPaint* paint) override { | |
| 177 this->updateMatrix(); | |
| 178 this->INHERITED::onDrawBitmap(bm, left, top, paint); | |
| 179 this->restore(); | |
| 180 } | |
| 181 | |
| 182 void onDrawBitmapRect(const SkBitmap& bm, const SkRect* src, const SkRect& d
st, | |
| 183 const SkPaint* paint, SrcRectConstraint constraint) ov
erride { | |
| 184 this->updateMatrix(); | |
| 185 this->INHERITED::onDrawBitmapRect(bm, src, dst, paint, constraint); | |
| 186 this->restore(); | |
| 187 } | |
| 188 | |
| 189 void onDrawBitmapNine(const SkBitmap& bm, const SkIRect& center, | |
| 190 const SkRect& dst, const SkPaint* paint) { | |
| 191 this->updateMatrix(); | |
| 192 this->INHERITED::onDrawBitmapNine(bm, center, dst, paint); | |
| 193 this->restore(); | |
| 194 } | |
| 195 | |
| 196 void onDrawImage(const SkImage* image, SkScalar left, SkScalar top, | |
| 197 const SkPaint* paint) override { | |
| 198 this->updateMatrix(); | |
| 199 this->INHERITED::onDrawImage(image, left, top, paint); | |
| 200 this->restore(); | |
| 201 } | |
| 202 | |
| 203 void onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect&
dst, | |
| 204 const SkPaint* paint, SrcRectConstraint constraint) ove
rride { | |
| 205 this->updateMatrix(); | |
| 206 this->INHERITED::onDrawImageRect(image, src, dst, paint, constraint); | |
| 207 this->restore(); | |
| 208 } | |
| 209 | |
| 210 void onDrawImageNine(const SkImage* image, const SkIRect& center, | |
| 211 const SkRect& dst, const SkPaint* paint) { | |
| 212 this->updateMatrix(); | |
| 213 this->INHERITED::onDrawImageNine(image, center, dst, paint); | |
| 214 this->restore(); | |
| 215 } | |
| 216 | |
| 217 void onDrawVertices(VertexMode vmode, int vertexCount, const SkPoint vertice
s[], | |
| 218 const SkPoint texs[], const SkColor colors[], SkXfermode
* xmode, | |
| 219 const uint16_t indices[], int indexCount, const SkPaint&
paint) override { | |
| 220 this->updateMatrix(); | |
| 221 this->INHERITED::onDrawVertices(vmode, vertexCount, vertices, texs, colo
rs, | |
| 222 xmode, indices, indexCount, paint); | |
| 223 this->restore(); | |
| 224 } | |
| 225 | |
| 226 void onDrawPatch(const SkPoint cubics[], const SkColor colors[], const SkPoi
nt texCoords[], | |
| 227 SkXfermode* xmode, const SkPaint& paint) override { | |
| 228 this->updateMatrix(); | |
| 229 this->INHERITED::onDrawPatch(cubics, colors, texCoords, xmode, paint); | |
| 230 this->restore(); | |
| 231 } | |
| 232 | |
| 233 void onDrawText(const void* text, size_t byteLength, | |
| 234 SkScalar x, SkScalar y, const SkPaint& paint) override { | |
| 235 this->updateMatrix(); | |
| 236 this->INHERITED::onDrawText(text, byteLength, x, y, paint); | |
| 237 this->restore(); | |
| 238 } | |
| 239 | |
| 240 void onDrawPosText(const void* text, size_t byteLength, | |
| 241 const SkPoint pos[], const SkPaint& paint) override { | |
| 242 this->updateMatrix(); | |
| 243 this->INHERITED::onDrawPosText(text, byteLength, pos, paint); | |
| 244 this->restore(); | |
| 245 } | |
| 246 | |
| 247 void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos
[], | |
| 248 SkScalar constY, const SkPaint& paint) override { | |
| 249 this->updateMatrix(); | |
| 250 this->INHERITED::onDrawPosTextH(text, byteLength, xpos, constY, paint); | |
| 251 this->restore(); | |
| 252 } | |
| 253 | |
| 254 void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& pat
h, | |
| 255 const SkMatrix* matrix, const SkPaint& paint) override
{ | |
| 256 this->updateMatrix(); | |
| 257 this->INHERITED::onDrawTextOnPath(text, byteLength, path, matrix, paint)
; | |
| 258 this->restore(); | |
| 259 } | |
| 260 | |
| 261 void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform
xform[], | |
| 262 const SkRect* cull, const SkPaint& paint) override { | |
| 263 this->updateMatrix(); | |
| 264 this->INHERITED::onDrawTextRSXform(text, byteLength, xform, cull, paint)
; | |
| 265 this->restore(); | |
| 266 } | |
| 267 | |
| 268 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, | |
| 269 const SkPaint& paint) override { | |
| 270 this->updateMatrix(); | |
| 271 this->INHERITED::onDrawTextBlob(blob, x, y, paint); | |
| 272 this->restore(); | |
| 273 } | |
| 274 | |
| 275 private: | |
| 276 typedef SkPaintFilterCanvas INHERITED; | |
| 277 }; | |
| 278 | |
| 279 class ShadowMapsGM : public GM { | 75 class ShadowMapsGM : public GM { |
| 280 public: | 76 public: |
| 281 ShadowMapsGM() { | 77 ShadowMapsGM() { |
| 282 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); | 78 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); |
| 283 } | 79 } |
| 284 | 80 |
| 285 void onOnceBeforeDraw() override { | 81 void onOnceBeforeDraw() override { |
| 286 // Create a light set consisting of | 82 // Create a light set consisting of |
| 287 // - bluish directional light pointing more right than down | 83 // - bluish directional light pointing more right than down |
| 288 // - reddish directional light pointing more down than right | 84 // - reddish directional light pointing more down than right |
| (...skipping 26 matching lines...) Expand all Loading... |
| 315 sk_sp<SkPicture> pic(make_test_picture(kWidth, kHeight)); | 111 sk_sp<SkPicture> pic(make_test_picture(kWidth, kHeight)); |
| 316 | 112 |
| 317 for (int i = 0; i < fLights->numLights(); ++i) { | 113 for (int i = 0; i < fLights->numLights(); ++i) { |
| 318 // skip over ambient lights; they don't cast shadows | 114 // skip over ambient lights; they don't cast shadows |
| 319 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type())
{ | 115 if (SkLights::Light::kAmbient_LightType == fLights->light(i).type())
{ |
| 320 continue; | 116 continue; |
| 321 } | 117 } |
| 322 // TODO: compute the correct size of the depth map from the light pr
operties | 118 // TODO: compute the correct size of the depth map from the light pr
operties |
| 323 // TODO: maybe add a kDepth_8_SkColorType | 119 // TODO: maybe add a kDepth_8_SkColorType |
| 324 | 120 |
| 325 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, | 121 // TODO: find actual max depth of picture |
| 122 SkISize shMapSize = SkShadowPaintFilterCanvas:: |
| 123 ComputeDepthMapSize(fLights->light(i), 255, kWidth, kHeight)
; |
| 124 |
| 125 SkImageInfo info = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHe
ight, |
| 326 kBGRA_8888_SkColorType, | 126 kBGRA_8888_SkColorType, |
| 327 kOpaque_SkAlphaType); | 127 kOpaque_SkAlphaType); |
| 328 | 128 |
| 329 // Create a new surface (that matches the backend of canvas) | 129 // Create a new surface (that matches the backend of canvas) |
| 330 // for each shadow map | 130 // for each shadow map |
| 331 sk_sp<SkSurface> surf(canvas->makeSurface(info)); | 131 sk_sp<SkSurface> surf(canvas->makeSurface(info)); |
| 332 | 132 |
| 333 // Wrap another SPFCanvas around the surface | 133 // Wrap another SPFCanvas around the surface |
| 334 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = | 134 sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = |
| 335 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); | 135 sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 | 201 |
| 402 typedef GM INHERITED; | 202 typedef GM INHERITED; |
| 403 }; | 203 }; |
| 404 | 204 |
| 405 ////////////////////////////////////////////////////////////////////////////// | 205 ////////////////////////////////////////////////////////////////////////////// |
| 406 | 206 |
| 407 DEF_GM(return new ShadowMapsGM;) | 207 DEF_GM(return new ShadowMapsGM;) |
| 408 } | 208 } |
| 409 | 209 |
| 410 #endif | 210 #endif |
| OLD | NEW |