Skip to content

Commit

Permalink
Add GraphiteCacheController for scheduling graphite cache cleanup
Browse files Browse the repository at this point in the history
Bug: 1472451
Change-Id: I7846235d3f0689e92a203209c6bd0ae582d43a72
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4797836
Reviewed-by: Sunny Sachanandani <sunnyps@chromium.org>
Commit-Queue: Peng Huang <penghuang@chromium.org>
Reviewed-by: Vasiliy Telezhnikov <vasilyt@chromium.org>
Auto-Submit: Peng Huang <penghuang@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1187328}
  • Loading branch information
phuang authored and Chromium LUCI CQ committed Aug 23, 2023
1 parent f03c6c0 commit 819b138
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ gpu::MailboxManager* SkiaOutputSurfaceDependencyWebView::GetMailboxManager() {
}

void SkiaOutputSurfaceDependencyWebView::ScheduleGrContextCleanup() {
shared_context_state_->ScheduleGrContextCleanup();
shared_context_state_->ScheduleSkiaCleanup();
}

scoped_refptr<base::TaskRunner>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ SkiaOutputSurfaceDependencyImpl::GetClientTaskRunner() {
}

void SkiaOutputSurfaceDependencyImpl::ScheduleGrContextCleanup() {
GetSharedContextState()->ScheduleGrContextCleanup();
GetSharedContextState()->ScheduleSkiaCleanup();
}

void SkiaOutputSurfaceDependencyImpl::ScheduleDelayedGPUTaskFromGPUThread(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "base/synchronization/waitable_event.h"
#include "base/system/sys_info.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/thread_checker.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
Expand All @@ -36,6 +37,7 @@
#include "gpu/command_buffer/common/gpu_memory_buffer_support.h"
#include "gpu/command_buffer/common/swap_buffers_complete_params.h"
#include "gpu/command_buffer/common/sync_token.h"
#include "gpu/command_buffer/service/graphite_cache_controller.h"
#include "gpu/command_buffer/service/scheduler.h"
#include "gpu/command_buffer/service/shared_image/shared_image_factory.h"
#include "gpu/command_buffer/service/shared_image/shared_image_format_service_utils.h"
Expand Down Expand Up @@ -131,6 +133,30 @@ gpu::ContextUrl& GetActiveUrl() {
return *active_url;
}

scoped_refptr<gpu::raster::GraphiteCacheController>
GetOrCreateGraphiteCacheController(skgpu::graphite::Recorder* recorder) {
#if DCHECK_IS_ON()
static base::ThreadChecker thread_checker;
#endif
// This method is called on viz thread only.
DCHECK_CALLED_ON_VALID_THREAD(thread_checker);

// All SkiaOutputSurfaceImpl instances share one cache controller, and the
// controller will be released when all SkiaOutputSurfaceImpl instances are
// released, so we use WeakPtr here.
static base::WeakPtr<gpu::raster::GraphiteCacheController>
weak_controller_ptr;
if (weak_controller_ptr) {
return base::WrapRefCounted(weak_controller_ptr.get());
}

auto controller = base::MakeRefCounted<gpu::raster::GraphiteCacheController>(
/*context=*/nullptr, recorder);
weak_controller_ptr = controller->GetWeakPtr();

return controller;
}

} // namespace

SkiaOutputSurfaceImpl::ScopedPaint::ScopedPaint(
Expand Down Expand Up @@ -765,6 +791,10 @@ void SkiaOutputSurfaceImpl::SwapBuffers(OutputSurfaceFrame frame) {
if (reset_ddl_recorder_on_swap_) {
RecreateRootDDLRecorder();
}

if (graphite_cache_controller_) {
graphite_cache_controller_->ScheduleCleanup();
}
}

void SkiaOutputSurfaceImpl::SwapBuffersSkipped(
Expand Down Expand Up @@ -1105,6 +1135,13 @@ bool SkiaOutputSurfaceImpl::Initialize() {
frame_buffer_damage_tracker_.emplace(capabilities_.number_of_buffers);
}
}

// |graphite_recorder_| is used on viz thread, so we get or create cache
// controller for graphite_recorder_ and use it on viz thread.
if (graphite_recorder_) {
graphite_cache_controller_ =
GetOrCreateGraphiteCacheController(graphite_recorder_);
}
return result;
}

Expand Down Expand Up @@ -1142,7 +1179,6 @@ void SkiaOutputSurfaceImpl::InitializeOnGpuThread(
}
graphite_recorder_ =
dependency_->GetSharedContextState()->viz_compositor_graphite_recorder();

*result = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class SharedImageRepresentationFactory;
struct SwapBuffersCompleteParams;
} // namespace gpu

