Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(398)

Side by Side Diff: cc/output/output_surface.cc

Issue 2408513002: Move memory observer off OutputSurface/CompositorFrameSink (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/output/output_surface.h" 5 #include "cc/output/output_surface.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/threading/thread_task_runner_handle.h" 12 #include "base/threading/thread_task_runner_handle.h"
14 #include "base/trace_event/memory_dump_manager.h"
15 #include "base/trace_event/trace_event.h" 13 #include "base/trace_event/trace_event.h"
16 #include "cc/output/managed_memory_policy.h"
17 #include "cc/output/output_surface_client.h" 14 #include "cc/output/output_surface_client.h"
18 #include "cc/output/output_surface_frame.h" 15 #include "cc/output/output_surface_frame.h"
19 #include "gpu/GLES2/gl2extchromium.h" 16 #include "gpu/GLES2/gl2extchromium.h"
20 #include "gpu/command_buffer/client/context_support.h" 17 #include "gpu/command_buffer/client/context_support.h"
21 #include "gpu/command_buffer/client/gles2_interface.h" 18 #include "gpu/command_buffer/client/gles2_interface.h"
22 #include "third_party/skia/include/core/SkTraceMemoryDump.h"
23 #include "third_party/skia/include/gpu/GrContext.h"
24 #include "ui/gfx/geometry/rect.h"
25 #include "ui/gfx/geometry/size.h"
26 #include "ui/gl/trace_util.h"
27
28 class SkDiscardableMemory;
29 19
30 namespace cc { 20 namespace cc {
31 21
32 namespace {
33
34 // Constants used by SkiaGpuTraceMemoryDump to identify different memory types.
35 const char* kGLTextureBackingType = "gl_texture";
36 const char* kGLBufferBackingType = "gl_buffer";
37 const char* kGLRenderbufferBackingType = "gl_renderbuffer";
38
39 // Derives from SkTraceMemoryDump and implements graphics specific memory
40 // backing functionality.
41 class SkiaGpuTraceMemoryDump : public SkTraceMemoryDump {
ericrk 2016/10/07 23:50:14 so glad this is no longer duplicated :D
42 public:
43 // This should never outlive the provided ProcessMemoryDump, as it should
44 // always be scoped to a single OnMemoryDump funciton call.
45 explicit SkiaGpuTraceMemoryDump(base::trace_event::ProcessMemoryDump* pmd,
46 uint64_t share_group_tracing_guid)
47 : pmd_(pmd), share_group_tracing_guid_(share_group_tracing_guid) {}
48
49 // Overridden from SkTraceMemoryDump:
50 void dumpNumericValue(const char* dump_name,
51 const char* value_name,
52 const char* units,
53 uint64_t value) override {
54 auto* dump = GetOrCreateAllocatorDump(dump_name);
55 dump->AddScalar(value_name, units, value);
56 }
57
58 void setMemoryBacking(const char* dump_name,
59 const char* backing_type,
60 const char* backing_object_id) override {
61 const uint64_t tracing_process_id =
62 base::trace_event::MemoryDumpManager::GetInstance()
63 ->GetTracingProcessId();
64
65 // For uniformity, skia provides this value as a string. Convert back to a
66 // uint32_t.
67 uint32_t gl_id =
68 std::strtoul(backing_object_id, nullptr /* str_end */, 10 /* base */);
69
70 // Populated in if statements below.
71 base::trace_event::MemoryAllocatorDumpGuid guid;
72
73 if (strcmp(backing_type, kGLTextureBackingType) == 0) {
74 guid = gl::GetGLTextureClientGUIDForTracing(share_group_tracing_guid_,
75 gl_id);
76 } else if (strcmp(backing_type, kGLBufferBackingType) == 0) {
77 guid = gl::GetGLBufferGUIDForTracing(tracing_process_id, gl_id);
78 } else if (strcmp(backing_type, kGLRenderbufferBackingType) == 0) {
79 guid = gl::GetGLRenderbufferGUIDForTracing(tracing_process_id, gl_id);
80 }
81
82 if (!guid.empty()) {
83 pmd_->CreateSharedGlobalAllocatorDump(guid);
84
85 auto* dump = GetOrCreateAllocatorDump(dump_name);
86
87 const int kImportance = 2;
88 pmd_->AddOwnershipEdge(dump->guid(), guid, kImportance);
89 }
90 }
91
92 void setDiscardableMemoryBacking(
93 const char* dump_name,
94 const SkDiscardableMemory& discardable_memory_object) override {
95 // We don't use this class for dumping discardable memory.
96 NOTREACHED();
97 }
98
99 LevelOfDetail getRequestedDetails() const override {
100 // TODO(ssid): Use MemoryDumpArgs to create light dumps when requested
101 // (crbug.com/499731).
102 return kObjectsBreakdowns_LevelOfDetail;
103 }
104
105 private:
106 // Helper to create allocator dumps.
107 base::trace_event::MemoryAllocatorDump* GetOrCreateAllocatorDump(
108 const char* dump_name) {
109 auto* dump = pmd_->GetAllocatorDump(dump_name);
110 if (!dump)
111 dump = pmd_->CreateAllocatorDump(dump_name);
112 return dump;
113 }
114
115 base::trace_event::ProcessMemoryDump* pmd_;
116 uint64_t share_group_tracing_guid_;
117
118 DISALLOW_COPY_AND_ASSIGN(SkiaGpuTraceMemoryDump);
119 };
120
121 } // namespace
122
123 OutputSurface::OutputSurface(scoped_refptr<ContextProvider> context_provider) 22 OutputSurface::OutputSurface(scoped_refptr<ContextProvider> context_provider)
124 : context_provider_(std::move(context_provider)), weak_ptr_factory_(this) { 23 : context_provider_(std::move(context_provider)), weak_ptr_factory_(this) {
125 DCHECK(context_provider_); 24 DCHECK(context_provider_);
126 thread_checker_.DetachFromThread(); 25 thread_checker_.DetachFromThread();
127 } 26 }
128 27
129 OutputSurface::OutputSurface( 28 OutputSurface::OutputSurface(
130 std::unique_ptr<SoftwareOutputDevice> software_device) 29 std::unique_ptr<SoftwareOutputDevice> software_device)
131 : software_device_(std::move(software_device)), weak_ptr_factory_(this) { 30 : software_device_(std::move(software_device)), weak_ptr_factory_(this) {
132 DCHECK(software_device_); 31 DCHECK(software_device_);
133 thread_checker_.DetachFromThread(); 32 thread_checker_.DetachFromThread();
134 } 33 }
135 34
136 OutputSurface::OutputSurface( 35 OutputSurface::OutputSurface(
137 scoped_refptr<VulkanContextProvider> vulkan_context_provider) 36 scoped_refptr<VulkanContextProvider> vulkan_context_provider)
138 : vulkan_context_provider_(vulkan_context_provider), 37 : vulkan_context_provider_(vulkan_context_provider),
139 weak_ptr_factory_(this) { 38 weak_ptr_factory_(this) {
140 DCHECK(vulkan_context_provider_); 39 DCHECK(vulkan_context_provider_);
141 thread_checker_.DetachFromThread(); 40 thread_checker_.DetachFromThread();
142 } 41 }
143 42
144 OutputSurface::~OutputSurface() { 43 OutputSurface::~OutputSurface() {
145 // Is destroyed on the thread it is bound to. 44 // Is destroyed on the thread it is bound to.
146 DCHECK(thread_checker_.CalledOnValidThread()); 45 DCHECK(thread_checker_.CalledOnValidThread());
147 46
148 if (!client_) 47 if (!client_)
149 return; 48 return;
150 49
151 // Unregister any dump provider. Safe to call (no-op) if we have not yet
152 // registered.
153 if (base::ThreadTaskRunnerHandle::IsSet()) {
154 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
155 this);
156 }
157
158 if (context_provider_) { 50 if (context_provider_) {
159 context_provider_->SetLostContextCallback( 51 context_provider_->SetLostContextCallback(
160 ContextProvider::LostContextCallback()); 52 ContextProvider::LostContextCallback());
161 } 53 }
162 } 54 }
163 55
164 bool OutputSurface::BindToClient(OutputSurfaceClient* client) { 56 bool OutputSurface::BindToClient(OutputSurfaceClient* client) {
165 DCHECK(thread_checker_.CalledOnValidThread()); 57 DCHECK(thread_checker_.CalledOnValidThread());
166 DCHECK(client); 58 DCHECK(client);
167 DCHECK(!client_); 59 DCHECK(!client_);
168 client_ = client; 60 client_ = client;
169 61
170 if (context_provider_) { 62 if (context_provider_) {
171 if (!context_provider_->BindToCurrentThread()) 63 if (!context_provider_->BindToCurrentThread())
172 return false; 64 return false;
173
174 context_provider_->SetLostContextCallback(base::Bind( 65 context_provider_->SetLostContextCallback(base::Bind(
175 &OutputSurface::DidLoseOutputSurface, base::Unretained(this))); 66 &OutputSurface::DidLoseOutputSurface, base::Unretained(this)));
176 } 67 }
177
178 // In certain cases, ThreadTaskRunnerHandle isn't set (Android Webview).
179 // Don't register a dump provider in these cases.
180 // TODO(ericrk): Get this working in Android Webview. crbug.com/517156
181 if (base::ThreadTaskRunnerHandle::IsSet()) {
182 // Now that we are on the context thread, register a dump provider with this
183 // thread's task runner. This will overwrite any previous dump provider
184 // registered.
185 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
186 this, "OutputSurface", base::ThreadTaskRunnerHandle::Get());
187 }
188 return true; 68 return true;
189 } 69 }
190 70
191 void OutputSurface::Reshape(const gfx::Size& size, 71 void OutputSurface::Reshape(const gfx::Size& size,
192 float scale_factor, 72 float scale_factor,
193 const gfx::ColorSpace& color_space, 73 const gfx::ColorSpace& color_space,
194 bool has_alpha) { 74 bool has_alpha) {
195 device_color_space_ = color_space; 75 device_color_space_ = color_space;
196 if (size == surface_size_ && scale_factor == device_scale_factor_ && 76 if (size == surface_size_ && scale_factor == device_scale_factor_ &&
197 has_alpha == has_alpha_) 77 has_alpha == has_alpha_)
(...skipping 15 matching lines...) Expand all
213 FROM_HERE, base::Bind(&OutputSurface::OnSwapBuffersComplete, 93 FROM_HERE, base::Bind(&OutputSurface::OnSwapBuffersComplete,
214 weak_ptr_factory_.GetWeakPtr())); 94 weak_ptr_factory_.GetWeakPtr()));
215 } 95 }
216 96
217 // We don't post tasks bound to the client directly since they might run 97 // We don't post tasks bound to the client directly since they might run
218 // after the OutputSurface has been destroyed. 98 // after the OutputSurface has been destroyed.
219 void OutputSurface::OnSwapBuffersComplete() { 99 void OutputSurface::OnSwapBuffersComplete() {
220 client_->DidSwapBuffersComplete(); 100 client_->DidSwapBuffersComplete();
221 } 101 }
222 102
223 bool OutputSurface::OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
224 base::trace_event::ProcessMemoryDump* pmd) {
225 if (auto* context_provider = this->context_provider()) {
226 // No need to lock, main context provider is not shared.
227 if (auto* gr_context = context_provider->GrContext()) {
228 SkiaGpuTraceMemoryDump trace_memory_dump(
229 pmd, context_provider->ContextSupport()->ShareGroupTracingGUID());
230 gr_context->dumpMemoryStatistics(&trace_memory_dump);
231 }
232 }
233 return true;
234 }
235
236 void OutputSurface::DidLoseOutputSurface() { 103 void OutputSurface::DidLoseOutputSurface() {
237 TRACE_EVENT0("cc", "OutputSurface::DidLoseOutputSurface"); 104 TRACE_EVENT0("cc", "OutputSurface::DidLoseOutputSurface");
238 client_->DidLoseOutputSurface(); 105 client_->DidLoseOutputSurface();
239 } 106 }
240 107
241 } // namespace cc 108 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698