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/resources/platform_color.h" | 20 #include "cc/resources/platform_color.h" |
18 #include "cc/resources/resource_util.h" | 21 #include "cc/resources/resource_util.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 690 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1107 use_sync_query_(false), | 1114 use_sync_query_(false), |
1108 use_persistent_map_for_gpu_memory_buffers_( | 1115 use_persistent_map_for_gpu_memory_buffers_( |
1109 use_persistent_map_for_gpu_memory_buffers) { | 1116 use_persistent_map_for_gpu_memory_buffers) { |
1110 DCHECK(output_surface_->HasClient()); | 1117 DCHECK(output_surface_->HasClient()); |
1111 DCHECK(id_allocation_chunk_size_); | 1118 DCHECK(id_allocation_chunk_size_); |
1112 } | 1119 } |
1113 | 1120 |
1114 void ResourceProvider::Initialize() { | 1121 void ResourceProvider::Initialize() { |
1115 DCHECK(thread_checker_.CalledOnValidThread()); | 1122 DCHECK(thread_checker_.CalledOnValidThread()); |
1116 | 1123 |
| 1124 // In certain cases, ThreadTaskRunnerHandle isn't set (Android Webview). |
| 1125 // Don't register a dump provider in these cases. |
| 1126 // TODO(ericrk): Get this working in Android Webview. crbug.com/517156 |
| 1127 if (base::ThreadTaskRunnerHandle::IsSet()) { |
| 1128 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
| 1129 this, base::ThreadTaskRunnerHandle::Get()); |
| 1130 } |
| 1131 |
1117 GLES2Interface* gl = ContextGL(); | 1132 GLES2Interface* gl = ContextGL(); |
1118 if (!gl) { | 1133 if (!gl) { |
1119 default_resource_type_ = RESOURCE_TYPE_BITMAP; | 1134 default_resource_type_ = RESOURCE_TYPE_BITMAP; |
1120 // Pick an arbitrary limit here similar to what hardware might. | 1135 // Pick an arbitrary limit here similar to what hardware might. |
1121 max_texture_size_ = 16 * 1024; | 1136 max_texture_size_ = 16 * 1024; |
1122 best_texture_format_ = RGBA_8888; | 1137 best_texture_format_ = RGBA_8888; |
1123 return; | 1138 return; |
1124 } | 1139 } |
1125 | 1140 |
1126 DCHECK(!texture_id_allocator_); | 1141 DCHECK(!texture_id_allocator_); |
(...skipping 819 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1946 return context_provider ? context_provider->ContextGL() : NULL; | 1961 return context_provider ? context_provider->ContextGL() : NULL; |
1947 } | 1962 } |
1948 | 1963 |
1949 class GrContext* ResourceProvider::GrContext(bool worker_context) const { | 1964 class GrContext* ResourceProvider::GrContext(bool worker_context) const { |
1950 ContextProvider* context_provider = | 1965 ContextProvider* context_provider = |
1951 worker_context ? output_surface_->worker_context_provider() | 1966 worker_context ? output_surface_->worker_context_provider() |
1952 : output_surface_->context_provider(); | 1967 : output_surface_->context_provider(); |
1953 return context_provider ? context_provider->GrContext() : NULL; | 1968 return context_provider ? context_provider->GrContext() : NULL; |
1954 } | 1969 } |
1955 | 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() |
| 1976 ->GetTracingProcessId(); |
| 1977 |
| 1978 for (const auto& resource_entry : resources_) { |
| 1979 const auto& resource = resource_entry.second; |
| 1980 |
| 1981 std::string dump_name = base::StringPrintf("cc/resource_memory/resource_%d", |
| 1982 resource_entry.first); |
| 1983 base::trace_event::MemoryAllocatorDump* dump = |
| 1984 pmd->CreateAllocatorDump(dump_name); |
| 1985 |
| 1986 uint64_t total_bytes = ResourceUtil::UncheckedSizeInBytesAligned<size_t>( |
| 1987 resource.size, resource.format); |
| 1988 dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 1989 base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 1990 static_cast<uint64_t>(total_bytes)); |
| 1991 |
| 1992 // Resources which are shared across processes require a shared GUID to |
| 1993 // prevent double counting the memory. We currently support shared GUIDs for |
| 1994 // GpuMemoryBuffer, SharedBitmap, and GL backed resources. |
| 1995 base::trace_event::MemoryAllocatorDumpGuid guid; |
| 1996 if (resource.gpu_memory_buffer) { |
| 1997 guid = gfx::GetGpuMemoryBufferGUIDForTracing( |
| 1998 tracing_process_id, resource.gpu_memory_buffer->GetHandle().id); |
| 1999 } else if (resource.shared_bitmap) { |
| 2000 guid = GetSharedBitmapGUIDForTracing(resource.shared_bitmap->id()); |
| 2001 } else if (resource.gl_id && resource.allocated) { |
| 2002 guid = |
| 2003 gfx::GetGLTextureGUIDForTracing(tracing_process_id, resource.gl_id); |
| 2004 } |
| 2005 |
| 2006 if (!guid.empty()) { |
| 2007 const int kImportance = 2; |
| 2008 pmd->CreateSharedGlobalAllocatorDump(guid); |
| 2009 pmd->AddOwnershipEdge(dump->guid(), guid, kImportance); |
| 2010 } |
| 2011 } |
| 2012 |
| 2013 return true; |
| 2014 } |
| 2015 |
1956 } // namespace cc | 2016 } // namespace cc |
OLD | NEW |