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/view_manager/gles2/command_buffer_impl.h"
8 #include "base/message_loop/message_loop.h"
9 #include "components/view_manager/gles2/command_buffer_driver.h"
10 #include "components/view_manager/gles2/command_buffer_impl_observer.h"
11 #include "components/view_manager/gles2/gpu_state.h"
12 #include "gpu/command_buffer/service/sync_point_manager.h"
16 void RunCallback(const mojo::Callback
<void()>& callback
) {
21 class CommandBufferImpl::CommandBufferDriverClientImpl
22 : public CommandBufferDriver::Client
{
24 explicit CommandBufferDriverClientImpl(CommandBufferImpl
* command_buffer
)
25 : command_buffer_(command_buffer
) {}
28 void DidLoseContext() override
{ command_buffer_
->DidLoseContext(); }
30 CommandBufferImpl
* command_buffer_
;
32 DISALLOW_COPY_AND_ASSIGN(CommandBufferDriverClientImpl
);
35 CommandBufferImpl::CommandBufferImpl(
36 mojo::InterfaceRequest
<mojo::CommandBuffer
> request
,
37 scoped_refptr
<GpuState
> gpu_state
,
38 scoped_ptr
<CommandBufferDriver
> driver
)
39 : gpu_state_(gpu_state
),
40 driver_task_runner_(base::MessageLoop::current()->task_runner()),
41 driver_(driver
.Pass()),
44 weak_ptr_factory_(this) {
45 driver_
->set_client(make_scoped_ptr(new CommandBufferDriverClientImpl(this)));
47 gpu_state_
->control_task_runner()->PostTask(
49 base::Bind(&CommandBufferImpl::BindToRequest
,
50 weak_ptr_factory_
.GetWeakPtr(), base::Passed(&request
)));
53 void CommandBufferImpl::Initialize(
54 mojo::CommandBufferSyncClientPtr sync_client
,
55 mojo::CommandBufferSyncPointClientPtr sync_point_client
,
56 mojo::CommandBufferLostContextObserverPtr loss_observer
,
57 mojo::ScopedSharedBufferHandle shared_state
) {
58 sync_point_client_
= sync_point_client
.Pass();
59 driver_task_runner_
->PostTask(
61 base::Bind(&CommandBufferDriver::Initialize
,
62 base::Unretained(driver_
.get()), base::Passed(&sync_client
),
63 base::Passed(&loss_observer
),
64 base::Passed(&shared_state
)));
67 void CommandBufferImpl::SetGetBuffer(int32_t buffer
) {
68 driver_task_runner_
->PostTask(
69 FROM_HERE
, base::Bind(&CommandBufferDriver::SetGetBuffer
,
70 base::Unretained(driver_
.get()), buffer
));
73 void CommandBufferImpl::Flush(int32_t put_offset
) {
74 driver_task_runner_
->PostTask(
75 FROM_HERE
, base::Bind(&CommandBufferDriver::Flush
,
76 base::Unretained(driver_
.get()), put_offset
));
79 void CommandBufferImpl::MakeProgress(int32_t last_get_offset
) {
80 driver_task_runner_
->PostTask(
81 FROM_HERE
, base::Bind(&CommandBufferDriver::MakeProgress
,
82 base::Unretained(driver_
.get()), last_get_offset
));
85 void CommandBufferImpl::RegisterTransferBuffer(
87 mojo::ScopedSharedBufferHandle transfer_buffer
,
89 driver_task_runner_
->PostTask(
90 FROM_HERE
, base::Bind(&CommandBufferDriver::RegisterTransferBuffer
,
91 base::Unretained(driver_
.get()), id
,
92 base::Passed(&transfer_buffer
), size
));
95 void CommandBufferImpl::DestroyTransferBuffer(int32_t id
) {
96 driver_task_runner_
->PostTask(
97 FROM_HERE
, base::Bind(&CommandBufferDriver::DestroyTransferBuffer
,
98 base::Unretained(driver_
.get()), id
));
101 void CommandBufferImpl::InsertSyncPoint(bool retire
) {
102 uint32_t sync_point
= gpu_state_
->sync_point_manager()->GenerateSyncPoint();
103 sync_point_client_
->DidInsertSyncPoint(sync_point
);
105 driver_task_runner_
->PostTask(
107 base::Bind(&gpu::SyncPointManager::RetireSyncPoint
,
108 base::Unretained(gpu_state_
->sync_point_manager()),
113 void CommandBufferImpl::RetireSyncPoint(uint32_t sync_point
) {
114 driver_task_runner_
->PostTask(
115 FROM_HERE
, base::Bind(&gpu::SyncPointManager::RetireSyncPoint
,
116 base::Unretained(gpu_state_
->sync_point_manager()),
120 void CommandBufferImpl::Echo(const mojo::Callback
<void()>& callback
) {
121 driver_task_runner_
->PostTaskAndReply(FROM_HERE
, base::Bind(&base::DoNothing
),
122 base::Bind(&RunCallback
, callback
));
125 void CommandBufferImpl::CreateImage(int32_t id
,
126 mojo::ScopedHandle memory_handle
,
130 int32_t internal_format
) {
131 driver_task_runner_
->PostTask(
132 FROM_HERE
, base::Bind(&CommandBufferDriver::CreateImage
,
133 base::Unretained(driver_
.get()), id
,
134 base::Passed(&memory_handle
), type
,
135 base::Passed(&size
), format
, internal_format
));
138 void CommandBufferImpl::DestroyImage(int32_t id
) {
139 driver_task_runner_
->PostTask(
140 FROM_HERE
, base::Bind(&CommandBufferDriver::DestroyImage
,
141 base::Unretained(driver_
.get()), id
));
144 CommandBufferImpl::~CommandBufferImpl() {
146 observer_
->OnCommandBufferImplDestroyed();
149 void CommandBufferImpl::BindToRequest(
150 mojo::InterfaceRequest
<mojo::CommandBuffer
> request
) {
151 binding_
.Bind(request
.Pass());
152 binding_
.set_connection_error_handler([this]() { OnConnectionError(); });
155 void CommandBufferImpl::OnConnectionError() {
156 // OnConnectionError() is called on the control thread |control_task_runner|.
157 // sync_point_client_ is assigned and accessed on the control thread and so it
158 // should also be destroyed on the control because InterfacePtrs are thread-
160 sync_point_client_
.reset();
162 // Objects we own (such as CommandBufferDriver) need to be destroyed on the
163 // thread we were created on.
164 driver_task_runner_
->DeleteSoon(FROM_HERE
, this);
167 void CommandBufferImpl::DidLoseContext() {