2 * Copyright 2012 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkBitmapSource.h"
11 #include "SkReadBuffer.h"
12 #include "SkWriteBuffer.h"
13 #include "SkValidationUtils.h"
15 SkBitmapSource::SkBitmapSource(const SkBitmap& bitmap)
18 fSrcRect(SkRect::MakeWH(SkIntToScalar(bitmap.width()),
19 SkIntToScalar(bitmap.height()))),
23 SkBitmapSource::SkBitmapSource(const SkBitmap& bitmap, const SkRect& srcRect, const SkRect& dstRect)
30 SkBitmapSource::SkBitmapSource(SkReadBuffer& buffer)
31 : INHERITED(0, buffer) {
32 fBitmap.unflatten(buffer);
33 buffer.readRect(&fSrcRect);
34 buffer.readRect(&fDstRect);
35 buffer.validate(buffer.isValid() && SkIsValidRect(fSrcRect) && SkIsValidRect(fDstRect));
38 void SkBitmapSource::flatten(SkWriteBuffer& buffer) const {
39 this->INHERITED::flatten(buffer);
40 fBitmap.flatten(buffer);
41 buffer.writeRect(fSrcRect);
42 buffer.writeRect(fDstRect);
45 bool SkBitmapSource::onFilterImage(Proxy* proxy, const SkBitmap&, const SkMatrix& matrix,
46 SkBitmap* result, SkIPoint* offset) const {
47 SkRect bounds, dstRect;
48 fBitmap.getBounds(&bounds);
49 matrix.mapRect(&dstRect, fDstRect);
50 if (fSrcRect == bounds && dstRect == bounds) {
51 // No regions cropped out or resized; return entire bitmap.
53 offset->fX = offset->fY = 0;
57 dstRect.roundOut(&dstIRect);
59 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(dstIRect.width(), dstIRect.height()));
60 if (NULL == device.get()) {
64 SkCanvas canvas(device.get());
67 // Subtract off the integer component of the translation (will be applied in loc, below).
68 dstRect.offset(-SkIntToScalar(dstIRect.fLeft), -SkIntToScalar(dstIRect.fTop));
69 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
70 // FIXME: this probably shouldn't be necessary, but drawBitmapRectToRect asserts
71 // None filtering when it's translate-only
73 fSrcRect.width() == dstRect.width() && fSrcRect.height() == dstRect.height() ?
74 SkPaint::kNone_FilterLevel : SkPaint::kMedium_FilterLevel);
75 canvas.drawBitmapRectToRect(fBitmap, &fSrcRect, dstRect, &paint);
77 *result = device.get()->accessBitmap(false);
78 offset->fX = dstIRect.fLeft;
79 offset->fY = dstIRect.fTop;
83 void SkBitmapSource::computeFastBounds(const SkRect&, SkRect* dst) const {
87 bool SkBitmapSource::onFilterBounds(const SkIRect& src, const SkMatrix& ctm,