Commit 5173392b authored by Christopher Cameron's avatar Christopher Cameron Committed by Commit Bot

macOS/Metal: Fix GL/WebGPU synchronization

This makes SharedImageBackingGLImage work with WebGPU. After
this, it will be possible to delete SharedImageBackingIOSurface.

Merge all of the Client interfaces into a single
SharedImageRepresentationGLTextureClient interface, now that they
all came the same functions.

Bug: 894929
Change-Id: I9990e66a438d97d411788485e244182b640fe9cf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2293618
Commit-Queue: ccameron <ccameron@chromium.org>
Reviewed-by: default avatarGeoff Lang <geofflang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#787842}
parent b8347d7b
...@@ -304,7 +304,7 @@ size_t EstimatedSize(viz::ResourceFormat format, const gfx::Size& size) { ...@@ -304,7 +304,7 @@ size_t EstimatedSize(viz::ResourceFormat format, const gfx::Size& size) {
SharedImageRepresentationGLTextureImpl::SharedImageRepresentationGLTextureImpl( SharedImageRepresentationGLTextureImpl::SharedImageRepresentationGLTextureImpl(
SharedImageManager* manager, SharedImageManager* manager,
SharedImageBacking* backing, SharedImageBacking* backing,
Client* client, SharedImageRepresentationGLTextureClient* client,
MemoryTypeTracker* tracker, MemoryTypeTracker* tracker,
gles2::Texture* texture) gles2::Texture* texture)
: SharedImageRepresentationGLTexture(manager, backing, tracker), : SharedImageRepresentationGLTexture(manager, backing, tracker),
...@@ -316,11 +316,16 @@ gles2::Texture* SharedImageRepresentationGLTextureImpl::GetTexture() { ...@@ -316,11 +316,16 @@ gles2::Texture* SharedImageRepresentationGLTextureImpl::GetTexture() {
} }
bool SharedImageRepresentationGLTextureImpl::BeginAccess(GLenum mode) { bool SharedImageRepresentationGLTextureImpl::BeginAccess(GLenum mode) {
if (client_) if (client_ && mode != GL_SHARED_IMAGE_ACCESS_MODE_OVERLAY_CHROMIUM)
return client_->OnGLTextureBeginAccess(mode); return client_->SharedImageRepresentationGLTextureBeginAccess();
return true; return true;
} }
void SharedImageRepresentationGLTextureImpl::EndAccess() {
if (client_)
return client_->SharedImageRepresentationGLTextureEndAccess();
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// SharedImageRepresentationGLTexturePassthroughImpl // SharedImageRepresentationGLTexturePassthroughImpl
...@@ -328,7 +333,7 @@ SharedImageRepresentationGLTexturePassthroughImpl:: ...@@ -328,7 +333,7 @@ SharedImageRepresentationGLTexturePassthroughImpl::
SharedImageRepresentationGLTexturePassthroughImpl( SharedImageRepresentationGLTexturePassthroughImpl(
SharedImageManager* manager, SharedImageManager* manager,
SharedImageBacking* backing, SharedImageBacking* backing,
Client* client, SharedImageRepresentationGLTextureClient* client,
MemoryTypeTracker* tracker, MemoryTypeTracker* tracker,
scoped_refptr<gles2::TexturePassthrough> texture_passthrough) scoped_refptr<gles2::TexturePassthrough> texture_passthrough)
: SharedImageRepresentationGLTexturePassthrough(manager, backing, tracker), : SharedImageRepresentationGLTexturePassthrough(manager, backing, tracker),
...@@ -345,11 +350,16 @@ SharedImageRepresentationGLTexturePassthroughImpl::GetTexturePassthrough() { ...@@ -345,11 +350,16 @@ SharedImageRepresentationGLTexturePassthroughImpl::GetTexturePassthrough() {
bool SharedImageRepresentationGLTexturePassthroughImpl::BeginAccess( bool SharedImageRepresentationGLTexturePassthroughImpl::BeginAccess(
GLenum mode) { GLenum mode) {
if (client_) if (client_ && mode != GL_SHARED_IMAGE_ACCESS_MODE_OVERLAY_CHROMIUM)
return client_->OnGLTexturePassthroughBeginAccess(mode); return client_->SharedImageRepresentationGLTextureBeginAccess();
return true; return true;
} }
void SharedImageRepresentationGLTexturePassthroughImpl::EndAccess() {
if (client_)
return client_->SharedImageRepresentationGLTextureEndAccess();
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// SharedImageBackingGLCommon // SharedImageBackingGLCommon
...@@ -396,7 +406,7 @@ void SharedImageBackingGLCommon::MakeTextureAndSetParameters( ...@@ -396,7 +406,7 @@ void SharedImageBackingGLCommon::MakeTextureAndSetParameters(
SharedImageRepresentationSkiaImpl::SharedImageRepresentationSkiaImpl( SharedImageRepresentationSkiaImpl::SharedImageRepresentationSkiaImpl(
SharedImageManager* manager, SharedImageManager* manager,
SharedImageBacking* backing, SharedImageBacking* backing,
Client* client, SharedImageRepresentationGLTextureClient* client,
scoped_refptr<SharedContextState> context_state, scoped_refptr<SharedContextState> context_state,
sk_sp<SkPromiseImageTexture> promise_texture, sk_sp<SkPromiseImageTexture> promise_texture,
MemoryTypeTracker* tracker) MemoryTypeTracker* tracker)
...@@ -424,14 +434,18 @@ sk_sp<SkSurface> SharedImageRepresentationSkiaImpl::BeginWriteAccess( ...@@ -424,14 +434,18 @@ sk_sp<SkSurface> SharedImageRepresentationSkiaImpl::BeginWriteAccess(
std::vector<GrBackendSemaphore>* begin_semaphores, std::vector<GrBackendSemaphore>* begin_semaphores,
std::vector<GrBackendSemaphore>* end_semaphores) { std::vector<GrBackendSemaphore>* end_semaphores) {
CheckContext(); CheckContext();
if (client_ && !client_->OnSkiaBeginWriteAccess()) if (client_) {
return nullptr; DCHECK(context_state_->GrContextIsGL());
if (!client_->SharedImageRepresentationGLTextureBeginAccess())
return nullptr;
}
if (write_surface_) if (write_surface_)
return nullptr; return nullptr;
if (!promise_texture_) { if (!promise_texture_)
return nullptr; return nullptr;
}
SkColorType sk_color_type = viz::ResourceFormatToClosestSkColorType( SkColorType sk_color_type = viz::ResourceFormatToClosestSkColorType(
/*gpu_compositing=*/true, format()); /*gpu_compositing=*/true, format());
auto surface = SkSurface::MakeFromBackendTexture( auto surface = SkSurface::MakeFromBackendTexture(
...@@ -449,19 +463,26 @@ void SharedImageRepresentationSkiaImpl::EndWriteAccess( ...@@ -449,19 +463,26 @@ void SharedImageRepresentationSkiaImpl::EndWriteAccess(
CheckContext(); CheckContext();
// TODO(ericrk): Keep the surface around for re-use. // TODO(ericrk): Keep the surface around for re-use.
write_surface_ = nullptr; write_surface_ = nullptr;
if (client_)
client_->SharedImageRepresentationGLTextureEndAccess();
} }
sk_sp<SkPromiseImageTexture> SharedImageRepresentationSkiaImpl::BeginReadAccess( sk_sp<SkPromiseImageTexture> SharedImageRepresentationSkiaImpl::BeginReadAccess(
std::vector<GrBackendSemaphore>* begin_semaphores, std::vector<GrBackendSemaphore>* begin_semaphores,
std::vector<GrBackendSemaphore>* end_semaphores) { std::vector<GrBackendSemaphore>* end_semaphores) {
CheckContext(); CheckContext();
if (client_ && !client_->OnSkiaBeginReadAccess()) if (client_) {
return nullptr; DCHECK(context_state_->GrContextIsGL());
if (!client_->SharedImageRepresentationGLTextureBeginAccess())
return nullptr;
}
return promise_texture_; return promise_texture_;
} }
void SharedImageRepresentationSkiaImpl::EndReadAccess() { void SharedImageRepresentationSkiaImpl::EndReadAccess() {
// TODO(ericrk): Handle begin/end correctness checks. if (client_)
client_->SharedImageRepresentationGLTextureEndAccess();
} }
bool SharedImageRepresentationSkiaImpl::SupportsMultipleConcurrentReadAccess() { bool SharedImageRepresentationSkiaImpl::SupportsMultipleConcurrentReadAccess() {
...@@ -814,6 +835,7 @@ SharedImageBackingGLImage::ProduceSkia( ...@@ -814,6 +835,7 @@ SharedImageBackingGLImage::ProduceSkia(
SharedImageManager* manager, SharedImageManager* manager,
MemoryTypeTracker* tracker, MemoryTypeTracker* tracker,
scoped_refptr<SharedContextState> context_state) { scoped_refptr<SharedContextState> context_state) {
SharedImageRepresentationGLTextureClient* gl_client = nullptr;
if (!cached_promise_texture_) { if (!cached_promise_texture_) {
if (context_state->GrContextIsMetal()) { if (context_state->GrContextIsMetal()) {
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
...@@ -823,6 +845,7 @@ SharedImageBackingGLImage::ProduceSkia( ...@@ -823,6 +845,7 @@ SharedImageBackingGLImage::ProduceSkia(
DCHECK(cached_promise_texture_); DCHECK(cached_promise_texture_);
#endif #endif
} else { } else {
gl_client = this;
GrBackendTexture backend_texture; GrBackendTexture backend_texture;
GetGrBackendTexture(context_state->feature_info(), GetGLTarget(), size(), GetGrBackendTexture(context_state->feature_info(), GetGLTarget(), size(),
GetGLServiceId(), format(), &backend_texture); GetGLServiceId(), format(), &backend_texture);
...@@ -830,8 +853,8 @@ SharedImageBackingGLImage::ProduceSkia( ...@@ -830,8 +853,8 @@ SharedImageBackingGLImage::ProduceSkia(
} }
} }
return std::make_unique<SharedImageRepresentationSkiaImpl>( return std::make_unique<SharedImageRepresentationSkiaImpl>(
manager, this, this, std::move(context_state), cached_promise_texture_, manager, this, gl_client, std::move(context_state),
tracker); cached_promise_texture_, tracker);
} }
std::unique_ptr<SharedImageRepresentationGLTexture> std::unique_ptr<SharedImageRepresentationGLTexture>
...@@ -897,24 +920,20 @@ void SharedImageBackingGLImage::Update( ...@@ -897,24 +920,20 @@ void SharedImageBackingGLImage::Update(
image_bind_or_copy_needed_ = true; image_bind_or_copy_needed_ = true;
} }
bool SharedImageBackingGLImage::OnGLTextureBeginAccess(GLenum mode) { bool SharedImageBackingGLImage::
if (mode == GL_SHARED_IMAGE_ACCESS_MODE_OVERLAY_CHROMIUM) SharedImageRepresentationGLTextureBeginAccess() {
return true;
return BindOrCopyImageIfNeeded();
}
bool SharedImageBackingGLImage::OnGLTexturePassthroughBeginAccess(GLenum mode) {
if (mode == GL_SHARED_IMAGE_ACCESS_MODE_OVERLAY_CHROMIUM)
return true;
return BindOrCopyImageIfNeeded();
}
bool SharedImageBackingGLImage::OnSkiaBeginReadAccess() {
return BindOrCopyImageIfNeeded(); return BindOrCopyImageIfNeeded();
} }
bool SharedImageBackingGLImage::OnSkiaBeginWriteAccess() { void SharedImageBackingGLImage::SharedImageRepresentationGLTextureEndAccess() {
return BindOrCopyImageIfNeeded(); #if defined(OS_MACOSX)
// If this image could potentially be shared with Metal via WebGPU, then flush
// the GL context to ensure Metal will see it.
if (usage() & SHARED_IMAGE_USAGE_WEBGPU) {
gl::GLApi* api = gl::g_current_gl_context;
api->glFlushFn();
}
#endif
} }
bool SharedImageBackingGLImage::BindOrCopyImageIfNeeded() { bool SharedImageBackingGLImage::BindOrCopyImageIfNeeded() {
...@@ -1333,7 +1352,12 @@ SharedImageBackingFactoryGLTexture::CreateSharedImageInternal( ...@@ -1333,7 +1352,12 @@ SharedImageBackingFactoryGLTexture::CreateSharedImageInternal(
return nullptr; return nullptr;
} }
#if defined(OS_MACOSX)
const bool use_buffer =
usage & (SHARED_IMAGE_USAGE_SCANOUT | SHARED_IMAGE_USAGE_WEBGPU);
#else
const bool use_buffer = usage & SHARED_IMAGE_USAGE_SCANOUT; const bool use_buffer = usage & SHARED_IMAGE_USAGE_SCANOUT;
#endif
if (use_buffer && !format_info.allow_scanout) { if (use_buffer && !format_info.allow_scanout) {
LOG(ERROR) << "CreateSharedImage: SCANOUT shared images unavailable"; LOG(ERROR) << "CreateSharedImage: SCANOUT shared images unavailable";
return nullptr; return nullptr;
......
...@@ -11,27 +11,33 @@ ...@@ -11,27 +11,33 @@
namespace gpu { namespace gpu {
// Interface through which a representation that has a GL texture calls into its
// GLImage backing.
class SharedImageRepresentationGLTextureClient {
public:
virtual bool SharedImageRepresentationGLTextureBeginAccess() = 0;
virtual void SharedImageRepresentationGLTextureEndAccess() = 0;
};
// Representation of a SharedImageBackingGLTexture or SharedImageBackingGLImage // Representation of a SharedImageBackingGLTexture or SharedImageBackingGLImage
// as a GL Texture. // as a GL Texture.
class SharedImageRepresentationGLTextureImpl class SharedImageRepresentationGLTextureImpl
: public SharedImageRepresentationGLTexture { : public SharedImageRepresentationGLTexture {
public: public:
class Client { SharedImageRepresentationGLTextureImpl(
public: SharedImageManager* manager,
virtual bool OnGLTextureBeginAccess(GLenum mode) = 0; SharedImageBacking* backing,
}; SharedImageRepresentationGLTextureClient* client,
SharedImageRepresentationGLTextureImpl(SharedImageManager* manager, MemoryTypeTracker* tracker,
SharedImageBacking* backing, gles2::Texture* texture);
Client* client,
MemoryTypeTracker* tracker,
gles2::Texture* texture);
private: private:
// SharedImageRepresentationGLTexture: // SharedImageRepresentationGLTexture:
gles2::Texture* GetTexture() override; gles2::Texture* GetTexture() override;
bool BeginAccess(GLenum mode) override; bool BeginAccess(GLenum mode) override;
void EndAccess() override;
Client* const client_ = nullptr; SharedImageRepresentationGLTextureClient* const client_ = nullptr;
gles2::Texture* texture_; gles2::Texture* texture_;
}; };
...@@ -47,7 +53,7 @@ class SharedImageRepresentationGLTexturePassthroughImpl ...@@ -47,7 +53,7 @@ class SharedImageRepresentationGLTexturePassthroughImpl
SharedImageRepresentationGLTexturePassthroughImpl( SharedImageRepresentationGLTexturePassthroughImpl(
SharedImageManager* manager, SharedImageManager* manager,
SharedImageBacking* backing, SharedImageBacking* backing,
Client* client, SharedImageRepresentationGLTextureClient* client,
MemoryTypeTracker* tracker, MemoryTypeTracker* tracker,
scoped_refptr<gles2::TexturePassthrough> texture_passthrough); scoped_refptr<gles2::TexturePassthrough> texture_passthrough);
~SharedImageRepresentationGLTexturePassthroughImpl() override; ~SharedImageRepresentationGLTexturePassthroughImpl() override;
...@@ -57,8 +63,9 @@ class SharedImageRepresentationGLTexturePassthroughImpl ...@@ -57,8 +63,9 @@ class SharedImageRepresentationGLTexturePassthroughImpl
const scoped_refptr<gles2::TexturePassthrough>& GetTexturePassthrough() const scoped_refptr<gles2::TexturePassthrough>& GetTexturePassthrough()
override; override;
bool BeginAccess(GLenum mode) override; bool BeginAccess(GLenum mode) override;
void EndAccess() override;
Client* const client_ = nullptr; SharedImageRepresentationGLTextureClient* const client_ = nullptr;
scoped_refptr<gles2::TexturePassthrough> texture_passthrough_; scoped_refptr<gles2::TexturePassthrough> texture_passthrough_;
}; };
...@@ -103,7 +110,7 @@ class SharedImageRepresentationSkiaImpl : public SharedImageRepresentationSkia { ...@@ -103,7 +110,7 @@ class SharedImageRepresentationSkiaImpl : public SharedImageRepresentationSkia {
SharedImageRepresentationSkiaImpl( SharedImageRepresentationSkiaImpl(
SharedImageManager* manager, SharedImageManager* manager,
SharedImageBacking* backing, SharedImageBacking* backing,
Client* client, SharedImageRepresentationGLTextureClient* client,
scoped_refptr<SharedContextState> context_state, scoped_refptr<SharedContextState> context_state,
sk_sp<SkPromiseImageTexture> promise_texture, sk_sp<SkPromiseImageTexture> promise_texture,
MemoryTypeTracker* tracker); MemoryTypeTracker* tracker);
...@@ -128,7 +135,7 @@ class SharedImageRepresentationSkiaImpl : public SharedImageRepresentationSkia { ...@@ -128,7 +135,7 @@ class SharedImageRepresentationSkiaImpl : public SharedImageRepresentationSkia {
void CheckContext(); void CheckContext();
Client* const client_ = nullptr; SharedImageRepresentationGLTextureClient* const client_ = nullptr;
scoped_refptr<SharedContextState> context_state_; scoped_refptr<SharedContextState> context_state_;
sk_sp<SkPromiseImageTexture> promise_texture_; sk_sp<SkPromiseImageTexture> promise_texture_;
...@@ -201,9 +208,7 @@ class SharedImageBackingGLTexture : public SharedImageBacking { ...@@ -201,9 +208,7 @@ class SharedImageBackingGLTexture : public SharedImageBacking {
// mailbox implementation. // mailbox implementation.
class SharedImageBackingGLImage class SharedImageBackingGLImage
: public SharedImageBacking, : public SharedImageBacking,
public SharedImageRepresentationGLTextureImpl::Client, public SharedImageRepresentationGLTextureClient {
public SharedImageRepresentationGLTexturePassthroughImpl::Client,
public SharedImageRepresentationSkiaImpl::Client {
public: public:
SharedImageBackingGLImage( SharedImageBackingGLImage(
scoped_refptr<gl::GLImage> image, scoped_refptr<gl::GLImage> image,
...@@ -257,15 +262,9 @@ class SharedImageBackingGLImage ...@@ -257,15 +262,9 @@ class SharedImageBackingGLImage
MemoryTypeTracker* tracker) override; MemoryTypeTracker* tracker) override;
void Update(std::unique_ptr<gfx::GpuFence> in_fence) override; void Update(std::unique_ptr<gfx::GpuFence> in_fence) override;
// SharedImageRepresentationGLTextureImpl::Client: // SharedImageRepresentationGLTextureClient:
bool OnGLTextureBeginAccess(GLenum mode) override; bool SharedImageRepresentationGLTextureBeginAccess() override;
void SharedImageRepresentationGLTextureEndAccess() override;
// SharedImageRepresentationGLTexturePassthroughImpl::Client:
bool OnGLTexturePassthroughBeginAccess(GLenum mode) override;
// SharedImageRepresentationGLTextureImpl::Client:
bool OnSkiaBeginReadAccess() override;
bool OnSkiaBeginWriteAccess() override;
bool IsPassthrough() const { return is_passthrough_; } bool IsPassthrough() const { return is_passthrough_; }
......
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