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 "SkBitmap.h"
10 #include "SkCanvas.h"
11 #include "SkDevice.h"
12 #include "SkReadBuffer.h"
13 #include "SkWriteBuffer.h"
14
Create(const SkPaint & paint,const CropRect * cropRect)15 SkImageFilter* SkPaintImageFilter::Create(const SkPaint& paint, const CropRect* cropRect) {
16 return new SkPaintImageFilter(paint, cropRect);
17 }
18
SkPaintImageFilter(const SkPaint & paint,const CropRect * cropRect)19 SkPaintImageFilter::SkPaintImageFilter(const SkPaint& paint, const CropRect* cropRect)
20 : INHERITED(0, nullptr, cropRect)
21 , fPaint(paint) {
22 }
23
CreateProc(SkReadBuffer & buffer)24 SkFlattenable* SkPaintImageFilter::CreateProc(SkReadBuffer& buffer) {
25 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0);
26 SkPaint paint;
27 buffer.readPaint(&paint);
28 return Create(paint, &common.cropRect());
29 }
30
flatten(SkWriteBuffer & buffer) const31 void SkPaintImageFilter::flatten(SkWriteBuffer& buffer) const {
32 this->INHERITED::flatten(buffer);
33 buffer.writePaint(fPaint);
34 }
35
onFilterImageDeprecated(Proxy * proxy,const SkBitmap & source,const Context & ctx,SkBitmap * result,SkIPoint * offset) const36 bool SkPaintImageFilter::onFilterImageDeprecated(Proxy* proxy,
37 const SkBitmap& source,
38 const Context& ctx,
39 SkBitmap* result,
40 SkIPoint* offset) const {
41 SkIRect bounds;
42 if (!this->applyCropRect(ctx, source.bounds(), &bounds)) {
43 return false;
44 }
45
46 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(),
47 bounds.height()));
48 if (nullptr == device.get()) {
49 return false;
50 }
51 SkCanvas canvas(device.get());
52
53 SkMatrix matrix(ctx.ctm());
54 matrix.postTranslate(SkIntToScalar(-bounds.left()), SkIntToScalar(-bounds.top()));
55 SkRect rect = SkRect::MakeWH(SkIntToScalar(bounds.width()), SkIntToScalar(bounds.height()));
56 SkMatrix inverse;
57 if (matrix.invert(&inverse)) {
58 inverse.mapRect(&rect);
59 }
60 canvas.setMatrix(matrix);
61 canvas.drawRect(rect, fPaint);
62
63 *result = device.get()->accessBitmap(false);
64 offset->fX = bounds.fLeft;
65 offset->fY = bounds.fTop;
66 return true;
67 }
68
canComputeFastBounds() const69 bool SkPaintImageFilter::canComputeFastBounds() const {
70 // http:skbug.com/4627: "make computeFastBounds and onFilterBounds() CropRect-aware"
71 // computeFastBounds() doesn't currently take the crop rect into account,
72 // so we can't compute it. If a full crop rect is set, we should return true here.
73 return false;
74 }
75
76 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const77 void SkPaintImageFilter::toString(SkString* str) const {
78 str->appendf("SkPaintImageFilter: (");
79 str->append(")");
80 }
81 #endif
82