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/tests/gl_manager.h"
9 #include "base/at_exit.h"
10 #include "base/bind.h"
11 #include "gpu/command_buffer/client/gles2_implementation.h"
12 #include "gpu/command_buffer/client/gles2_lib.h"
13 #include "gpu/command_buffer/client/transfer_buffer.h"
14 #include "gpu/command_buffer/common/constants.h"
15 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
16 #include "gpu/command_buffer/service/command_buffer_service.h"
17 #include "gpu/command_buffer/service/context_group.h"
18 #include "gpu/command_buffer/service/gl_context_virtual.h"
19 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
20 #include "gpu/command_buffer/service/gpu_control_service.h"
21 #include "gpu/command_buffer/service/gpu_scheduler.h"
22 #include "gpu/command_buffer/service/image_manager.h"
23 #include "gpu/command_buffer/service/mailbox_manager.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "ui/gl/gl_context.h"
26 #include "ui/gl/gl_share_group.h"
27 #include "ui/gl/gl_surface.h"
31 int GLManager::use_count_;
32 scoped_refptr<gfx::GLShareGroup>* GLManager::base_share_group_;
33 scoped_refptr<gfx::GLSurface>* GLManager::base_surface_;
34 scoped_refptr<gfx::GLContext>* GLManager::base_context_;
36 GLManager::Options::Options()
38 share_group_manager(NULL),
39 share_mailbox_manager(NULL),
40 virtual_manager(NULL),
41 bind_generates_resource(false),
42 context_lost_allowed(false),
46 GLManager::GLManager()
47 : context_lost_allowed_(false) {
51 GLManager::~GLManager() {
54 if (base_share_group_) {
69 void GLManager::Initialize(const GLManager::Options& options) {
70 const int32 kCommandBufferSize = 1024 * 1024;
71 const size_t kStartTransferBufferSize = 4 * 1024 * 1024;
72 const size_t kMinTransferBufferSize = 1 * 256 * 1024;
73 const size_t kMaxTransferBufferSize = 16 * 1024 * 1024;
75 context_lost_allowed_ = options.context_lost_allowed;
77 gles2::MailboxManager* mailbox_manager = NULL;
78 if (options.share_mailbox_manager) {
79 mailbox_manager = options.share_mailbox_manager->mailbox_manager();
80 } else if (options.share_group_manager) {
81 mailbox_manager = options.share_group_manager->mailbox_manager();
84 gfx::GLShareGroup* share_group = NULL;
85 if (options.share_group_manager) {
86 share_group = options.share_group_manager->share_group();
87 } else if (options.share_mailbox_manager) {
88 share_group = options.share_mailbox_manager->share_group();
91 gles2::ContextGroup* context_group = NULL;
92 gles2::ShareGroup* client_share_group = NULL;
93 if (options.share_group_manager) {
94 context_group = options.share_group_manager->decoder_->GetContextGroup();
96 options.share_group_manager->gles2_implementation()->share_group();
99 gfx::GLContext* real_gl_context = NULL;
100 if (options.virtual_manager) {
101 real_gl_context = options.virtual_manager->context();
105 mailbox_manager ? mailbox_manager : new gles2::MailboxManager;
107 share_group ? share_group : new gfx::GLShareGroup;
109 gfx::GpuPreference gpu_preference(gfx::PreferDiscreteGpu);
110 std::vector<int32> attribs;
111 gles2::ContextCreationAttribHelper attrib_helper;
112 attrib_helper.red_size_ = 8;
113 attrib_helper.green_size_ = 8;
114 attrib_helper.blue_size_ = 8;
115 attrib_helper.alpha_size_ = 8;
116 attrib_helper.depth_size_ = 16;
117 attrib_helper.Serialize(&attribs);
119 if (!context_group) {
120 context_group = new gles2::ContextGroup(mailbox_manager_.get(),
121 options.image_manager,
124 options.bind_generates_resource);
127 decoder_.reset(::gpu::gles2::GLES2Decoder::Create(context_group));
129 command_buffer_.reset(new CommandBufferService(
130 decoder_->GetContextGroup()->transfer_buffer_manager()));
131 ASSERT_TRUE(command_buffer_->Initialize())
132 << "could not create command buffer service";
134 gpu_scheduler_.reset(new GpuScheduler(command_buffer_.get(),
138 decoder_->set_engine(gpu_scheduler_.get());
140 surface_ = gfx::GLSurface::CreateOffscreenGLSurface(options.size);
141 ASSERT_TRUE(surface_.get() != NULL) << "could not create offscreen surface";
143 if (real_gl_context) {
144 context_ = scoped_refptr<gfx::GLContext>(new gpu::GLContextVirtual(
145 share_group_.get(), real_gl_context, decoder_->AsWeakPtr()));
146 ASSERT_TRUE(context_->Initialize(
147 surface_.get(), gfx::PreferIntegratedGpu));
150 context_ = scoped_refptr<gfx::GLContext>(new gpu::GLContextVirtual(
151 share_group_.get(), base_context_->get(), decoder_->AsWeakPtr()));
152 ASSERT_TRUE(context_->Initialize(
153 surface_.get(), gfx::PreferIntegratedGpu));
155 context_ = gfx::GLContext::CreateGLContext(share_group_.get(),
160 ASSERT_TRUE(context_.get() != NULL) << "could not create GL context";
162 ASSERT_TRUE(context_->MakeCurrent(surface_.get()));
164 ASSERT_TRUE(decoder_->Initialize(
169 ::gpu::gles2::DisallowedFeatures(),
170 attribs)) << "could not initialize decoder";
173 new GpuControlService(decoder_->GetContextGroup()->image_manager(),
174 options.gpu_memory_buffer_factory,
175 decoder_->GetContextGroup()->mailbox_manager(),
176 decoder_->GetQueryManager()));
178 command_buffer_->SetPutOffsetChangeCallback(
179 base::Bind(&GLManager::PumpCommands, base::Unretained(this)));
180 command_buffer_->SetGetBufferChangeCallback(
181 base::Bind(&GLManager::GetBufferChanged, base::Unretained(this)));
183 // Create the GLES2 helper, which writes the command buffer protocol.
184 gles2_helper_.reset(new gles2::GLES2CmdHelper(command_buffer_.get()));
185 ASSERT_TRUE(gles2_helper_->Initialize(kCommandBufferSize));
187 // Create a transfer buffer.
188 transfer_buffer_.reset(new TransferBuffer(gles2_helper_.get()));
190 // Create the object exposing the OpenGL API.
191 gles2_implementation_.reset(new gles2::GLES2Implementation(
194 transfer_buffer_.get(),
195 options.bind_generates_resource,
196 gpu_control_.get()));
198 ASSERT_TRUE(gles2_implementation_->Initialize(
199 kStartTransferBufferSize,
200 kMinTransferBufferSize,
201 kMaxTransferBufferSize,
202 gpu::gles2::GLES2Implementation::kNoLimit))
203 << "Could not init GLES2Implementation";
208 void GLManager::SetupBaseContext() {
210 #if defined(OS_ANDROID)
211 base_share_group_ = new scoped_refptr<gfx::GLShareGroup>(
212 new gfx::GLShareGroup);
213 gfx::Size size(4, 4);
214 base_surface_ = new scoped_refptr<gfx::GLSurface>(
215 gfx::GLSurface::CreateOffscreenGLSurface(size));
216 gfx::GpuPreference gpu_preference(gfx::PreferDiscreteGpu);
217 base_context_ = new scoped_refptr<gfx::GLContext>(
218 gfx::GLContext::CreateGLContext(base_share_group_->get(),
219 base_surface_->get(),
226 void GLManager::MakeCurrent() {
227 ::gles2::SetGLContext(gles2_implementation_.get());
230 void GLManager::SetSurface(gfx::GLSurface* surface) {
231 decoder_->SetSurface(surface);
234 void GLManager::Destroy() {
235 if (gles2_implementation_.get()) {
237 EXPECT_TRUE(glGetError() == GL_NONE);
238 gles2_implementation_->Flush();
239 gles2_implementation_.reset();
241 transfer_buffer_.reset();
242 gles2_helper_.reset();
243 command_buffer_.reset();
244 if (decoder_.get()) {
245 decoder_->MakeCurrent();
246 decoder_->Destroy(true);
251 const gpu::gles2::FeatureInfo::Workarounds& GLManager::workarounds() const {
252 return decoder_->GetContextGroup()->feature_info()->workarounds();
255 void GLManager::PumpCommands() {
256 decoder_->MakeCurrent();
257 gpu_scheduler_->PutChanged();
258 ::gpu::CommandBuffer::State state = command_buffer_->GetState();
259 if (!context_lost_allowed_) {
260 ASSERT_EQ(::gpu::error::kNoError, state.error);
264 bool GLManager::GetBufferChanged(int32 transfer_buffer_id) {
265 return gpu_scheduler_->SetGetBuffer(transfer_buffer_id);