kkinnunen | 63a4702 | 2014-12-30 23:03:56 -0800 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2014 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 | |
| 9 | #include <QtGui> |
| 10 | |
| 11 | #include "SkDebugger.h" |
| 12 | #include "SkDrawCommandGeometryWidget.h" |
| 13 | |
| 14 | SkDrawCommandGeometryWidget::SkDrawCommandGeometryWidget(SkDebugger *debugger) |
| 15 | : QFrame() |
kkinnunen | 0cfeaf3 | 2015-01-07 07:33:46 -0800 | [diff] [blame] | 16 | , fDebugger(debugger) |
| 17 | , fCommandIndex(-1) { |
kkinnunen | 63a4702 | 2014-12-30 23:03:56 -0800 | [diff] [blame] | 18 | this->setStyleSheet("QFrame {background-color: black; border: 1px solid #cccccc;}"); |
| 19 | } |
| 20 | |
| 21 | void SkDrawCommandGeometryWidget::resizeEvent(QResizeEvent* event) { |
| 22 | this->QFrame::resizeEvent(event); |
| 23 | QRect r = this->contentsRect(); |
| 24 | int dim = std::min(r.width(), r.height()); |
| 25 | if (dim == 0) { |
robertphillips | d98120e | 2016-03-24 08:29:40 -0700 | [diff] [blame] | 26 | fSurface = nullptr; |
kkinnunen | 63a4702 | 2014-12-30 23:03:56 -0800 | [diff] [blame] | 27 | } else { |
| 28 | SkImageInfo info = SkImageInfo::MakeN32Premul(dim, dim); |
robertphillips | d98120e | 2016-03-24 08:29:40 -0700 | [diff] [blame] | 29 | fSurface = SkSurface::MakeRaster(info); |
kkinnunen | 63a4702 | 2014-12-30 23:03:56 -0800 | [diff] [blame] | 30 | this->updateImage(); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | void SkDrawCommandGeometryWidget::paintEvent(QPaintEvent* event) { |
| 35 | this->QFrame::paintEvent(event); |
| 36 | |
| 37 | if (!fSurface) { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | QPainter painter(this); |
| 42 | painter.setRenderHint(QPainter::Antialiasing); |
| 43 | |
robertphillips | f1d746c | 2016-03-10 06:56:21 -0800 | [diff] [blame] | 44 | SkPixmap pixmap; |
| 45 | |
| 46 | if (fSurface->peekPixels(&pixmap)) { |
| 47 | SkASSERT(pixmap.width() > 0); |
| 48 | SkASSERT(pixmap.height() > 0); |
kkinnunen | 63a4702 | 2014-12-30 23:03:56 -0800 | [diff] [blame] | 49 | |
| 50 | QRectF resultRect; |
| 51 | if (this->width() < this->height()) { |
robertphillips | f1d746c | 2016-03-10 06:56:21 -0800 | [diff] [blame] | 52 | float ratio = this->width() / pixmap.width(); |
| 53 | resultRect = QRectF(0, 0, this->width(), ratio * pixmap.height()); |
kkinnunen | 63a4702 | 2014-12-30 23:03:56 -0800 | [diff] [blame] | 54 | } else { |
robertphillips | f1d746c | 2016-03-10 06:56:21 -0800 | [diff] [blame] | 55 | float ratio = this->height() / pixmap.height(); |
| 56 | resultRect = QRectF(0, 0, ratio * pixmap.width(), this->height()); |
kkinnunen | 63a4702 | 2014-12-30 23:03:56 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | resultRect.moveCenter(this->contentsRect().center()); |
| 60 | |
robertphillips | f1d746c | 2016-03-10 06:56:21 -0800 | [diff] [blame] | 61 | QImage image(reinterpret_cast<const uchar*>(pixmap.addr()), |
| 62 | pixmap.width(), |
| 63 | pixmap.height(), |
| 64 | pixmap.rowBytes(), |
kkinnunen | 63a4702 | 2014-12-30 23:03:56 -0800 | [diff] [blame] | 65 | QImage::Format_ARGB32_Premultiplied); |
| 66 | painter.drawImage(resultRect, image); |
| 67 | } |
| 68 | } |
| 69 | |
kkinnunen | 0cfeaf3 | 2015-01-07 07:33:46 -0800 | [diff] [blame] | 70 | void SkDrawCommandGeometryWidget::setDrawCommandIndex(int commandIndex) { |
| 71 | fCommandIndex = commandIndex; |
| 72 | this->updateImage(); |
| 73 | } |
| 74 | |
kkinnunen | 63a4702 | 2014-12-30 23:03:56 -0800 | [diff] [blame] | 75 | void SkDrawCommandGeometryWidget::updateImage() { |
| 76 | if (!fSurface) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | bool didRender = false; |
| 81 | const SkTDArray<SkDrawCommand*>& commands = fDebugger->getDrawCommands(); |
kkinnunen | 0cfeaf3 | 2015-01-07 07:33:46 -0800 | [diff] [blame] | 82 | if (0 != commands.count() && fCommandIndex >= 0) { |
| 83 | SkASSERT(commands.count() > fCommandIndex); |
| 84 | SkDrawCommand* command = commands[fCommandIndex]; |
kkinnunen | 63a4702 | 2014-12-30 23:03:56 -0800 | [diff] [blame] | 85 | didRender = command->render(fSurface->getCanvas()); |
| 86 | } |
| 87 | |
| 88 | if (!didRender) { |
| 89 | fSurface->getCanvas()->clear(SK_ColorTRANSPARENT); |
| 90 | } |
| 91 | |
| 92 | fSurface->getCanvas()->flush(); |
kkinnunen | 0cfeaf3 | 2015-01-07 07:33:46 -0800 | [diff] [blame] | 93 | this->update(); |
kkinnunen | 63a4702 | 2014-12-30 23:03:56 -0800 | [diff] [blame] | 94 | } |