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) {
|
2015-08-27 14:41:13 +00:00
|
|
|
fSurface.reset(nullptr);
|
2015-01-14 06:49:02 +00:00
|
|
|
} else {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(r.width(), r.height());
|
|
|
|
fSurface.reset(SkSurface::NewRaster(info));
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
emit drawComplete();
|
|
|
|
}
|
2012-07-28 23:26:10 +00:00
|
|
|
|
2015-01-14 06:49:02 +00:00
|
|
|
SkImageInfo info;
|
|
|
|
size_t rowBytes;
|
|
|
|
if (const void* pixels = fSurface->peekPixels(&info, &rowBytes)) {
|
|
|
|
QImage image(reinterpret_cast<const uchar*>(pixels),
|
|
|
|
info.width(),
|
|
|
|
info.height(),
|
|
|
|
rowBytes,
|
|
|
|
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();
|
|
|
|
}
|