| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "gpu/command_buffer/client/gl_in_process_context.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <memory> | |
| 11 #include <set> | |
| 12 #include <utility> | |
| 13 #include <vector> | |
| 14 | |
| 15 #include <GLES2/gl2.h> | |
| 16 #ifndef GL_GLEXT_PROTOTYPES | |
| 17 #define GL_GLEXT_PROTOTYPES 1 | |
| 18 #endif | |
| 19 #include <GLES2/gl2ext.h> | |
| 20 #include <GLES2/gl2extchromium.h> | |
| 21 | |
| 22 #include "base/bind.h" | |
| 23 #include "base/bind_helpers.h" | |
| 24 #include "base/lazy_instance.h" | |
| 25 #include "base/logging.h" | |
| 26 #include "base/macros.h" | |
| 27 #include "base/memory/weak_ptr.h" | |
| 28 #include "base/message_loop/message_loop.h" | |
| 29 #include "base/threading/thread_task_runner_handle.h" | |
| 30 #include "gpu/command_buffer/client/gles2_cmd_helper.h" | |
| 31 #include "gpu/command_buffer/client/gles2_implementation.h" | |
| 32 #include "gpu/command_buffer/client/shared_memory_limits.h" | |
| 33 #include "gpu/command_buffer/client/transfer_buffer.h" | |
| 34 #include "gpu/command_buffer/common/command_buffer.h" | |
| 35 #include "gpu/command_buffer/common/constants.h" | |
| 36 #include "ui/gfx/geometry/size.h" | |
| 37 #include "ui/gl/gl_image.h" | |
| 38 | |
| 39 #if defined(OS_ANDROID) | |
| 40 #include "ui/gl/android/surface_texture.h" | |
| 41 #endif | |
| 42 | |
| 43 namespace gpu { | |
| 44 | |
| 45 namespace { | |
| 46 | |
| 47 class GLInProcessContextImpl | |
| 48 : public GLInProcessContext, | |
| 49 public base::SupportsWeakPtr<GLInProcessContextImpl> { | |
| 50 public: | |
| 51 GLInProcessContextImpl(); | |
| 52 ~GLInProcessContextImpl() override; | |
| 53 | |
| 54 bool Initialize(scoped_refptr<gl::GLSurface> surface, | |
| 55 bool is_offscreen, | |
| 56 GLInProcessContext* share_context, | |
| 57 gfx::AcceleratedWidget window, | |
| 58 const gpu::gles2::ContextCreationAttribHelper& attribs, | |
| 59 const scoped_refptr<InProcessCommandBuffer::Service>& service, | |
| 60 const SharedMemoryLimits& mem_limits, | |
| 61 GpuMemoryBufferManager* gpu_memory_buffer_manager, | |
| 62 ImageFactory* image_factory, | |
| 63 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
| 64 | |
| 65 // GLInProcessContext implementation: | |
| 66 gles2::GLES2Implementation* GetImplementation() override; | |
| 67 void SetLock(base::Lock* lock) override; | |
| 68 | |
| 69 private: | |
| 70 void Destroy(); | |
| 71 void OnSignalSyncPoint(const base::Closure& callback); | |
| 72 | |
| 73 std::unique_ptr<gles2::GLES2CmdHelper> gles2_helper_; | |
| 74 std::unique_ptr<TransferBuffer> transfer_buffer_; | |
| 75 std::unique_ptr<gles2::GLES2Implementation> gles2_implementation_; | |
| 76 std::unique_ptr<InProcessCommandBuffer> command_buffer_; | |
| 77 | |
| 78 DISALLOW_COPY_AND_ASSIGN(GLInProcessContextImpl); | |
| 79 }; | |
| 80 | |
| 81 GLInProcessContextImpl::GLInProcessContextImpl() = default; | |
| 82 | |
| 83 GLInProcessContextImpl::~GLInProcessContextImpl() { | |
| 84 Destroy(); | |
| 85 } | |
| 86 | |
| 87 gles2::GLES2Implementation* GLInProcessContextImpl::GetImplementation() { | |
| 88 return gles2_implementation_.get(); | |
| 89 } | |
| 90 | |
| 91 void GLInProcessContextImpl::SetLock(base::Lock* lock) { | |
| 92 NOTREACHED(); | |
| 93 } | |
| 94 | |
| 95 bool GLInProcessContextImpl::Initialize( | |
| 96 scoped_refptr<gl::GLSurface> surface, | |
| 97 bool is_offscreen, | |
| 98 GLInProcessContext* share_context, | |
| 99 gfx::AcceleratedWidget window, | |
| 100 const gles2::ContextCreationAttribHelper& attribs, | |
| 101 const scoped_refptr<InProcessCommandBuffer::Service>& service, | |
| 102 const SharedMemoryLimits& mem_limits, | |
| 103 GpuMemoryBufferManager* gpu_memory_buffer_manager, | |
| 104 ImageFactory* image_factory, | |
| 105 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | |
| 106 DCHECK_GE(attribs.offscreen_framebuffer_size.width(), 0); | |
| 107 DCHECK_GE(attribs.offscreen_framebuffer_size.height(), 0); | |
| 108 | |
| 109 command_buffer_.reset(new InProcessCommandBuffer(service)); | |
| 110 | |
| 111 scoped_refptr<gles2::ShareGroup> share_group; | |
| 112 InProcessCommandBuffer* share_command_buffer = nullptr; | |
| 113 if (share_context) { | |
| 114 GLInProcessContextImpl* impl = | |
| 115 static_cast<GLInProcessContextImpl*>(share_context); | |
| 116 share_group = impl->gles2_implementation_->share_group(); | |
| 117 share_command_buffer = impl->command_buffer_.get(); | |
| 118 DCHECK(share_group); | |
| 119 DCHECK(share_command_buffer); | |
| 120 } | |
| 121 | |
| 122 if (!command_buffer_->Initialize( | |
| 123 surface, is_offscreen, window, attribs, share_command_buffer, | |
| 124 gpu_memory_buffer_manager, image_factory, std::move(task_runner))) { | |
| 125 DLOG(ERROR) << "Failed to initialize InProcessCommmandBuffer"; | |
| 126 return false; | |
| 127 } | |
| 128 | |
| 129 // Create the GLES2 helper, which writes the command buffer protocol. | |
| 130 gles2_helper_.reset(new gles2::GLES2CmdHelper(command_buffer_.get())); | |
| 131 if (!gles2_helper_->Initialize(mem_limits.command_buffer_size)) { | |
| 132 LOG(ERROR) << "Failed to initialize GLES2CmdHelper"; | |
| 133 Destroy(); | |
| 134 return false; | |
| 135 } | |
| 136 | |
| 137 // Create a transfer buffer. | |
| 138 transfer_buffer_.reset(new TransferBuffer(gles2_helper_.get())); | |
| 139 | |
| 140 // Check for consistency. | |
| 141 DCHECK(!attribs.bind_generates_resource); | |
| 142 const bool bind_generates_resource = false; | |
| 143 const bool support_client_side_arrays = false; | |
| 144 | |
| 145 // Create the object exposing the OpenGL API. | |
| 146 gles2_implementation_.reset(new gles2::GLES2Implementation( | |
| 147 gles2_helper_.get(), share_group.get(), transfer_buffer_.get(), | |
| 148 bind_generates_resource, attribs.lose_context_when_out_of_memory, | |
| 149 support_client_side_arrays, command_buffer_.get())); | |
| 150 | |
| 151 if (!gles2_implementation_->Initialize( | |
| 152 mem_limits.start_transfer_buffer_size, | |
| 153 mem_limits.min_transfer_buffer_size, | |
| 154 mem_limits.max_transfer_buffer_size, | |
| 155 mem_limits.mapped_memory_reclaim_limit)) { | |
| 156 return false; | |
| 157 } | |
| 158 | |
| 159 return true; | |
| 160 } | |
| 161 | |
| 162 void GLInProcessContextImpl::Destroy() { | |
| 163 if (gles2_implementation_) { | |
| 164 // First flush the context to ensure that any pending frees of resources | |
| 165 // are completed. Otherwise, if this context is part of a share group, | |
| 166 // those resources might leak. Also, any remaining side effects of commands | |
| 167 // issued on this context might not be visible to other contexts in the | |
| 168 // share group. | |
| 169 gles2_implementation_->Flush(); | |
| 170 | |
| 171 gles2_implementation_.reset(); | |
| 172 } | |
| 173 | |
| 174 transfer_buffer_.reset(); | |
| 175 gles2_helper_.reset(); | |
| 176 command_buffer_.reset(); | |
| 177 } | |
| 178 | |
| 179 } // anonymous namespace | |
| 180 | |
| 181 // static | |
| 182 GLInProcessContext* GLInProcessContext::Create( | |
| 183 scoped_refptr<gpu::InProcessCommandBuffer::Service> service, | |
| 184 scoped_refptr<gl::GLSurface> surface, | |
| 185 bool is_offscreen, | |
| 186 gfx::AcceleratedWidget window, | |
| 187 GLInProcessContext* share_context, | |
| 188 const ::gpu::gles2::ContextCreationAttribHelper& attribs, | |
| 189 const SharedMemoryLimits& memory_limits, | |
| 190 GpuMemoryBufferManager* gpu_memory_buffer_manager, | |
| 191 ImageFactory* image_factory, | |
| 192 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | |
| 193 // If a surface is provided, we are running in a webview and should not have | |
| 194 // a task runner. We must have a task runner in all other cases. | |
| 195 DCHECK_EQ(!!surface, !task_runner); | |
| 196 | |
| 197 if (surface) { | |
| 198 DCHECK_EQ(surface->IsOffscreen(), is_offscreen); | |
| 199 DCHECK_EQ(gfx::kNullAcceleratedWidget, window); | |
| 200 } | |
| 201 | |
| 202 std::unique_ptr<GLInProcessContextImpl> context(new GLInProcessContextImpl); | |
| 203 if (!context->Initialize(surface, is_offscreen, share_context, window, | |
| 204 attribs, service, memory_limits, | |
| 205 gpu_memory_buffer_manager, image_factory, | |
| 206 std::move(task_runner))) | |
| 207 return NULL; | |
| 208 | |
| 209 return context.release(); | |
| 210 } | |
| 211 | |
| 212 } // namespace gpu | |
| OLD | NEW |