From 27b337848467b206a0dc99ba7b5a384995050b56 Mon Sep 17 00:00:00 2001 From: v00863305 Date: Mon, 15 Jan 2024 15:17:15 +0300 Subject: [PATCH] [memory] Release recycled video frame resources in background To improve memory reduction by browser application there are possibility to reclaim recycled resources in background from video submitter's resource updater. Recycle plane resources for bilibili.com: 2 video frames I420A 1984*140 -> 1.3 Mb (total) To disable this feature please add the folowing line to NWEB: --disable-features=VideoReclaimResourcesInBackground Change-Id: I89a5a11677509b713870f89db35082bf111e731e Signed-off-by: Volykhin Andrei --- .../graphics/video_frame_resource_provider.cc | 6 +++++ .../graphics/video_frame_resource_provider.h | 4 +++ .../graphics/video_frame_submitter.cc | 26 +++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/blink/renderer/platform/graphics/video_frame_resource_provider.cc b/blink/renderer/platform/graphics/video_frame_resource_provider.cc index 9a645ae00..cb5427b0b 100644 --- a/blink/renderer/platform/graphics/video_frame_resource_provider.cc +++ b/blink/renderer/platform/graphics/video_frame_resource_provider.cc @@ -129,6 +129,12 @@ void VideoFrameResourceProvider::ReleaseFrameResources() { resource_updater_->ReleaseFrameResources(); } +#if BUILDFLAG(IS_OHOS) +void VideoFrameResourceProvider::ClearRecycledResources() { + resource_updater_->ClearRecycledResources(); +} +#endif + void VideoFrameResourceProvider::PrepareSendToParent( const WebVector& resource_ids, WebVector* transferable_resources) { diff --git a/blink/renderer/platform/graphics/video_frame_resource_provider.h b/blink/renderer/platform/graphics/video_frame_resource_provider.h index cf05cf0d3..95dc49d23 100644 --- a/blink/renderer/platform/graphics/video_frame_resource_provider.h +++ b/blink/renderer/platform/graphics/video_frame_resource_provider.h @@ -48,6 +48,10 @@ class PLATFORM_EXPORT VideoFrameResourceProvider { bool is_opaque); virtual void ReleaseFrameResources(); +#if BUILDFLAG(IS_OHOS) + virtual void ClearRecycledResources(); +#endif + // Once the context is lost, we must call Initialize again before we can // continue doing work. void OnContextLost(); diff --git a/blink/renderer/platform/graphics/video_frame_submitter.cc b/blink/renderer/platform/graphics/video_frame_submitter.cc index 2efcabb40..1736e08ce 100644 --- a/blink/renderer/platform/graphics/video_frame_submitter.cc +++ b/blink/renderer/platform/graphics/video_frame_submitter.cc @@ -16,6 +16,7 @@ #include "base/threading/platform_thread.h" #include "base/trace_event/trace_event.h" #include "build/build_config.h" +#include "cc/base/features.h" #include "cc/metrics/video_playback_roughness_reporter.h" #include "components/power_scheduler/power_mode.h" #include "components/power_scheduler/power_mode_arbiter.h" @@ -24,6 +25,7 @@ #include "components/viz/common/resources/resource_id.h" #include "components/viz/common/resources/returned_resource.h" #include "components/viz/common/surfaces/frame_sink_bundle_id.h" +#include "gpu/command_buffer/client/context_support.h" #include "media/base/video_frame.h" #include "media/base/video_types.h" #include "mojo/public/cpp/bindings/remote.h" @@ -52,6 +54,14 @@ BASE_FEATURE(kUseVideoFrameSinkBundle, "UseVideoFrameSinkBundle", base::FEATURE_ENABLED_BY_DEFAULT); +#if BUILDFLAG(IS_OHOS) +// When VideoFrameSubmitter::ReclaimResources() is called in background, +// trigger a release of recycled video frames resources. +BASE_FEATURE(kVideoReclaimResourcesInBackground, + "VideoReclaimResourcesInBackground", + base::FEATURE_ENABLED_BY_DEFAULT); +#endif + // Builds a cc::FrameInfo representing a video frame, which is considered // Compositor-only. cc::FrameInfo CreateFrameInfo(cc::FrameInfo::FrameFinalState final_state) { @@ -494,6 +504,22 @@ void VideoFrameSubmitter::ReclaimResources( WTF::Vector resources) { DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); resource_provider_->ReceiveReturnsFromParent(std::move(resources)); + +#if BUILDFLAG(IS_OHOS) + if (!is_page_visible_ && + base::FeatureList::IsEnabled(kVideoReclaimResourcesInBackground)) { + // We now might be able to release more resources that were held because + // they were exported. + resource_provider_->ClearRecycledResources(); + + // Aggressively flush here to make sure those DeleteSharedImage() calls + // make it to the GPU process to free up the memory. + if (context_provider_) { + context_provider_->ContextGL()->OrderingBarrierCHROMIUM(); + context_provider_->ContextSupport()->FlushPendingWork(); + } + } +#endif } void VideoFrameSubmitter::DidAllocateSharedBitmap( -- Gitee