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/view_manager/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/view_manager/gles2/command_buffer_type_conversions.h" | |
11 #include "components/view_manager/gles2/gpu_memory_tracker.h" | |
12 #include "components/view_manager/gles2/gpu_state.h" | |
13 #include "components/view_manager/gles2/mojo_buffer_backing.h" | |
14 #include "gpu/command_buffer/common/constants.h" | |
15 #include "gpu/command_buffer/common/value_state.h" | |
16 #include "gpu/command_buffer/service/command_buffer_service.h" | |
17 #include "gpu/command_buffer/service/context_group.h" | |
18 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" | |
19 #include "gpu/command_buffer/service/gpu_scheduler.h" | |
20 #include "gpu/command_buffer/service/image_factory.h" | |
21 #include "gpu/command_buffer/service/image_manager.h" | |
22 #include "gpu/command_buffer/service/mailbox_manager.h" | |
23 #include "gpu/command_buffer/service/memory_tracking.h" | |
24 #include "gpu/command_buffer/service/sync_point_manager.h" | |
25 #include "gpu/command_buffer/service/transfer_buffer_manager.h" | |
26 #include "gpu/command_buffer/service/valuebuffer_manager.h" | |
27 #include "mojo/converters/geometry/geometry_type_converters.h" | |
28 #include "mojo/platform_handle/platform_handle_functions.h" | |
29 #include "ui/gfx/gpu_memory_buffer.h" | |
30 #include "ui/gfx/vsync_provider.h" | |
31 #include "ui/gl/gl_context.h" | |
32 #include "ui/gl/gl_image_shared_memory.h" | |
33 #include "ui/gl/gl_surface.h" | |
34 | |
35 namespace gles2 { | |
36 | |
37 CommandBufferDriver::Client::~Client() { | |
38 } | |
39 | |
40 CommandBufferDriver::CommandBufferDriver(scoped_refptr<GpuState> gpu_state) | |
41 : client_(nullptr), | |
42 gpu_state_(gpu_state), | |
43 weak_factory_(this) { | |
44 } | |
45 | |
46 CommandBufferDriver::~CommandBufferDriver() { | |
47 DestroyDecoder(); | |
48 } | |
49 | |
50 void CommandBufferDriver::Initialize( | |
51 mojo::CommandBufferSyncClientPtr sync_client, | |
52 mojo::CommandBufferLostContextObserverPtr loss_observer, | |
53 mojo::ScopedSharedBufferHandle shared_state) { | |
54 sync_client_ = sync_client.Pass(); | |
55 loss_observer_ = loss_observer.Pass(); | |
56 bool success = DoInitialize(shared_state.Pass()); | |
57 mojo::GpuCapabilitiesPtr capabilities = | |
58 success ? mojo::GpuCapabilities::From(decoder_->GetCapabilities()) | |
59 : nullptr; | |
60 sync_client_->DidInitialize(success, capabilities.Pass()); | |
61 } | |
62 | |
63 bool CommandBufferDriver::MakeCurrent() { | |
64 if (!decoder_) | |
65 return false; | |
66 if (decoder_->MakeCurrent()) | |
67 return true; | |
68 DLOG(ERROR) << "Context lost because MakeCurrent failed."; | |
69 gpu::error::ContextLostReason reason = | |
70 static_cast<gpu::error::ContextLostReason>( | |
71 decoder_->GetContextLostReason()); | |
72 command_buffer_->SetContextLostReason(reason); | |
73 command_buffer_->SetParseError(gpu::error::kLostContext); | |
74 OnContextLost(reason); | |
75 return false; | |
76 } | |
77 | |
78 bool CommandBufferDriver::DoInitialize( | |
79 mojo::ScopedSharedBufferHandle shared_state) { | |
80 surface_ = gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size(1, 1)); | |
81 | |
82 if (!surface_.get()) | |
83 return false; | |
84 | |
85 // TODO(piman): virtual contexts, gpu preference. | |
86 context_ = gfx::GLContext::CreateGLContext( | |
87 gpu_state_->share_group(), surface_.get(), gfx::PreferIntegratedGpu); | |
88 if (!context_.get()) | |
89 return false; | |
90 | |
91 if (!context_->MakeCurrent(surface_.get())) | |
92 return false; | |
93 | |
94 // TODO(piman): ShaderTranslatorCache is currently per-ContextGroup but | |
95 // only needs to be per-thread. | |
96 bool bind_generates_resource = false; | |
97 scoped_refptr<gpu::gles2::ContextGroup> context_group = | |
98 new gpu::gles2::ContextGroup( | |
99 gpu_state_->mailbox_manager(), new GpuMemoryTracker, | |
100 new gpu::gles2::ShaderTranslatorCache, | |
101 new gpu::gles2::FramebufferCompletenessCache, nullptr, nullptr, | |
102 nullptr, bind_generates_resource); | |
103 | |
104 command_buffer_.reset( | |
105 new gpu::CommandBufferService(context_group->transfer_buffer_manager())); | |
106 bool result = command_buffer_->Initialize(); | |
107 DCHECK(result); | |
108 | |
109 decoder_.reset(::gpu::gles2::GLES2Decoder::Create(context_group.get())); | |
110 scheduler_.reset(new gpu::GpuScheduler(command_buffer_.get(), decoder_.get(), | |
111 decoder_.get())); | |
112 decoder_->set_engine(scheduler_.get()); | |
113 decoder_->SetResizeCallback( | |
114 base::Bind(&CommandBufferDriver::OnResize, base::Unretained(this))); | |
115 decoder_->SetWaitSyncPointCallback(base::Bind( | |
116 &CommandBufferDriver::OnWaitSyncPoint, base::Unretained(this))); | |
117 | |
118 gpu::gles2::DisallowedFeatures disallowed_features; | |
119 | |
120 // TODO(piman): attributes. | |
121 std::vector<int32> attrib_vector; | |
122 if (!decoder_->Initialize(surface_, context_, false /* offscreen */, | |
123 gfx::Size(1, 1), disallowed_features, | |
124 attrib_vector)) | |
125 return false; | |
126 | |
127 command_buffer_->SetPutOffsetChangeCallback(base::Bind( | |
128 &gpu::GpuScheduler::PutChanged, base::Unretained(scheduler_.get()))); | |
129 command_buffer_->SetGetBufferChangeCallback(base::Bind( | |
130 &gpu::GpuScheduler::SetGetBuffer, base::Unretained(scheduler_.get()))); | |
131 command_buffer_->SetParseErrorCallback( | |
132 base::Bind(&CommandBufferDriver::OnParseError, base::Unretained(this))); | |
133 | |
134 // TODO(piman): other callbacks | |
135 | |
136 const size_t kSize = sizeof(gpu::CommandBufferSharedState); | |
137 scoped_ptr<gpu::BufferBacking> backing( | |
138 gles2::MojoBufferBacking::Create(shared_state.Pass(), kSize)); | |
139 if (!backing) | |
140 return false; | |
141 | |
142 command_buffer_->SetSharedStateBuffer(backing.Pass()); | |
143 return true; | |
144 } | |
145 | |
146 void CommandBufferDriver::SetGetBuffer(int32_t buffer) { | |
147 command_buffer_->SetGetBuffer(buffer); | |
148 } | |
149 | |
150 void CommandBufferDriver::Flush(int32_t put_offset) { | |
151 if (!context_) | |
152 return; | |
153 if (!context_->MakeCurrent(surface_.get())) { | |
154 DLOG(WARNING) << "Context lost"; | |
155 OnContextLost(gpu::error::kUnknown); | |
156 return; | |
157 } | |
158 command_buffer_->Flush(put_offset); | |
159 } | |
160 | |
161 void CommandBufferDriver::MakeProgress(int32_t last_get_offset) { | |
162 // TODO(piman): handle out-of-order. | |
163 sync_client_->DidMakeProgress( | |
164 mojo::CommandBufferState::From(command_buffer_->GetLastState())); | |
165 } | |
166 | |
167 void CommandBufferDriver::RegisterTransferBuffer( | |
168 int32_t id, | |
169 mojo::ScopedSharedBufferHandle transfer_buffer, | |
170 uint32_t size) { | |
171 // Take ownership of the memory and map it into this process. | |
172 // This validates the size. | |
173 scoped_ptr<gpu::BufferBacking> backing( | |
174 gles2::MojoBufferBacking::Create(transfer_buffer.Pass(), size)); | |
175 if (!backing) { | |
176 DVLOG(0) << "Failed to map shared memory."; | |
177 return; | |
178 } | |
179 command_buffer_->RegisterTransferBuffer(id, backing.Pass()); | |
180 } | |
181 | |
182 void CommandBufferDriver::DestroyTransferBuffer(int32_t id) { | |
183 command_buffer_->DestroyTransferBuffer(id); | |
184 } | |
185 | |
186 void CommandBufferDriver::Echo(const mojo::Callback<void()>& callback) { | |
187 callback.Run(); | |
188 } | |
189 | |
190 void CommandBufferDriver::CreateImage(int32_t id, | |
191 mojo::ScopedHandle memory_handle, | |
192 int32 type, | |
193 mojo::SizePtr size, | |
194 int32_t format, | |
195 int32_t internal_format) { | |
196 if (!MakeCurrent()) | |
197 return; | |
198 | |
199 gpu::gles2::ImageManager* image_manager = decoder_->GetImageManager(); | |
200 if (image_manager->LookupImage(id)) { | |
201 LOG(ERROR) << "Image already exists with same ID."; | |
202 return; | |
203 } | |
204 | |
205 gfx::BufferFormat gpu_format = static_cast<gfx::BufferFormat>(format); | |
206 if (!gpu::ImageFactory::IsGpuMemoryBufferFormatSupported( | |
207 gpu_format, decoder_->GetCapabilities())) { | |
208 LOG(ERROR) << "Format is not supported."; | |
209 return; | |
210 } | |
211 | |
212 gfx::Size gfx_size = size.To<gfx::Size>(); | |
213 if (!gpu::ImageFactory::IsImageSizeValidForGpuMemoryBufferFormat( | |
214 gfx_size, gpu_format)) { | |
215 LOG(ERROR) << "Invalid image size for format."; | |
216 return; | |
217 } | |
218 | |
219 if (!gpu::ImageFactory::IsImageFormatCompatibleWithGpuMemoryBufferFormat( | |
220 internal_format, gpu_format)) { | |
221 LOG(ERROR) << "Incompatible image format."; | |
222 return; | |
223 } | |
224 | |
225 if (type != gfx::SHARED_MEMORY_BUFFER) { | |
226 NOTIMPLEMENTED(); | |
227 return; | |
228 } | |
229 | |
230 gfx::GpuMemoryBufferHandle gfx_handle; | |
231 // TODO(jam): create mojo enum for this and converter | |
232 gfx_handle.type = static_cast<gfx::GpuMemoryBufferType>(type); | |
233 gfx_handle.id = gfx::GpuMemoryBufferId(id); | |
234 | |
235 MojoPlatformHandle platform_handle; | |
236 MojoResult extract_result = MojoExtractPlatformHandle( | |
237 memory_handle.release().value(), | |
238 &platform_handle); | |
239 if (extract_result != MOJO_RESULT_OK) { | |
240 NOTREACHED(); | |
241 return; | |
242 } | |
243 | |
244 #if defined(OS_WIN) | |
245 gfx_handle.handle = platform_handle; | |
246 #else | |
247 gfx_handle.handle = base::FileDescriptor(platform_handle, false); | |
248 #endif | |
249 | |
250 scoped_refptr<gfx::GLImageSharedMemory> image = | |
251 new gfx::GLImageSharedMemory(gfx_size, internal_format); | |
252 // TODO(jam): also need a mojo enum for this enum | |
253 if (!image->Initialize(gfx_handle, gpu_format)) { | |
254 NOTREACHED(); | |
255 return; | |
256 } | |
257 | |
258 image_manager->AddImage(image.get(), id); | |
259 } | |
260 | |
261 void CommandBufferDriver::DestroyImage(int32_t id) { | |
262 gpu::gles2::ImageManager* image_manager = decoder_->GetImageManager(); | |
263 if (!image_manager->LookupImage(id)) { | |
264 LOG(ERROR) << "Image with ID doesn't exist."; | |
265 return; | |
266 } | |
267 if (!MakeCurrent()) | |
268 return; | |
269 image_manager->RemoveImage(id); | |
270 } | |
271 | |
272 void CommandBufferDriver::OnParseError() { | |
273 gpu::CommandBuffer::State state = command_buffer_->GetLastState(); | |
274 OnContextLost(state.context_lost_reason); | |
275 } | |
276 | |
277 void CommandBufferDriver::OnResize(gfx::Size size, float scale_factor) { | |
278 surface_->Resize(size); | |
279 } | |
280 | |
281 bool CommandBufferDriver::OnWaitSyncPoint(uint32_t sync_point) { | |
282 if (!sync_point) | |
283 return true; | |
284 if (gpu_state_->sync_point_manager()->IsSyncPointRetired(sync_point)) | |
285 return true; | |
286 scheduler_->SetScheduled(false); | |
287 gpu_state_->sync_point_manager()->AddSyncPointCallback( | |
288 sync_point, base::Bind(&CommandBufferDriver::OnSyncPointRetired, | |
289 weak_factory_.GetWeakPtr())); | |
290 return scheduler_->IsScheduled(); | |
291 } | |
292 | |
293 void CommandBufferDriver::OnSyncPointRetired() { | |
294 scheduler_->SetScheduled(true); | |
295 } | |
296 | |
297 void CommandBufferDriver::OnContextLost(uint32_t reason) { | |
298 loss_observer_->DidLoseContext(reason); | |
299 client_->DidLoseContext(); | |
300 } | |
301 | |
302 void CommandBufferDriver::DestroyDecoder() { | |
303 if (decoder_) { | |
304 bool have_context = decoder_->MakeCurrent(); | |
305 decoder_->Destroy(have_context); | |
306 decoder_.reset(); | |
307 } | |
308 } | |
309 | |
310 } // namespace gles2 | |
OLD | NEW |