blob: 2134077acfa6cabc49dd3ca5fea6172ab1c99dc9 [file] [log] [blame]
kkinnunen63a47022014-12-30 23:03:56 -08001
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
14SkDrawCommandGeometryWidget::SkDrawCommandGeometryWidget(SkDebugger *debugger)
15 : QFrame()
kkinnunen0cfeaf32015-01-07 07:33:46 -080016 , fDebugger(debugger)
17 , fCommandIndex(-1) {
kkinnunen63a47022014-12-30 23:03:56 -080018 this->setStyleSheet("QFrame {background-color: black; border: 1px solid #cccccc;}");
19}
20
21void 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) {
robertphillipsd98120e2016-03-24 08:29:40 -070026 fSurface = nullptr;
kkinnunen63a47022014-12-30 23:03:56 -080027 } else {
28 SkImageInfo info = SkImageInfo::MakeN32Premul(dim, dim);
robertphillipsd98120e2016-03-24 08:29:40 -070029 fSurface = SkSurface::MakeRaster(info);
kkinnunen63a47022014-12-30 23:03:56 -080030 this->updateImage();
31 }
32}
33
34void 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
robertphillipsf1d746c2016-03-10 06:56:21 -080044 SkPixmap pixmap;
45
46 if (fSurface->peekPixels(&pixmap)) {
47 SkASSERT(pixmap.width() > 0);
48 SkASSERT(pixmap.height() > 0);
kkinnunen63a47022014-12-30 23:03:56 -080049
50 QRectF resultRect;
51 if (this->width() < this->height()) {
robertphillipsf1d746c2016-03-10 06:56:21 -080052 float ratio = this->width() / pixmap.width();
53 resultRect = QRectF(0, 0, this->width(), ratio * pixmap.height());
kkinnunen63a47022014-12-30 23:03:56 -080054 } else {
robertphillipsf1d746c2016-03-10 06:56:21 -080055 float ratio = this->height() / pixmap.height();
56 resultRect = QRectF(0, 0, ratio * pixmap.width(), this->height());
kkinnunen63a47022014-12-30 23:03:56 -080057 }
58
59 resultRect.moveCenter(this->contentsRect().center());
60
robertphillipsf1d746c2016-03-10 06:56:21 -080061 QImage image(reinterpret_cast<const uchar*>(pixmap.addr()),
62 pixmap.width(),
63 pixmap.height(),
64 pixmap.rowBytes(),
kkinnunen63a47022014-12-30 23:03:56 -080065 QImage::Format_ARGB32_Premultiplied);
66 painter.drawImage(resultRect, image);
67 }
68}
69
kkinnunen0cfeaf32015-01-07 07:33:46 -080070void SkDrawCommandGeometryWidget::setDrawCommandIndex(int commandIndex) {
71 fCommandIndex = commandIndex;
72 this->updateImage();
73}
74
kkinnunen63a47022014-12-30 23:03:56 -080075void SkDrawCommandGeometryWidget::updateImage() {
76 if (!fSurface) {
77 return;
78 }
79
80 bool didRender = false;
81 const SkTDArray<SkDrawCommand*>& commands = fDebugger->getDrawCommands();
kkinnunen0cfeaf32015-01-07 07:33:46 -080082 if (0 != commands.count() && fCommandIndex >= 0) {
83 SkASSERT(commands.count() > fCommandIndex);
84 SkDrawCommand* command = commands[fCommandIndex];
kkinnunen63a47022014-12-30 23:03:56 -080085 didRender = command->render(fSurface->getCanvas());
86 }
87
88 if (!didRender) {
89 fSurface->getCanvas()->clear(SK_ColorTRANSPARENT);
90 }
91
92 fSurface->getCanvas()->flush();
kkinnunen0cfeaf32015-01-07 07:33:46 -080093 this->update();
kkinnunen63a47022014-12-30 23:03:56 -080094}