Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / mojo / services / gles2 / command_buffer_impl.cc
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.
4
5 #include "mojo/services/gles2/command_buffer_impl.h"
6
7 #include "base/bind.h"
8 #include "base/memory/shared_memory.h"
9
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"
24
25 namespace mojo {
26 namespace services {
27
28 namespace {
29
30 class MemoryTrackerStub : public gpu::gles2::MemoryTracker {
31  public:
32   MemoryTrackerStub() {}
33
34   virtual void TrackMemoryAllocatedChange(size_t old_size,
35                                           size_t new_size,
36                                           gpu::gles2::MemoryTracker::Pool pool)
37       OVERRIDE {}
38
39   virtual bool EnsureGPUMemoryAvailable(size_t size_needed) OVERRIDE {
40     return true;
41   };
42
43  private:
44   virtual ~MemoryTrackerStub() {}
45
46   DISALLOW_COPY_AND_ASSIGN(MemoryTrackerStub);
47 };
48
49 }  // anonymous namespace
50
51 CommandBufferImpl::CommandBufferImpl(gfx::AcceleratedWidget widget,
52                                      const gfx::Size& size)
53     : widget_(widget), size_(size) {}
54
55 CommandBufferImpl::~CommandBufferImpl() {
56   client_->DidDestroy();
57   if (decoder_.get()) {
58     bool have_context = decoder_->MakeCurrent();
59     decoder_->Destroy(have_context);
60   }
61 }
62
63 void CommandBufferImpl::OnConnectionError() {
64   // TODO(darin): How should we handle this error?
65 }
66
67 void CommandBufferImpl::SetClient(CommandBufferClient* client) {
68   client_ = client;
69 }
70
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()));
76 }
77
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_);
83   if (!surface.get())
84     return false;
85
86   // TODO(piman): context sharing, virtual contexts, gpu preference.
87   scoped_refptr<gfx::GLContext> context = gfx::GLContext::CreateGLContext(
88       NULL, surface.get(), gfx::PreferIntegratedGpu);
89   if (!context.get())
90     return false;
91
92   if (!context->MakeCurrent(surface.get()))
93     return false;
94
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,
99                                    NULL,
100                                    new MemoryTrackerStub,
101                                    new gpu::gles2::ShaderTranslatorCache,
102                                    NULL,
103                                    true);
104
105   command_buffer_.reset(
106       new gpu::CommandBufferService(context_group->transfer_buffer_manager()));
107   bool result = command_buffer_->Initialize();
108   DCHECK(result);
109
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());
114
115   gpu::gles2::DisallowedFeatures disallowed_features;
116
117   // TODO(piman): attributes.
118   std::vector<int32> attrib_vector;
119   if (!decoder_->Initialize(surface,
120                             context,
121                             false /* offscreen */,
122                             size_,
123                             disallowed_features,
124                             attrib_vector))
125     return false;
126
127   gpu_control_.reset(
128       new gpu::GpuControlService(context_group->image_manager(), NULL));
129
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)));
136
137   // TODO(piman): other callbacks
138
139   const size_t kSize = sizeof(gpu::CommandBufferSharedState);
140   scoped_ptr<gpu::BufferBacking> backing(
141       gles2::MojoBufferBacking::Create(shared_state.Pass(), kSize));
142   if (!backing.get())
143     return false;
144
145   command_buffer_->SetSharedStateBuffer(backing.Pass());
146   return true;
147 }
148
149 void CommandBufferImpl::SetGetBuffer(int32_t buffer) {
150   command_buffer_->SetGetBuffer(buffer);
151 }
152
153 void CommandBufferImpl::Flush(int32_t put_offset) {
154   command_buffer_->Flush(put_offset);
155 }
156
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());
161 }
162
163 void CommandBufferImpl::RegisterTransferBuffer(
164     int32_t id,
165     mojo::ScopedSharedBufferHandle transfer_buffer,
166     uint32_t size) {
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.";
173     return;
174   }
175   command_buffer_->RegisterTransferBuffer(id, backing.Pass());
176 }
177
178 void CommandBufferImpl::DestroyTransferBuffer(int32_t id) {
179   command_buffer_->DestroyTransferBuffer(id);
180 }
181
182 void CommandBufferImpl::Echo(const Callback<void()>& callback) {
183   callback.Run();
184 }
185
186 void CommandBufferImpl::RequestAnimationFrames() {
187   timer_.Start(FROM_HERE,
188                base::TimeDelta::FromMilliseconds(16),
189                this,
190                &CommandBufferImpl::DrawAnimationFrame);
191 }
192
193 void CommandBufferImpl::CancelAnimationFrames() { timer_.Stop(); }
194
195 void CommandBufferImpl::OnParseError() {
196   gpu::CommandBuffer::State state = command_buffer_->GetLastState();
197   client_->LostContext(state.context_lost_reason);
198 }
199
200 void CommandBufferImpl::DrawAnimationFrame() { client_->DrawAnimationFrame(); }
201
202 }  // namespace services
203 }  // namespace mojo