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)
27 , fDstRect(dstRect) {}
29 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
30 SkBitmapSource::SkBitmapSource(SkReadBuffer& buffer) : INHERITED(0, buffer) {
31 if (buffer.isVersionLT(SkReadBuffer::kNoMoreBitmapFlatten_Version)) {
32 fBitmap.legacyUnflatten(buffer);
34 buffer.readBitmap(&fBitmap);
36 buffer.readRect(&fSrcRect);
37 buffer.readRect(&fDstRect);
38 buffer.validate(buffer.isValid() && SkIsValidRect(fSrcRect) && SkIsValidRect(fDstRect));
42 SkFlattenable* SkBitmapSource::CreateProc(SkReadBuffer& buffer) {
44 buffer.readRect(&src);
45 buffer.readRect(&dst);
47 if (!buffer.readBitmap(&bitmap)) {
50 return SkBitmapSource::Create(bitmap, src, dst);
53 void SkBitmapSource::flatten(SkWriteBuffer& buffer) const {
54 buffer.writeRect(fSrcRect);
55 buffer.writeRect(fDstRect);
56 buffer.writeBitmap(fBitmap);
59 bool SkBitmapSource::onFilterImage(Proxy* proxy, const SkBitmap&, const Context& ctx,
60 SkBitmap* result, SkIPoint* offset) const {
61 SkRect bounds, dstRect;
62 fBitmap.getBounds(&bounds);
63 ctx.ctm().mapRect(&dstRect, fDstRect);
64 if (fSrcRect == bounds && dstRect == bounds) {
65 // No regions cropped out or resized; return entire bitmap.
67 offset->fX = offset->fY = 0;
71 dstRect.roundOut(&dstIRect);
73 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(dstIRect.width(), dstIRect.height()));
74 if (NULL == device.get()) {
78 SkCanvas canvas(device.get());
81 // Subtract off the integer component of the translation (will be applied in loc, below).
82 dstRect.offset(-SkIntToScalar(dstIRect.fLeft), -SkIntToScalar(dstIRect.fTop));
83 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
84 // FIXME: this probably shouldn't be necessary, but drawBitmapRectToRect asserts
85 // None filtering when it's translate-only
87 fSrcRect.width() == dstRect.width() && fSrcRect.height() == dstRect.height() ?
88 SkPaint::kNone_FilterLevel : SkPaint::kHigh_FilterLevel);
89 canvas.drawBitmapRectToRect(fBitmap, &fSrcRect, dstRect, &paint);
91 *result = device.get()->accessBitmap(false);
92 offset->fX = dstIRect.fLeft;
93 offset->fY = dstIRect.fTop;
97 void SkBitmapSource::computeFastBounds(const SkRect&, SkRect* dst) const {