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"
8 #include <GLES2/gl2ext.h>
9 #include <GLES2/gl2extchromium.h>
13 #include "base/at_exit.h"
14 #include "base/bind.h"
15 #include "base/memory/ref_counted_memory.h"
16 #include "gpu/command_buffer/client/gles2_implementation.h"
17 #include "gpu/command_buffer/client/gles2_lib.h"
18 #include "gpu/command_buffer/client/transfer_buffer.h"
19 #include "gpu/command_buffer/common/constants.h"
20 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
21 #include "gpu/command_buffer/common/value_state.h"
22 #include "gpu/command_buffer/service/command_buffer_service.h"
23 #include "gpu/command_buffer/service/context_group.h"
24 #include "gpu/command_buffer/service/gl_context_virtual.h"
25 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
26 #include "gpu/command_buffer/service/gpu_scheduler.h"
27 #include "gpu/command_buffer/service/image_manager.h"
28 #include "gpu/command_buffer/service/mailbox_manager_impl.h"
29 #include "gpu/command_buffer/service/memory_tracking.h"
30 #include "gpu/command_buffer/service/valuebuffer_manager.h"
31 #include "testing/gtest/include/gtest/gtest.h"
32 #include "ui/gfx/gpu_memory_buffer.h"
33 #include "ui/gl/gl_context.h"
34 #include "ui/gl/gl_image_ref_counted_memory.h"
35 #include "ui/gl/gl_share_group.h"
36 #include "ui/gl/gl_surface.h"
41 size_t StrideInBytes(size_t width
, gfx::GpuMemoryBuffer::Format format
) {
43 case gfx::GpuMemoryBuffer::ATCIA
:
44 case gfx::GpuMemoryBuffer::DXT5
:
46 case gfx::GpuMemoryBuffer::ATC
:
47 case gfx::GpuMemoryBuffer::DXT1
:
48 case gfx::GpuMemoryBuffer::ETC1
:
49 DCHECK_EQ(width
% 2, 0U);
51 case gfx::GpuMemoryBuffer::RGBA_8888
:
52 case gfx::GpuMemoryBuffer::BGRA_8888
:
54 case gfx::GpuMemoryBuffer::RGBX_8888
:
63 class GpuMemoryBufferImpl
: public gfx::GpuMemoryBuffer
{
65 GpuMemoryBufferImpl(base::RefCountedBytes
* bytes
,
66 const gfx::Size
& size
,
67 gfx::GpuMemoryBuffer::Format format
)
68 : bytes_(bytes
), size_(size
), format_(format
), mapped_(false) {}
70 static GpuMemoryBufferImpl
* FromClientBuffer(ClientBuffer buffer
) {
71 return reinterpret_cast<GpuMemoryBufferImpl
*>(buffer
);
74 // Overridden from gfx::GpuMemoryBuffer:
75 bool Map(void** data
) override
{
77 *data
= &bytes_
->data().front();
80 void Unmap() override
{ mapped_
= false; }
81 bool IsMapped() const override
{ return mapped_
; }
82 Format
GetFormat() const override
{ return format_
; }
83 void GetStride(uint32
* stride
) const override
{
84 *stride
= StrideInBytes(size_
.width(), format_
);
86 gfx::GpuMemoryBufferHandle
GetHandle() const override
{
88 return gfx::GpuMemoryBufferHandle();
90 ClientBuffer
AsClientBuffer() override
{
91 return reinterpret_cast<ClientBuffer
>(this);
94 base::RefCountedBytes
* bytes() { return bytes_
.get(); }
97 scoped_refptr
<base::RefCountedBytes
> bytes_
;
98 const gfx::Size size_
;
99 gfx::GpuMemoryBuffer::Format format_
;
105 int GLManager::use_count_
;
106 scoped_refptr
<gfx::GLShareGroup
>* GLManager::base_share_group_
;
107 scoped_refptr
<gfx::GLSurface
>* GLManager::base_surface_
;
108 scoped_refptr
<gfx::GLContext
>* GLManager::base_context_
;
110 GLManager::Options::Options()
112 share_group_manager(NULL
),
113 share_mailbox_manager(NULL
),
114 virtual_manager(NULL
),
115 bind_generates_resource(false),
116 lose_context_when_out_of_memory(false),
117 context_lost_allowed(false) {
120 GLManager::GLManager() : context_lost_allowed_(false) {
124 GLManager::~GLManager() {
127 if (base_share_group_
) {
128 delete base_context_
;
129 base_context_
= NULL
;
132 delete base_surface_
;
133 base_surface_
= NULL
;
136 delete base_context_
;
137 base_context_
= NULL
;
143 scoped_ptr
<gfx::GpuMemoryBuffer
> GLManager::CreateGpuMemoryBuffer(
144 const gfx::Size
& size
,
145 gfx::GpuMemoryBuffer::Format format
) {
146 std::vector
<unsigned char> data(
147 StrideInBytes(size
.width(), format
) * size
.height(), 0);
148 scoped_refptr
<base::RefCountedBytes
> bytes(new base::RefCountedBytes(data
));
149 return make_scoped_ptr
<gfx::GpuMemoryBuffer
>(
150 new GpuMemoryBufferImpl(bytes
.get(), size
, format
));
153 void GLManager::Initialize(const GLManager::Options
& options
) {
154 InitializeWithCommandLine(options
, nullptr);
156 void GLManager::InitializeWithCommandLine(const GLManager::Options
& options
,
157 base::CommandLine
* command_line
) {
158 const int32 kCommandBufferSize
= 1024 * 1024;
159 const size_t kStartTransferBufferSize
= 4 * 1024 * 1024;
160 const size_t kMinTransferBufferSize
= 1 * 256 * 1024;
161 const size_t kMaxTransferBufferSize
= 16 * 1024 * 1024;
163 context_lost_allowed_
= options
.context_lost_allowed
;
165 gles2::MailboxManager
* mailbox_manager
= NULL
;
166 if (options
.share_mailbox_manager
) {
167 mailbox_manager
= options
.share_mailbox_manager
->mailbox_manager();
168 } else if (options
.share_group_manager
) {
169 mailbox_manager
= options
.share_group_manager
->mailbox_manager();
172 gfx::GLShareGroup
* share_group
= NULL
;
173 if (options
.share_group_manager
) {
174 share_group
= options
.share_group_manager
->share_group();
175 } else if (options
.share_mailbox_manager
) {
176 share_group
= options
.share_mailbox_manager
->share_group();
179 gles2::ContextGroup
* context_group
= NULL
;
180 gles2::ShareGroup
* client_share_group
= NULL
;
181 if (options
.share_group_manager
) {
182 context_group
= options
.share_group_manager
->decoder_
->GetContextGroup();
184 options
.share_group_manager
->gles2_implementation()->share_group();
187 gfx::GLContext
* real_gl_context
= NULL
;
188 if (options
.virtual_manager
) {
189 real_gl_context
= options
.virtual_manager
->context();
193 mailbox_manager
? mailbox_manager
: new gles2::MailboxManagerImpl
;
195 share_group
? share_group
: new gfx::GLShareGroup
;
197 gfx::GpuPreference
gpu_preference(gfx::PreferDiscreteGpu
);
198 std::vector
<int32
> attribs
;
199 gles2::ContextCreationAttribHelper attrib_helper
;
200 attrib_helper
.red_size
= 8;
201 attrib_helper
.green_size
= 8;
202 attrib_helper
.blue_size
= 8;
203 attrib_helper
.alpha_size
= 8;
204 attrib_helper
.depth_size
= 16;
205 attrib_helper
.stencil_size
= 8;
206 attrib_helper
.Serialize(&attribs
);
208 DCHECK(!command_line
|| !context_group
);
209 if (!context_group
) {
210 scoped_refptr
<gles2::FeatureInfo
> feature_info
;
212 feature_info
= new gles2::FeatureInfo(*command_line
);
214 new gles2::ContextGroup(mailbox_manager_
.get(),
216 new gpu::gles2::ShaderTranslatorCache
,
220 options
.bind_generates_resource
);
223 decoder_
.reset(::gpu::gles2::GLES2Decoder::Create(context_group
));
225 command_buffer_
.reset(new CommandBufferService(
226 decoder_
->GetContextGroup()->transfer_buffer_manager()));
227 ASSERT_TRUE(command_buffer_
->Initialize())
228 << "could not create command buffer service";
230 gpu_scheduler_
.reset(new GpuScheduler(command_buffer_
.get(),
234 decoder_
->set_engine(gpu_scheduler_
.get());
236 surface_
= gfx::GLSurface::CreateOffscreenGLSurface(options
.size
);
237 ASSERT_TRUE(surface_
.get() != NULL
) << "could not create offscreen surface";
240 context_
= scoped_refptr
<gfx::GLContext
>(new gpu::GLContextVirtual(
241 share_group_
.get(), base_context_
->get(), decoder_
->AsWeakPtr()));
242 ASSERT_TRUE(context_
->Initialize(
243 surface_
.get(), gfx::PreferIntegratedGpu
));
245 if (real_gl_context
) {
246 context_
= scoped_refptr
<gfx::GLContext
>(new gpu::GLContextVirtual(
247 share_group_
.get(), real_gl_context
, decoder_
->AsWeakPtr()));
248 ASSERT_TRUE(context_
->Initialize(
249 surface_
.get(), gfx::PreferIntegratedGpu
));
251 context_
= gfx::GLContext::CreateGLContext(share_group_
.get(),
256 ASSERT_TRUE(context_
.get() != NULL
) << "could not create GL context";
258 ASSERT_TRUE(context_
->MakeCurrent(surface_
.get()));
260 ASSERT_TRUE(decoder_
->Initialize(
265 ::gpu::gles2::DisallowedFeatures(),
266 attribs
)) << "could not initialize decoder";
268 command_buffer_
->SetPutOffsetChangeCallback(
269 base::Bind(&GLManager::PumpCommands
, base::Unretained(this)));
270 command_buffer_
->SetGetBufferChangeCallback(
271 base::Bind(&GLManager::GetBufferChanged
, base::Unretained(this)));
273 // Create the GLES2 helper, which writes the command buffer protocol.
274 gles2_helper_
.reset(new gles2::GLES2CmdHelper(command_buffer_
.get()));
275 ASSERT_TRUE(gles2_helper_
->Initialize(kCommandBufferSize
));
277 // Create a transfer buffer.
278 transfer_buffer_
.reset(new TransferBuffer(gles2_helper_
.get()));
280 // Create the object exposing the OpenGL API.
281 const bool support_client_side_arrays
= true;
282 gles2_implementation_
.reset(
283 new gles2::GLES2Implementation(gles2_helper_
.get(),
285 transfer_buffer_
.get(),
286 options
.bind_generates_resource
,
287 options
.lose_context_when_out_of_memory
,
288 support_client_side_arrays
,
291 ASSERT_TRUE(gles2_implementation_
->Initialize(
292 kStartTransferBufferSize
,
293 kMinTransferBufferSize
,
294 kMaxTransferBufferSize
,
295 gpu::gles2::GLES2Implementation::kNoLimit
))
296 << "Could not init GLES2Implementation";
301 void GLManager::SetupBaseContext() {
303 #if defined(OS_ANDROID)
304 base_share_group_
= new scoped_refptr
<gfx::GLShareGroup
>(
305 new gfx::GLShareGroup
);
306 gfx::Size
size(4, 4);
307 base_surface_
= new scoped_refptr
<gfx::GLSurface
>(
308 gfx::GLSurface::CreateOffscreenGLSurface(size
));
309 gfx::GpuPreference
gpu_preference(gfx::PreferDiscreteGpu
);
310 base_context_
= new scoped_refptr
<gfx::GLContext
>(
311 gfx::GLContext::CreateGLContext(base_share_group_
->get(),
312 base_surface_
->get(),
319 void GLManager::MakeCurrent() {
320 ::gles2::SetGLContext(gles2_implementation_
.get());
323 void GLManager::SetSurface(gfx::GLSurface
* surface
) {
324 decoder_
->SetSurface(surface
);
327 void GLManager::Destroy() {
328 if (gles2_implementation_
.get()) {
330 EXPECT_TRUE(glGetError() == GL_NONE
);
331 gles2_implementation_
->Flush();
332 gles2_implementation_
.reset();
334 transfer_buffer_
.reset();
335 gles2_helper_
.reset();
336 command_buffer_
.reset();
337 if (decoder_
.get()) {
338 decoder_
->MakeCurrent();
339 decoder_
->Destroy(true);
344 const gpu::gles2::FeatureInfo::Workarounds
& GLManager::workarounds() const {
345 return decoder_
->GetContextGroup()->feature_info()->workarounds();
348 void GLManager::PumpCommands() {
349 if (!decoder_
->MakeCurrent()) {
350 command_buffer_
->SetContextLostReason(decoder_
->GetContextLostReason());
351 command_buffer_
->SetParseError(::gpu::error::kLostContext
);
354 gpu_scheduler_
->PutChanged();
355 ::gpu::CommandBuffer::State state
= command_buffer_
->GetLastState();
356 if (!context_lost_allowed_
) {
357 ASSERT_EQ(::gpu::error::kNoError
, state
.error
);
361 bool GLManager::GetBufferChanged(int32 transfer_buffer_id
) {
362 return gpu_scheduler_
->SetGetBuffer(transfer_buffer_id
);
365 Capabilities
GLManager::GetCapabilities() {
366 return decoder_
->GetCapabilities();
369 int32
GLManager::CreateImage(ClientBuffer buffer
,
372 unsigned internalformat
) {
373 GpuMemoryBufferImpl
* gpu_memory_buffer
=
374 GpuMemoryBufferImpl::FromClientBuffer(buffer
);
376 scoped_refptr
<gfx::GLImageRefCountedMemory
> image(
377 new gfx::GLImageRefCountedMemory(gfx::Size(width
, height
),
379 if (!image
->Initialize(gpu_memory_buffer
->bytes(),
380 gpu_memory_buffer
->GetFormat())) {
384 static int32 next_id
= 1;
385 int32 new_id
= next_id
++;
387 gpu::gles2::ImageManager
* image_manager
= decoder_
->GetImageManager();
388 DCHECK(image_manager
);
389 image_manager
->AddImage(image
.get(), new_id
);
393 int32
GLManager::CreateGpuMemoryBufferImage(size_t width
,
395 unsigned internalformat
,
397 DCHECK_EQ(usage
, static_cast<unsigned>(GL_MAP_CHROMIUM
));
398 scoped_ptr
<gfx::GpuMemoryBuffer
> buffer
= GLManager::CreateGpuMemoryBuffer(
399 gfx::Size(width
, height
), gfx::GpuMemoryBuffer::RGBA_8888
);
400 return CreateImage(buffer
->AsClientBuffer(), width
, height
, internalformat
);
403 void GLManager::DestroyImage(int32 id
) {
404 gpu::gles2::ImageManager
* image_manager
= decoder_
->GetImageManager();
405 DCHECK(image_manager
);
406 image_manager
->RemoveImage(id
);
409 uint32
GLManager::InsertSyncPoint() {
414 uint32
GLManager::InsertFutureSyncPoint() {
419 void GLManager::RetireSyncPoint(uint32 sync_point
) {
423 void GLManager::SignalSyncPoint(uint32 sync_point
,
424 const base::Closure
& callback
) {
428 void GLManager::SignalQuery(uint32 query
, const base::Closure
& callback
) {
432 void GLManager::SetSurfaceVisible(bool visible
) {
436 uint32
GLManager::CreateStreamTexture(uint32 texture_id
) {
441 void GLManager::SetLock(base::Lock
*) {