OLD | NEW |
(Empty) | |
| 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_driver.h" |
| 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" |
| 11 #include "gpu/command_buffer/common/value_state.h" |
| 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" |
| 19 #include "gpu/command_buffer/service/sync_point_manager.h" |
| 20 #include "gpu/command_buffer/service/valuebuffer_manager.h" |
| 21 #include "mojo/services/gles2/command_buffer_type_conversions.h" |
| 22 #include "mojo/services/gles2/mojo_buffer_backing.h" |
| 23 #include "ui/gfx/vsync_provider.h" |
| 24 #include "ui/gl/gl_context.h" |
| 25 #include "ui/gl/gl_surface.h" |
| 26 |
| 27 namespace gles2 { |
| 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 |
| 50 CommandBufferDriver::Client::~Client() { |
| 51 } |
| 52 |
| 53 CommandBufferDriver::CommandBufferDriver( |
| 54 gfx::GLShareGroup* share_group, |
| 55 gpu::gles2::MailboxManager* mailbox_manager, |
| 56 gpu::SyncPointManager* sync_point_manager) |
| 57 : CommandBufferDriver(gfx::kNullAcceleratedWidget, |
| 58 share_group, |
| 59 mailbox_manager, |
| 60 sync_point_manager) { |
| 61 } |
| 62 |
| 63 CommandBufferDriver::CommandBufferDriver( |
| 64 gfx::AcceleratedWidget widget, |
| 65 gfx::GLShareGroup* share_group, |
| 66 gpu::gles2::MailboxManager* mailbox_manager, |
| 67 gpu::SyncPointManager* sync_point_manager) |
| 68 : client_(nullptr), |
| 69 widget_(widget), |
| 70 share_group_(share_group), |
| 71 mailbox_manager_(mailbox_manager), |
| 72 sync_point_manager_(sync_point_manager), |
| 73 weak_factory_(this) { |
| 74 } |
| 75 |
| 76 CommandBufferDriver::~CommandBufferDriver() { |
| 77 if (decoder_) { |
| 78 bool have_context = decoder_->MakeCurrent(); |
| 79 decoder_->Destroy(have_context); |
| 80 } |
| 81 } |
| 82 |
| 83 void CommandBufferDriver::Initialize( |
| 84 mojo::CommandBufferSyncClientPtr sync_client, |
| 85 mojo::CommandBufferLostContextObserverPtr loss_observer, |
| 86 mojo::ScopedSharedBufferHandle shared_state) { |
| 87 sync_client_ = sync_client.Pass(); |
| 88 loss_observer_ = loss_observer.Pass(); |
| 89 bool success = DoInitialize(shared_state.Pass()); |
| 90 mojo::GpuCapabilitiesPtr capabilities = |
| 91 success ? mojo::GpuCapabilities::From(decoder_->GetCapabilities()) |
| 92 : mojo::GpuCapabilities::New(); |
| 93 sync_client_->DidInitialize(success, capabilities.Pass()); |
| 94 } |
| 95 |
| 96 bool CommandBufferDriver::DoInitialize( |
| 97 mojo::ScopedSharedBufferHandle shared_state) { |
| 98 if (widget_ == gfx::kNullAcceleratedWidget) |
| 99 surface_ = gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size(1, 1)); |
| 100 else { |
| 101 surface_ = gfx::GLSurface::CreateViewGLSurface(widget_); |
| 102 if (auto vsync_provider = surface_->GetVSyncProvider()) { |
| 103 vsync_provider->GetVSyncParameters( |
| 104 base::Bind(&CommandBufferDriver::OnUpdateVSyncParameters, |
| 105 weak_factory_.GetWeakPtr())); |
| 106 } |
| 107 } |
| 108 |
| 109 if (!surface_.get()) |
| 110 return false; |
| 111 |
| 112 // TODO(piman): virtual contexts, gpu preference. |
| 113 context_ = gfx::GLContext::CreateGLContext(share_group_.get(), surface_.get(), |
| 114 gfx::PreferIntegratedGpu); |
| 115 if (!context_.get()) |
| 116 return false; |
| 117 |
| 118 if (!context_->MakeCurrent(surface_.get())) |
| 119 return false; |
| 120 |
| 121 // TODO(piman): ShaderTranslatorCache is currently per-ContextGroup but |
| 122 // only needs to be per-thread. |
| 123 bool bind_generates_resource = false; |
| 124 scoped_refptr<gpu::gles2::ContextGroup> context_group = |
| 125 new gpu::gles2::ContextGroup( |
| 126 mailbox_manager_.get(), new MemoryTrackerStub, |
| 127 new gpu::gles2::ShaderTranslatorCache, nullptr, nullptr, nullptr, |
| 128 bind_generates_resource); |
| 129 |
| 130 command_buffer_.reset( |
| 131 new gpu::CommandBufferService(context_group->transfer_buffer_manager())); |
| 132 bool result = command_buffer_->Initialize(); |
| 133 DCHECK(result); |
| 134 |
| 135 decoder_.reset(::gpu::gles2::GLES2Decoder::Create(context_group.get())); |
| 136 scheduler_.reset(new gpu::GpuScheduler(command_buffer_.get(), decoder_.get(), |
| 137 decoder_.get())); |
| 138 decoder_->set_engine(scheduler_.get()); |
| 139 decoder_->SetResizeCallback( |
| 140 base::Bind(&CommandBufferDriver::OnResize, base::Unretained(this))); |
| 141 decoder_->SetWaitSyncPointCallback(base::Bind( |
| 142 &CommandBufferDriver::OnWaitSyncPoint, base::Unretained(this))); |
| 143 |
| 144 gpu::gles2::DisallowedFeatures disallowed_features; |
| 145 |
| 146 // TODO(piman): attributes. |
| 147 std::vector<int32> attrib_vector; |
| 148 if (!decoder_->Initialize(surface_, context_, false /* offscreen */, |
| 149 gfx::Size(1, 1), disallowed_features, |
| 150 attrib_vector)) |
| 151 return false; |
| 152 |
| 153 command_buffer_->SetPutOffsetChangeCallback(base::Bind( |
| 154 &gpu::GpuScheduler::PutChanged, base::Unretained(scheduler_.get()))); |
| 155 command_buffer_->SetGetBufferChangeCallback(base::Bind( |
| 156 &gpu::GpuScheduler::SetGetBuffer, base::Unretained(scheduler_.get()))); |
| 157 command_buffer_->SetParseErrorCallback( |
| 158 base::Bind(&CommandBufferDriver::OnParseError, base::Unretained(this))); |
| 159 |
| 160 // TODO(piman): other callbacks |
| 161 |
| 162 const size_t kSize = sizeof(gpu::CommandBufferSharedState); |
| 163 scoped_ptr<gpu::BufferBacking> backing( |
| 164 gles2::MojoBufferBacking::Create(shared_state.Pass(), kSize)); |
| 165 if (!backing) |
| 166 return false; |
| 167 |
| 168 command_buffer_->SetSharedStateBuffer(backing.Pass()); |
| 169 return true; |
| 170 } |
| 171 |
| 172 void CommandBufferDriver::SetGetBuffer(int32_t buffer) { |
| 173 command_buffer_->SetGetBuffer(buffer); |
| 174 } |
| 175 |
| 176 void CommandBufferDriver::Flush(int32_t put_offset) { |
| 177 if (!context_->MakeCurrent(surface_.get())) { |
| 178 DLOG(WARNING) << "Context lost"; |
| 179 OnContextLost(gpu::error::kUnknown); |
| 180 return; |
| 181 } |
| 182 command_buffer_->Flush(put_offset); |
| 183 } |
| 184 |
| 185 void CommandBufferDriver::MakeProgress(int32_t last_get_offset) { |
| 186 // TODO(piman): handle out-of-order. |
| 187 sync_client_->DidMakeProgress( |
| 188 mojo::CommandBufferState::From(command_buffer_->GetLastState())); |
| 189 } |
| 190 |
| 191 void CommandBufferDriver::RegisterTransferBuffer( |
| 192 int32_t id, |
| 193 mojo::ScopedSharedBufferHandle transfer_buffer, |
| 194 uint32_t size) { |
| 195 // Take ownership of the memory and map it into this process. |
| 196 // This validates the size. |
| 197 scoped_ptr<gpu::BufferBacking> backing( |
| 198 gles2::MojoBufferBacking::Create(transfer_buffer.Pass(), size)); |
| 199 if (!backing) { |
| 200 DVLOG(0) << "Failed to map shared memory."; |
| 201 return; |
| 202 } |
| 203 command_buffer_->RegisterTransferBuffer(id, backing.Pass()); |
| 204 } |
| 205 |
| 206 void CommandBufferDriver::DestroyTransferBuffer(int32_t id) { |
| 207 command_buffer_->DestroyTransferBuffer(id); |
| 208 } |
| 209 |
| 210 void CommandBufferDriver::Echo(const mojo::Callback<void()>& callback) { |
| 211 callback.Run(); |
| 212 } |
| 213 |
| 214 void CommandBufferDriver::OnParseError() { |
| 215 gpu::CommandBuffer::State state = command_buffer_->GetLastState(); |
| 216 OnContextLost(state.context_lost_reason); |
| 217 } |
| 218 |
| 219 void CommandBufferDriver::OnResize(gfx::Size size, float scale_factor) { |
| 220 surface_->Resize(size); |
| 221 } |
| 222 |
| 223 bool CommandBufferDriver::OnWaitSyncPoint(uint32_t sync_point) { |
| 224 if (!sync_point) |
| 225 return true; |
| 226 if (sync_point_manager_->IsSyncPointRetired(sync_point)) |
| 227 return true; |
| 228 scheduler_->SetScheduled(false); |
| 229 sync_point_manager_->AddSyncPointCallback( |
| 230 sync_point, base::Bind(&CommandBufferDriver::OnSyncPointRetired, |
| 231 weak_factory_.GetWeakPtr())); |
| 232 return scheduler_->IsScheduled(); |
| 233 } |
| 234 |
| 235 void CommandBufferDriver::OnSyncPointRetired() { |
| 236 scheduler_->SetScheduled(true); |
| 237 } |
| 238 |
| 239 void CommandBufferDriver::OnContextLost(uint32_t reason) { |
| 240 loss_observer_->DidLoseContext(reason); |
| 241 client_->DidLoseContext(); |
| 242 } |
| 243 |
| 244 void CommandBufferDriver::OnUpdateVSyncParameters( |
| 245 const base::TimeTicks timebase, |
| 246 const base::TimeDelta interval) { |
| 247 client_->UpdateVSyncParameters(timebase, interval); |
| 248 } |
| 249 |
| 250 } // namespace gles2 |
OLD | NEW |