diff options
author | fmalita@google.com <fmalita@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-06-13 20:59:14 +0000 |
---|---|---|
committer | fmalita@google.com <fmalita@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-06-13 20:59:14 +0000 |
commit | 86681b37bd20204e47a492119b345c01d00bc939 (patch) | |
tree | 42d49ff31fcb89a9f9612f3c90ff65d9ad169b2b /src/utils/debugger | |
parent | 732c5d516a3ad729e69d88043c771f8b8eac2560 (diff) |
Expose debugger backend classes externally.
SkDebugCanvas & friends are useful beyond the Qt debugger app - Chromium's picture inspector (in development) is at the point where it could take advantage of it.
This patch moves SkDrawCommand, SkDebugCanvas & SkObjectParser from trunk/debugger to trunk/src/utils (since several tools already use these, this seems appropriate), and makes SkDrawCommand & SkDebugCanvas' symbols visible externally (SK_API).
R=reed@google.com
Review URL: https://codereview.chromium.org/16638014
git-svn-id: http://skia.googlecode.com/svn/trunk@9598 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/utils/debugger')
-rw-r--r-- | src/utils/debugger/SkDebugCanvas.cpp | 430 | ||||
-rw-r--r-- | src/utils/debugger/SkDebugCanvas.h | 273 | ||||
-rw-r--r-- | src/utils/debugger/SkDrawCommand.cpp | 878 | ||||
-rw-r--r-- | src/utils/debugger/SkDrawCommand.h | 556 | ||||
-rw-r--r-- | src/utils/debugger/SkObjectParser.cpp | 375 | ||||
-rw-r--r-- | src/utils/debugger/SkObjectParser.h | 128 |
6 files changed, 2640 insertions, 0 deletions
diff --git a/src/utils/debugger/SkDebugCanvas.cpp b/src/utils/debugger/SkDebugCanvas.cpp new file mode 100644 index 0000000000..199c4c9b0a --- /dev/null +++ b/src/utils/debugger/SkDebugCanvas.cpp @@ -0,0 +1,430 @@ + +/* + * Copyright 2012 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + + +#include "SkColorPriv.h" +#include "SkDebugCanvas.h" +#include "SkDrawCommand.h" +#include "SkDrawFilter.h" +#include "SkDevice.h" +#include "SkXfermode.h" + +#ifdef SK_BUILD_FOR_WIN + // iostream includes xlocale which generates warning 4530 because we're compiling without + // exceptions + #pragma warning(push) + #pragma warning(disable : 4530) +#endif +#include <iostream> +#ifdef SK_BUILD_FOR_WIN + #pragma warning(pop) +#endif + +static SkBitmap make_noconfig_bm(int width, int height) { + SkBitmap bm; + bm.setConfig(SkBitmap::kNo_Config, width, height); + return bm; +} + +SkDebugCanvas::SkDebugCanvas(int width, int height) + : INHERITED(make_noconfig_bm(width, height)) + , fOverdrawViz(false) + , fOverdrawFilter(NULL) + , fOutstandingSaveCount(0) { + // TODO(chudy): Free up memory from all draw commands in destructor. + fWidth = width; + fHeight = height; + // do we need fBm anywhere? + fBm.setConfig(SkBitmap::kNo_Config, fWidth, fHeight); + fFilter = false; + fIndex = 0; + fUserMatrix.reset(); +} + +SkDebugCanvas::~SkDebugCanvas() { + fCommandVector.deleteAll(); + SkSafeUnref(fOverdrawFilter); +} + +void SkDebugCanvas::addDrawCommand(SkDrawCommand* command) { + fCommandVector.push(command); +} + +void SkDebugCanvas::draw(SkCanvas* canvas) { + if(!fCommandVector.isEmpty()) { + for (int i = 0; i < fCommandVector.count(); i++) { + if (fCommandVector[i]->isVisible()) { + fCommandVector[i]->execute(canvas); + } + } + } + fIndex = fCommandVector.count() - 1; +} + +void SkDebugCanvas::applyUserTransform(SkCanvas* canvas) { + canvas->concat(fUserMatrix); +} + +int SkDebugCanvas::getCommandAtPoint(int x, int y, int index) { + SkBitmap bitmap; + bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); + bitmap.allocPixels(); + + SkCanvas canvas(bitmap); + canvas.translate(SkIntToScalar(-x), SkIntToScalar(-y)); + applyUserTransform(&canvas); + + int layer = 0; + SkColor prev = bitmap.getColor(0,0); + for (int i = 0; i < index; i++) { + if (fCommandVector[i]->isVisible()) { + fCommandVector[i]->execute(&canvas); + } + if (prev != bitmap.getColor(0,0)) { + layer = i; + } + prev = bitmap.getColor(0,0); + } + return layer; +} + +static SkPMColor OverdrawXferModeProc(SkPMColor src, SkPMColor dst) { + // This table encodes the color progression of the overdraw visualization + static const SkPMColor gTable[] = { + SkPackARGB32(0x00, 0x00, 0x00, 0x00), + SkPackARGB32(0xFF, 128, 158, 255), + SkPackARGB32(0xFF, 170, 185, 212), + SkPackARGB32(0xFF, 213, 195, 170), + SkPackARGB32(0xFF, 255, 192, 127), + SkPackARGB32(0xFF, 255, 185, 85), + SkPackARGB32(0xFF, 255, 165, 42), + SkPackARGB32(0xFF, 255, 135, 0), + SkPackARGB32(0xFF, 255, 95, 0), + SkPackARGB32(0xFF, 255, 50, 0), + SkPackARGB32(0xFF, 255, 0, 0) + }; + + for (size_t i = 0; i < SK_ARRAY_COUNT(gTable)-1; ++i) { + if (gTable[i] == dst) { + return gTable[i+1]; + } + } + + return gTable[SK_ARRAY_COUNT(gTable)-1]; +} + +// The OverdrawFilter modifies every paint to use an SkProcXfermode which +// in turn invokes OverdrawXferModeProc +class OverdrawFilter : public SkDrawFilter { +public: + OverdrawFilter() { + fXferMode = new SkProcXfermode(OverdrawXferModeProc); + } + + virtual ~OverdrawFilter() { + delete fXferMode; + } + + virtual bool filter(SkPaint* p, Type) SK_OVERRIDE { + p->setXfermode(fXferMode); + return true; + } + +protected: + SkXfermode* fXferMode; + +private: + typedef SkDrawFilter INHERITED; +}; + +void SkDebugCanvas::drawTo(SkCanvas* canvas, int index) { + SkASSERT(!fCommandVector.isEmpty()); + SkASSERT(index < fCommandVector.count()); + int i; + + // This only works assuming the canvas and device are the same ones that + // were previously drawn into because they need to preserve all saves + // and restores. + if (fIndex < index) { + i = fIndex + 1; + } else { + for (int j = 0; j < fOutstandingSaveCount; j++) { + canvas->restore(); + } + i = 0; + canvas->clear(SK_ColorTRANSPARENT); + canvas->resetMatrix(); + SkRect rect = SkRect::MakeWH(SkIntToScalar(fWidth), + SkIntToScalar(fHeight)); + canvas->clipRect(rect, SkRegion::kReplace_Op ); + applyUserTransform(canvas); + fOutstandingSaveCount = 0; + + // The setting of the draw filter has to go here (rather than in + // SkRasterWidget) due to the canvas restores this class performs. + // Since the draw filter is stored in the layer stack if we + // call setDrawFilter on anything but the root layer odd things happen + if (fOverdrawViz) { + if (NULL == fOverdrawFilter) { + fOverdrawFilter = new OverdrawFilter; + } + + if (fOverdrawFilter != canvas->getDrawFilter()) { + canvas->setDrawFilter(fOverdrawFilter); + } + } else { + canvas->setDrawFilter(NULL); + } + } + + for (; i <= index; i++) { + if (i == index && fFilter) { + SkPaint p; + p.setColor(0xAAFFFFFF); + canvas->save(); + canvas->resetMatrix(); + SkRect mask; + mask.set(SkIntToScalar(0), SkIntToScalar(0), + SkIntToScalar(fWidth), SkIntToScalar(fHeight)); + canvas->clipRect(mask, SkRegion::kReplace_Op, false); + canvas->drawRectCoords(SkIntToScalar(0), SkIntToScalar(0), + SkIntToScalar(fWidth), SkIntToScalar(fHeight), p); + canvas->restore(); + } + + if (fCommandVector[i]->isVisible()) { + fCommandVector[i]->execute(canvas); + fCommandVector[i]->trackSaveState(&fOutstandingSaveCount); + } + } + fMatrix = canvas->getTotalMatrix(); + fClip = canvas->getTotalClip().getBounds(); + fIndex = index; +} + +void SkDebugCanvas::deleteDrawCommandAt(int index) { + SkASSERT(index < fCommandVector.count()); + delete fCommandVector[index]; + fCommandVector.remove(index); +} + +SkDrawCommand* SkDebugCanvas::getDrawCommandAt(int index) { + SkASSERT(index < fCommandVector.count()); + return fCommandVector[index]; +} + +void SkDebugCanvas::setDrawCommandAt(int index, SkDrawCommand* command) { + SkASSERT(index < fCommandVector.count()); + delete fCommandVector[index]; + fCommandVector[index] = command; +} + +SkTDArray<SkString*>* SkDebugCanvas::getCommandInfo(int index) { + SkASSERT(index < fCommandVector.count()); + return fCommandVector[index]->Info(); +} + +bool SkDebugCanvas::getDrawCommandVisibilityAt(int index) { + SkASSERT(index < fCommandVector.count()); + return fCommandVector[index]->isVisible(); +} + +const SkTDArray <SkDrawCommand*>& SkDebugCanvas::getDrawCommands() const { + return fCommandVector; +} + +SkTDArray <SkDrawCommand*>& SkDebugCanvas::getDrawCommands() { + return fCommandVector; +} + +// TODO(chudy): Free command string memory. +SkTArray<SkString>* SkDebugCanvas::getDrawCommandsAsStrings() const { + SkTArray<SkString>* commandString = new SkTArray<SkString>(fCommandVector.count()); + if (!fCommandVector.isEmpty()) { + for (int i = 0; i < fCommandVector.count(); i ++) { + commandString->push_back() = fCommandVector[i]->toString(); + } + } + return commandString; +} + +void SkDebugCanvas::toggleFilter(bool toggle) { + fFilter = toggle; +} + +void SkDebugCanvas::clear(SkColor color) { + addDrawCommand(new Clear(color)); +} + +bool SkDebugCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) { + addDrawCommand(new ClipPath(path, op, doAA)); + return true; +} + +bool SkDebugCanvas::clipRect(const SkRect& rect, SkRegion::Op op, bool doAA) { + addDrawCommand(new ClipRect(rect, op, doAA)); + return true; +} + +bool SkDebugCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) { + addDrawCommand(new ClipRRect(rrect, op, doAA)); + return true; +} + +bool SkDebugCanvas::clipRegion(const SkRegion& region, SkRegion::Op op) { + addDrawCommand(new ClipRegion(region, op)); + return true; +} + +bool SkDebugCanvas::concat(const SkMatrix& matrix) { + addDrawCommand(new Concat(matrix)); + return true; +} + +void SkDebugCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left, + SkScalar top, const SkPaint* paint = NULL) { + addDrawCommand(new DrawBitmap(bitmap, left, top, paint)); +} + +void SkDebugCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, + const SkRect* src, const SkRect& dst, const SkPaint* paint) { + addDrawCommand(new DrawBitmapRect(bitmap, src, dst, paint)); +} + +void SkDebugCanvas::drawBitmapMatrix(const SkBitmap& bitmap, + const SkMatrix& matrix, const SkPaint* paint) { + addDrawCommand(new DrawBitmapMatrix(bitmap, matrix, paint)); +} + +void SkDebugCanvas::drawBitmapNine(const SkBitmap& bitmap, + const SkIRect& center, const SkRect& dst, const SkPaint* paint) { + addDrawCommand(new DrawBitmapNine(bitmap, center, dst, paint)); +} + +void SkDebugCanvas::drawData(const void* data, size_t length) { + addDrawCommand(new DrawData(data, length)); +} + +void SkDebugCanvas::beginCommentGroup(const char* description) { + addDrawCommand(new BeginCommentGroup(description)); +} + +void SkDebugCanvas::addComment(const char* kywd, const char* value) { + addDrawCommand(new Comment(kywd, value)); +} + +void SkDebugCanvas::endCommentGroup() { + addDrawCommand(new EndCommentGroup()); +} + +void SkDebugCanvas::drawOval(const SkRect& oval, const SkPaint& paint) { + addDrawCommand(new DrawOval(oval, paint)); +} + +void SkDebugCanvas::drawPaint(const SkPaint& paint) { + addDrawCommand(new DrawPaint(paint)); +} + +void SkDebugCanvas::drawPath(const SkPath& path, const SkPaint& paint) { + addDrawCommand(new DrawPath(path, paint)); +} + +void SkDebugCanvas::drawPicture(SkPicture& picture) { + addDrawCommand(new DrawPicture(picture)); +} + +void SkDebugCanvas::drawPoints(PointMode mode, size_t count, + const SkPoint pts[], const SkPaint& paint) { + addDrawCommand(new DrawPoints(mode, count, pts, paint)); +} + +void SkDebugCanvas::drawPosText(const void* text, size_t byteLength, + const SkPoint pos[], const SkPaint& paint) { + addDrawCommand(new DrawPosText(text, byteLength, pos, paint)); +} + +void SkDebugCanvas::drawPosTextH(const void* text, size_t byteLength, + const SkScalar xpos[], SkScalar constY, const SkPaint& paint) { + addDrawCommand(new DrawPosTextH(text, byteLength, xpos, constY, paint)); +} + +void SkDebugCanvas::drawRect(const SkRect& rect, const SkPaint& paint) { + // NOTE(chudy): Messing up when renamed to DrawRect... Why? + addDrawCommand(new DrawRectC(rect, paint)); +} + +void SkDebugCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) { + addDrawCommand(new DrawRRect(rrect, paint)); +} + +void SkDebugCanvas::drawSprite(const SkBitmap& bitmap, int left, int top, + const SkPaint* paint = NULL) { + addDrawCommand(new DrawSprite(bitmap, left, top, paint)); +} + +void SkDebugCanvas::drawText(const void* text, size_t byteLength, SkScalar x, + SkScalar y, const SkPaint& paint) { + addDrawCommand(new DrawTextC(text, byteLength, x, y, paint)); +} + +void SkDebugCanvas::drawTextOnPath(const void* text, size_t byteLength, + const SkPath& path, const SkMatrix* matrix, const SkPaint& paint) { + addDrawCommand(new DrawTextOnPath(text, byteLength, path, matrix, paint)); +} + +void SkDebugCanvas::drawVertices(VertexMode vmode, int vertexCount, + const SkPoint vertices[], const SkPoint texs[], const SkColor colors[], + SkXfermode*, const uint16_t indices[], int indexCount, + const SkPaint& paint) { + addDrawCommand(new DrawVertices(vmode, vertexCount, vertices, texs, colors, + NULL, indices, indexCount, paint)); +} + +void SkDebugCanvas::restore() { + addDrawCommand(new Restore()); +} + +bool SkDebugCanvas::rotate(SkScalar degrees) { + addDrawCommand(new Rotate(degrees)); + return true; +} + +int SkDebugCanvas::save(SaveFlags flags) { + addDrawCommand(new Save(flags)); + return true; +} + +int SkDebugCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint, + SaveFlags flags) { + addDrawCommand(new SaveLayer(bounds, paint, flags)); + return true; +} + +bool SkDebugCanvas::scale(SkScalar sx, SkScalar sy) { + addDrawCommand(new Scale(sx, sy)); + return true; +} + +void SkDebugCanvas::setMatrix(const SkMatrix& matrix) { + addDrawCommand(new SetMatrix(matrix)); +} + +bool SkDebugCanvas::skew(SkScalar sx, SkScalar sy) { + addDrawCommand(new Skew(sx, sy)); + return true; +} + +bool SkDebugCanvas::translate(SkScalar dx, SkScalar dy) { + addDrawCommand(new Translate(dx, dy)); + return true; +} + +void SkDebugCanvas::toggleCommand(int index, bool toggle) { + SkASSERT(index < fCommandVector.count()); + fCommandVector[index]->setVisible(toggle); +} diff --git a/src/utils/debugger/SkDebugCanvas.h b/src/utils/debugger/SkDebugCanvas.h new file mode 100644 index 0000000000..99df00e3fb --- /dev/null +++ b/src/utils/debugger/SkDebugCanvas.h @@ -0,0 +1,273 @@ + +/* + * Copyright 2012 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + + +#ifndef SKDEBUGCANVAS_H_ +#define SKDEBUGCANVAS_H_ + +#include "SkCanvas.h" +#include "SkDrawCommand.h" +#include "SkPicture.h" +#include "SkTArray.h" +#include "SkString.h" + +class SK_API SkDebugCanvas : public SkCanvas { +public: + SkDebugCanvas(int width, int height); + virtual ~SkDebugCanvas(); + + void toggleFilter(bool toggle); + + /** + * Enable or disable overdraw visualization + */ + void setOverdrawViz(bool overdrawViz) { fOverdrawViz = overdrawViz; } + + /** + Executes all draw calls to the canvas. + @param canvas The canvas being drawn to + */ + void draw(SkCanvas* canvas); + + /** + Executes the draw calls in the specified range. + @param canvas The canvas being drawn to + @param i The beginning of the range + @param j The end of the range + TODO(chudy): Implement + */ + void drawRange(SkCanvas* canvas, int i, int j); + + /** + Executes the draw calls up to the specified index. + @param canvas The canvas being drawn to + @param index The index of the final command being executed + */ + void drawTo(SkCanvas* canvas, int index); + + /** + Returns the most recently calculated transformation matrix + */ + const SkMatrix& getCurrentMatrix() { + return fMatrix; + } + + /** + Returns the most recently calculated clip + */ + const SkIRect& getCurrentClip() { + return fClip; + } + + /** + Returns the index of the last draw command to write to the pixel at (x,y) + */ + int getCommandAtPoint(int x, int y, int index); + + /** + Removes the command at the specified index + @param index The index of the command to delete + */ + void deleteDrawCommandAt(int index); + + /** + Returns the draw command at the given index. + @param index The index of the command + */ + SkDrawCommand* getDrawCommandAt(int index); + + /** + Sets the draw command for a given index. + @param index The index to overwrite + @param command The new command + */ + void setDrawCommandAt(int index, SkDrawCommand* command); + + /** + Returns information about the command at the given index. + @param index The index of the command + */ + SkTDArray<SkString*>* getCommandInfo(int index); + + /** + Returns the visibility of the command at the given index. + @param index The index of the command + */ + bool getDrawCommandVisibilityAt(int index); + + /** + Returns the vector of draw commands + DEPRECATED: please use getDrawCommandAt and getSize instead + */ + const SkTDArray<SkDrawCommand*>& getDrawCommands() const; + + /** + Returns the vector of draw commands. Do not use this entry + point - it is going away! + */ + SkTDArray<SkDrawCommand*>& getDrawCommands(); + + /** + * Returns the string vector of draw commands + */ + SkTArray<SkString>* getDrawCommandsAsStrings() const; + + /** + Returns length of draw command vector. + */ + int getSize() { + return fCommandVector.count(); + } + + /** + Toggles the visibility / execution of the draw command at index i with + the value of toggle. + */ + void toggleCommand(int index, bool toggle); + + void setBounds(int width, int height) { + fWidth = width; + fHeight = height; + } + + void setUserMatrix(SkMatrix matrix) { + fUserMatrix = matrix; + } + +//////////////////////////////////////////////////////////////////////////////// +// Inherited from SkCanvas +//////////////////////////////////////////////////////////////////////////////// + + virtual void clear(SkColor) SK_OVERRIDE; + + virtual bool clipPath(const SkPath&, SkRegion::Op, bool) SK_OVERRIDE; + + virtual bool clipRect(const SkRect&, SkRegion::Op, bool) SK_OVERRIDE; + + virtual bool clipRRect(const SkRRect& rrect, + SkRegion::Op op = SkRegion::kIntersect_Op, + bool doAntiAlias = false) SK_OVERRIDE; + + virtual bool clipRegion(const SkRegion& region, SkRegion::Op op) SK_OVERRIDE; + + virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE; + + virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top, + const SkPaint*) SK_OVERRIDE; + + virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src, + const SkRect& dst, const SkPaint*) SK_OVERRIDE; + + virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&, + const SkPaint*) SK_OVERRIDE; + + virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, + const SkRect& dst, const SkPaint*) SK_OVERRIDE; + + virtual void drawData(const void*, size_t) SK_OVERRIDE; + + virtual void beginCommentGroup(const char* description) SK_OVERRIDE; + + virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE; + + virtual void endCommentGroup() SK_OVERRIDE; + + virtual void drawOval(const SkRect& oval, const SkPaint&) SK_OVERRIDE; + + virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE; + + virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE; + + virtual void drawPicture(SkPicture& picture) SK_OVERRIDE; + + virtual void drawPoints(PointMode, size_t count, const SkPoint pts[], + const SkPaint&) SK_OVERRIDE; + + virtual void drawPosText(const void* text, size_t byteLength, + const SkPoint pos[], const SkPaint&) SK_OVERRIDE; + + virtual void drawPosTextH(const void* text, size_t byteLength, + const SkScalar xpos[], SkScalar constY, + const SkPaint&) SK_OVERRIDE; + + virtual void drawRect(const SkRect& rect, const SkPaint&) SK_OVERRIDE; + + virtual void drawRRect(const SkRRect& rrect, const SkPaint& paint) SK_OVERRIDE; + + virtual void drawSprite(const SkBitmap&, int left, int top, + const SkPaint*) SK_OVERRIDE; + + virtual void drawText(const void* text, size_t byteLength, SkScalar x, + SkScalar y, const SkPaint&) SK_OVERRIDE; + + virtual void drawTextOnPath(const void* text, size_t byteLength, + const SkPath& path, const SkMatrix* matrix, + const SkPaint&) SK_OVERRIDE; + + virtual void drawVertices(VertexMode, int vertexCount, + const SkPoint vertices[], const SkPoint texs[], + const SkColor colors[], SkXfermode*, + const uint16_t indices[], int indexCount, + const SkPaint&) SK_OVERRIDE; + + virtual void restore() SK_OVERRIDE; + + virtual bool rotate(SkScalar degrees) SK_OVERRIDE; + + virtual int save(SaveFlags) SK_OVERRIDE; + + virtual int saveLayer(const SkRect* bounds, const SkPaint*, SaveFlags) SK_OVERRIDE; + + virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE; + + virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE; + + virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE; + + virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE; + + static const int kVizImageHeight = 256; + static const int kVizImageWidth = 256; + +private: + SkTDArray<SkDrawCommand*> fCommandVector; + int fHeight; + int fWidth; + SkBitmap fBm; + bool fFilter; + int fIndex; + SkMatrix fUserMatrix; + SkMatrix fMatrix; + SkIRect fClip; + bool fOverdrawViz; + SkDrawFilter* fOverdrawFilter; + + /** + Number of unmatched save() calls at any point during a draw. + If there are any saveLayer() calls outstanding, we need to resolve + all of them, which in practice means resolving all save() calls, + to avoid corruption of our canvas. + */ + int fOutstandingSaveCount; + + /** + Adds the command to the classes vector of commands. + @param command The draw command for execution + */ + void addDrawCommand(SkDrawCommand* command); + + /** + Applies any panning and zooming the user has specified before + drawing anything else into the canvas. + */ + void applyUserTransform(SkCanvas* canvas); + + typedef SkCanvas INHERITED; +}; + +#endif diff --git a/src/utils/debugger/SkDrawCommand.cpp b/src/utils/debugger/SkDrawCommand.cpp new file mode 100644 index 0000000000..c9d9a47da3 --- /dev/null +++ b/src/utils/debugger/SkDrawCommand.cpp @@ -0,0 +1,878 @@ + +/* + * Copyright 2012 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + + +#include "SkDrawCommand.h" +#include "SkObjectParser.h" + +// TODO(chudy): Refactor into non subclass model. + +SkDrawCommand::SkDrawCommand(DrawType type) + : fDrawType(type) + , fVisible(true) { +} + +SkDrawCommand::SkDrawCommand() { + fVisible = true; +} + +SkDrawCommand::~SkDrawCommand() { + fInfo.deleteAll(); +} + +const char* SkDrawCommand::GetCommandString(DrawType type) { + switch (type) { + case UNUSED: SkDEBUGFAIL("DrawType UNUSED\n"); break; + case DRAW_CLEAR: return "Clear"; + case CLIP_PATH: return "Clip Path"; + case CLIP_REGION: return "Clip Region"; + case CLIP_RECT: return "Clip Rect"; + case CLIP_RRECT: return "Clip RRect"; + case CONCAT: return "Concat"; + case DRAW_BITMAP: return "Draw Bitmap"; + case DRAW_BITMAP_MATRIX: return "Draw Bitmap Matrix"; + case DRAW_BITMAP_NINE: return "Draw Bitmap Nine"; + case DRAW_BITMAP_RECT_TO_RECT: return "Draw Bitmap Rect"; + case DRAW_DATA: return "Draw Data"; + case DRAW_OVAL: return "Draw Oval"; + case DRAW_PAINT: return "Draw Paint"; + case DRAW_PATH: return "Draw Path"; + case DRAW_PICTURE: return "Draw Picture"; + case DRAW_POINTS: return "Draw Points"; + case DRAW_POS_TEXT: return "Draw Pos Text"; + case DRAW_POS_TEXT_H: return "Draw Pos Text H"; + case DRAW_RECT: return "Draw Rect"; + case DRAW_RRECT: return "Draw RRect"; + case DRAW_SPRITE: return "Draw Sprite"; + case DRAW_TEXT: return "Draw Text"; + case DRAW_TEXT_ON_PATH: return "Draw Text On Path"; + case DRAW_VERTICES: return "Draw Vertices"; + case RESTORE: return "Restore"; + case ROTATE: return "Rotate"; + case SAVE: return "Save"; + case SAVE_LAYER: return "Save Layer"; + case SCALE: return "Scale"; + case SET_MATRIX: return "Set Matrix"; + case SKEW: return "Skew"; + case TRANSLATE: return "Translate"; + case NOOP: return "NoOp"; + case BEGIN_COMMENT_GROUP: return "BeginCommentGroup"; + case COMMENT: return "Comment"; + case END_COMMENT_GROUP: return "EndCommentGroup"; + default: + SkDebugf("DrawType error 0x%08x\n", type); + SkASSERT(0); + break; + } + SkDEBUGFAIL("DrawType UNUSED\n"); + return NULL; +} + +SkString SkDrawCommand::toString() { + return SkString(GetCommandString(fDrawType)); +} + +Clear::Clear(SkColor color) { + fColor = color; + fDrawType = DRAW_CLEAR; + fInfo.push(SkObjectParser::CustomTextToString("No Parameters")); +} + +void Clear::execute(SkCanvas* canvas) { + canvas->clear(fColor); +} + +namespace { + +void xlate_and_scale_to_bounds(SkCanvas* canvas, const SkRect& bounds) { + const SkISize& size = canvas->getDeviceSize(); + + static const SkScalar kInsetFrac = 0.9f; // Leave a border around object + + canvas->translate(size.fWidth/2.0f, size.fHeight/2.0f); + if (bounds.width() > bounds.height()) { + canvas->scale(SkDoubleToScalar((kInsetFrac*size.fWidth)/bounds.width()), + SkDoubleToScalar((kInsetFrac*size.fHeight)/bounds.width())); + } else { + canvas->scale(SkDoubleToScalar((kInsetFrac*size.fWidth)/bounds.height()), + SkDoubleToScalar((kInsetFrac*size.fHeight)/bounds.height())); + } + canvas->translate(-bounds.centerX(), -bounds.centerY()); +} + + +void render_path(SkCanvas* canvas, const SkPath& path) { + canvas->clear(0xFFFFFFFF); + canvas->save(); + + const SkRect& bounds = path.getBounds(); + + xlate_and_scale_to_bounds(canvas, bounds); + + SkPaint p; + p.setColor(SK_ColorBLACK); + p.setStyle(SkPaint::kStroke_Style); + + canvas->drawPath(path, p); + canvas->restore(); +} + +void render_bitmap(SkCanvas* canvas, const SkBitmap& input, const SkRect* srcRect = NULL) { + const SkISize& size = canvas->getDeviceSize(); + + SkScalar xScale = SkIntToScalar(size.fWidth-2) / input.width(); + SkScalar yScale = SkIntToScalar(size.fHeight-2) / input.height(); + + if (input.width() > input.height()) { + yScale *= input.height() / (float) input.width(); + } else { + xScale *= input.width() / (float) input.height(); + } + + SkRect dst = SkRect::MakeXYWH(SK_Scalar1, SK_Scalar1, + xScale * input.width(), + yScale * input.height()); + + canvas->clear(0xFFFFFFFF); + canvas->drawBitmapRect(input, NULL, dst); + + if (NULL != srcRect) { + SkRect r = SkRect::MakeLTRB(srcRect->fLeft * xScale + SK_Scalar1, + srcRect->fTop * yScale + SK_Scalar1, + srcRect->fRight * xScale + SK_Scalar1, + srcRect->fBottom * yScale + SK_Scalar1); + SkPaint p; + p.setColor(SK_ColorRED); + p.setStyle(SkPaint::kStroke_Style); + + canvas->drawRect(r, p); + } +} + +void render_rrect(SkCanvas* canvas, const SkRRect& rrect) { + canvas->clear(0xFFFFFFFF); + canvas->save(); + + const SkRect& bounds = rrect.getBounds(); + + xlate_and_scale_to_bounds(canvas, bounds); + + SkPaint p; + p.setColor(SK_ColorBLACK); + p.setStyle(SkPaint::kStroke_Style); + + canvas->drawRRect(rrect, p); + canvas->restore(); +} + +}; + + +ClipPath::ClipPath(const SkPath& path, SkRegion::Op op, bool doAA) { + fPath = path; + fOp = op; + fDoAA = doAA; + fDrawType = CLIP_PATH; + + fInfo.push(SkObjectParser::PathToString(path)); + fInfo.push(SkObjectParser::RegionOpToString(op)); + fInfo.push(SkObjectParser::BoolToString(doAA)); +} + +void ClipPath::execute(SkCanvas* canvas) { + canvas->clipPath(fPath, fOp, fDoAA); +} + +bool ClipPath::render(SkCanvas* canvas) const { + render_path(canvas, fPath); + return true; +} + +ClipRegion::ClipRegion(const SkRegion& region, SkRegion::Op op) { + fRegion = region; + fOp = op; + fDrawType = CLIP_REGION; + + fInfo.push(SkObjectParser::RegionToString(region)); + fInfo.push(SkObjectParser::RegionOpToString(op)); +} + +void ClipRegion::execute(SkCanvas* canvas) { + canvas->clipRegion(fRegion, fOp); +} + +ClipRect::ClipRect(const SkRect& rect, SkRegion::Op op, bool doAA) { + fRect = rect; + fOp = op; + fDoAA = doAA; + fDrawType = CLIP_RECT; + + fInfo.push(SkObjectParser::RectToString(rect)); + fInfo.push(SkObjectParser::RegionOpToString(op)); + fInfo.push(SkObjectParser::BoolToString(doAA)); +} + +void ClipRect::execute(SkCanvas* canvas) { + canvas->clipRect(fRect, fOp, fDoAA); +} + +ClipRRect::ClipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) { + fRRect = rrect; + fOp = op; + fDoAA = doAA; + fDrawType = CLIP_RRECT; + + fInfo.push(SkObjectParser::RRectToString(rrect)); + fInfo.push(SkObjectParser::RegionOpToString(op)); + fInfo.push(SkObjectParser::BoolToString(doAA)); +} + +void ClipRRect::execute(SkCanvas* canvas) { + canvas->clipRRect(fRRect, fOp, fDoAA); +} + +bool ClipRRect::render(SkCanvas* canvas) const { + render_rrect(canvas, fRRect); + return true; +} + +Concat::Concat(const SkMatrix& matrix) { + fMatrix = matrix; + fDrawType = CONCAT; + + fInfo.push(SkObjectParser::MatrixToString(matrix)); +} + +void Concat::execute(SkCanvas* canvas) { + canvas->concat(fMatrix); +} + +DrawBitmap::DrawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, + const SkPaint* paint) { + fBitmap = bitmap; + fLeft = left; + fTop = top; + if (NULL != paint) { + fPaint = *paint; + fPaintPtr = &fPaint; + } else { + fPaintPtr = NULL; + } + fDrawType = DRAW_BITMAP; + + fInfo.push(SkObjectParser::BitmapToString(bitmap)); + fInfo.push(SkObjectParser::ScalarToString(left, "SkScalar left: ")); + fInfo.push(SkObjectParser::ScalarToString(top, "SkScalar top: ")); + if (NULL != paint) { + fInfo.push(SkObjectParser::PaintToString(*paint)); + } +} + +void DrawBitmap::execute(SkCanvas* canvas) { + canvas->drawBitmap(fBitmap, fLeft, fTop, fPaintPtr); +} + +bool DrawBitmap::render(SkCanvas* canvas) const { + render_bitmap(canvas, fBitmap); + return true; +} + +DrawBitmapMatrix::DrawBitmapMatrix(const SkBitmap& bitmap, + const SkMatrix& matrix, + const SkPaint* paint) { + fBitmap = bitmap; + fMatrix = matrix; + if (NULL != paint) { + fPaint = *paint; + fPaintPtr = &fPaint; + } else { + fPaintPtr = NULL; + } + fDrawType = DRAW_BITMAP_MATRIX; + + fInfo.push(SkObjectParser::BitmapToString(bitmap)); + fInfo.push(SkObjectParser::MatrixToString(matrix)); + if (NULL != paint) { + fInfo.push(SkObjectParser::PaintToString(*paint)); + } +} + +void DrawBitmapMatrix::execute(SkCanvas* canvas) { + canvas->drawBitmapMatrix(fBitmap, fMatrix, fPaintPtr); +} + +bool DrawBitmapMatrix::render(SkCanvas* canvas) const { + render_bitmap(canvas, fBitmap); + return true; +} + +DrawBitmapNine::DrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, + const SkRect& dst, const SkPaint* paint) { + fBitmap = bitmap; + fCenter = center; + fDst = dst; + if (NULL != paint) { + fPaint = *paint; + fPaintPtr = &fPaint; + } else { + fPaintPtr = NULL; + } + fDrawType = DRAW_BITMAP_NINE; + + fInfo.push(SkObjectParser::BitmapToString(bitmap)); + fInfo.push(SkObjectParser::IRectToString(center)); + fInfo.push(SkObjectParser::RectToString(dst, "Dst: ")); + if (NULL != paint) { + fInfo.push(SkObjectParser::PaintToString(*paint)); + } +} + +void DrawBitmapNine::execute(SkCanvas* canvas) { + canvas->drawBitmapNine(fBitmap, fCenter, fDst, fPaintPtr); +} + +bool DrawBitmapNine::render(SkCanvas* canvas) const { + render_bitmap(canvas, fBitmap); + return true; +} + +DrawBitmapRect::DrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, + const SkRect& dst, const SkPaint* paint) { + fBitmap = bitmap; + if (NULL != src) { + fSrc = *src; + } else { + fSrc.setEmpty(); + } + fDst = dst; + + if (NULL != paint) { + fPaint = *paint; + fPaintPtr = &fPaint; + } else { + fPaintPtr = NULL; + } + fDrawType = DRAW_BITMAP_RECT_TO_RECT; + + fInfo.push(SkObjectParser::BitmapToString(bitmap)); + if (NULL != src) { + fInfo.push(SkObjectParser::RectToString(*src, "Src: ")); + } + fInfo.push(SkObjectParser::RectToString(dst, "Dst: ")); + if (NULL != paint) { + fInfo.push(SkObjectParser::PaintToString(*paint)); + } +} + +void DrawBitmapRect::execute(SkCanvas* canvas) { + canvas->drawBitmapRectToRect(fBitmap, this->srcRect(), fDst, fPaintPtr); +} + +bool DrawBitmapRect::render(SkCanvas* canvas) const { + render_bitmap(canvas, fBitmap, this->srcRect()); + return true; +} + +DrawData::DrawData(const void* data, size_t length) { + fData = new char[length]; + memcpy(fData, data, length); + fLength = length; + fDrawType = DRAW_DATA; + + // TODO: add display of actual data? + SkString* str = new SkString; + str->appendf("length: %d", (int) length); + fInfo.push(str); +} + +void DrawData::execute(SkCanvas* canvas) { + canvas->drawData(fData, fLength); +} + +BeginCommentGroup::BeginCommentGroup(const char* description) + : INHERITED(BEGIN_COMMENT_GROUP) + , fDescription(description) { + SkString* temp = new SkString; + temp->appendf("Description: %s", description); + fInfo.push(temp); +} + +Comment::Comment(const char* kywd, const char* value) + : INHERITED(COMMENT) + , fKywd(kywd) + , fValue(value) { + SkString* temp = new SkString; + temp->appendf("%s: %s", kywd, value); + fInfo.push(temp); +} + +EndCommentGroup::EndCommentGroup() : INHERITED(END_COMMENT_GROUP) { +} + +DrawOval::DrawOval(const SkRect& oval, const SkPaint& paint) { + fOval = oval; + fPaint = paint; + fDrawType = DRAW_OVAL; + + fInfo.push(SkObjectParser::RectToString(oval)); + fInfo.push(SkObjectParser::PaintToString(paint)); +} + +void DrawOval::execute(SkCanvas* canvas) { + canvas->drawOval(fOval, fPaint); +} + +bool DrawOval::render(SkCanvas* canvas) const { + canvas->clear(0xFFFFFFFF); + canvas->save(); + + xlate_and_scale_to_bounds(canvas, fOval); + + SkPaint p; + p.setColor(SK_ColorBLACK); + p.setStyle(SkPaint::kStroke_Style); + + canvas->drawOval(fOval, p); + canvas->restore(); + + return true; +} + +DrawPaint::DrawPaint(const SkPaint& paint) { + fPaint = paint; + fDrawType = DRAW_PAINT; + + fInfo.push(SkObjectParser::PaintToString(paint)); +} + +void DrawPaint::execute(SkCanvas* canvas) { + canvas->drawPaint(fPaint); +} + +bool DrawPaint::render(SkCanvas* canvas) const { + canvas->clear(0xFFFFFFFF); + canvas->drawPaint(fPaint); + return true; +} + +DrawPath::DrawPath(const SkPath& path, const SkPaint& paint) { + fPath = path; + fPaint = paint; + fDrawType = DRAW_PATH; + + fInfo.push(SkObjectParser::PathToString(path)); + fInfo.push(SkObjectParser::PaintToString(paint)); +} + +void DrawPath::execute(SkCanvas* canvas) { + canvas->drawPath(fPath, fPaint); +} + +bool DrawPath::render(SkCanvas* canvas) const { + render_path(canvas, fPath); + return true; +} + +DrawPicture::DrawPicture(SkPicture& picture) : + fPicture(picture) { + fDrawType = DRAW_PICTURE; + fInfo.push(SkObjectParser::CustomTextToString("To be implemented.")); +} + +void DrawPicture::execute(SkCanvas* canvas) { + canvas->drawPicture(fPicture); +} + +DrawPoints::DrawPoints(SkCanvas::PointMode mode, size_t count, + const SkPoint pts[], const SkPaint& paint) { + fMode = mode; + fCount = count; + fPts = new SkPoint[count]; + memcpy(fPts, pts, count * sizeof(SkPoint)); + fPaint = paint; + fDrawType = DRAW_POINTS; + + fInfo.push(SkObjectParser::PointsToString(pts, count)); + fInfo.push(SkObjectParser::ScalarToString(SkIntToScalar((unsigned int)count), + "Points: ")); + fInfo.push(SkObjectParser::PointModeToString(mode)); + fInfo.push(SkObjectParser::PaintToString(paint)); +} + +void DrawPoints::execute(SkCanvas* canvas) { + canvas->drawPoints(fMode, fCount, fPts, fPaint); +} + +bool DrawPoints::render(SkCanvas* canvas) const { + canvas->clear(0xFFFFFFFF); + canvas->save(); + + SkRect bounds; + + bounds.setEmpty(); + for (unsigned int i = 0; i < fCount; ++i) { + bounds.growToInclude(fPts[i].fX, fPts[i].fY); + } + + xlate_and_scale_to_bounds(canvas, bounds); + + SkPaint p; + p.setColor(SK_ColorBLACK); + p.setStyle(SkPaint::kStroke_Style); + + canvas->drawPoints(fMode, fCount, fPts, p); + canvas->restore(); + + return true; +} + +DrawPosText::DrawPosText(const void* text, size_t byteLength, const SkPoint pos[], + const SkPaint& paint) { + size_t numPts = paint.countText(text, byteLength); + + fText = new char[byteLength]; + memcpy(fText, text, byteLength); + fByteLength = byteLength; + + fPos = new SkPoint[numPts]; + memcpy(fPos, pos, numPts * sizeof(SkPoint)); + + fPaint = paint; + fDrawType = DRAW_POS_TEXT; + + fInfo.push(SkObjectParser::TextToString(text, byteLength, paint.getTextEncoding())); + // TODO(chudy): Test that this works. + fInfo.push(SkObjectParser::PointsToString(pos, 1)); + fInfo.push(SkObjectParser::PaintToString(paint)); +} + +void DrawPosText::execute(SkCanvas* canvas) { + canvas->drawPosText(fText, fByteLength, fPos, fPaint); +} + + +DrawPosTextH::DrawPosTextH(const void* text, size_t byteLength, + const SkScalar xpos[], SkScalar constY, + const SkPaint& paint) { + size_t numPts = paint.countText(text, byteLength); + + fText = new char[byteLength]; + memcpy(fText, text, byteLength); + fByteLength = byteLength; + + fXpos = new SkScalar[numPts]; + memcpy(fXpos, xpos, numPts * sizeof(SkScalar)); + + fConstY = constY; + fPaint = paint; + fDrawType = DRAW_POS_TEXT_H; + + fInfo.push(SkObjectParser::TextToString(text, byteLength, paint.getTextEncoding())); + fInfo.push(SkObjectParser::ScalarToString(xpos[0], "XPOS: ")); + fInfo.push(SkObjectParser::ScalarToString(constY, "SkScalar constY: ")); + fInfo.push(SkObjectParser::PaintToString(paint)); +} + +void DrawPosTextH::execute(SkCanvas* canvas) { + canvas->drawPosTextH(fText, fByteLength, fXpos, fConstY, fPaint); +} + +DrawRectC::DrawRectC(const SkRect& rect, const SkPaint& paint) { + fRect = rect; + fPaint = paint; + fDrawType = DRAW_RECT; + + fInfo.push(SkObjectParser::RectToString(rect)); + fInfo.push(SkObjectParser::PaintToString(paint)); +} + +void DrawRectC::execute(SkCanvas* canvas) { + canvas->drawRect(fRect, fPaint); +} + +DrawRRect::DrawRRect(const SkRRect& rrect, const SkPaint& paint) { + fRRect = rrect; + fPaint = paint; + fDrawType = DRAW_RRECT; + + fInfo.push(SkObjectParser::RRectToString(rrect)); + fInfo.push(SkObjectParser::PaintToString(paint)); +} + +void DrawRRect::execute(SkCanvas* canvas) { + canvas->drawRRect(fRRect, fPaint); +} + +bool DrawRRect::render(SkCanvas* canvas) const { + render_rrect(canvas, fRRect); + return true; +} + +DrawSprite::DrawSprite(const SkBitmap& bitmap, int left, int top, + const SkPaint* paint) { + fBitmap = bitmap; + fLeft = left; + fTop = top; + if (NULL != paint) { + fPaint = *paint; + fPaintPtr = &fPaint; + } else { + fPaintPtr = NULL; + } + fDrawType = DRAW_SPRITE; + + fInfo.push(SkObjectParser::BitmapToString(bitmap)); + fInfo.push(SkObjectParser::IntToString(left, "Left: ")); + fInfo.push(SkObjectParser::IntToString(top, "Top: ")); + if (NULL != paint) { + fInfo.push(SkObjectParser::PaintToString(*paint)); + } +} + +void DrawSprite::execute(SkCanvas* canvas) { + canvas->drawSprite(fBitmap, fLeft, fTop, fPaintPtr); +} + +bool DrawSprite::render(SkCanvas* canvas) const { + render_bitmap(canvas, fBitmap); + return true; +} + +DrawTextC::DrawTextC(const void* text, size_t byteLength, SkScalar x, SkScalar y, + const SkPaint& paint) { + fText = new char[byteLength]; + memcpy(fText, text, byteLength); + fByteLength = byteLength; + fX = x; + fY = y; + fPaint = paint; + fDrawType = DRAW_TEXT; + + fInfo.push(SkObjectParser::TextToString(text, byteLength, paint.getTextEncoding())); + fInfo.push(SkObjectParser::ScalarToString(x, "SkScalar x: ")); + fInfo.push(SkObjectParser::ScalarToString(y, "SkScalar y: ")); + fInfo.push(SkObjectParser::PaintToString(paint)); +} + +void DrawTextC::execute(SkCanvas* canvas) { + canvas->drawText(fText, fByteLength, fX, fY, fPaint); +} + +DrawTextOnPath::DrawTextOnPath(const void* text, size_t byteLength, + const SkPath& path, const SkMatrix* matrix, + const SkPaint& paint) { + fText = new char[byteLength]; + memcpy(fText, text, byteLength); + fByteLength = byteLength; + fPath = path; + if (NULL != matrix) { + fMatrix = *matrix; + } else { + fMatrix.setIdentity(); + } + fPaint = paint; + fDrawType = DRAW_TEXT_ON_PATH; + + fInfo.push(SkObjectParser::TextToString(text, byteLength, paint.getTextEncoding())); + fInfo.push(SkObjectParser::PathToString(path)); + if (NULL != matrix) { + fInfo.push(SkObjectParser::MatrixToString(*matrix)); + } + fInfo.push(SkObjectParser::PaintToString(paint)); +} + +void DrawTextOnPath::execute(SkCanvas* canvas) { + canvas->drawTextOnPath(fText, fByteLength, fPath, + fMatrix.isIdentity() ? NULL : &fMatrix, + fPaint); +} + +DrawVertices::DrawVertices(SkCanvas::VertexMode vmode, int vertexCount, + const SkPoint vertices[], const SkPoint texs[], + const SkColor colors[], SkXfermode* xfermode, + const uint16_t indices[], int indexCount, + const SkPaint& paint) { + fVmode = vmode; + + fVertexCount = vertexCount; + + fVertices = new SkPoint[vertexCount]; + memcpy(fVertices, vertices, vertexCount * sizeof(SkPoint)); + + if (NULL != texs) { + fTexs = new SkPoint[vertexCount]; + memcpy(fTexs, texs, vertexCount * sizeof(SkPoint)); + } else { + fTexs = NULL; + } + + if (NULL != colors) { + fColors = new SkColor[vertexCount]; + memcpy(fColors, colors, vertexCount * sizeof(SkColor)); + } else { + fColors = NULL; + } + + fXfermode = xfermode; + if (NULL != fXfermode) { + fXfermode->ref(); + } + + if (indexCount > 0) { + fIndices = new uint16_t[indexCount]; + memcpy(fIndices, indices, indexCount * sizeof(uint16_t)); + } else { + fIndices = NULL; + } + + fIndexCount = indexCount; + fPaint = paint; + fDrawType = DRAW_VERTICES; + + // TODO(chudy) + fInfo.push(SkObjectParser::CustomTextToString("To be implemented.")); + fInfo.push(SkObjectParser::PaintToString(paint)); +} + +DrawVertices::~DrawVertices() { + delete [] fVertices; + delete [] fTexs; + delete [] fColors; + SkSafeUnref(fXfermode); + delete [] fIndices; +} + +void DrawVertices::execute(SkCanvas* canvas) { + canvas->drawVertices(fVmode, fVertexCount, fVertices, + fTexs, fColors, fXfermode, fIndices, + fIndexCount, fPaint); +} + +Restore::Restore() { + fDrawType = RESTORE; + fInfo.push(SkObjectParser::CustomTextToString("No Parameters")); +} + +void Restore::execute(SkCanvas* canvas) { + canvas->restore(); +} + +void Restore::trackSaveState(int* state) { + (*state)--; +} + +Rotate::Rotate(SkScalar degrees) { + fDegrees = degrees; + fDrawType = ROTATE; + + fInfo.push(SkObjectParser::ScalarToString(degrees, "SkScalar degrees: ")); +} + +void Rotate::execute(SkCanvas* canvas) { + canvas->rotate(fDegrees); +} + +Save::Save(SkCanvas::SaveFlags flags) { + fFlags = flags; + fDrawType = SAVE; + fInfo.push(SkObjectParser::SaveFlagsToString(flags)); +} + +void Save::execute(SkCanvas* canvas) { + canvas->save(fFlags); +} + +void Save::trackSaveState(int* state) { + (*state)++; +} + +SaveLayer::SaveLayer(const SkRect* bounds, const SkPaint* paint, + SkCanvas::SaveFlags flags) { + if (NULL != bounds) { + fBounds = *bounds; + } else { + fBounds.setEmpty(); + } + + if (NULL != paint) { + fPaint = *paint; + fPaintPtr = &fPaint; + } else { + fPaintPtr = NULL; + } + fFlags = flags; + fDrawType = SAVE_LAYER; + + if (NULL != bounds) { + fInfo.push(SkObjectParser::RectToString(*bounds, "Bounds: ")); + } + if (NULL != paint) { + fInfo.push(SkObjectParser::PaintToString(*paint)); + } + fInfo.push(SkObjectParser::SaveFlagsToString(flags)); +} + +void SaveLayer::execute(SkCanvas* canvas) { + canvas->saveLayer(fBounds.isEmpty() ? NULL : &fBounds, + fPaintPtr, + fFlags); +} + +void SaveLayer::trackSaveState(int* state) { + (*state)++; +} + +Scale::Scale(SkScalar sx, SkScalar sy) { + fSx = sx; + fSy = sy; + fDrawType = SCALE; + + fInfo.push(SkObjectParser::ScalarToString(sx, "SkScalar sx: ")); + fInfo.push(SkObjectParser::ScalarToString(sy, "SkScalar sy: ")); +} + +void Scale::execute(SkCanvas* canvas) { + canvas->scale(fSx, fSy); +} + +SetMatrix::SetMatrix(const SkMatrix& matrix) { + fMatrix = matrix; + fDrawType = SET_MATRIX; + + fInfo.push(SkObjectParser::MatrixToString(matrix)); +} + +void SetMatrix::execute(SkCanvas* canvas) { + canvas->setMatrix(fMatrix); +} + +Skew::Skew(SkScalar sx, SkScalar sy) { + fSx = sx; + fSy = sy; + fDrawType = SKEW; + + fInfo.push(SkObjectParser::ScalarToString(sx, "SkScalar sx: ")); + fInfo.push(SkObjectParser::ScalarToString(sy, "SkScalar sy: ")); +} + +void Skew::execute(SkCanvas* canvas) { + canvas->skew(fSx, fSy); +} + +Translate::Translate(SkScalar dx, SkScalar dy) { + fDx = dx; + fDy = dy; + fDrawType = TRANSLATE; + + fInfo.push(SkObjectParser::ScalarToString(dx, "SkScalar dx: ")); + fInfo.push(SkObjectParser::ScalarToString(dy, "SkScalar dy: ")); +} + +void Translate::execute(SkCanvas* canvas) { + canvas->translate(fDx, fDy); +} diff --git a/src/utils/debugger/SkDrawCommand.h b/src/utils/debugger/SkDrawCommand.h new file mode 100644 index 0000000000..f48a79b337 --- /dev/null +++ b/src/utils/debugger/SkDrawCommand.h @@ -0,0 +1,556 @@ + +/* + * Copyright 2012 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SKDRAWCOMMAND_H_ +#define SKDRAWCOMMAND_H_ + +#include "SkPictureFlat.h" +#include "SkCanvas.h" +#include "SkString.h" + +class SK_API SkDrawCommand { +public: + /* TODO(chudy): Remove subclasses. */ + SkDrawCommand(DrawType drawType); + SkDrawCommand(); + + virtual ~SkDrawCommand(); + + virtual SkString toString(); + + virtual const char* toCString() { + return GetCommandString(fDrawType); + } + + bool isVisible() const { + return fVisible; + } + + void setVisible(bool toggle) { + fVisible = toggle; + } + + SkTDArray<SkString*>* Info() {return &fInfo; }; + virtual void execute(SkCanvas* canvas)=0; + /** Does nothing by default, but used by save() and restore()-type + subclassse to track unresolved save() calls. */ + virtual void trackSaveState(int* state) { }; + DrawType getType() { return fDrawType; }; + + virtual bool render(SkCanvas* canvas) const { return false; } + + static const char* GetCommandString(DrawType type); + +protected: + DrawType fDrawType; + SkTDArray<SkString*> fInfo; + +private: + bool fVisible; +}; + +class Restore : public SkDrawCommand { +public: + Restore(); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; + virtual void trackSaveState(int* state) SK_OVERRIDE; + +private: + typedef SkDrawCommand INHERITED; +}; + +class Clear : public SkDrawCommand { +public: + Clear(SkColor color); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; +private: + SkColor fColor; + + typedef SkDrawCommand INHERITED; +}; + +class ClipPath : public SkDrawCommand { +public: + ClipPath(const SkPath& path, SkRegion::Op op, bool doAA); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; + virtual bool render(SkCanvas* canvas) const SK_OVERRIDE; +private: + SkPath fPath; + SkRegion::Op fOp; + bool fDoAA; + + typedef SkDrawCommand INHERITED; +}; + +class ClipRegion : public SkDrawCommand { +public: + ClipRegion(const SkRegion& region, SkRegion::Op op); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; +private: + SkRegion fRegion; + SkRegion::Op fOp; + + typedef SkDrawCommand INHERITED; +}; + +class ClipRect : public SkDrawCommand { +public: + ClipRect(const SkRect& rect, SkRegion::Op op, bool doAA); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; + + const SkRect& rect() const { return fRect; } + SkRegion::Op op() const { return fOp; } + bool doAA() const { return fDoAA; } + +private: + SkRect fRect; + SkRegion::Op fOp; + bool fDoAA; + + typedef SkDrawCommand INHERITED; +}; + +class ClipRRect : public SkDrawCommand { +public: + ClipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; + virtual bool render(SkCanvas* canvas) const SK_OVERRIDE; + + const SkRRect& rrect() const { return fRRect; } + SkRegion::Op op() const { return fOp; } + bool doAA() const { return fDoAA; } + +private: + SkRRect fRRect; + SkRegion::Op fOp; + bool fDoAA; + + typedef SkDrawCommand INHERITED; +}; + +class Concat : public SkDrawCommand { +public: + Concat(const SkMatrix& matrix); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; +private: + SkMatrix fMatrix; + + typedef SkDrawCommand INHERITED; +}; + +class DrawBitmap : public SkDrawCommand { +public: + DrawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, + const SkPaint* paint); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; + virtual bool render(SkCanvas* canvas) const SK_OVERRIDE; +private: + SkBitmap fBitmap; + SkScalar fLeft; + SkScalar fTop; + SkPaint fPaint; + SkPaint* fPaintPtr; + + typedef SkDrawCommand INHERITED; +}; + +class DrawBitmapMatrix : public SkDrawCommand { +public: + DrawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& matrix, + const SkPaint* paint); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; + virtual bool render(SkCanvas* canvas) const SK_OVERRIDE; +private: + SkBitmap fBitmap; + SkMatrix fMatrix; + SkPaint fPaint; + SkPaint* fPaintPtr; + + typedef SkDrawCommand INHERITED; +}; + +class DrawBitmapNine : public SkDrawCommand { +public: + DrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, + const SkRect& dst, const SkPaint* paint); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; + virtual bool render(SkCanvas* canvas) const SK_OVERRIDE; +private: + SkBitmap fBitmap; + SkIRect fCenter; + SkRect fDst; + SkPaint fPaint; + SkPaint* fPaintPtr; + + typedef SkDrawCommand INHERITED; +}; + +class DrawBitmapRect : public SkDrawCommand { +public: + DrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, + const SkRect& dst, const SkPaint* paint); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; + virtual bool render(SkCanvas* canvas) const SK_OVERRIDE; + + const SkBitmap& bitmap() const { return fBitmap; } + + // The non-const 'paint' method allows modification of this object's + // SkPaint. For this reason the ctor and setPaint method make a local copy. + // The 'fPaintPtr' member acts a signal that the local SkPaint is valid + // (since only an SkPaint* is passed into the ctor). + const SkPaint* paint() const { return fPaintPtr; } + SkPaint* paint() { return fPaintPtr; } + + void setPaint(const SkPaint& paint) { fPaint = paint; fPaintPtr = &fPaint; } + + const SkRect* srcRect() const { return fSrc.isEmpty() ? NULL : &fSrc; } + const SkRect& dstRect() const { return fDst; } + + void setSrcRect(const SkRect& src) { fSrc = src; } + void setDstRect(const SkRect& dst) { fDst = dst; } + +private: + SkBitmap fBitmap; + SkRect fSrc; + SkRect fDst; + SkPaint fPaint; + SkPaint* fPaintPtr; + + typedef SkDrawCommand INHERITED; +}; + +class DrawData : public SkDrawCommand { +public: + DrawData(const void* data, size_t length); + virtual ~DrawData() { delete [] fData; } + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; +private: + char* fData; + size_t fLength; + + typedef SkDrawCommand INHERITED; +}; + +class BeginCommentGroup : public SkDrawCommand { +public: + BeginCommentGroup(const char* description); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE { + canvas->beginCommentGroup(fDescription.c_str()); + }; +private: + SkString fDescription; + + typedef SkDrawCommand INHERITED; +}; + +class Comment : public SkDrawCommand { +public: + Comment(const char* kywd, const char* value); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE { + canvas->addComment(fKywd.c_str(), fValue.c_str()); + }; +private: + SkString fKywd; + SkString fValue; + + typedef SkDrawCommand INHERITED; +}; + +class EndCommentGroup : public SkDrawCommand { +public: + EndCommentGroup(); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE { + canvas->endCommentGroup(); + }; +private: + typedef SkDrawCommand INHERITED; +}; + +class DrawOval : public SkDrawCommand { +public: + DrawOval(const SkRect& oval, const SkPaint& paint); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; + virtual bool render(SkCanvas* canvas) const SK_OVERRIDE; +private: + SkRect fOval; + SkPaint fPaint; + + typedef SkDrawCommand INHERITED; +}; + +class DrawPaint : public SkDrawCommand { +public: + DrawPaint(const SkPaint& paint); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; + virtual bool render(SkCanvas* canvas) const SK_OVERRIDE; +private: + SkPaint fPaint; + + typedef SkDrawCommand INHERITED; +}; + +class DrawPath : public SkDrawCommand { +public: + DrawPath(const SkPath& path, const SkPaint& paint); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; + virtual bool render(SkCanvas* canvas) const SK_OVERRIDE; + +private: + SkPath fPath; + SkPaint fPaint; + + typedef SkDrawCommand INHERITED; +}; + +class DrawPicture : public SkDrawCommand { +public: + DrawPicture(SkPicture& picture); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; +private: + SkPicture fPicture; + + typedef SkDrawCommand INHERITED; +}; + +class DrawPoints : public SkDrawCommand { +public: + DrawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint pts[], + const SkPaint& paint); + virtual ~DrawPoints() { delete [] fPts; } + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; + virtual bool render(SkCanvas* canvas) const SK_OVERRIDE; +private: + SkCanvas::PointMode fMode; + size_t fCount; + SkPoint* fPts; + SkPaint fPaint; + + typedef SkDrawCommand INHERITED; +}; + +/* TODO(chudy): DrawText is a predefined macro and was breaking something + * in the windows build of the debugger. + */ +class DrawTextC : public SkDrawCommand { +public: + DrawTextC(const void* text, size_t byteLength, SkScalar x, SkScalar y, + const SkPaint& paint); + virtual ~DrawTextC() { delete [] fText; } + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; +private: + char* fText; + size_t fByteLength; + SkScalar fX; + SkScalar fY; + SkPaint fPaint; + + typedef SkDrawCommand INHERITED; +}; + +class DrawPosText : public SkDrawCommand { +public: + DrawPosText(const void* text, size_t byteLength, const SkPoint pos[], + const SkPaint& paint); + virtual ~DrawPosText() { delete [] fPos; delete [] fText; } + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; +private: + char* fText; + size_t fByteLength; + SkPoint* fPos; + SkPaint fPaint; + + typedef SkDrawCommand INHERITED; +}; + +class DrawTextOnPath : public SkDrawCommand { +public: + DrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, + const SkMatrix* matrix, const SkPaint& paint); + virtual ~DrawTextOnPath() { delete [] fText; } + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; +private: + char* fText; + size_t fByteLength; + SkPath fPath; + SkMatrix fMatrix; + SkPaint fPaint; + + typedef SkDrawCommand INHERITED; +}; + +class DrawPosTextH : public SkDrawCommand { +public: + DrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], + SkScalar constY, const SkPaint& paint); + virtual ~DrawPosTextH() { delete [] fXpos; delete [] fText; } + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; +private: + SkScalar* fXpos; + char* fText; + size_t fByteLength; + SkScalar fConstY; + SkPaint fPaint; + + typedef SkDrawCommand INHERITED; +}; + +class DrawRectC : public SkDrawCommand { +public: + DrawRectC(const SkRect& rect, const SkPaint& paint); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; + + const SkRect& rect() const { return fRect; } + const SkPaint& paint() const { return fPaint; } +private: + SkRect fRect; + SkPaint fPaint; + + typedef SkDrawCommand INHERITED; +}; + +class DrawRRect : public SkDrawCommand { +public: + DrawRRect(const SkRRect& rrect, const SkPaint& paint); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; + virtual bool render(SkCanvas* canvas) const SK_OVERRIDE; +private: + SkRRect fRRect; + SkPaint fPaint; + + typedef SkDrawCommand INHERITED; +}; + +class DrawSprite : public SkDrawCommand { +public: + DrawSprite(const SkBitmap& bitmap, int left, int top, const SkPaint* paint); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; + virtual bool render(SkCanvas* canvas) const SK_OVERRIDE; +private: + SkBitmap fBitmap; + int fLeft; + int fTop; + SkPaint fPaint; + SkPaint* fPaintPtr; + + typedef SkDrawCommand INHERITED; +}; + +class DrawVertices : public SkDrawCommand { +public: + DrawVertices(SkCanvas::VertexMode vmode, int vertexCount, + const SkPoint vertices[], const SkPoint texs[], + const SkColor colors[], SkXfermode* xfermode, + const uint16_t indices[], int indexCount, + const SkPaint& paint); + virtual ~DrawVertices(); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; +private: + SkCanvas::VertexMode fVmode; + int fVertexCount; + SkPoint* fVertices; + SkPoint* fTexs; + SkColor* fColors; + SkXfermode* fXfermode; + uint16_t* fIndices; + int fIndexCount; + SkPaint fPaint; + + typedef SkDrawCommand INHERITED; +}; + +class Rotate : public SkDrawCommand { +public: + Rotate(SkScalar degrees); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; +private: + SkScalar fDegrees; + + typedef SkDrawCommand INHERITED; +}; + +class Save : public SkDrawCommand { +public: + Save(SkCanvas::SaveFlags flags); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; + virtual void trackSaveState(int* state) SK_OVERRIDE; +private: + SkCanvas::SaveFlags fFlags; + + typedef SkDrawCommand INHERITED; +}; + +class SaveLayer : public SkDrawCommand { +public: + SaveLayer(const SkRect* bounds, const SkPaint* paint, + SkCanvas::SaveFlags flags); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; + virtual void trackSaveState(int* state) SK_OVERRIDE; + + const SkPaint* paint() const { return fPaintPtr; } + +private: + SkRect fBounds; + SkPaint fPaint; + SkPaint* fPaintPtr; + SkCanvas::SaveFlags fFlags; + + typedef SkDrawCommand INHERITED; +}; + +class Scale : public SkDrawCommand { +public: + Scale(SkScalar sx, SkScalar sy); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; + + SkScalar x() const { return fSx; } + SkScalar y() const { return fSy; } + +private: + SkScalar fSx; + SkScalar fSy; + + typedef SkDrawCommand INHERITED; +}; + +class SetMatrix : public SkDrawCommand { +public: + SetMatrix(const SkMatrix& matrix); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; +private: + SkMatrix fMatrix; + + typedef SkDrawCommand INHERITED; +}; + +class Skew : public SkDrawCommand { +public: + Skew(SkScalar sx, SkScalar sy); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; +private: + SkScalar fSx; + SkScalar fSy; + + typedef SkDrawCommand INHERITED; +}; + +class Translate : public SkDrawCommand { +public: + Translate(SkScalar dx, SkScalar dy); + virtual void execute(SkCanvas* canvas) SK_OVERRIDE; + + SkScalar x() const { return fDx; } + SkScalar y() const { return fDy; } + +private: + SkScalar fDx; + SkScalar fDy; + + typedef SkDrawCommand INHERITED; +}; + +#endif diff --git a/src/utils/debugger/SkObjectParser.cpp b/src/utils/debugger/SkObjectParser.cpp new file mode 100644 index 0000000000..7004bf0622 --- /dev/null +++ b/src/utils/debugger/SkObjectParser.cpp @@ -0,0 +1,375 @@ + +/* + * Copyright 2012 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkObjectParser.h" +#include "SkData.h" +#include "SkFontDescriptor.h" +#include "SkRRect.h" +#include "SkShader.h" +#include "SkStream.h" +#include "SkStringUtils.h" +#include "SkTypeface.h" +#include "SkUtils.h" + +/* TODO(chudy): Replace all std::strings with char */ + +SkString* SkObjectParser::BitmapToString(const SkBitmap& bitmap) { + SkString* mBitmap = new SkString("SkBitmap: "); + mBitmap->append("W: "); + mBitmap->appendS32(bitmap.width()); + mBitmap->append(" H: "); + mBitmap->appendS32(bitmap.height()); + + const char* gConfigStrings[] = { + "None", "A1", "A8", "Index8", "RGB565", "ARGB4444", "ARGB8888", "RLE8" + }; + SkASSERT(SkBitmap::kConfigCount == 8); + + mBitmap->append(" Config: "); + mBitmap->append(gConfigStrings[bitmap.getConfig()]); + + if (bitmap.isOpaque()) { + mBitmap->append(" opaque"); + } else { + mBitmap->append(" not-opaque"); + } + + if (bitmap.isImmutable()) { + mBitmap->append(" immutable"); + } else { + mBitmap->append(" not-immutable"); + } + + if (bitmap.isVolatile()) { + mBitmap->append(" volatile"); + } else { + mBitmap->append(" not-volatile"); + } + + mBitmap->append(" genID: "); + mBitmap->appendS32(bitmap.getGenerationID()); + + return mBitmap; +} + +SkString* SkObjectParser::BoolToString(bool doAA) { + SkString* mBool = new SkString("Bool doAA: "); + if (doAA) { + mBool->append("True"); + } else { + mBool->append("False"); + } + return mBool; +} + +SkString* SkObjectParser::CustomTextToString(const char* text) { + SkString* mText = new SkString(text); + return mText; +} + +SkString* SkObjectParser::IntToString(int x, const char* text) { + SkString* mInt = new SkString(text); + mInt->append(" "); + mInt->appendScalar(SkIntToScalar(x)); + return mInt; +} + +SkString* SkObjectParser::IRectToString(const SkIRect& rect) { + SkString* mRect = new SkString("SkIRect: "); + mRect->append("L: "); + mRect->appendS32(rect.left()); + mRect->append(", T: "); + mRect->appendS32(rect.top()); + mRect->append(", R: "); + mRect->appendS32(rect.right()); + mRect->append(", B: "); + mRect->appendS32(rect.bottom()); + return mRect; +} + +SkString* SkObjectParser::MatrixToString(const SkMatrix& matrix) { + SkString* str = new SkString("SkMatrix: "); +#ifdef SK_DEVELOPER + matrix.toString(str); +#endif + return str; +} + +SkString* SkObjectParser::PaintToString(const SkPaint& paint) { + SkString* str = new SkString; +#ifdef SK_DEVELOPER + paint.toString(str); +#endif + return str; +} + +SkString* SkObjectParser::PathToString(const SkPath& path) { + SkString* mPath = new SkString("Path ("); + + static const char* gFillStrings[] = { + "Winding", "EvenOdd", "InverseWinding", "InverseEvenOdd" + }; + + mPath->append(gFillStrings[path.getFillType()]); + mPath->append(", "); + + static const char* gConvexityStrings[] = { + "Unknown", "Convex", "Concave" + }; + SkASSERT(SkPath::kConcave_Convexity == 2); + + mPath->append(gConvexityStrings[path.getConvexity()]); + mPath->append(", "); + + if (path.isRect(NULL)) { + mPath->append("isRect, "); + } else { + mPath->append("isNotRect, "); + } + + mPath->appendS32(path.countVerbs()); + mPath->append("V, "); + mPath->appendS32(path.countPoints()); + mPath->append("P): "); + + static const char* gVerbStrings[] = { + "Move", "Line", "Quad", "Conic", "Cubic", "Close", "Done" + }; + static const int gPtsPerVerb[] = { 1, 1, 2, 2, 3, 0, 0 }; + static const int gPtOffsetPerVerb[] = { 0, 1, 1, 1, 1, 0, 0 }; + SkASSERT(SkPath::kDone_Verb == 6); + + SkPath::Iter iter(const_cast<SkPath&>(path), false); + SkPath::Verb verb; + SkPoint points[4]; + + for(verb = iter.next(points, false); + verb != SkPath::kDone_Verb; + verb = iter.next(points, false)) { + + mPath->append(gVerbStrings[verb]); + mPath->append(" "); + + for (int i = 0; i < gPtsPerVerb[verb]; ++i) { + mPath->append("("); + mPath->appendScalar(points[gPtOffsetPerVerb[verb]+i].fX); + mPath->append(", "); + mPath->appendScalar(points[gPtOffsetPerVerb[verb]+i].fY); + mPath->append(")"); + } + + if (SkPath::kConic_Verb == verb) { + mPath->append("("); + mPath->appendScalar(iter.conicWeight()); + mPath->append(")"); + } + + mPath->append(" "); + } + + SkString* boundStr = SkObjectParser::RectToString(path.getBounds(), " Bound: "); + + if (NULL != boundStr) { + mPath->append(*boundStr); + SkDELETE(boundStr); + } + + return mPath; +} + +SkString* SkObjectParser::PointsToString(const SkPoint pts[], size_t count) { + SkString* mPoints = new SkString("SkPoints pts[]: "); + for (unsigned int i = 0; i < count; i++) { + mPoints->append("("); + mPoints->appendScalar(pts[i].fX); + mPoints->append(","); + mPoints->appendScalar(pts[i].fY); + mPoints->append(")"); + } + return mPoints; +} + +SkString* SkObjectParser::PointModeToString(SkCanvas::PointMode mode) { + SkString* mMode = new SkString("SkCanvas::PointMode: "); + if (mode == SkCanvas::kPoints_PointMode) { + mMode->append("kPoints_PointMode"); + } else if (mode == SkCanvas::kLines_PointMode) { + mMode->append("kLines_Mode"); + } else if (mode == SkCanvas::kPolygon_PointMode) { + mMode->append("kPolygon_PointMode"); + } + return mMode; +} + +SkString* SkObjectParser::RectToString(const SkRect& rect, const char* title) { + + SkString* mRect = new SkString; + + if (NULL == title) { + mRect->append("SkRect: "); + } else { + mRect->append(title); + } + mRect->append("("); + mRect->appendScalar(rect.left()); + mRect->append(", "); + mRect->appendScalar(rect.top()); + mRect->append(", "); + mRect->appendScalar(rect.right()); + mRect->append(", "); + mRect->appendScalar(rect.bottom()); + mRect->append(")"); + return mRect; +} + +SkString* SkObjectParser::RRectToString(const SkRRect& rrect, const char* title) { + + SkString* mRRect = new SkString; + + if (NULL == title) { + mRRect->append("SkRRect ("); + if (rrect.isEmpty()) { + mRRect->append("empty"); + } else if (rrect.isRect()) { + mRRect->append("rect"); + } else if (rrect.isOval()) { + mRRect->append("oval"); + } else if (rrect.isSimple()) { + mRRect->append("simple"); + } else { + SkASSERT(rrect.isComplex()); + mRRect->append("complex"); + } + mRRect->append("): "); + } else { + mRRect->append(title); + } + mRRect->append("("); + mRRect->appendScalar(rrect.rect().left()); + mRRect->append(", "); + mRRect->appendScalar(rrect.rect().top()); + mRRect->append(", "); + mRRect->appendScalar(rrect.rect().right()); + mRRect->append(", "); + mRRect->appendScalar(rrect.rect().bottom()); + mRRect->append(") radii: ("); + for (int i = 0; i < 4; ++i) { + const SkVector& radii = rrect.radii((SkRRect::Corner) i); + mRRect->appendScalar(radii.fX); + mRRect->append(", "); + mRRect->appendScalar(radii.fY); + if (i < 3) { + mRRect->append(", "); + } + } + mRRect->append(")"); + return mRRect; +} + +SkString* SkObjectParser::RegionOpToString(SkRegion::Op op) { + SkString* mOp = new SkString("SkRegion::Op: "); + if (op == SkRegion::kDifference_Op) { + mOp->append("kDifference_Op"); + } else if (op == SkRegion::kIntersect_Op) { + mOp->append("kIntersect_Op"); + } else if (op == SkRegion::kUnion_Op) { + mOp->append("kUnion_Op"); + } else if (op == SkRegion::kXOR_Op) { + mOp->append("kXOR_Op"); + } else if (op == SkRegion::kReverseDifference_Op) { + mOp->append("kReverseDifference_Op"); + } else if (op == SkRegion::kReplace_Op) { + mOp->append("kReplace_Op"); + } else { + mOp->append("Unknown Type"); + } + return mOp; +} + +SkString* SkObjectParser::RegionToString(const SkRegion& region) { + SkString* mRegion = new SkString("SkRegion: Data unavailable."); + return mRegion; +} + +SkString* SkObjectParser::SaveFlagsToString(SkCanvas::SaveFlags flags) { + SkString* mFlags = new SkString("SkCanvas::SaveFlags: "); + if(flags == SkCanvas::kMatrixClip_SaveFlag) { + mFlags->append("kMatrixClip_SaveFlag"); + } else if (flags == SkCanvas::kClip_SaveFlag) { + mFlags->append("kClip_SaveFlag"); + } else if (flags == SkCanvas::kHasAlphaLayer_SaveFlag) { + mFlags->append("kHasAlphaLayer_SaveFlag"); + } else if (flags == SkCanvas::kFullColorLayer_SaveFlag) { + mFlags->append("kFullColorLayer_SaveFlag"); + } else if (flags == SkCanvas::kClipToLayer_SaveFlag) { + mFlags->append("kClipToLayer_SaveFlag"); + } else if (flags == SkCanvas::kMatrixClip_SaveFlag) { + mFlags->append("kMatrixClip_SaveFlag"); + } else if (flags == SkCanvas::kARGB_NoClipLayer_SaveFlag) { + mFlags->append("kARGB_NoClipLayer_SaveFlag"); + } else if (flags == SkCanvas::kARGB_ClipLayer_SaveFlag) { + mFlags->append("kARGB_ClipLayer_SaveFlag"); + } else { + mFlags->append("Data Unavailable"); + } + return mFlags; +} + +SkString* SkObjectParser::ScalarToString(SkScalar x, const char* text) { + SkString* mScalar = new SkString(text); + mScalar->append(" "); + mScalar->appendScalar(x); + return mScalar; +} + +SkString* SkObjectParser::TextToString(const void* text, size_t byteLength, + SkPaint::TextEncoding encoding) { + + SkString* decodedText = new SkString(); + switch (encoding) { + case SkPaint::kUTF8_TextEncoding: { + decodedText->append("UTF-8: "); + decodedText->append((const char*)text, byteLength); + break; + } + case SkPaint::kUTF16_TextEncoding: { + decodedText->append("UTF-16: "); + size_t sizeNeeded = SkUTF16_ToUTF8((uint16_t*)text, byteLength / 2, NULL); + char* utf8 = new char[sizeNeeded]; + SkUTF16_ToUTF8((uint16_t*)text, byteLength / 2, utf8); + decodedText->append(utf8, sizeNeeded); + delete utf8; + break; + } + case SkPaint::kUTF32_TextEncoding: { + decodedText->append("UTF-32: "); + const SkUnichar* begin = (const SkUnichar*)text; + const SkUnichar* end = (const SkUnichar*)((const char*)text + byteLength); + for (const SkUnichar* unichar = begin; unichar < end; ++unichar) { + decodedText->appendUnichar(*unichar); + } + break; + } + case SkPaint::kGlyphID_TextEncoding: { + decodedText->append("GlyphID: "); + const uint16_t* begin = (const uint16_t*)text; + const uint16_t* end = (const uint16_t*)((const char*)text + byteLength); + for (const uint16_t* glyph = begin; glyph < end; ++glyph) { + decodedText->append("0x"); + decodedText->appendHex(*glyph); + decodedText->append(" "); + } + break; + } + default: + decodedText->append("Unknown text encoding."); + break; + } + + return decodedText; +} diff --git a/src/utils/debugger/SkObjectParser.h b/src/utils/debugger/SkObjectParser.h new file mode 100644 index 0000000000..911b778921 --- /dev/null +++ b/src/utils/debugger/SkObjectParser.h @@ -0,0 +1,128 @@ +/* + * Copyright 2012 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SKOBJECTPARSER_H_ +#define SKOBJECTPARSER_H_ + +#include "SkCanvas.h" +#include "SkString.h" + +/** \class SkObjectParser + + The ObjectParser is used to return string information about parameters + in each draw command. + */ +class SkObjectParser { +public: + + /** + Returns a string about a bitmaps bounds and config. + @param bitmap SkBitmap + */ + static SkString* BitmapToString(const SkBitmap& bitmap); + + /** + Returns a string representation of a boolean. + @param doAA boolean + */ + static SkString* BoolToString(bool doAA); + + /** + Returns a string representation of the text pointer passed in. + */ + static SkString* CustomTextToString(const char* text); + + /** + Returns a string representation of an integer with the text parameter + at the front of the string. + @param x integer + @param text + */ + static SkString* IntToString(int x, const char* text); + /** + Returns a string representation of the SkIRects coordinates. + @param rect SkIRect + */ + static SkString* IRectToString(const SkIRect& rect); + + /** + Returns a string representation of an SkMatrix's contents + @param matrix SkMatrix + */ + static SkString* MatrixToString(const SkMatrix& matrix); + + /** + Returns a string representation of an SkPaint's color + @param paint SkPaint + */ + static SkString* PaintToString(const SkPaint& paint); + + /** + Returns a string representation of a SkPath's points. + @param path SkPath + */ + static SkString* PathToString(const SkPath& path); + + /** + Returns a string representation of the points in the point array. + @param pts[] Array of SkPoints + @param count + */ + static SkString* PointsToString(const SkPoint pts[], size_t count); + + /** + Returns a string representation of the SkCanvas PointMode enum. + */ + static SkString* PointModeToString(SkCanvas::PointMode mode); + + /** + Returns a string representation of the SkRects coordinates. + @param rect SkRect + */ + static SkString* RectToString(const SkRect& rect, const char* title = NULL); + + /** + Returns a string representation of an SkRRect. + @param rrect SkRRect + */ + static SkString* RRectToString(const SkRRect& rrect, const char* title = NULL); + + /** + Returns a string representation of the SkRegion enum. + @param op SkRegion::op enum + */ + static SkString* RegionOpToString(SkRegion::Op op); + + /** + Returns a string representation of the SkRegion. + @param region SkRegion + */ + static SkString* RegionToString(const SkRegion& region); + + /** + Returns a string representation of the SkCanvas::SaveFlags enum. + @param flags SkCanvas::SaveFlags enum + */ + static SkString* SaveFlagsToString(SkCanvas::SaveFlags flags); + + /** + Returns a string representation of an SkScalar with the text parameter + at the front of the string. + @param x SkScalar + @param text + */ + static SkString* ScalarToString(SkScalar x, const char* text); + + /** + Returns a string representation of the char pointer passed in. + @param text const void* that will be cast to a char* + */ + static SkString* TextToString(const void* text, size_t byteLength, + SkPaint::TextEncoding encoding); +}; + +#endif |