OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/resources/resource_provider.h" | 5 #include "cc/resources/resource_provider.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "base/numerics/safe_math.h" | 12 #include "base/numerics/safe_math.h" |
13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
14 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/stringprintf.h" |
| 17 #include "base/thread_task_runner_handle.h" |
| 18 #include "base/trace_event/memory_dump_manager.h" |
16 #include "base/trace_event/trace_event.h" | 19 #include "base/trace_event/trace_event.h" |
17 #include "cc/base/math_util.h" | 20 #include "cc/base/math_util.h" |
18 #include "cc/resources/platform_color.h" | 21 #include "cc/resources/platform_color.h" |
19 #include "cc/resources/returned_resource.h" | 22 #include "cc/resources/returned_resource.h" |
20 #include "cc/resources/shared_bitmap_manager.h" | 23 #include "cc/resources/shared_bitmap_manager.h" |
21 #include "cc/resources/transferable_resource.h" | 24 #include "cc/resources/transferable_resource.h" |
22 #include "gpu/GLES2/gl2extchromium.h" | 25 #include "gpu/GLES2/gl2extchromium.h" |
23 #include "gpu/command_buffer/client/gles2_interface.h" | 26 #include "gpu/command_buffer/client/gles2_interface.h" |
24 #include "gpu/command_buffer/client/gpu_memory_buffer_manager.h" | 27 #include "gpu/command_buffer/client/gpu_memory_buffer_manager.h" |
25 #include "third_party/khronos/GLES2/gl2.h" | 28 #include "third_party/khronos/GLES2/gl2.h" |
26 #include "third_party/khronos/GLES2/gl2ext.h" | 29 #include "third_party/khronos/GLES2/gl2ext.h" |
27 #include "third_party/skia/include/core/SkSurface.h" | 30 #include "third_party/skia/include/core/SkSurface.h" |
28 #include "third_party/skia/include/gpu/GrContext.h" | 31 #include "third_party/skia/include/gpu/GrContext.h" |
29 #include "third_party/skia/include/gpu/GrTextureProvider.h" | 32 #include "third_party/skia/include/gpu/GrTextureProvider.h" |
30 #include "ui/gfx/geometry/rect.h" | 33 #include "ui/gfx/geometry/rect.h" |
31 #include "ui/gfx/geometry/vector2d.h" | 34 #include "ui/gfx/geometry/vector2d.h" |
32 #include "ui/gfx/gpu_memory_buffer.h" | 35 #include "ui/gfx/gpu_memory_buffer.h" |
| 36 #include "ui/gl/trace_util.h" |
33 | 37 |
34 using gpu::gles2::GLES2Interface; | 38 using gpu::gles2::GLES2Interface; |
35 | 39 |
36 namespace cc { | 40 namespace cc { |
37 | 41 |
38 class IdAllocator { | 42 class IdAllocator { |
39 public: | 43 public: |
40 virtual ~IdAllocator() {} | 44 virtual ~IdAllocator() {} |
41 | 45 |
42 virtual GLuint NextId() = 0; | 46 virtual GLuint NextId() = 0; |
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 scoped_ptr<ResourceProvider> resource_provider(new ResourceProvider( | 401 scoped_ptr<ResourceProvider> resource_provider(new ResourceProvider( |
398 output_surface, shared_bitmap_manager, gpu_memory_buffer_manager, | 402 output_surface, shared_bitmap_manager, gpu_memory_buffer_manager, |
399 blocking_main_thread_task_runner, highp_threshold_min, | 403 blocking_main_thread_task_runner, highp_threshold_min, |
400 use_rgba_4444_texture_format, id_allocation_chunk_size, | 404 use_rgba_4444_texture_format, id_allocation_chunk_size, |
401 use_persistent_map_for_gpu_memory_buffers)); | 405 use_persistent_map_for_gpu_memory_buffers)); |
402 resource_provider->Initialize(); | 406 resource_provider->Initialize(); |
403 return resource_provider; | 407 return resource_provider; |
404 } | 408 } |
405 | 409 |
406 ResourceProvider::~ResourceProvider() { | 410 ResourceProvider::~ResourceProvider() { |
| 411 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( |
| 412 this); |
| 413 |
407 while (!children_.empty()) | 414 while (!children_.empty()) |
408 DestroyChildInternal(children_.begin(), FOR_SHUTDOWN); | 415 DestroyChildInternal(children_.begin(), FOR_SHUTDOWN); |
409 while (!resources_.empty()) | 416 while (!resources_.empty()) |
410 DeleteResourceInternal(resources_.begin(), FOR_SHUTDOWN); | 417 DeleteResourceInternal(resources_.begin(), FOR_SHUTDOWN); |
411 | 418 |
412 GLES2Interface* gl = ContextGL(); | 419 GLES2Interface* gl = ContextGL(); |
413 if (default_resource_type_ != RESOURCE_TYPE_GL_TEXTURE) { | 420 if (default_resource_type_ != RESOURCE_TYPE_GL_TEXTURE) { |
414 // We are not in GL mode, but double check before returning. | 421 // We are not in GL mode, but double check before returning. |
415 DCHECK(!gl); | 422 DCHECK(!gl); |
416 return; | 423 return; |
(...skipping 693 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1110 use_sync_query_(false), | 1117 use_sync_query_(false), |
1111 use_persistent_map_for_gpu_memory_buffers_( | 1118 use_persistent_map_for_gpu_memory_buffers_( |
1112 use_persistent_map_for_gpu_memory_buffers) { | 1119 use_persistent_map_for_gpu_memory_buffers) { |
1113 DCHECK(output_surface_->HasClient()); | 1120 DCHECK(output_surface_->HasClient()); |
1114 DCHECK(id_allocation_chunk_size_); | 1121 DCHECK(id_allocation_chunk_size_); |
1115 } | 1122 } |
1116 | 1123 |
1117 void ResourceProvider::Initialize() { | 1124 void ResourceProvider::Initialize() { |
1118 DCHECK(thread_checker_.CalledOnValidThread()); | 1125 DCHECK(thread_checker_.CalledOnValidThread()); |
1119 | 1126 |
| 1127 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
| 1128 this, base::ThreadTaskRunnerHandle::Get()); |
| 1129 |
1120 GLES2Interface* gl = ContextGL(); | 1130 GLES2Interface* gl = ContextGL(); |
1121 if (!gl) { | 1131 if (!gl) { |
1122 default_resource_type_ = RESOURCE_TYPE_BITMAP; | 1132 default_resource_type_ = RESOURCE_TYPE_BITMAP; |
1123 // Pick an arbitrary limit here similar to what hardware might. | 1133 // Pick an arbitrary limit here similar to what hardware might. |
1124 max_texture_size_ = 16 * 1024; | 1134 max_texture_size_ = 16 * 1024; |
1125 best_texture_format_ = RGBA_8888; | 1135 best_texture_format_ = RGBA_8888; |
1126 return; | 1136 return; |
1127 } | 1137 } |
1128 | 1138 |
1129 DCHECK(!texture_id_allocator_); | 1139 DCHECK(!texture_id_allocator_); |
(...skipping 821 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1951 return context_provider ? context_provider->ContextGL() : NULL; | 1961 return context_provider ? context_provider->ContextGL() : NULL; |
1952 } | 1962 } |
1953 | 1963 |
1954 class GrContext* ResourceProvider::GrContext(bool worker_context) const { | 1964 class GrContext* ResourceProvider::GrContext(bool worker_context) const { |
1955 ContextProvider* context_provider = | 1965 ContextProvider* context_provider = |
1956 worker_context ? output_surface_->worker_context_provider() | 1966 worker_context ? output_surface_->worker_context_provider() |
1957 : output_surface_->context_provider(); | 1967 : output_surface_->context_provider(); |
1958 return context_provider ? context_provider->GrContext() : NULL; | 1968 return context_provider ? context_provider->GrContext() : NULL; |
1959 } | 1969 } |
1960 | 1970 |
| 1971 bool ResourceProvider::OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd) { |
| 1972 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1973 |
| 1974 const uint64 tracing_process_id = |
| 1975 base::trace_event::MemoryDumpManager::GetInstance()->tracing_process_id(); |
| 1976 |
| 1977 for (const auto& resource_entry : resources_) { |
| 1978 const auto& resource = resource_entry.second; |
| 1979 |
| 1980 // Only log INTERNAL resources - other resources are owned by (and will be |
| 1981 // logged by) another ResourceProvider. |
| 1982 if (resource.origin != Resource::INTERNAL) |
| 1983 continue; |
| 1984 |
| 1985 std::string dump_name = base::StringPrintf("cc/resource_memory/resource_%d", |
| 1986 resource_entry.first); |
| 1987 base::trace_event::MemoryAllocatorDump* dump = |
| 1988 pmd->CreateAllocatorDump(dump_name); |
| 1989 |
| 1990 // Calculate the size of the current resource. |
| 1991 // TODO(ericrk): Replace this with helper fn once crrev.com/1202843008 |
| 1992 // goes in. |
| 1993 unsigned bytes_per_pixel = BitsPerPixel(resource.format) / 8; |
| 1994 uint32_t total_bytes = |
| 1995 resource.size.height() * |
| 1996 MathUtil::RoundUp(bytes_per_pixel * resource.size.width(), 4u); |
| 1997 |
| 1998 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 1999 base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 2000 static_cast<uint64_t>(total_bytes)); |
| 2001 |
| 2002 // Resources which are shared across processes require a shared GUID to |
| 2003 // prevent double counting the memory. We currently support shared GUIDs for |
| 2004 // GpuMemoryBuffer, SharedBitmap, and GL backed resources. |
| 2005 base::trace_event::MemoryAllocatorDumpGuid guid; |
| 2006 if (resource.gpu_memory_buffer) { |
| 2007 guid = gfx::GetGpuMemoryBufferGUIDForTracing( |
| 2008 tracing_process_id, resource.gpu_memory_buffer->GetHandle().id); |
| 2009 } else if (resource.shared_bitmap) { |
| 2010 guid = GetSharedBitmapGUIDForTracing(resource.shared_bitmap->id()); |
| 2011 } else if (resource.gl_id && resource.allocated) { |
| 2012 guid = |
| 2013 gfx::GetGLTextureGUIDForTracing(tracing_process_id, resource.gl_id); |
| 2014 } |
| 2015 |
| 2016 if (!guid.empty()) { |
| 2017 const int kImportance = 2; |
| 2018 pmd->CreateSharedGlobalAllocatorDump(guid); |
| 2019 pmd->AddOwnershipEdge(dump->guid(), guid, kImportance); |
| 2020 } |
| 2021 } |
| 2022 |
| 2023 return true; |
| 2024 } |
| 2025 |
1961 } // namespace cc | 2026 } // namespace cc |
OLD | NEW |