Commit 7089b2da authored by Julien Isorce's avatar Julien Isorce Committed by Commit Bot

Add specific initialisers to GLImageEGL's childs

GLImageAHardwareBuffer:
    bool Initialize(AHardwareBuffer* buffer, bool preserved)

GLImageNativePixmap:
    bool Initialize(uint32_t texture_id)

Not keeping the same signature as GLImageEGL::Initialize
to indicate how it is used. And if needed, more initialisers
can be added.

This prevents the user from doing mistakes when
instantiating a subclass of GLImageEGL but calling
the parent's initialiser.

For consolidation this CL also moves GLImageEGL::Initialize
from public to protected.

Note that GLImageEGL cannot be instantiated directly
because it does not implement a few pure virtual functions
it got from its parent, gl::GLImage.

Bug: 785201
Test: gl_unittests --gtest_filter=*NativePixmap*
Cq-Include-Trybots: master.tryserver.chromium.android:android_optional_gpu_tests_rel;master.tryserver.chromium.linux:linux_optional_gpu_tests_rel;master.tryserver.chromium.mac:mac_optional_gpu_tests_rel;master.tryserver.chromium.win:win_optional_gpu_tests_rel
Change-Id: Ida5834bbf964893680f449f1f5da7371e46075bc
Reviewed-on: https://chromium-review.googlesource.com/817563
Commit-Queue: Julien Isorce <julien.isorce@chromium.org>
Reviewed-by: default avatarDavid Reveman <reveman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#525307}
parent 55a56452
...@@ -55,12 +55,9 @@ GpuMemoryBufferFactoryAndroidHardwareBuffer::CreateImageForGpuMemoryBuffer( ...@@ -55,12 +55,9 @@ GpuMemoryBufferFactoryAndroidHardwareBuffer::CreateImageForGpuMemoryBuffer(
AHardwareBuffer* buffer = handle.handle.GetMemoryObject(); AHardwareBuffer* buffer = handle.handle.GetMemoryObject();
DCHECK(buffer); DCHECK(buffer);
EGLint attribs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_FALSE, EGL_NONE}; scoped_refptr<gl::GLImageAHardwareBuffer> image(
new gl::GLImageAHardwareBuffer(size));
scoped_refptr<gl::GLImageEGL> image(new gl::GLImageAHardwareBuffer(size)); if (!image->Initialize(buffer, /* preserved */ false)) {
EGLClientBuffer client_buffer = eglGetNativeClientBufferANDROID(buffer);
if (!image->Initialize(EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
client_buffer, attribs)) {
DLOG(ERROR) << "Failed to create GLImage " << size.ToString(); DLOG(ERROR) << "Failed to create GLImage " << size.ToString();
image = nullptr; image = nullptr;
} }
......
...@@ -13,6 +13,15 @@ GLImageAHardwareBuffer::GLImageAHardwareBuffer(const gfx::Size& size) ...@@ -13,6 +13,15 @@ GLImageAHardwareBuffer::GLImageAHardwareBuffer(const gfx::Size& size)
GLImageAHardwareBuffer::~GLImageAHardwareBuffer() {} GLImageAHardwareBuffer::~GLImageAHardwareBuffer() {}
bool GLImageAHardwareBuffer::Initialize(AHardwareBuffer* buffer,
bool preserved) {
EGLint attribs[] = {EGL_IMAGE_PRESERVED_KHR, preserved ? EGL_TRUE : EGL_FALSE,
EGL_NONE};
EGLClientBuffer client_buffer = eglGetNativeClientBufferANDROID(buffer);
return GLImageEGL::Initialize(EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
client_buffer, attribs);
}
unsigned GLImageAHardwareBuffer::GetInternalFormat() { unsigned GLImageAHardwareBuffer::GetInternalFormat() {
return GL_RGBA; return GL_RGBA;
} }
......
...@@ -15,6 +15,9 @@ class GL_EXPORT GLImageAHardwareBuffer : public GLImageEGL { ...@@ -15,6 +15,9 @@ class GL_EXPORT GLImageAHardwareBuffer : public GLImageEGL {
public: public:
explicit GLImageAHardwareBuffer(const gfx::Size& size); explicit GLImageAHardwareBuffer(const gfx::Size& size);
// Create an EGLImage from a given Android hardware buffer.
bool Initialize(AHardwareBuffer* buffer, bool preserved);
// Overridden from GLImage: // Overridden from GLImage:
unsigned GetInternalFormat() override; unsigned GetInternalFormat() override;
bool CopyTexImage(unsigned target) override; bool CopyTexImage(unsigned target) override;
......
...@@ -76,10 +76,7 @@ class GLImageAHardwareBufferTestDelegate { ...@@ -76,10 +76,7 @@ class GLImageAHardwareBufferTestDelegate {
EXPECT_EQ(unlock_result, 0); EXPECT_EQ(unlock_result, 0);
auto image = base::MakeRefCounted<GLImageAHardwareBuffer>(size); auto image = base::MakeRefCounted<GLImageAHardwareBuffer>(size);
EGLint attribs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE}; bool rv = image->Initialize(buffer, /* preserved */ true);
EGLClientBuffer client_buffer = eglGetNativeClientBufferANDROID(buffer);
bool rv = image->Initialize(EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
client_buffer, attribs);
EXPECT_TRUE(rv); EXPECT_TRUE(rv);
return image; return image;
} }
......
...@@ -18,6 +18,14 @@ class GL_EXPORT GLImageEGL : public GLImage { ...@@ -18,6 +18,14 @@ class GL_EXPORT GLImageEGL : public GLImage {
public: public:
explicit GLImageEGL(const gfx::Size& size); explicit GLImageEGL(const gfx::Size& size);
// Overridden from GLImage:
gfx::Size GetSize() override;
bool BindTexImage(unsigned target) override;
void ReleaseTexImage(unsigned target) override {}
protected:
~GLImageEGL() override;
// Same semantic as specified for eglCreateImageKHR. There two main usages: // Same semantic as specified for eglCreateImageKHR. There two main usages:
// 1- When using the |target| EGL_GL_TEXTURE_2D_KHR it is required to pass // 1- When using the |target| EGL_GL_TEXTURE_2D_KHR it is required to pass
// a valid |context|. This allows to create an EGLImage from a GL texture. // a valid |context|. This allows to create an EGLImage from a GL texture.
...@@ -32,14 +40,6 @@ class GL_EXPORT GLImageEGL : public GLImage { ...@@ -32,14 +40,6 @@ class GL_EXPORT GLImageEGL : public GLImage {
EGLClientBuffer buffer, EGLClientBuffer buffer,
const EGLint* attrs); const EGLint* attrs);
// Overridden from GLImage:
gfx::Size GetSize() override;
bool BindTexImage(unsigned target) override;
void ReleaseTexImage(unsigned target) override {}
protected:
~GLImageEGL() override;
EGLImageKHR egl_image_; EGLImageKHR egl_image_;
const gfx::Size size_; const gfx::Size size_;
base::ThreadChecker thread_checker_; base::ThreadChecker thread_checker_;
......
...@@ -239,6 +239,26 @@ bool GLImageNativePixmap::Initialize(gfx::NativePixmap* pixmap, ...@@ -239,6 +239,26 @@ bool GLImageNativePixmap::Initialize(gfx::NativePixmap* pixmap,
return true; return true;
} }
bool GLImageNativePixmap::InitializeFromTexture(uint32_t texture_id) {
GLContext* current_context = GLContext::GetCurrent();
if (!current_context || !current_context->IsCurrent(nullptr)) {
LOG(ERROR) << "No gl context bound to the current thread";
return false;
}
EGLContext context_handle =
reinterpret_cast<EGLContext>(current_context->GetHandle());
DCHECK_NE(context_handle, EGL_NO_CONTEXT);
if (!GLImageEGL::Initialize(context_handle, EGL_GL_TEXTURE_2D_KHR,
reinterpret_cast<EGLClientBuffer>(texture_id),
nullptr)) {
return false;
}
return true;
}
gfx::NativePixmapHandle GLImageNativePixmap::ExportHandle() { gfx::NativePixmapHandle GLImageNativePixmap::ExportHandle() {
DCHECK(!pixmap_); DCHECK(!pixmap_);
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
......
...@@ -19,7 +19,11 @@ class GL_EXPORT GLImageNativePixmap : public gl::GLImageEGL { ...@@ -19,7 +19,11 @@ class GL_EXPORT GLImageNativePixmap : public gl::GLImageEGL {
public: public:
GLImageNativePixmap(const gfx::Size& size, unsigned internalformat); GLImageNativePixmap(const gfx::Size& size, unsigned internalformat);
// Create an EGLImage from a given NativePixmap.
bool Initialize(gfx::NativePixmap* pixmap, gfx::BufferFormat format); bool Initialize(gfx::NativePixmap* pixmap, gfx::BufferFormat format);
// Create an EGLImage from a given GL texture.
bool InitializeFromTexture(uint32_t texture_id);
// Export the wrapped EGLImage to dmabuf fds.
gfx::NativePixmapHandle ExportHandle(); gfx::NativePixmapHandle ExportHandle();
// Overridden from GLImage: // Overridden from GLImage:
......
...@@ -53,7 +53,10 @@ class GLImageNativePixmapTest : public testing::Test { ...@@ -53,7 +53,10 @@ class GLImageNativePixmapTest : public testing::Test {
surface_ = gl::init::CreateOffscreenGLSurface(gfx::Size()); surface_ = gl::init::CreateOffscreenGLSurface(gfx::Size());
context_ = context_ =
gl::init::CreateGLContext(nullptr, surface_.get(), GLContextAttribs()); gl::init::CreateGLContext(nullptr, surface_.get(), GLContextAttribs());
context_->MakeCurrent(surface_.get()); if (!context_->MakeCurrent(surface_.get())) {
LOG(WARNING) << "Skip test, failed to make the GL context current";
return;
}
skip_test_ = false; skip_test_ = false;
} }
...@@ -76,12 +79,6 @@ void GLTexture2DToDmabuf(gfx::BufferFormat image_format, ...@@ -76,12 +79,6 @@ void GLTexture2DToDmabuf(gfx::BufferFormat image_format,
GLenum tex_format) { GLenum tex_format) {
const gfx::Size image_size(64, 64); const gfx::Size image_size(64, 64);
EXPECT_NE(nullptr, GLContext::GetCurrent());
EGLContext context =
reinterpret_cast<EGLContext>(GLContext::GetCurrent()->GetHandle());
EXPECT_NE(EGL_NO_CONTEXT, context);
scoped_refptr<gl::GLImageNativePixmap> image(new gl::GLImageNativePixmap( scoped_refptr<gl::GLImageNativePixmap> image(new gl::GLImageNativePixmap(
image_size, image_size,
gl::GLImageNativePixmap::GetInternalFormatForTesting(image_format))); gl::GLImageNativePixmap::GetInternalFormatForTesting(image_format)));
...@@ -97,10 +94,7 @@ void GLTexture2DToDmabuf(gfx::BufferFormat image_format, ...@@ -97,10 +94,7 @@ void GLTexture2DToDmabuf(gfx::BufferFormat image_format,
glTexImage2D(GL_TEXTURE_2D, 0, tex_internal_format, image_size.width(), glTexImage2D(GL_TEXTURE_2D, 0, tex_internal_format, image_size.width(),
image_size.height(), 0, tex_format, GL_UNSIGNED_BYTE, nullptr); image_size.height(), 0, tex_format, GL_UNSIGNED_BYTE, nullptr);
scoped_refptr<gl::GLImageEGL> base_image = image; EXPECT_TRUE(image->InitializeFromTexture(texture_id));
EXPECT_TRUE(base_image->Initialize(
context, EGL_GL_TEXTURE_2D_KHR,
reinterpret_cast<EGLClientBuffer>(texture_id), nullptr));
gfx::NativePixmapHandle native_pixmap_handle = image->ExportHandle(); gfx::NativePixmapHandle native_pixmap_handle = image->ExportHandle();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment