Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / gpu / command_buffer / tests / gl_manager.cc
blob3b9e1705d4ab49718f34dba5ba37f4d3550ce3bc
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"
7 #include <GLES2/gl2.h>
8 #include <GLES2/gl2ext.h>
9 #include <GLES2/gl2extchromium.h>
11 #include <vector>
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_cmd_helper.h"
17 #include "gpu/command_buffer/client/gles2_implementation.h"
18 #include "gpu/command_buffer/client/gles2_lib.h"
19 #include "gpu/command_buffer/client/transfer_buffer.h"
20 #include "gpu/command_buffer/common/constants.h"
21 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
22 #include "gpu/command_buffer/common/value_state.h"
23 #include "gpu/command_buffer/service/command_buffer_service.h"
24 #include "gpu/command_buffer/service/context_group.h"
25 #include "gpu/command_buffer/service/gl_context_virtual.h"
26 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
27 #include "gpu/command_buffer/service/gpu_scheduler.h"
28 #include "gpu/command_buffer/service/image_manager.h"
29 #include "gpu/command_buffer/service/mailbox_manager_impl.h"
30 #include "gpu/command_buffer/service/memory_tracking.h"
31 #include "gpu/command_buffer/service/valuebuffer_manager.h"
32 #include "testing/gtest/include/gtest/gtest.h"
33 #include "ui/gfx/gpu_memory_buffer.h"
34 #include "ui/gl/gl_context.h"
35 #include "ui/gl/gl_image_ref_counted_memory.h"
36 #include "ui/gl/gl_share_group.h"
37 #include "ui/gl/gl_surface.h"
39 namespace gpu {
40 namespace {
42 size_t NumberOfPlanesForGpuMemoryBufferFormat(gfx::BufferFormat format) {
43 switch (format) {
44 case gfx::BufferFormat::ATC:
45 case gfx::BufferFormat::ATCIA:
46 case gfx::BufferFormat::DXT1:
47 case gfx::BufferFormat::DXT5:
48 case gfx::BufferFormat::ETC1:
49 case gfx::BufferFormat::R_8:
50 case gfx::BufferFormat::RGBA_4444:
51 case gfx::BufferFormat::RGBA_8888:
52 case gfx::BufferFormat::RGBX_8888:
53 case gfx::BufferFormat::BGRA_8888:
54 return 1;
55 case gfx::BufferFormat::YUV_420:
56 return 3;
58 NOTREACHED();
59 return 0;
62 size_t SubsamplingFactor(gfx::BufferFormat format, int plane) {
63 switch (format) {
64 case gfx::BufferFormat::ATC:
65 case gfx::BufferFormat::ATCIA:
66 case gfx::BufferFormat::DXT1:
67 case gfx::BufferFormat::DXT5:
68 case gfx::BufferFormat::ETC1:
69 case gfx::BufferFormat::R_8:
70 case gfx::BufferFormat::RGBA_4444:
71 case gfx::BufferFormat::RGBA_8888:
72 case gfx::BufferFormat::RGBX_8888:
73 case gfx::BufferFormat::BGRA_8888:
74 return 1;
75 case gfx::BufferFormat::YUV_420: {
76 static size_t factor[] = {1, 2, 2};
77 DCHECK_LT(static_cast<size_t>(plane), arraysize(factor));
78 return factor[plane];
81 NOTREACHED();
82 return 0;
85 size_t StrideInBytes(size_t width, gfx::BufferFormat format, int plane) {
86 switch (format) {
87 case gfx::BufferFormat::ATCIA:
88 case gfx::BufferFormat::DXT5:
89 DCHECK_EQ(plane, 0);
90 return width;
91 case gfx::BufferFormat::ATC:
92 case gfx::BufferFormat::DXT1:
93 case gfx::BufferFormat::ETC1:
94 DCHECK_EQ(plane, 0);
95 DCHECK_EQ(width % 2, 0U);
96 return width / 2;
97 case gfx::BufferFormat::R_8:
98 return (width + 3) & ~0x3;
99 case gfx::BufferFormat::RGBA_4444:
100 DCHECK_EQ(plane, 0);
101 return width * 2;
102 case gfx::BufferFormat::RGBA_8888:
103 case gfx::BufferFormat::BGRA_8888:
104 DCHECK_EQ(plane, 0);
105 return width * 4;
106 case gfx::BufferFormat::RGBX_8888:
107 NOTREACHED();
108 return 0;
109 case gfx::BufferFormat::YUV_420:
110 return width / SubsamplingFactor(format, plane);
113 NOTREACHED();
114 return 0;
117 size_t BufferSizeInBytes(const gfx::Size& size, gfx::BufferFormat format) {
118 size_t size_in_bytes = 0;
119 size_t num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format);
120 for (size_t i = 0; i < num_planes; ++i) {
121 size_in_bytes += StrideInBytes(size.width(), format, i) *
122 (size.height() / SubsamplingFactor(format, i));
124 return size_in_bytes;
127 class GpuMemoryBufferImpl : public gfx::GpuMemoryBuffer {
128 public:
129 GpuMemoryBufferImpl(base::RefCountedBytes* bytes,
130 const gfx::Size& size,
131 gfx::BufferFormat format)
132 : bytes_(bytes), size_(size), format_(format), mapped_(false) {}
134 static GpuMemoryBufferImpl* FromClientBuffer(ClientBuffer buffer) {
135 return reinterpret_cast<GpuMemoryBufferImpl*>(buffer);
138 // Overridden from gfx::GpuMemoryBuffer:
139 bool Map(void** data) override {
140 size_t offset = 0;
141 size_t num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format_);
142 for (size_t i = 0; i < num_planes; ++i) {
143 data[i] = reinterpret_cast<uint8*>(&bytes_->data().front()) + offset;
144 offset += StrideInBytes(size_.width(), format_, i) *
145 (size_.height() / SubsamplingFactor(format_, i));
147 mapped_ = true;
148 return true;
150 void Unmap() override { mapped_ = false; }
151 bool IsMapped() const override { return mapped_; }
152 gfx::BufferFormat GetFormat() const override { return format_; }
153 void GetStride(int* stride) const override {
154 size_t num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format_);
155 for (size_t i = 0; i < num_planes; ++i)
156 stride[i] = StrideInBytes(size_.width(), format_, i);
158 gfx::GpuMemoryBufferId GetId() const override {
159 NOTREACHED();
160 return 0;
162 gfx::GpuMemoryBufferHandle GetHandle() const override {
163 NOTREACHED();
164 return gfx::GpuMemoryBufferHandle();
166 ClientBuffer AsClientBuffer() override {
167 return reinterpret_cast<ClientBuffer>(this);
170 base::RefCountedBytes* bytes() { return bytes_.get(); }
172 private:
173 scoped_refptr<base::RefCountedBytes> bytes_;
174 const gfx::Size size_;
175 gfx::BufferFormat format_;
176 bool mapped_;
179 } // namespace
181 int GLManager::use_count_;
182 scoped_refptr<gfx::GLShareGroup>* GLManager::base_share_group_;
183 scoped_refptr<gfx::GLSurface>* GLManager::base_surface_;
184 scoped_refptr<gfx::GLContext>* GLManager::base_context_;
186 GLManager::Options::Options()
187 : size(4, 4),
188 share_group_manager(NULL),
189 share_mailbox_manager(NULL),
190 virtual_manager(NULL),
191 bind_generates_resource(false),
192 lose_context_when_out_of_memory(false),
193 context_lost_allowed(false),
194 webgl_version(0) {
197 GLManager::GLManager() : context_lost_allowed_(false) {
198 SetupBaseContext();
201 GLManager::~GLManager() {
202 --use_count_;
203 if (!use_count_) {
204 if (base_share_group_) {
205 delete base_context_;
206 base_context_ = NULL;
208 if (base_surface_) {
209 delete base_surface_;
210 base_surface_ = NULL;
212 if (base_context_) {
213 delete base_context_;
214 base_context_ = NULL;
219 // static
220 scoped_ptr<gfx::GpuMemoryBuffer> GLManager::CreateGpuMemoryBuffer(
221 const gfx::Size& size,
222 gfx::BufferFormat format) {
223 std::vector<unsigned char> data(BufferSizeInBytes(size, format), 0);
224 scoped_refptr<base::RefCountedBytes> bytes(new base::RefCountedBytes(data));
225 return make_scoped_ptr<gfx::GpuMemoryBuffer>(
226 new GpuMemoryBufferImpl(bytes.get(), size, format));
229 void GLManager::Initialize(const GLManager::Options& options) {
230 InitializeWithCommandLine(options, nullptr);
232 void GLManager::InitializeWithCommandLine(const GLManager::Options& options,
233 base::CommandLine* command_line) {
234 const int32 kCommandBufferSize = 1024 * 1024;
235 const size_t kStartTransferBufferSize = 4 * 1024 * 1024;
236 const size_t kMinTransferBufferSize = 1 * 256 * 1024;
237 const size_t kMaxTransferBufferSize = 16 * 1024 * 1024;
239 context_lost_allowed_ = options.context_lost_allowed;
241 gles2::MailboxManager* mailbox_manager = NULL;
242 if (options.share_mailbox_manager) {
243 mailbox_manager = options.share_mailbox_manager->mailbox_manager();
244 } else if (options.share_group_manager) {
245 mailbox_manager = options.share_group_manager->mailbox_manager();
248 gfx::GLShareGroup* share_group = NULL;
249 if (options.share_group_manager) {
250 share_group = options.share_group_manager->share_group();
251 } else if (options.share_mailbox_manager) {
252 share_group = options.share_mailbox_manager->share_group();
255 gles2::ContextGroup* context_group = NULL;
256 gles2::ShareGroup* client_share_group = NULL;
257 if (options.share_group_manager) {
258 context_group = options.share_group_manager->decoder_->GetContextGroup();
259 client_share_group =
260 options.share_group_manager->gles2_implementation()->share_group();
263 gfx::GLContext* real_gl_context = NULL;
264 if (options.virtual_manager) {
265 real_gl_context = options.virtual_manager->context();
268 mailbox_manager_ =
269 mailbox_manager ? mailbox_manager : new gles2::MailboxManagerImpl;
270 share_group_ =
271 share_group ? share_group : new gfx::GLShareGroup;
273 gfx::GpuPreference gpu_preference(gfx::PreferDiscreteGpu);
274 std::vector<int32> attribs;
275 gles2::ContextCreationAttribHelper attrib_helper;
276 attrib_helper.red_size = 8;
277 attrib_helper.green_size = 8;
278 attrib_helper.blue_size = 8;
279 attrib_helper.alpha_size = 8;
280 attrib_helper.depth_size = 16;
281 attrib_helper.stencil_size = 8;
282 attrib_helper.webgl_version = options.webgl_version;
283 attrib_helper.Serialize(&attribs);
285 DCHECK(!command_line || !context_group);
286 if (!context_group) {
287 scoped_refptr<gles2::FeatureInfo> feature_info;
288 if (command_line)
289 feature_info = new gles2::FeatureInfo(*command_line);
290 context_group =
291 new gles2::ContextGroup(mailbox_manager_.get(),
292 NULL,
293 new gpu::gles2::ShaderTranslatorCache,
294 feature_info,
295 NULL,
296 NULL,
297 options.bind_generates_resource);
300 decoder_.reset(::gpu::gles2::GLES2Decoder::Create(context_group));
302 command_buffer_.reset(new CommandBufferService(
303 decoder_->GetContextGroup()->transfer_buffer_manager()));
304 ASSERT_TRUE(command_buffer_->Initialize())
305 << "could not create command buffer service";
307 gpu_scheduler_.reset(new GpuScheduler(command_buffer_.get(),
308 decoder_.get(),
309 decoder_.get()));
311 decoder_->set_engine(gpu_scheduler_.get());
313 surface_ = gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size());
314 ASSERT_TRUE(surface_.get() != NULL) << "could not create offscreen surface";
316 if (base_context_) {
317 context_ = scoped_refptr<gfx::GLContext>(new gpu::GLContextVirtual(
318 share_group_.get(), base_context_->get(), decoder_->AsWeakPtr()));
319 ASSERT_TRUE(context_->Initialize(
320 surface_.get(), gfx::PreferIntegratedGpu));
321 } else {
322 if (real_gl_context) {
323 context_ = scoped_refptr<gfx::GLContext>(new gpu::GLContextVirtual(
324 share_group_.get(), real_gl_context, decoder_->AsWeakPtr()));
325 ASSERT_TRUE(context_->Initialize(
326 surface_.get(), gfx::PreferIntegratedGpu));
327 } else {
328 context_ = gfx::GLContext::CreateGLContext(share_group_.get(),
329 surface_.get(),
330 gpu_preference);
333 ASSERT_TRUE(context_.get() != NULL) << "could not create GL context";
335 ASSERT_TRUE(context_->MakeCurrent(surface_.get()));
337 ASSERT_TRUE(decoder_->Initialize(
338 surface_.get(),
339 context_.get(),
340 true,
341 options.size,
342 ::gpu::gles2::DisallowedFeatures(),
343 attribs)) << "could not initialize decoder";
345 command_buffer_->SetPutOffsetChangeCallback(
346 base::Bind(&GLManager::PumpCommands, base::Unretained(this)));
347 command_buffer_->SetGetBufferChangeCallback(
348 base::Bind(&GLManager::GetBufferChanged, base::Unretained(this)));
350 // Create the GLES2 helper, which writes the command buffer protocol.
351 gles2_helper_.reset(new gles2::GLES2CmdHelper(command_buffer_.get()));
352 ASSERT_TRUE(gles2_helper_->Initialize(kCommandBufferSize));
354 // Create a transfer buffer.
355 transfer_buffer_.reset(new TransferBuffer(gles2_helper_.get()));
357 // Create the object exposing the OpenGL API.
358 const bool support_client_side_arrays = true;
359 gles2_implementation_.reset(
360 new gles2::GLES2Implementation(gles2_helper_.get(),
361 client_share_group,
362 transfer_buffer_.get(),
363 options.bind_generates_resource,
364 options.lose_context_when_out_of_memory,
365 support_client_side_arrays,
366 this));
368 ASSERT_TRUE(gles2_implementation_->Initialize(
369 kStartTransferBufferSize,
370 kMinTransferBufferSize,
371 kMaxTransferBufferSize,
372 gpu::gles2::GLES2Implementation::kNoLimit))
373 << "Could not init GLES2Implementation";
375 MakeCurrent();
378 void GLManager::SetupBaseContext() {
379 if (use_count_) {
380 #if defined(OS_ANDROID)
381 base_share_group_ = new scoped_refptr<gfx::GLShareGroup>(
382 new gfx::GLShareGroup);
383 gfx::Size size(4, 4);
384 base_surface_ = new scoped_refptr<gfx::GLSurface>(
385 gfx::GLSurface::CreateOffscreenGLSurface(size));
386 gfx::GpuPreference gpu_preference(gfx::PreferDiscreteGpu);
387 base_context_ = new scoped_refptr<gfx::GLContext>(
388 gfx::GLContext::CreateGLContext(base_share_group_->get(),
389 base_surface_->get(),
390 gpu_preference));
391 #endif
393 ++use_count_;
396 void GLManager::MakeCurrent() {
397 ::gles2::SetGLContext(gles2_implementation_.get());
400 void GLManager::SetSurface(gfx::GLSurface* surface) {
401 decoder_->SetSurface(surface);
404 void GLManager::Destroy() {
405 if (gles2_implementation_.get()) {
406 MakeCurrent();
407 EXPECT_TRUE(glGetError() == GL_NONE);
408 gles2_implementation_->Flush();
409 gles2_implementation_.reset();
411 transfer_buffer_.reset();
412 gles2_helper_.reset();
413 command_buffer_.reset();
414 if (decoder_.get()) {
415 bool have_context = decoder_->GetGLContext()->MakeCurrent(surface_.get());
416 decoder_->Destroy(have_context);
417 decoder_.reset();
421 const gpu::gles2::FeatureInfo::Workarounds& GLManager::workarounds() const {
422 return decoder_->GetContextGroup()->feature_info()->workarounds();
425 void GLManager::PumpCommands() {
426 if (!decoder_->MakeCurrent()) {
427 command_buffer_->SetContextLostReason(decoder_->GetContextLostReason());
428 command_buffer_->SetParseError(::gpu::error::kLostContext);
429 return;
431 gpu_scheduler_->PutChanged();
432 ::gpu::CommandBuffer::State state = command_buffer_->GetLastState();
433 if (!context_lost_allowed_) {
434 ASSERT_EQ(::gpu::error::kNoError, state.error);
438 bool GLManager::GetBufferChanged(int32 transfer_buffer_id) {
439 return gpu_scheduler_->SetGetBuffer(transfer_buffer_id);
442 Capabilities GLManager::GetCapabilities() {
443 return decoder_->GetCapabilities();
446 int32 GLManager::CreateImage(ClientBuffer buffer,
447 size_t width,
448 size_t height,
449 unsigned internalformat) {
450 GpuMemoryBufferImpl* gpu_memory_buffer =
451 GpuMemoryBufferImpl::FromClientBuffer(buffer);
453 scoped_refptr<gfx::GLImageRefCountedMemory> image(
454 new gfx::GLImageRefCountedMemory(gfx::Size(width, height),
455 internalformat));
456 if (!image->Initialize(gpu_memory_buffer->bytes(),
457 gpu_memory_buffer->GetFormat())) {
458 return -1;
461 static int32 next_id = 1;
462 int32 new_id = next_id++;
464 gpu::gles2::ImageManager* image_manager = decoder_->GetImageManager();
465 DCHECK(image_manager);
466 image_manager->AddImage(image.get(), new_id);
467 return new_id;
470 int32 GLManager::CreateGpuMemoryBufferImage(size_t width,
471 size_t height,
472 unsigned internalformat,
473 unsigned usage) {
474 DCHECK_EQ(usage, static_cast<unsigned>(GL_MAP_CHROMIUM));
475 scoped_ptr<gfx::GpuMemoryBuffer> buffer = GLManager::CreateGpuMemoryBuffer(
476 gfx::Size(width, height), gfx::BufferFormat::RGBA_8888);
477 return CreateImage(buffer->AsClientBuffer(), width, height, internalformat);
480 void GLManager::DestroyImage(int32 id) {
481 gpu::gles2::ImageManager* image_manager = decoder_->GetImageManager();
482 DCHECK(image_manager);
483 image_manager->RemoveImage(id);
486 uint32 GLManager::InsertSyncPoint() {
487 NOTIMPLEMENTED();
488 return 0u;
491 uint32 GLManager::InsertFutureSyncPoint() {
492 NOTIMPLEMENTED();
493 return 0u;
496 void GLManager::RetireSyncPoint(uint32 sync_point) {
497 NOTIMPLEMENTED();
500 void GLManager::SignalSyncPoint(uint32 sync_point,
501 const base::Closure& callback) {
502 NOTIMPLEMENTED();
505 void GLManager::SignalQuery(uint32 query, const base::Closure& callback) {
506 NOTIMPLEMENTED();
509 void GLManager::SetSurfaceVisible(bool visible) {
510 NOTIMPLEMENTED();
513 uint32 GLManager::CreateStreamTexture(uint32 texture_id) {
514 NOTIMPLEMENTED();
515 return 0;
518 void GLManager::SetLock(base::Lock*) {
519 NOTIMPLEMENTED();
522 bool GLManager::IsGpuChannelLost() {
523 NOTIMPLEMENTED();
524 return false;
527 } // namespace gpu