2014-12-31 07:03:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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()
|
2015-01-07 15:33:46 +00:00
|
|
|
, fDebugger(debugger)
|
|
|
|
, fCommandIndex(-1) {
|
2014-12-31 07:03:56 +00:00
|
|
|
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) {
|
2016-03-24 15:29:40 +00:00
|
|
|
fSurface = nullptr;
|
2014-12-31 07:03:56 +00:00
|
|
|
} else {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(dim, dim);
|
2016-03-24 15:29:40 +00:00
|
|
|
fSurface = SkSurface::MakeRaster(info);
|
2014-12-31 07:03:56 +00:00
|
|
|
this->updateImage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkDrawCommandGeometryWidget::paintEvent(QPaintEvent* event) {
|
|
|
|
this->QFrame::paintEvent(event);
|
|
|
|
|
|
|
|
if (!fSurface) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPainter painter(this);
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
|
2016-03-10 14:56:21 +00:00
|
|
|
SkPixmap pixmap;
|
|
|
|
|
|
|
|
if (fSurface->peekPixels(&pixmap)) {
|
|
|
|
SkASSERT(pixmap.width() > 0);
|
|
|
|
SkASSERT(pixmap.height() > 0);
|
2014-12-31 07:03:56 +00:00
|
|
|
|
|
|
|
QRectF resultRect;
|
|
|
|
if (this->width() < this->height()) {
|
2016-03-10 14:56:21 +00:00
|
|
|
float ratio = this->width() / pixmap.width();
|
|
|
|
resultRect = QRectF(0, 0, this->width(), ratio * pixmap.height());
|
2014-12-31 07:03:56 +00:00
|
|
|
} else {
|
2016-03-10 14:56:21 +00:00
|
|
|
float ratio = this->height() / pixmap.height();
|
|
|
|
resultRect = QRectF(0, 0, ratio * pixmap.width(), this->height());
|
2014-12-31 07:03:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resultRect.moveCenter(this->contentsRect().center());
|
|
|
|
|
2016-03-10 14:56:21 +00:00
|
|
|
QImage image(reinterpret_cast<const uchar*>(pixmap.addr()),
|
|
|
|
pixmap.width(),
|
|
|
|
pixmap.height(),
|
|
|
|
pixmap.rowBytes(),
|
2014-12-31 07:03:56 +00:00
|
|
|
QImage::Format_ARGB32_Premultiplied);
|
|
|
|
painter.drawImage(resultRect, image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 15:33:46 +00:00
|
|
|
void SkDrawCommandGeometryWidget::setDrawCommandIndex(int commandIndex) {
|
|
|
|
fCommandIndex = commandIndex;
|
|
|
|
this->updateImage();
|
|
|
|
}
|
|
|
|
|
2014-12-31 07:03:56 +00:00
|
|
|
void SkDrawCommandGeometryWidget::updateImage() {
|
|
|
|
if (!fSurface) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool didRender = false;
|
|
|
|
const SkTDArray<SkDrawCommand*>& commands = fDebugger->getDrawCommands();
|
2015-01-07 15:33:46 +00:00
|
|
|
if (0 != commands.count() && fCommandIndex >= 0) {
|
|
|
|
SkASSERT(commands.count() > fCommandIndex);
|
|
|
|
SkDrawCommand* command = commands[fCommandIndex];
|
2014-12-31 07:03:56 +00:00
|
|
|
didRender = command->render(fSurface->getCanvas());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!didRender) {
|
|
|
|
fSurface->getCanvas()->clear(SK_ColorTRANSPARENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
fSurface->getCanvas()->flush();
|
2015-01-07 15:33:46 +00:00
|
|
|
this->update();
|
2014-12-31 07:03:56 +00:00
|
|
|
}
|