blob: 9f19785667388a24b8a419e825c882d3c6cdb8e3 [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) {
26 fSurface.reset(NULL);
27 } else {
28 SkImageInfo info = SkImageInfo::MakeN32Premul(dim, dim);
29 fSurface.reset(SkSurface::NewRaster(info));
30 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
44 SkImageInfo info;
kkinnunencfdc0e32015-01-13 22:49:02 -080045 size_t rowBytes;
46 if (const void* pixels = fSurface->peekPixels(&info, &rowBytes)) {
kkinnunen63a47022014-12-30 23:03:56 -080047 SkASSERT(info.width() > 0);
48 SkASSERT(info.height() > 0);
49
50 QRectF resultRect;
51 if (this->width() < this->height()) {
52 float ratio = this->width() / info.width();
53 resultRect = QRectF(0, 0, this->width(), ratio * info.height());
54 } else {
55 float ratio = this->height() / info.height();
56 resultRect = QRectF(0, 0, ratio * info.width(), this->height());
57 }
58
59 resultRect.moveCenter(this->contentsRect().center());
60
61 QImage image(reinterpret_cast<const uchar*>(pixels),
62 info.width(),
63 info.height(),
kkinnunencfdc0e32015-01-13 22:49:02 -080064 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}