#include "gpu/command_buffer/service/shared_context_state.h"
#include <atomic>
#include "base/debug/dump_without_crashing.h"
#include "base/immediate_crash.h"
#include "base/metrics/histogram_functions.h"
#include "base/observer_list.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "base/system/sys_info.h"
#include "base/task/single_thread_task_runner.h"
#include "base/trace_event/memory_dump_manager.h"
#include "build/build_config.h"
#include "components/crash/core/common/crash_key.h"
#include "gpu/command_buffer/common/shm_count.h"
#include "gpu/command_buffer/service/context_state.h"
#include "gpu/command_buffer/service/dawn_context_provider.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"
#include "gpu/command_buffer/service/skia_utils.h"
#include "gpu/config/gpu_driver_bug_workarounds.h"
#include "gpu/config/gpu_finch_features.h"
#include "gpu/config/skia_limits.h"
#include "gpu/ipc/common/gpu_client_ids.h"
#include "gpu/vulkan/buildflags.h"
#include "skia/buildflags.h"
#include "skia/ext/skia_trace_memory_dump_impl.h"
#include "third_party/skia/include/gpu/GrBackendSemaphore.h"
#include "third_party/skia/include/gpu/GrTypes.h"
#include "third_party/skia/include/gpu/ganesh/SkSurfaceGanesh.h"
#include "third_party/skia/include/gpu/ganesh/gl/GrGLDirectContext.h"
#include "third_party/skia/include/gpu/graphite/Context.h"
#include "third_party/skia/include/gpu/mock/GrMockTypes.h"
#include "ui/gl/gl_bindings.h"
#include "ui/gl/gl_context.h"
#include "ui/gl/gl_share_group.h"
#include "ui/gl/gl_surface.h"
#include "ui/gl/gl_version_info.h"
#include "ui/gl/init/create_gr_gl_interface.h"
#if BUILDFLAG(ENABLE_VULKAN)
#include <vulkan/vulkan.h>
#include "components/viz/common/gpu/vulkan_context_provider.h"
#include "gpu/vulkan/vulkan_device_queue.h"
#include "gpu/vulkan/vulkan_implementation.h"
#include "gpu/vulkan/vulkan_util.h"
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_FUCHSIA) || BUILDFLAG(IS_WIN)
#include "gpu/command_buffer/service/external_semaphore_pool.h"
#endif
#endif
#if BUILDFLAG(IS_FUCHSIA)
#include "gpu/vulkan/fuchsia/vulkan_fuchsia_ext.h"
#endif
#if BUILDFLAG(SKIA_USE_METAL)
#include "components/viz/common/gpu/metal_context_provider.h"
#endif
#if BUILDFLAG(SKIA_USE_DAWN)
#include "gpu/command_buffer/service/dawn_context_provider.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "ui/gl/gl_angle_util_win.h"
#endif
namespace gpu {
namespace {
static constexpr size_t kInitialScratchDeserializationBufferSize = …;
size_t MaxNumSkSurface() { … }
std::unique_ptr<skgpu::graphite::Recorder> MakeGraphiteRecorder(
skgpu::graphite::Context* context,
size_t max_resource_cache_bytes,
size_t max_image_provider_cache_bytes) { … }
enum class SkiaBackendType { … };
SkiaBackendType FindSkiaBackendType(SharedContextState* context) { … }
}
void SharedContextState::compileError(const char* shader,
const char* errors,
bool shaderWasCached) { … }
SharedContextState::MemoryTrackerObserver::MemoryTrackerObserver(
base::WeakPtr<gpu::MemoryTracker::Observer> peak_memory_monitor)
: … { … }
SharedContextState::MemoryTrackerObserver::~MemoryTrackerObserver() { … }
void SharedContextState::MemoryTrackerObserver::OnMemoryAllocatedChange(
CommandBufferId id,
uint64_t old_size,
uint64_t new_size,
GpuPeakMemoryAllocationSource source) { … }
base::AtomicSequenceNumber g_next_command_buffer_id;
SharedContextState::MemoryTracker::MemoryTracker(Observer* observer)
: … { … }
SharedContextState::MemoryTracker::~MemoryTracker() { … }
void SharedContextState::MemoryTracker::TrackMemoryAllocatedChange(
int64_t delta) { … }
uint64_t SharedContextState::MemoryTracker::GetSize() const { … }
uint64_t SharedContextState::MemoryTracker::ClientTracingId() const { … }
int SharedContextState::MemoryTracker::ClientId() const { … }
uint64_t SharedContextState::MemoryTracker::ContextGroupTracingId() const { … }
SharedContextState::SharedContextState(
scoped_refptr<gl::GLShareGroup> share_group,
scoped_refptr<gl::GLSurface> surface,
scoped_refptr<gl::GLContext> context,
bool use_virtualized_gl_contexts,
ContextLostCallback context_lost_callback,
GrContextType gr_context_type,
viz::VulkanContextProvider* vulkan_context_provider,
viz::MetalContextProvider* metal_context_provider,
DawnContextProvider* dawn_context_provider,
base::WeakPtr<gpu::MemoryTracker::Observer> peak_memory_monitor,
bool created_on_compositor_gpu_thread)
: … { … }
SharedContextState::~SharedContextState() { … }
bool SharedContextState::IsUsingGL() const { … }
bool SharedContextState::IsGraphiteDawn() const { … }
bool SharedContextState::IsGraphiteMetal() const { … }
bool SharedContextState::IsGraphiteDawnMetal() const { … }
bool SharedContextState::IsGraphiteDawnD3D() const { … }
bool SharedContextState::IsGraphiteDawnVulkan() const { … }
bool SharedContextState::IsGraphiteDawnVulkanSwiftShader() const { … }
bool SharedContextState::InitializeSkia(
const GpuPreferences& gpu_preferences,
const GpuDriverBugWorkarounds& workarounds,
gpu::raster::GrShaderCache* cache,
GpuProcessShmCount* use_shader_cache_shm_count,
gl::ProgressReporter* progress_reporter) { … }
bool SharedContextState::InitializeGanesh(
const GpuPreferences& gpu_preferences,
const GpuDriverBugWorkarounds& workarounds,
gpu::raster::GrShaderCache* cache,
GpuProcessShmCount* use_shader_cache_shm_count,
gl::ProgressReporter* progress_reporter) { … }
bool SharedContextState::InitializeGraphite(
const GpuPreferences& gpu_preferences,
const GpuDriverBugWorkarounds& workarounds) { … }
bool SharedContextState::InitializeGL(
const GpuPreferences& gpu_preferences,
scoped_refptr<gles2::FeatureInfo> feature_info) { … }
void SharedContextState::FlushGraphiteRecorder() { … }
void SharedContextState::FlushAndSubmit(bool sync_to_cpu) { … }
void SharedContextState::FlushWriteAccess(
SkiaImageRepresentation::ScopedWriteAccess* access) { … }
void SharedContextState::SubmitIfNecessary(
std::vector<GrBackendSemaphore> signal_semaphores,
bool need_graphite_submit) { … }
bool SharedContextState::MakeCurrent(gl::GLSurface* surface, bool needs_gl) { … }
void SharedContextState::ReleaseCurrent(gl::GLSurface* surface) { … }
void SharedContextState::MarkContextLost(error::ContextLostReason reason) { … }
bool SharedContextState::IsCurrent(gl::GLSurface* surface, bool needs_gl) { … }
bool SharedContextState::OnMemoryDump(
const base::trace_event::MemoryDumpArgs& args,
base::trace_event::ProcessMemoryDump* pmd) { … }
void SharedContextState::AddContextLostObserver(ContextLostObserver* obs) { … }
void SharedContextState::RemoveContextLostObserver(ContextLostObserver* obs) { … }
void SharedContextState::PurgeMemory(
base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { … }
uint64_t SharedContextState::GetMemoryUsage() { … }
void SharedContextState::UpdateSkiaOwnedMemorySize() { … }
void SharedContextState::PessimisticallyResetGrContext() const { … }
void SharedContextState::StoreVkPipelineCacheIfNeeded() { … }
void SharedContextState::UseShaderCache(
std::optional<gpu::raster::GrShaderCache::ScopedCacheUse>& cache_use,
int32_t client_id) const { … }
gl::GLSurface* SharedContextState::surface() const { … }
gl::GLDisplay* SharedContextState::display() { … }
bool SharedContextState::initialized() const { … }
const gles2::ContextState* SharedContextState::GetContextState() { … }
void SharedContextState::RestoreState(const gles2::ContextState* prev_state) { … }
void SharedContextState::RestoreGlobalState() const { … }
void SharedContextState::ClearAllAttributes() const { … }
void SharedContextState::RestoreActiveTexture() const { … }
void SharedContextState::RestoreAllTextureUnitAndSamplerBindings(
const gles2::ContextState* prev_state) const { … }
void SharedContextState::RestoreActiveTextureUnitBinding(
unsigned int target) const { … }
void SharedContextState::RestoreBufferBinding(unsigned int target) { … }
void SharedContextState::RestoreBufferBindings() const { … }
void SharedContextState::RestoreFramebufferBindings() const { … }
void SharedContextState::RestoreRenderbufferBindings() { … }
void SharedContextState::RestoreProgramBindings() const { … }
void SharedContextState::RestoreTextureUnitBindings(unsigned unit) const { … }
void SharedContextState::RestoreVertexAttribArray(unsigned index) { … }
void SharedContextState::RestoreAllExternalTextureBindingsIfNeeded() { … }
QueryManager* SharedContextState::GetQueryManager() { … }
std::optional<error::ContextLostReason> SharedContextState::GetResetStatus(
bool needs_gl) { … }
bool SharedContextState::CheckResetStatus(bool need_gl) { … }
void SharedContextState::ScheduleSkiaCleanup() { … }
int32_t SharedContextState::GetMaxTextureSize() { … }
#if BUILDFLAG(IS_WIN)
Microsoft::WRL::ComPtr<ID3D11Device> SharedContextState::GetD3D11Device()
const {
switch (gr_context_type_) {
case GrContextType::kNone:
return nullptr;
case GrContextType::kGL:
case GrContextType::kVulkan:
return gl::QueryD3D11DeviceObjectFromANGLE();
#if BUILDFLAG(SKIA_USE_DAWN)
case GrContextType::kGraphiteDawn:
return dawn_context_provider_->GetD3D11Device();
#endif
default:
NOTREACHED_IN_MIGRATION();
return nullptr;
}
}
#endif
}