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 Diff line number Diff line
@@ -88,6 +88,7 @@ filegroup {
        "skia/SkiaGLRenderEngine.cpp",
        "skia/SkiaVkRenderEngine.cpp",
        "skia/VulkanInterface.cpp",
        "skia/compat/GaneshGpuContext.cpp",
        "skia/debug/CaptureTimer.cpp",
        "skia/debug/CommonPool.cpp",
        "skia/debug/SkiaCapture.cpp",
+24 −36
Original line number Diff line number Diff line
@@ -21,12 +21,12 @@
#define ATRACE_TAG ATRACE_TAG_GRAPHICS

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

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

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

    GrBackendApi backend = context->backend();
    GrBackendApi backend = mGrContext->backend();
    if (backend == GrBackendApi::kOpenGL) {
        backendFormat =
                GrAHardwareBufferUtils::GetGLBackendFormat(context, desc.format, false);
                GrAHardwareBufferUtils::GetGLBackendFormat(mGrContext.get(), desc.format, false);
        mBackendTexture =
                GrAHardwareBufferUtils::MakeGLBackendTexture(context,
                                                             buffer,
                                                             desc.width,
                                                             desc.height,
                                                             &mDeleteProc,
                                                             &mUpdateProc,
                                                             &mImageCtx,
                                                             createProtectedImage,
                                                             backendFormat,
                GrAHardwareBufferUtils::MakeGLBackendTexture(mGrContext.get(), buffer, desc.width,
                                                             desc.height, &mDeleteProc,
                                                             &mUpdateProc, &mImageCtx,
                                                             createProtectedImage, backendFormat,
                                                             isOutputBuffer);
    } else if (backend == GrBackendApi::kVulkan) {
        backendFormat =
                GrAHardwareBufferUtils::GetVulkanBackendFormat(context,
                                                               buffer,
                                                               desc.format,
                                                               false);
        backendFormat = GrAHardwareBufferUtils::GetVulkanBackendFormat(mGrContext.get(), buffer,
                                                                       desc.format, false);
        mBackendTexture =
                GrAHardwareBufferUtils::MakeVulkanBackendTexture(context,
                                                                 buffer,
                                                                 desc.width,
                                                                 desc.height,
                                                                 &mDeleteProc,
                                                                 &mUpdateProc,
                                                                 &mImageCtx,
                                                                 createProtectedImage,
                                                                 backendFormat,
                                                                 isOutputBuffer);
                GrAHardwareBufferUtils::MakeVulkanBackendTexture(mGrContext.get(), buffer,
                                                                 desc.width, desc.height,
                                                                 &mDeleteProc, &mUpdateProc,
                                                                 &mImageCtx, createProtectedImage,
                                                                 backendFormat, isOutputBuffer);
    } else {
        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,
                                             GrDirectContext* context) {
sk_sp<SkImage> AutoBackendTexture::makeImage(ui::Dataspace dataspace, SkAlphaType alphaType) {
    ATRACE_CALL();

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

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

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

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

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

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

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

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

private:
    DISALLOW_COPY_AND_ASSIGN(AutoBackendTexture);

    // 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);

    // 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.
    // As SkImages are immutable but buffer content is not, we create
    // a new SkImage every time.
    sk_sp<SkImage> makeImage(ui::Dataspace dataspace, SkAlphaType alphaType,
                             GrDirectContext* context);
    sk_sp<SkImage> makeImage(ui::Dataspace dataspace, SkAlphaType alphaType);

    // 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;
    GrAHardwareBufferUtils::DeleteImageProc mDeleteProc;
    GrAHardwareBufferUtils::UpdateImageProc mUpdateProc;
    GrAHardwareBufferUtils::TexImageCtx mImageCtx;

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

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

    int mUsageCount = 0;

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

#include "SkiaGLRenderEngine.h"

#include "compat/SkiaGpuContext.h"

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

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

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

SkiaRenderEngine::Contexts SkiaGLRenderEngine::createContexts() {
    LOG_ALWAYS_FATAL_IF(isProtected(),
                        "Cannot setup contexts while already in protected mode");

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

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

@@ -330,14 +330,14 @@ bool SkiaGLRenderEngine::useProtectedContextImpl(GrProtected isProtected) {
    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)) {
        ATRACE_NAME("SkiaGLRenderEngine::waitFence");
        sync_wait(fenceFd.get(), -1);
    }
}

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

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

private:
Loading