gfx/skia/trunk/src/effects/SkBitmapSource.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/effects/SkBitmapSource.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,91 @@
     1.4 +/*
     1.5 + * Copyright 2012 The Android Open Source Project
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#include "SkBitmapSource.h"
    1.12 +#include "SkDevice.h"
    1.13 +#include "SkCanvas.h"
    1.14 +#include "SkReadBuffer.h"
    1.15 +#include "SkWriteBuffer.h"
    1.16 +#include "SkValidationUtils.h"
    1.17 +
    1.18 +SkBitmapSource::SkBitmapSource(const SkBitmap& bitmap)
    1.19 +  : INHERITED(0, 0),
    1.20 +    fBitmap(bitmap),
    1.21 +    fSrcRect(SkRect::MakeWH(SkIntToScalar(bitmap.width()),
    1.22 +                            SkIntToScalar(bitmap.height()))),
    1.23 +    fDstRect(fSrcRect) {
    1.24 +}
    1.25 +
    1.26 +SkBitmapSource::SkBitmapSource(const SkBitmap& bitmap, const SkRect& srcRect, const SkRect& dstRect)
    1.27 +  : INHERITED(0, 0),
    1.28 +    fBitmap(bitmap),
    1.29 +    fSrcRect(srcRect),
    1.30 +    fDstRect(dstRect) {
    1.31 +}
    1.32 +
    1.33 +SkBitmapSource::SkBitmapSource(SkReadBuffer& buffer)
    1.34 +  : INHERITED(0, buffer) {
    1.35 +    fBitmap.unflatten(buffer);
    1.36 +    buffer.readRect(&fSrcRect);
    1.37 +    buffer.readRect(&fDstRect);
    1.38 +    buffer.validate(buffer.isValid() && SkIsValidRect(fSrcRect) && SkIsValidRect(fDstRect));
    1.39 +}
    1.40 +
    1.41 +void SkBitmapSource::flatten(SkWriteBuffer& buffer) const {
    1.42 +    this->INHERITED::flatten(buffer);
    1.43 +    fBitmap.flatten(buffer);
    1.44 +    buffer.writeRect(fSrcRect);
    1.45 +    buffer.writeRect(fDstRect);
    1.46 +}
    1.47 +
    1.48 +bool SkBitmapSource::onFilterImage(Proxy* proxy, const SkBitmap&, const Context& ctx,
    1.49 +                                   SkBitmap* result, SkIPoint* offset) const {
    1.50 +    SkRect bounds, dstRect;
    1.51 +    fBitmap.getBounds(&bounds);
    1.52 +    ctx.ctm().mapRect(&dstRect, fDstRect);
    1.53 +    if (fSrcRect == bounds && dstRect == bounds) {
    1.54 +        // No regions cropped out or resized; return entire bitmap.
    1.55 +        *result = fBitmap;
    1.56 +        offset->fX = offset->fY = 0;
    1.57 +        return true;
    1.58 +    }
    1.59 +    SkIRect dstIRect;
    1.60 +    dstRect.roundOut(&dstIRect);
    1.61 +
    1.62 +    SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(dstIRect.width(), dstIRect.height()));
    1.63 +    if (NULL == device.get()) {
    1.64 +        return false;
    1.65 +    }
    1.66 +
    1.67 +    SkCanvas canvas(device.get());
    1.68 +    SkPaint paint;
    1.69 +
    1.70 +    // Subtract off the integer component of the translation (will be applied in loc, below).
    1.71 +    dstRect.offset(-SkIntToScalar(dstIRect.fLeft), -SkIntToScalar(dstIRect.fTop));
    1.72 +    paint.setXfermodeMode(SkXfermode::kSrc_Mode);
    1.73 +    // FIXME: this probably shouldn't be necessary, but drawBitmapRectToRect asserts
    1.74 +    // None filtering when it's translate-only
    1.75 +    paint.setFilterLevel(
    1.76 +        fSrcRect.width() == dstRect.width() && fSrcRect.height() == dstRect.height() ?
    1.77 +        SkPaint::kNone_FilterLevel : SkPaint::kHigh_FilterLevel);
    1.78 +    canvas.drawBitmapRectToRect(fBitmap, &fSrcRect, dstRect, &paint);
    1.79 +
    1.80 +    *result = device.get()->accessBitmap(false);
    1.81 +    offset->fX = dstIRect.fLeft;
    1.82 +    offset->fY = dstIRect.fTop;
    1.83 +    return true;
    1.84 +}
    1.85 +
    1.86 +void SkBitmapSource::computeFastBounds(const SkRect&, SkRect* dst) const {
    1.87 +    *dst = fDstRect;
    1.88 +}
    1.89 +
    1.90 +bool SkBitmapSource::onFilterBounds(const SkIRect& src, const SkMatrix& ctm,
    1.91 +                                    SkIRect* dst) const {
    1.92 +    *dst = src;
    1.93 +    return true;
    1.94 +}

mercurial