namespace gpu::raster {
class GraphiteCacheController;
} // namespace gpu::raster

namespace skgpu::graphite {
class Recorder;
class Recording;
Expand Down Expand Up @@ -411,6 +415,8 @@ class VIZ_SERVICE_EXPORT SkiaOutputSurfaceImpl : public SkiaOutputSurface {
gpu::GrContextType gr_context_type_ = gpu::GrContextType::kGL;
sk_sp<GrContextThreadSafeProxy> gr_context_thread_safe_;
raw_ptr<skgpu::graphite::Recorder> graphite_recorder_ = nullptr;
scoped_refptr<gpu::raster::GraphiteCacheController>
graphite_cache_controller_;

bool has_set_draw_rectangle_for_frame_ = false;
absl::optional<gfx::Rect> draw_rectangle_;
Expand Down
2 changes: 2 additions & 0 deletions gpu/command_buffer/service/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ target(link_target_type, "gles2_sources") {
"gr_cache_controller.h",
"gr_shader_cache.cc",
"gr_shader_cache.h",
"graphite_cache_controller.cc",
"graphite_cache_controller.h",
"graphite_image_provider.cc",
"graphite_image_provider.h",
"id_manager.cc",
Expand Down
43 changes: 43 additions & 0 deletions gpu/command_buffer/service/graphite_cache_controller.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "gpu/command_buffer/service/graphite_cache_controller.h"

#include "base/functional/callback_helpers.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "third_party/skia/include/gpu/graphite/Context.h"
#include "third_party/skia/include/gpu/graphite/Recorder.h"

namespace gpu::raster {
namespace {
constexpr base::TimeDelta kCleanupDelay = base::Seconds(5);
}

GraphiteCacheController::GraphiteCacheController(
skgpu::graphite::Context* context,
skgpu::graphite::Recorder* recorder)
: context_(context), recorder_(recorder) {
timer_ = std::make_unique<base::RetainingOneShotTimer>(
FROM_HERE, kCleanupDelay,
base::BindRepeating(&GraphiteCacheController::PerformCleanup,
GetWeakPtr()));
}

GraphiteCacheController::~GraphiteCacheController() = default;

void GraphiteCacheController::ScheduleCleanup() {
timer_->Reset();
}

void GraphiteCacheController::PerformCleanup() {
if (context_) {
// TODO(crbug.com/1472451): cleanup resources in context_;
}
if (recorder_) {
// TODO(crbug.com/1472451): cleanup resources in recorder_
}
}

} // namespace gpu::raster
59 changes: 59 additions & 0 deletions gpu/command_buffer/service/graphite_cache_controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef GPU_COMMAND_BUFFER_SERVICE_GRAPHITE_CACHE_CONTROLLER_H_
#define GPU_COMMAND_BUFFER_SERVICE_GRAPHITE_CACHE_CONTROLLER_H_

#include <memory>

#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "gpu/gpu_gles2_export.h"

namespace base {
class RetainingOneShotTimer;
} // namespace base

namespace skgpu::graphite {
class Context;
class Recorder;
} // namespace skgpu::graphite

namespace gpu::raster {

class GPU_GLES2_EXPORT GraphiteCacheController
: public base::RefCounted<GraphiteCacheController> {
public:
// |context| and |recorder| are optional, GraphiteCacheController only purge
// resource in non-null |context| and |recorder|.
GraphiteCacheController(skgpu::graphite::Context* context,
skgpu::graphite::Recorder* recorder);
GraphiteCacheController(const GraphiteCacheController&) = delete;
GraphiteCacheController& operator=(const GraphiteCacheController&) = delete;

base::WeakPtr<GraphiteCacheController> GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}

// Schedule cleanup for the graphite cache, the cleanup will be performed
// until ScheduleCleanup() is called for a while.
void ScheduleCleanup();

private:
friend class base::RefCounted<GraphiteCacheController>;
~GraphiteCacheController();

void PerformCleanup();

const raw_ptr<skgpu::graphite::Context> context_;
const raw_ptr<skgpu::graphite::Recorder> recorder_;
std::unique_ptr<base::RetainingOneShotTimer> timer_;

base::WeakPtrFactory<GraphiteCacheController> weak_factory_{this};
};

} // namespace gpu::raster

#endif // GPU_COMMAND_BUFFER_SERVICE_GRAPHITE_CACHE_CONTROLLER_H_
16 changes: 14 additions & 2 deletions gpu/command_buffer/service/shared_context_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#include "gpu/command_buffer/common/shm_count.h"
#include "gpu/command_buffer/service/context_state.h"
#include "gpu/command_buffer/service/gl_context_virtual.h"
#include "gpu/command_buffer/service/gr_cache_controller.h"
#include "gpu/command_buffer/service/gr_shader_cache.h"
#include "gpu/command_buffer/service/graphite_cache_controller.h"
#include "gpu/command_buffer/service/graphite_image_provider.h"
#include "gpu/command_buffer/service/service_transfer_cache.h"
#include "gpu/command_buffer/service/service_utils.h"
Expand Down Expand Up @@ -378,6 +380,7 @@ bool SharedContextState::InitializeGanesh(

gr_context_->setResourceCacheLimit(max_resource_cache_bytes);
transfer_cache_ = std::make_unique<ServiceTransferCache>(gpu_preferences);
gr_cache_controller_ = std::make_unique<raster::GrCacheController>(this);
return true;
}

Expand Down Expand Up @@ -419,6 +422,10 @@ bool SharedContextState::InitializeGraphite(
// promoted to composited).
gpu_main_graphite_recorder_ =
MakeGraphiteRecorderWithImageProvider(graphite_context_);
gpu_main_graphite_cache_controller_ =
base::MakeRefCounted<raster::GraphiteCacheController>(
graphite_context_.get(), gpu_main_graphite_recorder_.get());

viz_compositor_graphite_recorder_ =
MakeGraphiteRecorderWithImageProvider(graphite_context_);

Expand Down Expand Up @@ -939,8 +946,13 @@ bool SharedContextState::CheckResetStatus(bool need_gl) {
return false;
}

void SharedContextState::ScheduleGrContextCleanup() {
gr_cache_controller_.ScheduleGrContextCleanup();
void SharedContextState::ScheduleSkiaCleanup() {
if (gr_cache_controller_) {
gr_cache_controller_->ScheduleGrContextCleanup();
}
if (gpu_main_graphite_cache_controller_) {
gpu_main_graphite_cache_controller_->ScheduleCleanup();
}
}

int32_t SharedContextState::GetMaxTextureSize() const {
Expand Down
12 changes: 9 additions & 3 deletions gpu/command_buffer/service/shared_context_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "gpu/command_buffer/common/gl2_types.h"
#include "gpu/command_buffer/common/skia_utils.h"
#include "gpu/command_buffer/service/gl_context_virtual_delegate.h"
#include "gpu/command_buffer/service/gr_cache_controller.h"
#include "gpu/command_buffer/service/gr_shader_cache.h"
#include "gpu/command_buffer/service/memory_tracking.h"
#include "gpu/config/gpu_preferences.h"
Expand Down Expand Up @@ -71,7 +70,9 @@ struct ContextState;
} // namespace gles2

namespace raster {
class GrCacheController;
class GrShaderCache;
class GraphiteCacheController;
class RasterDecoderTestBase;
} // namespace raster

Expand Down Expand Up @@ -252,7 +253,7 @@ class GPU_GLES2_EXPORT SharedContextState
bool CheckResetStatus(bool needs_gl);
bool device_needs_reset() { return device_needs_reset_; }

void ScheduleGrContextCleanup();
void ScheduleSkiaCleanup();

int32_t GetMaxTextureSize() const;

Expand Down Expand Up @@ -407,7 +408,12 @@ class GPU_GLES2_EXPORT SharedContextState
std::unique_ptr<ExternalSemaphorePool> external_semaphore_pool_;
#endif

raster::GrCacheController gr_cache_controller_{this};
std::unique_ptr<raster::GrCacheController> gr_cache_controller_;

// The graphite cache controller for |graphite_context_| and
// |gpu_main_graphite_recorder_|.
scoped_refptr<raster::GraphiteCacheController>
gpu_main_graphite_cache_controller_;

base::WeakPtrFactory<SharedContextState> weak_ptr_factory_{this};
};
Expand Down
2 changes: 1 addition & 1 deletion gpu/ipc/in_process_command_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ void InProcessCommandBuffer::OnSwapBuffers(uint64_t swap_id, uint32_t flags) {

void InProcessCommandBuffer::ScheduleGrContextCleanup() {
DCHECK_CALLED_ON_VALID_SEQUENCE(gpu_sequence_checker_);
context_state_->ScheduleGrContextCleanup();
context_state_->ScheduleSkiaCleanup();
}

void InProcessCommandBuffer::HandleReturnData(base::span<const uint8_t> data) {
Expand Down
2 changes: 1 addition & 1 deletion gpu/ipc/service/gpu_channel_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ void GpuChannelManager::OnContextLost(
void GpuChannelManager::ScheduleGrContextCleanup() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);

shared_context_state_->ScheduleGrContextCleanup();
shared_context_state_->ScheduleSkiaCleanup();
}

void GpuChannelManager::StoreShader(const std::string& key,
Expand Down

0 comments on commit 819b138

Please sign in to comment.