blob: 378fca64c923d44e77f71967a7a8c1d8eec27961 [file] [log] [blame]
chudy@google.com38b08ce2012-07-28 23:26:10 +00001
2/*
3 * Copyright 2012 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
chudy@google.com38b08ce2012-07-28 23:26:10 +00009#include "SkRasterWidget.h"
kkinnunencfdc0e32015-01-13 22:49:02 -080010#include "SkDebugger.h"
11#include <QtGui>
chudy@google.com38b08ce2012-07-28 23:26:10 +000012
kkinnunencfdc0e32015-01-13 22:49:02 -080013SkRasterWidget::SkRasterWidget(SkDebugger *debugger)
14 : QWidget()
15 , fDebugger(debugger)
16 , fNeedImageUpdate(false) {
kkinnunen534c63e2014-12-22 05:56:45 -080017 this->setStyleSheet("QWidget {background-color: black; border: 1px solid #cccccc;}");
chudy@google.com38b08ce2012-07-28 23:26:10 +000018}
19
chudy@google.com38b08ce2012-07-28 23:26:10 +000020void SkRasterWidget::resizeEvent(QResizeEvent* event) {
kkinnunencfdc0e32015-01-13 22:49:02 -080021 this->QWidget::resizeEvent(event);
22
23 QRect r = this->contentsRect();
24 if (r.width() == 0 || r.height() == 0) {
robertphillipsd98120e2016-03-24 08:29:40 -070025 fSurface = nullptr;
kkinnunencfdc0e32015-01-13 22:49:02 -080026 } else {
27 SkImageInfo info = SkImageInfo::MakeN32Premul(r.width(), r.height());
robertphillipsd98120e2016-03-24 08:29:40 -070028 fSurface = SkSurface::MakeRaster(info);
kkinnunencfdc0e32015-01-13 22:49:02 -080029 }
30 this->updateImage();
chudy@google.com38b08ce2012-07-28 23:26:10 +000031}
32
33void SkRasterWidget::paintEvent(QPaintEvent* event) {
kkinnunencfdc0e32015-01-13 22:49:02 -080034 QPainter painter(this);
35 painter.setRenderHint(QPainter::Antialiasing);
36 QStyleOption opt;
37 opt.init(this);
38 style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
chudy@google.com80a4a602012-07-30 18:54:07 +000039
kkinnunencfdc0e32015-01-13 22:49:02 -080040 if (!fSurface) {
41 return;
42 }
chudy@google.com80a4a602012-07-30 18:54:07 +000043
kkinnunencfdc0e32015-01-13 22:49:02 -080044 if (fNeedImageUpdate) {
45 fDebugger->draw(fSurface->getCanvas());
46 fSurface->getCanvas()->flush();
47 fNeedImageUpdate = false;
robertphillips9ea8acd2016-03-01 09:34:38 -080048 Q_EMIT drawComplete();
chudy@google.com38b08ce2012-07-28 23:26:10 +000049 }
kkinnunencfdc0e32015-01-13 22:49:02 -080050
robertphillipsf1d746c2016-03-10 06:56:21 -080051 SkPixmap pixmap;
52
53 if (fSurface->peekPixels(&pixmap)) {
54 QImage image(reinterpret_cast<const uchar*>(pixmap.addr()),
55 pixmap.width(),
56 pixmap.height(),
57 pixmap.rowBytes(),
kkinnunencfdc0e32015-01-13 22:49:02 -080058 QImage::Format_ARGB32_Premultiplied);
59#if SK_R32_SHIFT == 0
60 painter.drawImage(this->contentsRect(), image.rgbSwapped());
61#else
62 painter.drawImage(this->contentsRect(), image);
63#endif
64 }
65}
66
67void SkRasterWidget::updateImage() {
68 if (!fSurface) {
69 return;
70 }
71 fNeedImageUpdate = true;
72 this->update();
chudy@google.com38b08ce2012-07-28 23:26:10 +000073}