1
2 /*
3 * Copyright 2014 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9 #include <QtGui>
10
11 #include "SkDebugger.h"
12 #include "SkDrawCommandGeometryWidget.h"
13
SkDrawCommandGeometryWidget(SkDebugger * debugger)14 SkDrawCommandGeometryWidget::SkDrawCommandGeometryWidget(SkDebugger *debugger)
15 : QFrame()
16 , fDebugger(debugger)
17 , fCommandIndex(-1) {
18 this->setStyleSheet("QFrame {background-color: black; border: 1px solid #cccccc;}");
19 }
20
resizeEvent(QResizeEvent * event)21 void SkDrawCommandGeometryWidget::resizeEvent(QResizeEvent* event) {
22 this->QFrame::resizeEvent(event);
23 QRect r = this->contentsRect();
24 int dim = std::min(r.width(), r.height());
25 if (dim == 0) {
26 fSurface = nullptr;
27 } else {
28 SkImageInfo info = SkImageInfo::MakeN32Premul(dim, dim);
29 fSurface = SkSurface::MakeRaster(info);
30 this->updateImage();
31 }
32 }
33
paintEvent(QPaintEvent * event)34 void SkDrawCommandGeometryWidget::paintEvent(QPaintEvent* event) {
35 this->QFrame::paintEvent(event);
36
37 if (!fSurface) {
38 return;
39 }
40
41 QPainter painter(this);
42 painter.setRenderHint(QPainter::Antialiasing);
43
44 SkPixmap pixmap;
45
46 if (fSurface->peekPixels(&pixmap)) {
47 SkASSERT(pixmap.width() > 0);
48 SkASSERT(pixmap.height() > 0);
49
50 QRectF resultRect;
51 if (this->width() < this->height()) {
52 float ratio = this->width() / pixmap.width();
53 resultRect = QRectF(0, 0, this->width(), ratio * pixmap.height());
54 } else {
55 float ratio = this->height() / pixmap.height();
56 resultRect = QRectF(0, 0, ratio * pixmap.width(), this->height());
57 }
58
59 resultRect.moveCenter(this->contentsRect().center());
60
61 QImage image(reinterpret_cast<const uchar*>(pixmap.addr()),
62 pixmap.width(),
63 pixmap.height(),
64 pixmap.rowBytes(),
65 QImage::Format_ARGB32_Premultiplied);
66 painter.drawImage(resultRect, image);
67 }
68 }
69
setDrawCommandIndex(int commandIndex)70 void SkDrawCommandGeometryWidget::setDrawCommandIndex(int commandIndex) {
71 fCommandIndex = commandIndex;
72 this->updateImage();
73 }
74
updateImage()75 void SkDrawCommandGeometryWidget::updateImage() {
76 if (!fSurface) {
77 return;
78 }
79
80 bool didRender = false;
81 const SkTDArray<SkDrawCommand*>& commands = fDebugger->getDrawCommands();
82 if (0 != commands.count() && fCommandIndex >= 0) {
83 SkASSERT(commands.count() > fCommandIndex);
84 SkDrawCommand* command = commands[fCommandIndex];
85 didRender = command->render(fSurface->getCanvas());
86 }
87
88 if (!didRender) {
89 fSurface->getCanvas()->clear(SK_ColorTRANSPARENT);
90 }
91
92 fSurface->getCanvas()->flush();
93 this->update();
94 }
95