senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkPictureImageFilter.h" |
| 9 | #include "SkDevice.h" |
| 10 | #include "SkCanvas.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 11 | #include "SkReadBuffer.h" |
| 12 | #include "SkWriteBuffer.h" |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 13 | #include "SkValidationUtils.h" |
| 14 | |
| 15 | SkPictureImageFilter::SkPictureImageFilter(SkPicture* picture) |
| 16 | : INHERITED(0, 0), |
| 17 | fPicture(picture), |
| 18 | fRect(SkRect::MakeWH(picture ? SkIntToScalar(picture->width()) : 0, |
| 19 | picture ? SkIntToScalar(picture->height()) : 0)) { |
| 20 | SkSafeRef(fPicture); |
| 21 | } |
| 22 | |
| 23 | SkPictureImageFilter::SkPictureImageFilter(SkPicture* picture, const SkRect& rect) |
| 24 | : INHERITED(0, 0), |
| 25 | fPicture(picture), |
| 26 | fRect(rect) { |
| 27 | SkSafeRef(fPicture); |
| 28 | } |
| 29 | |
| 30 | SkPictureImageFilter::~SkPictureImageFilter() { |
| 31 | SkSafeUnref(fPicture); |
| 32 | } |
| 33 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 34 | SkPictureImageFilter::SkPictureImageFilter(SkReadBuffer& buffer) |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 35 | : INHERITED(0, buffer), |
| 36 | fPicture(NULL) { |
fmalita@google.com | 7713c38 | 2014-02-07 05:55:39 +0000 | [diff] [blame^] | 37 | // FIXME: unflatten picture here. |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 38 | buffer.readRect(&fRect); |
| 39 | } |
| 40 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 41 | void SkPictureImageFilter::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 42 | this->INHERITED::flatten(buffer); |
fmalita@google.com | 7713c38 | 2014-02-07 05:55:39 +0000 | [diff] [blame^] | 43 | // FIXME: flatten picture here. |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 44 | buffer.writeRect(fRect); |
| 45 | } |
| 46 | |
| 47 | bool SkPictureImageFilter::onFilterImage(Proxy* proxy, const SkBitmap&, const SkMatrix& matrix, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 48 | SkBitmap* result, SkIPoint* offset) const { |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 49 | if (!fPicture) { |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 50 | offset->fX = offset->fY = 0; |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 51 | return true; |
| 52 | } |
| 53 | |
| 54 | SkRect floatBounds; |
| 55 | SkIRect bounds; |
| 56 | matrix.mapRect(&floatBounds, fRect); |
| 57 | floatBounds.roundOut(&bounds); |
| 58 | |
| 59 | if (bounds.isEmpty()) { |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 60 | offset->fX = offset->fY = 0; |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 61 | return true; |
| 62 | } |
| 63 | |
| 64 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height())); |
| 65 | if (NULL == device.get()) { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | SkCanvas canvas(device.get()); |
| 70 | SkPaint paint; |
| 71 | |
| 72 | canvas.translate(-SkIntToScalar(bounds.fLeft), -SkIntToScalar(bounds.fTop)); |
| 73 | canvas.concat(matrix); |
| 74 | canvas.drawPicture(*fPicture); |
| 75 | |
| 76 | *result = device.get()->accessBitmap(false); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 77 | offset->fX = bounds.fLeft; |
| 78 | offset->fY = bounds.fTop; |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 79 | return true; |
| 80 | } |