Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2012 The Android Open Source Project |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #include "SkBitmapSource.h" |
michael@0 | 9 | #include "SkDevice.h" |
michael@0 | 10 | #include "SkCanvas.h" |
michael@0 | 11 | #include "SkReadBuffer.h" |
michael@0 | 12 | #include "SkWriteBuffer.h" |
michael@0 | 13 | #include "SkValidationUtils.h" |
michael@0 | 14 | |
michael@0 | 15 | SkBitmapSource::SkBitmapSource(const SkBitmap& bitmap) |
michael@0 | 16 | : INHERITED(0, 0), |
michael@0 | 17 | fBitmap(bitmap), |
michael@0 | 18 | fSrcRect(SkRect::MakeWH(SkIntToScalar(bitmap.width()), |
michael@0 | 19 | SkIntToScalar(bitmap.height()))), |
michael@0 | 20 | fDstRect(fSrcRect) { |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | SkBitmapSource::SkBitmapSource(const SkBitmap& bitmap, const SkRect& srcRect, const SkRect& dstRect) |
michael@0 | 24 | : INHERITED(0, 0), |
michael@0 | 25 | fBitmap(bitmap), |
michael@0 | 26 | fSrcRect(srcRect), |
michael@0 | 27 | fDstRect(dstRect) { |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | SkBitmapSource::SkBitmapSource(SkReadBuffer& buffer) |
michael@0 | 31 | : INHERITED(0, buffer) { |
michael@0 | 32 | fBitmap.unflatten(buffer); |
michael@0 | 33 | buffer.readRect(&fSrcRect); |
michael@0 | 34 | buffer.readRect(&fDstRect); |
michael@0 | 35 | buffer.validate(buffer.isValid() && SkIsValidRect(fSrcRect) && SkIsValidRect(fDstRect)); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | void SkBitmapSource::flatten(SkWriteBuffer& buffer) const { |
michael@0 | 39 | this->INHERITED::flatten(buffer); |
michael@0 | 40 | fBitmap.flatten(buffer); |
michael@0 | 41 | buffer.writeRect(fSrcRect); |
michael@0 | 42 | buffer.writeRect(fDstRect); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | bool SkBitmapSource::onFilterImage(Proxy* proxy, const SkBitmap&, const Context& ctx, |
michael@0 | 46 | SkBitmap* result, SkIPoint* offset) const { |
michael@0 | 47 | SkRect bounds, dstRect; |
michael@0 | 48 | fBitmap.getBounds(&bounds); |
michael@0 | 49 | ctx.ctm().mapRect(&dstRect, fDstRect); |
michael@0 | 50 | if (fSrcRect == bounds && dstRect == bounds) { |
michael@0 | 51 | // No regions cropped out or resized; return entire bitmap. |
michael@0 | 52 | *result = fBitmap; |
michael@0 | 53 | offset->fX = offset->fY = 0; |
michael@0 | 54 | return true; |
michael@0 | 55 | } |
michael@0 | 56 | SkIRect dstIRect; |
michael@0 | 57 | dstRect.roundOut(&dstIRect); |
michael@0 | 58 | |
michael@0 | 59 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(dstIRect.width(), dstIRect.height())); |
michael@0 | 60 | if (NULL == device.get()) { |
michael@0 | 61 | return false; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | SkCanvas canvas(device.get()); |
michael@0 | 65 | SkPaint paint; |
michael@0 | 66 | |
michael@0 | 67 | // Subtract off the integer component of the translation (will be applied in loc, below). |
michael@0 | 68 | dstRect.offset(-SkIntToScalar(dstIRect.fLeft), -SkIntToScalar(dstIRect.fTop)); |
michael@0 | 69 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
michael@0 | 70 | // FIXME: this probably shouldn't be necessary, but drawBitmapRectToRect asserts |
michael@0 | 71 | // None filtering when it's translate-only |
michael@0 | 72 | paint.setFilterLevel( |
michael@0 | 73 | fSrcRect.width() == dstRect.width() && fSrcRect.height() == dstRect.height() ? |
michael@0 | 74 | SkPaint::kNone_FilterLevel : SkPaint::kHigh_FilterLevel); |
michael@0 | 75 | canvas.drawBitmapRectToRect(fBitmap, &fSrcRect, dstRect, &paint); |
michael@0 | 76 | |
michael@0 | 77 | *result = device.get()->accessBitmap(false); |
michael@0 | 78 | offset->fX = dstIRect.fLeft; |
michael@0 | 79 | offset->fY = dstIRect.fTop; |
michael@0 | 80 | return true; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | void SkBitmapSource::computeFastBounds(const SkRect&, SkRect* dst) const { |
michael@0 | 84 | *dst = fDstRect; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | bool SkBitmapSource::onFilterBounds(const SkIRect& src, const SkMatrix& ctm, |
michael@0 | 88 | SkIRect* dst) const { |
michael@0 | 89 | *dst = src; |
michael@0 | 90 | return true; |
michael@0 | 91 | } |