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

Commit cb0f460a authored by Marzia Favaro's avatar Marzia Favaro Committed by Android (Google) Code Review
Browse files

Merge "Enforce shader compilation success, copy SkRuntimeShaderBuilder to avoid leaks" into main

parents 826e9f05 73fc95c1
Loading
Loading
Loading
Loading
+24 −15
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#include <SkRuntimeEffect.h>
#include <SkStream.h>
#include <SkString.h>
#include <com_android_graphics_libgui_flags.h>
#include "log/log_main.h"

namespace android::renderengine::skia {
@@ -56,25 +57,33 @@ static const SkString edgeShader = SkString(R"(
    }
)");

sk_sp<SkShader> EdgeExtensionShaderFactory::createSkShader(const sk_sp<SkShader>& inputShader,
                                                           const LayerSettings& layer,
                                                           const SkRect& imageBounds) {
    if (mBuilder == nullptr) {
        const static SkRuntimeEffect::Result instance = SkRuntimeEffect::MakeForShader(edgeShader);
        if (!instance.errorText.isEmpty()) {
            ALOGE("EdgeExtensionShaderFactory terminated with an error: %s",
                  instance.errorText.c_str());
            return nullptr;
EdgeExtensionShaderFactory::EdgeExtensionShaderFactory() {
    if (!com::android::graphics::libgui::flags::edge_extension_shader()) {
        return;
    }
        mBuilder = std::make_unique<SkRuntimeShaderBuilder>(instance.effect);
    mResult = std::make_unique<SkRuntimeEffect::Result>(SkRuntimeEffect::MakeForShader(edgeShader));
    LOG_ALWAYS_FATAL_IF(!mResult->errorText.isEmpty(),
                        "EdgeExtensionShaderFactory compilation "
                        "failed with an unexpected error: %s",
                        mResult->errorText.c_str());
}
    mBuilder->child("uContentTexture") = inputShader;

sk_sp<SkShader> EdgeExtensionShaderFactory::createSkShader(const sk_sp<SkShader>& inputShader,
                                                           const LayerSettings& layer,
                                                           const SkRect& imageBounds) const {
    LOG_ALWAYS_FATAL_IF(mResult == nullptr,
                        "EdgeExtensionShaderFactory did not initialize mResult. "
                        "This means that we unexpectedly applied the edge extension shader");

    SkRuntimeShaderBuilder builder = SkRuntimeShaderBuilder(mResult->effect);

    builder.child("uContentTexture") = inputShader;
    if (imageBounds.isEmpty()) {
        mBuilder->uniform("uImgSize") = SkPoint{layer.geometry.boundaries.getWidth(),
        builder.uniform("uImgSize") = SkPoint{layer.geometry.boundaries.getWidth(),
                                              layer.geometry.boundaries.getHeight()};
    } else {
        mBuilder->uniform("uImgSize") = SkPoint{imageBounds.width(), imageBounds.height()};
        builder.uniform("uImgSize") = SkPoint{imageBounds.width(), imageBounds.height()};
    }
    return mBuilder->makeShader();
    return builder.makeShader();
}
} // namespace android::renderengine::skia
 No newline at end of file
+4 −2
Original line number Diff line number Diff line
@@ -33,10 +33,12 @@ namespace android::renderengine::skia {
 */
class EdgeExtensionShaderFactory {
public:
    EdgeExtensionShaderFactory();

    sk_sp<SkShader> createSkShader(const sk_sp<SkShader>& inputShader, const LayerSettings& layer,
                                   const SkRect& imageBounds);
                                   const SkRect& imageBounds) const;

private:
    std::unique_ptr<SkRuntimeShaderBuilder> mBuilder;
    std::unique_ptr<const SkRuntimeEffect::Result> mResult;
};
} // namespace android::renderengine::skia