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), |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame^] | 18 | fCropRect(SkRect::MakeWH(picture ? SkIntToScalar(picture->width()) : 0, |
| 19 | picture ? SkIntToScalar(picture->height()) : 0)) { |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 20 | SkSafeRef(fPicture); |
| 21 | } |
| 22 | |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame^] | 23 | SkPictureImageFilter::SkPictureImageFilter(SkPicture* picture, const SkRect& cropRect) |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 24 | : INHERITED(0, 0), |
| 25 | fPicture(picture), |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame^] | 26 | fCropRect(cropRect) { |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 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) { |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 37 | #ifdef SK_ALLOW_PICTUREIMAGEFILTER_SERIALIZATION |
| 38 | if (buffer.readBool()) { |
| 39 | fPicture = SkPicture::CreateFromBuffer(buffer); |
| 40 | } |
| 41 | #else |
| 42 | buffer.readBool(); |
| 43 | #endif |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame^] | 44 | buffer.readRect(&fCropRect); |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 45 | } |
| 46 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 47 | void SkPictureImageFilter::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 48 | this->INHERITED::flatten(buffer); |
commit-bot@chromium.org | 5e0995e | 2014-02-07 12:20:04 +0000 | [diff] [blame] | 49 | #ifdef SK_ALLOW_PICTUREIMAGEFILTER_SERIALIZATION |
| 50 | bool hasPicture = (fPicture != NULL); |
| 51 | buffer.writeBool(hasPicture); |
| 52 | if (hasPicture) { |
| 53 | fPicture->flatten(buffer); |
| 54 | } |
| 55 | #else |
| 56 | buffer.writeBool(false); |
| 57 | #endif |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame^] | 58 | buffer.writeRect(fCropRect); |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | bool SkPictureImageFilter::onFilterImage(Proxy* proxy, const SkBitmap&, const SkMatrix& matrix, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 62 | SkBitmap* result, SkIPoint* offset) const { |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 63 | if (!fPicture) { |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 64 | offset->fX = offset->fY = 0; |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 65 | return true; |
| 66 | } |
| 67 | |
| 68 | SkRect floatBounds; |
| 69 | SkIRect bounds; |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame^] | 70 | matrix.mapRect(&floatBounds, fCropRect); |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 71 | floatBounds.roundOut(&bounds); |
| 72 | |
| 73 | if (bounds.isEmpty()) { |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 74 | offset->fX = offset->fY = 0; |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 75 | return true; |
| 76 | } |
| 77 | |
| 78 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height())); |
| 79 | if (NULL == device.get()) { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | SkCanvas canvas(device.get()); |
| 84 | SkPaint paint; |
| 85 | |
| 86 | canvas.translate(-SkIntToScalar(bounds.fLeft), -SkIntToScalar(bounds.fTop)); |
| 87 | canvas.concat(matrix); |
| 88 | canvas.drawPicture(*fPicture); |
| 89 | |
| 90 | *result = device.get()->accessBitmap(false); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 91 | offset->fX = bounds.fLeft; |
| 92 | offset->fY = bounds.fTop; |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 93 | return true; |
| 94 | } |