Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ce3ed9d0 authored by Noelle Scobie's avatar Noelle Scobie
Browse files

Centralize all SkRuntimeEffect creation and make them easily accessible

This is in preparation for the SkRuntimeEffects to be referenced during
precompilation.

Bug: b/380159947
Test: core CUJs still work
Flag: EXEMPT refactor
Change-Id: I00755d5a297b7f7200d1439dad0211ddd96eb0f0
parent c29c8e1a
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -102,15 +102,15 @@ filegroup {
        "skia/debug/SkiaCapture.cpp",
        "skia/debug/SkiaMemoryReporter.cpp",
        "skia/filters/BlurFilter.cpp",
        "skia/filters/EdgeExtensionShaderFactory.cpp",
        "skia/filters/GainmapFactory.cpp",
        "skia/filters/GaussianBlurFilter.cpp",
        "skia/filters/KawaseBlurDualFilter.cpp",
        "skia/filters/KawaseBlurFilter.cpp",
        "skia/filters/LinearEffect.cpp",
        "skia/filters/LutShader.cpp",
        "skia/filters/MouriMap.cpp",
        "skia/filters/RuntimeEffectManager.cpp",
        "skia/filters/StretchShaderFactory.cpp",
        "skia/filters/EdgeExtensionShaderFactory.cpp",
    ],
}

+0 −1
Original line number Diff line number Diff line
@@ -38,7 +38,6 @@
#include "android-base/macros.h"
#include "debug/SkiaCapture.h"
#include "filters/BlurFilter.h"
#include "filters/LinearEffect.h"
#include "filters/StretchShaderFactory.h"

