Making SkDrawCommand more robust

https://codereview.appspot.com/7486052/



git-svn-id: http://skia.googlecode.com/svn/trunk@8181 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/debugger/SkDrawCommand.cpp b/debugger/SkDrawCommand.cpp
index c85d8c9..a24b891 100644
--- a/debugger/SkDrawCommand.cpp
+++ b/debugger/SkDrawCommand.cpp
@@ -80,19 +80,19 @@
 }
 
 ClipPath::ClipPath(const SkPath& path, SkRegion::Op op, bool doAA, SkBitmap& bitmap) {
-    this->fPath = &path;
-    this->fOp = op;
-    this->fDoAA = doAA;
-    this->fDrawType = CLIP_PATH;
-    this->fBitmap = bitmap;
+    fPath = path;
+    fOp = op;
+    fDoAA = doAA;
+    fDrawType = CLIP_PATH;
+    fBitmap = bitmap;
 
-    this->fInfo.push(SkObjectParser::PathToString(path));
-    this->fInfo.push(SkObjectParser::RegionOpToString(op));
-    this->fInfo.push(SkObjectParser::BoolToString(doAA));
+    fInfo.push(SkObjectParser::PathToString(path));
+    fInfo.push(SkObjectParser::RegionOpToString(op));
+    fInfo.push(SkObjectParser::BoolToString(doAA));
 }
 
 void ClipPath::execute(SkCanvas* canvas) {
-    canvas->clipPath(*this->fPath, this->fOp, this->fDoAA);
+    canvas->clipPath(fPath, fOp, fDoAA);
 }
 
 const SkBitmap* ClipPath::getBitmap() const {
@@ -227,31 +227,37 @@
 }
 
 DrawBitmapRect::DrawBitmapRect(const SkBitmap& bitmap, const SkRect* src,
-        const SkRect& dst, const SkPaint* paint, SkBitmap& resizedBitmap) {
-    this->fBitmap = &bitmap;
-    this->fSrc = src;
-    this->fDst = &dst;
-    if (NULL != paint) {
-        this->fPaint = *paint;
-        this->fPaintPtr = &this->fPaint;
-    } else {
-        this->fPaintPtr = NULL;
-    }
-    this->fDrawType = DRAW_BITMAP_RECT_TO_RECT;
-    this->fResizedBitmap = resizedBitmap;
-
-    this->fInfo.push(SkObjectParser::BitmapToString(bitmap));
+                               const SkRect& dst, const SkPaint* paint, 
+                               SkBitmap& resizedBitmap) {
+    fBitmap = bitmap;
     if (NULL != src) {
-        this->fInfo.push(SkObjectParser::RectToString(*src, "Src: "));
+        fSrc = *src;
+    } else {
+        fSrc.setEmpty();
     }
-    this->fInfo.push(SkObjectParser::RectToString(dst, "Dst: "));
+    fDst = dst;
+
     if (NULL != paint) {
-        this->fInfo.push(SkObjectParser::PaintToString(*paint));
+        fPaint = *paint;
+        fPaintPtr = &fPaint;
+    } else {
+        fPaintPtr = NULL;
+    }
+    fDrawType = DRAW_BITMAP_RECT_TO_RECT;
+    fResizedBitmap = resizedBitmap;
+
+    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(*this->fBitmap, this->fSrc, *this->fDst, this->fPaintPtr);
+    canvas->drawBitmapRectToRect(fBitmap, this->srcRect(), fDst, fPaintPtr);
 }
 
 const SkBitmap* DrawBitmapRect::getBitmap() const {
@@ -294,17 +300,17 @@
 }
 
 DrawPath::DrawPath(const SkPath& path, const SkPaint& paint, SkBitmap& bitmap) {
-    this->fPath = &path;
-    this->fPaint = &paint;
-    this->fBitmap = bitmap;
-    this->fDrawType = DRAW_PATH;
+    fPath = path;
+    fPaint = paint;
+    fBitmap = bitmap;
+    fDrawType = DRAW_PATH;
 
-    this->fInfo.push(SkObjectParser::PathToString(path));
-    this->fInfo.push(SkObjectParser::PaintToString(paint));
+    fInfo.push(SkObjectParser::PathToString(path));
+    fInfo.push(SkObjectParser::PaintToString(paint));
 }
 
 void DrawPath::execute(SkCanvas* canvas) {
-    canvas->drawPath(*this->fPath, *this->fPaint);
+    canvas->drawPath(fPath, fPaint);
 }
 
 const SkBitmap* DrawPath::getBitmap() const {
@@ -360,36 +366,36 @@
 
 
 DrawPosTextH::DrawPosTextH(const void* text, size_t byteLength,
-        const SkScalar xpos[], SkScalar constY, const SkPaint& paint) {
-    this->fText = text;
-    this->fByteLength = byteLength;
-    this->fXpos = xpos;
-    this->fConstY = constY;
-    this->fPaint = &paint;
-    this->fDrawType = DRAW_POS_TEXT_H;
+                           const SkScalar xpos[], SkScalar constY, 
+                           const SkPaint& paint) {
+    fText = text;
+    fByteLength = byteLength;
+    fXpos = xpos;
+    fConstY = constY;
+    fPaint = paint;
+    fDrawType = DRAW_POS_TEXT_H;
 
-    this->fInfo.push(SkObjectParser::TextToString(text, byteLength, paint.getTextEncoding()));
-    this->fInfo.push(SkObjectParser::ScalarToString(xpos[0], "XPOS: "));
-    this->fInfo.push(SkObjectParser::ScalarToString(constY, "SkScalar constY: "));
-    this->fInfo.push(SkObjectParser::PaintToString(paint));
+    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(this->fText, this->fByteLength, this->fXpos, this->fConstY,
-            *this->fPaint);
+    canvas->drawPosTextH(fText, fByteLength, fXpos, fConstY, fPaint);
 }
 
 DrawRectC::DrawRectC(const SkRect& rect, const SkPaint& paint) {
-    this->fRect = ▭
-    this->fPaint = &paint;
-    this->fDrawType = DRAW_RECT;
+    fRect = rect;
+    fPaint = paint;
+    fDrawType = DRAW_RECT;
 
-    this->fInfo.push(SkObjectParser::RectToString(rect));
-    this->fInfo.push(SkObjectParser::PaintToString(paint));
+    fInfo.push(SkObjectParser::RectToString(rect));
+    fInfo.push(SkObjectParser::PaintToString(paint));
 }
 
 void DrawRectC::execute(SkCanvas* canvas) {
-    canvas->drawRect(*this->fRect, *this->fPaint);
+    canvas->drawRect(fRect, fPaint);
 }
 
 DrawRRect::DrawRRect(const SkRRect& rrect, const SkPaint& paint) {
diff --git a/debugger/SkDrawCommand.h b/debugger/SkDrawCommand.h
index d06e7fe..3ede8ff 100644
--- a/debugger/SkDrawCommand.h
+++ b/debugger/SkDrawCommand.h
@@ -73,10 +73,12 @@
     virtual void execute(SkCanvas* canvas) SK_OVERRIDE;
     virtual const SkBitmap* getBitmap() const SK_OVERRIDE;
 private:
-    const SkPath* fPath;
+    SkPath fPath;
     SkRegion::Op fOp;
     bool fDoAA;
     SkBitmap fBitmap;
+
+    typedef SkDrawCommand INHERITED;
 };
 
 class ClipRegion : public SkDrawCommand {
@@ -170,7 +172,8 @@
 class DrawBitmapRect : public SkDrawCommand {
 public:
     DrawBitmapRect(const SkBitmap& bitmap, const SkRect* src,
-            const SkRect& dst, const SkPaint* paint, SkBitmap& resizedBitmap);
+                   const SkRect& dst, const SkPaint* paint, 
+                   SkBitmap& resizedBitmap);
     virtual void execute(SkCanvas* canvas) SK_OVERRIDE;
     virtual const SkBitmap* getBitmap() const SK_OVERRIDE;
 
@@ -183,15 +186,18 @@
 
     void setPaint(const SkPaint& paint) { fPaint = paint; fPaintPtr = &fPaint; }
 
-    const SkRect& dstRect() { return *fDst; }
+    const SkRect* srcRect() const { return fSrc.isEmpty() ? NULL : &fSrc; }
+    const SkRect& dstRect() const { return fDst; }
 
 private:
-    const SkRect* fSrc;
+    SkRect  fSrc;
     SkPaint fPaint;
     SkPaint* fPaintPtr;
-    const SkBitmap* fBitmap;
-    const SkRect* fDst;
+    SkBitmap fBitmap;
+    SkRect  fDst;
     SkBitmap fResizedBitmap;
+
+    typedef SkDrawCommand INHERITED;
 };
 
 class DrawData : public SkDrawCommand {
@@ -227,9 +233,11 @@
     virtual const SkBitmap* getBitmap() const SK_OVERRIDE;
 
 private:
-    const SkPath* fPath;
-    const SkPaint* fPaint;
+    SkPath  fPath;
+    SkPaint fPaint;
     SkBitmap fBitmap;
+
+    typedef SkDrawCommand INHERITED;
 };
 
 class DrawPicture : public SkDrawCommand {
@@ -296,14 +304,16 @@
 class DrawPosTextH : public SkDrawCommand {
 public:
     DrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
-            SkScalar constY, const SkPaint& paint);
+                 SkScalar constY, const SkPaint& paint);
     virtual void execute(SkCanvas* canvas) SK_OVERRIDE;
 private:
     const SkScalar* fXpos;
     const void* fText;
     size_t fByteLength;
     SkScalar fConstY;
-    const SkPaint* fPaint;
+    SkPaint fPaint;
+
+    typedef SkDrawCommand INHERITED;
 };
 
 class DrawRectC : public SkDrawCommand {
@@ -311,11 +321,13 @@
     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; }
+    const SkRect& rect() const   { return fRect; }
+    const SkPaint& paint() const { return fPaint; }
 private:
-    const SkRect* fRect;
-    const SkPaint* fPaint;
+    SkRect  fRect;
+    SkPaint fPaint;
+
+    typedef SkDrawCommand INHERITED;
 };
 
 class DrawRRect : public SkDrawCommand {
diff --git a/tools/filtermain.cpp b/tools/filtermain.cpp
index 74cf427..5d7241e 100644
--- a/tools/filtermain.cpp
+++ b/tools/filtermain.cpp
@@ -238,7 +238,7 @@
     restore->setVisible(false);
 
     // TODO: could skip paint re-creation if the AA settings already match
-    SkPaint newPaint = *dr->paint();
+    SkPaint newPaint = dr->paint();
     newPaint.setAntiAlias(crr->doAA());
     DrawRRect* drr = new DrawRRect(crr->rrect(), newPaint);
     commands[curCommand+2] = drr;