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 "gpu/command_buffer/service/async_pixel_transfer_manager_idle.h"
8 #include "base/lazy_instance.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/trace_event/trace_event.h"
11 #include "base/trace_event/trace_event_synthetic_delay.h"
12 #include "ui/gl/scoped_binders.h"
18 static uint64 g_next_pixel_transfer_state_id
= 1;
20 void PerformNotifyCompletion(
21 AsyncMemoryParams mem_params
,
22 scoped_refptr
<AsyncPixelTransferCompletionObserver
> observer
) {
23 TRACE_EVENT0("gpu", "PerformNotifyCompletion");
24 observer
->DidComplete(mem_params
);
29 // Class which handles async pixel transfers in a platform
31 class AsyncPixelTransferDelegateIdle
32 : public AsyncPixelTransferDelegate
,
33 public base::SupportsWeakPtr
<AsyncPixelTransferDelegateIdle
> {
35 AsyncPixelTransferDelegateIdle(
36 AsyncPixelTransferManagerIdle::SharedState
* state
,
38 const AsyncTexImage2DParams
& define_params
);
39 ~AsyncPixelTransferDelegateIdle() override
;
41 // Implement AsyncPixelTransferDelegate:
42 void AsyncTexImage2D(const AsyncTexImage2DParams
& tex_params
,
43 const AsyncMemoryParams
& mem_params
,
44 const base::Closure
& bind_callback
) override
;
45 void AsyncTexSubImage2D(const AsyncTexSubImage2DParams
& tex_params
,
46 const AsyncMemoryParams
& mem_params
) override
;
47 bool TransferIsInProgress() override
;
48 void WaitForTransferCompletion() override
;
51 void PerformAsyncTexImage2D(AsyncTexImage2DParams tex_params
,
52 AsyncMemoryParams mem_params
,
53 const base::Closure
& bind_callback
);
54 void PerformAsyncTexSubImage2D(AsyncTexSubImage2DParams tex_params
,
55 AsyncMemoryParams mem_params
);
59 bool transfer_in_progress_
;
60 AsyncTexImage2DParams define_params_
;
62 // Safe to hold a raw pointer because SharedState is owned by the Manager
63 // which owns the Delegate.
64 AsyncPixelTransferManagerIdle::SharedState
* shared_state_
;
66 DISALLOW_COPY_AND_ASSIGN(AsyncPixelTransferDelegateIdle
);
69 AsyncPixelTransferDelegateIdle::AsyncPixelTransferDelegateIdle(
70 AsyncPixelTransferManagerIdle::SharedState
* shared_state
,
72 const AsyncTexImage2DParams
& define_params
)
73 : id_(g_next_pixel_transfer_state_id
++),
74 texture_id_(texture_id
),
75 transfer_in_progress_(false),
76 define_params_(define_params
),
77 shared_state_(shared_state
) {}
79 AsyncPixelTransferDelegateIdle::~AsyncPixelTransferDelegateIdle() {}
81 void AsyncPixelTransferDelegateIdle::AsyncTexImage2D(
82 const AsyncTexImage2DParams
& tex_params
,
83 const AsyncMemoryParams
& mem_params
,
84 const base::Closure
& bind_callback
) {
85 TRACE_EVENT_SYNTHETIC_DELAY_BEGIN("gpu.AsyncTexImage");
86 DCHECK_EQ(static_cast<GLenum
>(GL_TEXTURE_2D
), tex_params
.target
);
88 shared_state_
->tasks
.push_back(AsyncPixelTransferManagerIdle::Task(
91 base::Bind(&AsyncPixelTransferDelegateIdle::PerformAsyncTexImage2D
,
97 transfer_in_progress_
= true;
100 void AsyncPixelTransferDelegateIdle::AsyncTexSubImage2D(
101 const AsyncTexSubImage2DParams
& tex_params
,
102 const AsyncMemoryParams
& mem_params
) {
103 TRACE_EVENT_SYNTHETIC_DELAY_BEGIN("gpu.AsyncTexImage");
104 DCHECK_EQ(static_cast<GLenum
>(GL_TEXTURE_2D
), tex_params
.target
);
106 shared_state_
->tasks
.push_back(AsyncPixelTransferManagerIdle::Task(
109 base::Bind(&AsyncPixelTransferDelegateIdle::PerformAsyncTexSubImage2D
,
114 transfer_in_progress_
= true;
117 bool AsyncPixelTransferDelegateIdle::TransferIsInProgress() {
118 return transfer_in_progress_
;
121 void AsyncPixelTransferDelegateIdle::WaitForTransferCompletion() {
122 for (std::list
<AsyncPixelTransferManagerIdle::Task
>::iterator iter
=
123 shared_state_
->tasks
.begin();
124 iter
!= shared_state_
->tasks
.end();
126 if (iter
->transfer_id
!= id_
)
130 shared_state_
->tasks
.erase(iter
);
134 shared_state_
->ProcessNotificationTasks();
137 void AsyncPixelTransferDelegateIdle::PerformAsyncTexImage2D(
138 AsyncTexImage2DParams tex_params
,
139 AsyncMemoryParams mem_params
,
140 const base::Closure
& bind_callback
) {
141 TRACE_EVENT2("gpu", "PerformAsyncTexImage2D",
142 "width", tex_params
.width
,
143 "height", tex_params
.height
);
145 void* data
= mem_params
.GetDataAddress();
147 base::TimeTicks
begin_time(base::TimeTicks::Now());
148 gfx::ScopedTextureBinder
texture_binder(tex_params
.target
, texture_id_
);
151 TRACE_EVENT0("gpu", "glTexImage2D");
155 tex_params
.internal_format
,
164 TRACE_EVENT_SYNTHETIC_DELAY_END("gpu.AsyncTexImage");
165 transfer_in_progress_
= false;
166 shared_state_
->texture_upload_count
++;
167 shared_state_
->total_texture_upload_time
+=
168 base::TimeTicks::Now() - begin_time
;
170 // The texture is already fully bound so just call it now.
174 void AsyncPixelTransferDelegateIdle::PerformAsyncTexSubImage2D(
175 AsyncTexSubImage2DParams tex_params
,
176 AsyncMemoryParams mem_params
) {
177 TRACE_EVENT2("gpu", "PerformAsyncTexSubImage2D",
178 "width", tex_params
.width
,
179 "height", tex_params
.height
);
181 void* data
= mem_params
.GetDataAddress();
183 base::TimeTicks
begin_time(base::TimeTicks::Now());
184 gfx::ScopedTextureBinder
texture_binder(tex_params
.target
, texture_id_
);
186 if (shared_state_
->use_teximage2d_over_texsubimage2d
&&
187 tex_params
.xoffset
== 0 &&
188 tex_params
.yoffset
== 0 &&
189 tex_params
.target
== define_params_
.target
&&
190 tex_params
.level
== define_params_
.level
&&
191 tex_params
.width
== define_params_
.width
&&
192 tex_params
.height
== define_params_
.height
) {
193 TRACE_EVENT0("gpu", "glTexImage2D");
195 define_params_
.target
,
196 define_params_
.level
,
197 define_params_
.internal_format
,
198 define_params_
.width
,
199 define_params_
.height
,
200 define_params_
.border
,
205 TRACE_EVENT0("gpu", "glTexSubImage2D");
218 TRACE_EVENT_SYNTHETIC_DELAY_END("gpu.AsyncTexImage");
219 transfer_in_progress_
= false;
220 shared_state_
->texture_upload_count
++;
221 shared_state_
->total_texture_upload_time
+=
222 base::TimeTicks::Now() - begin_time
;
225 AsyncPixelTransferManagerIdle::Task::Task(
227 AsyncPixelTransferDelegate
* delegate
,
228 const base::Closure
& task
)
229 : transfer_id(transfer_id
),
234 AsyncPixelTransferManagerIdle::Task::~Task() {}
236 AsyncPixelTransferManagerIdle::SharedState::SharedState(
237 bool use_teximage2d_over_texsubimage2d
)
238 : use_teximage2d_over_texsubimage2d(use_teximage2d_over_texsubimage2d
),
239 texture_upload_count(0) {
242 AsyncPixelTransferManagerIdle::SharedState::~SharedState() {}
244 void AsyncPixelTransferManagerIdle::SharedState::ProcessNotificationTasks() {
245 while (!tasks
.empty()) {
246 // Stop when we reach a pixel transfer task.
247 if (tasks
.front().transfer_id
)
250 tasks
.front().task
.Run();
255 AsyncPixelTransferManagerIdle::AsyncPixelTransferManagerIdle(
256 bool use_teximage2d_over_texsubimage2d
)
257 : shared_state_(use_teximage2d_over_texsubimage2d
) {
260 AsyncPixelTransferManagerIdle::~AsyncPixelTransferManagerIdle() {}
262 void AsyncPixelTransferManagerIdle::BindCompletedAsyncTransfers() {
263 // Everything is already bound.
266 void AsyncPixelTransferManagerIdle::AsyncNotifyCompletion(
267 const AsyncMemoryParams
& mem_params
,
268 AsyncPixelTransferCompletionObserver
* observer
) {
269 if (shared_state_
.tasks
.empty()) {
270 observer
->DidComplete(mem_params
);
274 shared_state_
.tasks
.push_back(
275 Task(0, // 0 transfer_id for notification tasks.
278 &PerformNotifyCompletion
,
280 make_scoped_refptr(observer
))));
283 uint32
AsyncPixelTransferManagerIdle::GetTextureUploadCount() {
284 return shared_state_
.texture_upload_count
;
287 base::TimeDelta
AsyncPixelTransferManagerIdle::GetTotalTextureUploadTime() {
288 return shared_state_
.total_texture_upload_time
;
291 void AsyncPixelTransferManagerIdle::ProcessMorePendingTransfers() {
292 if (shared_state_
.tasks
.empty())
295 // First task should always be a pixel transfer task.
296 DCHECK(shared_state_
.tasks
.front().transfer_id
);
297 shared_state_
.tasks
.front().task
.Run();
298 shared_state_
.tasks
.pop_front();
300 shared_state_
.ProcessNotificationTasks();
303 bool AsyncPixelTransferManagerIdle::NeedsProcessMorePendingTransfers() {
304 return !shared_state_
.tasks
.empty();
307 void AsyncPixelTransferManagerIdle::WaitAllAsyncTexImage2D() {
308 if (shared_state_
.tasks
.empty())
311 const Task
& task
= shared_state_
.tasks
.back();
313 task
.delegate
->WaitForTransferCompletion();
316 AsyncPixelTransferDelegate
*
317 AsyncPixelTransferManagerIdle::CreatePixelTransferDelegateImpl(
318 gles2::TextureRef
* ref
,
319 const AsyncTexImage2DParams
& define_params
) {
320 return new AsyncPixelTransferDelegateIdle(&shared_state_
,