chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 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 | |
chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 9 | #include "SkRasterWidget.h" |
| 10 | |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 11 | SkRasterWidget::SkRasterWidget(SkDebugger *debugger) : QWidget() { |
chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 12 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 800, 800); |
| 13 | fBitmap.allocPixels(); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 14 | fBitmap.eraseColor(SK_ColorTRANSPARENT); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 15 | fDevice = new SkBitmapDevice(fBitmap); |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 16 | fDebugger = debugger; |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 17 | fCanvas = new SkCanvas(fDevice); |
chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 18 | this->setStyleSheet("QWidget {background-color: white; border: 1px solid #cccccc;}"); |
| 19 | } |
| 20 | |
| 21 | SkRasterWidget::~SkRasterWidget() { |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 22 | SkSafeUnref(fCanvas); |
| 23 | SkSafeUnref(fDevice); |
chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | void SkRasterWidget::resizeEvent(QResizeEvent* event) { |
| 27 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, event->size().width(), event->size().height()); |
| 28 | fBitmap.allocPixels(); |
robertphillips@google.com | f4741c1 | 2013-02-06 20:13:54 +0000 | [diff] [blame] | 29 | fBitmap.eraseColor(SK_ColorTRANSPARENT); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 30 | SkSafeUnref(fCanvas); |
| 31 | SkSafeUnref(fDevice); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 32 | fDevice = new SkBitmapDevice(fBitmap); |
chudy@google.com | 830b879 | 2012-08-01 15:57:52 +0000 | [diff] [blame] | 33 | fCanvas = new SkCanvas(fDevice); |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 34 | fDebugger->resize(event->size().width(), event->size().height()); |
chudy@google.com | 2d537a1 | 2012-07-31 12:49:52 +0000 | [diff] [blame] | 35 | this->update(); |
chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void SkRasterWidget::paintEvent(QPaintEvent* event) { |
chudy@google.com | 607357f | 2012-08-07 16:12:23 +0000 | [diff] [blame] | 39 | if (!this->isHidden()) { |
| 40 | fDebugger->draw(fCanvas); |
chudy@google.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 41 | QPainter painter(this); |
| 42 | QStyleOption opt; |
| 43 | opt.init(this); |
| 44 | |
| 45 | style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this); |
| 46 | |
| 47 | QPoint origin(0,0); |
| 48 | QImage image((uchar *)fBitmap.getPixels(), fBitmap.width(), |
| 49 | fBitmap.height(), QImage::Format_ARGB32_Premultiplied); |
| 50 | |
humper@google.com | 5a867c7 | 2013-07-10 15:53:36 +0000 | [diff] [blame] | 51 | #if SK_R32_SHIFT == 0 |
| 52 | painter.drawImage(origin, image.rgbSwapped()); |
| 53 | #else |
chudy@google.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 54 | painter.drawImage(origin, image); |
humper@google.com | 5a867c7 | 2013-07-10 15:53:36 +0000 | [diff] [blame] | 55 | #endif |
chudy@google.com | 80a4a60 | 2012-07-30 18:54:07 +0000 | [diff] [blame] | 56 | painter.end(); |
chudy@google.com | a9e937c | 2012-08-03 17:32:05 +0000 | [diff] [blame] | 57 | emit drawComplete(); |
chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 58 | } |
chudy@google.com | 38b08ce | 2012-07-28 23:26:10 +0000 | [diff] [blame] | 59 | } |