OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "gpu/command_buffer/client/gl_in_process_context.h" | 5 #include "gpu/command_buffer/client/gl_in_process_context.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <set> | 10 #include <set> |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 class GLInProcessContextImpl | 50 class GLInProcessContextImpl |
51 : public GLInProcessContext, | 51 : public GLInProcessContext, |
52 public base::SupportsWeakPtr<GLInProcessContextImpl> { | 52 public base::SupportsWeakPtr<GLInProcessContextImpl> { |
53 public: | 53 public: |
54 explicit GLInProcessContextImpl( | 54 explicit GLInProcessContextImpl( |
55 const GLInProcessContextSharedMemoryLimits& mem_limits); | 55 const GLInProcessContextSharedMemoryLimits& mem_limits); |
56 ~GLInProcessContextImpl() override; | 56 ~GLInProcessContextImpl() override; |
57 | 57 |
58 bool Initialize(scoped_refptr<gfx::GLSurface> surface, | 58 bool Initialize(scoped_refptr<gfx::GLSurface> surface, |
59 bool is_offscreen, | 59 bool is_offscreen, |
60 bool use_global_share_group, | |
61 GLInProcessContext* share_context, | 60 GLInProcessContext* share_context, |
62 gfx::AcceleratedWidget window, | 61 gfx::AcceleratedWidget window, |
63 const gfx::Size& size, | 62 const gfx::Size& size, |
64 const gpu::gles2::ContextCreationAttribHelper& attribs, | 63 const gpu::gles2::ContextCreationAttribHelper& attribs, |
65 gfx::GpuPreference gpu_preference, | 64 gfx::GpuPreference gpu_preference, |
66 const scoped_refptr<InProcessCommandBuffer::Service>& service, | 65 const scoped_refptr<InProcessCommandBuffer::Service>& service, |
67 GpuMemoryBufferManager* gpu_memory_buffer_manager, | 66 GpuMemoryBufferManager* gpu_memory_buffer_manager, |
68 ImageFactory* image_factory); | 67 ImageFactory* image_factory); |
69 | 68 |
70 // GLInProcessContext implementation: | 69 // GLInProcessContext implementation: |
71 void SetContextLostCallback(const base::Closure& callback) override; | |
72 gles2::GLES2Implementation* GetImplementation() override; | 70 gles2::GLES2Implementation* GetImplementation() override; |
73 size_t GetMappedMemoryLimit() override; | 71 size_t GetMappedMemoryLimit() override; |
74 void SetLock(base::Lock* lock) override; | 72 void SetLock(base::Lock* lock) override; |
75 | 73 |
76 #if defined(OS_ANDROID) | 74 #if defined(OS_ANDROID) |
77 scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture( | 75 scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture( |
78 uint32_t stream_id) override; | 76 uint32_t stream_id) override; |
79 uint32_t CreateStreamTexture(uint32_t texture_id) override; | 77 uint32_t CreateStreamTexture(uint32_t texture_id) override; |
80 #endif | 78 #endif |
81 | 79 |
82 private: | 80 private: |
83 void Destroy(); | 81 void Destroy(); |
84 void OnContextLost(); | |
85 void OnSignalSyncPoint(const base::Closure& callback); | 82 void OnSignalSyncPoint(const base::Closure& callback); |
86 | 83 |
87 scoped_ptr<gles2::GLES2CmdHelper> gles2_helper_; | 84 scoped_ptr<gles2::GLES2CmdHelper> gles2_helper_; |
88 scoped_ptr<TransferBuffer> transfer_buffer_; | 85 scoped_ptr<TransferBuffer> transfer_buffer_; |
89 scoped_ptr<gles2::GLES2Implementation> gles2_implementation_; | 86 scoped_ptr<gles2::GLES2Implementation> gles2_implementation_; |
90 scoped_ptr<InProcessCommandBuffer> command_buffer_; | 87 scoped_ptr<InProcessCommandBuffer> command_buffer_; |
91 | 88 |
92 const GLInProcessContextSharedMemoryLimits mem_limits_; | 89 const GLInProcessContextSharedMemoryLimits mem_limits_; |
93 bool context_lost_; | |
94 base::Closure context_lost_callback_; | |
95 base::Lock* lock_; | |
96 | 90 |
97 DISALLOW_COPY_AND_ASSIGN(GLInProcessContextImpl); | 91 DISALLOW_COPY_AND_ASSIGN(GLInProcessContextImpl); |
98 }; | 92 }; |
99 | 93 |
100 base::LazyInstance<base::Lock> g_all_shared_contexts_lock = | |
101 LAZY_INSTANCE_INITIALIZER; | |
102 base::LazyInstance<std::set<GLInProcessContextImpl*> > g_all_shared_contexts = | |
103 LAZY_INSTANCE_INITIALIZER; | |
104 | |
105 GLInProcessContextImpl::GLInProcessContextImpl( | 94 GLInProcessContextImpl::GLInProcessContextImpl( |
106 const GLInProcessContextSharedMemoryLimits& mem_limits) | 95 const GLInProcessContextSharedMemoryLimits& mem_limits) |
107 : mem_limits_(mem_limits), context_lost_(false), lock_(nullptr) { | 96 : mem_limits_(mem_limits) {} |
108 } | |
109 | 97 |
110 GLInProcessContextImpl::~GLInProcessContextImpl() { | 98 GLInProcessContextImpl::~GLInProcessContextImpl() { |
111 { | |
112 base::AutoLock lock(g_all_shared_contexts_lock.Get()); | |
113 g_all_shared_contexts.Get().erase(this); | |
114 } | |
115 Destroy(); | 99 Destroy(); |
116 } | 100 } |
117 | 101 |
118 gles2::GLES2Implementation* GLInProcessContextImpl::GetImplementation() { | 102 gles2::GLES2Implementation* GLInProcessContextImpl::GetImplementation() { |
119 return gles2_implementation_.get(); | 103 return gles2_implementation_.get(); |
120 } | 104 } |
121 | 105 |
122 size_t GLInProcessContextImpl::GetMappedMemoryLimit() { | 106 size_t GLInProcessContextImpl::GetMappedMemoryLimit() { |
123 return mem_limits_.mapped_memory_reclaim_limit; | 107 return mem_limits_.mapped_memory_reclaim_limit; |
124 } | 108 } |
125 | 109 |
126 void GLInProcessContextImpl::SetLock(base::Lock* lock) { | 110 void GLInProcessContextImpl::SetLock(base::Lock* lock) { |
127 command_buffer_->SetLock(lock); | 111 NOTREACHED(); |
128 lock_ = lock; | |
129 } | |
130 | |
131 void GLInProcessContextImpl::SetContextLostCallback( | |
132 const base::Closure& callback) { | |
133 context_lost_callback_ = callback; | |
134 } | |
135 | |
136 void GLInProcessContextImpl::OnContextLost() { | |
137 scoped_ptr<base::AutoLock> lock; | |
138 if (lock_) | |
139 lock.reset(new base::AutoLock(*lock_)); | |
140 context_lost_ = true; | |
141 if (!context_lost_callback_.is_null()) { | |
142 context_lost_callback_.Run(); | |
143 } | |
144 } | 112 } |
145 | 113 |
146 bool GLInProcessContextImpl::Initialize( | 114 bool GLInProcessContextImpl::Initialize( |
147 scoped_refptr<gfx::GLSurface> surface, | 115 scoped_refptr<gfx::GLSurface> surface, |
148 bool is_offscreen, | 116 bool is_offscreen, |
149 bool use_global_share_group, | |
150 GLInProcessContext* share_context, | 117 GLInProcessContext* share_context, |
151 gfx::AcceleratedWidget window, | 118 gfx::AcceleratedWidget window, |
152 const gfx::Size& size, | 119 const gfx::Size& size, |
153 const gles2::ContextCreationAttribHelper& attribs, | 120 const gles2::ContextCreationAttribHelper& attribs, |
154 gfx::GpuPreference gpu_preference, | 121 gfx::GpuPreference gpu_preference, |
155 const scoped_refptr<InProcessCommandBuffer::Service>& service, | 122 const scoped_refptr<InProcessCommandBuffer::Service>& service, |
156 GpuMemoryBufferManager* gpu_memory_buffer_manager, | 123 GpuMemoryBufferManager* gpu_memory_buffer_manager, |
157 ImageFactory* image_factory) { | 124 ImageFactory* image_factory) { |
158 DCHECK(!use_global_share_group || !share_context); | |
159 DCHECK(size.width() >= 0 && size.height() >= 0); | 125 DCHECK(size.width() >= 0 && size.height() >= 0); |
160 | 126 |
161 std::vector<int32_t> attrib_vector; | 127 std::vector<int32_t> attrib_vector; |
162 attribs.Serialize(&attrib_vector); | 128 attribs.Serialize(&attrib_vector); |
163 | 129 |
164 base::Closure wrapped_callback = | |
165 base::Bind(&GLInProcessContextImpl::OnContextLost, AsWeakPtr()); | |
166 command_buffer_.reset(new InProcessCommandBuffer(service)); | 130 command_buffer_.reset(new InProcessCommandBuffer(service)); |
167 | 131 |
168 scoped_ptr<base::AutoLock> scoped_shared_context_lock; | |
169 scoped_refptr<gles2::ShareGroup> share_group; | 132 scoped_refptr<gles2::ShareGroup> share_group; |
170 InProcessCommandBuffer* share_command_buffer = NULL; | 133 InProcessCommandBuffer* share_command_buffer = nullptr; |
171 if (use_global_share_group) { | 134 if (share_context) { |
172 scoped_shared_context_lock.reset( | |
173 new base::AutoLock(g_all_shared_contexts_lock.Get())); | |
174 for (std::set<GLInProcessContextImpl*>::const_iterator it = | |
175 g_all_shared_contexts.Get().begin(); | |
176 it != g_all_shared_contexts.Get().end(); | |
177 it++) { | |
178 const GLInProcessContextImpl* context = *it; | |
179 if (!context->context_lost_) { | |
180 share_group = context->gles2_implementation_->share_group(); | |
181 share_command_buffer = context->command_buffer_.get(); | |
182 DCHECK(share_group.get()); | |
183 DCHECK(share_command_buffer); | |
184 break; | |
185 } | |
186 } | |
187 } else if (share_context) { | |
188 GLInProcessContextImpl* impl = | 135 GLInProcessContextImpl* impl = |
189 static_cast<GLInProcessContextImpl*>(share_context); | 136 static_cast<GLInProcessContextImpl*>(share_context); |
190 share_group = impl->gles2_implementation_->share_group(); | 137 share_group = impl->gles2_implementation_->share_group(); |
191 share_command_buffer = impl->command_buffer_.get(); | 138 share_command_buffer = impl->command_buffer_.get(); |
192 DCHECK(share_group.get()); | 139 DCHECK(share_group); |
193 DCHECK(share_command_buffer); | 140 DCHECK(share_command_buffer); |
194 } | 141 } |
195 | 142 |
196 if (!command_buffer_->Initialize(surface, | 143 if (!command_buffer_->Initialize(surface, |
197 is_offscreen, | 144 is_offscreen, |
198 window, | 145 window, |
199 size, | 146 size, |
200 attrib_vector, | 147 attrib_vector, |
201 gpu_preference, | 148 gpu_preference, |
202 wrapped_callback, | |
203 share_command_buffer, | 149 share_command_buffer, |
204 gpu_memory_buffer_manager, | 150 gpu_memory_buffer_manager, |
205 image_factory)) { | 151 image_factory)) { |
206 LOG(ERROR) << "Failed to initialize InProcessCommmandBuffer"; | 152 DLOG(ERROR) << "Failed to initialize InProcessCommmandBuffer"; |
207 return false; | 153 return false; |
208 } | 154 } |
209 | 155 |
210 // Create the GLES2 helper, which writes the command buffer protocol. | 156 // Create the GLES2 helper, which writes the command buffer protocol. |
211 gles2_helper_.reset(new gles2::GLES2CmdHelper(command_buffer_.get())); | 157 gles2_helper_.reset(new gles2::GLES2CmdHelper(command_buffer_.get())); |
212 if (!gles2_helper_->Initialize(mem_limits_.command_buffer_size)) { | 158 if (!gles2_helper_->Initialize(mem_limits_.command_buffer_size)) { |
213 LOG(ERROR) << "Failed to initialize GLES2CmdHelper"; | 159 LOG(ERROR) << "Failed to initialize GLES2CmdHelper"; |
214 Destroy(); | 160 Destroy(); |
215 return false; | 161 return false; |
216 } | 162 } |
217 | 163 |
218 // Create a transfer buffer. | 164 // Create a transfer buffer. |
219 transfer_buffer_.reset(new TransferBuffer(gles2_helper_.get())); | 165 transfer_buffer_.reset(new TransferBuffer(gles2_helper_.get())); |
220 | 166 |
221 // Check for consistency. | 167 // Check for consistency. |
222 DCHECK(!attribs.bind_generates_resource); | 168 DCHECK(!attribs.bind_generates_resource); |
223 const bool bind_generates_resource = false; | 169 const bool bind_generates_resource = false; |
224 const bool support_client_side_arrays = false; | 170 const bool support_client_side_arrays = false; |
225 | 171 |
226 // Create the object exposing the OpenGL API. | 172 // Create the object exposing the OpenGL API. |
227 gles2_implementation_.reset( | 173 gles2_implementation_.reset( |
228 new gles2::GLES2Implementation(gles2_helper_.get(), | 174 new gles2::GLES2Implementation(gles2_helper_.get(), |
229 share_group.get(), | 175 share_group.get(), |
230 transfer_buffer_.get(), | 176 transfer_buffer_.get(), |
231 bind_generates_resource, | 177 bind_generates_resource, |
232 attribs.lose_context_when_out_of_memory, | 178 attribs.lose_context_when_out_of_memory, |
233 support_client_side_arrays, | 179 support_client_side_arrays, |
234 command_buffer_.get())); | 180 command_buffer_.get())); |
235 | 181 |
236 if (use_global_share_group) { | |
237 g_all_shared_contexts.Get().insert(this); | |
238 scoped_shared_context_lock.reset(); | |
239 } | |
240 | |
241 if (!gles2_implementation_->Initialize( | 182 if (!gles2_implementation_->Initialize( |
242 mem_limits_.start_transfer_buffer_size, | 183 mem_limits_.start_transfer_buffer_size, |
243 mem_limits_.min_transfer_buffer_size, | 184 mem_limits_.min_transfer_buffer_size, |
244 mem_limits_.max_transfer_buffer_size, | 185 mem_limits_.max_transfer_buffer_size, |
245 mem_limits_.mapped_memory_reclaim_limit)) { | 186 mem_limits_.mapped_memory_reclaim_limit)) { |
246 return false; | 187 return false; |
247 } | 188 } |
248 | 189 |
249 return true; | 190 return true; |
250 } | 191 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 } | 229 } |
289 | 230 |
290 // static | 231 // static |
291 GLInProcessContext* GLInProcessContext::Create( | 232 GLInProcessContext* GLInProcessContext::Create( |
292 scoped_refptr<gpu::InProcessCommandBuffer::Service> service, | 233 scoped_refptr<gpu::InProcessCommandBuffer::Service> service, |
293 scoped_refptr<gfx::GLSurface> surface, | 234 scoped_refptr<gfx::GLSurface> surface, |
294 bool is_offscreen, | 235 bool is_offscreen, |
295 gfx::AcceleratedWidget window, | 236 gfx::AcceleratedWidget window, |
296 const gfx::Size& size, | 237 const gfx::Size& size, |
297 GLInProcessContext* share_context, | 238 GLInProcessContext* share_context, |
298 bool use_global_share_group, | |
299 const ::gpu::gles2::ContextCreationAttribHelper& attribs, | 239 const ::gpu::gles2::ContextCreationAttribHelper& attribs, |
300 gfx::GpuPreference gpu_preference, | 240 gfx::GpuPreference gpu_preference, |
301 const GLInProcessContextSharedMemoryLimits& memory_limits, | 241 const GLInProcessContextSharedMemoryLimits& memory_limits, |
302 GpuMemoryBufferManager* gpu_memory_buffer_manager, | 242 GpuMemoryBufferManager* gpu_memory_buffer_manager, |
303 ImageFactory* image_factory) { | 243 ImageFactory* image_factory) { |
304 DCHECK(!use_global_share_group || !share_context); | |
305 if (surface.get()) { | 244 if (surface.get()) { |
306 DCHECK_EQ(surface->IsOffscreen(), is_offscreen); | 245 DCHECK_EQ(surface->IsOffscreen(), is_offscreen); |
307 DCHECK(surface->GetSize() == size); | 246 DCHECK(surface->GetSize() == size); |
308 DCHECK_EQ(gfx::kNullAcceleratedWidget, window); | 247 DCHECK_EQ(gfx::kNullAcceleratedWidget, window); |
309 } | 248 } |
310 | 249 |
311 scoped_ptr<GLInProcessContextImpl> context( | 250 scoped_ptr<GLInProcessContextImpl> context( |
312 new GLInProcessContextImpl(memory_limits)); | 251 new GLInProcessContextImpl(memory_limits)); |
313 if (!context->Initialize(surface, | 252 if (!context->Initialize(surface, |
314 is_offscreen, | 253 is_offscreen, |
315 use_global_share_group, | |
316 share_context, | 254 share_context, |
317 window, | 255 window, |
318 size, | 256 size, |
319 attribs, | 257 attribs, |
320 gpu_preference, | 258 gpu_preference, |
321 service, | 259 service, |
322 gpu_memory_buffer_manager, | 260 gpu_memory_buffer_manager, |
323 image_factory)) | 261 image_factory)) |
324 return NULL; | 262 return NULL; |
325 | 263 |
326 return context.release(); | 264 return context.release(); |
327 } | 265 } |
328 | 266 |
329 } // namespace gpu | 267 } // namespace gpu |
OLD | NEW |