1 // Copyright 2014 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_impl.h"
8 #include "base/message_loop/message_loop.h"
9 #include "components/mus/gles2/command_buffer_driver.h"
10 #include "components/mus/gles2/command_buffer_impl_observer.h"
11 #include "components/mus/gles2/gpu_state.h"
12 #include "gpu/command_buffer/service/sync_point_manager.h"
18 void RunCallback(const mojo::Callback
<void()>& callback
) {
24 class CommandBufferImpl::CommandBufferDriverClientImpl
25 : public CommandBufferDriver::Client
{
27 explicit CommandBufferDriverClientImpl(CommandBufferImpl
* command_buffer
)
28 : command_buffer_(command_buffer
) {}
31 void DidLoseContext() override
{ command_buffer_
->DidLoseContext(); }
33 CommandBufferImpl
* command_buffer_
;
35 DISALLOW_COPY_AND_ASSIGN(CommandBufferDriverClientImpl
);
38 CommandBufferImpl::CommandBufferImpl(
39 mojo::InterfaceRequest
<mojo::CommandBuffer
> request
,
40 scoped_refptr
<GpuState
> gpu_state
,
41 scoped_ptr
<CommandBufferDriver
> driver
)
42 : gpu_state_(gpu_state
),
43 driver_task_runner_(base::MessageLoop::current()->task_runner()),
44 driver_(driver
.Pass()),
47 weak_ptr_factory_(this) {
48 driver_
->set_client(make_scoped_ptr(new CommandBufferDriverClientImpl(this)));
50 gpu_state_
->control_task_runner()->PostTask(
52 base::Bind(&CommandBufferImpl::BindToRequest
,
53 weak_ptr_factory_
.GetWeakPtr(), base::Passed(&request
)));
56 void CommandBufferImpl::Initialize(
57 mojo::CommandBufferSyncClientPtr sync_client
,
58 mojo::CommandBufferSyncPointClientPtr sync_point_client
,
59 mojo::CommandBufferLostContextObserverPtr loss_observer
,
60 mojo::ScopedSharedBufferHandle shared_state
,
61 mojo::Array
<int32_t> attribs
) {
62 sync_point_client_
= sync_point_client
.Pass();
63 driver_task_runner_
->PostTask(
65 base::Bind(&CommandBufferDriver::Initialize
,
66 base::Unretained(driver_
.get()), base::Passed(&sync_client
),
67 base::Passed(&loss_observer
), base::Passed(&shared_state
),
68 base::Passed(&attribs
)));
71 void CommandBufferImpl::SetGetBuffer(int32_t buffer
) {
72 driver_task_runner_
->PostTask(
73 FROM_HERE
, base::Bind(&CommandBufferDriver::SetGetBuffer
,
74 base::Unretained(driver_
.get()), buffer
));
77 void CommandBufferImpl::Flush(int32_t put_offset
) {
78 driver_task_runner_
->PostTask(
79 FROM_HERE
, base::Bind(&CommandBufferDriver::Flush
,
80 base::Unretained(driver_
.get()), put_offset
));
83 void CommandBufferImpl::MakeProgress(int32_t last_get_offset
) {
84 driver_task_runner_
->PostTask(
85 FROM_HERE
, base::Bind(&CommandBufferDriver::MakeProgress
,
86 base::Unretained(driver_
.get()), last_get_offset
));
89 void CommandBufferImpl::RegisterTransferBuffer(
91 mojo::ScopedSharedBufferHandle transfer_buffer
,
93 driver_task_runner_
->PostTask(
94 FROM_HERE
, base::Bind(&CommandBufferDriver::RegisterTransferBuffer
,
95 base::Unretained(driver_
.get()), id
,
96 base::Passed(&transfer_buffer
), size
));
99 void CommandBufferImpl::DestroyTransferBuffer(int32_t id
) {
100 driver_task_runner_
->PostTask(
101 FROM_HERE
, base::Bind(&CommandBufferDriver::DestroyTransferBuffer
,
102 base::Unretained(driver_
.get()), id
));
105 void CommandBufferImpl::InsertSyncPoint(bool retire
) {
106 uint32_t sync_point
= gpu_state_
->sync_point_manager()->GenerateSyncPoint();
107 sync_point_client_
->DidInsertSyncPoint(sync_point
);
109 driver_task_runner_
->PostTask(
111 base::Bind(&gpu::SyncPointManager::RetireSyncPoint
,
112 base::Unretained(gpu_state_
->sync_point_manager()),
117 void CommandBufferImpl::RetireSyncPoint(uint32_t sync_point
) {
118 driver_task_runner_
->PostTask(
119 FROM_HERE
, base::Bind(&gpu::SyncPointManager::RetireSyncPoint
,
120 base::Unretained(gpu_state_
->sync_point_manager()),
124 void CommandBufferImpl::Echo(const mojo::Callback
<void()>& callback
) {
125 driver_task_runner_
->PostTaskAndReply(FROM_HERE
, base::Bind(&base::DoNothing
),
126 base::Bind(&RunCallback
, callback
));
129 void CommandBufferImpl::CreateImage(int32_t id
,
130 mojo::ScopedHandle memory_handle
,
134 int32_t internal_format
) {
135 driver_task_runner_
->PostTask(
136 FROM_HERE
, base::Bind(&CommandBufferDriver::CreateImage
,
137 base::Unretained(driver_
.get()), id
,
138 base::Passed(&memory_handle
), type
,
139 base::Passed(&size
), format
, internal_format
));
142 void CommandBufferImpl::DestroyImage(int32_t id
) {
143 driver_task_runner_
->PostTask(
144 FROM_HERE
, base::Bind(&CommandBufferDriver::DestroyImage
,
145 base::Unretained(driver_
.get()), id
));
148 CommandBufferImpl::~CommandBufferImpl() {
150 observer_
->OnCommandBufferImplDestroyed();
153 void CommandBufferImpl::BindToRequest(
154 mojo::InterfaceRequest
<mojo::CommandBuffer
> request
) {
155 binding_
.Bind(request
.Pass());
156 binding_
.set_connection_error_handler([this]() { OnConnectionError(); });
159 void CommandBufferImpl::OnConnectionError() {
160 // OnConnectionError() is called on the control thread |control_task_runner|.
161 // sync_point_client_ is assigned and accessed on the control thread and so it
162 // should also be destroyed on the control because InterfacePtrs are thread-
164 sync_point_client_
.reset();
166 // Objects we own (such as CommandBufferDriver) need to be destroyed on the
167 // thread we were created on.
168 driver_task_runner_
->DeleteSoon(FROM_HERE
, this);
171 void CommandBufferImpl::DidLoseContext() {