blob: 03572cfdd1ad3f833153ace92b526b19e5b748d8 [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;
45 size_t rowPixels;
46 if (const void* pixels = fSurface->peekPixels(&info, &rowPixels)) {
47 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(),
64 QImage::Format_ARGB32_Premultiplied);
65 painter.drawImage(resultRect, image);
66 }
67}
68
kkinnunen0cfeaf32015-01-07 07:33:46 -080069void SkDrawCommandGeometryWidget::setDrawCommandIndex(int commandIndex) {
70 fCommandIndex = commandIndex;
71 this->updateImage();
72}
73
kkinnunen63a47022014-12-30 23:03:56 -080074void SkDrawCommandGeometryWidget::updateImage() {
75 if (!fSurface) {
76 return;
77 }
78
79 bool didRender = false;
80 const SkTDArray<SkDrawCommand*>& commands = fDebugger->getDrawCommands();
kkinnunen0cfeaf32015-01-07 07:33:46 -080081 if (0 != commands.count() && fCommandIndex >= 0) {
82 SkASSERT(commands.count() > fCommandIndex);
83 SkDrawCommand* command = commands[fCommandIndex];
kkinnunen63a47022014-12-30 23:03:56 -080084 didRender = command->render(fSurface->getCanvas());
85 }
86
87 if (!didRender) {
88 fSurface->getCanvas()->clear(SK_ColorTRANSPARENT);
89 }
90
91 fSurface->getCanvas()->flush();
kkinnunen0cfeaf32015-01-07 07:33:46 -080092 this->update();
kkinnunen63a47022014-12-30 23:03:56 -080093}