Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 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 | |
James Robinson | c9b163e | 2014-12-03 13:31:32 -0800 | [diff] [blame] | 5 | #include "services/gles2/command_buffer_driver.h" |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 6 | |
| 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 Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 11 | #include "gpu/command_buffer/common/value_state.h" |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 12 | #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 Barth | b087f9a | 2014-11-20 12:26:29 -0800 | [diff] [blame] | 19 | #include "gpu/command_buffer/service/sync_point_manager.h" |
Etienne Membrives | 175837a | 2014-12-19 15:45:38 +0100 | [diff] [blame] | 20 | #include "gpu/command_buffer/service/valuebuffer_manager.h" |
James Robinson | c9b163e | 2014-12-03 13:31:32 -0800 | [diff] [blame] | 21 | #include "services/gles2/command_buffer_type_conversions.h" |
| 22 | #include "services/gles2/mojo_buffer_backing.h" |
Adam Barth | 6848731 | 2014-11-25 12:14:20 -0800 | [diff] [blame] | 23 | #include "ui/gfx/vsync_provider.h" |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 24 | #include "ui/gl/gl_context.h" |
| 25 | #include "ui/gl/gl_surface.h" |
| 26 | |
James Robinson | c3704cd | 2014-12-05 11:10:47 -0800 | [diff] [blame] | 27 | namespace gles2 { |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 28 | |
| 29 | namespace { |
| 30 | |
| 31 | class 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 Barth | 6848731 | 2014-11-25 12:14:20 -0800 | [diff] [blame] | 50 | CommandBufferDriver::Client::~Client() { |
| 51 | } |
| 52 | |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 53 | CommandBufferDriver::CommandBufferDriver( |
| 54 | gfx::GLShareGroup* share_group, |
Adam Barth | b087f9a | 2014-11-20 12:26:29 -0800 | [diff] [blame] | 55 | gpu::gles2::MailboxManager* mailbox_manager, |
Ian Fischer | f66f437 | 2015-06-09 16:50:28 -0700 | [diff] [blame] | 56 | gpu::SyncPointManager* sync_point_manager, |
| 57 | const gfx::SurfaceConfiguration& requested_configuration) |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 58 | : CommandBufferDriver(gfx::kNullAcceleratedWidget, |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 59 | share_group, |
Adam Barth | b087f9a | 2014-11-20 12:26:29 -0800 | [diff] [blame] | 60 | mailbox_manager, |
Ian Fischer | f66f437 | 2015-06-09 16:50:28 -0700 | [diff] [blame] | 61 | sync_point_manager, |
| 62 | requested_configuration) { |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | CommandBufferDriver::CommandBufferDriver( |
| 66 | gfx::AcceleratedWidget widget, |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 67 | gfx::GLShareGroup* share_group, |
Adam Barth | b087f9a | 2014-11-20 12:26:29 -0800 | [diff] [blame] | 68 | gpu::gles2::MailboxManager* mailbox_manager, |
Ian Fischer | f66f437 | 2015-06-09 16:50:28 -0700 | [diff] [blame] | 69 | gpu::SyncPointManager* sync_point_manager, |
| 70 | const gfx::SurfaceConfiguration& requested_configuration) |
Adam Barth | 6848731 | 2014-11-25 12:14:20 -0800 | [diff] [blame] | 71 | : client_(nullptr), |
| 72 | widget_(widget), |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 73 | share_group_(share_group), |
Adam Barth | b087f9a | 2014-11-20 12:26:29 -0800 | [diff] [blame] | 74 | mailbox_manager_(mailbox_manager), |
| 75 | sync_point_manager_(sync_point_manager), |
Ian Fischer | f66f437 | 2015-06-09 16:50:28 -0700 | [diff] [blame] | 76 | requested_configuration_(requested_configuration), |
Adam Barth | b087f9a | 2014-11-20 12:26:29 -0800 | [diff] [blame] | 77 | weak_factory_(this) { |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | CommandBufferDriver::~CommandBufferDriver() { |
| 81 | if (decoder_) { |
| 82 | bool have_context = decoder_->MakeCurrent(); |
| 83 | decoder_->Destroy(have_context); |
| 84 | } |
| 85 | } |
| 86 | |
James Robinson | c3704cd | 2014-12-05 11:10:47 -0800 | [diff] [blame] | 87 | void CommandBufferDriver::Initialize( |
| 88 | mojo::CommandBufferSyncClientPtr sync_client, |
James Robinson | fb32716 | 2015-01-30 13:12:13 -0800 | [diff] [blame] | 89 | mojo::CommandBufferLostContextObserverPtr loss_observer, |
James Robinson | c3704cd | 2014-12-05 11:10:47 -0800 | [diff] [blame] | 90 | mojo::ScopedSharedBufferHandle shared_state) { |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 91 | sync_client_ = sync_client.Pass(); |
James Robinson | fb32716 | 2015-01-30 13:12:13 -0800 | [diff] [blame] | 92 | loss_observer_ = loss_observer.Pass(); |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 93 | bool success = DoInitialize(shared_state.Pass()); |
James Robinson | c3704cd | 2014-12-05 11:10:47 -0800 | [diff] [blame] | 94 | mojo::GpuCapabilitiesPtr capabilities = |
| 95 | success ? mojo::GpuCapabilities::From(decoder_->GetCapabilities()) |
| 96 | : mojo::GpuCapabilities::New(); |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 97 | sync_client_->DidInitialize(success, capabilities.Pass()); |
| 98 | } |
| 99 | |
James Robinson | c3704cd | 2014-12-05 11:10:47 -0800 | [diff] [blame] | 100 | bool CommandBufferDriver::DoInitialize( |
| 101 | mojo::ScopedSharedBufferHandle shared_state) { |
Craig Stout | e5b4b95 | 2015-09-11 14:57:22 -0700 | [diff] [blame] | 102 | if (widget_ == gfx::kNullAcceleratedWidget) { |
| 103 | // This is a dummy surface, so attempt to skip actual surface |
| 104 | // allocation by passing size 0. |
Ian Fischer | f66f437 | 2015-06-09 16:50:28 -0700 | [diff] [blame] | 105 | surface_ = gfx::GLSurface::CreateOffscreenGLSurface( |
Craig Stout | e5b4b95 | 2015-09-11 14:57:22 -0700 | [diff] [blame] | 106 | 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 Fischer | f66f437 | 2015-06-09 16:50:28 -0700 | [diff] [blame] | 113 | surface_ = |
| 114 | gfx::GLSurface::CreateViewGLSurface(widget_, requested_configuration_); |
Ojan Vafai | dfb2b12 | 2014-12-04 15:33:52 -0800 | [diff] [blame] | 115 | if (auto vsync_provider = surface_->GetVSyncProvider()) { |
| 116 | vsync_provider->GetVSyncParameters( |
| 117 | base::Bind(&CommandBufferDriver::OnUpdateVSyncParameters, |
| 118 | weak_factory_.GetWeakPtr())); |
| 119 | } |
Adam Barth | 6848731 | 2014-11-25 12:14:20 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 122 | 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 Barth | c53ca4b | 2015-01-28 15:55:36 -0800 | [diff] [blame] | 136 | bool bind_generates_resource = false; |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 137 | scoped_refptr<gpu::gles2::ContextGroup> context_group = |
Adam Barth | c53ca4b | 2015-01-28 15:55:36 -0800 | [diff] [blame] | 138 | new gpu::gles2::ContextGroup( |
| 139 | mailbox_manager_.get(), new MemoryTrackerStub, |
| 140 | new gpu::gles2::ShaderTranslatorCache, nullptr, nullptr, nullptr, |
| 141 | bind_generates_resource); |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 142 | |
| 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 Barth | b087f9a | 2014-11-20 12:26:29 -0800 | [diff] [blame] | 154 | decoder_->SetWaitSyncPointCallback(base::Bind( |
| 155 | &CommandBufferDriver::OnWaitSyncPoint, base::Unretained(this))); |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 156 | |
| 157 | gpu::gles2::DisallowedFeatures disallowed_features; |
| 158 | |
| 159 | // TODO(piman): attributes. |
| 160 | std::vector<int32> attrib_vector; |
James Robinson | 187e1fa | 2015-03-04 15:49:53 -0800 | [diff] [blame] | 161 | if (!decoder_->Initialize(surface_, context_, false /* offscreen */, |
| 162 | gfx::Size(1, 1), disallowed_features, |
| 163 | attrib_vector)) |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 164 | 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 | |
| 185 | void CommandBufferDriver::SetGetBuffer(int32_t buffer) { |
| 186 | command_buffer_->SetGetBuffer(buffer); |
| 187 | } |
| 188 | |
| 189 | void 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 | |
| 198 | void CommandBufferDriver::MakeProgress(int32_t last_get_offset) { |
| 199 | // TODO(piman): handle out-of-order. |
| 200 | sync_client_->DidMakeProgress( |
James Robinson | c3704cd | 2014-12-05 11:10:47 -0800 | [diff] [blame] | 201 | mojo::CommandBufferState::From(command_buffer_->GetLastState())); |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void CommandBufferDriver::RegisterTransferBuffer( |
| 205 | int32_t id, |
James Robinson | c3704cd | 2014-12-05 11:10:47 -0800 | [diff] [blame] | 206 | mojo::ScopedSharedBufferHandle transfer_buffer, |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 207 | 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 | |
| 219 | void CommandBufferDriver::DestroyTransferBuffer(int32_t id) { |
| 220 | command_buffer_->DestroyTransferBuffer(id); |
| 221 | } |
| 222 | |
James Robinson | c3704cd | 2014-12-05 11:10:47 -0800 | [diff] [blame] | 223 | void CommandBufferDriver::Echo(const mojo::Callback<void()>& callback) { |
Adam Barth | 1658f11 | 2014-11-19 20:09:14 -0800 | [diff] [blame] | 224 | callback.Run(); |
| 225 | } |
| 226 | |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 227 | void CommandBufferDriver::OnParseError() { |
| 228 | gpu::CommandBuffer::State state = command_buffer_->GetLastState(); |
| 229 | OnContextLost(state.context_lost_reason); |
| 230 | } |
| 231 | |
| 232 | void CommandBufferDriver::OnResize(gfx::Size size, float scale_factor) { |
| 233 | surface_->Resize(size); |
| 234 | } |
| 235 | |
Adam Barth | b087f9a | 2014-11-20 12:26:29 -0800 | [diff] [blame] | 236 | bool CommandBufferDriver::OnWaitSyncPoint(uint32_t sync_point) { |
| 237 | if (!sync_point) |
| 238 | return true; |
cdotstout | 6353225 | 2015-09-17 23:02:57 -0700 | [diff] [blame^] | 239 | if (mailbox_manager_->UsesSync()) |
| 240 | mailbox_manager_->PullTextureUpdates(sync_point); |
Adam Barth | b087f9a | 2014-11-20 12:26:29 -0800 | [diff] [blame] | 241 | 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 | |
| 250 | void CommandBufferDriver::OnSyncPointRetired() { |
| 251 | scheduler_->SetScheduled(true); |
| 252 | } |
| 253 | |
cdotstout | 6353225 | 2015-09-17 23:02:57 -0700 | [diff] [blame^] | 254 | void 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 Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 260 | void CommandBufferDriver::OnContextLost(uint32_t reason) { |
James Robinson | fb32716 | 2015-01-30 13:12:13 -0800 | [diff] [blame] | 261 | loss_observer_->DidLoseContext(reason); |
| 262 | client_->DidLoseContext(); |
Adam Barth | 6848731 | 2014-11-25 12:14:20 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void CommandBufferDriver::OnUpdateVSyncParameters( |
| 266 | const base::TimeTicks timebase, |
| 267 | const base::TimeDelta interval) { |
| 268 | client_->UpdateVSyncParameters(timebase, interval); |
Adam Barth | 116f6fe | 2014-11-18 09:30:34 -0800 | [diff] [blame] | 269 | } |
| 270 | |
James Robinson | c3704cd | 2014-12-05 11:10:47 -0800 | [diff] [blame] | 271 | } // namespace gles2 |