OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 The Android Open Source Project | 2 * Copyright 2014 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkMatrixImageFilter.h" | 8 #include "SkMatrixImageFilter.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkDevice.h" | 11 #include "SkSurface.h" |
12 #include "SkColorPriv.h" | 12 #include "SkColorPriv.h" |
13 #include "SkReadBuffer.h" | 13 #include "SkReadBuffer.h" |
14 #include "SkWriteBuffer.h" | 14 #include "SkWriteBuffer.h" |
15 #include "SkMatrix.h" | 15 #include "SkMatrix.h" |
16 #include "SkRect.h" | 16 #include "SkRect.h" |
17 | 17 |
18 SkMatrixImageFilter::SkMatrixImageFilter(const SkMatrix& transform, | 18 SkMatrixImageFilter::SkMatrixImageFilter(const SkMatrix& transform, |
19 SkPaint::FilterLevel filterLevel, | 19 SkPaint::FilterLevel filterLevel, |
20 SkImageFilter* input, | 20 SkImageFilter* input, |
21 uint32_t uniqueID) | 21 uint32_t uniqueID) |
(...skipping 20 matching lines...) Expand all Loading... |
42 void SkMatrixImageFilter::flatten(SkWriteBuffer& buffer) const { | 42 void SkMatrixImageFilter::flatten(SkWriteBuffer& buffer) const { |
43 this->INHERITED::flatten(buffer); | 43 this->INHERITED::flatten(buffer); |
44 buffer.writeMatrix(fTransform); | 44 buffer.writeMatrix(fTransform); |
45 buffer.writeInt(fFilterLevel); | 45 buffer.writeInt(fFilterLevel); |
46 } | 46 } |
47 | 47 |
48 SkMatrixImageFilter::~SkMatrixImageFilter() { | 48 SkMatrixImageFilter::~SkMatrixImageFilter() { |
49 } | 49 } |
50 | 50 |
51 bool SkMatrixImageFilter::onFilterImage(Proxy* proxy, | 51 bool SkMatrixImageFilter::onFilterImage(Proxy* proxy, |
52 const SkBitmap& source, | 52 SkImage& source, |
53 const Context& ctx, | 53 const Context& ctx, |
54 SkBitmap* result, | 54 SkAutoTUnref<SkImage>& result, |
55 SkIPoint* offset) const { | 55 SkIPoint* offset) const { |
56 SkBitmap src = source; | 56 SkAutoTUnref<SkImage> src(SkRef(&source)); |
57 SkIPoint srcOffset = SkIPoint::Make(0, 0); | 57 SkIPoint srcOffset = SkIPoint::Make(0, 0); |
58 if (getInput(0) && !getInput(0)->filterImage(proxy, source, ctx, &src, &srcO
ffset)) { | 58 if (getInput(0) && !getInput(0)->filterImage(proxy, source, ctx, src, &srcOf
fset)) { |
59 return false; | 59 return false; |
60 } | 60 } |
61 | 61 |
62 SkRect dstRect; | 62 SkRect dstRect; |
63 SkIRect srcBounds, dstBounds; | 63 SkIRect srcBounds = SkIRect::MakeWH(src->width(), src->height()); |
64 src.getBounds(&srcBounds); | |
65 srcBounds.offset(srcOffset); | 64 srcBounds.offset(srcOffset); |
66 SkRect srcRect = SkRect::Make(srcBounds); | 65 SkRect srcRect = SkRect::Make(srcBounds); |
67 SkMatrix matrix; | 66 SkMatrix matrix; |
68 if (!ctx.ctm().invert(&matrix)) { | 67 if (!ctx.ctm().invert(&matrix)) { |
69 return false; | 68 return false; |
70 } | 69 } |
71 matrix.postConcat(fTransform); | 70 matrix.postConcat(fTransform); |
72 matrix.postConcat(ctx.ctm()); | 71 matrix.postConcat(ctx.ctm()); |
73 matrix.mapRect(&dstRect, srcRect); | 72 matrix.mapRect(&dstRect, srcRect); |
| 73 SkIRect dstBounds; |
74 dstRect.roundOut(&dstBounds); | 74 dstRect.roundOut(&dstBounds); |
75 | 75 |
76 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(dstBounds.width(), dst
Bounds.height())); | 76 SkAutoTUnref<SkSurface> surface(proxy->createSurface(dstBounds.width(), dstB
ounds.height())); |
77 if (NULL == device.get()) { | 77 if (NULL == surface) { |
78 return false; | 78 return false; |
79 } | 79 } |
80 | 80 |
81 SkCanvas canvas(device.get()); | 81 SkCanvas* canvas = surface->getCanvas(); |
82 canvas.translate(-SkIntToScalar(dstBounds.x()), -SkIntToScalar(dstBounds.y()
)); | 82 canvas->translate(-SkIntToScalar(dstBounds.x()), -SkIntToScalar(dstBounds.y(
))); |
83 canvas.concat(matrix); | 83 canvas->concat(matrix); |
84 SkPaint paint; | 84 SkPaint paint; |
85 | 85 |
86 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 86 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
87 paint.setFilterLevel(fFilterLevel); | 87 paint.setFilterLevel(fFilterLevel); |
88 canvas.drawBitmap(src, srcRect.x(), srcRect.y(), &paint); | 88 canvas->drawImage(src, srcRect.x(), srcRect.y(), &paint); |
89 | 89 |
90 *result = device.get()->accessBitmap(false); | 90 SkImage* image = surface->newImageSnapshot(SkSurface::kYes_Budgeted); |
| 91 if (NULL == image) { |
| 92 return false; |
| 93 } |
| 94 result.reset(image); |
91 offset->fX = dstBounds.fLeft; | 95 offset->fX = dstBounds.fLeft; |
92 offset->fY = dstBounds.fTop; | 96 offset->fY = dstBounds.fTop; |
93 return true; | 97 return true; |
94 } | 98 } |
95 | 99 |
96 void SkMatrixImageFilter::computeFastBounds(const SkRect& src, SkRect* dst) cons
t { | 100 void SkMatrixImageFilter::computeFastBounds(const SkRect& src, SkRect* dst) cons
t { |
97 SkRect bounds = src; | 101 SkRect bounds = src; |
98 if (getInput(0)) { | 102 if (getInput(0)) { |
99 getInput(0)->computeFastBounds(src, &bounds); | 103 getInput(0)->computeFastBounds(src, &bounds); |
100 } | 104 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 fTransform[SkMatrix::kMPersp2]); | 145 fTransform[SkMatrix::kMPersp2]); |
142 | 146 |
143 str->append("<dt>FilterLevel:</dt><dd>"); | 147 str->append("<dt>FilterLevel:</dt><dd>"); |
144 static const char* gFilterLevelStrings[] = { "None", "Low", "Medium", "High"
}; | 148 static const char* gFilterLevelStrings[] = { "None", "Low", "Medium", "High"
}; |
145 str->append(gFilterLevelStrings[fFilterLevel]); | 149 str->append(gFilterLevelStrings[fFilterLevel]); |
146 str->append("</dd>"); | 150 str->append("</dd>"); |
147 | 151 |
148 str->appendf(")"); | 152 str->appendf(")"); |
149 } | 153 } |
150 #endif | 154 #endif |
OLD | NEW |