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 "include/core/SkCanvas.h"
9 #include "include/effects/SkImageFilters.h"
10 #include "src/core/SkImageFilter_Base.h"
11 #include "src/core/SkReadBuffer.h"
12 #include "src/core/SkSpecialImage.h"
13 #include "src/core/SkSpecialSurface.h"
14 #include "src/core/SkValidationUtils.h"
15 #include "src/core/SkWriteBuffer.h"
16
17 namespace {
18
19 class SkMergeImageFilter final : public SkImageFilter_Base {
20 public:
SkMergeImageFilter(sk_sp<SkImageFilter> * const filters,int count,const SkRect * cropRect)21 SkMergeImageFilter(sk_sp<SkImageFilter>* const filters, int count,
22 const SkRect* cropRect)
23 : INHERITED(filters, count, cropRect) {
24 SkASSERT(count >= 0);
25 }
26
27 protected:
28 sk_sp<SkSpecialImage> onFilterImage(const Context&, SkIPoint* offset) const override;
onGetCTMCapability() const29 MatrixCapability onGetCTMCapability() const override { return MatrixCapability::kComplex; }
30
31 private:
32 friend void ::SkRegisterMergeImageFilterFlattenable();
33 SK_FLATTENABLE_HOOKS(SkMergeImageFilter)
34
35 using INHERITED = SkImageFilter_Base;
36 };
37
38 } // end namespace
Merge(sk_sp<SkImageFilter> * const filters,int count,const CropRect & cropRect)39 sk_sp<SkImageFilter> SkImageFilters::Merge(sk_sp<SkImageFilter>* const filters, int count,
40 const CropRect& cropRect) {
41 return sk_sp<SkImageFilter>(new SkMergeImageFilter(filters, count, cropRect));
42 }
43
SkRegisterMergeImageFilterFlattenable()44 void SkRegisterMergeImageFilterFlattenable() {
45 SK_REGISTER_FLATTENABLE(SkMergeImageFilter);
46 // TODO (michaelludwig) - Remove after grace period for SKPs to stop using old name
47 SkFlattenable::Register("SkMergeImageFilterImpl", SkMergeImageFilter::CreateProc);
48 }
49
CreateProc(SkReadBuffer & buffer)50 sk_sp<SkFlattenable> SkMergeImageFilter::CreateProc(SkReadBuffer& buffer) {
51 Common common;
52 if (!common.unflatten(buffer, -1) || !buffer.isValid()) {
53 return nullptr;
54 }
55 return SkImageFilters::Merge(common.inputs(), common.inputCount(), common.cropRect());
56 }
57
58 ///////////////////////////////////////////////////////////////////////////////
59
onFilterImage(const Context & ctx,SkIPoint * offset) const60 sk_sp<SkSpecialImage> SkMergeImageFilter::onFilterImage(const Context& ctx,
61 SkIPoint* offset) const {
62 int inputCount = this->countInputs();
63 if (inputCount < 1) {
64 return nullptr;
65 }
66
67 SkIRect bounds;
68 bounds.setEmpty();
69
70 std::unique_ptr<sk_sp<SkSpecialImage>[]> inputs(new sk_sp<SkSpecialImage>[inputCount]);
71 std::unique_ptr<SkIPoint[]> offsets(new SkIPoint[inputCount]);
72
73 // Filter all of the inputs.
74 for (int i = 0; i < inputCount; ++i) {
75 offsets[i] = { 0, 0 };
76 inputs[i] = this->filterInput(i, ctx, &offsets[i]);
77 if (!inputs[i]) {
78 continue;
79 }
80 const SkIRect inputBounds = SkIRect::MakeXYWH(offsets[i].fX, offsets[i].fY,
81 inputs[i]->width(), inputs[i]->height());
82 bounds.join(inputBounds);
83 }
84 if (bounds.isEmpty()) {
85 return nullptr;
86 }
87
88 // Apply the crop rect to the union of the inputs' bounds.
89 // Note that the crop rect can only reduce the bounds, since this
90 // filter does not affect transparent black.
91 bool embiggen = false;
92 this->getCropRect().applyTo(bounds, ctx.ctm(), embiggen, &bounds);
93 if (!bounds.intersect(ctx.clipBounds())) {
94 return nullptr;
95 }
96
97 const int x0 = bounds.left();
98 const int y0 = bounds.top();
99
100 sk_sp<SkSpecialSurface> surf(ctx.makeSurface(bounds.size()));
101 if (!surf) {
102 return nullptr;
103 }
104
105 SkCanvas* canvas = surf->getCanvas();
106 SkASSERT(canvas);
107
108 canvas->clear(0x0);
109
110 // Composite all of the filter inputs.
111 for (int i = 0; i < inputCount; ++i) {
112 if (!inputs[i]) {
113 continue;
114 }
115
116 inputs[i]->draw(canvas,
117 SkIntToScalar(offsets[i].x() - x0), SkIntToScalar(offsets[i].y() - y0));
118 }
119
120 offset->fX = bounds.left();
121 offset->fY = bounds.top();
122 return surf->makeImageSnapshot();
123 }
124