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_scheduler.h"
15 #include "gpu/command_buffer/service/image_manager.h"
16 #include "gpu/command_buffer/service/mailbox_manager.h"
17 #include "gpu/command_buffer/service/memory_tracking.h"
18 #include "mojo/services/gles2/command_buffer_type_conversions.h"
19 #include "mojo/services/gles2/mojo_buffer_backing.h"
20 #include "ui/gl/gl_context.h"
21 #include "ui/gl/gl_surface.h"
27 class MemoryTrackerStub
: public gpu::gles2::MemoryTracker
{
29 MemoryTrackerStub() {}
31 virtual void TrackMemoryAllocatedChange(size_t old_size
,
33 gpu::gles2::MemoryTracker::Pool pool
)
36 virtual bool EnsureGPUMemoryAvailable(size_t size_needed
) OVERRIDE
{
41 virtual ~MemoryTrackerStub() {}
43 DISALLOW_COPY_AND_ASSIGN(MemoryTrackerStub
);
46 } // anonymous namespace
48 CommandBufferImpl::CommandBufferImpl(
49 gfx::GLShareGroup
* share_group
,
50 gpu::gles2::MailboxManager
* mailbox_manager
)
51 : widget_(gfx::kNullAcceleratedWidget
),
53 share_group_(share_group
),
54 mailbox_manager_(mailbox_manager
) {
57 CommandBufferImpl::CommandBufferImpl(
58 gfx::AcceleratedWidget widget
,
59 const gfx::Size
& size
,
60 gfx::GLShareGroup
* share_group
,
61 gpu::gles2::MailboxManager
* mailbox_manager
)
64 share_group_(share_group
),
65 mailbox_manager_(mailbox_manager
) {
68 CommandBufferImpl::~CommandBufferImpl() {
69 client()->DidDestroy();
71 bool have_context
= decoder_
->MakeCurrent();
72 decoder_
->Destroy(have_context
);
76 void CommandBufferImpl::Initialize(
77 CommandBufferSyncClientPtr sync_client
,
78 mojo::ScopedSharedBufferHandle shared_state
) {
79 sync_client_
= sync_client
.Pass();
80 sync_client_
->DidInitialize(DoInitialize(shared_state
.Pass()));
83 bool CommandBufferImpl::DoInitialize(
84 mojo::ScopedSharedBufferHandle shared_state
) {
85 if (widget_
== gfx::kNullAcceleratedWidget
)
86 surface_
= gfx::GLSurface::CreateOffscreenGLSurface(size_
);
88 surface_
= gfx::GLSurface::CreateViewGLSurface(widget_
);
92 // TODO(piman): virtual contexts, gpu preference.
93 context_
= gfx::GLContext::CreateGLContext(
94 share_group_
.get(), surface_
.get(), gfx::PreferIntegratedGpu
);
98 if (!context_
->MakeCurrent(surface_
.get()))
101 // TODO(piman): ShaderTranslatorCache is currently per-ContextGroup but
102 // only needs to be per-thread.
103 scoped_refptr
<gpu::gles2::ContextGroup
> context_group
=
104 new gpu::gles2::ContextGroup(mailbox_manager_
.get(),
105 new MemoryTrackerStub
,
106 new gpu::gles2::ShaderTranslatorCache
,
110 command_buffer_
.reset(
111 new gpu::CommandBufferService(context_group
->transfer_buffer_manager()));
112 bool result
= command_buffer_
->Initialize();
115 decoder_
.reset(::gpu::gles2::GLES2Decoder::Create(context_group
.get()));
116 scheduler_
.reset(new gpu::GpuScheduler(
117 command_buffer_
.get(), decoder_
.get(), decoder_
.get()));
118 decoder_
->set_engine(scheduler_
.get());
119 decoder_
->SetResizeCallback(
120 base::Bind(&CommandBufferImpl::OnResize
, base::Unretained(this)));
122 gpu::gles2::DisallowedFeatures disallowed_features
;
124 // TODO(piman): attributes.
125 std::vector
<int32
> attrib_vector
;
126 if (!decoder_
->Initialize(surface_
,
128 false /* offscreen */,
134 command_buffer_
->SetPutOffsetChangeCallback(base::Bind(
135 &gpu::GpuScheduler::PutChanged
, base::Unretained(scheduler_
.get())));
136 command_buffer_
->SetGetBufferChangeCallback(base::Bind(
137 &gpu::GpuScheduler::SetGetBuffer
, base::Unretained(scheduler_
.get())));
138 command_buffer_
->SetParseErrorCallback(
139 base::Bind(&CommandBufferImpl::OnParseError
, base::Unretained(this)));
141 // TODO(piman): other callbacks
143 const size_t kSize
= sizeof(gpu::CommandBufferSharedState
);
144 scoped_ptr
<gpu::BufferBacking
> backing(
145 gles2::MojoBufferBacking::Create(shared_state
.Pass(), kSize
));
149 command_buffer_
->SetSharedStateBuffer(backing
.Pass());
153 void CommandBufferImpl::SetGetBuffer(int32_t buffer
) {
154 command_buffer_
->SetGetBuffer(buffer
);
157 void CommandBufferImpl::Flush(int32_t put_offset
) {
158 if (!context_
->MakeCurrent(surface_
.get())) {
159 DLOG(WARNING
) << "Context lost";
160 client()->LostContext(gpu::error::kUnknown
);
163 command_buffer_
->Flush(put_offset
);
166 void CommandBufferImpl::MakeProgress(int32_t last_get_offset
) {
167 // TODO(piman): handle out-of-order.
168 sync_client_
->DidMakeProgress(
169 CommandBufferState::From(command_buffer_
->GetLastState()));
172 void CommandBufferImpl::RegisterTransferBuffer(
174 mojo::ScopedSharedBufferHandle transfer_buffer
,
176 // Take ownership of the memory and map it into this process.
177 // This validates the size.
178 scoped_ptr
<gpu::BufferBacking
> backing(
179 gles2::MojoBufferBacking::Create(transfer_buffer
.Pass(), size
));
181 DVLOG(0) << "Failed to map shared memory.";
184 command_buffer_
->RegisterTransferBuffer(id
, backing
.Pass());
187 void CommandBufferImpl::DestroyTransferBuffer(int32_t id
) {
188 command_buffer_
->DestroyTransferBuffer(id
);
191 void CommandBufferImpl::Echo(const Callback
<void()>& callback
) {
195 void CommandBufferImpl::OnParseError() {
196 gpu::CommandBuffer::State state
= command_buffer_
->GetLastState();
197 client()->LostContext(state
.context_lost_reason
);
200 void CommandBufferImpl::OnResize(gfx::Size size
, float scale_factor
) {
201 surface_
->Resize(size
);