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

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

Issue 2408513002: Move memory observer off OutputSurface/CompositorFrameSink (Closed)
Patch Set: memoryobs-contextprovider: . 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
« no previous file with comments | « cc/output/compositor_frame_sink.h ('k') | cc/output/output_surface.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/compositor_frame_sink.h" 5 #include "cc/output/compositor_frame_sink.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/compositor_frame_sink_client.h" 14 #include "cc/output/compositor_frame_sink_client.h"
17 #include "cc/output/managed_memory_policy.h"
18 #include "gpu/GLES2/gl2extchromium.h" 15 #include "gpu/GLES2/gl2extchromium.h"
19 #include "gpu/command_buffer/client/context_support.h" 16 #include "gpu/command_buffer/client/context_support.h"
20 #include "gpu/command_buffer/client/gles2_interface.h" 17 #include "gpu/command_buffer/client/gles2_interface.h"
21 #include "third_party/skia/include/core/SkTraceMemoryDump.h"
22 #include "third_party/skia/include/gpu/GrContext.h"
23 #include "ui/gfx/geometry/rect.h"
24 #include "ui/gfx/geometry/size.h"
25 #include "ui/gl/trace_util.h"
26
27 class SkDiscardableMemory;
28 18
29 namespace cc { 19 namespace cc {
30 20
31 namespace {
32
33 // Constants used by SkiaGpuTraceMemoryDump to identify different memory types.
34 const char* kGLTextureBackingType = "gl_texture";
35 const char* kGLBufferBackingType = "gl_buffer";
36 const char* kGLRenderbufferBackingType = "gl_renderbuffer";
37
38 // Derives from SkTraceMemoryDump and implements graphics specific memory
39 // backing functionality.
40 class SkiaGpuTraceMemoryDump : public SkTraceMemoryDump {
41 public:
42 // This should never outlive the provided ProcessMemoryDump, as it should
43 // always be scoped to a single OnMemoryDump funciton call.
44 explicit SkiaGpuTraceMemoryDump(base::trace_event::ProcessMemoryDump* pmd,
45 uint64_t share_group_tracing_guid)
46 : pmd_(pmd), share_group_tracing_guid_(share_group_tracing_guid) {}
47
48 // Overridden from SkTraceMemoryDump:
49 void dumpNumericValue(const char* dump_name,
50 const char* value_name,
51 const char* units,
52 uint64_t value) override {
53 auto* dump = GetOrCreateAllocatorDump(dump_name);
54 dump->AddScalar(value_name, units, value);
55 }
56
57 void setMemoryBacking(const char* dump_name,
58 const char* backing_type,
59 const char* backing_object_id) override {
60 const uint64_t tracing_process_id =
61 base::trace_event::MemoryDumpManager::GetInstance()
62 ->GetTracingProcessId();
63
64 // For uniformity, skia provides this value as a string. Convert back to a
65 // uint32_t.
66 uint32_t gl_id =
67 std::strtoul(backing_object_id, nullptr /* str_end */, 10 /* base */);
68
69 // Populated in if statements below.
70 base::trace_event::MemoryAllocatorDumpGuid guid;
71
72 if (strcmp(backing_type, kGLTextureBackingType) == 0) {
73 guid = gl::GetGLTextureClientGUIDForTracing(share_group_tracing_guid_,
74 gl_id);
75 } else if (strcmp(backing_type, kGLBufferBackingType) == 0) {
76 guid = gl::GetGLBufferGUIDForTracing(tracing_process_id, gl_id);
77 } else if (strcmp(backing_type, kGLRenderbufferBackingType) == 0) {
78 guid = gl::GetGLRenderbufferGUIDForTracing(tracing_process_id, gl_id);
79 }
80
81 if (!guid.empty()) {
82 pmd_->CreateSharedGlobalAllocatorDump(guid);
83
84 auto* dump = GetOrCreateAllocatorDump(dump_name);
85
86 const int kImportance = 2;
87 pmd_->AddOwnershipEdge(dump->guid(), guid, kImportance);
88 }
89 }
90
91 void setDiscardableMemoryBacking(
92 const char* dump_name,
93 const SkDiscardableMemory& discardable_memory_object) override {
94 // We don't use this class for dumping discardable memory.
95 NOTREACHED();
96 }
97
98 LevelOfDetail getRequestedDetails() const override {
99 // TODO(ssid): Use MemoryDumpArgs to create light dumps when requested
100 // (crbug.com/499731).
101 return kObjectsBreakdowns_LevelOfDetail;
102 }
103
104 private:
105 // Helper to create allocator dumps.
106 base::trace_event::MemoryAllocatorDump* GetOrCreateAllocatorDump(
107 const char* dump_name) {
108 auto* dump = pmd_->GetAllocatorDump(dump_name);
109 if (!dump)
110 dump = pmd_->CreateAllocatorDump(dump_name);
111 return dump;
112 }
113
114 base::trace_event::ProcessMemoryDump* pmd_;
115 uint64_t share_group_tracing_guid_;
116
117 DISALLOW_COPY_AND_ASSIGN(SkiaGpuTraceMemoryDump);
118 };
119
120 } // namespace
121
122 CompositorFrameSink::CompositorFrameSink( 21 CompositorFrameSink::CompositorFrameSink(
123 scoped_refptr<ContextProvider> context_provider, 22 scoped_refptr<ContextProvider> context_provider,
124 scoped_refptr<ContextProvider> worker_context_provider) 23 scoped_refptr<ContextProvider> worker_context_provider)
125 : context_provider_(std::move(context_provider)), 24 : context_provider_(std::move(context_provider)),
126 worker_context_provider_(std::move(worker_context_provider)), 25 worker_context_provider_(std::move(worker_context_provider)),
127 weak_ptr_factory_(this) { 26 weak_ptr_factory_(this) {
128 client_thread_checker_.DetachFromThread(); 27 client_thread_checker_.DetachFromThread();
129 } 28 }
130 29
131 CompositorFrameSink::CompositorFrameSink( 30 CompositorFrameSink::CompositorFrameSink(
(...skipping 17 matching lines...) Expand all
149 48
150 if (context_provider_.get()) { 49 if (context_provider_.get()) {
151 success = context_provider_->BindToCurrentThread(); 50 success = context_provider_->BindToCurrentThread();
152 if (success) { 51 if (success) {
153 context_provider_->SetLostContextCallback( 52 context_provider_->SetLostContextCallback(
154 base::Bind(&CompositorFrameSink::DidLoseCompositorFrameSink, 53 base::Bind(&CompositorFrameSink::DidLoseCompositorFrameSink,
155 base::Unretained(this))); 54 base::Unretained(this)));
156 } 55 }
157 } 56 }
158 57
159 // In certain cases, ThreadTaskRunnerHandle isn't set (Android Webview).
160 // Don't register a dump provider in these cases.
161 // TODO(ericrk): Get this working in Android Webview. crbug.com/517156
162 if (base::ThreadTaskRunnerHandle::IsSet()) {
163 // Now that we are on the context thread, register a dump provider with this
164 // thread's task runner. This will overwrite any previous dump provider
165 // registered.
166 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
167 this, "CompositorFrameSink", base::ThreadTaskRunnerHandle::Get());
168 }
169
170 if (!success) 58 if (!success)
171 DetachFromClient(); 59 DetachFromClient();
172 return success; 60 return success;
173 } 61 }
174 62
175 void CompositorFrameSink::DetachFromClient() { 63 void CompositorFrameSink::DetachFromClient() {
176 DetachFromClientInternal(); 64 DetachFromClientInternal();
177 } 65 }
178 66
179 void CompositorFrameSink::PostSwapBuffersComplete() { 67 void CompositorFrameSink::PostSwapBuffersComplete() {
180 base::ThreadTaskRunnerHandle::Get()->PostTask( 68 base::ThreadTaskRunnerHandle::Get()->PostTask(
181 FROM_HERE, base::Bind(&CompositorFrameSink::OnSwapBuffersComplete, 69 FROM_HERE, base::Bind(&CompositorFrameSink::OnSwapBuffersComplete,
182 weak_ptr_factory_.GetWeakPtr())); 70 weak_ptr_factory_.GetWeakPtr()));
183 } 71 }
184 72
185 // We don't post tasks bound to the client directly since they might run 73 // We don't post tasks bound to the client directly since they might run
186 // after the CompositorFrameSink has been destroyed. 74 // after the CompositorFrameSink has been destroyed.
187 void CompositorFrameSink::OnSwapBuffersComplete() { 75 void CompositorFrameSink::OnSwapBuffersComplete() {
188 client_->DidSwapBuffersComplete(); 76 client_->DidSwapBuffersComplete();
189 } 77 }
190 78
191 bool CompositorFrameSink::OnMemoryDump(
192 const base::trace_event::MemoryDumpArgs& args,
193 base::trace_event::ProcessMemoryDump* pmd) {
194 if (auto* context_provider = this->context_provider()) {
195 // No need to lock, main context provider is not shared.
196 if (auto* gr_context = context_provider->GrContext()) {
197 SkiaGpuTraceMemoryDump trace_memory_dump(
198 pmd, context_provider->ContextSupport()->ShareGroupTracingGUID());
199 gr_context->dumpMemoryStatistics(&trace_memory_dump);
200 }
201 }
202 if (auto* context_provider = worker_context_provider()) {
203 ContextProvider::ScopedContextLock scoped_context(context_provider);
204
205 if (auto* gr_context = context_provider->GrContext()) {
206 SkiaGpuTraceMemoryDump trace_memory_dump(
207 pmd, context_provider->ContextSupport()->ShareGroupTracingGUID());
208 gr_context->dumpMemoryStatistics(&trace_memory_dump);
209 }
210 }
211
212 return true;
213 }
214
215 void CompositorFrameSink::DetachFromClientInternal() { 79 void CompositorFrameSink::DetachFromClientInternal() {
216 DCHECK(client_thread_checker_.CalledOnValidThread()); 80 DCHECK(client_thread_checker_.CalledOnValidThread());
217 DCHECK(client_); 81 DCHECK(client_);
218 82
219 // Unregister any dump provider. Safe to call (no-op) if we have not yet
220 // registered.
221 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
222 this);
223
224 if (context_provider_.get()) { 83 if (context_provider_.get()) {
225 context_provider_->SetLostContextCallback( 84 context_provider_->SetLostContextCallback(
226 ContextProvider::LostContextCallback()); 85 ContextProvider::LostContextCallback());
227 } 86 }
228 context_provider_ = nullptr; 87 context_provider_ = nullptr;
229 client_ = nullptr; 88 client_ = nullptr;
230 weak_ptr_factory_.InvalidateWeakPtrs(); 89 weak_ptr_factory_.InvalidateWeakPtrs();
231 } 90 }
232 91
233 void CompositorFrameSink::DidLoseCompositorFrameSink() { 92 void CompositorFrameSink::DidLoseCompositorFrameSink() {
234 TRACE_EVENT0("cc", "CompositorFrameSink::DidLoseCompositorFrameSink"); 93 TRACE_EVENT0("cc", "CompositorFrameSink::DidLoseCompositorFrameSink");
235 client_->DidLoseCompositorFrameSink(); 94 client_->DidLoseCompositorFrameSink();
236 } 95 }
237 96
238 } // namespace cc 97 } // namespace cc
OLDNEW
« no previous file with comments | « cc/output/compositor_frame_sink.h ('k') | cc/output/output_surface.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698