Debian Codegraph @

src/core/SkLocalMatrixImageFilter.cpp

raw
1 /*
2 * Copyright 2015 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 "src/core/SkLocalMatrixImageFilter.h"
9
10 #include "include/core/SkImageFilter.h"
11 #include "src/core/SkReadBuffer.h"
12 #include "src/core/SkWriteBuffer.h"
13
14 sk_sp<SkImageFilter> SkLocalMatrixImageFilter::Make(const SkMatrix& localMatrix,
15 sk_sp<SkImageFilter> input) {
16 if (!input) {
17 return nullptr;
18 }
19 if (localMatrix.isIdentity()) {
20 return input;
21 }
22
23 MatrixCapability inputCapability = as_IFB(input)->getCTMCapability();
24 if ((inputCapability == MatrixCapability::kTranslate && !localMatrix.isTranslate()) ||
25 (inputCapability == MatrixCapability::kScaleTranslate && !localMatrix.isScaleTranslate())) {
26 // Nothing we can do at this point
27 return nullptr;
28 }
29
30 if (auto invLocal = localMatrix.invert()) {
31 return sk_sp<SkImageFilter>(new SkLocalMatrixImageFilter(localMatrix, *invLocal, &input));
32 }
33 return nullptr;
34 }
35
36 sk_sp<SkFlattenable> SkLocalMatrixImageFilter::CreateProc(SkReadBuffer& buffer) {
37 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
38 SkMatrix lm;
39 buffer.readMatrix(&lm);
40 return SkLocalMatrixImageFilter::Make(lm, common.getInput(0));
41 }
42
43 void SkLocalMatrixImageFilter::flatten(SkWriteBuffer& buffer) const {
44 this->SkImageFilter_Base::flatten(buffer);
45 buffer.writeMatrix(fLocalMatrix);
46 // fInvLocalMatrix will be reconstructed
47 }
48
49 ///////////////////////////////////////////////////////////////////////////////
50
51 skif::Mapping SkLocalMatrixImageFilter::localMapping(const skif::Mapping& mapping) const {
52 skif::Mapping localMapping = mapping;
53 localMapping.concatLocal(fLocalMatrix);
54 return localMapping;
55 }
56
57 skif::FilterResult SkLocalMatrixImageFilter::onFilterImage(const skif::Context& ctx) const {
58 skif::Mapping localMapping = this->localMapping(ctx.mapping());
59 return this->getChildOutput(0, ctx.withNewMapping(localMapping));
60 }
61
62 skif::LayerSpace<SkIRect> SkLocalMatrixImageFilter::onGetInputLayerBounds(
63 const skif::Mapping& mapping,
64 const skif::LayerSpace<SkIRect>& desiredOutput,
65 std::optional<skif::LayerSpace<SkIRect>> contentBounds) const {
66 // The local matrix changes 'mapping' by adjusting the parameter space of the image filter, but
67 // both 'desiredOutput' and 'contentBounds' have already been transformed to the consistent
68 // layer space. They remain unchanged with the new mapping.
69 return this->getChildInputLayerBounds(0, this->localMapping(mapping),
70 desiredOutput, contentBounds);
71 }
72
73 std::optional<skif::LayerSpace<SkIRect>> SkLocalMatrixImageFilter::onGetOutputLayerBounds(
74 const skif::Mapping& mapping,
75 std::optional<skif::LayerSpace<SkIRect>> contentBounds) const {
76 return this->getChildOutputLayerBounds(0, this->localMapping(mapping), contentBounds);
77 }
78
79 SkRect SkLocalMatrixImageFilter::computeFastBounds(const SkRect& bounds) const {
80 // In onGet[Input|Output]LayerBounds, there is a Mapping that can be adjusted by the
81 // local matrix, so their layer-space parameters do not need to be modified. Since
82 // computeFastBounds() takes no matrix, it always operates as if it has the identity mapping.
83 //
84 // In order to match the behavior of onGetInputLayerBounds, we map 'bounds' by the inverse of
85 // the local matrix, pass that to the child, and then map the result by the local matrix.
86 // TODO: Implementing computeFastBounds in terms of onGetOutputLayerBounds() trivially removes
87 // this complexity.
88 SkRect localBounds = fInvLocalMatrix.mapRect(bounds);
89 return fLocalMatrix.mapRect(this->getInput(0)->computeFastBounds(localBounds));
90 }