OLD | NEW |
---|---|
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 "content/common/gpu/client/context_provider_command_buffer.h" | 5 #include "content/common/gpu/client/context_provider_command_buffer.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <set> | 10 #include <set> |
11 #include <utility> | 11 #include <utility> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/callback_helpers.h" | 14 #include "base/callback_helpers.h" |
15 #include "base/command_line.h" | |
15 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
16 #include "cc/output/managed_memory_policy.h" | 17 #include "cc/output/managed_memory_policy.h" |
17 #include "gpu/command_buffer/client/gles2_implementation.h" | 18 #include "gpu/command_buffer/client/gles2_implementation.h" |
19 #include "gpu/command_buffer/client/gles2_trace_implementation.h" | |
20 #include "gpu/command_buffer/client/gpu_switches.h" | |
18 #include "gpu/skia_bindings/grcontext_for_gles2_interface.h" | 21 #include "gpu/skia_bindings/grcontext_for_gles2_interface.h" |
19 #include "third_party/skia/include/gpu/GrContext.h" | 22 #include "third_party/skia/include/gpu/GrContext.h" |
20 | 23 |
21 namespace content { | 24 namespace content { |
22 | 25 |
23 class ContextProviderCommandBuffer::LostContextCallbackProxy | 26 class ContextProviderCommandBuffer::LostContextCallbackProxy |
24 : public blink::WebGraphicsContext3D::WebGraphicsContextLostCallback { | 27 : public WebGraphicsContext3DCommandBufferImpl:: |
28 WebGraphicsContextLostCallback { | |
25 public: | 29 public: |
26 explicit LostContextCallbackProxy(ContextProviderCommandBuffer* provider) | 30 explicit LostContextCallbackProxy(ContextProviderCommandBuffer* provider) |
27 : provider_(provider) { | 31 : provider_(provider) { |
28 provider_->context3d_->setContextLostCallback(this); | 32 provider_->context3d_->SetContextLostCallback(this); |
29 } | 33 } |
30 | 34 |
31 ~LostContextCallbackProxy() override { | 35 ~LostContextCallbackProxy() override { |
32 provider_->context3d_->setContextLostCallback(NULL); | 36 provider_->context3d_->SetContextLostCallback(nullptr); |
33 } | 37 } |
34 | 38 |
35 void onContextLost() override { provider_->OnLostContext(); } | 39 void onContextLost() override { provider_->OnLostContext(); } |
36 | 40 |
37 private: | 41 private: |
38 ContextProviderCommandBuffer* provider_; | 42 ContextProviderCommandBuffer* provider_; |
39 }; | 43 }; |
40 | 44 |
41 ContextProviderCommandBuffer::ContextProviderCommandBuffer( | 45 ContextProviderCommandBuffer::ContextProviderCommandBuffer( |
42 std::unique_ptr<WebGraphicsContext3DCommandBufferImpl> context3d, | 46 std::unique_ptr<WebGraphicsContext3DCommandBufferImpl> context3d, |
43 const gpu::SharedMemoryLimits& memory_limits, | 47 const gpu::SharedMemoryLimits& memory_limits, |
44 CommandBufferContextType type) | 48 CommandBufferContextType type) |
45 : context3d_(std::move(context3d)), | 49 : context3d_(std::move(context3d)), |
46 memory_limits_(memory_limits), | 50 memory_limits_(memory_limits), |
47 context_type_(type), | 51 context_type_(type), |
48 debug_name_(CommandBufferContextTypeToString(type)) { | 52 debug_name_(CommandBufferContextTypeToString(type)) { |
49 DCHECK(main_thread_checker_.CalledOnValidThread()); | 53 DCHECK(main_thread_checker_.CalledOnValidThread()); |
50 DCHECK(context3d_); | 54 DCHECK(context3d_); |
51 context_thread_checker_.DetachFromThread(); | 55 context_thread_checker_.DetachFromThread(); |
52 } | 56 } |
53 | 57 |
54 ContextProviderCommandBuffer::~ContextProviderCommandBuffer() { | 58 ContextProviderCommandBuffer::~ContextProviderCommandBuffer() { |
55 DCHECK(main_thread_checker_.CalledOnValidThread() || | 59 DCHECK(main_thread_checker_.CalledOnValidThread() || |
56 context_thread_checker_.CalledOnValidThread()); | 60 context_thread_checker_.CalledOnValidThread()); |
57 | 61 |
58 // Destroy references to the context3d_ before leaking it. | 62 // Destroy references to the context3d_ before leaking it. |
63 // TODO(danakj): Delete this. | |
59 if (context3d_->GetCommandBufferProxy()) | 64 if (context3d_->GetCommandBufferProxy()) |
60 context3d_->GetCommandBufferProxy()->SetLock(nullptr); | 65 context3d_->GetCommandBufferProxy()->SetLock(nullptr); |
61 lost_context_callback_proxy_.reset(); | 66 |
67 if (lost_context_callback_proxy_) { | |
68 // Disconnect lost callbacks during destruction. | |
69 lost_context_callback_proxy_.reset(); | |
70 // Flush the context before destroying it to ensure that any pending frees | |
71 // of resources are completed. Otherwise, if this context is part of a share | |
72 // group, those resources might leak. Also, any remaining side effects of | |
73 // commands issued on this context might not be visible to other contexts in | |
74 // the share group. | |
75 ContextGL()->Flush(); | |
76 } | |
62 } | 77 } |
63 | 78 |
64 gpu::CommandBufferProxyImpl* | 79 gpu::CommandBufferProxyImpl* |
65 ContextProviderCommandBuffer::GetCommandBufferProxy() { | 80 ContextProviderCommandBuffer::GetCommandBufferProxy() { |
66 return context3d_->GetCommandBufferProxy(); | 81 return context3d_->GetCommandBufferProxy(); |
67 } | 82 } |
68 | 83 |
69 WebGraphicsContext3DCommandBufferImpl* | 84 WebGraphicsContext3DCommandBufferImpl* |
70 ContextProviderCommandBuffer::WebContext3D() { | 85 ContextProviderCommandBuffer::WebContext3D() { |
71 DCHECK(context3d_); | 86 DCHECK(context3d_); |
72 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 87 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
73 DCHECK(context_thread_checker_.CalledOnValidThread()); | 88 DCHECK(context_thread_checker_.CalledOnValidThread()); |
74 | |
75 return context3d_.get(); | 89 return context3d_.get(); |
76 } | 90 } |
77 | 91 |
78 bool ContextProviderCommandBuffer::BindToCurrentThread() { | 92 bool ContextProviderCommandBuffer::BindToCurrentThread() { |
79 // This is called on the thread the context will be used. | 93 // This is called on the thread the context will be used. |
80 DCHECK(context_thread_checker_.CalledOnValidThread()); | 94 DCHECK(context_thread_checker_.CalledOnValidThread()); |
81 | 95 |
82 if (lost_context_callback_proxy_) | 96 if (lost_context_callback_proxy_) |
83 return true; | 97 return true; |
84 | 98 |
85 context3d_->SetContextType(context_type_); | 99 context3d_->SetContextType(context_type_); |
86 if (!context3d_->InitializeOnCurrentThread(memory_limits_)) | 100 if (!context3d_->InitializeOnCurrentThread(memory_limits_)) |
87 return false; | 101 return false; |
102 lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this)); | |
88 | 103 |
104 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
105 switches::kEnableGpuClientTracing)) { | |
106 // This wraps the real GLES2Implementation and we should always use this | |
107 // instead when it's present. | |
108 trace_impl_.reset(new gpu::gles2::GLES2TraceImplementation( | |
109 context3d_->GetImplementation())); | |
110 } | |
111 | |
112 // Do this last once the context is set up. | |
89 std::string unique_context_name = | 113 std::string unique_context_name = |
90 base::StringPrintf("%s-%p", debug_name_.c_str(), context3d_.get()); | 114 base::StringPrintf("%s-%p", debug_name_.c_str(), context3d_.get()); |
91 context3d_->GetGLInterface()->TraceBeginCHROMIUM("gpu_toplevel", | 115 ContextGL()->TraceBeginCHROMIUM("gpu_toplevel", unique_context_name.c_str()); |
92 unique_context_name.c_str()); | |
93 | |
94 lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this)); | |
95 return true; | 116 return true; |
96 } | 117 } |
97 | 118 |
98 void ContextProviderCommandBuffer::DetachFromThread() { | 119 void ContextProviderCommandBuffer::DetachFromThread() { |
99 context_thread_checker_.DetachFromThread(); | 120 context_thread_checker_.DetachFromThread(); |
100 } | 121 } |
101 | 122 |
102 gpu::gles2::GLES2Interface* ContextProviderCommandBuffer::ContextGL() { | 123 gpu::gles2::GLES2Interface* ContextProviderCommandBuffer::ContextGL() { |
103 DCHECK(context3d_); | 124 DCHECK(context3d_); |
104 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 125 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
105 DCHECK(context_thread_checker_.CalledOnValidThread()); | 126 DCHECK(context_thread_checker_.CalledOnValidThread()); |
106 | 127 |
128 if (trace_impl_) | |
danakj
2016/04/21 00:38:41
We *could* avoid a branch here and put a GLES2Inte
piman
2016/04/21 00:44:21
I was thinking about it, but decided that this is
danakj
2016/04/21 00:52:44
I think we normally cache the result if we call it
| |
129 return trace_impl_.get(); | |
107 return context3d_->GetImplementation(); | 130 return context3d_->GetImplementation(); |
108 } | 131 } |
109 | 132 |
110 gpu::ContextSupport* ContextProviderCommandBuffer::ContextSupport() { | 133 gpu::ContextSupport* ContextProviderCommandBuffer::ContextSupport() { |
111 return context3d_->GetContextSupport(); | 134 return context3d_->GetContextSupport(); |
112 } | 135 } |
113 | 136 |
114 class GrContext* ContextProviderCommandBuffer::GrContext() { | 137 class GrContext* ContextProviderCommandBuffer::GrContext() { |
115 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 138 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
116 DCHECK(context_thread_checker_.CalledOnValidThread()); | 139 DCHECK(context_thread_checker_.CalledOnValidThread()); |
117 | 140 |
118 if (gr_context_) | 141 if (gr_context_) |
119 return gr_context_->get(); | 142 return gr_context_->get(); |
120 | 143 |
121 gr_context_.reset(new skia_bindings::GrContextForGLES2Interface( | 144 gr_context_.reset(new skia_bindings::GrContextForGLES2Interface(ContextGL())); |
122 context3d_->GetGLInterface())); | |
123 | 145 |
124 // If GlContext is already lost, also abandon the new GrContext. | 146 // If GlContext is already lost, also abandon the new GrContext. |
125 if (gr_context_->get() && | 147 if (gr_context_->get() && |
126 ContextGL()->GetGraphicsResetStatusKHR() != GL_NO_ERROR) | 148 ContextGL()->GetGraphicsResetStatusKHR() != GL_NO_ERROR) |
127 gr_context_->get()->abandonContext(); | 149 gr_context_->get()->abandonContext(); |
128 | 150 |
129 return gr_context_->get(); | 151 return gr_context_->get(); |
130 } | 152 } |
131 | 153 |
132 void ContextProviderCommandBuffer::InvalidateGrContext(uint32_t state) { | 154 void ContextProviderCommandBuffer::InvalidateGrContext(uint32_t state) { |
133 if (gr_context_) { | 155 if (gr_context_) { |
134 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 156 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
135 DCHECK(context_thread_checker_.CalledOnValidThread()); | 157 DCHECK(context_thread_checker_.CalledOnValidThread()); |
136 gr_context_->ResetContext(state); | 158 gr_context_->ResetContext(state); |
137 } | 159 } |
138 } | 160 } |
139 | 161 |
140 void ContextProviderCommandBuffer::SetupLock() { | 162 void ContextProviderCommandBuffer::SetupLock() { |
141 DCHECK(context3d_); | 163 DCHECK(context3d_); |
142 context3d_->GetCommandBufferProxy()->SetLock(&context_lock_); | 164 context3d_->GetCommandBufferProxy()->SetLock(&context_lock_); |
143 } | 165 } |
144 | 166 |
145 base::Lock* ContextProviderCommandBuffer::GetLock() { | 167 base::Lock* ContextProviderCommandBuffer::GetLock() { |
146 return &context_lock_; | 168 return &context_lock_; |
147 } | 169 } |
148 | 170 |
149 gpu::Capabilities ContextProviderCommandBuffer::ContextCapabilities() { | 171 gpu::Capabilities ContextProviderCommandBuffer::ContextCapabilities() { |
150 DCHECK(lost_context_callback_proxy_); // Is bound to thread. | 172 DCHECK(lost_context_callback_proxy_); // Is bound to thread. |
151 DCHECK(context_thread_checker_.CalledOnValidThread()); | 173 DCHECK(context_thread_checker_.CalledOnValidThread()); |
174 // Skips past the trace_impl_ as it doesn't have capabilities. | |
152 return context3d_->GetImplementation()->capabilities(); | 175 return context3d_->GetImplementation()->capabilities(); |
153 } | 176 } |
154 | 177 |
155 void ContextProviderCommandBuffer::DeleteCachedResources() { | 178 void ContextProviderCommandBuffer::DeleteCachedResources() { |
156 DCHECK(context_thread_checker_.CalledOnValidThread()); | 179 DCHECK(context_thread_checker_.CalledOnValidThread()); |
157 | 180 |
158 if (gr_context_) | 181 if (gr_context_) |
159 gr_context_->FreeGpuResources(); | 182 gr_context_->FreeGpuResources(); |
160 } | 183 } |
161 | 184 |
162 void ContextProviderCommandBuffer::OnLostContext() { | 185 void ContextProviderCommandBuffer::OnLostContext() { |
163 DCHECK(context_thread_checker_.CalledOnValidThread()); | 186 DCHECK(context_thread_checker_.CalledOnValidThread()); |
164 | 187 |
165 if (!lost_context_callback_.is_null()) | 188 if (!lost_context_callback_.is_null()) |
166 lost_context_callback_.Run(); | 189 lost_context_callback_.Run(); |
167 if (gr_context_) | 190 if (gr_context_) |
168 gr_context_->OnLostContext(); | 191 gr_context_->OnLostContext(); |
169 } | 192 } |
170 | 193 |
171 void ContextProviderCommandBuffer::SetLostContextCallback( | 194 void ContextProviderCommandBuffer::SetLostContextCallback( |
172 const LostContextCallback& lost_context_callback) { | 195 const LostContextCallback& lost_context_callback) { |
173 DCHECK(context_thread_checker_.CalledOnValidThread()); | 196 DCHECK(context_thread_checker_.CalledOnValidThread()); |
174 DCHECK(lost_context_callback_.is_null() || | 197 DCHECK(lost_context_callback_.is_null() || |
175 lost_context_callback.is_null()); | 198 lost_context_callback.is_null()); |
176 lost_context_callback_ = lost_context_callback; | 199 lost_context_callback_ = lost_context_callback; |
177 } | 200 } |
178 | 201 |
179 } // namespace content | 202 } // namespace content |
OLD | NEW |