Chromium Code Reviews| Index: src/utils/SkShadowPaintFilterCanvas.cpp |
| diff --git a/src/utils/SkShadowPaintFilterCanvas.cpp b/src/utils/SkShadowPaintFilterCanvas.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7ed491a4fc84919aaa471b8d1d706ea348adb3b5 |
| --- /dev/null |
| +++ b/src/utils/SkShadowPaintFilterCanvas.cpp |
| @@ -0,0 +1,212 @@ |
| +/* |
| + * Copyright 2016 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
|
robertphillips
2016/08/03 18:47:48
I think we only need the "SkShadowPaintFilterCanva
vjiaoblack
2016/08/04 13:36:22
Done.
|
| +#include "SkCanvas.h" |
| +#include "SkNWayCanvas.h" |
| +#include "SkPaintFilterCanvas.h" |
| +#include "SkShadowPaintFilterCanvas.h" |
| + |
| +#ifdef SK_EXPERIMENTAL_SHADOWING |
| + |
| +SkShadowPaintFilterCanvas::SkShadowPaintFilterCanvas(SkCanvas *canvas) |
| + : SkPaintFilterCanvas(canvas) { } |
| + |
| +// TODO use a shader instead |
| +bool SkShadowPaintFilterCanvas::onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type type) const { |
| + if (*paint) { |
| + int z = this->getZ(); |
| + SkASSERT(z <= 0xFF && z >= 0x00); |
| + |
| + SkPaint newPaint; |
| + newPaint.setPathEffect(sk_ref_sp<SkPathEffect>((*paint)->getPathEffect())); |
| + |
| + SkColor color = 0xFF000000; // init color to opaque black |
| + color |= z; // Put the index into the blue component |
| + newPaint.setColor(color); |
| + |
| + *paint->writable() = newPaint; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::onDrawPicture(const SkPicture *picture, const SkMatrix *matrix, |
| + const SkPaint *paint) { |
| + SkTCopyOnFirstWrite<SkPaint> filteredPaint(paint); |
| + if (onFilter(&filteredPaint, kPicture_Type)) |
|
robertphillips
2016/08/03 18:47:48
Add brackets around this call
vjiaoblack
2016/08/04 13:36:22
Done.
|
| + SkCanvas::onDrawPicture(picture, matrix, filteredPaint); |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::updateMatrix() { |
| + 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::kAmbient_LightType) { |
|
robertphillips
2016/08/03 18:47:48
const SkVector3& lightDir = ... ?
vjiaoblack
2016/08/04 13:36:22
Done.
vjiaoblack
2016/08/04 18:03:58
Done.
|
| + SkVector3 lightDir = this->fLights->light(0).dir(); |
| + SkScalar x = lightDir.fX * this->getZ(); |
| + SkScalar y = lightDir.fY * this->getZ(); |
| + |
| + this->translate(x, y); |
| + } |
| + |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::onDrawPaint(const SkPaint &paint) { |
|
robertphillips
2016/08/03 18:47:48
add this-> to member function calls here and elsew
vjiaoblack
2016/08/04 13:36:22
Done.
vjiaoblack
2016/08/04 18:03:58
Done.
|
| + updateMatrix(); |
| + this->INHERITED::onDrawPaint(paint); |
| + restore(); |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[], |
| + const SkPaint &paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawPoints(mode, count, pts, paint); |
| + restore(); |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::onDrawRect(const SkRect &rect, const SkPaint &paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawRect(rect, paint); |
| + restore(); |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::onDrawRRect(const SkRRect &rrect, const SkPaint &paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawRRect(rrect, paint); |
| + restore(); |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::onDrawDRRect(const SkRRect &outer, const SkRRect &inner, |
| + const SkPaint &paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawDRRect(outer, inner, paint); |
| + restore(); |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::onDrawOval(const SkRect &rect, const SkPaint &paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawOval(rect, paint); |
| + restore(); |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::onDrawPath(const SkPath &path, const SkPaint &paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawPath(path, paint); |
| + restore(); |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::onDrawBitmap(const SkBitmap &bm, SkScalar left, SkScalar top, |
| + const SkPaint *paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawBitmap(bm, left, top, paint); |
| + restore(); |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::onDrawBitmapRect(const SkBitmap &bm, const SkRect *src, |
| + const SkRect &dst, const SkPaint *paint, |
| + SrcRectConstraint constraint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawBitmapRect(bm, src, dst, paint, constraint); |
| + restore(); |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::onDrawBitmapNine(const SkBitmap &bm, const SkIRect ¢er, |
| + const SkRect &dst, const SkPaint *paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawBitmapNine(bm, center, dst, paint); |
| + restore(); |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::onDrawImage(const SkImage *image, SkScalar left, |
| + SkScalar top, const SkPaint *paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawImage(image, left, top, paint); |
| + restore(); |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::onDrawImageRect(const SkImage *image, const SkRect *src, |
| + const SkRect &dst, const SkPaint *paint, |
| + SrcRectConstraint constraint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawImageRect(image, src, dst, paint, constraint); |
| + restore(); |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::onDrawImageNine(const SkImage *image, const SkIRect ¢er, |
| + const SkRect &dst, const SkPaint *paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawImageNine(image, center, dst, paint); |
| + restore(); |
| +} |
| + |
| + |
| +void SkShadowPaintFilterCanvas::onDrawVertices(VertexMode vmode, int vertexCount, |
| + const SkPoint vertices[], const SkPoint texs[], |
| + const SkColor colors[], SkXfermode *xmode, |
| + const uint16_t indices[], int indexCount, |
| + const SkPaint &paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawVertices(vmode, vertexCount, vertices, texs, colors, |
| + xmode, indices, indexCount, paint); |
| + restore(); |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::onDrawPatch(const SkPoint cubics[], const SkColor colors[], |
| + const SkPoint texCoords[], SkXfermode *xmode, |
| + const SkPaint &paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawPatch(cubics, colors, texCoords, xmode, paint); |
| + restore(); |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::onDrawText(const void *text, size_t byteLength, SkScalar x, |
| + SkScalar y, const SkPaint &paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawText(text, byteLength, x, y, paint); |
| + restore(); |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::onDrawPosText(const void *text, size_t byteLength, |
| + const SkPoint pos[], const SkPaint &paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawPosText(text, byteLength, pos, paint); |
| + restore(); |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::onDrawPosTextH(const void *text, size_t byteLength, |
| + const SkScalar xpos[], |
| + SkScalar constY, const SkPaint &paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawPosTextH(text, byteLength, xpos, constY, paint); |
| + restore(); |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::onDrawTextOnPath(const void *text, size_t byteLength, |
| + const SkPath &path, const SkMatrix *matrix, |
| + const SkPaint &paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawTextOnPath(text, byteLength, path, matrix, paint); |
| + restore(); |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::onDrawTextRSXform(const void *text, size_t byteLength, |
| + const SkRSXform xform[], const SkRect *cull, |
| + const SkPaint &paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawTextRSXform(text, byteLength, xform, cull, paint); |
| + restore(); |
| +} |
| + |
| +void SkShadowPaintFilterCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, |
| + const SkPaint &paint) { |
| + updateMatrix(); |
| + this->INHERITED::onDrawTextBlob(blob, x, y, paint); |
| + restore(); |
| +} |
| + |
| +#endif |