Commit d6814a3a authored by jchen10's avatar jchen10 Committed by Commit Bot

[WebCodecs] Cleanup of DestroySharedImage in VideoFrame

This makes MailboxTextureBacking keep a reference to MailboxRef and
updates its release SyncToken when MailboxTextureBacking goes out of
life.

Bug: 897297, 1121019, 1131123
Change-Id: Ia121d3c3898eeab89f814eba6de43406871f4c37
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2351452
Commit-Queue: Dale Curtis <dalecurtis@chromium.org>
Reviewed-by: default avatarKhushal <khushalsagar@chromium.org>
Reviewed-by: default avatarDan Sanders <sandersd@chromium.org>
Reviewed-by: default avatarFernando Serboncini <fserb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#810313}
parent 253e72e7
...@@ -402,13 +402,8 @@ ScriptPromise VideoFrame::CreateImageBitmap(ScriptState* script_state, ...@@ -402,13 +402,8 @@ ScriptPromise VideoFrame::CreateImageBitmap(ScriptState* script_state,
auto release_callback = viz::SingleReleaseCallback::Create(base::BindOnce( auto release_callback = viz::SingleReleaseCallback::Create(base::BindOnce(
[](gpu::SharedImageInterface* sii, gpu::Mailbox mailbox, [](gpu::SharedImageInterface* sii, gpu::Mailbox mailbox,
const gpu::SyncToken& sync_token, bool is_lost) { const gpu::SyncToken& sync_token,
// Ideally the SharedImage could be release here this way: bool is_lost) { sii->DestroySharedImage(sync_token, mailbox); },
// sii->DestroySharedImage(sync_token, mailbox);
// But AcceleratedStaticBitmapImage leaks it when
// PaintImageForCurrentFrame() is called by ImageBitmap. So the
// 'sync_token' is not precise to destroy the mailbox.
},
base::Unretained(shared_image_interface), dest_holder.mailbox)); base::Unretained(shared_image_interface), dest_holder.mailbox));
const SkImageInfo sk_image_info = const SkImageInfo sk_image_info =
......
...@@ -1022,6 +1022,8 @@ component("platform") { ...@@ -1022,6 +1022,8 @@ component("platform") {
"graphics/interpolation_space.h", "graphics/interpolation_space.h",
"graphics/logging_canvas.cc", "graphics/logging_canvas.cc",
"graphics/logging_canvas.h", "graphics/logging_canvas.h",
"graphics/mailbox_ref.cc",
"graphics/mailbox_ref.h",
"graphics/mailbox_texture_backing.cc", "graphics/mailbox_texture_backing.cc",
"graphics/mailbox_texture_backing.h", "graphics/mailbox_texture_backing.h",
"graphics/main_thread_mutator_client.cc", "graphics/main_thread_mutator_client.cc",
......
...@@ -18,46 +18,14 @@ ...@@ -18,46 +18,14 @@
#include "third_party/blink/public/platform/web_graphics_context_3d_provider.h" #include "third_party/blink/public/platform/web_graphics_context_3d_provider.h"
#include "third_party/blink/renderer/platform/graphics/canvas_resource_provider.h" #include "third_party/blink/renderer/platform/graphics/canvas_resource_provider.h"
#include "third_party/blink/renderer/platform/graphics/gpu/shared_gpu_context.h" #include "third_party/blink/renderer/platform/graphics/gpu/shared_gpu_context.h"
#include "third_party/blink/renderer/platform/graphics/mailbox_ref.h"
#include "third_party/blink/renderer/platform/graphics/mailbox_texture_backing.h" #include "third_party/blink/renderer/platform/graphics/mailbox_texture_backing.h"
#include "third_party/blink/renderer/platform/graphics/skia/skia_utils.h" #include "third_party/blink/renderer/platform/graphics/skia/skia_utils.h"
#include "third_party/blink/renderer/platform/graphics/unaccelerated_static_bitmap_image.h" #include "third_party/blink/renderer/platform/graphics/unaccelerated_static_bitmap_image.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"
#include "third_party/skia/include/core/SkImage.h" #include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/gpu/GrDirectContext.h" #include "third_party/skia/include/gpu/GrDirectContext.h"
namespace blink { namespace blink {
namespace {
void ReleaseCallbackOnContextThread(
std::unique_ptr<viz::SingleReleaseCallback> callback,
const gpu::SyncToken sync_token) {
callback->Run(sync_token, /* is_lost = */ false);
}
} // namespace
AcceleratedStaticBitmapImage::MailboxRef::MailboxRef(
const gpu::SyncToken& sync_token,
base::PlatformThreadRef context_thread_ref,
scoped_refptr<base::SingleThreadTaskRunner> context_task_runner,
std::unique_ptr<viz::SingleReleaseCallback> release_callback)
: sync_token_(sync_token),
context_thread_ref_(context_thread_ref),
context_task_runner_(std::move(context_task_runner)),
release_callback_(std::move(release_callback)) {
DCHECK(sync_token.HasData());
}
AcceleratedStaticBitmapImage::MailboxRef::~MailboxRef() {
if (context_thread_ref_ == base::PlatformThread::CurrentRef()) {
ReleaseCallbackOnContextThread(std::move(release_callback_), sync_token_);
} else {
context_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&ReleaseCallbackOnContextThread,
std::move(release_callback_), sync_token_));
}
}
// static // static
void AcceleratedStaticBitmapImage::ReleaseTexture(void* ctx) { void AcceleratedStaticBitmapImage::ReleaseTexture(void* ctx) {
...@@ -271,7 +239,8 @@ void AcceleratedStaticBitmapImage::InitializeTextureBacking( ...@@ -271,7 +239,8 @@ void AcceleratedStaticBitmapImage::InitializeTextureBacking(
DCHECK_EQ(shared_image_texture_id, 0u); DCHECK_EQ(shared_image_texture_id, 0u);
skia_context_provider_wrapper_ = context_provider_wrapper; skia_context_provider_wrapper_ = context_provider_wrapper;
texture_backing_ = sk_make_sp<MailboxTextureBacking>( texture_backing_ = sk_make_sp<MailboxTextureBacking>(
mailbox_, sk_image_info_, std::move(context_provider_wrapper)); mailbox_, mailbox_ref_, sk_image_info_,
std::move(context_provider_wrapper));
return; return;
} }
...@@ -318,9 +287,9 @@ void AcceleratedStaticBitmapImage::InitializeTextureBacking( ...@@ -318,9 +287,9 @@ void AcceleratedStaticBitmapImage::InitializeTextureBacking(
if (sk_image) { if (sk_image) {
skia_context_provider_wrapper_ = context_provider_wrapper; skia_context_provider_wrapper_ = context_provider_wrapper;
texture_backing_ = texture_backing_ = sk_make_sp<MailboxTextureBacking>(
sk_make_sp<MailboxTextureBacking>(std::move(sk_image), sk_image_info_, std::move(sk_image), mailbox_ref_, sk_image_info_,
std::move(context_provider_wrapper)); std::move(context_provider_wrapper));
} }
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/threading/thread_checker.h" #include "base/threading/thread_checker.h"
#include "third_party/blink/renderer/platform/graphics/mailbox_ref.h"
#include "third_party/blink/renderer/platform/graphics/static_bitmap_image.h" #include "third_party/blink/renderer/platform/graphics/static_bitmap_image.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread.h" #include "third_party/blink/renderer/platform/scheduler/public/thread.h"
...@@ -120,31 +121,6 @@ class PLATFORM_EXPORT AcceleratedStaticBitmapImage final ...@@ -120,31 +121,6 @@ class PLATFORM_EXPORT AcceleratedStaticBitmapImage final
PaintImage PaintImageForCurrentFrame() override; PaintImage PaintImageForCurrentFrame() override;
private: private:
class MailboxRef : public ThreadSafeRefCounted<MailboxRef> {
public:
MailboxRef(const gpu::SyncToken& sync_token,
base::PlatformThreadRef context_thread_ref,
scoped_refptr<base::SingleThreadTaskRunner> context_task_runner,
std::unique_ptr<viz::SingleReleaseCallback> release_callback);
~MailboxRef();
bool is_cross_thread() const {
return base::PlatformThread::CurrentRef() != context_thread_ref_;
}
void set_sync_token(gpu::SyncToken token) {
DCHECK(sync_token_.HasData());
sync_token_ = token;
}
const gpu::SyncToken& sync_token() const { return sync_token_; }
bool verified_flush() { return sync_token_.verified_flush(); }
private:
gpu::SyncToken sync_token_;
const base::PlatformThreadRef context_thread_ref_;
const scoped_refptr<base::SingleThreadTaskRunner> context_task_runner_;
std::unique_ptr<viz::SingleReleaseCallback> release_callback_;
};
struct ReleaseContext { struct ReleaseContext {
scoped_refptr<MailboxRef> mailbox_ref; scoped_refptr<MailboxRef> mailbox_ref;
GLuint texture_id = 0u; GLuint texture_id = 0u;
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/graphics/mailbox_ref.h"
#include "components/viz/common/resources/single_release_callback.h"
#include "gpu/command_buffer/client/raster_interface.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/web_graphics_context_3d_provider.h"
#include "third_party/blink/renderer/platform/graphics/web_graphics_context_3d_provider_wrapper.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"
namespace blink {
namespace {
void ReleaseCallbackOnContextThread(
std::unique_ptr<viz::SingleReleaseCallback> callback,
const gpu::SyncToken sync_token) {
callback->Run(sync_token, /* is_lost = */ false);
}
} // namespace
MailboxRef::MailboxRef(
const gpu::SyncToken& sync_token,
base::PlatformThreadRef context_thread_ref,
scoped_refptr<base::SingleThreadTaskRunner> context_task_runner,
std::unique_ptr<viz::SingleReleaseCallback> release_callback)
: sync_token_(sync_token),
context_thread_ref_(context_thread_ref),
context_task_runner_(std::move(context_task_runner)),
release_callback_(std::move(release_callback)) {
DCHECK(sync_token.HasData());
}
MailboxRef::~MailboxRef() {
if (context_thread_ref_ == base::PlatformThread::CurrentRef()) {
ReleaseCallbackOnContextThread(std::move(release_callback_), sync_token_);
} else {
context_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&ReleaseCallbackOnContextThread,
std::move(release_callback_), sync_token_));
}
}
} // namespace blink
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_MAILBOX_REF_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_MAILBOX_REF_H_
#include <memory>
#include "base/memory/weak_ptr.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread_checker.h"
#include "gpu/command_buffer/common/sync_token.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread.h"
#include "third_party/blink/renderer/platform/wtf/thread_safe_ref_counted.h"
namespace viz {
class SingleReleaseCallback;
} // namespace viz
namespace blink {
class WebGraphicsContext3DProviderWrapper;
class MailboxRef : public ThreadSafeRefCounted<MailboxRef> {
public:
MailboxRef(const gpu::SyncToken& sync_token,
base::PlatformThreadRef context_thread_ref,
scoped_refptr<base::SingleThreadTaskRunner> context_task_runner,
std::unique_ptr<viz::SingleReleaseCallback> release_callback);
~MailboxRef();
bool is_cross_thread() const {
return base::PlatformThread::CurrentRef() != context_thread_ref_;
}
void set_sync_token(gpu::SyncToken token) {
DCHECK(sync_token_.HasData());
sync_token_ = token;
}
const gpu::SyncToken& sync_token() const { return sync_token_; }
bool verified_flush() { return sync_token_.verified_flush(); }
private:
gpu::SyncToken sync_token_;
const base::PlatformThreadRef context_thread_ref_;
const scoped_refptr<base::SingleThreadTaskRunner> context_task_runner_;
std::unique_ptr<viz::SingleReleaseCallback> release_callback_;
};
} // namespace blink
#endif
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "third_party/blink/renderer/platform/graphics/mailbox_texture_backing.h" #include "third_party/blink/renderer/platform/graphics/mailbox_texture_backing.h"
#include "third_party/blink/renderer/platform/graphics/accelerated_static_bitmap_image.h"
#include "third_party/blink/renderer/platform/graphics/mailbox_ref.h"
#include "third_party/blink/renderer/platform/graphics/skia/skia_utils.h" #include "third_party/blink/renderer/platform/graphics/skia/skia_utils.h"
#include "third_party/blink/renderer/platform/graphics/web_graphics_context_3d_provider_wrapper.h" #include "third_party/blink/renderer/platform/graphics/web_graphics_context_3d_provider_wrapper.h"
...@@ -11,20 +13,36 @@ namespace blink { ...@@ -11,20 +13,36 @@ namespace blink {
MailboxTextureBacking::MailboxTextureBacking( MailboxTextureBacking::MailboxTextureBacking(
sk_sp<SkImage> sk_image, sk_sp<SkImage> sk_image,
scoped_refptr<MailboxRef> mailbox_ref,
const SkImageInfo& info, const SkImageInfo& info,
base::WeakPtr<WebGraphicsContext3DProviderWrapper> context_provider_wrapper) base::WeakPtr<WebGraphicsContext3DProviderWrapper> context_provider_wrapper)
: sk_image_(std::move(sk_image)), : sk_image_(std::move(sk_image)),
mailbox_ref_(std::move(mailbox_ref)),
sk_image_info_(info), sk_image_info_(info),
context_provider_wrapper_(std::move(context_provider_wrapper)) {} context_provider_wrapper_(std::move(context_provider_wrapper)) {}
MailboxTextureBacking::MailboxTextureBacking( MailboxTextureBacking::MailboxTextureBacking(
const gpu::Mailbox& mailbox, const gpu::Mailbox& mailbox,
scoped_refptr<MailboxRef> mailbox_ref,
const SkImageInfo& info, const SkImageInfo& info,
base::WeakPtr<WebGraphicsContext3DProviderWrapper> context_provider_wrapper) base::WeakPtr<WebGraphicsContext3DProviderWrapper> context_provider_wrapper)
: mailbox_(mailbox), : mailbox_(mailbox),
mailbox_ref_(std::move(mailbox_ref)),
sk_image_info_(info), sk_image_info_(info),
context_provider_wrapper_(std::move(context_provider_wrapper)) {} context_provider_wrapper_(std::move(context_provider_wrapper)) {}
MailboxTextureBacking::~MailboxTextureBacking() {
if (context_provider_wrapper_) {
gpu::raster::RasterInterface* ri =
context_provider_wrapper_->ContextProvider()->RasterInterface();
// Update the sync token for MailboxRef.
ri->WaitSyncTokenCHROMIUM(mailbox_ref_->sync_token().GetConstData());
gpu::SyncToken sync_token;
ri->GenUnverifiedSyncTokenCHROMIUM(sync_token.GetData());
mailbox_ref_->set_sync_token(sync_token);
}
}
const SkImageInfo& MailboxTextureBacking::GetSkImageInfo() { const SkImageInfo& MailboxTextureBacking::GetSkImageInfo() {
return sk_image_info_; return sk_image_info_;
} }
......
...@@ -13,19 +13,23 @@ ...@@ -13,19 +13,23 @@
namespace blink { namespace blink {
class WebGraphicsContext3DProviderWrapper; class WebGraphicsContext3DProviderWrapper;
class MailboxRef;
class MailboxTextureBacking : public TextureBacking { class MailboxTextureBacking : public TextureBacking {
public: public:
explicit MailboxTextureBacking( explicit MailboxTextureBacking(
sk_sp<SkImage> sk_image, sk_sp<SkImage> sk_image,
scoped_refptr<MailboxRef> mailbox_ref,
const SkImageInfo& info, const SkImageInfo& info,
base::WeakPtr<WebGraphicsContext3DProviderWrapper> base::WeakPtr<WebGraphicsContext3DProviderWrapper>
context_provider_wrapper); context_provider_wrapper);
explicit MailboxTextureBacking( explicit MailboxTextureBacking(
const gpu::Mailbox& mailbox, const gpu::Mailbox& mailbox,
scoped_refptr<MailboxRef> mailbox_ref,
const SkImageInfo& info, const SkImageInfo& info,
base::WeakPtr<WebGraphicsContext3DProviderWrapper> base::WeakPtr<WebGraphicsContext3DProviderWrapper>
context_provider_wrapper); context_provider_wrapper);
~MailboxTextureBacking() override;
const SkImageInfo& GetSkImageInfo() override; const SkImageInfo& GetSkImageInfo() override;
gpu::Mailbox GetMailbox() const override; gpu::Mailbox GetMailbox() const override;
sk_sp<SkImage> GetAcceleratedSkImage() override; sk_sp<SkImage> GetAcceleratedSkImage() override;
...@@ -40,6 +44,7 @@ class MailboxTextureBacking : public TextureBacking { ...@@ -40,6 +44,7 @@ class MailboxTextureBacking : public TextureBacking {
private: private:
const sk_sp<SkImage> sk_image_; const sk_sp<SkImage> sk_image_;
const gpu::Mailbox mailbox_; const gpu::Mailbox mailbox_;
scoped_refptr<MailboxRef> mailbox_ref_;
const SkImageInfo sk_image_info_; const SkImageInfo sk_image_info_;
base::WeakPtr<WebGraphicsContext3DProviderWrapper> context_provider_wrapper_; base::WeakPtr<WebGraphicsContext3DProviderWrapper> context_provider_wrapper_;
}; };
......
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