| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 The Android Open Source Project | 2 * Copyright 2012 The Android Open Source Project |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmapSource.h" | 8 #include "SkBitmapSource.h" |
| 9 #include "SkDevice.h" | |
| 10 #include "SkCanvas.h" | |
| 11 #include "SkFlattenableBuffers.h" | |
| 12 #include "SkValidationUtils.h" | |
| 13 | 9 |
| 14 SkBitmapSource::SkBitmapSource(const SkBitmap& bitmap) | 10 SkBitmapSource::SkBitmapSource(const SkBitmap& bitmap) |
| 15 : INHERITED(0, 0), | 11 : INHERITED(0, 0), |
| 16 fBitmap(bitmap), | 12 fBitmap(bitmap) { |
| 17 fSrcRect(SkRect::MakeWH(SkIntToScalar(bitmap.width()), | |
| 18 SkIntToScalar(bitmap.height()))), | |
| 19 fDstRect(fSrcRect) { | |
| 20 } | |
| 21 | |
| 22 SkBitmapSource::SkBitmapSource(const SkBitmap& bitmap, const SkRect& srcRect, co
nst SkRect& dstRect) | |
| 23 : INHERITED(0, 0), | |
| 24 fBitmap(bitmap), | |
| 25 fSrcRect(srcRect), | |
| 26 fDstRect(dstRect) { | |
| 27 } | 13 } |
| 28 | 14 |
| 29 SkBitmapSource::SkBitmapSource(SkFlattenableReadBuffer& buffer) | 15 SkBitmapSource::SkBitmapSource(SkFlattenableReadBuffer& buffer) |
| 30 : INHERITED(0, buffer) { | 16 : INHERITED(0, buffer) { |
| 31 fBitmap.unflatten(buffer); | 17 fBitmap.unflatten(buffer); |
| 32 buffer.readRect(&fSrcRect); | |
| 33 buffer.readRect(&fDstRect); | |
| 34 buffer.validate(SkIsValidRect(fSrcRect) && SkIsValidRect(fDstRect)); | |
| 35 } | 18 } |
| 36 | 19 |
| 37 void SkBitmapSource::flatten(SkFlattenableWriteBuffer& buffer) const { | 20 void SkBitmapSource::flatten(SkFlattenableWriteBuffer& buffer) const { |
| 38 this->INHERITED::flatten(buffer); | 21 this->INHERITED::flatten(buffer); |
| 39 fBitmap.flatten(buffer); | 22 fBitmap.flatten(buffer); |
| 40 buffer.writeRect(fSrcRect); | |
| 41 buffer.writeRect(fDstRect); | |
| 42 } | 23 } |
| 43 | 24 |
| 44 bool SkBitmapSource::onFilterImage(Proxy* proxy, const SkBitmap&, const SkMatrix
& matrix, | 25 bool SkBitmapSource::onFilterImage(Proxy*, const SkBitmap&, const SkMatrix&, |
| 45 SkBitmap* result, SkIPoint* offset) { | 26 SkBitmap* result, SkIPoint* offset) { |
| 46 SkRect bounds, dstRect; | 27 *result = fBitmap; |
| 47 fBitmap.getBounds(&bounds); | |
| 48 matrix.mapRect(&dstRect, fDstRect); | |
| 49 if (fSrcRect == bounds && dstRect == bounds) { | |
| 50 *result = fBitmap; | |
| 51 return true; | |
| 52 } | |
| 53 SkIRect dstIRect; | |
| 54 dstRect.roundOut(&dstIRect); | |
| 55 | |
| 56 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(dstIRect.width(), dstI
Rect.height())); | |
| 57 if (NULL == device.get()) { | |
| 58 return false; | |
| 59 } | |
| 60 | |
| 61 SkCanvas canvas(device.get()); | |
| 62 SkPaint paint; | |
| 63 | |
| 64 // Subtract off the integer component of the translation (will be applied in
loc, below). | |
| 65 dstRect.offset(-SkIntToScalar(dstIRect.fLeft), -SkIntToScalar(dstIRect.fTop)
); | |
| 66 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | |
| 67 // FIXME: this probably shouldn't be necessary, but drawBitmapRectToRect ass
erts | |
| 68 // None filtering when it's translate-only | |
| 69 paint.setFilterLevel(fSrcRect.width() == dstRect.width() && fSrcRect.height(
) == dstRect.height() ? SkPaint::kNone_FilterLevel : SkPaint::kMedium_FilterLeve
l); | |
| 70 canvas.drawBitmapRectToRect(fBitmap, &fSrcRect, dstRect, &paint); | |
| 71 | |
| 72 *result = device.get()->accessBitmap(false); | |
| 73 offset->fX += dstIRect.fLeft; | |
| 74 offset->fY += dstIRect.fTop; | |
| 75 return true; | 28 return true; |
| 76 } | 29 } |
| OLD | NEW |