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