1 /*
2 * Copyright 2012 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 "SkOffsetImageFilter.h"
9 #include "SkColorSpaceXformer.h"
10 #include "SkCanvas.h"
11 #include "SkMatrix.h"
12 #include "SkPaint.h"
13 #include "SkReadBuffer.h"
14 #include "SkSpecialImage.h"
15 #include "SkSpecialSurface.h"
16 #include "SkWriteBuffer.h"
17
Make(SkScalar dx,SkScalar dy,sk_sp<SkImageFilter> input,const CropRect * cropRect)18 sk_sp<SkImageFilter> SkOffsetImageFilter::Make(SkScalar dx, SkScalar dy,
19 sk_sp<SkImageFilter> input,
20 const CropRect* cropRect) {
21 if (!SkScalarIsFinite(dx) || !SkScalarIsFinite(dy)) {
22 return nullptr;
23 }
24
25 return sk_sp<SkImageFilter>(new SkOffsetImageFilter(dx, dy, std::move(input), cropRect));
26 }
27
onFilterImage(SkSpecialImage * source,const Context & ctx,SkIPoint * offset) const28 sk_sp<SkSpecialImage> SkOffsetImageFilter::onFilterImage(SkSpecialImage* source,
29 const Context& ctx,
30 SkIPoint* offset) const {
31 SkIPoint srcOffset = SkIPoint::Make(0, 0);
32 sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &srcOffset));
33 if (!input) {
34 return nullptr;
35 }
36
37 SkVector vec;
38 ctx.ctm().mapVectors(&vec, &fOffset, 1);
39
40 if (!this->cropRectIsSet()) {
41 offset->fX = srcOffset.fX + SkScalarRoundToInt(vec.fX);
42 offset->fY = srcOffset.fY + SkScalarRoundToInt(vec.fY);
43 return input;
44 } else {
45 SkIRect bounds;
46 SkIRect srcBounds = SkIRect::MakeWH(input->width(), input->height());
47 srcBounds.offset(srcOffset);
48 if (!this->applyCropRect(ctx, srcBounds, &bounds)) {
49 return nullptr;
50 }
51
52 sk_sp<SkSpecialSurface> surf(source->makeSurface(ctx.outputProperties(), bounds.size()));
53 if (!surf) {
54 return nullptr;
55 }
56
57 SkCanvas* canvas = surf->getCanvas();
58 SkASSERT(canvas);
59
60 // TODO: it seems like this clear shouldn't be necessary (see skbug.com/5075)
61 canvas->clear(0x0);
62
63 SkPaint paint;
64 paint.setBlendMode(SkBlendMode::kSrc);
65 canvas->translate(SkIntToScalar(srcOffset.fX - bounds.fLeft),
66 SkIntToScalar(srcOffset.fY - bounds.fTop));
67
68 input->draw(canvas, vec.x(), vec.y(), &paint);
69
70 offset->fX = bounds.fLeft;
71 offset->fY = bounds.fTop;
72 return surf->makeImageSnapshot();
73 }
74 }
75
onMakeColorSpace(SkColorSpaceXformer * xformer) const76 sk_sp<SkImageFilter> SkOffsetImageFilter::onMakeColorSpace(SkColorSpaceXformer* xformer) const {
77 SkASSERT(1 == this->countInputs());
78
79 auto input = xformer->apply(this->getInput(0));
80 if (input.get() != this->getInput(0)) {
81 return SkOffsetImageFilter::Make(fOffset.fX, fOffset.fY, std::move(input),
82 this->getCropRectIfSet());
83 }
84 return this->refMe();
85 }
86
computeFastBounds(const SkRect & src) const87 SkRect SkOffsetImageFilter::computeFastBounds(const SkRect& src) const {
88 SkRect bounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src) : src;
89 bounds.offset(fOffset.fX, fOffset.fY);
90 return bounds;
91 }
92
onFilterNodeBounds(const SkIRect & src,const SkMatrix & ctm,MapDirection direction) const93 SkIRect SkOffsetImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm,
94 MapDirection direction) const {
95 SkVector vec;
96 ctm.mapVectors(&vec, &fOffset, 1);
97 if (kReverse_MapDirection == direction) {
98 vec.negate();
99 }
100
101 return src.makeOffset(SkScalarCeilToInt(vec.fX), SkScalarCeilToInt(vec.fY));
102 }
103
CreateProc(SkReadBuffer & buffer)104 sk_sp<SkFlattenable> SkOffsetImageFilter::CreateProc(SkReadBuffer& buffer) {
105 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
106 SkPoint offset;
107 buffer.readPoint(&offset);
108 return Make(offset.x(), offset.y(), common.getInput(0), &common.cropRect());
109 }
110
flatten(SkWriteBuffer & buffer) const111 void SkOffsetImageFilter::flatten(SkWriteBuffer& buffer) const {
112 this->INHERITED::flatten(buffer);
113 buffer.writePoint(fOffset);
114 }
115
SkOffsetImageFilter(SkScalar dx,SkScalar dy,sk_sp<SkImageFilter> input,const CropRect * cropRect)116 SkOffsetImageFilter::SkOffsetImageFilter(SkScalar dx, SkScalar dy,
117 sk_sp<SkImageFilter> input,
118 const CropRect* cropRect)
119 : INHERITED(&input, 1, cropRect) {
120 fOffset.set(dx, dy);
121 }
122
123 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const124 void SkOffsetImageFilter::toString(SkString* str) const {
125 str->appendf("SkOffsetImageFilter: (");
126 str->appendf("offset: (%f, %f) ", fOffset.fX, fOffset.fY);
127 str->append("input: (");
128 if (this->getInput(0)) {
129 this->getInput(0)->toString(str);
130 }
131 str->append("))");
132 }
133 #endif
134