Don't add an aura tooltip to bubble close buttons on Windows.
[chromium-blink-merge.git] / gpu / command_buffer / service / async_pixel_transfer_manager_idle.cc
blobb44b4774046e9995963009cb5214b1e449f5a05f
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"
7 #include "base/bind.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"
14 namespace gpu {
16 namespace {
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);
27 } // namespace
29 // Class which handles async pixel transfers in a platform
30 // independent way.
31 class AsyncPixelTransferDelegateIdle
32 : public AsyncPixelTransferDelegate,
33 public base::SupportsWeakPtr<AsyncPixelTransferDelegateIdle> {
34 public:
35 AsyncPixelTransferDelegateIdle(
36 AsyncPixelTransferManagerIdle::SharedState* state,
37 GLuint texture_id,
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;
50 private:
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);
57 uint64 id_;
58 GLuint texture_id_;
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,
71 GLuint texture_id,
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(
89 id_,
90 this,
91 base::Bind(&AsyncPixelTransferDelegateIdle::PerformAsyncTexImage2D,
92 AsWeakPtr(),
93 tex_params,
94 mem_params,
95 bind_callback)));
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(
107 id_,
108 this,
109 base::Bind(&AsyncPixelTransferDelegateIdle::PerformAsyncTexSubImage2D,
110 AsWeakPtr(),
111 tex_params,
112 mem_params)));
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();
125 ++iter) {
126 if (iter->transfer_id != id_)
127 continue;
129 (*iter).task.Run();
130 shared_state_->tasks.erase(iter);
131 break;
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");
152 glTexImage2D(
153 tex_params.target,
154 tex_params.level,
155 tex_params.internal_format,
156 tex_params.width,
157 tex_params.height,
158 tex_params.border,
159 tex_params.format,
160 tex_params.type,
161 data);
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.
171 bind_callback.Run();
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");
194 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,
201 tex_params.format,
202 tex_params.type,
203 data);
204 } else {
205 TRACE_EVENT0("gpu", "glTexSubImage2D");
206 glTexSubImage2D(
207 tex_params.target,
208 tex_params.level,
209 tex_params.xoffset,
210 tex_params.yoffset,
211 tex_params.width,
212 tex_params.height,
213 tex_params.format,
214 tex_params.type,
215 data);
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(
226 uint64 transfer_id,
227 AsyncPixelTransferDelegate* delegate,
228 const base::Closure& task)
229 : transfer_id(transfer_id),
230 delegate(delegate),
231 task(task) {
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)
248 return;
250 tasks.front().task.Run();
251 tasks.pop_front();
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);
271 return;
274 shared_state_.tasks.push_back(
275 Task(0, // 0 transfer_id for notification tasks.
276 NULL,
277 base::Bind(
278 &PerformNotifyCompletion,
279 mem_params,
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())
293 return;
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())
309 return;
311 const Task& task = shared_state_.tasks.back();
312 if (task.delegate)
313 task.delegate->WaitForTransferCompletion();
316 AsyncPixelTransferDelegate*
317 AsyncPixelTransferManagerIdle::CreatePixelTransferDelegateImpl(
318 gles2::TextureRef* ref,
319 const AsyncTexImage2DParams& define_params) {
320 return new AsyncPixelTransferDelegateIdle(&shared_state_,
321 ref->service_id(),
322 define_params);
325 } // namespace gpu