class SkData;
+21 −34
Original line number Diff line number Diff line
@@ -81,8 +81,9 @@
#include "filters/GaussianBlurFilter.h"
#include "filters/KawaseBlurDualFilter.h"
#include "filters/KawaseBlurFilter.h"
#include "filters/LinearEffect.h"
#include "filters/LutShader.h"
#include "filters/MouriMap.h"
#include "filters/RuntimeEffectManager.h"
#include "log/log_main.h"
#include "skia/compat/SkiaBackendTexture.h"
#include "skia/debug/SkiaCapture.h"
@@ -292,17 +293,17 @@ SkiaRenderEngine::SkiaRenderEngine(Threaded threaded, PixelFormat pixelFormat,
    switch (blurAlgorithm) {
        case BlurAlgorithm::GAUSSIAN: {
            ALOGD("Background Blurs Enabled (Gaussian algorithm)");
            mBlurFilter = new GaussianBlurFilter();
            mBlurFilter = new GaussianBlurFilter(mRuntimeEffectManager);
            break;
        }
        case BlurAlgorithm::KAWASE: {
            ALOGD("Background Blurs Enabled (Kawase algorithm)");
            mBlurFilter = new KawaseBlurFilter();
            mBlurFilter = new KawaseBlurFilter(mRuntimeEffectManager);
            break;
        }
        case BlurAlgorithm::KAWASE_DUAL_FILTER: {
            ALOGD("Background Blurs Enabled (Kawase dual-filtering algorithm)");
            mBlurFilter = new KawaseBlurDualFilter();
            mBlurFilter = new KawaseBlurDualFilter(mRuntimeEffectManager);
            break;
        }
        default: {
@@ -525,6 +526,7 @@ void SkiaRenderEngine::cleanupPostRender() {

sk_sp<SkShader> SkiaRenderEngine::createRuntimeEffectShader(
        const RuntimeEffectShaderParameters& parameters) {
    SFTRACE_CALL();
    // The given surface will be stretched by HWUI via matrix transformation
    // which gets similar results for most surfaces
    // Determine later on if we need to leverage the stretch shader within
@@ -585,14 +587,8 @@ sk_sp<SkShader> SkiaRenderEngine::createRuntimeEffectShader(
                                      .undoPremultipliedAlpha = parameters.undoPremultipliedAlpha,
                                      .fakeOutputDataspace = parameters.fakeOutputDataspace};

        auto effectIter = mRuntimeEffects.find(effect);
        sk_sp<SkRuntimeEffect> runtimeEffect = nullptr;
        if (effectIter == mRuntimeEffects.end()) {
            runtimeEffect = buildRuntimeEffect(effect);
            mRuntimeEffects.insert({effect, runtimeEffect});
        } else {
            runtimeEffect = effectIter->second;
        }
        sk_sp<SkRuntimeEffect> runtimeEffect =
                mRuntimeEffectManager.getOrCreateLinearRuntimeEffect(effect);

        mat4 colorTransform = parameters.layer.colorTransform;

@@ -603,19 +599,23 @@ sk_sp<SkShader> SkiaRenderEngine::createRuntimeEffectShader(
        }

        const auto hardwareBuffer = graphicBuffer ? graphicBuffer->toAHardwareBuffer() : nullptr;
        return createLinearEffectShader(shader, effect, runtimeEffect, std::move(colorTransform),
        return RuntimeEffectManager::createLinearEffectShader(shader, effect, runtimeEffect,
                                                              std::move(colorTransform),
                                                              parameters.display.maxLuminance,
                                        parameters.display.currentLuminanceNits,
                                        parameters.layer.source.buffer.maxLuminanceNits,
                                        hardwareBuffer, parameters.display.renderIntent);
                                                              parameters.display
                                                                      .currentLuminanceNits,
                                                              parameters.layer.source.buffer
                                                                      .maxLuminanceNits,
                                                              hardwareBuffer,
                                                              parameters.display.renderIntent);
    }
    return shader;
}

sk_sp<SkShader> SkiaRenderEngine::localTonemap(sk_sp<SkShader> shader, float inputMultiplier,
                                               float targetHdrSdrRatio) {
    static MouriMap kMapper;
    return kMapper.mouriMap(getActiveContext(), shader, inputMultiplier, targetHdrSdrRatio);
    return mLocalTonemapper.mouriMap(getActiveContext(), shader, inputMultiplier,
                                     targetHdrSdrRatio);
}

void SkiaRenderEngine::initCanvas(SkCanvas* canvas, const DisplaySettings& display) {
@@ -1353,9 +1353,8 @@ void SkiaRenderEngine::tonemapAndDrawGainmapInternal(

    const auto tonemappedShader = localTonemap(hdrShader, 1.0f, 1.0f);

    static GainmapFactory kGainmapFactory;
    const auto gainmapShader =
            kGainmapFactory.createSkShader(tonemappedShader, hdrShader, hdrSdrRatio);
            mGainmapFactory.createSkShader(tonemappedShader, hdrShader, hdrSdrRatio);

    sp<Fence> drawFence;

@@ -1501,19 +1500,7 @@ void SkiaRenderEngine::dump(std::string& result) {
        gpuProtectedReporter.logOutput(result, true);

        StringAppendF(&result, "\n");
        StringAppendF(&result, "RenderEngine runtime effects: %zu\n", mRuntimeEffects.size());
        for (const auto& [linearEffect, unused] : mRuntimeEffects) {
            StringAppendF(&result, "- inputDataspace: %s\n",
                          dataspaceDetails(
                                  static_cast<android_dataspace>(linearEffect.inputDataspace))
                                  .c_str());
            StringAppendF(&result, "- outputDataspace: %s\n",
                          dataspaceDetails(
                                  static_cast<android_dataspace>(linearEffect.outputDataspace))
                                  .c_str());
            StringAppendF(&result, "undoPremultipliedAlpha: %s\n",
                          linearEffect.undoPremultipliedAlpha ? "true" : "false");
        }
        mRuntimeEffectManager.dump(result);
    }
    StringAppendF(&result, "\n");
}
+12 −6
Original line number Diff line number Diff line
@@ -38,8 +38,10 @@
#include "debug/SkiaCapture.h"
#include "filters/BlurFilter.h"
#include "filters/EdgeExtensionShaderFactory.h"
#include "filters/LinearEffect.h"
#include "filters/GainmapFactory.h"
#include "filters/LutShader.h"
#include "filters/MouriMap.h"
#include "filters/RuntimeEffectManager.h"
#include "filters/StretchShaderFactory.h"

class SkData;
@@ -127,6 +129,7 @@ protected:
    };

    SkSLCacheMonitor mSkSLCacheMonitor;
    RuntimeEffectManager mRuntimeEffectManager;

private:
    void mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer,
@@ -180,13 +183,16 @@ private:
            GUARDED_BY(mRenderingMutex);
    std::unordered_map<GraphicBufferId, std::shared_ptr<AutoBackendTexture::LocalRef>> mTextureCache
            GUARDED_BY(mRenderingMutex);
    std::unordered_map<shaders::LinearEffect, sk_sp<SkRuntimeEffect>, shaders::LinearEffectHasher>
            mRuntimeEffects;
    AutoBackendTexture::CleanupManager mTextureCleanupMgr GUARDED_BY(mRenderingMutex);

    StretchShaderFactory mStretchShaderFactory;
    EdgeExtensionShaderFactory mEdgeExtensionShaderFactory;
    LutShader mLutShader;
    // Alphabetical by type name
    // TODO(b/380159947): move these into RuntimeEffectManager
    EdgeExtensionShaderFactory mEdgeExtensionShaderFactory =
            EdgeExtensionShaderFactory(mRuntimeEffectManager);
    GainmapFactory mGainmapFactory = GainmapFactory(mRuntimeEffectManager);
    LutShader mLutShader = LutShader(mRuntimeEffectManager);
    MouriMap mLocalTonemapper = MouriMap(mRuntimeEffectManager);
    StretchShaderFactory mStretchShaderFactory = StretchShaderFactory(mRuntimeEffectManager);

    sp<Fence> mLastDrawFence;
    BlurFilter* mBlurFilter = nullptr;
+17 −17
Original line number Diff line number Diff line
@@ -28,12 +28,13 @@
#include <common/trace.h>
#include <log/log.h>

#include "RuntimeEffectManager.h"

namespace android {
namespace renderengine {
namespace skia {

static sk_sp<SkRuntimeEffect> createMixEffect() {
    SkString mixString(R"(
static const SkString kMixString(R"(
    uniform shader blurredInput;
    uniform shader originalInput;
    uniform float mixFactor;
@@ -43,13 +44,6 @@ static sk_sp<SkRuntimeEffect> createMixEffect() {
    }
)");

    auto [mixEffect, mixError] = SkRuntimeEffect::MakeForShader(mixString);
    if (!mixEffect) {
        LOG_ALWAYS_FATAL("RuntimeShader error: %s", mixError.c_str());
    }
    return mixEffect;
}

static SkMatrix getShaderTransform(const SkCanvas* canvas, const SkRect& blurRect,
                                   const float scale, const float zoomScale) {
    // 1. Apply the blur shader matrix, which scales up the blurred surface to its real size
@@ -74,9 +68,15 @@ static SkMatrix getShaderTransform(const SkCanvas* canvas, const SkRect& blurRec
    return matrix;
}

BlurFilter::BlurFilter(const float maxCrossFadeRadius)
BlurFilter::BlurFilter(RuntimeEffectManager& effectManager, const float maxCrossFadeRadius)
      : mMaxCrossFadeRadius(maxCrossFadeRadius),
        mMixEffect(maxCrossFadeRadius > 0 ? createMixEffect() : nullptr) {}
        mMixEffect(
                maxCrossFadeRadius > 0
                        ? effectManager.createAndStoreRuntimeEffect(RuntimeEffectManager::KnownId::
                                                                            kBlurFilter_MixEffect,
                                                                    "BlurFilter_MixEffect",
                                                                    kMixString)
                        : nullptr) {}

float BlurFilter::getMaxCrossFadeRadius() const {
    return mMaxCrossFadeRadius;
Loading