|
9 | 9 | #include "include/core/SkRefCnt.h" |
10 | 10 | #include "include/core/SkString.h" |
11 | 11 | #include "include/core/SkUnPreMultiply.h" |
| 12 | +#include "include/effects/SkRuntimeEffect.h" |
12 | 13 | #include "include/private/SkNx.h" |
13 | 14 | #include "include/private/SkTDArray.h" |
14 | 15 | #include "src/core/SkArenaAlloc.h" |
|
18 | 19 | #include "src/core/SkReadBuffer.h" |
19 | 20 | #include "src/core/SkVM.h" |
20 | 21 | #include "src/core/SkWriteBuffer.h" |
| 22 | +#include "src/sksl/SkSLInterpreter.h" |
21 | 23 |
|
22 | 24 | #if SK_SUPPORT_GPU |
23 | 25 | #include "src/gpu/GrFragmentProcessor.h" |
@@ -389,11 +391,135 @@ sk_sp<SkColorFilter> SkColorFilters::Lerp(float weight, sk_sp<SkColorFilter> cf0 |
389 | 391 |
|
390 | 392 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
391 | 393 |
|
| 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 | + |
392 | 517 | #include "src/core/SkModeColorFilter.h" |
393 | 518 |
|
394 | 519 | void SkColorFilter::RegisterFlattenables() { |
395 | 520 | SK_REGISTER_FLATTENABLE(SkComposeColorFilter); |
396 | 521 | SK_REGISTER_FLATTENABLE(SkModeColorFilter); |
397 | 522 | SK_REGISTER_FLATTENABLE(SkSRGBGammaColorFilter); |
398 | 523 | SK_REGISTER_FLATTENABLE(SkMixerColorFilter); |
| 524 | + SK_REGISTER_FLATTENABLE(SkRuntimeColorFilter); |
399 | 525 | } |
0 commit comments