1 /*
2 * Copyright 2023 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 "src/shaders/SkCoordClampShader.h"
9
10 #include "include/core/SkFlattenable.h"
11 #include "include/private/base/SkTo.h"
12 #include "src/base/SkArenaAlloc.h"
13 #include "src/core/SkEffectPriv.h"
14 #include "src/core/SkRasterPipeline.h"
15 #include "src/core/SkRasterPipelineOpContexts.h"
16 #include "src/core/SkRasterPipelineOpList.h"
17 #include "src/core/SkReadBuffer.h"
18 #include "src/core/SkWriteBuffer.h"
19 #include "src/shaders/SkShaderBase.h"
20
21 #include <optional>
22
CreateProc(SkReadBuffer & buffer)23 sk_sp<SkFlattenable> SkCoordClampShader::CreateProc(SkReadBuffer& buffer) {
24 sk_sp<SkShader> shader(buffer.readShader());
25 SkRect subset = buffer.readRect();
26 if (!buffer.validate(SkToBool(shader))) {
27 return nullptr;
28 }
29 return SkShaders::CoordClamp(std::move(shader), subset);
30 }
31
flatten(SkWriteBuffer & buffer) const32 void SkCoordClampShader::flatten(SkWriteBuffer& buffer) const {
33 buffer.writeFlattenable(fShader.get());
34 buffer.writeRect(fSubset);
35 }
36
appendStages(const SkStageRec & rec,const SkShaders::MatrixRec & mRec) const37 bool SkCoordClampShader::appendStages(const SkStageRec& rec,
38 const SkShaders::MatrixRec& mRec) const {
39 std::optional<SkShaders::MatrixRec> childMRec = mRec.apply(rec);
40 if (!childMRec.has_value()) {
41 return false;
42 }
43 // Strictly speaking, childMRec's total matrix is not valid. It is only valid inside the subset
44 // rectangle. However, we don't mark it as such because we want the "total matrix is valid"
45 // behavior in SkImageShader for filtering.
46 auto clampCtx = rec.fAlloc->make<SkRasterPipeline_CoordClampCtx>();
47 *clampCtx = {fSubset.fLeft, fSubset.fTop, fSubset.fRight, fSubset.fBottom};
48 rec.fPipeline->append(SkRasterPipelineOp::clamp_x_and_y, clampCtx);
49 return as_SB(fShader)->appendStages(rec, *childMRec);
50 }
51
SkRegisterCoordClampShaderFlattenable()52 void SkRegisterCoordClampShaderFlattenable() {
53 SK_REGISTER_FLATTENABLE(SkCoordClampShader);
54
55 // Previous name
56 SkFlattenable::Register("SkShader_CoordClamp", SkCoordClampShader::CreateProc);
57 }
58
CoordClamp(sk_sp<SkShader> shader,const SkRect & subset)59 sk_sp<SkShader> SkShaders::CoordClamp(sk_sp<SkShader> shader, const SkRect& subset) {
60 if (!shader) {
61 return nullptr;
62 }
63 if (!subset.isSorted()) {
64 return nullptr;
65 }
66 return sk_make_sp<SkCoordClampShader>(std::move(shader), subset);
67 }
68