aboutsummaryrefslogtreecommitdiffhomepage
path: root/debugger/QT/SkDrawCommandGeometryWidget.cpp
blob: 2134077acfa6cabc49dd3ca5fea6172ab1c99dc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

/*
 * Copyright 2014 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 "SkDrawCommandGeometryWidget.h"

SkDrawCommandGeometryWidget::SkDrawCommandGeometryWidget(SkDebugger *debugger)
    : QFrame()
    , fDebugger(debugger)
    , fCommandIndex(-1) {
    this->setStyleSheet("QFrame {background-color: black; border: 1px solid #cccccc;}");
}

void SkDrawCommandGeometryWidget::resizeEvent(QResizeEvent* event) {
    this->QFrame::resizeEvent(event);
    QRect r = this->contentsRect();
    int dim = std::min(r.width(), r.height());
    if (dim == 0) {
        fSurface = nullptr;
    } else {
        SkImageInfo info = SkImageInfo::MakeN32Premul(dim, dim);
        fSurface = SkSurface::MakeRaster(info);
        this->updateImage();
    }
}

void SkDrawCommandGeometryWidget::paintEvent(QPaintEvent* event) {
    this->QFrame::paintEvent(event);

    if (!fSurface) {
        return;
    }

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    SkPixmap pixmap;

    if (fSurface->peekPixels(&pixmap)) {
        SkASSERT(pixmap.width() > 0);
        SkASSERT(pixmap.height() > 0);

        QRectF resultRect;
        if (this->width() < this->height()) {
            float ratio = this->width() / pixmap.width();
            resultRect = QRectF(0, 0, this->width(), ratio * pixmap.height());
        } else {
            float ratio = this->height() / pixmap.height();
            resultRect = QRectF(0, 0, ratio * pixmap.width(), this->height());
        }

        resultRect.moveCenter(this->contentsRect().center());

        QImage image(reinterpret_cast<const uchar*>(pixmap.addr()),
                     pixmap.width(),
                     pixmap.height(),
                     pixmap.rowBytes(),
                     QImage::Format_ARGB32_Premultiplied);
        painter.drawImage(resultRect, image);
    }
}

void SkDrawCommandGeometryWidget::setDrawCommandIndex(int commandIndex) {
    fCommandIndex = commandIndex;
    this->updateImage();
}

void SkDrawCommandGeometryWidget::updateImage() {
    if (!fSurface) {
        return;
    }

    bool didRender = false;
    const SkTDArray<SkDrawCommand*>& commands = fDebugger->getDrawCommands();
    if (0 != commands.count() && fCommandIndex >= 0) {
        SkASSERT(commands.count() > fCommandIndex);
        SkDrawCommand* command = commands[fCommandIndex];
        didRender = command->render(fSurface->getCanvas());
    }

    if (!didRender) {
        fSurface->getCanvas()->clear(SK_ColorTRANSPARENT);
    }

    fSurface->getCanvas()->flush();
    this->update();
}