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 "gpu/command_buffer/service/sync_point_manager.h"
15 void RunCallback(const mojo::Callback
<void()>& callback
) {
20 class CommandBufferImpl::CommandBufferDriverClientImpl
21 : public CommandBufferDriver::Client
{
23 explicit CommandBufferDriverClientImpl(CommandBufferImpl
* command_buffer
)
24 : command_buffer_(command_buffer
) {}
27 void UpdateVSyncParameters(base::TimeTicks timebase
,
28 base::TimeDelta interval
) override
{
29 command_buffer_
->UpdateVSyncParameters(timebase
, interval
);
32 void DidLoseContext() override
{ command_buffer_
->DidLoseContext(); }
34 CommandBufferImpl
* command_buffer_
;
36 DISALLOW_COPY_AND_ASSIGN(CommandBufferDriverClientImpl
);
39 CommandBufferImpl::CommandBufferImpl(
40 mojo::InterfaceRequest
<mojo::CommandBuffer
> request
,
41 mojo::ViewportParameterListenerPtr listener
,
42 scoped_refptr
<base::SingleThreadTaskRunner
> control_task_runner
,
43 gpu::SyncPointManager
* sync_point_manager
,
44 scoped_ptr
<CommandBufferDriver
> driver
)
45 : sync_point_manager_(sync_point_manager
),
46 driver_task_runner_(base::MessageLoop::current()->task_runner()),
47 driver_(driver
.Pass()),
48 viewport_parameter_listener_(listener
.Pass()),
51 driver_
->set_client(make_scoped_ptr(new CommandBufferDriverClientImpl(this)));
53 control_task_runner
->PostTask(
54 FROM_HERE
, base::Bind(&CommandBufferImpl::BindToRequest
,
55 base::Unretained(this), base::Passed(&request
)));
58 void CommandBufferImpl::Initialize(
59 mojo::CommandBufferSyncClientPtr sync_client
,
60 mojo::CommandBufferSyncPointClientPtr sync_point_client
,
61 mojo::CommandBufferLostContextObserverPtr loss_observer
,
62 mojo::ScopedSharedBufferHandle shared_state
) {
63 sync_point_client_
= sync_point_client
.Pass();
64 driver_task_runner_
->PostTask(
66 base::Bind(&CommandBufferDriver::Initialize
,
67 base::Unretained(driver_
.get()), base::Passed(&sync_client
),
68 base::Passed(&loss_observer
),
69 base::Passed(&shared_state
)));
72 void CommandBufferImpl::SetGetBuffer(int32_t buffer
) {
73 driver_task_runner_
->PostTask(
74 FROM_HERE
, base::Bind(&CommandBufferDriver::SetGetBuffer
,
75 base::Unretained(driver_
.get()), buffer
));
78 void CommandBufferImpl::Flush(int32_t put_offset
) {
79 driver_task_runner_
->PostTask(
80 FROM_HERE
, base::Bind(&CommandBufferDriver::Flush
,
81 base::Unretained(driver_
.get()), put_offset
));
84 void CommandBufferImpl::MakeProgress(int32_t last_get_offset
) {
85 driver_task_runner_
->PostTask(
86 FROM_HERE
, base::Bind(&CommandBufferDriver::MakeProgress
,
87 base::Unretained(driver_
.get()), last_get_offset
));
90 void CommandBufferImpl::RegisterTransferBuffer(
92 mojo::ScopedSharedBufferHandle transfer_buffer
,
94 driver_task_runner_
->PostTask(
95 FROM_HERE
, base::Bind(&CommandBufferDriver::RegisterTransferBuffer
,
96 base::Unretained(driver_
.get()), id
,
97 base::Passed(&transfer_buffer
), size
));
100 void CommandBufferImpl::DestroyTransferBuffer(int32_t id
) {
101 driver_task_runner_
->PostTask(
102 FROM_HERE
, base::Bind(&CommandBufferDriver::DestroyTransferBuffer
,
103 base::Unretained(driver_
.get()), id
));
106 void CommandBufferImpl::InsertSyncPoint(bool retire
) {
107 uint32_t sync_point
= sync_point_manager_
->GenerateSyncPoint();
108 sync_point_client_
->DidInsertSyncPoint(sync_point
);
110 driver_task_runner_
->PostTask(
111 FROM_HERE
, base::Bind(&gpu::SyncPointManager::RetireSyncPoint
,
112 sync_point_manager_
, sync_point
));
116 void CommandBufferImpl::RetireSyncPoint(uint32_t sync_point
) {
117 driver_task_runner_
->PostTask(
118 FROM_HERE
, base::Bind(&gpu::SyncPointManager::RetireSyncPoint
,
119 sync_point_manager_
, sync_point
));
122 void CommandBufferImpl::Echo(const mojo::Callback
<void()>& callback
) {
123 driver_task_runner_
->PostTaskAndReply(FROM_HERE
, base::Bind(&base::DoNothing
),
124 base::Bind(&RunCallback
, callback
));
127 void CommandBufferImpl::CreateImage(int32_t id
,
128 mojo::ScopedHandle memory_handle
,
132 int32_t internal_format
) {
133 driver_task_runner_
->PostTask(
134 FROM_HERE
, base::Bind(&CommandBufferDriver::CreateImage
,
135 base::Unretained(driver_
.get()), id
,
136 base::Passed(&memory_handle
), type
,
137 base::Passed(&size
), format
, internal_format
));
140 void CommandBufferImpl::DestroyImage(int32_t id
) {
141 driver_task_runner_
->PostTask(
142 FROM_HERE
, base::Bind(&CommandBufferDriver::DestroyImage
,
143 base::Unretained(driver_
.get()), id
));
146 CommandBufferImpl::~CommandBufferImpl() {
148 observer_
->OnCommandBufferImplDestroyed();
151 void CommandBufferImpl::BindToRequest(
152 mojo::InterfaceRequest
<mojo::CommandBuffer
> request
) {
153 binding_
.Bind(request
.Pass());
154 binding_
.set_error_handler(this);
157 void CommandBufferImpl::OnConnectionError() {
158 // OnConnectionError() is called on the background thread
159 // |control_task_runner| but objects we own (such as CommandBufferDriver)
160 // need to be destroyed on the thread we were created on.
161 driver_task_runner_
->DeleteSoon(FROM_HERE
, this);
164 void CommandBufferImpl::DidLoseContext() {
168 void CommandBufferImpl::UpdateVSyncParameters(base::TimeTicks timebase
,
169 base::TimeDelta interval
) {
170 if (!viewport_parameter_listener_
)
172 viewport_parameter_listener_
->OnVSyncParametersUpdated(
173 timebase
.ToInternalValue(), interval
.ToInternalValue());