Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / effects / SkResizeImageFilter.cpp
1 /*
2  * Copyright 2013 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7
8 #include "SkResizeImageFilter.h"
9 #include "SkBitmap.h"
10 #include "SkCanvas.h"
11 #include "SkDevice.h"
12 #include "SkColorPriv.h"
13 #include "SkReadBuffer.h"
14 #include "SkWriteBuffer.h"
15 #include "SkMatrix.h"
16 #include "SkRect.h"
17
18 SkResizeImageFilter::SkResizeImageFilter(SkScalar sx, SkScalar sy, SkPaint::FilterLevel filterLevel,
19                                          SkImageFilter* input)
20   : INHERITED(input),
21     fSx(sx),
22     fSy(sy),
23     fFilterLevel(filterLevel) {
24 }
25
26 SkResizeImageFilter::SkResizeImageFilter(SkReadBuffer& buffer)
27   : INHERITED(1, buffer) {
28     fSx = buffer.readScalar();
29     fSy = buffer.readScalar();
30     fFilterLevel = static_cast<SkPaint::FilterLevel>(buffer.readInt());
31 }
32
33 void SkResizeImageFilter::flatten(SkWriteBuffer& buffer) const {
34     this->INHERITED::flatten(buffer);
35     buffer.writeScalar(fSx);
36     buffer.writeScalar(fSy);
37     buffer.writeInt(fFilterLevel);
38 }
39
40 SkResizeImageFilter::~SkResizeImageFilter() {
41 }
42
43 bool SkResizeImageFilter::onFilterImage(Proxy* proxy,
44                                          const SkBitmap& source,
45                                          const Context& ctx,
46                                          SkBitmap* result,
47                                          SkIPoint* offset) const {
48     SkBitmap src = source;
49     SkIPoint srcOffset = SkIPoint::Make(0, 0);
50     if (getInput(0) && !getInput(0)->filterImage(proxy, source, ctx, &src, &srcOffset)) {
51         return false;
52     }
53
54     SkRect dstRect;
55     SkIRect srcBounds, dstBounds;
56     src.getBounds(&srcBounds);
57     srcBounds.offset(srcOffset);
58     SkRect srcRect = SkRect::Make(srcBounds);
59     SkMatrix matrix;
60     if (!ctx.ctm().invert(&matrix)) {
61         return false;
62     }
63     matrix.postScale(fSx, fSy);
64     matrix.postConcat(ctx.ctm());
65     matrix.mapRect(&dstRect, srcRect);
66     dstRect.roundOut(&dstBounds);
67
68     SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(dstBounds.width(), dstBounds.height()));
69     if (NULL == device.get()) {
70         return false;
71     }
72
73     SkCanvas canvas(device.get());
74     canvas.scale(fSx, fSy);
75     SkPaint paint;
76
77     paint.setXfermodeMode(SkXfermode::kSrc_Mode);
78     paint.setFilterLevel(fFilterLevel);
79     canvas.drawBitmap(src, 0, 0, &paint);
80
81     *result = device.get()->accessBitmap(false);
82     offset->fX = dstBounds.fLeft;
83     offset->fY = dstBounds.fTop;
84     return true;
85 }
86
87 void SkResizeImageFilter::computeFastBounds(const SkRect& src, SkRect* dst) const {
88     SkRect bounds = src;
89     if (getInput(0)) {
90         getInput(0)->computeFastBounds(src, &bounds);
91     }
92     dst->setXYWH(bounds.x(), bounds.y(), bounds.width() * fSx, bounds.height() * fSy);
93 }
94
95 bool SkResizeImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& ctm,
96                                          SkIRect* dst) const {
97     SkMatrix matrix;
98     if (!ctm.invert(&matrix)) {
99         return false;
100     }
101     matrix.postScale(SkScalarInvert(fSx), SkScalarInvert(fSy));
102     matrix.postConcat(ctm);
103     SkRect floatBounds;
104     matrix.mapRect(&floatBounds, SkRect::Make(src));
105     SkIRect bounds;
106     floatBounds.roundOut(&bounds);
107     if (getInput(0) && !getInput(0)->filterBounds(bounds, ctm, &bounds)) {
108         return false;
109     }
110
111     *dst = bounds;
112     return true;
113 }