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

Commit 10f53929 authored by Nolan Scobie's avatar Nolan Scobie Committed by Android (Google) Code Review
Browse files

Merge changes I19b02580,I7d4b0b95 into main

* changes:
  Add and plumb abstraction layer over GrDirectContext
  Make AutoBackendTexture remember which context it was created with
parents 70c5c4b0 fc125ece
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -88,6 +88,7 @@ filegroup {
        "skia/SkiaGLRenderEngine.cpp",
        "skia/SkiaGLRenderEngine.cpp",
        "skia/SkiaVkRenderEngine.cpp",
        "skia/SkiaVkRenderEngine.cpp",
        "skia/VulkanInterface.cpp",
        "skia/VulkanInterface.cpp",
        "skia/compat/GaneshGpuContext.cpp",
        "skia/debug/CaptureTimer.cpp",
        "skia/debug/CaptureTimer.cpp",
        "skia/debug/CommonPool.cpp",
        "skia/debug/CommonPool.cpp",
        "skia/debug/SkiaCapture.cpp",
        "skia/debug/SkiaCapture.cpp",
+24 −36
Original line number Original line Diff line number Diff line
@@ -21,12 +21,12 @@
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
#define ATRACE_TAG ATRACE_TAG_GRAPHICS


#include <SkImage.h>
#include <SkImage.h>
#include <android/hardware_buffer.h>
#include <include/gpu/ganesh/SkImageGanesh.h>
#include <include/gpu/ganesh/SkImageGanesh.h>
#include <include/gpu/ganesh/SkSurfaceGanesh.h>
#include <include/gpu/ganesh/SkSurfaceGanesh.h>
#include <include/gpu/ganesh/gl/GrGLBackendSurface.h>
#include <include/gpu/ganesh/gl/GrGLBackendSurface.h>
#include <include/gpu/ganesh/vk/GrVkBackendSurface.h>
#include <include/gpu/ganesh/vk/GrVkBackendSurface.h>
#include <include/gpu/vk/GrVkTypes.h>
#include <include/gpu/vk/GrVkTypes.h>
#include <android/hardware_buffer.h>
#include "ColorSpaces.h"
#include "ColorSpaces.h"
#include "log/log_main.h"
#include "log/log_main.h"
#include "utils/Trace.h"
#include "utils/Trace.h"
@@ -35,47 +35,37 @@ namespace android {
namespace renderengine {
namespace renderengine {
namespace skia {
namespace skia {


AutoBackendTexture::AutoBackendTexture(GrDirectContext* context, AHardwareBuffer* buffer,
AutoBackendTexture::AutoBackendTexture(SkiaGpuContext* context, AHardwareBuffer* buffer,
                                       bool isOutputBuffer, CleanupManager& cleanupMgr)
                                       bool isOutputBuffer, CleanupManager& cleanupMgr)
      : mCleanupMgr(cleanupMgr), mIsOutputBuffer(isOutputBuffer) {
      : mGrContext(context->grDirectContext()),
        mCleanupMgr(cleanupMgr),
        mIsOutputBuffer(isOutputBuffer) {
    ATRACE_CALL();
    ATRACE_CALL();

    AHardwareBuffer_Desc desc;
    AHardwareBuffer_Desc desc;
    AHardwareBuffer_describe(buffer, &desc);
    AHardwareBuffer_describe(buffer, &desc);
    bool createProtectedImage = 0 != (desc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT);
    bool createProtectedImage = 0 != (desc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT);
    GrBackendFormat backendFormat;
    GrBackendFormat backendFormat;


    GrBackendApi backend = context->backend();
    GrBackendApi backend = mGrContext->backend();
    if (backend == GrBackendApi::kOpenGL) {
    if (backend == GrBackendApi::kOpenGL) {
        backendFormat =
        backendFormat =
                GrAHardwareBufferUtils::GetGLBackendFormat(context, desc.format, false);
                GrAHardwareBufferUtils::GetGLBackendFormat(mGrContext.get(), desc.format, false);
        mBackendTexture =
        mBackendTexture =
                GrAHardwareBufferUtils::MakeGLBackendTexture(context,
                GrAHardwareBufferUtils::MakeGLBackendTexture(mGrContext.get(), buffer, desc.width,
                                                             buffer,
                                                             desc.height, &mDeleteProc,
                                                             desc.width,
                                                             &mUpdateProc, &mImageCtx,
                                                             desc.height,
                                                             createProtectedImage, backendFormat,
                                                             &mDeleteProc,
                                                             &mUpdateProc,
                                                             &mImageCtx,
                                                             createProtectedImage,
                                                             backendFormat,
                                                             isOutputBuffer);
                                                             isOutputBuffer);
    } else if (backend == GrBackendApi::kVulkan) {
    } else if (backend == GrBackendApi::kVulkan) {
        backendFormat =
        backendFormat = GrAHardwareBufferUtils::GetVulkanBackendFormat(mGrContext.get(), buffer,
                GrAHardwareBufferUtils::GetVulkanBackendFormat(context,
                                                                       desc.format, false);
                                                               buffer,
                                                               desc.format,
                                                               false);
        mBackendTexture =
        mBackendTexture =
                GrAHardwareBufferUtils::MakeVulkanBackendTexture(context,
                GrAHardwareBufferUtils::MakeVulkanBackendTexture(mGrContext.get(), buffer,
                                                                 buffer,
                                                                 desc.width, desc.height,
                                                                 desc.width,
                                                                 &mDeleteProc, &mUpdateProc,
                                                                 desc.height,
                                                                 &mImageCtx, createProtectedImage,
                                                                 &mDeleteProc,
                                                                 backendFormat, isOutputBuffer);
                                                                 &mUpdateProc,
                                                                 &mImageCtx,
                                                                 createProtectedImage,
                                                                 backendFormat,
                                                                 isOutputBuffer);
    } else {
    } else {
        LOG_ALWAYS_FATAL("Unexpected backend %u", static_cast<unsigned>(backend));
        LOG_ALWAYS_FATAL("Unexpected backend %u", static_cast<unsigned>(backend));
    }
    }
@@ -158,12 +148,11 @@ void logFatalTexture(const char* msg, const GrBackendTexture& tex, ui::Dataspace
    }
    }
}
}


sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaType alphaType,
sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaType alphaType) {
                                             GrDirectContext* context) {
    ATRACE_CALL();
    ATRACE_CALL();


    if (mBackendTexture.isValid()) {
    if (mBackendTexture.isValid()) {
        mUpdateProc(mImageCtx, context);
        mUpdateProc(mImageCtx, mGrContext.get());
    }
    }


    auto colorType = mColorType;
    auto colorType = mColorType;
@@ -174,7 +163,7 @@ sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaTyp
    }
    }


    sk_sp<SkImage> image =
    sk_sp<SkImage> image =
            SkImages::BorrowTextureFrom(context, mBackendTexture, kTopLeft_GrSurfaceOrigin,
            SkImages::BorrowTextureFrom(mGrContext.get(), mBackendTexture, kTopLeft_GrSurfaceOrigin,
                                        colorType, alphaType, toSkColorSpace(dataspace),
                                        colorType, alphaType, toSkColorSpace(dataspace),
                                        releaseImageProc, this);
                                        releaseImageProc, this);
    if (image.get()) {
    if (image.get()) {
@@ -190,13 +179,12 @@ sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaTyp
    return mImage;
    return mImage;
}
}


sk_sp<SkSurface> AutoBackendTexture::getOrCreateSurface(ui::Dataspace dataspace,
sk_sp<SkSurface> AutoBackendTexture::getOrCreateSurface(ui::Dataspace dataspace) {
                                                        GrDirectContext* context) {
    ATRACE_CALL();
    ATRACE_CALL();
    LOG_ALWAYS_FATAL_IF(!mIsOutputBuffer, "You can't generate a SkSurface for a read-only texture");
    LOG_ALWAYS_FATAL_IF(!mIsOutputBuffer, "You can't generate a SkSurface for a read-only texture");
    if (!mSurface.get() || mDataspace != dataspace) {
    if (!mSurface.get() || mDataspace != dataspace) {
        sk_sp<SkSurface> surface =
        sk_sp<SkSurface> surface =
                SkSurfaces::WrapBackendTexture(context, mBackendTexture,
                SkSurfaces::WrapBackendTexture(mGrContext.get(), mBackendTexture,
                                               kTopLeft_GrSurfaceOrigin, 0, mColorType,
                                               kTopLeft_GrSurfaceOrigin, 0, mColorType,
                                               toSkColorSpace(dataspace), nullptr,
                                               toSkColorSpace(dataspace), nullptr,
                                               releaseSurfaceProc, this);
                                               releaseSurfaceProc, this);
+13 −11
Original line number Original line Diff line number Diff line
@@ -24,6 +24,7 @@
#include <ui/GraphicTypes.h>
#include <ui/GraphicTypes.h>


#include "android-base/macros.h"
#include "android-base/macros.h"
#include "compat/SkiaGpuContext.h"


#include <mutex>
#include <mutex>
#include <vector>
#include <vector>
@@ -80,7 +81,7 @@ public:
    // of shared ownership with Skia objects, so we wrap it here instead.
    // of shared ownership with Skia objects, so we wrap it here instead.
    class LocalRef {
    class LocalRef {
    public:
    public:
        LocalRef(GrDirectContext* context, AHardwareBuffer* buffer, bool isOutputBuffer,
        LocalRef(SkiaGpuContext* context, AHardwareBuffer* buffer, bool isOutputBuffer,
                 CleanupManager& cleanupMgr) {
                 CleanupManager& cleanupMgr) {
            mTexture = new AutoBackendTexture(context, buffer, isOutputBuffer, cleanupMgr);
            mTexture = new AutoBackendTexture(context, buffer, isOutputBuffer, cleanupMgr);
            mTexture->ref();
            mTexture->ref();
@@ -95,14 +96,13 @@ public:
        // Makes a new SkImage from the texture content.
        // Makes a new SkImage from the texture content.
        // As SkImages are immutable but buffer content is not, we create
        // As SkImages are immutable but buffer content is not, we create
        // a new SkImage every time.
        // a new SkImage every time.
        sk_sp<SkImage> makeImage(ui::Dataspace dataspace, SkAlphaType alphaType,
        sk_sp<SkImage> makeImage(ui::Dataspace dataspace, SkAlphaType alphaType) {
                                 GrDirectContext* context) {
            return mTexture->makeImage(dataspace, alphaType);
            return mTexture->makeImage(dataspace, alphaType, context);
        }
        }


        // Makes a new SkSurface from the texture content, if needed.
        // Makes a new SkSurface from the texture content, if needed.
        sk_sp<SkSurface> getOrCreateSurface(ui::Dataspace dataspace, GrDirectContext* context) {
        sk_sp<SkSurface> getOrCreateSurface(ui::Dataspace dataspace) {
            return mTexture->getOrCreateSurface(dataspace, context);
            return mTexture->getOrCreateSurface(dataspace);
        }
        }


        SkColorType colorType() const { return mTexture->mColorType; }
        SkColorType colorType() const { return mTexture->mColorType; }
@@ -114,8 +114,10 @@ public:
    };
    };


private:
private:
    DISALLOW_COPY_AND_ASSIGN(AutoBackendTexture);

    // Creates a GrBackendTexture whose contents come from the provided buffer.
    // Creates a GrBackendTexture whose contents come from the provided buffer.
    AutoBackendTexture(GrDirectContext* context, AHardwareBuffer* buffer, bool isOutputBuffer,
    AutoBackendTexture(SkiaGpuContext* context, AHardwareBuffer* buffer, bool isOutputBuffer,
                       CleanupManager& cleanupMgr);
                       CleanupManager& cleanupMgr);


    // The only way to invoke dtor is with unref, when mUsageCount is 0.
    // The only way to invoke dtor is with unref, when mUsageCount is 0.
@@ -130,24 +132,24 @@ private:
    // Makes a new SkImage from the texture content.
    // Makes a new SkImage from the texture content.
    // As SkImages are immutable but buffer content is not, we create
    // As SkImages are immutable but buffer content is not, we create
    // a new SkImage every time.
    // a new SkImage every time.
    sk_sp<SkImage> makeImage(ui::Dataspace dataspace, SkAlphaType alphaType,
    sk_sp<SkImage> makeImage(ui::Dataspace dataspace, SkAlphaType alphaType);
                             GrDirectContext* context);


    // Makes a new SkSurface from the texture content, if needed.
    // Makes a new SkSurface from the texture content, if needed.
    sk_sp<SkSurface> getOrCreateSurface(ui::Dataspace dataspace, GrDirectContext* context);
    sk_sp<SkSurface> getOrCreateSurface(ui::Dataspace dataspace);


    GrBackendTexture mBackendTexture;
    GrBackendTexture mBackendTexture;
    GrAHardwareBufferUtils::DeleteImageProc mDeleteProc;
    GrAHardwareBufferUtils::DeleteImageProc mDeleteProc;
    GrAHardwareBufferUtils::UpdateImageProc mUpdateProc;
    GrAHardwareBufferUtils::UpdateImageProc mUpdateProc;
    GrAHardwareBufferUtils::TexImageCtx mImageCtx;
    GrAHardwareBufferUtils::TexImageCtx mImageCtx;


    // TODO: b/293371537 - Graphite abstractions for ABT.
    const sk_sp<GrDirectContext> mGrContext = nullptr;
    CleanupManager& mCleanupMgr;
    CleanupManager& mCleanupMgr;


    static void releaseSurfaceProc(SkSurface::ReleaseContext releaseContext);
    static void releaseSurfaceProc(SkSurface::ReleaseContext releaseContext);
    static void releaseImageProc(SkImages::ReleaseContext releaseContext);
    static void releaseImageProc(SkImages::ReleaseContext releaseContext);


    int mUsageCount = 0;
    int mUsageCount = 0;

    const bool mIsOutputBuffer;
    const bool mIsOutputBuffer;
    sk_sp<SkImage> mImage = nullptr;
    sk_sp<SkImage> mImage = nullptr;
    sk_sp<SkSurface> mSurface = nullptr;
    sk_sp<SkSurface> mSurface = nullptr;
+10 −10
Original line number Original line Diff line number Diff line
@@ -21,6 +21,8 @@


#include "SkiaGLRenderEngine.h"
#include "SkiaGLRenderEngine.h"


#include "compat/SkiaGpuContext.h"

#include <EGL/egl.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <EGL/eglext.h>
#include <GrContextOptions.h>
#include <GrContextOptions.h>
@@ -207,7 +209,7 @@ std::unique_ptr<SkiaGLRenderEngine> SkiaGLRenderEngine::create(
    std::unique_ptr<SkiaGLRenderEngine> engine(new SkiaGLRenderEngine(args, display, ctxt,
    std::unique_ptr<SkiaGLRenderEngine> engine(new SkiaGLRenderEngine(args, display, ctxt,
                                                                      placeholder, protectedContext,
                                                                      placeholder, protectedContext,
                                                                      protectedPlaceholder));
                                                                      protectedPlaceholder));
    engine->ensureGrContextsCreated();
    engine->ensureContextsCreated();


    ALOGI("OpenGL ES informations:");
    ALOGI("OpenGL ES informations:");
    ALOGI("vendor    : %s", extensions.getVendor());
    ALOGI("vendor    : %s", extensions.getVendor());
@@ -295,9 +297,7 @@ SkiaGLRenderEngine::~SkiaGLRenderEngine() {
    eglReleaseThread();
    eglReleaseThread();
}
}


SkiaRenderEngine::Contexts SkiaGLRenderEngine::createDirectContexts(
SkiaRenderEngine::Contexts SkiaGLRenderEngine::createContexts() {
    const GrContextOptions& options) {

    LOG_ALWAYS_FATAL_IF(isProtected(),
    LOG_ALWAYS_FATAL_IF(isProtected(),
                        "Cannot setup contexts while already in protected mode");
                        "Cannot setup contexts while already in protected mode");


@@ -306,10 +306,10 @@ SkiaRenderEngine::Contexts SkiaGLRenderEngine::createDirectContexts(
    LOG_ALWAYS_FATAL_IF(!glInterface.get(), "GrGLMakeNativeInterface() failed");
    LOG_ALWAYS_FATAL_IF(!glInterface.get(), "GrGLMakeNativeInterface() failed");


    SkiaRenderEngine::Contexts contexts;
    SkiaRenderEngine::Contexts contexts;
    contexts.first = GrDirectContexts::MakeGL(glInterface, options);
    contexts.first = SkiaGpuContext::MakeGL_Ganesh(glInterface, mSkSLCacheMonitor);
    if (supportsProtectedContentImpl()) {
    if (supportsProtectedContentImpl()) {
        useProtectedContextImpl(GrProtected::kYes);
        useProtectedContextImpl(GrProtected::kYes);
        contexts.second = GrDirectContexts::MakeGL(glInterface, options);
        contexts.second = SkiaGpuContext::MakeGL_Ganesh(glInterface, mSkSLCacheMonitor);
        useProtectedContextImpl(GrProtected::kNo);
        useProtectedContextImpl(GrProtected::kNo);
    }
    }


@@ -330,14 +330,14 @@ bool SkiaGLRenderEngine::useProtectedContextImpl(GrProtected isProtected) {
    return eglMakeCurrent(mEGLDisplay, surface, surface, context) == EGL_TRUE;
    return eglMakeCurrent(mEGLDisplay, surface, surface, context) == EGL_TRUE;
}
}


void SkiaGLRenderEngine::waitFence(GrDirectContext*, base::borrowed_fd fenceFd) {
void SkiaGLRenderEngine::waitFence(SkiaGpuContext*, base::borrowed_fd fenceFd) {
    if (fenceFd.get() >= 0 && !waitGpuFence(fenceFd)) {
    if (fenceFd.get() >= 0 && !waitGpuFence(fenceFd)) {
        ATRACE_NAME("SkiaGLRenderEngine::waitFence");
        ATRACE_NAME("SkiaGLRenderEngine::waitFence");
        sync_wait(fenceFd.get(), -1);
        sync_wait(fenceFd.get(), -1);
    }
    }
}
}


base::unique_fd SkiaGLRenderEngine::flushAndSubmit(GrDirectContext* grContext) {
base::unique_fd SkiaGLRenderEngine::flushAndSubmit(SkiaGpuContext* context) {
    base::unique_fd drawFence = flush();
    base::unique_fd drawFence = flush();


    bool requireSync = drawFence.get() < 0;
    bool requireSync = drawFence.get() < 0;
@@ -346,8 +346,8 @@ base::unique_fd SkiaGLRenderEngine::flushAndSubmit(GrDirectContext* grContext) {
    } else {
    } else {
        ATRACE_BEGIN("Submit(sync=false)");
        ATRACE_BEGIN("Submit(sync=false)");
    }
    }
    bool success = grContext->submit(requireSync ? GrSyncCpu::kYes :
    bool success =
                                                   GrSyncCpu::kNo);
            context->grDirectContext()->submit(requireSync ? GrSyncCpu::kYes : GrSyncCpu::kNo);
    ATRACE_END();
    ATRACE_END();
    if (!success) {
    if (!success) {
        ALOGE("Failed to flush RenderEngine commands");
        ALOGE("Failed to flush RenderEngine commands");
+3 −3
Original line number Original line Diff line number Diff line
@@ -59,11 +59,11 @@ public:
protected:
protected:
    // Implementations of abstract SkiaRenderEngine functions specific to
    // Implementations of abstract SkiaRenderEngine functions specific to
    // rendering backend
    // rendering backend
    virtual SkiaRenderEngine::Contexts createDirectContexts(const GrContextOptions& options);
    virtual SkiaRenderEngine::Contexts createContexts();
    bool supportsProtectedContentImpl() const override;
    bool supportsProtectedContentImpl() const override;
    bool useProtectedContextImpl(GrProtected isProtected) override;
    bool useProtectedContextImpl(GrProtected isProtected) override;
    void waitFence(GrDirectContext* grContext, base::borrowed_fd fenceFd) override;
    void waitFence(SkiaGpuContext* context, base::borrowed_fd fenceFd) override;
    base::unique_fd flushAndSubmit(GrDirectContext* context) override;
    base::unique_fd flushAndSubmit(SkiaGpuContext* context) override;
    void appendBackendSpecificInfoToDump(std::string& result) override;
    void appendBackendSpecificInfoToDump(std::string& result) override;


private:
private:
Loading