1 /* 2 * Copyright 2016 Google Inc. 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 "SkPaintImageFilter.h" 9 #include "SkCanvas.h" 10 #include "SkReadBuffer.h" 11 #include "SkSpecialImage.h" 12 #include "SkSpecialSurface.h" 13 #include "SkWriteBuffer.h" 14 15 sk_sp<SkImageFilter> SkPaintImageFilter::Make(const SkPaint& paint, 16 const CropRect* cropRect) { 17 return sk_sp<SkImageFilter>(new SkPaintImageFilter(paint, cropRect)); 18 } 19 20 SkPaintImageFilter::SkPaintImageFilter(const SkPaint& paint, const CropRect* cropRect) 21 : INHERITED(nullptr, 0, cropRect) 22 , fPaint(paint) { 23 } 24 25 sk_sp<SkFlattenable> SkPaintImageFilter::CreateProc(SkReadBuffer& buffer) { 26 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0); 27 SkPaint paint; 28 buffer.readPaint(&paint); 29 return SkPaintImageFilter::Make(paint, &common.cropRect()); 30 } 31 32 void SkPaintImageFilter::flatten(SkWriteBuffer& buffer) const { 33 this->INHERITED::flatten(buffer); 34 buffer.writePaint(fPaint); 35 } 36 37 sk_sp<SkSpecialImage> SkPaintImageFilter::onFilterImage(SkSpecialImage* source, 38 const Context& ctx, 39 SkIPoint* offset) const { 40 SkIRect bounds; 41 const SkIRect srcBounds = SkIRect::MakeWH(source->width(), source->height()); 42 if (!this->applyCropRect(ctx, srcBounds, &bounds)) { 43 return nullptr; 44 } 45 46 sk_sp<SkSpecialSurface> surf(source->makeSurface(ctx.outputProperties(), bounds.size())); 47 if (!surf) { 48 return nullptr; 49 } 50 51 SkCanvas* canvas = surf->getCanvas(); 52 SkASSERT(canvas); 53 54 canvas->clear(0x0); 55 56 SkMatrix matrix(ctx.ctm()); 57 matrix.postTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top())); 58 SkRect rect = SkRect::MakeIWH(bounds.width(), bounds.height()); 59 SkMatrix inverse; 60 if (matrix.invert(&inverse)) { 61 inverse.mapRect(&rect); 62 } 63 canvas->setMatrix(matrix); 64 canvas->drawRect(rect, fPaint); 65 66 offset->fX = bounds.fLeft; 67 offset->fY = bounds.fTop; 68 return surf->makeImageSnapshot(); 69 } 70 71 bool SkPaintImageFilter::affectsTransparentBlack() const { 72 return true; 73 } 74 75 #ifndef SK_IGNORE_TO_STRING 76 void SkPaintImageFilter::toString(SkString* str) const { 77 str->appendf("SkPaintImageFilter: ("); 78 fPaint.toString(str); 79 str->append(")"); 80 } 81 #endif 82