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/public/cpp/bindings/allocation_scope.h"
20 #include "mojo/services/gles2/command_buffer_type_conversions.h"
21 #include "mojo/services/gles2/mojo_buffer_backing.h"
22 #include "ui/gl/gl_context.h"
23 #include "ui/gl/gl_surface.h"
30 class MemoryTrackerStub : public gpu::gles2::MemoryTracker {
32 MemoryTrackerStub() {}
34 virtual void TrackMemoryAllocatedChange(size_t old_size,
36 gpu::gles2::MemoryTracker::Pool pool)
39 virtual bool EnsureGPUMemoryAvailable(size_t size_needed) OVERRIDE {
44 virtual ~MemoryTrackerStub() {}
46 DISALLOW_COPY_AND_ASSIGN(MemoryTrackerStub);
49 } // anonymous namespace
51 CommandBufferImpl::CommandBufferImpl(gfx::AcceleratedWidget widget,
52 const gfx::Size& size)
53 : widget_(widget), size_(size) {}
55 CommandBufferImpl::~CommandBufferImpl() {
56 client_->DidDestroy();
58 bool have_context = decoder_->MakeCurrent();
59 decoder_->Destroy(have_context);
63 void CommandBufferImpl::OnConnectionError() {
64 // TODO(darin): How should we handle this error?
67 void CommandBufferImpl::SetClient(CommandBufferClient* client) {
71 void CommandBufferImpl::Initialize(
72 CommandBufferSyncClientPtr sync_client,
73 mojo::ScopedSharedBufferHandle shared_state) {
74 sync_client_ = sync_client.Pass();
75 sync_client_->DidInitialize(DoInitialize(shared_state.Pass()));
78 bool CommandBufferImpl::DoInitialize(
79 mojo::ScopedSharedBufferHandle shared_state) {
80 // TODO(piman): offscreen surface.
81 scoped_refptr<gfx::GLSurface> surface =
82 gfx::GLSurface::CreateViewGLSurface(widget_);
86 // TODO(piman): context sharing, virtual contexts, gpu preference.
87 scoped_refptr<gfx::GLContext> context = gfx::GLContext::CreateGLContext(
88 NULL, surface.get(), gfx::PreferIntegratedGpu);
92 if (!context->MakeCurrent(surface.get()))
95 // TODO(piman): ShaderTranslatorCache is currently per-ContextGroup but
96 // only needs to be per-thread.
97 scoped_refptr<gpu::gles2::ContextGroup> context_group =
98 new gpu::gles2::ContextGroup(NULL,
100 new MemoryTrackerStub,
101 new gpu::gles2::ShaderTranslatorCache,
105 command_buffer_.reset(
106 new gpu::CommandBufferService(context_group->transfer_buffer_manager()));
107 bool result = command_buffer_->Initialize();
110 decoder_.reset(::gpu::gles2::GLES2Decoder::Create(context_group.get()));
111 scheduler_.reset(new gpu::GpuScheduler(
112 command_buffer_.get(), decoder_.get(), decoder_.get()));
113 decoder_->set_engine(scheduler_.get());
115 gpu::gles2::DisallowedFeatures disallowed_features;
117 // TODO(piman): attributes.
118 std::vector<int32> attrib_vector;
119 if (!decoder_->Initialize(surface,
121 false /* offscreen */,
128 new gpu::GpuControlService(context_group->image_manager(), NULL));
130 command_buffer_->SetPutOffsetChangeCallback(base::Bind(
131 &gpu::GpuScheduler::PutChanged, base::Unretained(scheduler_.get())));
132 command_buffer_->SetGetBufferChangeCallback(base::Bind(
133 &gpu::GpuScheduler::SetGetBuffer, base::Unretained(scheduler_.get())));
134 command_buffer_->SetParseErrorCallback(
135 base::Bind(&CommandBufferImpl::OnParseError, base::Unretained(this)));
137 // TODO(piman): other callbacks
139 const size_t kSize = sizeof(gpu::CommandBufferSharedState);
140 scoped_ptr<gpu::BufferBacking> backing(
141 gles2::MojoBufferBacking::Create(shared_state.Pass(), kSize));
145 command_buffer_->SetSharedStateBuffer(backing.Pass());
149 void CommandBufferImpl::SetGetBuffer(int32_t buffer) {
150 command_buffer_->SetGetBuffer(buffer);
153 void CommandBufferImpl::Flush(int32_t put_offset) {
154 command_buffer_->Flush(put_offset);
157 void CommandBufferImpl::MakeProgress(int32_t last_get_offset) {
158 // TODO(piman): handle out-of-order.
159 AllocationScope scope;
160 sync_client_->DidMakeProgress(command_buffer_->GetLastState());
163 void CommandBufferImpl::RegisterTransferBuffer(
165 mojo::ScopedSharedBufferHandle transfer_buffer,
167 // Take ownership of the memory and map it into this process.
168 // This validates the size.
169 scoped_ptr<gpu::BufferBacking> backing(
170 gles2::MojoBufferBacking::Create(transfer_buffer.Pass(), size));
171 if (!backing.get()) {
172 DVLOG(0) << "Failed to map shared memory.";
175 command_buffer_->RegisterTransferBuffer(id, backing.Pass());
178 void CommandBufferImpl::DestroyTransferBuffer(int32_t id) {
179 command_buffer_->DestroyTransferBuffer(id);
182 void CommandBufferImpl::Echo(const Callback<void()>& callback) {
186 void CommandBufferImpl::RequestAnimationFrames() {
187 timer_.Start(FROM_HERE,
188 base::TimeDelta::FromMilliseconds(16),
190 &CommandBufferImpl::DrawAnimationFrame);
193 void CommandBufferImpl::CancelAnimationFrames() { timer_.Stop(); }
195 void CommandBufferImpl::OnParseError() {
196 gpu::CommandBuffer::State state = command_buffer_->GetLastState();
197 client_->LostContext(state.context_lost_reason);
200 void CommandBufferImpl::DrawAnimationFrame() { client_->DrawAnimationFrame(); }
202 } // namespace services