Commit 9b215c6d authored by Vikas Soni's avatar Vikas Soni Committed by Commit Bot

Skia representation of SharedImageBackingAHardwareBuffer.

Add a GL backed skia representation of
SharedImageBackingAHardwareBuffer. Update unit test.

Bug: 891060
Change-Id: Ib3b458615be13e7a536a12097e8452052b78a14f
Reviewed-on: https://chromium-review.googlesource.com/c/1330205Reviewed-by: default avatarAntoine Labour <piman@chromium.org>
Commit-Queue: vikas soni <vikassoni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#607760}
parent 420fad02
...@@ -17,23 +17,26 @@ ...@@ -17,23 +17,26 @@
#include "gpu/command_buffer/service/memory_tracking.h" #include "gpu/command_buffer/service/memory_tracking.h"
#include "gpu/command_buffer/service/shared_image_backing.h" #include "gpu/command_buffer/service/shared_image_backing.h"
#include "gpu/command_buffer/service/shared_image_representation.h" #include "gpu/command_buffer/service/shared_image_representation.h"
#include "gpu/command_buffer/service/skia_utils.h"
#include "gpu/command_buffer/service/texture_manager.h" #include "gpu/command_buffer/service/texture_manager.h"
#include "third_party/skia/include/gpu/GrBackendSurface.h"
#include "ui/gfx/color_space.h" #include "ui/gfx/color_space.h"
#include "ui/gfx/geometry/size.h" #include "ui/gfx/geometry/size.h"
#include "ui/gl/gl_context.h"
#include "ui/gl/gl_gl_api_implementation.h" #include "ui/gl/gl_gl_api_implementation.h"
#include "ui/gl/gl_image_ahardwarebuffer.h" #include "ui/gl/gl_image_ahardwarebuffer.h"
#include "ui/gl/gl_version_info.h" #include "ui/gl/gl_version_info.h"
namespace gpu { namespace gpu {
// Representation of a SharedImageBackingAHardwareBuffer as a GL Texture. // Representation of a SharedImageBackingAHB as a GL Texture.
class SharedImageRepresentationGLTextureAHardwareBuffer class SharedImageRepresentationGLTextureAHB
: public SharedImageRepresentationGLTexture { : public SharedImageRepresentationGLTexture {
public: public:
SharedImageRepresentationGLTextureAHardwareBuffer(SharedImageManager* manager, SharedImageRepresentationGLTextureAHB(SharedImageManager* manager,
SharedImageBacking* backing, SharedImageBacking* backing,
MemoryTypeTracker* tracker, MemoryTypeTracker* tracker,
gles2::Texture* texture) gles2::Texture* texture)
: SharedImageRepresentationGLTexture(manager, backing, tracker), : SharedImageRepresentationGLTexture(manager, backing, tracker),
texture_(texture) {} texture_(texture) {}
...@@ -42,22 +45,93 @@ class SharedImageRepresentationGLTextureAHardwareBuffer ...@@ -42,22 +45,93 @@ class SharedImageRepresentationGLTextureAHardwareBuffer
private: private:
gles2::Texture* texture_; gles2::Texture* texture_;
DISALLOW_COPY_AND_ASSIGN(SharedImageRepresentationGLTextureAHardwareBuffer); DISALLOW_COPY_AND_ASSIGN(SharedImageRepresentationGLTextureAHB);
};
// GL backed Skia representation of SharedImageBackingAHB.
// TODO(vikassoni): Add follow up patch to add a vulkan backed skia
// representation.
class SharedImageRepresentationSkiaGLAHB
: public SharedImageRepresentationSkia {
public:
SharedImageRepresentationSkiaGLAHB(SharedImageManager* manager,
SharedImageBacking* backing,
MemoryTypeTracker* tracker,
GLenum target,
GLenum internal_format,
GLenum driver_internal_format,
GLuint service_id)
: SharedImageRepresentationSkia(manager, backing, tracker),
target_(target),
internal_format_(internal_format),
driver_internal_format_(driver_internal_format),
service_id_(service_id) {}
~SharedImageRepresentationSkiaGLAHB() override { DCHECK(!write_surface_); }
sk_sp<SkSurface> BeginWriteAccess(
GrContext* gr_context,
int final_msaa_count,
SkColorType color_type,
const SkSurfaceProps& surface_props) override {
if (write_surface_)
return nullptr;
GrBackendTexture backend_texture;
if (!GetGrBackendTexture(target_, size(), internal_format_,
driver_internal_format_, service_id_, color_type,
&backend_texture)) {
return nullptr;
}
auto surface = SkSurface::MakeFromBackendTextureAsRenderTarget(
gr_context, backend_texture, kTopLeft_GrSurfaceOrigin, final_msaa_count,
color_type, nullptr, &surface_props);
write_surface_ = surface.get();
return surface;
}
void EndWriteAccess(sk_sp<SkSurface> surface) override {
DCHECK_EQ(surface.get(), write_surface_);
DCHECK(surface->unique());
// TODO(ericrk): Keep the surface around for re-use.
write_surface_ = nullptr;
}
bool BeginReadAccess(SkColorType color_type,
GrBackendTexture* backend_texture) override {
if (!GetGrBackendTexture(target_, size(), internal_format_,
driver_internal_format_, service_id_, color_type,
backend_texture)) {
return false;
}
return true;
}
void EndReadAccess() override {
// TODO(ericrk): Handle begin/end correctness checks.
}
private:
GLenum target_;
GLenum internal_format_ = 0;
GLenum driver_internal_format_ = 0;
GLuint service_id_;
SkSurface* write_surface_ = nullptr;
}; };
// Implementation of SharedImageBacking that holds an AHardwareBuffer. This // Implementation of SharedImageBacking that holds an AHardwareBuffer. This
// can be used to create a GL texture or a VK Image from the AHardwareBuffer // can be used to create a GL texture or a VK Image from the AHardwareBuffer
// backing. // backing.
class SharedImageBackingAHardwareBuffer : public SharedImageBacking { class SharedImageBackingAHB : public SharedImageBacking {
public: public:
SharedImageBackingAHardwareBuffer( SharedImageBackingAHB(const Mailbox& mailbox,
const Mailbox& mailbox, viz::ResourceFormat format,
viz::ResourceFormat format, const gfx::Size& size,
const gfx::Size& size, const gfx::ColorSpace& color_space,
const gfx::ColorSpace& color_space, uint32_t usage,
uint32_t usage, base::android::ScopedHardwareBufferHandle handle,
base::android::ScopedHardwareBufferHandle handle, size_t estimated_size)
size_t estimated_size)
: SharedImageBacking(mailbox, : SharedImageBacking(mailbox,
format, format,
size, size,
...@@ -68,7 +142,7 @@ class SharedImageBackingAHardwareBuffer : public SharedImageBacking { ...@@ -68,7 +142,7 @@ class SharedImageBackingAHardwareBuffer : public SharedImageBacking {
DCHECK(hardware_buffer_handle_.is_valid()); DCHECK(hardware_buffer_handle_.is_valid());
} }
~SharedImageBackingAHardwareBuffer() override { ~SharedImageBackingAHB() override {
// Check to make sure buffer is explicitly destroyed using Destroy() api // Check to make sure buffer is explicitly destroyed using Destroy() api
// before this destructor is called. // before this destructor is called.
DCHECK(!hardware_buffer_handle_.is_valid()); DCHECK(!hardware_buffer_handle_.is_valid());
...@@ -117,21 +191,39 @@ class SharedImageBackingAHardwareBuffer : public SharedImageBacking { ...@@ -117,21 +191,39 @@ class SharedImageBackingAHardwareBuffer : public SharedImageBacking {
return nullptr; return nullptr;
DCHECK(texture_); DCHECK(texture_);
return std::make_unique<SharedImageRepresentationGLTextureAHardwareBuffer>( return std::make_unique<SharedImageRepresentationGLTextureAHB>(
manager, this, tracker, texture_); manager, this, tracker, texture_);
} }
std::unique_ptr<SharedImageRepresentationSkia> ProduceSkia(
SharedImageManager* manager,
MemoryTypeTracker* tracker) override {
// TODO(vikassoni): Currently we only have a GL backed skia representation.
// Follow up patch will add support to check whether we are in Vulkan mode
// OR GL mode and accordingly create Skia representation.
if (!GenGLTexture())
return nullptr;
DCHECK(texture_);
return std::make_unique<SharedImageRepresentationSkiaGLAHB>(
manager, this, tracker, texture_->target(), internal_format_,
driver_internal_format_, texture_->service_id());
}
private: private:
bool GenGLTexture() { bool GenGLTexture() {
if (texture_) if (texture_)
return true; return true;
DCHECK(hardware_buffer_handle_.is_valid()); DCHECK(hardware_buffer_handle_.is_valid());
DCHECK(usage() & SHARED_IMAGE_USAGE_GLES2);
// Target for AHB backed egl images. // Target for AHB backed egl images.
GLenum target = GL_TEXTURE_EXTERNAL_OES; // Note that we are not using GL_TEXTURE_EXTERNAL_OES target since sksurface
GLenum get_target = GL_TEXTURE_BINDING_EXTERNAL_OES; // doesnt 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.
GLenum target = GL_TEXTURE_2D;
GLenum get_target = GL_TEXTURE_BINDING_2D;
// Create a gles2 texture using the AhardwareBuffer. // Create a gles2 texture using the AhardwareBuffer.
gl::GLApi* api = gl::g_current_gl_context; gl::GLApi* api = gl::g_current_gl_context;
...@@ -182,6 +274,9 @@ class SharedImageBackingAHardwareBuffer : public SharedImageBacking { ...@@ -182,6 +274,9 @@ class SharedImageBackingAHardwareBuffer : public SharedImageBacking {
texture_->SetLevelImage(target, 0, egl_image.get(), gles2::Texture::BOUND); texture_->SetLevelImage(target, 0, egl_image.get(), gles2::Texture::BOUND);
texture_->SetImmutable(true); texture_->SetImmutable(true);
api->glBindTextureFn(target, old_texture_binding); api->glBindTextureFn(target, old_texture_binding);
internal_format_ = egl_image->GetInternalFormat();
driver_internal_format_ = gl::GetInternalFormat(
gl::GLContext::GetCurrent()->GetVersionInfo(), internal_format_);
return true; return true;
} }
...@@ -190,6 +285,8 @@ class SharedImageBackingAHardwareBuffer : public SharedImageBacking { ...@@ -190,6 +285,8 @@ class SharedImageBackingAHardwareBuffer : public SharedImageBacking {
// This texture will be lazily initialised/created when ProduceGLTexture is // This texture will be lazily initialised/created when ProduceGLTexture is
// called. // called.
gles2::Texture* texture_ = nullptr; gles2::Texture* texture_ = nullptr;
GLenum internal_format_ = 0;
GLenum driver_internal_format_ = 0;
// TODO(vikassoni): In future when we add begin/end write support, we will // TODO(vikassoni): In future when we add begin/end write support, we will
// need to properly use this flag to pass the is_cleared_ information to // need to properly use this flag to pass the is_cleared_ information to
...@@ -199,20 +296,19 @@ class SharedImageBackingAHardwareBuffer : public SharedImageBacking { ...@@ -199,20 +296,19 @@ class SharedImageBackingAHardwareBuffer : public SharedImageBacking {
// texture representation. // texture representation.
bool is_cleared_ = false; bool is_cleared_ = false;
DISALLOW_COPY_AND_ASSIGN(SharedImageBackingAHardwareBuffer); DISALLOW_COPY_AND_ASSIGN(SharedImageBackingAHB);
}; };
SharedImageBackingFactoryAHardwareBuffer:: SharedImageBackingFactoryAHB::SharedImageBackingFactoryAHB(
SharedImageBackingFactoryAHardwareBuffer( const GpuDriverBugWorkarounds& workarounds,
const GpuDriverBugWorkarounds& workarounds, const GpuFeatureInfo& gpu_feature_info) {
const GpuFeatureInfo& gpu_feature_info) {
scoped_refptr<gles2::FeatureInfo> feature_info = scoped_refptr<gles2::FeatureInfo> feature_info =
new gles2::FeatureInfo(workarounds, gpu_feature_info); new gles2::FeatureInfo(workarounds, gpu_feature_info);
feature_info->Initialize(ContextType::CONTEXT_TYPE_OPENGLES2, false, feature_info->Initialize(ContextType::CONTEXT_TYPE_OPENGLES2, false,
gles2::DisallowedFeatures()); gles2::DisallowedFeatures());
const gles2::Validators* validators = feature_info->validators(); const gles2::Validators* validators = feature_info->validators();
const bool is_egl_image_external_supported = const bool is_egl_image_supported =
feature_info->feature_flags().oes_egl_image_external; gl::g_current_gl_driver->ext.b_GL_OES_EGL_image;
// Build the feature info for all the resource formats. // Build the feature info for all the resource formats.
for (int i = 0; i <= viz::RESOURCE_FORMAT_MAX; ++i) { for (int i = 0; i <= viz::RESOURCE_FORMAT_MAX; ++i) {
...@@ -223,12 +319,13 @@ SharedImageBackingFactoryAHardwareBuffer:: ...@@ -223,12 +319,13 @@ SharedImageBackingFactoryAHardwareBuffer::
// backing. // backing.
if (!AHardwareBufferSupportedFormat(format)) if (!AHardwareBufferSupportedFormat(format))
continue; continue;
info.ahb_supported = true; info.ahb_supported = true;
info.ahb_format = AHardwareBufferFormat(format); info.ahb_format = AHardwareBufferFormat(format);
// Check if GL_TEXTURE_EXTERNAL_OES texture target is supported. This // TODO(vikassoni): In future when we use GL_TEXTURE_EXTERNAL_OES target
// texture target is required to use AHB backed EGLImage as a texture. // with AHB, we need to check if oes_egl_image_external is supported or not.
if (!is_egl_image_external_supported) if (!is_egl_image_supported)
continue; continue;
// Check if AHB backed GL texture can be created using this format and // Check if AHB backed GL texture can be created using this format and
...@@ -273,11 +370,10 @@ SharedImageBackingFactoryAHardwareBuffer:: ...@@ -273,11 +370,10 @@ SharedImageBackingFactoryAHardwareBuffer::
} }
} }
SharedImageBackingFactoryAHardwareBuffer:: SharedImageBackingFactoryAHB::~SharedImageBackingFactoryAHB() = default;
~SharedImageBackingFactoryAHardwareBuffer() = default;
std::unique_ptr<SharedImageBacking> std::unique_ptr<SharedImageBacking>
SharedImageBackingFactoryAHardwareBuffer::CreateSharedImage( SharedImageBackingFactoryAHB::CreateSharedImage(
const Mailbox& mailbox, const Mailbox& mailbox,
viz::ResourceFormat format, viz::ResourceFormat format,
const gfx::Size& size, const gfx::Size& size,
...@@ -294,9 +390,14 @@ SharedImageBackingFactoryAHardwareBuffer::CreateSharedImage( ...@@ -294,9 +390,14 @@ SharedImageBackingFactoryAHardwareBuffer::CreateSharedImage(
return nullptr; return nullptr;
} }
// TODO(vikassoni): Also check for !gpu_preferences.enable_vulkan and maybe // SHARED_IMAGE_USAGE_RASTER is set when we want to write on Skia
// some other usage flags. // representation and SHARED_IMAGE_USAGE_DISPLAY is used for cases we want to
const bool use_gles2 = usage & SHARED_IMAGE_USAGE_GLES2; // read from skia representation.
// TODO(vikassoni): Also check gpu_preferences.enable_vulkan to figure out if
// skia is using vulkan backing or GL backing.
const bool use_gles2 =
(usage & (SHARED_IMAGE_USAGE_GLES2 | SHARED_IMAGE_USAGE_RASTER |
SHARED_IMAGE_USAGE_DISPLAY));
// If usage flags indicated this backing can be used as a GL texture, then do // If usage flags indicated this backing can be used as a GL texture, then do
// below gl related checks. // below gl related checks.
...@@ -356,17 +457,17 @@ SharedImageBackingFactoryAHardwareBuffer::CreateSharedImage( ...@@ -356,17 +457,17 @@ SharedImageBackingFactoryAHardwareBuffer::CreateSharedImage(
return nullptr; return nullptr;
} }
auto backing = std::make_unique<SharedImageBackingAHardwareBuffer>( auto backing = std::make_unique<SharedImageBackingAHB>(
mailbox, format, size, color_space, usage, mailbox, format, size, color_space, usage,
base::android::ScopedHardwareBufferHandle::Adopt(buffer), estimated_size); base::android::ScopedHardwareBufferHandle::Adopt(buffer), estimated_size);
return backing; return backing;
} }
SharedImageBackingFactoryAHardwareBuffer::FormatInfo::FormatInfo() = default; SharedImageBackingFactoryAHB::FormatInfo::FormatInfo() = default;
SharedImageBackingFactoryAHardwareBuffer::FormatInfo::~FormatInfo() = default; SharedImageBackingFactoryAHB::FormatInfo::~FormatInfo() = default;
std::unique_ptr<SharedImageBacking> std::unique_ptr<SharedImageBacking>
SharedImageBackingFactoryAHardwareBuffer::CreateSharedImage( SharedImageBackingFactoryAHB::CreateSharedImage(
const Mailbox& mailbox, const Mailbox& mailbox,
int client_id, int client_id,
gfx::GpuMemoryBufferHandle handle, gfx::GpuMemoryBufferHandle handle,
......
...@@ -24,13 +24,12 @@ struct Mailbox; ...@@ -24,13 +24,12 @@ struct Mailbox;
// Implementation of SharedImageBackingFactory that produces AHardwareBuffer // Implementation of SharedImageBackingFactory that produces AHardwareBuffer
// backed SharedImages. This is meant to be used on Android only. // backed SharedImages. This is meant to be used on Android only.
class GPU_GLES2_EXPORT SharedImageBackingFactoryAHardwareBuffer class GPU_GLES2_EXPORT SharedImageBackingFactoryAHB
: public SharedImageBackingFactory { : public SharedImageBackingFactory {
public: public:
SharedImageBackingFactoryAHardwareBuffer( SharedImageBackingFactoryAHB(const GpuDriverBugWorkarounds& workarounds,
const GpuDriverBugWorkarounds& workarounds, const GpuFeatureInfo& gpu_feature_info);
const GpuFeatureInfo& gpu_feature_info); ~SharedImageBackingFactoryAHB() override;
~SharedImageBackingFactoryAHardwareBuffer() override;
// SharedImageBackingFactory implementation. // SharedImageBackingFactory implementation.
std::unique_ptr<SharedImageBacking> CreateSharedImage( std::unique_ptr<SharedImageBacking> CreateSharedImage(
...@@ -72,7 +71,7 @@ class GPU_GLES2_EXPORT SharedImageBackingFactoryAHardwareBuffer ...@@ -72,7 +71,7 @@ class GPU_GLES2_EXPORT SharedImageBackingFactoryAHardwareBuffer
// Used to limit the max size of AHardwareBuffer. // Used to limit the max size of AHardwareBuffer.
int32_t max_gl_texture_size_ = 0; int32_t max_gl_texture_size_ = 0;
DISALLOW_COPY_AND_ASSIGN(SharedImageBackingFactoryAHardwareBuffer); DISALLOW_COPY_AND_ASSIGN(SharedImageBackingFactoryAHB);
}; };
} // namespace gpu } // namespace gpu
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "gpu/command_buffer/common/mailbox.h" #include "gpu/command_buffer/common/mailbox.h"
#include "gpu/command_buffer/common/shared_image_usage.h" #include "gpu/command_buffer/common/shared_image_usage.h"
#include "gpu/command_buffer/service/mailbox_manager_impl.h" #include "gpu/command_buffer/service/mailbox_manager_impl.h"
#include "gpu/command_buffer/service/raster_decoder_context_state.h"
#include "gpu/command_buffer/service/shared_image_backing.h" #include "gpu/command_buffer/service/shared_image_backing.h"
#include "gpu/command_buffer/service/shared_image_factory.h" #include "gpu/command_buffer/service/shared_image_factory.h"
#include "gpu/command_buffer/service/shared_image_manager.h" #include "gpu/command_buffer/service/shared_image_manager.h"
...@@ -15,16 +16,20 @@ ...@@ -15,16 +16,20 @@
#include "gpu/command_buffer/service/texture_manager.h" #include "gpu/command_buffer/service/texture_manager.h"
#include "gpu/config/gpu_driver_bug_workarounds.h" #include "gpu/config/gpu_driver_bug_workarounds.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "third_party/skia/include/gpu/GrBackendSurface.h"
#include "ui/gfx/color_space.h" #include "ui/gfx/color_space.h"
#include "ui/gl/gl_bindings.h" #include "ui/gl/gl_bindings.h"
#include "ui/gl/gl_context.h" #include "ui/gl/gl_context.h"
#include "ui/gl/gl_gl_api_implementation.h"
#include "ui/gl/gl_surface.h" #include "ui/gl/gl_surface.h"
#include "ui/gl/init/gl_factory.h" #include "ui/gl/init/gl_factory.h"
namespace gpu { namespace gpu {
namespace { namespace {
class SharedImageBackingFactoryAHardwareBufferTest : public testing::Test { class SharedImageBackingFactoryAHBTest : public testing::Test {
public: public:
void SetUp() override { void SetUp() override {
// AHardwareBuffer is only supported on ANDROID O+. Hence these tests // AHardwareBuffer is only supported on ANDROID O+. Hence these tests
...@@ -42,9 +47,14 @@ class SharedImageBackingFactoryAHardwareBufferTest : public testing::Test { ...@@ -42,9 +47,14 @@ class SharedImageBackingFactoryAHardwareBufferTest : public testing::Test {
GpuDriverBugWorkarounds workarounds; GpuDriverBugWorkarounds workarounds;
workarounds.max_texture_size = INT_MAX - 1; workarounds.max_texture_size = INT_MAX - 1;
backing_factory_ = backing_factory_ = std::make_unique<SharedImageBackingFactoryAHB>(
std::make_unique<SharedImageBackingFactoryAHardwareBuffer>( workarounds, GpuFeatureInfo());
workarounds, GpuFeatureInfo());
scoped_refptr<gl::GLShareGroup> share_group = new gl::GLShareGroup();
context_state_ = new raster::RasterDecoderContextState(
std::move(share_group), surface_, context_,
false /* use_virtualized_gl_contexts */);
context_state_->InitializeGrContext(workarounds, nullptr);
memory_type_tracker_ = std::make_unique<MemoryTypeTracker>(nullptr); memory_type_tracker_ = std::make_unique<MemoryTypeTracker>(nullptr);
shared_image_representation_factory_ = shared_image_representation_factory_ =
...@@ -52,10 +62,13 @@ class SharedImageBackingFactoryAHardwareBufferTest : public testing::Test { ...@@ -52,10 +62,13 @@ class SharedImageBackingFactoryAHardwareBufferTest : public testing::Test {
&shared_image_manager_, nullptr); &shared_image_manager_, nullptr);
} }
GrContext* gr_context() { return context_state_->gr_context; }
protected: protected:
scoped_refptr<gl::GLSurface> surface_; scoped_refptr<gl::GLSurface> surface_;
scoped_refptr<gl::GLContext> context_; scoped_refptr<gl::GLContext> context_;
std::unique_ptr<SharedImageBackingFactoryAHardwareBuffer> backing_factory_; scoped_refptr<raster::RasterDecoderContextState> context_state_;
std::unique_ptr<SharedImageBackingFactoryAHB> backing_factory_;
gles2::MailboxManagerImpl mailbox_manager_; gles2::MailboxManagerImpl mailbox_manager_;
SharedImageManager shared_image_manager_; SharedImageManager shared_image_manager_;
std::unique_ptr<MemoryTypeTracker> memory_type_tracker_; std::unique_ptr<MemoryTypeTracker> memory_type_tracker_;
...@@ -64,7 +77,7 @@ class SharedImageBackingFactoryAHardwareBufferTest : public testing::Test { ...@@ -64,7 +77,7 @@ class SharedImageBackingFactoryAHardwareBufferTest : public testing::Test {
}; };
// Basic test to check creation and deletion of AHB backed shared image. // Basic test to check creation and deletion of AHB backed shared image.
TEST_F(SharedImageBackingFactoryAHardwareBufferTest, Basic) { TEST_F(SharedImageBackingFactoryAHBTest, Basic) {
if (!base::AndroidHardwareBufferCompat::IsSupportAvailable()) if (!base::AndroidHardwareBufferCompat::IsSupportAvailable())
return; return;
...@@ -72,7 +85,11 @@ TEST_F(SharedImageBackingFactoryAHardwareBufferTest, Basic) { ...@@ -72,7 +85,11 @@ TEST_F(SharedImageBackingFactoryAHardwareBufferTest, Basic) {
auto format = viz::ResourceFormat::RGBA_8888; auto format = viz::ResourceFormat::RGBA_8888;
gfx::Size size(256, 256); gfx::Size size(256, 256);
auto color_space = gfx::ColorSpace::CreateSRGB(); auto color_space = gfx::ColorSpace::CreateSRGB();
uint32_t usage = SHARED_IMAGE_USAGE_GLES2;
// SHARED_IMAGE_USAGE_DISPLAY for skia read and SHARED_IMAGE_USAGE_RASTER for
// skia write.
uint32_t usage = SHARED_IMAGE_USAGE_GLES2 | SHARED_IMAGE_USAGE_RASTER |
SHARED_IMAGE_USAGE_DISPLAY;
auto backing = backing_factory_->CreateSharedImage(mailbox, format, size, auto backing = backing_factory_->CreateSharedImage(mailbox, format, size,
color_space, usage); color_space, usage);
EXPECT_TRUE(backing); EXPECT_TRUE(backing);
...@@ -84,7 +101,7 @@ TEST_F(SharedImageBackingFactoryAHardwareBufferTest, Basic) { ...@@ -84,7 +101,7 @@ TEST_F(SharedImageBackingFactoryAHardwareBufferTest, Basic) {
} }
// First, validate via a legacy mailbox. // First, validate via a legacy mailbox.
GLenum expected_target = GL_TEXTURE_EXTERNAL_OES; GLenum expected_target = GL_TEXTURE_2D;
EXPECT_TRUE(backing->ProduceLegacyMailbox(&mailbox_manager_)); EXPECT_TRUE(backing->ProduceLegacyMailbox(&mailbox_manager_));
TextureBase* texture_base = mailbox_manager_.ConsumeTexture(mailbox); TextureBase* texture_base = mailbox_manager_.ConsumeTexture(mailbox);
...@@ -116,12 +133,115 @@ TEST_F(SharedImageBackingFactoryAHardwareBufferTest, Basic) { ...@@ -116,12 +133,115 @@ TEST_F(SharedImageBackingFactoryAHardwareBufferTest, Basic) {
EXPECT_EQ(usage, gl_representation->usage()); EXPECT_EQ(usage, gl_representation->usage());
gl_representation.reset(); gl_representation.reset();
// Finally, validate a SharedImageRepresentationSkia.
auto skia_representation =
shared_image_representation_factory_->ProduceSkia(mailbox);
EXPECT_TRUE(skia_representation);
auto surface = skia_representation->BeginWriteAccess(
gr_context(), 0, kRGBA_8888_SkColorType,
SkSurfaceProps(0, kUnknown_SkPixelGeometry));
EXPECT_TRUE(surface);
EXPECT_EQ(size.width(), surface->width());
EXPECT_EQ(size.height(), surface->height());
skia_representation->EndWriteAccess(std::move(surface));
GrBackendTexture backend_texture;
EXPECT_TRUE(skia_representation->BeginReadAccess(
kRGBA_8888_SkColorType, &backend_texture));
EXPECT_EQ(size.width(), backend_texture.width());
EXPECT_EQ(size.width(), backend_texture.width());
skia_representation->EndReadAccess();
skia_representation.reset();
factory_ref.reset();
EXPECT_FALSE(mailbox_manager_.ConsumeTexture(mailbox));
}
// Test to check interaction between Gl and skia GL representations.
// We write to a GL texture using gl representation and then read from skia
// representation.
TEST_F(SharedImageBackingFactoryAHBTest, GLSkiaGL) {
if (!base::AndroidHardwareBufferCompat::IsSupportAvailable())
return;
// Create a backing using mailbox.
auto mailbox = Mailbox::Generate();
auto format = viz::ResourceFormat::RGBA_8888;
gfx::Size size(1, 1);
auto color_space = gfx::ColorSpace::CreateSRGB();
uint32_t usage = SHARED_IMAGE_USAGE_GLES2 | SHARED_IMAGE_USAGE_DISPLAY;
auto backing = backing_factory_->CreateSharedImage(mailbox, format, size,
color_space, usage);
EXPECT_TRUE(backing);
GLenum expected_target = GL_TEXTURE_2D;
std::unique_ptr<SharedImageRepresentationFactoryRef> factory_ref =
shared_image_manager_.Register(std::move(backing),
memory_type_tracker_.get());
// Create a SharedImageRepresentationGLTexture.
auto gl_representation =
shared_image_representation_factory_->ProduceGLTexture(mailbox);
EXPECT_TRUE(gl_representation);
EXPECT_EQ(expected_target, gl_representation->GetTexture()->target());
// Create an FBO.
GLuint fbo = 0;
gl::GLApi* api = gl::g_current_gl_context;
api->glGenFramebuffersEXTFn(1, &fbo);
api->glBindFramebufferEXTFn(GL_FRAMEBUFFER, fbo);
// Attach the texture to FBO.
api->glFramebufferTexture2DEXTFn(
GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
gl_representation->GetTexture()->target(),
gl_representation->GetTexture()->service_id(), 0);
// Set the clear color to green.
api->glClearColorFn(0.0f, 1.0f, 0.0f, 1.0f);
api->glClearFn(GL_COLOR_BUFFER_BIT);
gl_representation.reset();
// Next create a SharedImageRepresentationSkia to read back the texture data.
auto skia_representation =
shared_image_representation_factory_->ProduceSkia(mailbox);
EXPECT_TRUE(skia_representation);
GrBackendTexture backend_texture;
EXPECT_TRUE(skia_representation->BeginReadAccess(kRGBA_8888_SkColorType,
&backend_texture));
EXPECT_EQ(size.width(), backend_texture.width());
EXPECT_EQ(size.width(), backend_texture.width());
// Create an Sk Image from GrBackendTexture.
auto sk_image = SkImage::MakeFromTexture(
gr_context(), backend_texture, kTopLeft_GrSurfaceOrigin,
kRGBA_8888_SkColorType, kOpaque_SkAlphaType, nullptr);
SkImageInfo dst_info =
SkImageInfo::Make(size.width(), size.height(), kRGBA_8888_SkColorType,
kOpaque_SkAlphaType, nullptr);
const int num_pixels = size.width() * size.height();
std::unique_ptr<uint8_t[]> dst_pixels(new uint8_t[num_pixels * 4]());
// Read back pixels from Sk Image.
EXPECT_TRUE(sk_image->readPixels(dst_info, dst_pixels.get(),
dst_info.minRowBytes(), 0, 0));
skia_representation->EndReadAccess();
// Compare the pixel values.
EXPECT_EQ(dst_pixels[0], 0);
EXPECT_EQ(dst_pixels[1], 255);
EXPECT_EQ(dst_pixels[2], 0);
EXPECT_EQ(dst_pixels[3], 255);
skia_representation.reset();
factory_ref.reset(); factory_ref.reset();
EXPECT_FALSE(mailbox_manager_.ConsumeTexture(mailbox)); EXPECT_FALSE(mailbox_manager_.ConsumeTexture(mailbox));
} }
// Test to check invalid format support. // Test to check invalid format support.
TEST_F(SharedImageBackingFactoryAHardwareBufferTest, InvalidFormat) { TEST_F(SharedImageBackingFactoryAHBTest, InvalidFormat) {
if (!base::AndroidHardwareBufferCompat::IsSupportAvailable()) if (!base::AndroidHardwareBufferCompat::IsSupportAvailable())
return; return;
...@@ -136,7 +256,7 @@ TEST_F(SharedImageBackingFactoryAHardwareBufferTest, InvalidFormat) { ...@@ -136,7 +256,7 @@ TEST_F(SharedImageBackingFactoryAHardwareBufferTest, InvalidFormat) {
} }
// Test to check invalid size support. // Test to check invalid size support.
TEST_F(SharedImageBackingFactoryAHardwareBufferTest, InvalidSize) { TEST_F(SharedImageBackingFactoryAHBTest, InvalidSize) {
if (!base::AndroidHardwareBufferCompat::IsSupportAvailable()) if (!base::AndroidHardwareBufferCompat::IsSupportAvailable())
return; return;
...@@ -155,7 +275,7 @@ TEST_F(SharedImageBackingFactoryAHardwareBufferTest, InvalidSize) { ...@@ -155,7 +275,7 @@ TEST_F(SharedImageBackingFactoryAHardwareBufferTest, InvalidSize) {
EXPECT_FALSE(backing); EXPECT_FALSE(backing);
} }
TEST_F(SharedImageBackingFactoryAHardwareBufferTest, EstimatedSize) { TEST_F(SharedImageBackingFactoryAHBTest, EstimatedSize) {
if (!base::AndroidHardwareBufferCompat::IsSupportAvailable()) if (!base::AndroidHardwareBufferCompat::IsSupportAvailable())
return; return;
......
...@@ -97,7 +97,7 @@ bool SharedImageFactory::CreateSharedImage(const Mailbox& mailbox, ...@@ -97,7 +97,7 @@ bool SharedImageFactory::CreateSharedImage(const Mailbox& mailbox,
const gfx::ColorSpace& color_space, const gfx::ColorSpace& color_space,
uint32_t usage) { uint32_t usage) {
// TODO(piman): depending on handle.type, choose platform-specific backing // TODO(piman): depending on handle.type, choose platform-specific backing
// factory, e.g. SharedImageBackingFactoryAHardwareBuffer. // factory, e.g. SharedImageBackingFactoryAHB.
std::unique_ptr<SharedImageBacking> backing = std::unique_ptr<SharedImageBacking> backing =
backing_factory_->CreateSharedImage(mailbox, client_id, std::move(handle), backing_factory_->CreateSharedImage(mailbox, client_id, std::move(handle),
format, surface_handle, size, format, surface_handle, size,
......
...@@ -37,7 +37,7 @@ class DecoderContext; ...@@ -37,7 +37,7 @@ class DecoderContext;
class ServiceDiscardableManager; class ServiceDiscardableManager;
class SharedImageBackingGLTexture; class SharedImageBackingGLTexture;
class SharedImageBackingFactoryGLTexture; class SharedImageBackingFactoryGLTexture;
class SharedImageBackingAHardwareBuffer; class SharedImageBackingAHB;
class SharedImageRepresentationGLTexture; class SharedImageRepresentationGLTexture;
namespace gles2 { namespace gles2 {
...@@ -355,7 +355,7 @@ class GPU_GLES2_EXPORT Texture final : public TextureBase { ...@@ -355,7 +355,7 @@ class GPU_GLES2_EXPORT Texture final : public TextureBase {
friend class MailboxManagerTest; friend class MailboxManagerTest;
friend class gpu::SharedImageBackingGLTexture; friend class gpu::SharedImageBackingGLTexture;
friend class gpu::SharedImageBackingFactoryGLTexture; friend class gpu::SharedImageBackingFactoryGLTexture;
friend class gpu::SharedImageBackingAHardwareBuffer; friend class gpu::SharedImageBackingAHB;
friend class TextureDefinition; friend class TextureDefinition;
friend class TextureManager; friend class TextureManager;
friend class TextureRef; friend class TextureRef;
......
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