1 // Copyright 2013 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 "mojo/services/gles2/command_buffer_impl.h"
8 #include "base/memory/shared_memory.h"
10 #include "gpu/command_buffer/common/constants.h"
11 #include "gpu/command_buffer/service/command_buffer_service.h"
12 #include "gpu/command_buffer/service/context_group.h"
13 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
14 #include "gpu/command_buffer/service/gpu_control_service.h"
15 #include "gpu/command_buffer/service/gpu_scheduler.h"
16 #include "gpu/command_buffer/service/image_manager.h"
17 #include "gpu/command_buffer/service/mailbox_manager.h"
18 #include "gpu/command_buffer/service/memory_tracking.h"
19 #include "mojo/services/gles2/command_buffer_type_conversions.h"
20 #include "mojo/services/gles2/mojo_buffer_backing.h"
21 #include "ui/gl/gl_context.h"
22 #include "ui/gl/gl_surface.h"
29 class MemoryTrackerStub
: public gpu::gles2::MemoryTracker
{
31 MemoryTrackerStub() {}
33 virtual void TrackMemoryAllocatedChange(size_t old_size
,
35 gpu::gles2::MemoryTracker::Pool pool
)
38 virtual bool EnsureGPUMemoryAvailable(size_t size_needed
) OVERRIDE
{
43 virtual ~MemoryTrackerStub() {}
45 DISALLOW_COPY_AND_ASSIGN(MemoryTrackerStub
);
48 } // anonymous namespace
50 CommandBufferImpl::CommandBufferImpl(gfx::AcceleratedWidget widget
,
51 const gfx::Size
& size
)
52 : widget_(widget
), size_(size
) {}
54 CommandBufferImpl::~CommandBufferImpl() {
55 client()->DidDestroy();
57 bool have_context
= decoder_
->MakeCurrent();
58 decoder_
->Destroy(have_context
);
62 void CommandBufferImpl::OnConnectionError() {
63 // TODO(darin): How should we handle this error?
66 void CommandBufferImpl::Initialize(
67 CommandBufferSyncClientPtr sync_client
,
68 mojo::ScopedSharedBufferHandle shared_state
) {
69 sync_client_
= sync_client
.Pass();
70 sync_client_
->DidInitialize(DoInitialize(shared_state
.Pass()));
73 bool CommandBufferImpl::DoInitialize(
74 mojo::ScopedSharedBufferHandle shared_state
) {
75 // TODO(piman): offscreen surface.
76 scoped_refptr
<gfx::GLSurface
> surface
=
77 gfx::GLSurface::CreateViewGLSurface(widget_
);
81 // TODO(piman): context sharing, virtual contexts, gpu preference.
82 scoped_refptr
<gfx::GLContext
> context
= gfx::GLContext::CreateGLContext(
83 NULL
, surface
.get(), gfx::PreferIntegratedGpu
);
87 if (!context
->MakeCurrent(surface
.get()))
90 // TODO(piman): ShaderTranslatorCache is currently per-ContextGroup but
91 // only needs to be per-thread.
92 scoped_refptr
<gpu::gles2::ContextGroup
> context_group
=
93 new gpu::gles2::ContextGroup(NULL
,
95 new MemoryTrackerStub
,
96 new gpu::gles2::ShaderTranslatorCache
,
100 command_buffer_
.reset(
101 new gpu::CommandBufferService(context_group
->transfer_buffer_manager()));
102 bool result
= command_buffer_
->Initialize();
105 decoder_
.reset(::gpu::gles2::GLES2Decoder::Create(context_group
.get()));
106 scheduler_
.reset(new gpu::GpuScheduler(
107 command_buffer_
.get(), decoder_
.get(), decoder_
.get()));
108 decoder_
->set_engine(scheduler_
.get());
110 gpu::gles2::DisallowedFeatures disallowed_features
;
112 // TODO(piman): attributes.
113 std::vector
<int32
> attrib_vector
;
114 if (!decoder_
->Initialize(surface
,
116 false /* offscreen */,
123 new gpu::GpuControlService(context_group
->image_manager(), NULL
));
125 command_buffer_
->SetPutOffsetChangeCallback(base::Bind(
126 &gpu::GpuScheduler::PutChanged
, base::Unretained(scheduler_
.get())));
127 command_buffer_
->SetGetBufferChangeCallback(base::Bind(
128 &gpu::GpuScheduler::SetGetBuffer
, base::Unretained(scheduler_
.get())));
129 command_buffer_
->SetParseErrorCallback(
130 base::Bind(&CommandBufferImpl::OnParseError
, base::Unretained(this)));
132 // TODO(piman): other callbacks
134 const size_t kSize
= sizeof(gpu::CommandBufferSharedState
);
135 scoped_ptr
<gpu::BufferBacking
> backing(
136 gles2::MojoBufferBacking::Create(shared_state
.Pass(), kSize
));
140 command_buffer_
->SetSharedStateBuffer(backing
.Pass());
144 void CommandBufferImpl::SetGetBuffer(int32_t buffer
) {
145 command_buffer_
->SetGetBuffer(buffer
);
148 void CommandBufferImpl::Flush(int32_t put_offset
) {
149 command_buffer_
->Flush(put_offset
);
152 void CommandBufferImpl::MakeProgress(int32_t last_get_offset
) {
153 // TODO(piman): handle out-of-order.
154 sync_client_
->DidMakeProgress(
155 CommandBufferState::From(command_buffer_
->GetLastState()));
158 void CommandBufferImpl::RegisterTransferBuffer(
160 mojo::ScopedSharedBufferHandle transfer_buffer
,
162 // Take ownership of the memory and map it into this process.
163 // This validates the size.
164 scoped_ptr
<gpu::BufferBacking
> backing(
165 gles2::MojoBufferBacking::Create(transfer_buffer
.Pass(), size
));
166 if (!backing
.get()) {
167 DVLOG(0) << "Failed to map shared memory.";
170 command_buffer_
->RegisterTransferBuffer(id
, backing
.Pass());
173 void CommandBufferImpl::DestroyTransferBuffer(int32_t id
) {
174 command_buffer_
->DestroyTransferBuffer(id
);
177 void CommandBufferImpl::Echo(const Callback
<void()>& callback
) {
181 void CommandBufferImpl::RequestAnimationFrames() {
182 timer_
.Start(FROM_HERE
,
183 base::TimeDelta::FromMilliseconds(16),
185 &CommandBufferImpl::DrawAnimationFrame
);
188 void CommandBufferImpl::CancelAnimationFrames() { timer_
.Stop(); }
190 void CommandBufferImpl::OnParseError() {
191 gpu::CommandBuffer::State state
= command_buffer_
->GetLastState();
192 client()->LostContext(state
.context_lost_reason
);
195 void CommandBufferImpl::DrawAnimationFrame() { client()->DrawAnimationFrame(); }
197 } // namespace services