2012-07-28 23:26:10 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 "SkRasterWidget.h"
|
2015-01-14 06:49:02 +00:00
|
|
|
#include "SkDebugger.h"
|
|
|
|
#include <QtGui>
|
2012-07-28 23:26:10 +00:00
|
|
|
|
2015-01-14 06:49:02 +00:00
|
|
|
SkRasterWidget::SkRasterWidget(SkDebugger *debugger)
|
|
|
|
: QWidget()
|
|
|
|
, fDebugger(debugger)
|
|
|
|
, fNeedImageUpdate(false) {
|
2014-12-22 13:56:45 +00:00
|
|
|
this->setStyleSheet("QWidget {background-color: black; border: 1px solid #cccccc;}");
|
2012-07-28 23:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkRasterWidget::resizeEvent(QResizeEvent* event) {
|
2015-01-14 06:49:02 +00:00
|
|
|
this->QWidget::resizeEvent(event);
|
|
|
|
|
|
|
|
QRect r = this->contentsRect();
|
|
|
|
if (r.width() == 0 || r.height() == 0) {
|
2016-03-24 15:29:40 +00:00
|
|
|
fSurface = nullptr;
|
2015-01-14 06:49:02 +00:00
|
|
|
} else {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(r.width(), r.height());
|
2016-03-24 15:29:40 +00:00
|
|
|
fSurface = SkSurface::MakeRaster(info);
|
2015-01-14 06:49:02 +00:00
|
|
|
}
|
|
|
|
this->updateImage();
|
2012-07-28 23:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkRasterWidget::paintEvent(QPaintEvent* event) {
|
2015-01-14 06:49:02 +00:00
|
|
|
QPainter painter(this);
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
QStyleOption opt;
|
|
|
|
opt.init(this);
|
|
|
|
style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
|
2012-07-28 23:26:10 +00:00
|
|
|
|
2015-01-14 06:49:02 +00:00
|
|
|
if (!fSurface) {
|
|
|
|
return;
|
|
|
|
}
|
2012-07-28 23:26:10 +00:00
|
|
|
|
2015-01-14 06:49:02 +00:00
|
|
|
if (fNeedImageUpdate) {
|
|
|
|
fDebugger->draw(fSurface->getCanvas());
|
|
|
|
fSurface->getCanvas()->flush();
|
|
|
|
fNeedImageUpdate = false;
|
2016-03-01 17:34:38 +00:00
|
|
|
Q_EMIT drawComplete();
|
2015-01-14 06:49:02 +00:00
|
|
|
}
|
2012-07-28 23:26:10 +00:00
|
|
|
|
2016-03-10 14:56:21 +00:00
|
|
|
SkPixmap pixmap;
|
|
|
|
|
|
|
|
if (fSurface->peekPixels(&pixmap)) {
|
|
|
|
QImage image(reinterpret_cast<const uchar*>(pixmap.addr()),
|
|
|
|
pixmap.width(),
|
|
|
|
pixmap.height(),
|
|
|
|
pixmap.rowBytes(),
|
2015-01-14 06:49:02 +00:00
|
|
|
QImage::Format_ARGB32_Premultiplied);
|
2013-07-10 15:53:36 +00:00
|
|
|
#if SK_R32_SHIFT == 0
|
2015-01-14 06:49:02 +00:00
|
|
|
painter.drawImage(this->contentsRect(), image.rgbSwapped());
|
2013-07-10 15:53:36 +00:00
|
|
|
#else
|
2015-01-14 06:49:02 +00:00
|
|
|
painter.drawImage(this->contentsRect(), image);
|
2013-07-10 15:53:36 +00:00
|
|
|
#endif
|
2012-07-30 18:54:07 +00:00
|
|
|
}
|
2012-07-28 23:26:10 +00:00
|
|
|
}
|
2015-01-14 06:49:02 +00:00
|
|
|
|
|
|
|
void SkRasterWidget::updateImage() {
|
|
|
|
if (!fSurface) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fNeedImageUpdate = true;
|
|
|
|
this->update();
|
|
|
|
}
|