Chromium Code Reviews| Index: src/utils/SkShadowPaintFilterCanvas.cpp |
| diff --git a/src/utils/SkShadowPaintFilterCanvas.cpp b/src/utils/SkShadowPaintFilterCanvas.cpp |
| index ef29c3709d95ba7a45c413aa922544ed2d5ff971..9069e1770417c729012c677c1ea2598e77209a03 100644 |
| --- a/src/utils/SkShadowPaintFilterCanvas.cpp |
| +++ b/src/utils/SkShadowPaintFilterCanvas.cpp |
| @@ -51,7 +51,7 @@ bool SkShadowPaintFilterCanvas::onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Ty |
| SkISize SkShadowPaintFilterCanvas::ComputeDepthMapSize(const SkLights::Light& light, int maxDepth, |
| int width, int height) { |
| if (light.type() != SkLights::Light::kDirectional_LightType) { |
|
robertphillips
2016/09/02 16:23:46
Just 4 - you want it to remain an int
vjiaoblack
2016/09/02 17:22:00
Done.
|
| - return SkISize::Make(width *2 , height * 2); |
| + return SkISize::Make(width * 4.0f, height * 4.0f); |
| } |
| int dMapWidth = SkMin32(maxDepth * fabs(light.dir().fX) + width, |
| @@ -74,8 +74,6 @@ void SkShadowPaintFilterCanvas::onDrawPicture(const SkPicture *picture, const Sk |
| } |
| void SkShadowPaintFilterCanvas::updateMatrix() { |
| - this->save(); |
| - |
| // It is up to the user to set the 0th light in fLights to |
| // the light the want to render the depth map with. |
| if (this->fLights->light(0).type() == SkLights::Light::kDirectional_LightType) { |
| @@ -84,10 +82,42 @@ void SkShadowPaintFilterCanvas::updateMatrix() { |
| SkScalar y = lightDir.fY * this->getZ(); |
| this->translate(x, y); |
| + } else if (this->fLights->light(0).type() == SkLights::Light::kPoint_LightType) { |
| + SkISize size = this->getBaseLayerSize(); |
| + |
| + SkPoint3 lightPos = this->fLights->light(0).pos(); |
| + lightPos.set(lightPos.fX, lightPos.fY, lightPos.fZ); |
| + |
| + // shadow maps for point lights are 4x the size of the diffuse map |
| + SkScalar diffuseHeight = size.fHeight / 4.0f; |
| + |
| + // move point light with canvas's CTM |
| + SkPoint lightPoint = SkPoint::Make(lightPos.fX, diffuseHeight - lightPos.fY); |
| + SkMatrix mat = this->getTotalMatrix(); |
| + if (mat.invert(&mat)) { |
| + mat.mapPoints(&lightPoint, 1); |
| + } |
| + lightPoint.set(lightPoint.fX, diffuseHeight - lightPoint.fY); |
| + |
| + // center the shadow map |
| + // note: the 3/8 constant is specific to the 4.0 depth map size multiplier |
| + mat = this->getTotalMatrix(); |
| + mat.postTranslate(size.width() * 0.375f, size.height() * 0.375f); |
| + this->setMatrix(mat); |
| + |
| + // project shapes onto canvas as shadows |
| + SkScalar scale = (lightPos.fZ) / (lightPos.fZ - this->getZ()); |
| + this->scale(scale, scale); |
| + |
| + this->translate(-lightPoint.fX * this->getZ() / |
| + ((lightPos.fZ - this->getZ()) * scale), |
| + -(diffuseHeight - lightPoint.fY) * this->getZ() / |
| + ((lightPos.fZ - this->getZ()) * scale)); |
| } |
| } |
| void SkShadowPaintFilterCanvas::onDrawPaint(const SkPaint &paint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawPaint(paint); |
| this->restore(); |
| @@ -95,18 +125,21 @@ void SkShadowPaintFilterCanvas::onDrawPaint(const SkPaint &paint) { |
| void SkShadowPaintFilterCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[], |
| const SkPaint &paint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawPoints(mode, count, pts, paint); |
| this->restore(); |
| } |
| void SkShadowPaintFilterCanvas::onDrawRect(const SkRect &rect, const SkPaint &paint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawRect(rect, paint); |
| this->restore(); |
| } |
| void SkShadowPaintFilterCanvas::onDrawRRect(const SkRRect &rrect, const SkPaint &paint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawRRect(rrect, paint); |
| this->restore(); |
| @@ -114,12 +147,14 @@ void SkShadowPaintFilterCanvas::onDrawRRect(const SkRRect &rrect, const SkPaint |
| void SkShadowPaintFilterCanvas::onDrawDRRect(const SkRRect &outer, const SkRRect &inner, |
| const SkPaint &paint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawDRRect(outer, inner, paint); |
| this->restore(); |
| } |
| void SkShadowPaintFilterCanvas::onDrawOval(const SkRect &rect, const SkPaint &paint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawOval(rect, paint); |
| this->restore(); |
| @@ -128,12 +163,14 @@ void SkShadowPaintFilterCanvas::onDrawOval(const SkRect &rect, const SkPaint &pa |
| void SkShadowPaintFilterCanvas::onDrawArc(const SkRect &rect, SkScalar startAngle, |
| SkScalar sweepAngle, bool useCenter, |
| const SkPaint &paint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawArc(rect, startAngle, sweepAngle, useCenter, paint); |
| this->restore(); |
| } |
| void SkShadowPaintFilterCanvas::onDrawPath(const SkPath &path, const SkPaint &paint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawPath(path, paint); |
| this->restore(); |
| @@ -141,6 +178,7 @@ void SkShadowPaintFilterCanvas::onDrawPath(const SkPath &path, const SkPaint &pa |
| void SkShadowPaintFilterCanvas::onDrawBitmap(const SkBitmap &bm, SkScalar left, SkScalar top, |
| const SkPaint *paint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawBitmap(bm, left, top, paint); |
| this->restore(); |
| @@ -149,6 +187,7 @@ void SkShadowPaintFilterCanvas::onDrawBitmap(const SkBitmap &bm, SkScalar left, |
| void SkShadowPaintFilterCanvas::onDrawBitmapRect(const SkBitmap &bm, const SkRect *src, |
| const SkRect &dst, const SkPaint *paint, |
| SrcRectConstraint constraint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawBitmapRect(bm, src, dst, paint, constraint); |
| this->restore(); |
| @@ -156,6 +195,7 @@ void SkShadowPaintFilterCanvas::onDrawBitmapRect(const SkBitmap &bm, const SkRec |
| void SkShadowPaintFilterCanvas::onDrawBitmapNine(const SkBitmap &bm, const SkIRect ¢er, |
| const SkRect &dst, const SkPaint *paint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawBitmapNine(bm, center, dst, paint); |
| this->restore(); |
| @@ -163,6 +203,7 @@ void SkShadowPaintFilterCanvas::onDrawBitmapNine(const SkBitmap &bm, const SkIRe |
| void SkShadowPaintFilterCanvas::onDrawImage(const SkImage *image, SkScalar left, |
| SkScalar top, const SkPaint *paint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawImage(image, left, top, paint); |
| this->restore(); |
| @@ -171,6 +212,7 @@ void SkShadowPaintFilterCanvas::onDrawImage(const SkImage *image, SkScalar left, |
| void SkShadowPaintFilterCanvas::onDrawImageRect(const SkImage *image, const SkRect *src, |
| const SkRect &dst, const SkPaint *paint, |
| SrcRectConstraint constraint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawImageRect(image, src, dst, paint, constraint); |
| this->restore(); |
| @@ -178,6 +220,7 @@ void SkShadowPaintFilterCanvas::onDrawImageRect(const SkImage *image, const SkRe |
| void SkShadowPaintFilterCanvas::onDrawImageNine(const SkImage *image, const SkIRect ¢er, |
| const SkRect &dst, const SkPaint *paint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawImageNine(image, center, dst, paint); |
| this->restore(); |
| @@ -189,6 +232,7 @@ void SkShadowPaintFilterCanvas::onDrawVertices(VertexMode vmode, int vertexCount |
| const SkColor colors[], SkXfermode *xmode, |
| const uint16_t indices[], int indexCount, |
| const SkPaint &paint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawVertices(vmode, vertexCount, vertices, texs, colors, |
| xmode, indices, indexCount, paint); |
| @@ -198,6 +242,7 @@ void SkShadowPaintFilterCanvas::onDrawVertices(VertexMode vmode, int vertexCount |
| void SkShadowPaintFilterCanvas::onDrawPatch(const SkPoint cubics[], const SkColor colors[], |
| const SkPoint texCoords[], SkXfermode *xmode, |
| const SkPaint &paint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawPatch(cubics, colors, texCoords, xmode, paint); |
| this->restore(); |
| @@ -205,6 +250,7 @@ void SkShadowPaintFilterCanvas::onDrawPatch(const SkPoint cubics[], const SkColo |
| void SkShadowPaintFilterCanvas::onDrawText(const void *text, size_t byteLength, SkScalar x, |
| SkScalar y, const SkPaint &paint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawText(text, byteLength, x, y, paint); |
| this->restore(); |
| @@ -212,6 +258,7 @@ void SkShadowPaintFilterCanvas::onDrawText(const void *text, size_t byteLength, |
| void SkShadowPaintFilterCanvas::onDrawPosText(const void *text, size_t byteLength, |
| const SkPoint pos[], const SkPaint &paint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawPosText(text, byteLength, pos, paint); |
| this->restore(); |
| @@ -220,6 +267,7 @@ void SkShadowPaintFilterCanvas::onDrawPosText(const void *text, size_t byteLengt |
| void SkShadowPaintFilterCanvas::onDrawPosTextH(const void *text, size_t byteLength, |
| const SkScalar xpos[], |
| SkScalar constY, const SkPaint &paint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawPosTextH(text, byteLength, xpos, constY, paint); |
| this->restore(); |
| @@ -228,6 +276,7 @@ void SkShadowPaintFilterCanvas::onDrawPosTextH(const void *text, size_t byteLeng |
| void SkShadowPaintFilterCanvas::onDrawTextOnPath(const void *text, size_t byteLength, |
| const SkPath &path, const SkMatrix *matrix, |
| const SkPaint &paint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawTextOnPath(text, byteLength, path, matrix, paint); |
| this->restore(); |
| @@ -236,6 +285,7 @@ void SkShadowPaintFilterCanvas::onDrawTextOnPath(const void *text, size_t byteLe |
| void SkShadowPaintFilterCanvas::onDrawTextRSXform(const void *text, size_t byteLength, |
| const SkRSXform xform[], const SkRect *cull, |
| const SkPaint &paint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawTextRSXform(text, byteLength, xform, cull, paint); |
| this->restore(); |
| @@ -243,6 +293,7 @@ void SkShadowPaintFilterCanvas::onDrawTextRSXform(const void *text, size_t byteL |
| void SkShadowPaintFilterCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, |
| const SkPaint &paint) { |
| + this->save(); |
| this->updateMatrix(); |
| this->INHERITED::onDrawTextBlob(blob, x, y, paint); |
| this->restore(); |