From cb60844b34766aad4151df5e87c144d4a57e9abe Mon Sep 17 00:00:00 2001 From: "reed@android.com" Date: Fri, 4 Dec 2009 21:32:27 +0000 Subject: [PATCH] add drawData() to canvas, to record data blobs git-svn-id: http://skia.googlecode.com/svn/trunk@452 2bbb7eff-a529-9590-31e7-b0007b416f81 --- include/core/SkCanvas.h | 8 ++++++++ include/utils/SkDumpCanvas.h | 4 +++- include/utils/SkProxyCanvas.h | 3 ++- samplecode/SamplePicture.cpp | 14 +++++++++++++- src/core/SkCanvas.cpp | 4 ++++ src/core/SkPictureFlat.h | 1 + src/core/SkPicturePlayback.cpp | 5 +++++ src/core/SkPictureRecord.cpp | 6 ++++++ src/core/SkPictureRecord.h | 1 + src/utils/SkDumpCanvas.cpp | 6 ++++++ src/utils/SkProxyCanvas.cpp | 4 ++++ 11 files changed, 53 insertions(+), 3 deletions(-) diff --git a/include/core/SkCanvas.h b/include/core/SkCanvas.h index 8fba6cf..40b5c56 100644 --- a/include/core/SkCanvas.h +++ b/include/core/SkCanvas.h @@ -626,6 +626,14 @@ public: const uint16_t indices[], int indexCount, const SkPaint& paint); + /** Send a blob of data to the canvas. + For canvases that draw, this call is effectively a no-op, as the data + is not parsed, but just ignored. However, this call exists for + subclasses like SkPicture's recording canvas, that can store the data + and then play it back later (via another call to drawData). + */ + virtual void drawData(const void* data, size_t length); + ////////////////////////////////////////////////////////////////////////// /** Get the current bounder object. diff --git a/include/utils/SkDumpCanvas.h b/include/utils/SkDumpCanvas.h index 6f16c92..3731bef 100644 --- a/include/utils/SkDumpCanvas.h +++ b/include/utils/SkDumpCanvas.h @@ -34,7 +34,8 @@ public: kDrawText_Verb, kDrawPicture_Verb, kDrawShape_Verb, - kDrawVertices_Verb + kDrawVertices_Verb, + kDrawData_Verb }; /** Subclasses of this are installed on the DumpCanvas, and then called for @@ -102,6 +103,7 @@ public: const SkColor colors[], SkXfermode* xmode, const uint16_t indices[], int indexCount, const SkPaint& paint); + virtual void drawData(const void*, size_t); private: Dumper* fDumper; diff --git a/include/utils/SkProxyCanvas.h b/include/utils/SkProxyCanvas.h index f2e57ab..082e0fd 100644 --- a/include/utils/SkProxyCanvas.h +++ b/include/utils/SkProxyCanvas.h @@ -75,7 +75,8 @@ public: const SkColor colors[], SkXfermode* xmode, const uint16_t indices[], int indexCount, const SkPaint& paint); - + virtual void drawData(const void* data, size_t length); + virtual SkBounder* setBounder(SkBounder* bounder); virtual SkDrawFilter* setDrawFilter(SkDrawFilter* filter); diff --git a/samplecode/SamplePicture.cpp b/samplecode/SamplePicture.cpp index e1b05ad..e232a50 100644 --- a/samplecode/SamplePicture.cpp +++ b/samplecode/SamplePicture.cpp @@ -1,11 +1,11 @@ #include "SampleCode.h" +#include "SkDumpCanvas.h" #include "SkView.h" #include "SkCanvas.h" #include "Sk64.h" #include "SkGradientShader.h" #include "SkGraphics.h" #include "SkImageDecoder.h" -#include "SkKernel33MaskFilter.h" #include "SkPath.h" #include "SkPicture.h" #include "SkRandom.h" @@ -103,15 +103,21 @@ protected: canvas->drawBitmap(fBitmap, 0, 0, NULL); canvas->restore(); + const char beforeStr[] = "before circle"; + const char afterStr[] = "after circle"; + paint.setAntiAlias(true); paint.setColor(SK_ColorRED); + canvas->drawData(beforeStr, sizeof(beforeStr)); canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50), SkIntToScalar(40), paint); + canvas->drawData(afterStr, sizeof(afterStr)); paint.setColor(SK_ColorBLACK); paint.setTextSize(SkIntToScalar(40)); canvas->drawText("Picture", 7, SkIntToScalar(50), SkIntToScalar(62), paint); + } virtual void onDraw(SkCanvas* canvas) { @@ -145,6 +151,12 @@ protected: canvas->translate(-SkIntToScalar(100), 0); canvas->drawPicture(*pict); canvas->restore(); + + if (false) { + SkDebugfDumper dumper; + SkDumpCanvas dumpCanvas(&dumper); + dumpCanvas.drawPicture(*pict); + } // test that we can re-record a subpicture, and see the results diff --git a/src/core/SkCanvas.cpp b/src/core/SkCanvas.cpp index 0bf0614..a5529af 100644 --- a/src/core/SkCanvas.cpp +++ b/src/core/SkCanvas.cpp @@ -1239,6 +1239,10 @@ void SkCanvas::drawVertices(VertexMode vmode, int vertexCount, ITER_END } +void SkCanvas::drawData(const void* data, size_t length) { + // do nothing. Subclasses may do something with the data +} + ////////////////////////////////////////////////////////////////////////////// // These methods are NOT virtual, and therefore must call back into virtual // methods, rather than actually drawing themselves. diff --git a/src/core/SkPictureFlat.h b/src/core/SkPictureFlat.h index 86a161d..2c0af5a 100644 --- a/src/core/SkPictureFlat.h +++ b/src/core/SkPictureFlat.h @@ -18,6 +18,7 @@ enum DrawType { DRAW_BITMAP, DRAW_BITMAP_MATRIX, DRAW_BITMAP_RECT, + DRAW_DATA, DRAW_PAINT, DRAW_PATH, DRAW_PICTURE, diff --git a/src/core/SkPicturePlayback.cpp b/src/core/SkPicturePlayback.cpp index 03cdc16..f488565 100644 --- a/src/core/SkPicturePlayback.cpp +++ b/src/core/SkPicturePlayback.cpp @@ -589,6 +589,11 @@ void SkPicturePlayback::draw(SkCanvas& canvas) { const SkMatrix* matrix = getMatrix(); canvas.drawBitmapMatrix(bitmap, *matrix, paint); } break; + case DRAW_DATA: { + size_t length = getInt(); + canvas.drawData(fReader.skip(length), length); + // skip handles padding the read out to a multiple of 4 + } break; case DRAW_PAINT: canvas.drawPaint(*getPaint()); break; diff --git a/src/core/SkPictureRecord.cpp b/src/core/SkPictureRecord.cpp index b565fc1..9844a48 100644 --- a/src/core/SkPictureRecord.cpp +++ b/src/core/SkPictureRecord.cpp @@ -410,6 +410,12 @@ void SkPictureRecord::drawVertices(VertexMode vmode, int vertexCount, } } +void SkPictureRecord::drawData(const void* data, size_t length) { + addDraw(DRAW_DATA); + addInt(length); + fWriter.writePad(data, length); +} + /////////////////////////////////////////////////////////////////////////////// void SkPictureRecord::reset() { diff --git a/src/core/SkPictureRecord.h b/src/core/SkPictureRecord.h index 05761af..f9c967d 100644 --- a/src/core/SkPictureRecord.h +++ b/src/core/SkPictureRecord.h @@ -56,6 +56,7 @@ public: const SkColor colors[], SkXfermode*, const uint16_t indices[], int indexCount, const SkPaint&); + virtual void drawData(const void*, size_t); void addFontMetricsTopBottom(const SkPaint& paint, SkScalar baselineY); diff --git a/src/utils/SkDumpCanvas.cpp b/src/utils/SkDumpCanvas.cpp index 2151133..4ff7a50 100644 --- a/src/utils/SkDumpCanvas.cpp +++ b/src/utils/SkDumpCanvas.cpp @@ -379,6 +379,12 @@ void SkDumpCanvas::drawVertices(VertexMode vmode, int vertexCount, SkScalarToFloat(vertices[0].fY)); } +void SkDumpCanvas::drawData(const void* data, size_t length) { +// this->dump(kDrawData_Verb, NULL, "drawData(%d)", length); + this->dump(kDrawData_Verb, NULL, "drawData(%d) %.*s", length, + SkMin32(length, 64), data); +} + /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// diff --git a/src/utils/SkProxyCanvas.cpp b/src/utils/SkProxyCanvas.cpp index cb0d44a..c643c82 100644 --- a/src/utils/SkProxyCanvas.cpp +++ b/src/utils/SkProxyCanvas.cpp @@ -151,6 +151,10 @@ void SkProxyCanvas::drawVertices(VertexMode vmode, int vertexCount, xmode, indices, indexCount, paint); } +void SkProxyCanvas::drawData(const void* data, size_t length) { + fProxy->drawData(data, length); +} + SkBounder* SkProxyCanvas::setBounder(SkBounder* bounder) { return fProxy->setBounder(bounder); } -- 2.7.4