Add drawing of paths to debugger
authorrobertphillips@google.com <robertphillips@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 21 Nov 2012 17:11:02 +0000 (17:11 +0000)
committerrobertphillips@google.com <robertphillips@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 21 Nov 2012 17:11:02 +0000 (17:11 +0000)
https://codereview.appspot.com/6850082/

git-svn-id: http://skia.googlecode.com/svn/trunk@6532 2bbb7eff-a529-9590-31e7-b0007b416f81

debugger/QT/SkDebuggerGUI.cpp
debugger/QT/SkDebuggerGUI.h
debugger/QT/SkImageWidget.cpp [new file with mode: 0644]
debugger/QT/SkImageWidget.h [new file with mode: 0644]
debugger/SkDebugCanvas.cpp
debugger/SkDrawCommand.cpp
debugger/SkDrawCommand.h
gyp/debugger.gyp

index 710791ace1a8aac306ba502f48dc0e1f254be807..448e449fe4329a0b315245b64b2d31b3570b1c4a 100644 (file)
@@ -45,6 +45,7 @@ SkDebuggerGUI::SkDebuggerGUI(QWidget *parent) :
     , fListWidget(&fCentralWidget)
     , fDirectoryWidget(&fCentralWidget)
     , fCanvasWidget(this, &fDebugger)
+    , fImageWidget(&fDebugger)
     , fMenuBar(this)
     , fMenuFile(this)
     , fMenuNavigate(this)
@@ -385,8 +386,10 @@ void SkDebuggerGUI::actionClearDeletes() {
     }
     if (fPause) {
         fCanvasWidget.drawTo(fPausedRow);
+        fImageWidget.draw();
     } else {
         fCanvasWidget.drawTo(fListWidget.currentRow());
+        fImageWidget.draw();
     }
 }
 
