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
3 // found in the LICENSE file.
5 #include "gpu/command_buffer/client/gl_in_process_context.h"
11 #include <GLES2/gl2.h>
12 #ifndef GL_GLEXT_PROTOTYPES
13 #define GL_GLEXT_PROTOTYPES 1
15 #include <GLES2/gl2ext.h>
16 #include <GLES2/gl2extchromium.h>
18 #include "base/bind.h"
19 #include "base/bind_helpers.h"
20 #include "base/lazy_instance.h"
21 #include "base/logging.h"
22 #include "base/memory/scoped_ptr.h"
23 #include "base/memory/weak_ptr.h"
24 #include "base/message_loop/message_loop.h"
25 #include "gpu/command_buffer/client/gles2_implementation.h"
26 #include "gpu/command_buffer/client/transfer_buffer.h"
27 #include "gpu/command_buffer/common/command_buffer.h"
28 #include "gpu/command_buffer/common/constants.h"
29 #include "ui/gfx/size.h"
30 #include "ui/gl/gl_image.h"
32 #if defined(OS_ANDROID)
33 #include "ui/gl/android/surface_texture.h"
40 const int32 kDefaultCommandBufferSize
= 1024 * 1024;
41 const unsigned int kDefaultStartTransferBufferSize
= 4 * 1024 * 1024;
42 const unsigned int kDefaultMinTransferBufferSize
= 1 * 256 * 1024;
43 const unsigned int kDefaultMaxTransferBufferSize
= 16 * 1024 * 1024;
45 class GLInProcessContextImpl
46 : public GLInProcessContext
,
47 public base::SupportsWeakPtr
<GLInProcessContextImpl
> {
49 explicit GLInProcessContextImpl(
50 const GLInProcessContextSharedMemoryLimits
& mem_limits
);
51 ~GLInProcessContextImpl() override
;
53 bool Initialize(scoped_refptr
<gfx::GLSurface
> surface
,
55 bool use_global_share_group
,
56 GLInProcessContext
* share_context
,
57 gfx::AcceleratedWidget window
,
58 const gfx::Size
& size
,
59 const gpu::gles2::ContextCreationAttribHelper
& attribs
,
60 gfx::GpuPreference gpu_preference
,
61 const scoped_refptr
<InProcessCommandBuffer::Service
>& service
,
62 GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
63 ImageFactory
* image_factory
);
65 // GLInProcessContext implementation:
66 void SetContextLostCallback(const base::Closure
& callback
) override
;
67 gles2::GLES2Implementation
* GetImplementation() override
;
68 size_t GetMappedMemoryLimit() override
;
70 #if defined(OS_ANDROID)
71 virtual scoped_refptr
<gfx::SurfaceTexture
> GetSurfaceTexture(
72 uint32 stream_id
) override
;
78 void OnSignalSyncPoint(const base::Closure
& callback
);
80 scoped_ptr
<gles2::GLES2CmdHelper
> gles2_helper_
;
81 scoped_ptr
<TransferBuffer
> transfer_buffer_
;
82 scoped_ptr
<gles2::GLES2Implementation
> gles2_implementation_
;
83 scoped_ptr
<InProcessCommandBuffer
> command_buffer_
;
85 const GLInProcessContextSharedMemoryLimits mem_limits_
;
87 base::Closure context_lost_callback_
;
89 DISALLOW_COPY_AND_ASSIGN(GLInProcessContextImpl
);
92 base::LazyInstance
<base::Lock
> g_all_shared_contexts_lock
=
93 LAZY_INSTANCE_INITIALIZER
;
94 base::LazyInstance
<std::set
<GLInProcessContextImpl
*> > g_all_shared_contexts
=
95 LAZY_INSTANCE_INITIALIZER
;
97 GLInProcessContextImpl::GLInProcessContextImpl(
98 const GLInProcessContextSharedMemoryLimits
& mem_limits
)
99 : mem_limits_(mem_limits
), context_lost_(false) {
102 GLInProcessContextImpl::~GLInProcessContextImpl() {
104 base::AutoLock
lock(g_all_shared_contexts_lock
.Get());
105 g_all_shared_contexts
.Get().erase(this);
110 gles2::GLES2Implementation
* GLInProcessContextImpl::GetImplementation() {
111 return gles2_implementation_
.get();
114 size_t GLInProcessContextImpl::GetMappedMemoryLimit() {
115 return mem_limits_
.mapped_memory_reclaim_limit
;
118 void GLInProcessContextImpl::SetContextLostCallback(
119 const base::Closure
& callback
) {
120 context_lost_callback_
= callback
;
123 void GLInProcessContextImpl::OnContextLost() {
124 context_lost_
= true;
125 if (!context_lost_callback_
.is_null()) {
126 context_lost_callback_
.Run();
130 bool GLInProcessContextImpl::Initialize(
131 scoped_refptr
<gfx::GLSurface
> surface
,
133 bool use_global_share_group
,
134 GLInProcessContext
* share_context
,
135 gfx::AcceleratedWidget window
,
136 const gfx::Size
& size
,
137 const gles2::ContextCreationAttribHelper
& attribs
,
138 gfx::GpuPreference gpu_preference
,
139 const scoped_refptr
<InProcessCommandBuffer::Service
>& service
,
140 GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
141 ImageFactory
* image_factory
) {
142 DCHECK(!use_global_share_group
|| !share_context
);
143 DCHECK(size
.width() >= 0 && size
.height() >= 0);
145 std::vector
<int32
> attrib_vector
;
146 attribs
.Serialize(&attrib_vector
);
148 base::Closure wrapped_callback
=
149 base::Bind(&GLInProcessContextImpl::OnContextLost
, AsWeakPtr());
150 command_buffer_
.reset(new InProcessCommandBuffer(service
));
152 scoped_ptr
<base::AutoLock
> scoped_shared_context_lock
;
153 scoped_refptr
<gles2::ShareGroup
> share_group
;
154 InProcessCommandBuffer
* share_command_buffer
= NULL
;
155 if (use_global_share_group
) {
156 scoped_shared_context_lock
.reset(
157 new base::AutoLock(g_all_shared_contexts_lock
.Get()));
158 for (std::set
<GLInProcessContextImpl
*>::const_iterator it
=
159 g_all_shared_contexts
.Get().begin();
160 it
!= g_all_shared_contexts
.Get().end();
162 const GLInProcessContextImpl
* context
= *it
;
163 if (!context
->context_lost_
) {
164 share_group
= context
->gles2_implementation_
->share_group();
165 share_command_buffer
= context
->command_buffer_
.get();
166 DCHECK(share_group
.get());
167 DCHECK(share_command_buffer
);
171 } else if (share_context
) {
172 GLInProcessContextImpl
* impl
=
173 static_cast<GLInProcessContextImpl
*>(share_context
);
174 share_group
= impl
->gles2_implementation_
->share_group();
175 share_command_buffer
= impl
->command_buffer_
.get();
176 DCHECK(share_group
.get());
177 DCHECK(share_command_buffer
);
180 if (!command_buffer_
->Initialize(surface
,
187 share_command_buffer
,
188 gpu_memory_buffer_manager
,
190 LOG(ERROR
) << "Failed to initialize InProcessCommmandBuffer";
194 // Create the GLES2 helper, which writes the command buffer protocol.
195 gles2_helper_
.reset(new gles2::GLES2CmdHelper(command_buffer_
.get()));
196 if (!gles2_helper_
->Initialize(mem_limits_
.command_buffer_size
)) {
197 LOG(ERROR
) << "Failed to initialize GLES2CmdHelper";
202 // Create a transfer buffer.
203 transfer_buffer_
.reset(new TransferBuffer(gles2_helper_
.get()));
205 // Check for consistency.
206 DCHECK(!attribs
.bind_generates_resource
);
207 const bool bind_generates_resource
= false;
208 const bool support_client_side_arrays
= false;
210 // Create the object exposing the OpenGL API.
211 gles2_implementation_
.reset(
212 new gles2::GLES2Implementation(gles2_helper_
.get(),
214 transfer_buffer_
.get(),
215 bind_generates_resource
,
216 attribs
.lose_context_when_out_of_memory
,
217 support_client_side_arrays
,
218 command_buffer_
.get()));
220 if (use_global_share_group
) {
221 g_all_shared_contexts
.Get().insert(this);
222 scoped_shared_context_lock
.reset();
225 if (!gles2_implementation_
->Initialize(
226 mem_limits_
.start_transfer_buffer_size
,
227 mem_limits_
.min_transfer_buffer_size
,
228 mem_limits_
.max_transfer_buffer_size
,
229 mem_limits_
.mapped_memory_reclaim_limit
)) {
236 void GLInProcessContextImpl::Destroy() {
237 if (gles2_implementation_
) {
238 // First flush the context to ensure that any pending frees of resources
239 // are completed. Otherwise, if this context is part of a share group,
240 // those resources might leak. Also, any remaining side effects of commands
241 // issued on this context might not be visible to other contexts in the
243 gles2_implementation_
->Flush();
245 gles2_implementation_
.reset();
248 transfer_buffer_
.reset();
249 gles2_helper_
.reset();
250 command_buffer_
.reset();
253 #if defined(OS_ANDROID)
254 scoped_refptr
<gfx::SurfaceTexture
>
255 GLInProcessContextImpl::GetSurfaceTexture(uint32 stream_id
) {
256 return command_buffer_
->GetSurfaceTexture(stream_id
);
260 } // anonymous namespace
262 GLInProcessContextSharedMemoryLimits::GLInProcessContextSharedMemoryLimits()
263 : command_buffer_size(kDefaultCommandBufferSize
),
264 start_transfer_buffer_size(kDefaultStartTransferBufferSize
),
265 min_transfer_buffer_size(kDefaultMinTransferBufferSize
),
266 max_transfer_buffer_size(kDefaultMaxTransferBufferSize
),
267 mapped_memory_reclaim_limit(gles2::GLES2Implementation::kNoLimit
) {
271 GLInProcessContext
* GLInProcessContext::Create(
272 scoped_refptr
<gpu::InProcessCommandBuffer::Service
> service
,
273 scoped_refptr
<gfx::GLSurface
> surface
,
275 gfx::AcceleratedWidget window
,
276 const gfx::Size
& size
,
277 GLInProcessContext
* share_context
,
278 bool use_global_share_group
,
279 const ::gpu::gles2::ContextCreationAttribHelper
& attribs
,
280 gfx::GpuPreference gpu_preference
,
281 const GLInProcessContextSharedMemoryLimits
& memory_limits
,
282 GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
283 ImageFactory
* image_factory
) {
284 DCHECK(!use_global_share_group
|| !share_context
);
286 DCHECK_EQ(surface
->IsOffscreen(), is_offscreen
);
287 DCHECK(surface
->GetSize() == size
);
288 DCHECK_EQ(gfx::kNullAcceleratedWidget
, window
);
291 scoped_ptr
<GLInProcessContextImpl
> context(
292 new GLInProcessContextImpl(memory_limits
));
293 if (!context
->Initialize(surface
,
295 use_global_share_group
,
302 gpu_memory_buffer_manager
,
306 return context
.release();