Skip to content

Commit

Permalink
Delete GLImageAHardwareBuffer
Browse files Browse the repository at this point in the history
GLImageAHardwareBuffer was used by SharedImage code to bind AHB to GL.
This CL replaces it with ScopedEGLImage and deletes the class.

Bug: 1310018
Change-Id: I5814d5477a258d9174ccbef8ec559613c92fc0a0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4108072
Reviewed-by: Kyle Charbonneau <kylechar@chromium.org>
Reviewed-by: Sunny Sachanandani <sunnyps@chromium.org>
Commit-Queue: Vasiliy Telezhnikov <vasilyt@chromium.org>
Reviewed-by: Saifuddin Hitawala <hitawala@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1084506}
  • Loading branch information
vasilyt authored and Chromium LUCI CQ committed Dec 16, 2022
1 parent 9ff4066 commit 8e44e64
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 449 deletions.
113 changes: 0 additions & 113 deletions gpu/command_buffer/service/ahardwarebuffer_utils.cc
Expand Up @@ -18,96 +18,10 @@
#include "ui/gfx/color_space.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gl/gl_gl_api_implementation.h"
#include "ui/gl/gl_image_ahardwarebuffer.h"
#include "ui/gl/scoped_binders.h"

namespace gpu {

// Helpers to allow for easy friending of the below restricted functions.
void SetColorSpaceOnGLImage(gl::GLImage* gl_image,
const gfx::ColorSpace& color_space) {
gl_image->SetColorSpace(color_space);
}
unsigned GetDataFormatOfGLImage(gl::GLImage* gl_image) {
return gl_image->GetDataFormat();
}

namespace {

gles2::Texture* MakeGLTexture(
GLenum target,
GLuint service_id,
scoped_refptr<gl::GLImageAHardwareBuffer> egl_image,
const gfx::Size& size,
const gfx::Rect& cleared_rect) {
auto* texture = gles2::CreateGLES2TextureWithLightRef(service_id, target);

texture->SetLevelInfo(target, 0, egl_image->GetInternalFormat(), size.width(),
size.height(), 1, 0,
GetDataFormatOfGLImage(egl_image.get()),
egl_image->GetDataType(), cleared_rect);
texture->SetLevelImage(target, 0, egl_image.get(), gles2::Texture::BOUND);
texture->SetImmutable(true, false);
return texture;
}

scoped_refptr<gles2::TexturePassthrough> MakeGLTexturePassthrough(
GLenum target,
GLuint service_id,
scoped_refptr<gl::GLImageAHardwareBuffer> egl_image,
const size_t estimated_size) {
auto passthrough_texture =
base::MakeRefCounted<gles2::TexturePassthrough>(service_id, target);
passthrough_texture->SetEstimatedSize(estimated_size);
passthrough_texture->SetLevelImage(target, 0, egl_image.get());
return passthrough_texture;
}

void GenGLTextureInternal(
AHardwareBuffer* buffer,
GLenum target,
const gfx::ColorSpace& color_space,
const gfx::Size& size,
const size_t estimated_size,
const gfx::Rect& cleared_rect,
scoped_refptr<gles2::TexturePassthrough>* passthrough_texture,
gles2::Texture** texture) {
gl::GLApi* api = gl::g_current_gl_context;
GLuint service_id = 0;
api->glGenTexturesFn(1, &service_id);
gl::ScopedTextureBinder texture_binder(target, service_id);

api->glTexParameteriFn(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
api->glTexParameteriFn(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
api->glTexParameteriFn(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
api->glTexParameteriFn(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

// Create an egl image using AHardwareBuffer.
auto egl_image = base::MakeRefCounted<gl::GLImageAHardwareBuffer>(size);
if (!egl_image->Initialize(buffer, false)) {
LOG(ERROR) << "Failed to create EGL image";
api->glDeleteTexturesFn(1, &service_id);
return;
}

if (!egl_image->BindTexImage(target)) {
LOG(ERROR) << "Failed to bind egl image";
api->glDeleteTexturesFn(1, &service_id);
return;
}
SetColorSpaceOnGLImage(egl_image.get(), color_space);

if (passthrough_texture) {
*passthrough_texture = MakeGLTexturePassthrough(
target, service_id, std::move(egl_image), estimated_size);
} else {
*texture = MakeGLTexture(target, service_id, std::move(egl_image), size,
cleared_rect);
}
}

} // namespace

bool AHardwareBufferSupportedFormat(viz::ResourceFormat format) {
switch (format) {
case viz::RGBA_8888:
Expand Down Expand Up @@ -143,33 +57,6 @@ unsigned int AHardwareBufferFormat(viz::ResourceFormat format) {
}
}

gles2::Texture* GenGLTexture(AHardwareBuffer* buffer,
GLenum target,
const gfx::ColorSpace& color_space,
const gfx::Size& size,
const size_t estimated_size,
const gfx::Rect& cleared_rect) {
gles2::Texture* texture = nullptr;
GenGLTextureInternal(buffer, target, color_space, size, estimated_size,
cleared_rect, nullptr /* passthrough_texture */,
&texture);
return texture;
}

scoped_refptr<gles2::TexturePassthrough> GenGLTexturePassthrough(
AHardwareBuffer* buffer,
GLenum target,
const gfx::ColorSpace& color_space,
const gfx::Size& size,
const size_t estimated_size,
const gfx::Rect& cleared_rect) {
scoped_refptr<gles2::TexturePassthrough> passthrough_texture;
GenGLTextureInternal(buffer, target, color_space, size, estimated_size,
cleared_rect, &passthrough_texture,
nullptr /* texture */);
return passthrough_texture;
}

std::unique_ptr<VulkanImage> CreateVkImageFromAhbHandle(
base::android::ScopedHardwareBufferHandle ahb_handle,
SharedContextState* context_state,
Expand Down
27 changes: 0 additions & 27 deletions gpu/command_buffer/service/ahardwarebuffer_utils.h
Expand Up @@ -23,20 +23,13 @@ class ScopedHardwareBufferHandle;
} // namespace base

namespace gfx {
class ColorSpace;
class Rect;
class Size;
} // namespace gfx

namespace gpu {
class SharedContextState;
class VulkanImage;

namespace gles2 {
class Texture;
class TexturePassthrough;
} // namespace gles2

// TODO(vikassoni): In future we will need to expose the set of formats and
// constraints (e.g. max size) to the clients somehow that are available for
// certain combinations of SharedImageUsage flags (e.g. when Vulkan is on,
Expand All @@ -52,26 +45,6 @@ AHardwareBufferSupportedFormat(viz::ResourceFormat format);
// Returns the corresponding AHardwareBuffer format.
unsigned int GPU_GLES2_EXPORT AHardwareBufferFormat(viz::ResourceFormat format);

// Generates a gles2 texture from AHB. This method must be called with a current
// GLContext which will be used to create the Texture. This method adds a
// lightweight ref on the Texture which the caller is responsible for releasing.
gles2::Texture* GenGLTexture(AHardwareBuffer* buffer,
GLenum target,
const gfx::ColorSpace& color_space,
const gfx::Size& size,
const size_t estimated_size,
const gfx::Rect& cleared_rect);

// Generates a passthrough texture from AHB. This method must be called with a
// current GLContext which will be used to create the Texture.
scoped_refptr<gles2::TexturePassthrough> GenGLTexturePassthrough(
AHardwareBuffer* buffer,
GLenum target,
const gfx::ColorSpace& color_space,
const gfx::Size& size,
const size_t estimated_size,
const gfx::Rect& cleared_rect);

// Create a vulkan image from the AHB handle.
std::unique_ptr<VulkanImage> CreateVkImageFromAhbHandle(
base::android::ScopedHardwareBufferHandle ahb_handle,
Expand Down
Expand Up @@ -52,9 +52,9 @@
#include "ui/gl/gl_context.h"
#include "ui/gl/gl_fence_android_native_fence_sync.h"
#include "ui/gl/gl_gl_api_implementation.h"
#include "ui/gl/gl_image_ahardwarebuffer.h"
#include "ui/gl/gl_utils.h"
#include "ui/gl/gl_version_info.h"
#include "ui/gl/scoped_binders.h"

namespace gpu {
namespace {
Expand Down Expand Up @@ -119,6 +119,21 @@ class OverlayImage final : public base::RefCounted<OverlayImage> {
base::ScopedFD previous_end_read_fence_;
};

GLuint CreateAndBindTexture(EGLImage image, GLenum target) {
gl::GLApi* api = gl::g_current_gl_context;
GLuint service_id = 0;
api->glGenTexturesFn(1, &service_id);
gl::ScopedTextureBinder texture_binder(target, service_id);

api->glTexParameteriFn(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
api->glTexParameteriFn(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
api->glTexParameteriFn(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
api->glTexParameteriFn(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glEGLImageTargetTexture2DOES(target, image);

return service_id;
}
} // namespace

// Implementation of SharedImageBacking that holds an AHardwareBuffer. This
Expand Down Expand Up @@ -322,19 +337,30 @@ AHardwareBufferImageBacking::ProduceGLTexture(SharedImageManager* manager,
// backing.
DCHECK(hardware_buffer_handle_.is_valid());

// Note that we are not using GL_TEXTURE_EXTERNAL_OES target(here and all
// other places in this file) since sksurface
// doesn't supports it. As per the egl documentation -
// https://www.khronos.org/registry/OpenGL/extensions/OES/OES_EGL_image_external.txt
// if GL_OES_EGL_image is supported then <target> may also be TEXTURE_2D.
auto* texture =
GenGLTexture(hardware_buffer_handle_.get(), GL_TEXTURE_2D, color_space(),
size(), GetEstimatedSize(), ClearedRect());
if (!texture)
auto egl_image =
CreateEGLImageFromAHardwareBuffer(hardware_buffer_handle_.get());

if (!egl_image.is_valid()) {
return nullptr;
}

// Android documentation states that right GL format for RGBX AHardwareBuffer
// is GL_RGB8, so we don't use angle rgbx.
auto gl_format_desc = ToGLFormatDesc(format(), /*plane_index=*/0,
/*use_angle_rgbx_format=*/false);
GLuint service_id =
CreateAndBindTexture(egl_image.get(), gl_format_desc.target);

auto* texture =
gles2::CreateGLES2TextureWithLightRef(service_id, gl_format_desc.target);
texture->SetLevelInfo(gl_format_desc.target, 0,
gl_format_desc.image_internal_format, size().width(),
size().height(), 1, 0, gl_format_desc.data_format,
gl_format_desc.data_type, ClearedRect());
texture->SetImmutable(true, false);

return std::make_unique<GLTextureAndroidImageRepresentation>(
manager, this, tracker, std::move(texture));
manager, this, tracker, std::move(egl_image), std::move(texture));
}

std::unique_ptr<GLTexturePassthroughImageRepresentation>
Expand All @@ -345,19 +371,25 @@ AHardwareBufferImageBacking::ProduceGLTexturePassthrough(
// backing.
DCHECK(hardware_buffer_handle_.is_valid());

// Note that we are not using GL_TEXTURE_EXTERNAL_OES target(here and all
// other places in this file) since sksurface
// doesn't supports it. As per the egl documentation -
// https://www.khronos.org/registry/OpenGL/extensions/OES/OES_EGL_image_external.txt
// if GL_OES_EGL_image is supported then <target> may also be TEXTURE_2D.
auto texture = GenGLTexturePassthrough(hardware_buffer_handle_.get(),
GL_TEXTURE_2D, color_space(), size(),
GetEstimatedSize(), ClearedRect());
if (!texture)
auto egl_image =
CreateEGLImageFromAHardwareBuffer(hardware_buffer_handle_.get());
if (!egl_image.is_valid()) {
return nullptr;
}

// Android documentation states that right GL format for RGBX AHardwareBuffer
// is GL_RGB8, so we don't use angle rgbx.
auto gl_format_desc = ToGLFormatDesc(format(), /*plane_index=*/0,
/*use_angle_rgbx_format=*/false);
GLuint service_id =
CreateAndBindTexture(egl_image.get(), gl_format_desc.target);

auto texture = base::MakeRefCounted<gles2::TexturePassthrough>(
service_id, gl_format_desc.target);
texture->SetEstimatedSize(GetEstimatedSize());

return std::make_unique<GLTexturePassthroughAndroidImageRepresentation>(
manager, this, tracker, std::move(texture));
manager, this, tracker, std::move(egl_image), std::move(texture));
}

std::unique_ptr<SkiaImageRepresentation>
Expand Down
Expand Up @@ -13,8 +13,10 @@ GLTextureAndroidImageRepresentation::GLTextureAndroidImageRepresentation(
SharedImageManager* manager,
AndroidImageBacking* backing,
MemoryTypeTracker* tracker,
ui::ScopedEGLImage egl_image,
gles2::Texture* texture)
: GLTextureImageRepresentation(manager, backing, tracker),
egl_image_(std::move(egl_image)),
texture_(texture) {}

GLTextureAndroidImageRepresentation::~GLTextureAndroidImageRepresentation() {
Expand Down
Expand Up @@ -8,6 +8,7 @@
#include "base/memory/raw_ptr.h"
#include "gpu/command_buffer/service/shared_image/android_image_backing.h"
#include "gpu/command_buffer/service/shared_image/shared_image_representation.h"
#include "ui/gl/scoped_egl_image.h"

namespace gpu {
class AndroidImageBacking;
Expand All @@ -20,6 +21,7 @@ class GLTextureAndroidImageRepresentation
GLTextureAndroidImageRepresentation(SharedImageManager* manager,
AndroidImageBacking* backing,
MemoryTypeTracker* tracker,
ui::ScopedEGLImage egl_image,
gles2::Texture* texture);
~GLTextureAndroidImageRepresentation() override;

Expand All @@ -38,6 +40,7 @@ class GLTextureAndroidImageRepresentation
return static_cast<AndroidImageBacking*>(backing());
}

ui::ScopedEGLImage egl_image_;
const raw_ptr<gles2::Texture, DanglingUntriaged> texture_;
RepresentationAccessMode mode_ = RepresentationAccessMode::kNone;
};
Expand Down
Expand Up @@ -14,8 +14,10 @@ GLTexturePassthroughAndroidImageRepresentation::
SharedImageManager* manager,
AndroidImageBacking* backing,
MemoryTypeTracker* tracker,
ui::ScopedEGLImage egl_image,
scoped_refptr<gles2::TexturePassthrough> texture)
: GLTexturePassthroughImageRepresentation(manager, backing, tracker),
egl_image_(std::move(egl_image)),
texture_(std::move(texture)) {
// TODO(https://crbug.com/1172769): Remove this CHECK.
CHECK(texture_);
Expand Down
Expand Up @@ -7,6 +7,7 @@

#include "gpu/command_buffer/service/shared_image/android_image_backing.h"
#include "gpu/command_buffer/service/shared_image/shared_image_representation.h"
#include "ui/gl/scoped_egl_image.h"

namespace gpu {
class AndroidImageBacking;
Expand All @@ -18,6 +19,7 @@ class GLTexturePassthroughAndroidImageRepresentation
SharedImageManager* manager,
AndroidImageBacking* backing,
MemoryTypeTracker* tracker,
ui::ScopedEGLImage egl_image,
scoped_refptr<gles2::TexturePassthrough> texture);
~GLTexturePassthroughAndroidImageRepresentation() override;

Expand All @@ -37,6 +39,7 @@ class GLTexturePassthroughAndroidImageRepresentation
return static_cast<AndroidImageBacking*>(backing());
}

ui::ScopedEGLImage egl_image_;
scoped_refptr<gles2::TexturePassthrough> texture_;
RepresentationAccessMode mode_ = RepresentationAccessMode::kNone;
};
Expand Down
Expand Up @@ -33,7 +33,6 @@
#include "third_party/skia/include/gpu/GrBackendSemaphore.h"
#include "third_party/skia/include/gpu/GrBackendSurface.h"
#include "ui/gl/android/egl_fence_utils.h"
#include "ui/gl/gl_image_ahardwarebuffer.h"
#include "ui/gl/gl_utils.h"

namespace gpu {
Expand All @@ -43,12 +42,12 @@ void CreateAndBindEglImageFromAHB(AHardwareBuffer* buffer, GLuint service_id) {
DCHECK(buffer);

AHardwareBuffer_Desc desc;

base::AndroidHardwareBufferCompat::GetInstance().Describe(buffer, &desc);
auto egl_image = base::MakeRefCounted<gl::GLImageAHardwareBuffer>(
gfx::Size(desc.width, desc.height));
if (egl_image->Initialize(buffer, false)) {
auto egl_image = CreateEGLImageFromAHardwareBuffer(buffer);
if (egl_image.is_valid()) {
glBindTexture(GL_TEXTURE_EXTERNAL_OES, service_id);
egl_image->BindTexImage(GL_TEXTURE_EXTERNAL_OES);
glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, egl_image.get());
} else {
LOG(ERROR) << "Failed to create EGL image ";
}
Expand Down

0 comments on commit 8e44e64

Please sign in to comment.