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/view_manager/gles2/command_buffer_driver.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"
37 CommandBufferDriver::Client::~Client() {
40 CommandBufferDriver::CommandBufferDriver(scoped_refptr
<GpuState
> gpu_state
)
42 gpu_state_(gpu_state
),
46 CommandBufferDriver::~CommandBufferDriver() {
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())
60 sync_client_
->DidInitialize(success
, capabilities
.Pass());
63 bool CommandBufferDriver::MakeCurrent() {
66 if (decoder_
->MakeCurrent())
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
);
78 bool CommandBufferDriver::DoInitialize(
79 mojo::ScopedSharedBufferHandle shared_state
) {
80 surface_
= gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size(1, 1));
85 // TODO(piman): virtual contexts, gpu preference.
86 context_
= gfx::GLContext::CreateGLContext(
87 gpu_state_
->share_group(), surface_
.get(), gfx::PreferIntegratedGpu
);
91 if (!context_
->MakeCurrent(surface_
.get()))
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
);
104 command_buffer_
.reset(
105 new gpu::CommandBufferService(context_group
->transfer_buffer_manager()));
106 bool result
= command_buffer_
->Initialize();
109 decoder_
.reset(::gpu::gles2::GLES2Decoder::Create(context_group
.get()));
110 scheduler_
.reset(new gpu::GpuScheduler(command_buffer_
.get(), 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)));
118 gpu::gles2::DisallowedFeatures disallowed_features
;
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
,
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)));
134 // TODO(piman): other callbacks
136 const size_t kSize
= sizeof(gpu::CommandBufferSharedState
);
137 scoped_ptr
<gpu::BufferBacking
> backing(
138 gles2::MojoBufferBacking::Create(shared_state
.Pass(), kSize
));
142 command_buffer_
->SetSharedStateBuffer(backing
.Pass());
146 void CommandBufferDriver::SetGetBuffer(int32_t buffer
) {
147 command_buffer_
->SetGetBuffer(buffer
);
150 void CommandBufferDriver::Flush(int32_t put_offset
) {
153 if (!context_
->MakeCurrent(surface_
.get())) {
154 DLOG(WARNING
) << "Context lost";
155 OnContextLost(gpu::error::kUnknown
);
158 command_buffer_
->Flush(put_offset
);
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()));
167 void CommandBufferDriver::RegisterTransferBuffer(
169 mojo::ScopedSharedBufferHandle transfer_buffer
,
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
));
176 DVLOG(0) << "Failed to map shared memory.";
179 command_buffer_
->RegisterTransferBuffer(id
, backing
.Pass());
182 void CommandBufferDriver::DestroyTransferBuffer(int32_t id
) {
183 command_buffer_
->DestroyTransferBuffer(id
);
186 void CommandBufferDriver::Echo(const mojo::Callback
<void()>& callback
) {
190 void CommandBufferDriver::CreateImage(int32_t id
,
191 mojo::ScopedHandle memory_handle
,
195 int32_t internal_format
) {
199 gpu::gles2::ImageManager
* image_manager
= decoder_
->GetImageManager();
200 if (image_manager
->LookupImage(id
)) {
201 LOG(ERROR
) << "Image already exists with same ID.";
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.";
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.";
219 if (!gpu::ImageFactory::IsImageFormatCompatibleWithGpuMemoryBufferFormat(
220 internal_format
, gpu_format
)) {
221 LOG(ERROR
) << "Incompatible image format.";
225 if (type
!= gfx::SHARED_MEMORY_BUFFER
) {
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
);
235 MojoPlatformHandle platform_handle
;
236 MojoResult extract_result
= MojoExtractPlatformHandle(
237 memory_handle
.release().value(),
239 if (extract_result
!= MOJO_RESULT_OK
) {
245 gfx_handle
.handle
= platform_handle
;
247 gfx_handle
.handle
= base::FileDescriptor(platform_handle
, false);
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
)) {
258 image_manager
->AddImage(image
.get(), id
);
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.";
269 image_manager
->RemoveImage(id
);
272 void CommandBufferDriver::OnParseError() {
273 gpu::CommandBuffer::State state
= command_buffer_
->GetLastState();
274 OnContextLost(state
.context_lost_reason
);
277 void CommandBufferDriver::OnResize(gfx::Size size
, float scale_factor
) {
278 surface_
->Resize(size
);
281 bool CommandBufferDriver::OnWaitSyncPoint(uint32_t sync_point
) {
284 if (gpu_state_
->sync_point_manager()->IsSyncPointRetired(sync_point
))
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();
293 void CommandBufferDriver::OnSyncPointRetired() {
294 scheduler_
->SetScheduled(true);
297 void CommandBufferDriver::OnContextLost(uint32_t reason
) {
298 loss_observer_
->DidLoseContext(reason
);
299 client_
->DidLoseContext();
302 void CommandBufferDriver::DestroyDecoder() {
304 bool have_context
= decoder_
->MakeCurrent();
305 decoder_
->Destroy(have_context
);