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) { 49 return true; 50 } 51 52 SkRect floatBounds; 53 SkIRect bounds; 54 matrix.mapRect(&floatBounds, fRect); 55 floatBounds.roundOut(&bounds); 56 57 if (bounds.isEmpty()) { 58 return true; 59 } 60 61 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height())); 62 if (NULL == device.get()) { 63 return false; 64 } 65 66 SkCanvas canvas(device.get()); 67 SkPaint paint; 68 69 canvas.translate(-SkIntToScalar(bounds.fLeft), -SkIntToScalar(bounds.fTop)); 70 canvas.concat(matrix); 71 canvas.drawPicture(*fPicture); 72 73 *result = device.get()->accessBitmap(false); 74 offset->fX += bounds.fLeft; 75 offset->fY += bounds.fTop; 76 return true; 77 } 78