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.
5 #include "components/mus/gles2/command_buffer_driver.h"
8 #include "base/macros.h"
9 #include "base/memory/shared_memory.h"
10 #include "components/mus/gles2/command_buffer_type_conversions.h"
11 #include "components/mus/gles2/gpu_memory_tracker.h"
12 #include "components/mus/gles2/gpu_state.h"
13 #include "components/mus/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"
37 CommandBufferDriver::Client::~Client() {}
39 CommandBufferDriver::CommandBufferDriver(scoped_refptr
<GpuState
> gpu_state
)
40 : client_(nullptr), gpu_state_(gpu_state
), weak_factory_(this) {}
42 CommandBufferDriver::~CommandBufferDriver() {
46 void CommandBufferDriver::Initialize(
47 mojo::CommandBufferSyncClientPtr sync_client
,
48 mojo::CommandBufferLostContextObserverPtr loss_observer
,
49 mojo::ScopedSharedBufferHandle shared_state
,
50 mojo::Array
<int32_t> attribs
) {
51 sync_client_
= sync_client
.Pass();
52 loss_observer_
= loss_observer
.Pass();
53 bool success
= DoInitialize(shared_state
.Pass(), attribs
.Pass());
54 mojo::GpuCapabilitiesPtr capabilities
=
55 success
? mojo::GpuCapabilities::From(decoder_
->GetCapabilities())
57 sync_client_
->DidInitialize(success
, capabilities
.Pass());
60 bool CommandBufferDriver::MakeCurrent() {
63 if (decoder_
->MakeCurrent())
65 DLOG(ERROR
) << "Context lost because MakeCurrent failed.";
66 gpu::error::ContextLostReason reason
=
67 static_cast<gpu::error::ContextLostReason
>(
68 decoder_
->GetContextLostReason());
69 command_buffer_
->SetContextLostReason(reason
);
70 command_buffer_
->SetParseError(gpu::error::kLostContext
);
71 OnContextLost(reason
);
75 bool CommandBufferDriver::DoInitialize(
76 mojo::ScopedSharedBufferHandle shared_state
,
77 mojo::Array
<int32_t> attribs
) {
78 gpu::gles2::ContextCreationAttribHelper attrib_helper
;
79 if (!attrib_helper
.Parse(attribs
.storage()))
82 surface_
= gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size(1, 1));
87 // TODO(piman): virtual contexts, gpu preference.
88 context_
= gfx::GLContext::CreateGLContext(
89 gpu_state_
->share_group(), surface_
.get(), gfx::PreferIntegratedGpu
);
93 if (!context_
->MakeCurrent(surface_
.get()))
96 // TODO(piman): ShaderTranslatorCache is currently per-ContextGroup but
97 // only needs to be per-thread.
98 const bool bind_generates_resource
= attrib_helper
.bind_generates_resource
;
99 scoped_refptr
<gpu::gles2::ContextGroup
> context_group
=
100 new gpu::gles2::ContextGroup(
101 gpu_state_
->mailbox_manager(), new GpuMemoryTracker
,
102 new gpu::gles2::ShaderTranslatorCache
,
103 new gpu::gles2::FramebufferCompletenessCache
, nullptr, nullptr,
104 nullptr, bind_generates_resource
);
106 command_buffer_
.reset(
107 new gpu::CommandBufferService(context_group
->transfer_buffer_manager()));
108 bool result
= command_buffer_
->Initialize();
111 decoder_
.reset(::gpu::gles2::GLES2Decoder::Create(context_group
.get()));
112 scheduler_
.reset(new gpu::GpuScheduler(command_buffer_
.get(), decoder_
.get(),
114 decoder_
->set_engine(scheduler_
.get());
115 decoder_
->SetResizeCallback(
116 base::Bind(&CommandBufferDriver::OnResize
, base::Unretained(this)));
117 decoder_
->SetWaitSyncPointCallback(base::Bind(
118 &CommandBufferDriver::OnWaitSyncPoint
, base::Unretained(this)));
120 gpu::gles2::DisallowedFeatures disallowed_features
;
122 const bool offscreen
= true;
123 std::vector
<int32
> attrib_vector
;
124 attrib_helper
.Serialize(&attrib_vector
);
125 if (!decoder_
->Initialize(surface_
, context_
, offscreen
, gfx::Size(1, 1),
126 disallowed_features
, attrib_vector
))
129 command_buffer_
->SetPutOffsetChangeCallback(base::Bind(
130 &gpu::GpuScheduler::PutChanged
, base::Unretained(scheduler_
.get())));
131 command_buffer_
->SetGetBufferChangeCallback(base::Bind(
132 &gpu::GpuScheduler::SetGetBuffer
, base::Unretained(scheduler_
.get())));
133 command_buffer_
->SetParseErrorCallback(
134 base::Bind(&CommandBufferDriver::OnParseError
, base::Unretained(this)));
136 // TODO(piman): other callbacks
138 const size_t kSize
= sizeof(gpu::CommandBufferSharedState
);
139 scoped_ptr
<gpu::BufferBacking
> backing(
140 MojoBufferBacking::Create(shared_state
.Pass(), kSize
));
144 command_buffer_
->SetSharedStateBuffer(backing
.Pass());
148 void CommandBufferDriver::SetGetBuffer(int32_t buffer
) {
149 command_buffer_
->SetGetBuffer(buffer
);
152 void CommandBufferDriver::Flush(int32_t put_offset
) {
155 if (!context_
->MakeCurrent(surface_
.get())) {
156 DLOG(WARNING
) << "Context lost";
157 OnContextLost(gpu::error::kUnknown
);
160 command_buffer_
->Flush(put_offset
);
163 void CommandBufferDriver::MakeProgress(int32_t last_get_offset
) {
164 // TODO(piman): handle out-of-order.
165 sync_client_
->DidMakeProgress(
166 mojo::CommandBufferState::From(command_buffer_
->GetLastState()));
169 void CommandBufferDriver::RegisterTransferBuffer(
171 mojo::ScopedSharedBufferHandle transfer_buffer
,
173 // Take ownership of the memory and map it into this process.
174 // This validates the size.
175 scoped_ptr
<gpu::BufferBacking
> backing(
176 MojoBufferBacking::Create(transfer_buffer
.Pass(), size
));
178 DVLOG(0) << "Failed to map shared memory.";
181 command_buffer_
->RegisterTransferBuffer(id
, backing
.Pass());
184 void CommandBufferDriver::DestroyTransferBuffer(int32_t id
) {
185 command_buffer_
->DestroyTransferBuffer(id
);
188 void CommandBufferDriver::Echo(const mojo::Callback
<void()>& callback
) {
192 void CommandBufferDriver::CreateImage(int32_t id
,
193 mojo::ScopedHandle memory_handle
,
197 int32_t internal_format
) {
201 gpu::gles2::ImageManager
* image_manager
= decoder_
->GetImageManager();
202 if (image_manager
->LookupImage(id
)) {
203 LOG(ERROR
) << "Image already exists with same ID.";
207 gfx::BufferFormat gpu_format
= static_cast<gfx::BufferFormat
>(format
);
208 if (!gpu::ImageFactory::IsGpuMemoryBufferFormatSupported(
209 gpu_format
, decoder_
->GetCapabilities())) {
210 LOG(ERROR
) << "Format is not supported.";
214 gfx::Size gfx_size
= size
.To
<gfx::Size
>();
215 if (!gpu::ImageFactory::IsImageSizeValidForGpuMemoryBufferFormat(
216 gfx_size
, gpu_format
)) {
217 LOG(ERROR
) << "Invalid image size for format.";
221 if (!gpu::ImageFactory::IsImageFormatCompatibleWithGpuMemoryBufferFormat(
222 internal_format
, gpu_format
)) {
223 LOG(ERROR
) << "Incompatible image format.";
227 if (type
!= gfx::SHARED_MEMORY_BUFFER
) {
232 gfx::GpuMemoryBufferHandle gfx_handle
;
233 // TODO(jam): create mojo enum for this and converter
234 gfx_handle
.type
= static_cast<gfx::GpuMemoryBufferType
>(type
);
235 gfx_handle
.id
= gfx::GpuMemoryBufferId(id
);
237 MojoPlatformHandle platform_handle
;
238 MojoResult extract_result
= MojoExtractPlatformHandle(
239 memory_handle
.release().value(), &platform_handle
);
240 if (extract_result
!= MOJO_RESULT_OK
) {
246 gfx_handle
.handle
= platform_handle
;
248 gfx_handle
.handle
= base::FileDescriptor(platform_handle
, false);
251 scoped_refptr
<gfx::GLImageSharedMemory
> image
=
252 new gfx::GLImageSharedMemory(gfx_size
, internal_format
);
253 // TODO(jam): also need a mojo enum for this enum
254 if (!image
->Initialize(gfx_handle
, gpu_format
)) {
259 image_manager
->AddImage(image
.get(), id
);
262 void CommandBufferDriver::DestroyImage(int32_t id
) {
263 gpu::gles2::ImageManager
* image_manager
= decoder_
->GetImageManager();
264 if (!image_manager
->LookupImage(id
)) {
265 LOG(ERROR
) << "Image with ID doesn't exist.";
270 image_manager
->RemoveImage(id
);
273 void CommandBufferDriver::OnParseError() {
274 gpu::CommandBuffer::State state
= command_buffer_
->GetLastState();
275 OnContextLost(state
.context_lost_reason
);
278 void CommandBufferDriver::OnResize(gfx::Size size
, float scale_factor
) {
279 surface_
->Resize(size
);
282 bool CommandBufferDriver::OnWaitSyncPoint(uint32_t sync_point
) {
285 if (gpu_state_
->sync_point_manager()->IsSyncPointRetired(sync_point
))
287 scheduler_
->SetScheduled(false);
288 gpu_state_
->sync_point_manager()->AddSyncPointCallback(
289 sync_point
, base::Bind(&CommandBufferDriver::OnSyncPointRetired
,
290 weak_factory_
.GetWeakPtr()));
291 return scheduler_
->scheduled();
294 void CommandBufferDriver::OnSyncPointRetired() {
295 scheduler_
->SetScheduled(true);
298 void CommandBufferDriver::OnContextLost(uint32_t reason
) {
299 loss_observer_
->DidLoseContext(reason
);
300 client_
->DidLoseContext();
303 void CommandBufferDriver::DestroyDecoder() {
305 bool have_context
= decoder_
->MakeCurrent();
306 decoder_
->Destroy(have_context
);