blob: 1c3f9f62314fb0375bf5ed7d36e79b24e67a2508 [file] [log] [blame]
Adam Barth116f6fe2014-11-18 09:30:34 -08001// 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
James Robinsonc9b163e2014-12-03 13:31:32 -08005#include "services/gles2/command_buffer_driver.h"
Adam Barth116f6fe2014-11-18 09:30:34 -08006
7#include "base/bind.h"
8#include "base/macros.h"
9#include "base/memory/shared_memory.h"
10#include "gpu/command_buffer/common/constants.h"
James Robinson6a64b812014-12-03 13:38:42 -080011#include "gpu/command_buffer/common/value_state.h"
Adam Barth116f6fe2014-11-18 09:30:34 -080012#include "gpu/command_buffer/service/command_buffer_service.h"
13#include "gpu/command_buffer/service/context_group.h"
14#include "gpu/command_buffer/service/gles2_cmd_decoder.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"
Adam Barthb087f9a2014-11-20 12:26:29 -080019#include "gpu/command_buffer/service/sync_point_manager.h"
Etienne Membrives175837a2014-12-19 15:45:38 +010020#include "gpu/command_buffer/service/valuebuffer_manager.h"
James Robinsonc9b163e2014-12-03 13:31:32 -080021#include "services/gles2/command_buffer_type_conversions.h"
22#include "services/gles2/mojo_buffer_backing.h"
Adam Barth68487312014-11-25 12:14:20 -080023#include "ui/gfx/vsync_provider.h"
Adam Barth116f6fe2014-11-18 09:30:34 -080024#include "ui/gl/gl_context.h"
25#include "ui/gl/gl_surface.h"
26
James Robinsonc3704cd2014-12-05 11:10:47 -080027namespace gles2 {
Adam Barth116f6fe2014-11-18 09:30:34 -080028
29namespace {
30
31class MemoryTrackerStub : public gpu::gles2::MemoryTracker {
32 public:
33 MemoryTrackerStub() {}
34
35 void TrackMemoryAllocatedChange(
36 size_t old_size,
37 size_t new_size,
38 gpu::gles2::MemoryTracker::Pool pool) override {}
39
40 bool EnsureGPUMemoryAvailable(size_t size_needed) override { return true; };
41
42 private:
43 ~MemoryTrackerStub() override {}
44
45 DISALLOW_COPY_AND_ASSIGN(MemoryTrackerStub);
46};
47
48} // anonymous namespace
49
Adam Barth68487312014-11-25 12:14:20 -080050CommandBufferDriver::Client::~Client() {
51}
52
Adam Barth116f6fe2014-11-18 09:30:34 -080053CommandBufferDriver::CommandBufferDriver(
54 gfx::GLShareGroup* share_group,
Adam Barthb087f9a2014-11-20 12:26:29 -080055 gpu::gles2::MailboxManager* mailbox_manager,
Ian Fischerf66f4372015-06-09 16:50:28 -070056 gpu::SyncPointManager* sync_point_manager,
57 const gfx::SurfaceConfiguration& requested_configuration)
Adam Barth116f6fe2014-11-18 09:30:34 -080058 : CommandBufferDriver(gfx::kNullAcceleratedWidget,
Adam Barth116f6fe2014-11-18 09:30:34 -080059 share_group,
Adam Barthb087f9a2014-11-20 12:26:29 -080060 mailbox_manager,
Ian Fischerf66f4372015-06-09 16:50:28 -070061 sync_point_manager,
62 requested_configuration) {
Adam Barth116f6fe2014-11-18 09:30:34 -080063}
64
65CommandBufferDriver::CommandBufferDriver(
66 gfx::AcceleratedWidget widget,
Adam Barth116f6fe2014-11-18 09:30:34 -080067 gfx::GLShareGroup* share_group,
Adam Barthb087f9a2014-11-20 12:26:29 -080068 gpu::gles2::MailboxManager* mailbox_manager,
Ian Fischerf66f4372015-06-09 16:50:28 -070069 gpu::SyncPointManager* sync_point_manager,
70 const gfx::SurfaceConfiguration& requested_configuration)
Adam Barth68487312014-11-25 12:14:20 -080071 : client_(nullptr),
72 widget_(widget),
Adam Barth116f6fe2014-11-18 09:30:34 -080073 share_group_(share_group),
Adam Barthb087f9a2014-11-20 12:26:29 -080074 mailbox_manager_(mailbox_manager),
75 sync_point_manager_(sync_point_manager),
Ian Fischerf66f4372015-06-09 16:50:28 -070076 requested_configuration_(requested_configuration),
Adam Barthb087f9a2014-11-20 12:26:29 -080077 weak_factory_(this) {
Adam Barth116f6fe2014-11-18 09:30:34 -080078}
79
80CommandBufferDriver::~CommandBufferDriver() {
81 if (decoder_) {
82 bool have_context = decoder_->MakeCurrent();
83 decoder_->Destroy(have_context);
84 }
85}
86
James Robinsonc3704cd2014-12-05 11:10:47 -080087void CommandBufferDriver::Initialize(
88 mojo::CommandBufferSyncClientPtr sync_client,
James Robinsonfb327162015-01-30 13:12:13 -080089 mojo::CommandBufferLostContextObserverPtr loss_observer,
James Robinsonc3704cd2014-12-05 11:10:47 -080090 mojo::ScopedSharedBufferHandle shared_state) {
Adam Barth116f6fe2014-11-18 09:30:34 -080091 sync_client_ = sync_client.Pass();
James Robinsonfb327162015-01-30 13:12:13 -080092 loss_observer_ = loss_observer.Pass();
Adam Barth116f6fe2014-11-18 09:30:34 -080093 bool success = DoInitialize(shared_state.Pass());
James Robinsonc3704cd2014-12-05 11:10:47 -080094 mojo::GpuCapabilitiesPtr capabilities =
95 success ? mojo::GpuCapabilities::From(decoder_->GetCapabilities())
96 : mojo::GpuCapabilities::New();
Adam Barth116f6fe2014-11-18 09:30:34 -080097 sync_client_->DidInitialize(success, capabilities.Pass());
98}
99
James Robinsonc3704cd2014-12-05 11:10:47 -0800100bool CommandBufferDriver::DoInitialize(
101 mojo::ScopedSharedBufferHandle shared_state) {
Craig Stoute5b4b952015-09-11 14:57:22 -0700102 if (widget_ == gfx::kNullAcceleratedWidget) {
103 // This is a dummy surface, so attempt to skip actual surface
104 // allocation by passing size 0.
Ian Fischerf66f4372015-06-09 16:50:28 -0700105 surface_ = gfx::GLSurface::CreateOffscreenGLSurface(
Craig Stoute5b4b952015-09-11 14:57:22 -0700106 gfx::Size(0, 0), requested_configuration_);
107 if (!surface_.get()) {
108 // If size 0 was a fail, retry with size 1.
109 surface_ = gfx::GLSurface::CreateOffscreenGLSurface(
110 gfx::Size(1, 1), requested_configuration_);
111 }
112 } else {
Ian Fischerf66f4372015-06-09 16:50:28 -0700113 surface_ =
114 gfx::GLSurface::CreateViewGLSurface(widget_, requested_configuration_);
Ojan Vafaidfb2b122014-12-04 15:33:52 -0800115 if (auto vsync_provider = surface_->GetVSyncProvider()) {
116 vsync_provider->GetVSyncParameters(
117 base::Bind(&CommandBufferDriver::OnUpdateVSyncParameters,
118 weak_factory_.GetWeakPtr()));
119 }
Adam Barth68487312014-11-25 12:14:20 -0800120 }
121
Adam Barth116f6fe2014-11-18 09:30:34 -0800122 if (!surface_.get())
123 return false;
124
125 // TODO(piman): virtual contexts, gpu preference.
126 context_ = gfx::GLContext::CreateGLContext(share_group_.get(), surface_.get(),
127 gfx::PreferIntegratedGpu);
128 if (!context_.get())
129 return false;
130
131 if (!context_->MakeCurrent(surface_.get()))
132 return false;
133
134 // TODO(piman): ShaderTranslatorCache is currently per-ContextGroup but
135 // only needs to be per-thread.
Adam Barthc53ca4b2015-01-28 15:55:36 -0800136 bool bind_generates_resource = false;
Adam Barth116f6fe2014-11-18 09:30:34 -0800137 scoped_refptr<gpu::gles2::ContextGroup> context_group =
Adam Barthc53ca4b2015-01-28 15:55:36 -0800138 new gpu::gles2::ContextGroup(
139 mailbox_manager_.get(), new MemoryTrackerStub,
140 new gpu::gles2::ShaderTranslatorCache, nullptr, nullptr, nullptr,
141 bind_generates_resource);
Adam Barth116f6fe2014-11-18 09:30:34 -0800142
143 command_buffer_.reset(
144 new gpu::CommandBufferService(context_group->transfer_buffer_manager()));
145 bool result = command_buffer_->Initialize();
146 DCHECK(result);
147
148 decoder_.reset(::gpu::gles2::GLES2Decoder::Create(context_group.get()));
149 scheduler_.reset(new gpu::GpuScheduler(command_buffer_.get(), decoder_.get(),
150 decoder_.get()));
151 decoder_->set_engine(scheduler_.get());
152 decoder_->SetResizeCallback(
153 base::Bind(&CommandBufferDriver::OnResize, base::Unretained(this)));
Adam Barthb087f9a2014-11-20 12:26:29 -0800154 decoder_->SetWaitSyncPointCallback(base::Bind(
155 &CommandBufferDriver::OnWaitSyncPoint, base::Unretained(this)));
Adam Barth116f6fe2014-11-18 09:30:34 -0800156
157 gpu::gles2::DisallowedFeatures disallowed_features;
158
159 // TODO(piman): attributes.
160 std::vector<int32> attrib_vector;
James Robinson187e1fa2015-03-04 15:49:53 -0800161 if (!decoder_->Initialize(surface_, context_, false /* offscreen */,
162 gfx::Size(1, 1), disallowed_features,
163 attrib_vector))
Adam Barth116f6fe2014-11-18 09:30:34 -0800164 return false;
165
166 command_buffer_->SetPutOffsetChangeCallback(base::Bind(
167 &gpu::GpuScheduler::PutChanged, base::Unretained(scheduler_.get())));
168 command_buffer_->SetGetBufferChangeCallback(base::Bind(
169 &gpu::GpuScheduler::SetGetBuffer, base::Unretained(scheduler_.get())));
170 command_buffer_->SetParseErrorCallback(
171 base::Bind(&CommandBufferDriver::OnParseError, base::Unretained(this)));
172
173 // TODO(piman): other callbacks
174
175 const size_t kSize = sizeof(gpu::CommandBufferSharedState);
176 scoped_ptr<gpu::BufferBacking> backing(
177 gles2::MojoBufferBacking::Create(shared_state.Pass(), kSize));
178 if (!backing)
179 return false;
180
181 command_buffer_->SetSharedStateBuffer(backing.Pass());
182 return true;
183}
184
185void CommandBufferDriver::SetGetBuffer(int32_t buffer) {
186 command_buffer_->SetGetBuffer(buffer);
187}
188
189void CommandBufferDriver::Flush(int32_t put_offset) {
190 if (!context_->MakeCurrent(surface_.get())) {
191 DLOG(WARNING) << "Context lost";
192 OnContextLost(gpu::error::kUnknown);
193 return;
194 }
195 command_buffer_->Flush(put_offset);
196}
197
198void CommandBufferDriver::MakeProgress(int32_t last_get_offset) {
199 // TODO(piman): handle out-of-order.
200 sync_client_->DidMakeProgress(
James Robinsonc3704cd2014-12-05 11:10:47 -0800201 mojo::CommandBufferState::From(command_buffer_->GetLastState()));
Adam Barth116f6fe2014-11-18 09:30:34 -0800202}
203
204void CommandBufferDriver::RegisterTransferBuffer(
205 int32_t id,
James Robinsonc3704cd2014-12-05 11:10:47 -0800206 mojo::ScopedSharedBufferHandle transfer_buffer,
Adam Barth116f6fe2014-11-18 09:30:34 -0800207 uint32_t size) {
208 // Take ownership of the memory and map it into this process.
209 // This validates the size.
210 scoped_ptr<gpu::BufferBacking> backing(
211 gles2::MojoBufferBacking::Create(transfer_buffer.Pass(), size));
212 if (!backing) {
213 DVLOG(0) << "Failed to map shared memory.";
214 return;
215 }
216 command_buffer_->RegisterTransferBuffer(id, backing.Pass());
217}
218
219void CommandBufferDriver::DestroyTransferBuffer(int32_t id) {
220 command_buffer_->DestroyTransferBuffer(id);
221}
222
James Robinsonc3704cd2014-12-05 11:10:47 -0800223void CommandBufferDriver::Echo(const mojo::Callback<void()>& callback) {
Adam Barth1658f112014-11-19 20:09:14 -0800224 callback.Run();
225}
226
Adam Barth116f6fe2014-11-18 09:30:34 -0800227void CommandBufferDriver::OnParseError() {
228 gpu::CommandBuffer::State state = command_buffer_->GetLastState();
229 OnContextLost(state.context_lost_reason);
230}
231
232void CommandBufferDriver::OnResize(gfx::Size size, float scale_factor) {
233 surface_->Resize(size);
234}
235
Adam Barthb087f9a2014-11-20 12:26:29 -0800236bool CommandBufferDriver::OnWaitSyncPoint(uint32_t sync_point) {
237 if (!sync_point)
238 return true;
cdotstout63532252015-09-17 23:02:57 -0700239 if (mailbox_manager_->UsesSync())
240 mailbox_manager_->PullTextureUpdates(sync_point);
Adam Barthb087f9a2014-11-20 12:26:29 -0800241 if (sync_point_manager_->IsSyncPointRetired(sync_point))
242 return true;
243 scheduler_->SetScheduled(false);
244 sync_point_manager_->AddSyncPointCallback(
245 sync_point, base::Bind(&CommandBufferDriver::OnSyncPointRetired,
246 weak_factory_.GetWeakPtr()));
247 return scheduler_->IsScheduled();
248}
249
250void CommandBufferDriver::OnSyncPointRetired() {
251 scheduler_->SetScheduled(true);
252}
253
cdotstout63532252015-09-17 23:02:57 -0700254void CommandBufferDriver::RetireSyncPointOnGpuThread(uint32 sync_point) {
255 if (mailbox_manager_->UsesSync())
256 mailbox_manager_->PushTextureUpdates(sync_point);
257 sync_point_manager_->RetireSyncPoint(sync_point);
258}
259
Adam Barth116f6fe2014-11-18 09:30:34 -0800260void CommandBufferDriver::OnContextLost(uint32_t reason) {
James Robinsonfb327162015-01-30 13:12:13 -0800261 loss_observer_->DidLoseContext(reason);
262 client_->DidLoseContext();
Adam Barth68487312014-11-25 12:14:20 -0800263}
264
265void CommandBufferDriver::OnUpdateVSyncParameters(
266 const base::TimeTicks timebase,
267 const base::TimeDelta interval) {
268 client_->UpdateVSyncParameters(timebase, interval);
Adam Barth116f6fe2014-11-18 09:30:34 -0800269}
270
James Robinsonc3704cd2014-12-05 11:10:47 -0800271} // namespace gles2