@@ -394,6 +397,7 @@ void SkDebuggerGUI::actionCommandFilter() {
     fDebugger.highlightCurrentCommand(
             fSettingsWidget.getVisibilityButton()->isChecked());
     fCanvasWidget.drawTo(fListWidget.currentRow());
+    fImageWidget.draw();
 }
 
 void SkDebuggerGUI::actionClose() {
@@ -414,8 +418,10 @@ void SkDebuggerGUI::actionDelete() {
 
     if (fPause) {
         fCanvasWidget.drawTo(fPausedRow);
+        fImageWidget.draw();
     } else {
         fCanvasWidget.drawTo(currentRow);
+        fImageWidget.draw();
     }
 }
 
@@ -426,8 +432,10 @@ void SkDebuggerGUI::actionGLWidget(bool isToggled) {
 void SkDebuggerGUI::actionInspector() {
     if (fInspectorWidget.isHidden()) {
         fInspectorWidget.setHidden(false);
+        fImageWidget.setHidden(false);
     } else {
         fInspectorWidget.setHidden(true);
+        fImageWidget.setHidden(true);
     }
 }
 
@@ -531,6 +539,7 @@ void SkDebuggerGUI::pauseDrawing(bool isPaused) {
     fPause = isPaused;
     fPausedRow = fListWidget.currentRow();
     fCanvasWidget.drawTo(fPausedRow);
+    fImageWidget.draw();
 }
 
 void SkDebuggerGUI::registerListClick(QListWidgetItem *item) {
@@ -540,6 +549,7 @@ void SkDebuggerGUI::registerListClick(QListWidgetItem *item) {
         if (currentRow != -1) {
             if (!fPause) {
                 fCanvasWidget.drawTo(currentRow);
+                fImageWidget.draw();
             }
             SkTDArray<SkString*> *currInfo = fDebugger.getCommandInfo(
                     currentRow);
@@ -712,10 +722,17 @@ void SkDebuggerGUI::setupUi(QMainWindow *SkDebuggerGUI) {
     fCanvasWidget.setSizePolicy(QSizePolicy::Expanding,
             QSizePolicy::Expanding);
 
+    fImageWidget.setFixedSize(SkImageWidget::kImageWidgetWidth, 
+                              SkImageWidget::kImageWidgetHeight);
+
     fInspectorWidget.setSizePolicy(QSizePolicy::Expanding,
             QSizePolicy::Expanding);
     fInspectorWidget.setMaximumHeight(300);
 
+    fSettingsAndImageLayout.setSpacing(6);
+    fSettingsAndImageLayout.addWidget(&fSettingsWidget);
+    fSettingsAndImageLayout.addWidget(&fImageWidget);
+
     fSettingsWidget.setSizePolicy(QSizePolicy::Expanding,
             QSizePolicy::Expanding);
     fSettingsWidget.setMaximumWidth(250);
@@ -724,12 +741,13 @@ void SkDebuggerGUI::setupUi(QMainWindow *SkDebuggerGUI) {
     fLeftColumnLayout.addWidget(&fListWidget);
     fLeftColumnLayout.addWidget(&fDirectoryWidget);
 
-    fCanvasAndSettingsLayout.setSpacing(6);
-    fCanvasAndSettingsLayout.addWidget(&fCanvasWidget);
-    fCanvasAndSettingsLayout.addWidget(&fSettingsWidget);
+    fCanvasSettingsAndImageLayout.setSpacing(6);
+    fCanvasSettingsAndImageLayout.addWidget(&fCanvasWidget);
+    fCanvasSettingsAndImageLayout.addLayout(&fSettingsAndImageLayout);
+
 
     fMainAndRightColumnLayout.setSpacing(6);
-    fMainAndRightColumnLayout.addLayout(&fCanvasAndSettingsLayout);
+    fMainAndRightColumnLayout.addLayout(&fCanvasSettingsAndImageLayout);
     fMainAndRightColumnLayout.addWidget(&fInspectorWidget);
 
     fCentralWidget.setLayout(&fContainerLayout);
index c74ed808595bfa9bcf8a6c4baaea3dc7362473ed..aed40721fe736e6c056f2a5567892430748fc920 100644 (file)
@@ -17,6 +17,7 @@
 #include "SkListWidget.h"
 #include "SkInspectorWidget.h"
 #include "SkRasterWidget.h"
+#include "SkImageWidget.h"
 #include "SkSettingsWidget.h"
 #include <QtCore/QVariant>
 #include <QtGui/QAction>
@@ -244,13 +245,15 @@ private:
     QHBoxLayout fContainerLayout;
     QVBoxLayout fLeftColumnLayout;
     QVBoxLayout fMainAndRightColumnLayout;
-    QHBoxLayout fCanvasAndSettingsLayout;
+    QHBoxLayout fCanvasSettingsAndImageLayout;
+    QVBoxLayout fSettingsAndImageLayout;
 
     QListWidget fListWidget;
     QListWidget fDirectoryWidget;
 
     SkDebugger fDebugger;
     SkCanvasWidget fCanvasWidget;
+    SkImageWidget fImageWidget;
     SkInspectorWidget fInspectorWidget;
     SkSettingsWidget fSettingsWidget;
 
diff --git a/debugger/QT/SkImageWidget.cpp b/debugger/QT/SkImageWidget.cpp
new file mode 100644 (file)
index 0000000..36a1b3d
--- /dev/null
@@ -0,0 +1,54 @@
+
+/*
+ * 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 <QtGui>
+
+#include "SkDebugger.h"
+#include "SkImageWidget.h"
+
+SkImageWidget::SkImageWidget(SkDebugger *debugger) 
+    : QWidget()
+    , fDebugger(debugger) {
+    this->setStyleSheet("QWidget {background-color: white; border: 1px solid #cccccc;}");
+}
+
+void SkImageWidget::paintEvent(QPaintEvent* event) {
+    if (this->isHidden()) {
+        return;
+    }
+
+    QPainter painter(this);
+    QStyleOption opt;
+    opt.init(this);
+
+    style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
+
+    const SkTDArray<SkDrawCommand*>& commands = fDebugger->getDrawCommands();
+    if (0 != commands.count()) {
+        SkDrawCommand* command = commands[fDebugger->index()];
+
+        const SkBitmap* bitmap = command->getBitmap();
+
+        if (NULL != bitmap) {
+            bitmap->lockPixels();
+
+            QPoint origin(0,0);
+            QImage image((uchar *)bitmap->getPixels(), bitmap->width(),
+                    bitmap->height(), QImage::Format_ARGB32_Premultiplied);
+
+            painter.drawImage(origin, image);
+
+            bitmap->unlockPixels();
+        } else {
+            painter.drawRect(0, 0, kImageWidgetWidth, kImageWidgetHeight);
+        }
+    }
+
+    painter.end();
+    emit drawComplete();
+}
diff --git a/debugger/QT/SkImageWidget.h b/debugger/QT/SkImageWidget.h
new file mode 100644 (file)
index 0000000..594b21a
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * 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 SKIMAGEWIDGET_H_
+#define SKIMAGEWIDGET_H_
+
+#include <QWidget>
+
+class SkDebugger;
+
+class  SkImageWidget : public QWidget {
+    Q_OBJECT
+
+public:
+    SkImageWidget(SkDebugger* debugger);
+
+    void draw() {
+        this->update();
+    }
+
+    static const int kImageWidgetWidth = 256;
+    static const int kImageWidgetHeight = 256;
+
+signals:
+    void drawComplete();
+
+protected:
+    void paintEvent(QPaintEvent* event);
+
+private:
+    SkDebugger *fDebugger;
+};
+
+#endif /* SKIMAGEWIDGET_H_ */
index f6c8082c2da1a9205ae2310845f6c7ef180d4fe5..acb9a7df6c15309cf1ef60ab7503035036c4f28c 100644 (file)
@@ -10,6 +10,8 @@
 #include <iostream>
 #include "SkDebugCanvas.h"
 #include "SkDrawCommand.h"
+#include "SkDevice.h"
+#include "SkImageWidget.h"
 
 static SkBitmap make_noconfig_bm(int width, int height) {
     SkBitmap bm;
@@ -165,8 +167,40 @@ void SkDebugCanvas::clear(SkColor color) {
     addDrawCommand(new Clear(color));
 }
 
+static SkBitmap createBitmap(const SkPath& path) {
+    SkBitmap bitmap;
+    bitmap.setConfig(SkBitmap::kARGB_8888_Config, 
+                     SkImageWidget::kImageWidgetWidth, 
+                     SkImageWidget::kImageWidgetHeight);
+    bitmap.allocPixels();
+    bitmap.eraseColor(SK_ColorWHITE);
+    SkDevice* device = new SkDevice(bitmap);
+
+    SkCanvas canvas(device);
+    device->unref();
+
+    const SkRect& bounds = path.getBounds();
+
+    if (bounds.width() > bounds.height()) {
+        canvas.scale(SkDoubleToScalar((0.9*SkImageWidget::kImageWidgetWidth)/bounds.width()), 
+                     SkDoubleToScalar((0.9*SkImageWidget::kImageWidgetHeight)/bounds.width()));
+    } else {
+        canvas.scale(SkDoubleToScalar((0.9*SkImageWidget::kImageWidgetWidth)/bounds.height()), 
+                     SkDoubleToScalar((0.9*SkImageWidget::kImageWidgetHeight)/bounds.height()));
+    }
+    canvas.translate(-bounds.fLeft+2, -bounds.fTop+2);
+
+    SkPaint p;
+    p.setColor(SK_ColorBLACK);
+    p.setStyle(SkPaint::kStroke_Style);
+
+    canvas.drawPath(path, p);
+
+    return bitmap;
+}
+
 bool SkDebugCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) {
-    addDrawCommand(new ClipPath(path, op, doAA));
+    addDrawCommand(new ClipPath(path, op, doAA, createBitmap(path)));
     return true;
 }
 
@@ -214,7 +248,7 @@ void SkDebugCanvas::drawPaint(const SkPaint& paint) {
 }
 
 void SkDebugCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
-    addDrawCommand(new DrawPath(path, paint));
+    addDrawCommand(new DrawPath(path, paint, createBitmap(path)));
 }
 
 void SkDebugCanvas::drawPicture(SkPicture& picture) {
index 74c92588d09458d42e132931717bbe87397188b6..45aaaf2c78c35001e2d354e12a53402c0655ae11 100644 (file)
@@ -75,11 +75,12 @@ void Clear::execute(SkCanvas* canvas) {
     canvas->clear(this->fColor);
 }
 
-ClipPath::ClipPath(const SkPath& path, SkRegion::Op op, bool doAA) {
+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;
 
     this->fInfo.push(SkObjectParser::PathToString(path));
     this->fInfo.push(SkObjectParser::RegionOpToString(op));
@@ -90,6 +91,10 @@ void ClipPath::execute(SkCanvas* canvas) {
     canvas->clipPath(*this->fPath, this->fOp, this->fDoAA);
 }
 
+const SkBitmap* ClipPath::getBitmap() const {
+    return &fBitmap;
+}
+
 ClipRegion::ClipRegion(const SkRegion& region, SkRegion::Op op) {
     this->fRegion = &region;
     this->fOp = op;
@@ -220,9 +225,10 @@ void DrawPaint::execute(SkCanvas* canvas) {
     canvas->drawPaint(*this->fPaint);
 }
 
-DrawPath::DrawPath(const SkPath& path, const SkPaint& paint) {
+DrawPath::DrawPath(const SkPath& path, const SkPaint& paint, SkBitmap& bitmap) {
     this->fPath = &path;
     this->fPaint = &paint;
+    this->fBitmap = bitmap;
     this->fDrawType = DRAW_PATH;
 
     this->fInfo.push(SkObjectParser::PathToString(path));
@@ -233,6 +239,10 @@ void DrawPath::execute(SkCanvas* canvas) {
     canvas->drawPath(*this->fPath, *this->fPaint);
 }
 
+const SkBitmap* DrawPath::getBitmap() const {
+    return &fBitmap;
+}
+
 DrawPicture::DrawPicture(SkPicture& picture) {
     this->fPicture = &picture;
     this->fDrawType = DRAW_PICTURE;
index 54fbf9ee15f79230edb594a001cd6cd334bff423..5d2c065fff6f44b971f2f7fc4e202fbd2d467572 100644 (file)
@@ -37,6 +37,8 @@ public:
     virtual void execute(SkCanvas* canvas)=0;
     DrawType getType() { return fDrawType; };
 
+    virtual const SkBitmap* getBitmap() const { return NULL; }
+
     static const char* GetCommandString(DrawType type);
 
 protected:
@@ -63,12 +65,14 @@ private:
 
 class ClipPath : public SkDrawCommand {
 public:
-    ClipPath(const SkPath& path, SkRegion::Op op, bool doAA);
+    ClipPath(const SkPath& path, SkRegion::Op op, bool doAA, SkBitmap& bitmap);
     virtual void execute(SkCanvas* canvas) SK_OVERRIDE;
+    virtual const SkBitmap* getBitmap() const SK_OVERRIDE;
 private:
     const SkPath* fPath;
     SkRegion::Op fOp;
     bool fDoAA;
+    SkBitmap fBitmap;
 };
 
 class ClipRegion : public SkDrawCommand {
@@ -164,11 +168,14 @@ private:
 
 class DrawPath : public SkDrawCommand {
 public:
-    DrawPath(const SkPath& path, const SkPaint& paint);
+    DrawPath(const SkPath& path, const SkPaint& paint, SkBitmap& bitmap);
     virtual void execute(SkCanvas* canvas) SK_OVERRIDE;
+    virtual const SkBitmap* DrawPath::getBitmap() const SK_OVERRIDE;
+
 private:
     const SkPath* fPath;
     const SkPaint* fPaint;
+    SkBitmap fBitmap;
 };
 
 class DrawPicture : public SkDrawCommand {
index 3d44cbc3ff61cb23ad1a5fe088fcafb75555c703..5c760db12a23fb55459f43dc61ff440e01498824 100644 (file)
         '../debugger/QT/SkGLWidget.cpp',
         '../debugger/QT/SkRasterWidget.h',
         '../debugger/QT/SkRasterWidget.cpp',
+        '../debugger/QT/SkImageWidget.h',
+        '../debugger/QT/SkImageWidget.cpp',
 
         # To update this file edit SkIcons.qrc and rerun rcc to generate cpp
         '../debugger/QT/qrc_SkIcons.cpp',
         '<(moc_gen_dir)/moc_SkInspectorWidget.cpp',
         '<(moc_gen_dir)/moc_SkSettingsWidget.cpp',
         '<(moc_gen_dir)/moc_SkRasterWidget.cpp',
+        '<(moc_gen_dir)/moc_SkImageWidget.cpp',
         '<(moc_gen_dir)/moc_SkGLWidget.cpp',
       ],
       'dependencies': [
         '<(moc_src_dir)/SkInspectorWidget.h',
         '<(moc_src_dir)/SkSettingsWidget.h',
         '<(moc_src_dir)/SkRasterWidget.h',
+        '<(moc_src_dir)/SkImageWidget.h',
         '<(moc_src_dir)/SkGLWidget.h',
       ],
       'rules': [