Debian Codegraph @

src/core/SkRuntimeBlender.cpp

raw
1 /*
2 * Copyright 2019 Google LLC
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 #include "src/core/SkRuntimeBlender.h"
8
9 #include "include/core/SkCapabilities.h"
10 #include "include/core/SkData.h"
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkString.h"
13 #include "include/effects/SkRuntimeEffect.h"
14 #include "include/private/SkSLSampleUsage.h"
15 #include "include/private/base/SkDebug.h"
16 #include "include/private/base/SkTArray.h"
17 #include "src/core/SkEffectPriv.h"
18 #include "src/core/SkKnownRuntimeEffects.h"
19 #include "src/core/SkPicturePriv.h"
20 #include "src/core/SkReadBuffer.h"
21 #include "src/core/SkRuntimeEffectPriv.h"
22 #include "src/core/SkWriteBuffer.h"
23 #include "src/shaders/SkShaderBase.h"
24 #include "src/sksl/codegen/SkSLRasterPipelineBuilder.h"
25
26 #include <cstdint>
27 #include <string>
28
29 using namespace skia_private;
30
31 #if defined(SK_BUILD_FOR_DEBUGGER)
32 constexpr bool kLenientSkSLDeserialization = true;
33 #else
34 constexpr bool kLenientSkSLDeserialization = false;
35 #endif
36
37 void SkRuntimeBlender::flatten(SkWriteBuffer& buffer) const {
38 if (SkKnownRuntimeEffects::IsSkiaKnownRuntimeEffect(fEffect->fStableKey)) {
39 // We only serialize Skia-internal stableKeys. First party stable keys are not serialized.
40 buffer.write32(fEffect->fStableKey);
41 } else {
42 buffer.write32(0);
43 buffer.writeString(fEffect->source().c_str());
44 }
45 buffer.writeDataAsByteArray(fUniforms.get());
46 SkRuntimeEffectPriv::WriteChildEffects(buffer, fChildren);
47 }
48
49 sk_sp<SkFlattenable> SkRuntimeBlender::CreateProc(SkReadBuffer& buffer) {
50 if (!buffer.validate(buffer.allowSkSL())) {
51 return nullptr;
52 }
53
54 sk_sp<SkRuntimeEffect> effect;
55 if (!buffer.isVersionLT(SkPicturePriv::kSerializeStableKeys)) {
56 uint32_t candidateStableKey = buffer.readUInt();
57 effect = SkKnownRuntimeEffects::MaybeGetKnownRuntimeEffect(candidateStableKey);
58 if (!effect && !buffer.validate(candidateStableKey == 0)) {
59 return nullptr;
60 }
61 }
62
63 if (!effect) {
64 SkString sksl;
65 buffer.readString(&sksl);
66 effect = SkMakeCachedRuntimeEffect(SkRuntimeEffect::MakeForBlender, std::move(sksl));
67 }
68 if constexpr (!kLenientSkSLDeserialization) {
69 if (!buffer.validate(effect != nullptr)) {
70 return nullptr;
71 }
72 }
73
74 sk_sp<SkData> uniforms = buffer.readByteArrayAsData();
75
76 STArray<4, SkRuntimeEffect::ChildPtr> children;
77 if (!SkRuntimeEffectPriv::ReadChildEffects(buffer, effect.get(), &children)) {
78 return nullptr;
79 }
80
81 if constexpr (kLenientSkSLDeserialization) {
82 if (!effect) {
83 SkDebugf("Serialized SkSL failed to compile. Ignoring/dropping SkSL blender.\n");
84 return nullptr;
85 }
86 }
87
88 return effect->makeBlender(std::move(uniforms), SkSpan(children));
89 }
90
91 bool SkRuntimeBlender::onAppendStages(const SkStageRec& rec) const {
92 if (!SkRuntimeEffectPriv::CanDraw(SkCapabilities::RasterBackend().get(), fEffect.get())) {
93 // SkRP has support for many parts of #version 300 already, but for now, we restrict its
94 // usage in runtime effects to just #version 100.
95 return false;
96 }
97 if (const SkSL::RP::Program* program = fEffect->getRPProgram(/*debugTrace=*/nullptr)) {
98 SkSpan<const float> uniforms = SkRuntimeEffectPriv::UniformsAsSpan(
99 fEffect->uniforms(),
100 fUniforms,
101 /*alwaysCopyIntoAlloc=*/false,
102 rec.fDstCS,
103 rec.fAlloc);
104 SkShaders::MatrixRec matrix(SkMatrix::I());
105 matrix.markCTMApplied();
106 RuntimeEffectRPCallbacks callbacks(rec, matrix, fChildren, fEffect->fSampleUsages);
107 bool success = program->appendStages(rec.fPipeline, rec.fAlloc, &callbacks, uniforms);
108 return success;
109 }
110 return false;
111 }