| 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 |
| 10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
| 11 #include "SkDevice.h" | |
| 12 #include "SkColorPriv.h" | |
| 13 #include "SkReadBuffer.h" | 11 #include "SkReadBuffer.h" |
| 12 #include "SkSpecialImage.h" |
| 13 #include "SkSpecialSurface.h" |
| 14 #include "SkWriteBuffer.h" | 14 #include "SkWriteBuffer.h" |
| 15 #include "SkMatrix.h" | |
| 16 #include "SkRect.h" | 15 #include "SkRect.h" |
| 17 | 16 |
| 18 SkMatrixImageFilter::SkMatrixImageFilter(const SkMatrix& transform, | 17 SkMatrixImageFilter::SkMatrixImageFilter(const SkMatrix& transform, |
| 19 SkFilterQuality filterQuality, | 18 SkFilterQuality filterQuality, |
| 20 SkImageFilter* input) | 19 SkImageFilter* input) |
| 21 : INHERITED(1, &input), | 20 : INHERITED(1, &input) |
| 22 fTransform(transform), | 21 , fTransform(transform) |
| 23 fFilterQuality(filterQuality) { | 22 , fFilterQuality(filterQuality) { |
| 24 } | 23 } |
| 25 | 24 |
| 26 SkMatrixImageFilter* SkMatrixImageFilter::Create(const SkMatrix& transform, | 25 SkMatrixImageFilter* SkMatrixImageFilter::Create(const SkMatrix& transform, |
| 27 SkFilterQuality filterQuality, | 26 SkFilterQuality filterQuality, |
| 28 SkImageFilter* input) { | 27 SkImageFilter* input) { |
| 29 return new SkMatrixImageFilter(transform, filterQuality, input); | 28 return new SkMatrixImageFilter(transform, filterQuality, input); |
| 30 } | 29 } |
| 31 | 30 |
| 32 SkFlattenable* SkMatrixImageFilter::CreateProc(SkReadBuffer& buffer) { | 31 SkFlattenable* SkMatrixImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 33 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); | 32 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 34 SkMatrix matrix; | 33 SkMatrix matrix; |
| 35 buffer.readMatrix(&matrix); | 34 buffer.readMatrix(&matrix); |
| 36 SkFilterQuality quality = static_cast<SkFilterQuality>(buffer.readInt()); | 35 SkFilterQuality quality = static_cast<SkFilterQuality>(buffer.readInt()); |
| 37 return Create(matrix, quality, common.getInput(0).get()); | 36 return Create(matrix, quality, common.getInput(0).get()); |
| 38 } | 37 } |
| 39 | 38 |
| 40 void SkMatrixImageFilter::flatten(SkWriteBuffer& buffer) const { | 39 void SkMatrixImageFilter::flatten(SkWriteBuffer& buffer) const { |
| 41 this->INHERITED::flatten(buffer); | 40 this->INHERITED::flatten(buffer); |
| 42 buffer.writeMatrix(fTransform); | 41 buffer.writeMatrix(fTransform); |
| 43 buffer.writeInt(fFilterQuality); | 42 buffer.writeInt(fFilterQuality); |
| 44 } | 43 } |
| 45 | 44 |
| 46 SkMatrixImageFilter::~SkMatrixImageFilter() { | 45 sk_sp<SkSpecialImage> SkMatrixImageFilter::onFilterImage(SkSpecialImage* source, |
| 47 } | 46 const Context& ctx, |
| 47 SkIPoint* offset) const
{ |
| 48 | 48 |
| 49 bool SkMatrixImageFilter::onFilterImageDeprecated(Proxy* proxy, | 49 SkIPoint inputOffset = SkIPoint::Make(0, 0); |
| 50 const SkBitmap& source, | 50 sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset))
; |
| 51 const Context& ctx, | 51 if (!input) { |
| 52 SkBitmap* result, | 52 return nullptr; |
| 53 SkIPoint* offset) const { | |
| 54 SkBitmap src = source; | |
| 55 SkIPoint srcOffset = SkIPoint::Make(0, 0); | |
| 56 if (!this->filterInputDeprecated(0, proxy, source, ctx, &src, &srcOffset)) { | |
| 57 return false; | |
| 58 } | 53 } |
| 59 | 54 |
| 60 SkRect dstRect; | |
| 61 SkIRect srcBounds, dstBounds; | |
| 62 src.getBounds(&srcBounds); | |
| 63 srcBounds.offset(srcOffset); | |
| 64 SkRect srcRect = SkRect::Make(srcBounds); | |
| 65 SkMatrix matrix; | 55 SkMatrix matrix; |
| 66 if (!ctx.ctm().invert(&matrix)) { | 56 if (!ctx.ctm().invert(&matrix)) { |
| 67 return false; | 57 return nullptr; |
| 68 } | 58 } |
| 69 matrix.postConcat(fTransform); | 59 matrix.postConcat(fTransform); |
| 70 matrix.postConcat(ctx.ctm()); | 60 matrix.postConcat(ctx.ctm()); |
| 61 |
| 62 const SkIRect srcBounds = SkIRect::MakeXYWH(inputOffset.x(), inputOffset.y()
, |
| 63 input->width(), input->height())
; |
| 64 const SkRect srcRect = SkRect::Make(srcBounds); |
| 65 |
| 66 SkRect dstRect; |
| 71 matrix.mapRect(&dstRect, srcRect); | 67 matrix.mapRect(&dstRect, srcRect); |
| 68 SkIRect dstBounds; |
| 72 dstRect.roundOut(&dstBounds); | 69 dstRect.roundOut(&dstBounds); |
| 73 | 70 |
| 74 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(dstBounds.width(), dst
Bounds.height())); | 71 const SkImageInfo info = SkImageInfo::MakeN32Premul(dstBounds.width(), dstBo
unds.height()); |
| 75 if (nullptr == device.get()) { | 72 |
| 76 return false; | 73 sk_sp<SkSpecialSurface> surf(input->makeSurface(info)); |
| 74 if (!surf) { |
| 75 return nullptr; |
| 77 } | 76 } |
| 78 | 77 |
| 79 SkCanvas canvas(device.get()); | 78 SkCanvas* canvas = surf->getCanvas(); |
| 80 canvas.translate(-SkIntToScalar(dstBounds.x()), -SkIntToScalar(dstBounds.y()
)); | 79 SkASSERT(canvas); |
| 81 canvas.concat(matrix); | 80 |
| 81 canvas->clear(0x0); |
| 82 |
| 83 canvas->translate(-SkIntToScalar(dstBounds.x()), -SkIntToScalar(dstBounds.y(
))); |
| 84 canvas->concat(matrix); |
| 85 |
| 82 SkPaint paint; | 86 SkPaint paint; |
| 83 | |
| 84 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 87 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 85 paint.setFilterQuality(fFilterQuality); | 88 paint.setFilterQuality(fFilterQuality); |
| 86 canvas.drawBitmap(src, srcRect.x(), srcRect.y(), &paint); | |
| 87 | 89 |
| 88 *result = device.get()->accessBitmap(false); | 90 input->draw(canvas, srcRect.x(), srcRect.y(), &paint); |
| 91 |
| 89 offset->fX = dstBounds.fLeft; | 92 offset->fX = dstBounds.fLeft; |
| 90 offset->fY = dstBounds.fTop; | 93 offset->fY = dstBounds.fTop; |
| 91 return true; | 94 return surf->makeImageSnapshot(); |
| 92 } | 95 } |
| 93 | 96 |
| 94 SkRect SkMatrixImageFilter::computeFastBounds(const SkRect& src) const { | 97 SkRect SkMatrixImageFilter::computeFastBounds(const SkRect& src) const { |
| 95 SkRect bounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src
) : src; | 98 SkRect bounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src
) : src; |
| 96 SkRect dst; | 99 SkRect dst; |
| 97 fTransform.mapRect(&dst, bounds); | 100 fTransform.mapRect(&dst, bounds); |
| 98 return dst; | 101 return dst; |
| 99 } | 102 } |
| 100 | 103 |
| 101 SkIRect SkMatrixImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatr
ix& ctm, | 104 SkIRect SkMatrixImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatr
ix& ctm, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 fTransform[SkMatrix::kMPersp2]); | 138 fTransform[SkMatrix::kMPersp2]); |
| 136 | 139 |
| 137 str->append("<dt>FilterLevel:</dt><dd>"); | 140 str->append("<dt>FilterLevel:</dt><dd>"); |
| 138 static const char* gFilterLevelStrings[] = { "None", "Low", "Medium", "High"
}; | 141 static const char* gFilterLevelStrings[] = { "None", "Low", "Medium", "High"
}; |
| 139 str->append(gFilterLevelStrings[fFilterQuality]); | 142 str->append(gFilterLevelStrings[fFilterQuality]); |
| 140 str->append("</dd>"); | 143 str->append("</dd>"); |
| 141 | 144 |
| 142 str->appendf(")"); | 145 str->appendf(")"); |
| 143 } | 146 } |
| 144 #endif | 147 #endif |
| OLD | NEW |