chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 1 | |
| 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.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 9 | #include "SkRasterWidget.h" |
kkinnunen | cfdc0e3 | 2015-01-13 22:49:02 -0800 | [diff] [blame] | 10 | #include "SkDebugger.h" |
| 11 | #include <QtGui> |
chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 12 | |
kkinnunen | cfdc0e3 | 2015-01-13 22:49:02 -0800 | [diff] [blame] | 13 | SkRasterWidget::SkRasterWidget(SkDebugger *debugger) |
| 14 | : QWidget() |
| 15 | , fDebugger(debugger) |
| 16 | , fNeedImageUpdate(false) { |
kkinnunen | 534c63e | 2014-12-22 05:56:45 -0800 | [diff] [blame] | 17 | this->setStyleSheet("QWidget {background-color: black; border: 1px solid #cccccc;}"); |
chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 18 | } |
| 19 | |
chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 20 | void SkRasterWidget::resizeEvent(QResizeEvent* event) { |
kkinnunen | cfdc0e3 | 2015-01-13 22:49:02 -0800 | [diff] [blame] | 21 | this->QWidget::resizeEvent(event); |
| 22 | |
| 23 | QRect r = this->contentsRect(); |
| 24 | if (r.width() == 0 || r.height() == 0) { |
| 25 | fSurface.reset(NULL); |
| 26 | } else { |
| 27 | SkImageInfo info = SkImageInfo::MakeN32Premul(r.width(), r.height()); |
| 28 | fSurface.reset(SkSurface::NewRaster(info)); |
| 29 | } |
| 30 | this->updateImage(); |
chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void SkRasterWidget::paintEvent(QPaintEvent* event) { |
kkinnunen | cfdc0e3 | 2015-01-13 22:49:02 -0800 | [diff] [blame] | 34 | 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.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 39 | |
kkinnunen | cfdc0e3 | 2015-01-13 22:49:02 -0800 | [diff] [blame] | 40 | if (!fSurface) { |
| 41 | return; |
| 42 | } |
chudy@google.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 43 | |
kkinnunen | cfdc0e3 | 2015-01-13 22:49:02 -0800 | [diff] [blame] | 44 | if (fNeedImageUpdate) { |
| 45 | fDebugger->draw(fSurface->getCanvas()); |
| 46 | fSurface->getCanvas()->flush(); |
| 47 | fNeedImageUpdate = false; |
chudy@google.com | a9e937c | 2012-08-03 17:32:05 +0000 | [diff] [blame] | 48 | emit drawComplete(); |
chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 49 | } |
kkinnunen | cfdc0e3 | 2015-01-13 22:49:02 -0800 | [diff] [blame] | 50 | |
| 51 | SkImageInfo info; |
| 52 | size_t rowBytes; |
| 53 | if (const void* pixels = fSurface->peekPixels(&info, &rowBytes)) { |
| 54 | QImage image(reinterpret_cast<const uchar*>(pixels), |
| 55 | info.width(), |
| 56 | info.height(), |
| 57 | rowBytes, |
| 58 | 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 | |
| 67 | void SkRasterWidget::updateImage() { |
| 68 | if (!fSurface) { |
| 69 | return; |
| 70 | } |
| 71 | fNeedImageUpdate = true; |
| 72 | this->update(); |
chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 73 | } |