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

Side by Side Diff: content/common/gpu/client/context_provider_command_buffer.cc

Issue 1414683003: Fix gpu command buffer use after free by GrContext (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: test fixup Created 5 years, 1 month 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 "content/common/gpu/client/context_provider_command_buffer.h" 5 #include "content/common/gpu/client/context_provider_command_buffer.h"
6 6
7 #include <set> 7 #include <set>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/callback_helpers.h" 10 #include "base/callback_helpers.h"
11 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "cc/output/managed_memory_policy.h" 12 #include "cc/output/managed_memory_policy.h"
13 #include "content/common/gpu/client/grcontext_for_webgraphicscontext3d.h" 13 #include "content/common/gpu/client/grcontext_for_webgraphicscontext3d.h"
14 #include "gpu/command_buffer/client/gles2_implementation.h" 14 #include "gpu/command_buffer/client/gles2_implementation.h"
15 #include "third_party/skia/include/gpu/GrContext.h" 15 #include "third_party/skia/include/gpu/GrContext.h"
16 16
17 namespace content { 17 namespace content {
18 18
19 class ContextProviderCommandBuffer::LostContextCallbackProxy 19 class ContextProviderCommandBuffer::LostContextCallbackProxy
20 : public blink::WebGraphicsContext3D::WebGraphicsContextLostCallback { 20 : public blink::WebGraphicsContext3D::WebGraphicsContextLostCallback {
21 public: 21 public:
22 explicit LostContextCallbackProxy(ContextProviderCommandBuffer* provider) 22 explicit LostContextCallbackProxy(ContextProviderCommandBuffer* provider)
23 : provider_(provider) { 23 : provider_(provider) {
24 provider_->context3d_->setContextLostCallback(this); 24 provider_->WebContext3DNoChecks()->setContextLostCallback(this);
25 } 25 }
26 26
27 ~LostContextCallbackProxy() override { 27 ~LostContextCallbackProxy() override {
28 provider_->context3d_->setContextLostCallback(NULL); 28 provider_->WebContext3DNoChecks()->setContextLostCallback(NULL);
29 } 29 }
30 30
31 void onContextLost() override { provider_->OnLostContext(); } 31 void onContextLost() override { provider_->OnLostContext(); }
32 32
33 private: 33 private:
34 ContextProviderCommandBuffer* provider_; 34 ContextProviderCommandBuffer* provider_;
35 }; 35 };
36 36
37 scoped_refptr<ContextProviderCommandBuffer> 37 scoped_refptr<ContextProviderCommandBuffer>
38 ContextProviderCommandBuffer::Create( 38 ContextProviderCommandBuffer::Create(
39 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context3d, 39 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context3d,
40 CommandBufferContextType type) { 40 CommandBufferContextType type) {
41 if (!context3d) 41 if (!context3d)
42 return NULL; 42 return NULL;
43 43
44 return new ContextProviderCommandBuffer(context3d.Pass(), type); 44 return new ContextProviderCommandBuffer(context3d.Pass(), type);
45 } 45 }
46 46
47 ContextProviderCommandBuffer::ContextProviderCommandBuffer( 47 ContextProviderCommandBuffer::ContextProviderCommandBuffer(
48 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context3d, 48 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> context3d,
49 CommandBufferContextType type) 49 CommandBufferContextType type)
50 : context3d_(context3d.Pass()), 50 : context_type_(type),
51 context_type_(type),
52 debug_name_(CommandBufferContextTypeToString(type)) { 51 debug_name_(CommandBufferContextTypeToString(type)) {
52 gr_interface_ = skia::AdoptRef(new GrGLInterfaceForWebGraphicsContext3D(
53 context3d.Pass()));
53 DCHECK(main_thread_checker_.CalledOnValidThread()); 54 DCHECK(main_thread_checker_.CalledOnValidThread());
54 DCHECK(context3d_); 55 DCHECK(gr_interface_->WebContext3D());
55 context_thread_checker_.DetachFromThread(); 56 context_thread_checker_.DetachFromThread();
56 } 57 }
57 58
58 ContextProviderCommandBuffer::~ContextProviderCommandBuffer() { 59 ContextProviderCommandBuffer::~ContextProviderCommandBuffer() {
59 DCHECK(main_thread_checker_.CalledOnValidThread() || 60 DCHECK(main_thread_checker_.CalledOnValidThread() ||
60 context_thread_checker_.CalledOnValidThread()); 61 context_thread_checker_.CalledOnValidThread());
61 62
62 // Destroy references to the context3d_ before leaking it. 63 // Destroy references to the context3d_ before leaking it.
63 if (context3d_->GetCommandBufferProxy()) 64 if (WebContext3DNoChecks()->GetCommandBufferProxy())
64 context3d_->GetCommandBufferProxy()->SetLock(nullptr); 65 WebContext3DNoChecks()->GetCommandBufferProxy()->SetLock(nullptr);
65 lost_context_callback_proxy_.reset(); 66 lost_context_callback_proxy_.reset();
66 } 67 }
67 68
68 69
69 CommandBufferProxyImpl* ContextProviderCommandBuffer::GetCommandBufferProxy() { 70 CommandBufferProxyImpl* ContextProviderCommandBuffer::GetCommandBufferProxy() {
70 return context3d_->GetCommandBufferProxy(); 71 return WebContext3D()->GetCommandBufferProxy();
71 } 72 }
72 73
73 WebGraphicsContext3DCommandBufferImpl* 74 WebGraphicsContext3DCommandBufferImpl*
74 ContextProviderCommandBuffer::WebContext3D() { 75 ContextProviderCommandBuffer::WebContext3D() {
75 DCHECK(context3d_); 76 DCHECK(gr_interface_);
77 DCHECK(gr_interface_->WebContext3D());
76 DCHECK(lost_context_callback_proxy_); // Is bound to thread. 78 DCHECK(lost_context_callback_proxy_); // Is bound to thread.
77 DCHECK(context_thread_checker_.CalledOnValidThread()); 79 DCHECK(context_thread_checker_.CalledOnValidThread());
78 80
79 return context3d_.get(); 81 return WebContext3DNoChecks();
82 }
83
84 WebGraphicsContext3DCommandBufferImpl*
85 ContextProviderCommandBuffer::WebContext3DNoChecks() {
86 DCHECK(gr_interface_);
87 return static_cast<WebGraphicsContext3DCommandBufferImpl*>(
88 gr_interface_->WebContext3D());
80 } 89 }
81 90
82 bool ContextProviderCommandBuffer::BindToCurrentThread() { 91 bool ContextProviderCommandBuffer::BindToCurrentThread() {
83 // This is called on the thread the context will be used. 92 // This is called on the thread the context will be used.
84 DCHECK(context_thread_checker_.CalledOnValidThread()); 93 DCHECK(context_thread_checker_.CalledOnValidThread());
94 DCHECK(gr_interface_ && gr_interface_->WebContext3D());
85 95
86 if (lost_context_callback_proxy_) 96 if (lost_context_callback_proxy_)
87 return true; 97 return true;
88 98
89 context3d_->SetContextType(context_type_); 99 WebContext3DNoChecks()->SetContextType(context_type_);
90 if (!context3d_->InitializeOnCurrentThread()) 100 if (!WebContext3DNoChecks()->InitializeOnCurrentThread())
91 return false; 101 return false;
92 102
103 gr_interface_->BindToCurrentThread();
93 InitializeCapabilities(); 104 InitializeCapabilities();
94 105
95 std::string unique_context_name = 106 std::string unique_context_name =
96 base::StringPrintf("%s-%p", debug_name_.c_str(), context3d_.get()); 107 base::StringPrintf("%s-%p", debug_name_.c_str(), WebContext3DNoChecks());
97 context3d_->traceBeginCHROMIUM("gpu_toplevel", unique_context_name.c_str()); 108 WebContext3DNoChecks()->traceBeginCHROMIUM("gpu_toplevel",
109 unique_context_name.c_str());
98 110
99 lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this)); 111 lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this));
100 return true; 112 return true;
101 } 113 }
102 114
103 void ContextProviderCommandBuffer::DetachFromThread() { 115 void ContextProviderCommandBuffer::DetachFromThread() {
104 context_thread_checker_.DetachFromThread(); 116 context_thread_checker_.DetachFromThread();
105 } 117 }
106 118
107 gpu::gles2::GLES2Interface* ContextProviderCommandBuffer::ContextGL() { 119 gpu::gles2::GLES2Interface* ContextProviderCommandBuffer::ContextGL() {
108 DCHECK(context3d_);
109 DCHECK(lost_context_callback_proxy_); // Is bound to thread. 120 DCHECK(lost_context_callback_proxy_); // Is bound to thread.
110 DCHECK(context_thread_checker_.CalledOnValidThread());
111 121
112 return context3d_->GetImplementation(); 122 return WebContext3D()->GetImplementation();
113 } 123 }
114 124
115 gpu::ContextSupport* ContextProviderCommandBuffer::ContextSupport() { 125 gpu::ContextSupport* ContextProviderCommandBuffer::ContextSupport() {
116 return context3d_->GetContextSupport(); 126 return WebContext3D()->GetContextSupport();
117 } 127 }
118 128
119 class GrContext* ContextProviderCommandBuffer::GrContext() { 129 class GrContext* ContextProviderCommandBuffer::GrContext() {
120 DCHECK(lost_context_callback_proxy_); // Is bound to thread. 130 DCHECK(lost_context_callback_proxy_); // Is bound to thread.
121 DCHECK(context_thread_checker_.CalledOnValidThread()); 131 DCHECK(context_thread_checker_.CalledOnValidThread());
122 132
123 if (gr_context_) 133 if (gr_context_)
124 return gr_context_->get(); 134 return gr_context_->get();
125 135
126 gr_context_.reset(new GrContextForWebGraphicsContext3D(context3d_.get())); 136 gr_context_.reset(new GrContextForWebGraphicsContext3D(gr_interface_));
127 137
128 // If GlContext is already lost, also abandon the new GrContext. 138 // If GlContext is already lost, also abandon the new GrContext.
129 if (gr_context_->get() && 139 if (gr_context_->get() &&
130 ContextGL()->GetGraphicsResetStatusKHR() != GL_NO_ERROR) 140 ContextGL()->GetGraphicsResetStatusKHR() != GL_NO_ERROR)
131 gr_context_->get()->abandonContext(); 141 gr_context_->get()->abandonContext();
132 142
133 return gr_context_->get(); 143 return gr_context_->get();
134 } 144 }
135 145
136 void ContextProviderCommandBuffer::InvalidateGrContext(uint32_t state) { 146 void ContextProviderCommandBuffer::InvalidateGrContext(uint32_t state) {
137 if (gr_context_) { 147 if (gr_context_) {
138 DCHECK(lost_context_callback_proxy_); // Is bound to thread. 148 DCHECK(lost_context_callback_proxy_); // Is bound to thread.
139 DCHECK(context_thread_checker_.CalledOnValidThread()); 149 DCHECK(context_thread_checker_.CalledOnValidThread());
140 gr_context_->get()->resetContext(state); 150 gr_context_->get()->resetContext(state);
141 } 151 }
142 } 152 }
143 153
144 void ContextProviderCommandBuffer::SetupLock() { 154 void ContextProviderCommandBuffer::SetupLock() {
145 DCHECK(context3d_); 155 WebContext3D()->GetCommandBufferProxy()->SetLock(&context_lock_);
146 context3d_->GetCommandBufferProxy()->SetLock(&context_lock_);
147 } 156 }
148 157
149 base::Lock* ContextProviderCommandBuffer::GetLock() { 158 base::Lock* ContextProviderCommandBuffer::GetLock() {
150 return &context_lock_; 159 return &context_lock_;
151 } 160 }
152 161
153 cc::ContextProvider::Capabilities 162 cc::ContextProvider::Capabilities
154 ContextProviderCommandBuffer::ContextCapabilities() { 163 ContextProviderCommandBuffer::ContextCapabilities() {
155 DCHECK(lost_context_callback_proxy_); // Is bound to thread. 164 DCHECK(lost_context_callback_proxy_); // Is bound to thread.
156 DCHECK(context_thread_checker_.CalledOnValidThread()); 165 DCHECK(context_thread_checker_.CalledOnValidThread());
(...skipping 12 matching lines...) Expand all
169 DCHECK(context_thread_checker_.CalledOnValidThread()); 178 DCHECK(context_thread_checker_.CalledOnValidThread());
170 179
171 if (!lost_context_callback_.is_null()) 180 if (!lost_context_callback_.is_null())
172 base::ResetAndReturn(&lost_context_callback_).Run(); 181 base::ResetAndReturn(&lost_context_callback_).Run();
173 if (gr_context_) 182 if (gr_context_)
174 gr_context_->OnLostContext(); 183 gr_context_->OnLostContext();
175 } 184 }
176 185
177 void ContextProviderCommandBuffer::InitializeCapabilities() { 186 void ContextProviderCommandBuffer::InitializeCapabilities() {
178 Capabilities caps; 187 Capabilities caps;
179 caps.gpu = context3d_->GetImplementation()->capabilities(); 188 caps.gpu = WebContext3DNoChecks()->GetImplementation()->capabilities();
180 189
181 size_t mapped_memory_limit = context3d_->GetMappedMemoryLimit(); 190 size_t mapped_memory_limit = WebContext3DNoChecks()->GetMappedMemoryLimit();
182 caps.max_transfer_buffer_usage_bytes = 191 caps.max_transfer_buffer_usage_bytes =
183 mapped_memory_limit == WebGraphicsContext3DCommandBufferImpl::kNoLimit 192 mapped_memory_limit == WebGraphicsContext3DCommandBufferImpl::kNoLimit
184 ? std::numeric_limits<size_t>::max() : mapped_memory_limit; 193 ? std::numeric_limits<size_t>::max() : mapped_memory_limit;
185 194
186 capabilities_ = caps; 195 capabilities_ = caps;
187 } 196 }
188 197
189 void ContextProviderCommandBuffer::SetLostContextCallback( 198 void ContextProviderCommandBuffer::SetLostContextCallback(
190 const LostContextCallback& lost_context_callback) { 199 const LostContextCallback& lost_context_callback) {
191 DCHECK(context_thread_checker_.CalledOnValidThread()); 200 DCHECK(context_thread_checker_.CalledOnValidThread());
192 DCHECK(lost_context_callback_.is_null() || 201 DCHECK(lost_context_callback_.is_null() ||
193 lost_context_callback.is_null()); 202 lost_context_callback.is_null());
194 lost_context_callback_ = lost_context_callback; 203 lost_context_callback_ = lost_context_callback;
195 } 204 }
196 205
197 } // namespace content 206 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698