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" |
| 11 | #include "SkFlattenableBuffers.h" |
| 12 | #include "SkValidationUtils.h" |
| 13 | |
| 14 | SkPictureImageFilter::SkPictureImageFilter(SkPicture* picture) |
| 15 | : INHERITED(0, 0), |
| 16 | fPicture(picture), |
| 17 | fRect(SkRect::MakeWH(picture ? SkIntToScalar(picture->width()) : 0, |
| 18 | picture ? SkIntToScalar(picture->height()) : 0)) { |
| 19 | SkSafeRef(fPicture); |
| 20 | } |
| 21 | |
| 22 | SkPictureImageFilter::SkPictureImageFilter(SkPicture* picture, const SkRect& rect) |
| 23 | : INHERITED(0, 0), |
| 24 | fPicture(picture), |
| 25 | fRect(rect) { |
| 26 | SkSafeRef(fPicture); |
| 27 | } |
| 28 | |
| 29 | SkPictureImageFilter::~SkPictureImageFilter() { |
| 30 | SkSafeUnref(fPicture); |
| 31 | } |
| 32 | |
| 33 | SkPictureImageFilter::SkPictureImageFilter(SkFlattenableReadBuffer& buffer) |
| 34 | : INHERITED(0, buffer), |
| 35 | fPicture(NULL) { |
| 36 | // FIXME: unflatten picture here. |
| 37 | buffer.readRect(&fRect); |
| 38 | } |
| 39 | |
| 40 | void SkPictureImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { |
| 41 | this->INHERITED::flatten(buffer); |
| 42 | // FIXME: flatten picture here. |
| 43 | buffer.writeRect(fRect); |
| 44 | } |
| 45 | |
| 46 | bool SkPictureImageFilter::onFilterImage(Proxy* proxy, const SkBitmap&, const SkMatrix& matrix, |
| 47 | SkBitmap* result, SkIPoint* offset) { |
| 48 | if (!fPicture) { |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame^] | 49 | offset->fX = offset->fY = 0; |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 50 | return true; |
| 51 | } |
| 52 | |
| 53 | SkRect floatBounds; |
| 54 | SkIRect bounds; |
| 55 | matrix.mapRect(&floatBounds, fRect); |
| 56 | floatBounds.roundOut(&bounds); |
| 57 | |
| 58 | if (bounds.isEmpty()) { |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame^] | 59 | offset->fX = offset->fY = 0; |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 60 | return true; |
| 61 | } |
| 62 | |
| 63 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height())); |
| 64 | if (NULL == device.get()) { |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | SkCanvas canvas(device.get()); |
| 69 | SkPaint paint; |
| 70 | |
| 71 | canvas.translate(-SkIntToScalar(bounds.fLeft), -SkIntToScalar(bounds.fTop)); |
| 72 | canvas.concat(matrix); |
| 73 | canvas.drawPicture(*fPicture); |
| 74 | |
| 75 | *result = device.get()->accessBitmap(false); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame^] | 76 | offset->fX = bounds.fLeft; |
| 77 | offset->fY = bounds.fTop; |
senorblanco@chromium.org | 5333300 | 2013-12-12 23:28:52 +0000 | [diff] [blame] | 78 | return true; |
| 79 | } |