Commit 399a7db6 authored by Kramer Ge's avatar Kramer Ge Committed by Commit Bot

Add MemoryTracker in SkiaOutputSurfaceImplOnGpu

Initialize SharedImageRepresentationFactory with non-null MemoryTracker from
SkiaOutputSurfaceImplOnGpu. Said tracker is a sub tracker of
SharedContextState::SharedImageMemoryTracker, which contributes to task manager
GPU Memory column and histograms Memory.GPU.PeakMemoryUsage.

TODO::The new memory tracker should also track SkiaRenderer GPU memory
allocations that are not shared image creations.

Bug: 899905
Change-Id: Idaddbddeba17361e0f4ad258d5bd4ad4b4c1509f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1940559
Commit-Queue: Kramer Ge <fangzhoug@chromium.org>
Reviewed-by: default avatarPeng Huang <penghuang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#720257}
parent 46f3f667
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/optional.h" #include "base/optional.h"
#include "base/synchronization/waitable_event.h" #include "base/synchronization/waitable_event.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/trace_event.h" #include "base/trace_event/trace_event.h"
#include "components/viz/common/features.h" #include "components/viz/common/features.h"
#include "components/viz/common/frame_sinks/copy_output_request.h" #include "components/viz/common/frame_sinks/copy_output_request.h"
...@@ -36,6 +37,7 @@ ...@@ -36,6 +37,7 @@
#include "gpu/command_buffer/service/gles2_cmd_decoder_passthrough.h" #include "gpu/command_buffer/service/gles2_cmd_decoder_passthrough.h"
#include "gpu/command_buffer/service/gr_shader_cache.h" #include "gpu/command_buffer/service/gr_shader_cache.h"
#include "gpu/command_buffer/service/mailbox_manager.h" #include "gpu/command_buffer/service/mailbox_manager.h"
#include "gpu/command_buffer/service/memory_tracking.h"
#include "gpu/command_buffer/service/scheduler.h" #include "gpu/command_buffer/service/scheduler.h"
#include "gpu/command_buffer/service/service_utils.h" #include "gpu/command_buffer/service/service_utils.h"
#include "gpu/command_buffer/service/shared_image_factory.h" #include "gpu/command_buffer/service/shared_image_factory.h"
...@@ -44,6 +46,7 @@ ...@@ -44,6 +46,7 @@
#include "gpu/command_buffer/service/texture_base.h" #include "gpu/command_buffer/service/texture_base.h"
#include "gpu/command_buffer/service/texture_manager.h" #include "gpu/command_buffer/service/texture_manager.h"
#include "gpu/config/gpu_preferences.h" #include "gpu/config/gpu_preferences.h"
#include "gpu/ipc/common/command_buffer_id.h"
#include "gpu/ipc/common/gpu_client_ids.h" #include "gpu/ipc/common/gpu_client_ids.h"
#include "gpu/ipc/common/gpu_surface_lookup.h" #include "gpu/ipc/common/gpu_surface_lookup.h"
#include "gpu/vulkan/buildflags.h" #include "gpu/vulkan/buildflags.h"
...@@ -112,6 +115,42 @@ struct ReadPixelsContext { ...@@ -112,6 +115,42 @@ struct ReadPixelsContext {
gfx::Rect result_rect; gfx::Rect result_rect;
}; };
class SharedImageSubMemoryTracker : public gpu::MemoryTracker {
public:
SharedImageSubMemoryTracker(gpu::CommandBufferId command_buffer_id,
uint64_t client_tracing_id,
Observer* observer)
: command_buffer_id_(command_buffer_id),
client_tracing_id_(client_tracing_id),
observer_(observer) {}
SharedImageSubMemoryTracker(const SharedImageSubMemoryTracker&) = delete;
SharedImageSubMemoryTracker& operator=(const SharedImageSubMemoryTracker&) =
delete;
~SharedImageSubMemoryTracker() override { DCHECK(!size_); }
// MemoryTracker implementation:
void TrackMemoryAllocatedChange(uint64_t delta) override {
uint64_t old_size = size_;
size_ += delta;
DCHECK(observer_);
observer_->OnMemoryAllocatedChange(command_buffer_id_, old_size, size_);
}
uint64_t GetSize() const override { return size_; }
uint64_t ClientTracingId() const override { return client_tracing_id_; }
int ClientId() const override {
return gpu::ChannelIdFromCommandBufferId(command_buffer_id_);
}
uint64_t ContextGroupTracingId() const override {
return command_buffer_id_.GetUnsafeValue();
}
private:
gpu::CommandBufferId command_buffer_id_;
const uint64_t client_tracing_id_;
MemoryTracker::Observer* const observer_;
uint64_t size_ = 0;
};
class CopyOutputResultYUV : public CopyOutputResult { class CopyOutputResultYUV : public CopyOutputResult {
public: public:
CopyOutputResultYUV(const gfx::Rect& rect, CopyOutputResultYUV(const gfx::Rect& rect,
...@@ -282,10 +321,11 @@ scoped_refptr<gpu::SyncPointClientState> CreateSyncPointClientState( ...@@ -282,10 +321,11 @@ scoped_refptr<gpu::SyncPointClientState> CreateSyncPointClientState(
} }
std::unique_ptr<gpu::SharedImageRepresentationFactory> std::unique_ptr<gpu::SharedImageRepresentationFactory>
CreateSharedImageRepresentationFactory(SkiaOutputSurfaceDependency* deps) { CreateSharedImageRepresentationFactory(SkiaOutputSurfaceDependency* deps,
gpu::MemoryTracker* memory_tracker) {
// TODO(https://crbug.com/899905): Use a real MemoryTracker, not nullptr. // TODO(https://crbug.com/899905): Use a real MemoryTracker, not nullptr.
return std::make_unique<gpu::SharedImageRepresentationFactory>( return std::make_unique<gpu::SharedImageRepresentationFactory>(
deps->GetSharedImageManager(), nullptr); deps->GetSharedImageManager(), memory_tracker);
} }
class ScopedSurfaceToTexture { class ScopedSurfaceToTexture {
...@@ -417,6 +457,7 @@ class DirectContextProviderDelegateImpl : public DirectContextProviderDelegate, ...@@ -417,6 +457,7 @@ class DirectContextProviderDelegateImpl : public DirectContextProviderDelegate,
gpu::SharedContextState* context_state, gpu::SharedContextState* context_state,
gpu::MailboxManager* mailbox_manager, gpu::MailboxManager* mailbox_manager,
gpu::SharedImageManager* shared_image_manager, gpu::SharedImageManager* shared_image_manager,
gpu::MemoryTracker* memory_tracker,
scoped_refptr<gpu::SyncPointClientState> sync_point_client_state) scoped_refptr<gpu::SyncPointClientState> sync_point_client_state)
: shared_image_manager_(shared_image_manager), : shared_image_manager_(shared_image_manager),
shared_image_factory_(gpu_preferences, shared_image_factory_(gpu_preferences,
...@@ -426,7 +467,7 @@ class DirectContextProviderDelegateImpl : public DirectContextProviderDelegate, ...@@ -426,7 +467,7 @@ class DirectContextProviderDelegateImpl : public DirectContextProviderDelegate,
mailbox_manager, mailbox_manager,
shared_image_manager, shared_image_manager,
nullptr /* image_factory */, nullptr /* image_factory */,
nullptr /* memory_tracker */, memory_tracker,
true /* is_using_skia_renderer */), true /* is_using_skia_renderer */),
sync_point_client_state_(sync_point_client_state) {} sync_point_client_state_(sync_point_client_state) {}
...@@ -658,8 +699,14 @@ SkiaOutputSurfaceImplOnGpu::SkiaOutputSurfaceImplOnGpu( ...@@ -658,8 +699,14 @@ SkiaOutputSurfaceImplOnGpu::SkiaOutputSurfaceImplOnGpu(
feature_info_(std::move(feature_info)), feature_info_(std::move(feature_info)),
sync_point_client_state_( sync_point_client_state_(
CreateSyncPointClientState(dependency_, sequence_id)), CreateSyncPointClientState(dependency_, sequence_id)),
memory_tracker_(std::make_unique<SharedImageSubMemoryTracker>(
sync_point_client_state_->command_buffer_id(),
base::trace_event::MemoryDumpManager::GetInstance()
->GetTracingProcessId(),
dependency_->GetSharedContextState()->memory_tracker())),
shared_image_representation_factory_( shared_image_representation_factory_(
CreateSharedImageRepresentationFactory(dependency_)), CreateSharedImageRepresentationFactory(dependency_,
memory_tracker_.get())),
vulkan_context_provider_(dependency_->GetVulkanContextProvider()), vulkan_context_provider_(dependency_->GetVulkanContextProvider()),
dawn_context_provider_(dependency_->GetDawnContextProvider()), dawn_context_provider_(dependency_->GetDawnContextProvider()),
renderer_settings_(renderer_settings), renderer_settings_(renderer_settings),
...@@ -1021,6 +1068,7 @@ void SkiaOutputSurfaceImplOnGpu::CopyOutput( ...@@ -1021,6 +1068,7 @@ void SkiaOutputSurfaceImplOnGpu::CopyOutput(
gpu_preferences_, dependency_->GetGpuDriverBugWorkarounds(), gpu_preferences_, dependency_->GetGpuDriverBugWorkarounds(),
dependency_->GetGpuFeatureInfo(), context_state_.get(), dependency_->GetGpuFeatureInfo(), context_state_.get(),
dependency_->GetMailboxManager(), dependency_->GetSharedImageManager(), dependency_->GetMailboxManager(), dependency_->GetSharedImageManager(),
memory_tracker_.get(),
CreateSyncPointClientState(dependency_, sequence_id_)); CreateSyncPointClientState(dependency_, sequence_id_));
context_provider_ = base::MakeRefCounted<DirectContextProvider>( context_provider_ = base::MakeRefCounted<DirectContextProvider>(
context_state_->context(), gl_surface_, supports_alpha_, context_state_->context(), gl_surface_, supports_alpha_,
......
...@@ -265,6 +265,7 @@ class SkiaOutputSurfaceImplOnGpu : public gpu::ImageTransportSurfaceDelegate, ...@@ -265,6 +265,7 @@ class SkiaOutputSurfaceImplOnGpu : public gpu::ImageTransportSurfaceDelegate,
SkiaOutputSurfaceDependency* const dependency_; SkiaOutputSurfaceDependency* const dependency_;
scoped_refptr<gpu::gles2::FeatureInfo> feature_info_; scoped_refptr<gpu::gles2::FeatureInfo> feature_info_;
scoped_refptr<gpu::SyncPointClientState> sync_point_client_state_; scoped_refptr<gpu::SyncPointClientState> sync_point_client_state_;
std::unique_ptr<gpu::MemoryTracker> memory_tracker_;
std::unique_ptr<gpu::SharedImageRepresentationFactory> std::unique_ptr<gpu::SharedImageRepresentationFactory>
shared_image_representation_factory_; shared_image_representation_factory_;
VulkanContextProvider* const vulkan_context_provider_; VulkanContextProvider* const vulkan_context_provider_;
......
...@@ -48,6 +48,29 @@ void SharedContextState::compileError(const char* shader, const char* errors) { ...@@ -48,6 +48,29 @@ void SharedContextState::compileError(const char* shader, const char* errors) {
} }
} }
SharedContextState::MemoryTracker::MemoryTracker(
gpu::MemoryTracker::Observer* peak_memory_monitor)
: peak_memory_monitor_(peak_memory_monitor) {}
SharedContextState::MemoryTracker::~MemoryTracker() {
DCHECK(!size_);
}
void SharedContextState::MemoryTracker::OnMemoryAllocatedChange(
CommandBufferId id,
uint64_t old_size,
uint64_t new_size) {
uint64_t delta = new_size - old_size;
old_size = size_;
size_ += delta;
if (peak_memory_monitor_)
peak_memory_monitor_->OnMemoryAllocatedChange(id, old_size, size_);
}
uint64_t SharedContextState::MemoryTracker::GetMemoryUsage() const {
return size_;
}
SharedContextState::SharedContextState( SharedContextState::SharedContextState(
scoped_refptr<gl::GLShareGroup> share_group, scoped_refptr<gl::GLShareGroup> share_group,
scoped_refptr<gl::GLSurface> surface, scoped_refptr<gl::GLSurface> surface,
...@@ -57,10 +80,12 @@ SharedContextState::SharedContextState( ...@@ -57,10 +80,12 @@ SharedContextState::SharedContextState(
GrContextType gr_context_type, GrContextType gr_context_type,
viz::VulkanContextProvider* vulkan_context_provider, viz::VulkanContextProvider* vulkan_context_provider,
viz::MetalContextProvider* metal_context_provider, viz::MetalContextProvider* metal_context_provider,
viz::DawnContextProvider* dawn_context_provider) viz::DawnContextProvider* dawn_context_provider,
gpu::MemoryTracker::Observer* peak_memory_monitor)
: use_virtualized_gl_contexts_(use_virtualized_gl_contexts), : use_virtualized_gl_contexts_(use_virtualized_gl_contexts),
context_lost_callback_(std::move(context_lost_callback)), context_lost_callback_(std::move(context_lost_callback)),
gr_context_type_(gr_context_type), gr_context_type_(gr_context_type),
memory_tracker_(peak_memory_monitor),
vk_context_provider_(vulkan_context_provider), vk_context_provider_(vulkan_context_provider),
metal_context_provider_(metal_context_provider), metal_context_provider_(metal_context_provider),
dawn_context_provider_(dawn_context_provider), dawn_context_provider_(dawn_context_provider),
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "build/build_config.h" #include "build/build_config.h"
#include "gpu/command_buffer/common/skia_utils.h" #include "gpu/command_buffer/common/skia_utils.h"
#include "gpu/command_buffer/service/gl_context_virtual_delegate.h" #include "gpu/command_buffer/service/gl_context_virtual_delegate.h"
#include "gpu/command_buffer/service/memory_tracking.h"
#include "gpu/config/gpu_preferences.h" #include "gpu/config/gpu_preferences.h"
#include "gpu/gpu_gles2_export.h" #include "gpu/gpu_gles2_export.h"
#include "third_party/skia/include/gpu/GrContext.h" #include "third_party/skia/include/gpu/GrContext.h"
...@@ -60,7 +61,8 @@ class GPU_GLES2_EXPORT SharedContextState ...@@ -60,7 +61,8 @@ class GPU_GLES2_EXPORT SharedContextState
GrContextType gr_context_type = GrContextType::kGL, GrContextType gr_context_type = GrContextType::kGL,
viz::VulkanContextProvider* vulkan_context_provider = nullptr, viz::VulkanContextProvider* vulkan_context_provider = nullptr,
viz::MetalContextProvider* metal_context_provider = nullptr, viz::MetalContextProvider* metal_context_provider = nullptr,
viz::DawnContextProvider* dawn_context_provider = nullptr); viz::DawnContextProvider* dawn_context_provider = nullptr,
gpu::MemoryTracker::Observer* peak_memory_monitor = nullptr);
void InitializeGrContext(const GpuDriverBugWorkarounds& workarounds, void InitializeGrContext(const GpuDriverBugWorkarounds& workarounds,
GrContextOptions::PersistentCache* cache, GrContextOptions::PersistentCache* cache,
...@@ -90,6 +92,8 @@ class GPU_GLES2_EXPORT SharedContextState ...@@ -90,6 +92,8 @@ class GPU_GLES2_EXPORT SharedContextState
void PurgeMemory( void PurgeMemory(
base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
uint64_t GetMemoryUsage() const { return memory_tracker_.GetMemoryUsage(); }
void PessimisticallyResetGrContext() const; void PessimisticallyResetGrContext() const;
gl::GLShareGroup* share_group() { return share_group_.get(); } gl::GLShareGroup* share_group() { return share_group_.get(); }
...@@ -130,6 +134,7 @@ class GPU_GLES2_EXPORT SharedContextState ...@@ -130,6 +134,7 @@ class GPU_GLES2_EXPORT SharedContextState
bool support_vulkan_external_object() const { bool support_vulkan_external_object() const {
return support_vulkan_external_object_; return support_vulkan_external_object_;
} }
gpu::MemoryTracker::Observer* memory_tracker() { return &memory_tracker_; }
// base::trace_event::MemoryDumpProvider implementation. // base::trace_event::MemoryDumpProvider implementation.
bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args, bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
...@@ -149,6 +154,28 @@ class GPU_GLES2_EXPORT SharedContextState ...@@ -149,6 +154,28 @@ class GPU_GLES2_EXPORT SharedContextState
private: private:
friend class base::RefCounted<SharedContextState>; friend class base::RefCounted<SharedContextState>;
// Observer which is notified when SkiaOutputSurfaceImpl takes ownership of a
// shared image, and forward information to both histograms and task manager.
class GPU_GLES2_EXPORT MemoryTracker : public gpu::MemoryTracker::Observer {
public:
MemoryTracker(gpu::MemoryTracker::Observer* peak_memory_monitor);
MemoryTracker(MemoryTracker&) = delete;
MemoryTracker& operator=(MemoryTracker&) = delete;
~MemoryTracker() override;
// gpu::MemoryTracker::Observer implementation:
void OnMemoryAllocatedChange(CommandBufferId id,
uint64_t old_size,
uint64_t new_size) override;
// Reports to GpuServiceImpl::GetVideoMemoryUsageStats()
uint64_t GetMemoryUsage() const;
private:
uint64_t size_ = 0;
gpu::MemoryTracker::Observer* const peak_memory_monitor_;
};
~SharedContextState() override; ~SharedContextState() override;
// gpu::GLContextVirtualDelegate implementation. // gpu::GLContextVirtualDelegate implementation.
...@@ -175,6 +202,7 @@ class GPU_GLES2_EXPORT SharedContextState ...@@ -175,6 +202,7 @@ class GPU_GLES2_EXPORT SharedContextState
bool support_vulkan_external_object_ = false; bool support_vulkan_external_object_ = false;
base::OnceClosure context_lost_callback_; base::OnceClosure context_lost_callback_;
GrContextType gr_context_type_ = GrContextType::kGL; GrContextType gr_context_type_ = GrContextType::kGL;
MemoryTracker memory_tracker_;
viz::VulkanContextProvider* const vk_context_provider_; viz::VulkanContextProvider* const vk_context_provider_;
viz::MetalContextProvider* const metal_context_provider_; viz::MetalContextProvider* const metal_context_provider_;
viz::DawnContextProvider* const dawn_context_provider_; viz::DawnContextProvider* const dawn_context_provider_;
......
...@@ -286,6 +286,9 @@ void GpuChannelManager::GetVideoMemoryUsageStats( ...@@ -286,6 +286,9 @@ void GpuChannelManager::GetVideoMemoryUsageStats(
.video_memory += size; .video_memory += size;
} }
if (shared_context_state_ && !shared_context_state_->context_lost())
total_size += shared_context_state_->GetMemoryUsage();
// Assign the total across all processes in the GPU process // Assign the total across all processes in the GPU process
video_memory_usage_stats->process_map[base::GetCurrentProcId()].video_memory = video_memory_usage_stats->process_map[base::GetCurrentProcId()].video_memory =
total_size; total_size;
...@@ -484,7 +487,7 @@ scoped_refptr<SharedContextState> GpuChannelManager::GetSharedContextState( ...@@ -484,7 +487,7 @@ scoped_refptr<SharedContextState> GpuChannelManager::GetSharedContextState(
base::BindOnce(&GpuChannelManager::OnContextLost, base::Unretained(this), base::BindOnce(&GpuChannelManager::OnContextLost, base::Unretained(this),
/*synthetic_loss=*/false), /*synthetic_loss=*/false),
gpu_preferences_.gr_context_type, vulkan_context_provider_, gpu_preferences_.gr_context_type, vulkan_context_provider_,
metal_context_provider_, dawn_context_provider_); metal_context_provider_, dawn_context_provider_, peak_memory_monitor());
// OOP-R needs GrContext for raster tiles. // OOP-R needs GrContext for raster tiles.
bool need_gr_context = bool need_gr_context =
......
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