Index: include/utils/SkShadowPaintFilterCanvas.h |
diff --git a/include/utils/SkShadowPaintFilterCanvas.h b/include/utils/SkShadowPaintFilterCanvas.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a242f2e2c7f7506276899f68db94751cf5b16790 |
--- /dev/null |
+++ b/include/utils/SkShadowPaintFilterCanvas.h |
@@ -0,0 +1,230 @@ |
+/* |
+ * Copyright 2016 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#ifndef SkShadowPaintFilterCanvas_DEFINED |
+#define SkShadowPaintFilterCanvas_DEFINED |
jvanverth1
2016/08/02 17:43:33
Does this need to be inside an #ifdef SK_EXPERIMEN
vjiaoblack
2016/08/03 14:04:21
Hmm... I guess it would make sense for it to be
|
+ |
+#include "SkNWayCanvas.h" |
+#include "SkTLazy.h" |
+#include "SkLights.h" |
+#include "SkPaintFilterCanvas.h" |
+#include "SkCanvas.h" |
+ |
+/** \class SkPaintFilterCanvas |
+ |
+ A utility proxy base class for implementing shadow maps. |
+*/ |
+ |
+/* We override the onFilter method to draw depths into the canvas |
+ * depending on the current draw depth of the canvas, throwing out |
+ * the actual draw color. |
+ */ |
+class SkShadowPaintFilterCanvas : public SkPaintFilterCanvas { |
+public: |
+ |
+ sk_sp<SkLights> getLights() { |
+ return INHERITED::getLights(); |
+ } |
+ |
robertphillips
2016/08/02 16:05:06
Will this fit on one line?
vjiaoblack
2016/08/03 14:04:21
Yeah, but I moved some of the code around and this
|
+ SkShadowPaintFilterCanvas(SkCanvas *canvas) |
+ : SkPaintFilterCanvas(canvas) { } |
+ |
+ // TODO use a shader instead |
+ bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type type) const override { |
+ 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 onDrawPicture(const SkPicture *picture, const SkMatrix *matrix, const SkPaint *paint) { |
+ SkTCopyOnFirstWrite<SkPaint> filteredPaint(paint); |
+ if (onFilter(&filteredPaint, kPicture_Type)) |
+ SkCanvas::onDrawPicture(picture, matrix, filteredPaint); |
+ } |
+ |
+ void updateMatrix() { |
+ save(); |
+ |
robertphillips
2016/08/02 16:05:06
Shouldn't this now read something like:
// The us
vjiaoblack
2016/08/03 14:04:21
Done.
|
+ // TODO which light do we set? |
+ if (this->fLights->light(0).type() != SkLights::Light::kAmbient_LightType) { |
+ SkVector3 lightDir = this->fLights->light(0).dir(); |
+ SkScalar x = lightDir.fX * this->getZ(); |
+ SkScalar y = lightDir.fY * this->getZ(); |
+ |
+ this->translate(x, y); |
+ } |
+ |
+ } |
+ |
robertphillips
2016/08/02 16:05:06
What happened to all the overrides?
vjiaoblack
2016/08/03 14:04:21
Currently, I can't have them. (after the refactori
|
+ void onDrawPaint(const SkPaint &paint) { |
+ updateMatrix(); |
+ this->INHERITED::onDrawPaint(paint); |
+ restore(); |
+ } |
+ |
+ void onDrawPoints(PointMode mode, size_t count, const SkPoint pts[], |
+ const SkPaint &paint) { |
+ updateMatrix(); |
+ this->INHERITED::onDrawPoints(mode, count, pts, paint); |
+ restore(); |
+ } |
+ |
+ void onDrawRect(const SkRect &rect, const SkPaint &paint) { |
+ updateMatrix(); |
+ this->INHERITED::onDrawRect(rect, paint); |
+ restore(); |
+ } |
+ |
+ void onDrawRRect(const SkRRect &rrect, const SkPaint &paint) { |
+ updateMatrix(); |
+ this->INHERITED::onDrawRRect(rrect, paint); |
+ restore(); |
+ } |
+ |
+ void onDrawDRRect(const SkRRect &outer, const SkRRect &inner, |
+ const SkPaint &paint) { |
+ updateMatrix(); |
+ this->INHERITED::onDrawDRRect(outer, inner, paint); |
+ restore(); |
+ } |
+ |
+ void onDrawOval(const SkRect &rect, const SkPaint &paint) { |
+ updateMatrix(); |
+ this->INHERITED::onDrawOval(rect, paint); |
+ restore(); |
+ } |
+ |
+ void onDrawPath(const SkPath &path, const SkPaint &paint) { |
+ updateMatrix(); |
+ this->INHERITED::onDrawPath(path, paint); |
+ restore(); |
+ } |
+ |
+ void onDrawBitmap(const SkBitmap &bm, SkScalar left, SkScalar top, |
+ const SkPaint *paint) { |
+ updateMatrix(); |
+ this->INHERITED::onDrawBitmap(bm, left, top, paint); |
+ restore(); |
+ } |
+ |
+ void 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 onDrawBitmapNine(const SkBitmap &bm, const SkIRect ¢er, |
+ const SkRect &dst, const SkPaint *paint) { |
+ updateMatrix(); |
+ this->INHERITED::onDrawBitmapNine(bm, center, dst, paint); |
+ restore(); |
+ } |
+ |
+ void onDrawImage(const SkImage *image, SkScalar left, SkScalar top, |
+ const SkPaint *paint) { |
+ updateMatrix(); |
+ this->INHERITED::onDrawImage(image, left, top, paint); |
+ restore(); |
+ } |
+ |
+ void 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 onDrawImageNine(const SkImage *image, const SkIRect ¢er, |
+ const SkRect &dst, const SkPaint *paint) { |
+ updateMatrix(); |
+ this->INHERITED::onDrawImageNine(image, center, dst, paint); |
+ restore(); |
+ } |
+ |
+ |
+ void 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 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 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 onDrawPosText(const void *text, size_t byteLength, const SkPoint pos[], |
+ const SkPaint &paint) { |
+ updateMatrix(); |
+ this->INHERITED::onDrawPosText(text, byteLength, pos, paint); |
+ restore(); |
+ } |
+ |
+ void 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 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 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 onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint) { |
+ updateMatrix(); |
+ this->INHERITED::onDrawTextBlob(blob, x, y, paint); |
+ restore(); |
+ } |
+ |
+private: |
+ typedef SkPaintFilterCanvas INHERITED; |
robertphillips
2016/08/02 16:05:06
extra '\n' here ?
vjiaoblack
2016/08/03 14:04:21
Done.
|
+ |
+}; |
+ |
+ |
+#endif |