Skip to content

Commit 7281a86

Browse files
brianosmanSkia Commit-Bot
authored andcommitted
Revert "Move runtime shader/colorfilter into SkRuntimeEffect.cpp"
This reverts commit 8980acd. Reason for revert: Win-Shared Original change's description: > Move runtime shader/colorfilter into SkRuntimeEffect.cpp > > Better organization that lets us share a bunch of code between these > (very similar) objects. > > Change-Id: Ie559d6e144d8588b98a95d4170e2e6c19d9623bd > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/270736 > Reviewed-by: Mike Reed <[email protected]> > Commit-Queue: Brian Osman <[email protected]> [email protected],[email protected],[email protected] Change-Id: Ic13d85b7c4f2d593a6c15dde067f118ea5753eb6 No-Presubmit: true No-Tree-Checks: true No-Try: true Reviewed-on: https://skia-review.googlesource.com/c/skia/+/271600 Reviewed-by: Brian Osman <[email protected]> Commit-Queue: Brian Osman <[email protected]>
1 parent 8980acd commit 7281a86

7 files changed

Lines changed: 352 additions & 278 deletions

File tree

gn/core.gni

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ skia_core_sources = [
445445
"$_src/shaders/SkLights.h",
446446
"$_src/shaders/SkLocalMatrixShader.cpp",
447447
"$_src/shaders/SkLocalMatrixShader.h",
448+
"$_src/shaders/SkRTShader.cpp",
448449
"$_src/shaders/SkShader.cpp",
449450
"$_src/shaders/SkShaderBase.h",
450451

include/effects/SkRuntimeEffect.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,6 @@ class SK_API SkRuntimeEffect : public SkRefCnt {
127127

128128
ByteCodeResult toByteCode(const void* inputs);
129129

130-
static void RegisterFlattenables();
131-
132130
private:
133131
SkRuntimeEffect(SkString sksl, std::unique_ptr<SkSL::Compiler> compiler,
134132
std::unique_ptr<SkSL::Program> baseProgram,

src/core/SkColorFilter.cpp

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "include/core/SkRefCnt.h"
1010
#include "include/core/SkString.h"
1111
#include "include/core/SkUnPreMultiply.h"
12+
#include "include/effects/SkRuntimeEffect.h"
1213
#include "include/private/SkNx.h"
1314
#include "include/private/SkTDArray.h"
1415
#include "src/core/SkArenaAlloc.h"
@@ -18,6 +19,7 @@
1819
#include "src/core/SkReadBuffer.h"
1920
#include "src/core/SkVM.h"
2021
#include "src/core/SkWriteBuffer.h"
22+
#include "src/sksl/SkSLInterpreter.h"
2123

2224
#if SK_SUPPORT_GPU
2325
#include "src/gpu/GrFragmentProcessor.h"
@@ -389,11 +391,135 @@ sk_sp<SkColorFilter> SkColorFilters::Lerp(float weight, sk_sp<SkColorFilter> cf0
389391

390392
///////////////////////////////////////////////////////////////////////////////////////////////////
391393

394+
#include "include/private/SkMutex.h"
395+
#include "src/sksl/SkSLByteCode.h"
396+
397+
#if SK_SUPPORT_GPU
398+
#include "include/private/GrRecordingContext.h"
399+
#include "src/gpu/effects/GrSkSLFP.h"
400+
#endif
401+
402+
class SkRuntimeColorFilter : public SkColorFilter {
403+
public:
404+
SkRuntimeColorFilter(sk_sp<SkRuntimeEffect> effect, sk_sp<SkData> inputs,
405+
sk_sp<SkColorFilter> children[], size_t childCount)
406+
: fEffect(std::move(effect))
407+
, fInputs(std::move(inputs))
408+
, fChildren(children, children + childCount) {}
409+
410+
#if SK_SUPPORT_GPU
411+
std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(
412+
GrRecordingContext* context, const GrColorInfo& colorInfo) const override {
413+
auto fp = GrSkSLFP::Make(context, fEffect, "Runtime Color Filter", fInputs);
414+
for (const auto& child : fChildren) {
415+
auto childFP = child ? child->asFragmentProcessor(context, colorInfo) : nullptr;
416+
if (!childFP) {
417+
// TODO: This is the case that should eventually mean "the original input color"
418+
return nullptr;
419+
}
420+
fp->addChild(std::move(childFP));
421+
}
422+
return fp;
423+
}
424+
#endif
425+
426+
bool onAppendStages(const SkStageRec& rec, bool shaderIsOpaque) const override {
427+
auto ctx = rec.fAlloc->make<SkRasterPipeline_InterpreterCtx>();
428+
// don't need to set ctx->paintColor
429+
ctx->inputs = fInputs->data();
430+
ctx->ninputs = fEffect->uniformSize() / 4;
431+
ctx->shaderConvention = false;
432+
433+
SkAutoMutexExclusive ama(fInterpreterMutex);
434+
if (!fInterpreter) {
435+
auto [byteCode, errorText] = fEffect->toByteCode(fInputs->data());
436+
if (!byteCode) {
437+
SkDebugf("%s\n", errorText.c_str());
438+
return false;
439+
}
440+
fMain = byteCode->getFunction("main");
441+
fInterpreter.reset(
442+
new SkSL::Interpreter<SkRasterPipeline_InterpreterCtx::VECTOR_WIDTH>(
443+
std::move(byteCode)));
444+
}
445+
ctx->fn = fMain;
446+
ctx->interpreter = fInterpreter.get();
447+
rec.fPipeline->append(SkRasterPipeline::interpreter, ctx);
448+
return true;
449+
}
450+
451+
protected:
452+
void flatten(SkWriteBuffer& buffer) const override {
453+
buffer.writeString(fEffect->source().c_str());
454+
if (fInputs) {
455+
buffer.writeDataAsByteArray(fInputs.get());
456+
} else {
457+
buffer.writeByteArray(nullptr, 0);
458+
}
459+
buffer.write32(fChildren.size());
460+
for (const auto& child : fChildren) {
461+
buffer.writeFlattenable(child.get());
462+
}
463+
}
464+
465+
private:
466+
SK_FLATTENABLE_HOOKS(SkRuntimeColorFilter)
467+
468+
sk_sp<SkRuntimeEffect> fEffect;
469+
sk_sp<SkData> fInputs;
470+
std::vector<sk_sp<SkColorFilter>> fChildren;
471+
472+
mutable SkMutex fInterpreterMutex;
473+
mutable std::unique_ptr<SkSL::Interpreter<SkRasterPipeline_InterpreterCtx::VECTOR_WIDTH>>
474+
fInterpreter;
475+
mutable const SkSL::ByteCodeFunction* fMain;
476+
477+
friend class SkColorFilter;
478+
479+
typedef SkColorFilter INHERITED;
480+
};
481+
482+
sk_sp<SkFlattenable> SkRuntimeColorFilter::CreateProc(SkReadBuffer& buffer) {
483+
SkString sksl;
484+
buffer.readString(&sksl);
485+
sk_sp<SkData> inputs = buffer.readByteArrayAsData();
486+
487+
auto effect = std::get<0>(SkRuntimeEffect::Make(std::move(sksl)));
488+
if (!effect) {
489+
buffer.validate(false);
490+
return nullptr;
491+
}
492+
493+
size_t childCount = buffer.read32();
494+
if (childCount != effect->children().count()) {
495+
buffer.validate(false);
496+
return nullptr;
497+
}
498+
499+
std::vector<sk_sp<SkColorFilter>> children;
500+
children.resize(childCount);
501+
for (size_t i = 0; i < children.size(); ++i) {
502+
children[i] = buffer.readColorFilter();
503+
}
504+
505+
return effect->makeColorFilter(std::move(inputs), children.data(), children.size());
506+
}
507+
508+
// Private helper method so SkRuntimeEffect can access SkRuntimeColorFilter
509+
sk_sp<SkColorFilter> SkMakeRuntimeColorFilter(sk_sp<SkRuntimeEffect> effect, sk_sp<SkData> inputs,
510+
sk_sp<SkColorFilter> children[], size_t childCount) {
511+
return sk_sp<SkColorFilter>(
512+
new SkRuntimeColorFilter(std::move(effect), std::move(inputs), children, childCount));
513+
}
514+
515+
///////////////////////////////////////////////////////////////////////////////////////////////////
516+
392517
#include "src/core/SkModeColorFilter.h"
393518

394519
void SkColorFilter::RegisterFlattenables() {
395520
SK_REGISTER_FLATTENABLE(SkComposeColorFilter);
396521
SK_REGISTER_FLATTENABLE(SkModeColorFilter);
397522
SK_REGISTER_FLATTENABLE(SkSRGBGammaColorFilter);
398523
SK_REGISTER_FLATTENABLE(SkMixerColorFilter);
524+
SK_REGISTER_FLATTENABLE(SkRuntimeColorFilter);
399525
}

0 commit comments

Comments
 (0)