OLD | NEW |
---|---|
(Empty) | |
1 | |
2 /* | |
robertphillips
2014/12/30 17:35:33
2014
Kimmo Kinnunen
2014/12/31 06:43:29
Done.
| |
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 | |
9 #include <QtGui> | |
10 | |
11 #include "SkDebugger.h" | |
12 #include "SkDrawGeometryWidget.h" | |
13 | |
14 SkDrawGeometryWidget::SkDrawGeometryWidget(SkDebugger *debugger) | |
15 : QFrame() | |
16 , fDebugger(debugger) { | |
17 this->setStyleSheet("QFrame {background-color: black; border: 1px solid #ccc ccc;}"); | |
18 } | |
19 | |
20 void SkDrawGeometryWidget::resizeEvent(QResizeEvent* event) { | |
21 this->QFrame::resizeEvent(event); | |
22 QRect r = this->contentsRect(); | |
23 int dim = std::min(r.width(), r.height()); | |
24 if (dim == 0) { | |
25 fSurface.reset(NULL); | |
26 } else { | |
27 SkImageInfo info = SkImageInfo::MakeN32Premul(dim, dim); | |
28 fSurface.reset(SkSurface::NewRaster(info)); | |
robertphillips
2014/12/30 17:35:33
this->
Kimmo Kinnunen
2014/12/31 06:43:29
Done.
| |
29 updateImage(); | |
30 } | |
31 } | |
32 | |
33 void SkDrawGeometryWidget::paintEvent(QPaintEvent* event) { | |
34 this->QFrame::paintEvent(event); | |
35 | |
36 if (!fSurface) { | |
37 return; | |
38 } | |
39 | |
40 QPainter painter(this); | |
41 painter.setRenderHint(QPainter::Antialiasing); | |
42 | |
43 SkImageInfo info; | |
44 size_t rowPixels; | |
45 if (const void* pixels = fSurface->peekPixels(&info, &rowPixels)) { | |
46 SkASSERT(info.width() > 0); | |
47 SkASSERT(info.height() > 0); | |
48 | |
49 QRectF resultRect; | |
50 if (this->width() < this->height()) { | |
51 float ratio = this->width() / info.width(); | |
52 resultRect = QRectF(0, 0, this->width(), ratio * info.height()); | |
53 } else { | |
54 float ratio = this->height() / info.height(); | |
55 resultRect = QRectF(0, 0, ratio * info.width(), this->height()); | |
56 } | |
57 | |
58 resultRect.moveCenter(this->contentsRect().center()); | |
59 | |
60 QImage image(reinterpret_cast<const uchar*>(pixels), | |
61 info.width(), | |
62 info.height(), | |
63 QImage::Format_ARGB32_Premultiplied); | |
64 painter.drawImage(resultRect, image); | |
65 } | |
66 } | |
67 | |
68 void SkDrawGeometryWidget::updateImage() { | |
69 if (!fSurface) { | |
70 return; | |
71 } | |
72 | |
73 bool didRender = false; | |
74 const SkTDArray<SkDrawCommand*>& commands = fDebugger->getDrawCommands(); | |
75 if (0 != commands.count()) { | |
76 SkDrawCommand* command = commands[fDebugger->index()]; | |
77 didRender = command->render(fSurface->getCanvas()); | |
78 } | |
79 | |
80 if (!didRender) { | |
81 fSurface->getCanvas()->clear(SK_ColorTRANSPARENT); | |
82 } | |
83 | |
84 fSurface->getCanvas()->flush(); | |
85 update(); | |
86 } | |
OLD | NEW |