Roll leveldb 3f7758:803d69 (v1.17 -> v1.18)
[chromium-blink-merge.git] / gpu / command_buffer / client / gl_in_process_context.cc
blob015a2a7824bf3a8e925e43fa5ec6e3d404689334
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"
7 #include <set>
8 #include <utility>
9 #include <vector>
11 #include <GLES2/gl2.h>
12 #ifndef GL_GLEXT_PROTOTYPES
13 #define GL_GLEXT_PROTOTYPES 1
14 #endif
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"
34 #endif
36 namespace gpu {
38 namespace {
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> {
48 public:
49 explicit GLInProcessContextImpl(
50 const GLInProcessContextSharedMemoryLimits& mem_limits);
51 ~GLInProcessContextImpl() override;
53 bool Initialize(scoped_refptr<gfx::GLSurface> surface,
54 bool is_offscreen,
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;
73 #endif
75 private:
76 void Destroy();
77 void OnContextLost();
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_;
86 bool context_lost_;
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);
107 Destroy();
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,
132 bool is_offscreen,
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();
161 it++) {
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);
168 break;
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,
181 is_offscreen,
182 window,
183 size,
184 attrib_vector,
185 gpu_preference,
186 wrapped_callback,
187 share_command_buffer,
188 gpu_memory_buffer_manager,
189 image_factory)) {
190 LOG(ERROR) << "Failed to initialize InProcessCommmandBuffer";
191 return false;
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";
198 Destroy();
199 return false;
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(),
213 share_group.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)) {
230 return false;
233 return true;
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
242 // share group.
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);
258 #endif
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) {
270 // static
271 GLInProcessContext* GLInProcessContext::Create(
272 scoped_refptr<gpu::InProcessCommandBuffer::Service> service,
273 scoped_refptr<gfx::GLSurface> surface,
274 bool is_offscreen,
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);
285 if (surface.get()) {
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,
294 is_offscreen,
295 use_global_share_group,
296 share_context,
297 window,
298 size,
299 attribs,
300 gpu_preference,
301 service,
302 gpu_memory_buffer_manager,
303 image_factory))
304 return NULL;
306 return context.release();
309 } // namespace gpu