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

Commit 4137a1d7 authored by Peiyong Lin's avatar Peiyong Lin Committed by Chong Zhang
Browse files

[RenderEngine] Refactor RenderEngine creation arguments.

Make RenderEngine creation arguments easier to extend. Allow all context
priority during creation.

BUG: 142331374
Test: build, flash and boot
Change-Id: I7100fdd8a1685a866b104654691d8d577f819fcf
parent 6edbd58d
Loading
Loading
Loading
Loading
+4 −5
Original line number Original line Diff line number Diff line
@@ -24,23 +24,22 @@
namespace android {
namespace android {
namespace renderengine {
namespace renderengine {


std::unique_ptr<impl::RenderEngine> RenderEngine::create(int hwcFormat, uint32_t featureFlags,
std::unique_ptr<impl::RenderEngine> RenderEngine::create(const RenderEngineCreationArgs& args) {
                                                         uint32_t imageCacheSize) {
    char prop[PROPERTY_VALUE_MAX];
    char prop[PROPERTY_VALUE_MAX];
    property_get(PROPERTY_DEBUG_RENDERENGINE_BACKEND, prop, "gles");
    property_get(PROPERTY_DEBUG_RENDERENGINE_BACKEND, prop, "gles");
    if (strcmp(prop, "gles") == 0) {
    if (strcmp(prop, "gles") == 0) {
        ALOGD("RenderEngine GLES Backend");
        ALOGD("RenderEngine GLES Backend");
        return renderengine::gl::GLESRenderEngine::create(hwcFormat, featureFlags, imageCacheSize);
        return renderengine::gl::GLESRenderEngine::create(args);
    }
    }
    ALOGE("UNKNOWN BackendType: %s, create GLES RenderEngine.", prop);
    ALOGE("UNKNOWN BackendType: %s, create GLES RenderEngine.", prop);
    return renderengine::gl::GLESRenderEngine::create(hwcFormat, featureFlags, imageCacheSize);
    return renderengine::gl::GLESRenderEngine::create(args);
}
}


RenderEngine::~RenderEngine() = default;
RenderEngine::~RenderEngine() = default;


namespace impl {
namespace impl {


RenderEngine::RenderEngine(uint32_t featureFlags) : mFeatureFlags(featureFlags) {}
RenderEngine::RenderEngine(const RenderEngineCreationArgs& args) : mArgs(args) {}


RenderEngine::~RenderEngine() = default;
RenderEngine::~RenderEngine() = default;


+20 −23
Original line number Original line Diff line number Diff line
@@ -227,8 +227,7 @@ static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint render
    return err;
    return err;
}
}


std::unique_ptr<GLESRenderEngine> GLESRenderEngine::create(int hwcFormat, uint32_t featureFlags,
std::unique_ptr<GLESRenderEngine> GLESRenderEngine::create(const RenderEngineCreationArgs& args) {
                                                           uint32_t imageCacheSize) {
    // initialize EGL for the default display
    // initialize EGL for the default display
    EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    if (!eglInitialize(display, nullptr, nullptr)) {
    if (!eglInitialize(display, nullptr, nullptr)) {
@@ -243,14 +242,13 @@ std::unique_ptr<GLESRenderEngine> GLESRenderEngine::create(int hwcFormat, uint32
    // supported.
    // supported.
    EGLConfig config = EGL_NO_CONFIG;
    EGLConfig config = EGL_NO_CONFIG;
    if (!extensions.hasNoConfigContext()) {
    if (!extensions.hasNoConfigContext()) {
        config = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
        config = chooseEglConfig(display, args.pixelFormat, /*logConfig*/ true);
    }
    }


    bool useContextPriority = extensions.hasContextPriority() &&
    bool useContextPriority =
            (featureFlags & RenderEngine::USE_HIGH_PRIORITY_CONTEXT);
            extensions.hasContextPriority() && args.contextPriority == ContextPriority::HIGH;
    EGLContext protectedContext = EGL_NO_CONTEXT;
    EGLContext protectedContext = EGL_NO_CONTEXT;
    if ((featureFlags & RenderEngine::ENABLE_PROTECTED_CONTEXT) &&
    if (args.enableProtectedContext && extensions.hasProtectedContent()) {
        extensions.hasProtectedContent()) {
        protectedContext = createEglContext(display, config, nullptr, useContextPriority,
        protectedContext = createEglContext(display, config, nullptr, useContextPriority,
                                            Protection::PROTECTED);
                                            Protection::PROTECTED);
        ALOGE_IF(protectedContext == EGL_NO_CONTEXT, "Can't create protected context");
        ALOGE_IF(protectedContext == EGL_NO_CONTEXT, "Can't create protected context");
@@ -264,7 +262,8 @@ std::unique_ptr<GLESRenderEngine> GLESRenderEngine::create(int hwcFormat, uint32


    EGLSurface dummy = EGL_NO_SURFACE;
    EGLSurface dummy = EGL_NO_SURFACE;
    if (!extensions.hasSurfacelessContext()) {
    if (!extensions.hasSurfacelessContext()) {
        dummy = createDummyEglPbufferSurface(display, config, hwcFormat, Protection::UNPROTECTED);
        dummy = createDummyEglPbufferSurface(display, config, args.pixelFormat,
                                             Protection::UNPROTECTED);
        LOG_ALWAYS_FATAL_IF(dummy == EGL_NO_SURFACE, "can't create dummy pbuffer");
        LOG_ALWAYS_FATAL_IF(dummy == EGL_NO_SURFACE, "can't create dummy pbuffer");
    }
    }
    EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt);
    EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt);
@@ -274,8 +273,8 @@ std::unique_ptr<GLESRenderEngine> GLESRenderEngine::create(int hwcFormat, uint32


    EGLSurface protectedDummy = EGL_NO_SURFACE;
    EGLSurface protectedDummy = EGL_NO_SURFACE;
    if (protectedContext != EGL_NO_CONTEXT && !extensions.hasSurfacelessContext()) {
    if (protectedContext != EGL_NO_CONTEXT && !extensions.hasSurfacelessContext()) {
        protectedDummy =
        protectedDummy = createDummyEglPbufferSurface(display, config, args.pixelFormat,
                createDummyEglPbufferSurface(display, config, hwcFormat, Protection::PROTECTED);
                                                      Protection::PROTECTED);
        ALOGE_IF(protectedDummy == EGL_NO_SURFACE, "can't create protected dummy pbuffer");
        ALOGE_IF(protectedDummy == EGL_NO_SURFACE, "can't create protected dummy pbuffer");
    }
    }


@@ -291,9 +290,8 @@ std::unique_ptr<GLESRenderEngine> GLESRenderEngine::create(int hwcFormat, uint32
            break;
            break;
        case GLES_VERSION_2_0:
        case GLES_VERSION_2_0:
        case GLES_VERSION_3_0:
        case GLES_VERSION_3_0:
            engine = std::make_unique<GLESRenderEngine>(featureFlags, display, config, ctxt, dummy,
            engine = std::make_unique<GLESRenderEngine>(args, display, config, ctxt, dummy,
                                                        protectedContext, protectedDummy,
                                                        protectedContext, protectedDummy);
                                                        imageCacheSize);
            break;
            break;
    }
    }


@@ -347,10 +345,10 @@ EGLConfig GLESRenderEngine::chooseEglConfig(EGLDisplay display, int format, bool
    return config;
    return config;
}
}


GLESRenderEngine::GLESRenderEngine(uint32_t featureFlags, EGLDisplay display, EGLConfig config,
GLESRenderEngine::GLESRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display,
                                   EGLContext ctxt, EGLSurface dummy, EGLContext protectedContext,
                                   EGLConfig config, EGLContext ctxt, EGLSurface dummy,
                                   EGLSurface protectedDummy, uint32_t imageCacheSize)
                                   EGLContext protectedContext, EGLSurface protectedDummy)
      : renderengine::impl::RenderEngine(featureFlags),
      : renderengine::impl::RenderEngine(args),
        mEGLDisplay(display),
        mEGLDisplay(display),
        mEGLConfig(config),
        mEGLConfig(config),
        mEGLContext(ctxt),
        mEGLContext(ctxt),
@@ -359,8 +357,8 @@ GLESRenderEngine::GLESRenderEngine(uint32_t featureFlags, EGLDisplay display, EG
        mProtectedDummySurface(protectedDummy),
        mProtectedDummySurface(protectedDummy),
        mVpWidth(0),
        mVpWidth(0),
        mVpHeight(0),
        mVpHeight(0),
        mFramebufferImageCacheSize(imageCacheSize),
        mFramebufferImageCacheSize(args.imageCacheSize),
        mUseColorManagement(featureFlags & USE_COLOR_MANAGEMENT) {
        mUseColorManagement(args.useColorManagement) {
    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
    glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
    glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);


@@ -457,10 +455,9 @@ Framebuffer* GLESRenderEngine::getFramebufferForDrawing() {
}
}


void GLESRenderEngine::primeCache() const {
void GLESRenderEngine::primeCache() const {
    ProgramCache::getInstance().primeCache(
    ProgramCache::getInstance().primeCache(mInProtectedContext ? mProtectedEGLContext : mEGLContext,
            mInProtectedContext ? mProtectedEGLContext : mEGLContext,
                                           mArgs.useColorManagement,
                    mFeatureFlags & USE_COLOR_MANAGEMENT,
                                           mArgs.precacheToneMapperShaderOnly);
                    mFeatureFlags & PRECACHE_TONE_MAPPER_SHADER_ONLY);
}
}


base::unique_fd GLESRenderEngine::flush() {
base::unique_fd GLESRenderEngine::flush() {
+4 −6
Original line number Original line Diff line number Diff line
@@ -49,13 +49,11 @@ class GLImage;


class GLESRenderEngine : public impl::RenderEngine {
class GLESRenderEngine : public impl::RenderEngine {
public:
public:
    static std::unique_ptr<GLESRenderEngine> create(int hwcFormat, uint32_t featureFlags,
    static std::unique_ptr<GLESRenderEngine> create(const RenderEngineCreationArgs& args);
                                                    uint32_t imageCacheSize);


    GLESRenderEngine(uint32_t featureFlags, // See RenderEngine::FeatureFlag
    GLESRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display, EGLConfig config,
                     EGLDisplay display, EGLConfig config, EGLContext ctxt, EGLSurface dummy,
                     EGLContext ctxt, EGLSurface dummy, EGLContext protectedContext,
                     EGLContext protectedContext, EGLSurface protectedDummy,
                     EGLSurface protectedDummy);
                     uint32_t imageCacheSize);
    ~GLESRenderEngine() override EXCLUDES(mRenderingMutex);
    ~GLESRenderEngine() override EXCLUDES(mRenderingMutex);


    void primeCache() const override;
    void primeCache() const override;
+78 −13
Original line number Original line Diff line number Diff line
@@ -48,6 +48,7 @@ class BindNativeBufferAsFramebuffer;
class Image;
class Image;
class Mesh;
class Mesh;
class Texture;
class Texture;
struct RenderEngineCreationArgs;


namespace impl {
namespace impl {
class RenderEngine;
class RenderEngine;
@@ -60,19 +61,13 @@ enum class Protection {


class RenderEngine {
class RenderEngine {
public:
public:
    enum FeatureFlag {
    enum class ContextPriority {
        USE_COLOR_MANAGEMENT = 1 << 0,      // Device manages color
        LOW = 1,
        USE_HIGH_PRIORITY_CONTEXT = 1 << 1, // Use high priority context
        MEDIUM = 2,

        HIGH = 3,
        // Create a protected context when if possible
        ENABLE_PROTECTED_CONTEXT = 1 << 2,

        // Only precache HDR to SDR tone-mapping shaders
        PRECACHE_TONE_MAPPER_SHADER_ONLY = 1 << 3,
    };
    };


    static std::unique_ptr<impl::RenderEngine> create(int hwcFormat, uint32_t featureFlags,
    static std::unique_ptr<impl::RenderEngine> create(const RenderEngineCreationArgs& args);
                                                      uint32_t imageCacheSize);


    virtual ~RenderEngine() = 0;
    virtual ~RenderEngine() = 0;


@@ -173,6 +168,76 @@ protected:
    friend class BindNativeBufferAsFramebuffer;
    friend class BindNativeBufferAsFramebuffer;
};
};


struct RenderEngineCreationArgs {
    int pixelFormat;
    uint32_t imageCacheSize;
    bool useColorManagement;
    bool enableProtectedContext;
    bool precacheToneMapperShaderOnly;
    RenderEngine::ContextPriority contextPriority;

    struct Builder;

private:
    // must be created by Builder via constructor with full argument list
    RenderEngineCreationArgs(
            int _pixelFormat,
            uint32_t _imageCacheSize,
            bool _useColorManagement,
            bool _enableProtectedContext,
            bool _precacheToneMapperShaderOnly,
            RenderEngine::ContextPriority _contextPriority)
        : pixelFormat(_pixelFormat)
        , imageCacheSize(_imageCacheSize)
        , useColorManagement(_useColorManagement)
        , enableProtectedContext(_enableProtectedContext)
        , precacheToneMapperShaderOnly(_precacheToneMapperShaderOnly)
        , contextPriority(_contextPriority) {}
    RenderEngineCreationArgs() = delete;
};

struct RenderEngineCreationArgs::Builder {
    Builder() {}

    Builder& setPixelFormat(int pixelFormat) {
        this->pixelFormat = pixelFormat;
        return *this;
    }
    Builder& setImageCacheSize(uint32_t imageCacheSize) {
        this->imageCacheSize = imageCacheSize;
        return *this;
    }
    Builder& setUseColorManagerment(bool useColorManagement) {
        this->useColorManagement = useColorManagement;
        return *this;
    }
    Builder& setEnableProtectedContext(bool enableProtectedContext) {
        this->enableProtectedContext = enableProtectedContext;
        return *this;
    }
    Builder& setPrecacheToneMapperShaderOnly(bool precacheToneMapperShaderOnly) {
        this->precacheToneMapperShaderOnly = precacheToneMapperShaderOnly;
        return *this;
    }
    Builder& setContextPriority(RenderEngine::ContextPriority contextPriority) {
        this->contextPriority = contextPriority;
        return *this;
    }
    RenderEngineCreationArgs build() const {
        return RenderEngineCreationArgs(pixelFormat, imageCacheSize, useColorManagement,
                enableProtectedContext, precacheToneMapperShaderOnly, contextPriority);
    }

private:
    // 1 means RGBA_8888
    int pixelFormat = 1;
    uint32_t imageCacheSize = 0;
    bool useColorManagement = true;
    bool enableProtectedContext = false;
    bool precacheToneMapperShaderOnly = false;
    RenderEngine::ContextPriority contextPriority = RenderEngine::ContextPriority::MEDIUM;
};

class BindNativeBufferAsFramebuffer {
class BindNativeBufferAsFramebuffer {
public:
public:
    BindNativeBufferAsFramebuffer(RenderEngine& engine, ANativeWindowBuffer* buffer,
    BindNativeBufferAsFramebuffer(RenderEngine& engine, ANativeWindowBuffer* buffer,
@@ -206,8 +271,8 @@ public:
    bool useWaitSync() const override;
    bool useWaitSync() const override;


protected:
protected:
    RenderEngine(uint32_t featureFlags);
    RenderEngine(const RenderEngineCreationArgs& args);
    const uint32_t mFeatureFlags;
    const RenderEngineCreationArgs mArgs;
};
};


} // namespace impl
} // namespace impl
+9 −3
Original line number Original line Diff line number Diff line
@@ -31,9 +31,15 @@ namespace android {


struct RenderEngineTest : public ::testing::Test {
struct RenderEngineTest : public ::testing::Test {
    static void SetUpTestSuite() {
    static void SetUpTestSuite() {
        sRE = renderengine::gl::GLESRenderEngine::create(static_cast<int32_t>(
        sRE = renderengine::gl::GLESRenderEngine::create(
                                                                 ui::PixelFormat::RGBA_8888),
                renderengine::RenderEngineCreationArgs::Builder()
                                                         0, 1);
                    .setPixelFormat(static_cast<int>(ui::PixelFormat::RGBA_8888))
                    .setImageCacheSize(1)
                    .setUseColorManagerment(false)
                    .setEnableProtectedContext(false)
                    .setPrecacheToneMapperShaderOnly(false)
                    .setContextPriority(renderengine::RenderEngine::ContextPriority::MEDIUM)
        .build());
    }
    }


    static void TearDownTestSuite() {
    static void TearDownTestSuite() {
Loading