gpu: Separate GpuControlService from GpuControl
[chromium-blink-merge.git] / mojo / services / gles2 / command_buffer_impl.cc
bloba18a50836aa85a443c4bf748b8f99c4e35751583
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"
7 #include "base/bind.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"
25 namespace mojo {
26 namespace services {
28 namespace {
30 class MemoryTrackerStub : public gpu::gles2::MemoryTracker {
31 public:
32 MemoryTrackerStub() {}
34 virtual void TrackMemoryAllocatedChange(size_t old_size,
35 size_t new_size,
36 gpu::gles2::MemoryTracker::Pool pool)
37 OVERRIDE {}
39 virtual bool EnsureGPUMemoryAvailable(size_t size_needed) OVERRIDE {
40 return true;
43 private:
44 virtual ~MemoryTrackerStub() {}
46 DISALLOW_COPY_AND_ASSIGN(MemoryTrackerStub);
49 } // anonymous namespace
51 CommandBufferImpl::CommandBufferImpl(ScopedCommandBufferClientHandle client,
52 gfx::AcceleratedWidget widget,
53 const gfx::Size& size)
54 : client_(client.Pass(), this), widget_(widget), size_(size) {}
56 CommandBufferImpl::~CommandBufferImpl() { client_->DidDestroy(); }
58 void CommandBufferImpl::Initialize(
59 ScopedCommandBufferSyncClientHandle sync_client,
60 mojo::ScopedSharedBufferHandle shared_state) {
61 sync_client_.reset(sync_client.Pass(), NULL);
62 sync_client_->DidInitialize(DoInitialize(shared_state.Pass()));
65 bool CommandBufferImpl::DoInitialize(
66 mojo::ScopedSharedBufferHandle shared_state) {
67 // TODO(piman): offscreen surface.
68 scoped_refptr<gfx::GLSurface> surface =
69 gfx::GLSurface::CreateViewGLSurface(widget_);
70 if (!surface.get())
71 return false;
73 // TODO(piman): context sharing, virtual contexts, gpu preference.
74 scoped_refptr<gfx::GLContext> context = gfx::GLContext::CreateGLContext(
75 NULL, surface.get(), gfx::PreferIntegratedGpu);
76 if (!context.get())
77 return false;
79 if (!context->MakeCurrent(surface.get()))
80 return false;
82 // TODO(piman): ShaderTranslatorCache is currently per-ContextGroup but
83 // only needs to be per-thread.
84 scoped_refptr<gpu::gles2::ContextGroup> context_group =
85 new gpu::gles2::ContextGroup(NULL,
86 NULL,
87 new MemoryTrackerStub,
88 new gpu::gles2::ShaderTranslatorCache,
89 NULL,
90 true);
92 command_buffer_.reset(
93 new gpu::CommandBufferService(context_group->transfer_buffer_manager()));
94 bool result = command_buffer_->Initialize();
95 DCHECK(result);
97 decoder_.reset(::gpu::gles2::GLES2Decoder::Create(context_group.get()));
98 scheduler_.reset(new gpu::GpuScheduler(
99 command_buffer_.get(), decoder_.get(), decoder_.get()));
100 decoder_->set_engine(scheduler_.get());
102 gpu::gles2::DisallowedFeatures disallowed_features;
104 // TODO(piman): attributes.
105 std::vector<int32> attrib_vector;
106 if (!decoder_->Initialize(surface,
107 context,
108 false /* offscreen */,
109 size_,
110 disallowed_features,
111 attrib_vector))
112 return false;
114 gpu_control_.reset(
115 new gpu::GpuControlService(context_group->image_manager(), NULL));
117 command_buffer_->SetPutOffsetChangeCallback(base::Bind(
118 &gpu::GpuScheduler::PutChanged, base::Unretained(scheduler_.get())));
119 command_buffer_->SetGetBufferChangeCallback(base::Bind(
120 &gpu::GpuScheduler::SetGetBuffer, base::Unretained(scheduler_.get())));
121 command_buffer_->SetParseErrorCallback(
122 base::Bind(&CommandBufferImpl::OnParseError, base::Unretained(this)));
124 // TODO(piman): other callbacks
126 const size_t kSize = sizeof(gpu::CommandBufferSharedState);
127 scoped_ptr<gpu::BufferBacking> backing(
128 gles2::MojoBufferBacking::Create(shared_state.Pass(), kSize));
129 if (!backing.get())
130 return false;
132 command_buffer_->SetSharedStateBuffer(backing.Pass());
133 return true;
136 void CommandBufferImpl::SetGetBuffer(int32_t buffer) {
137 command_buffer_->SetGetBuffer(buffer);
140 void CommandBufferImpl::Flush(int32_t put_offset) {
141 command_buffer_->Flush(put_offset);
144 void CommandBufferImpl::MakeProgress(int32_t last_get_offset) {
145 // TODO(piman): handle out-of-order.
146 AllocationScope scope;
147 sync_client_->DidMakeProgress(command_buffer_->GetLastState());
150 void CommandBufferImpl::RegisterTransferBuffer(
151 int32_t id,
152 mojo::ScopedSharedBufferHandle transfer_buffer,
153 uint32_t size) {
154 // Take ownership of the memory and map it into this process.
155 // This validates the size.
156 scoped_ptr<gpu::BufferBacking> backing(
157 gles2::MojoBufferBacking::Create(transfer_buffer.Pass(), size));
158 if (!backing.get()) {
159 DVLOG(0) << "Failed to map shared memory.";
160 return;
162 command_buffer_->RegisterTransferBuffer(id, backing.Pass());
165 void CommandBufferImpl::DestroyTransferBuffer(int32_t id) {
166 command_buffer_->DestroyTransferBuffer(id);
169 void CommandBufferImpl::Echo(const Callback<void()>& callback) {
170 callback.Run();
173 void CommandBufferImpl::RequestAnimationFrames() {
174 timer_.Start(FROM_HERE,
175 base::TimeDelta::FromMilliseconds(16),
176 this,
177 &CommandBufferImpl::DrawAnimationFrame);
180 void CommandBufferImpl::CancelAnimationFrames() { timer_.Stop(); }
182 void CommandBufferImpl::OnParseError() {
183 gpu::CommandBuffer::State state = command_buffer_->GetLastState();
184 client_->LostContext(state.context_lost_reason);
187 void CommandBufferImpl::DrawAnimationFrame() { client_->DrawAnimationFrame(); }
189 } // namespace services
190 } // namespace mojo