Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(478)

Side by Side Diff: cc/raster/one_copy_tile_task_worker_pool.cc

Issue 1230203007: Re-land: cc: Use worker context for one-copy tile initialization. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: remove components/html_viewer/ change Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "cc/raster/one_copy_tile_task_worker_pool.h" 5 #include "cc/raster/one_copy_tile_task_worker_pool.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <limits> 8 #include <limits>
9 9
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/trace_event/memory_dump_manager.h"
11 #include "base/trace_event/trace_event.h" 13 #include "base/trace_event/trace_event.h"
12 #include "base/trace_event/trace_event_argument.h" 14 #include "base/trace_event/trace_event_argument.h"
13 #include "cc/base/math_util.h" 15 #include "cc/base/math_util.h"
14 #include "cc/debug/traced_value.h" 16 #include "cc/debug/traced_value.h"
15 #include "cc/raster/raster_buffer.h" 17 #include "cc/raster/raster_buffer.h"
16 #include "cc/resources/platform_color.h" 18 #include "cc/resources/platform_color.h"
17 #include "cc/resources/resource_pool.h" 19 #include "cc/resources/resource_format.h"
20 #include "cc/resources/resource_util.h"
18 #include "cc/resources/scoped_resource.h" 21 #include "cc/resources/scoped_resource.h"
22 #include "gpu/GLES2/gl2extchromium.h"
19 #include "gpu/command_buffer/client/gles2_interface.h" 23 #include "gpu/command_buffer/client/gles2_interface.h"
24 #include "gpu/command_buffer/client/gpu_memory_buffer_manager.h"
20 #include "ui/gfx/buffer_format_util.h" 25 #include "ui/gfx/buffer_format_util.h"
21 #include "ui/gfx/gpu_memory_buffer.h"
22 26
23 namespace cc { 27 namespace cc {
24 namespace { 28 namespace {
25 29
26 class RasterBufferImpl : public RasterBuffer { 30 class RasterBufferImpl : public RasterBuffer {
27 public: 31 public:
28 RasterBufferImpl(OneCopyTileTaskWorkerPool* worker_pool, 32 RasterBufferImpl(OneCopyTileTaskWorkerPool* worker_pool,
29 ResourceProvider* resource_provider, 33 ResourceProvider* resource_provider,
30 ResourcePool* resource_pool,
31 ResourceFormat resource_format, 34 ResourceFormat resource_format,
32 const Resource* output_resource, 35 const Resource* resource,
33 uint64_t previous_content_id) 36 uint64_t previous_content_id)
34 : worker_pool_(worker_pool), 37 : worker_pool_(worker_pool),
35 resource_provider_(resource_provider), 38 resource_(resource),
36 resource_pool_(resource_pool), 39 lock_(resource_provider, resource->id()),
37 output_resource_(output_resource), 40 previous_content_id_(previous_content_id) {}
38 raster_content_id_(0),
39 sequence_(0) {
40 if (worker_pool->have_persistent_gpu_memory_buffers() &&
41 previous_content_id) {
42 raster_resource_ =
43 resource_pool->TryAcquireResourceWithContentId(previous_content_id);
44 }
45 if (raster_resource_) {
46 raster_content_id_ = previous_content_id;
47 DCHECK_EQ(resource_format, raster_resource_->format());
48 DCHECK_EQ(output_resource->size().ToString(),
49 raster_resource_->size().ToString());
50 } else {
51 raster_resource_ = resource_pool->AcquireResource(output_resource->size(),
52 resource_format);
53 }
54 41
55 lock_.reset(new ResourceProvider::ScopedWriteLockGpuMemoryBuffer( 42 ~RasterBufferImpl() override {}
56 resource_provider_, raster_resource_->id()));
57 }
58
59 ~RasterBufferImpl() override {
60 // Release write lock in case a copy was never scheduled.
61 lock_.reset();
62
63 // Make sure any scheduled copy operations are issued before we release the
64 // raster resource.
65 if (sequence_)
66 worker_pool_->AdvanceLastIssuedCopyTo(sequence_);
67
68 // Return resources to pool so they can be used by another RasterBuffer
69 // instance.
70 resource_pool_->ReleaseResource(raster_resource_.Pass(),
71 raster_content_id_);
72 }
73 43
74 // Overridden from RasterBuffer: 44 // Overridden from RasterBuffer:
75 void Playback(const RasterSource* raster_source, 45 void Playback(const RasterSource* raster_source,
76 const gfx::Rect& raster_full_rect, 46 const gfx::Rect& raster_full_rect,
77 const gfx::Rect& raster_dirty_rect, 47 const gfx::Rect& raster_dirty_rect,
78 uint64_t new_content_id, 48 uint64_t new_content_id,
79 float scale, 49 float scale,
80 bool include_images) override { 50 bool include_images) override {
81 // If there's a raster_content_id_, we are reusing a resource with that 51 worker_pool_->PlaybackAndCopyOnWorkerThread(
82 // content id. 52 resource_, &lock_, raster_source, raster_full_rect, raster_dirty_rect,
83 bool reusing_raster_resource = raster_content_id_ != 0; 53 scale, include_images, previous_content_id_, new_content_id);
84 sequence_ = worker_pool_->PlaybackAndScheduleCopyOnWorkerThread(
85 reusing_raster_resource, lock_.Pass(), raster_resource_.get(),
86 output_resource_, raster_source, raster_full_rect, raster_dirty_rect,
87 scale, include_images);
88 // Store the content id of the resource to return to the pool.
89 raster_content_id_ = new_content_id;
90 } 54 }
91 55
92 private: 56 private:
93 OneCopyTileTaskWorkerPool* worker_pool_; 57 OneCopyTileTaskWorkerPool* worker_pool_;
94 ResourceProvider* resource_provider_; 58 const Resource* resource_;
95 ResourcePool* resource_pool_; 59 ResourceProvider::ScopedWriteLockGL lock_;
96 const Resource* output_resource_; 60 uint64_t previous_content_id_;
97 uint64_t raster_content_id_;
98 scoped_ptr<ScopedResource> raster_resource_;
99 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> lock_;
100 CopySequenceNumber sequence_;
101 61
102 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl); 62 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl);
103 }; 63 };
104 64
105 // Number of in-flight copy operations to allow. 65 // Delay between checking for query result to be available.
106 const int kMaxCopyOperations = 32; 66 const int kCheckForQueryResultAvailableTickRateMs = 1;
107 67
108 // Delay been checking for copy operations to complete. 68 // Number of attempts to allow before we perform a check that will wait for
109 const int kCheckForCompletedCopyOperationsTickRateMs = 1; 69 // query to complete.
110 70 const int kMaxCheckForQueryResultAvailableAttempts = 256;
111 // Number of failed attempts to allow before we perform a check that will
112 // wait for copy operations to complete if needed.
113 const int kFailedAttemptsBeforeWaitIfNeeded = 256;
114 71
115 // 4MiB is the size of 4 512x512 tiles, which has proven to be a good 72 // 4MiB is the size of 4 512x512 tiles, which has proven to be a good
116 // default batch size for copy operations. 73 // default batch size for copy operations.
117 const int kMaxBytesPerCopyOperation = 1024 * 1024 * 4; 74 const int kMaxBytesPerCopyOperation = 1024 * 1024 * 4;
118 75
76 // Delay before a staging buffer might be released.
77 const int kStagingBufferExpirationDelayMs = 1000;
78
79 bool CheckForQueryResult(gpu::gles2::GLES2Interface* gl, unsigned query_id) {
80 unsigned complete = 1;
81 gl->GetQueryObjectuivEXT(query_id, GL_QUERY_RESULT_AVAILABLE_EXT, &complete);
82 return !!complete;
83 }
84
85 void WaitForQueryResult(gpu::gles2::GLES2Interface* gl, unsigned query_id) {
86 TRACE_EVENT0("cc", "WaitForQueryResult");
87
88 int attempts_left = kMaxCheckForQueryResultAvailableAttempts;
89 while (attempts_left--) {
90 if (CheckForQueryResult(gl, query_id))
91 break;
92
93 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
94 kCheckForQueryResultAvailableTickRateMs));
95 }
96
97 unsigned result = 0;
98 gl->GetQueryObjectuivEXT(query_id, GL_QUERY_RESULT_EXT, &result);
99 }
100
119 } // namespace 101 } // namespace
120 102
121 OneCopyTileTaskWorkerPool::CopyOperation::CopyOperation( 103 OneCopyTileTaskWorkerPool::StagingBuffer::StagingBuffer(const gfx::Size& size)
122 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer> src_write_lock, 104 : size(size), texture_id(0), image_id(0), query_id(0), content_id(0) {}
123 const Resource* src, 105
124 const Resource* dst, 106 OneCopyTileTaskWorkerPool::StagingBuffer::~StagingBuffer() {
125 const gfx::Rect& rect) 107 DCHECK_EQ(texture_id, 0u);
126 : src_write_lock(src_write_lock.Pass()), src(src), dst(dst), rect(rect) { 108 DCHECK_EQ(image_id, 0u);
109 DCHECK_EQ(query_id, 0u);
127 } 110 }
128 111
129 OneCopyTileTaskWorkerPool::CopyOperation::~CopyOperation() { 112 void OneCopyTileTaskWorkerPool::StagingBuffer::DestroyGLResources(
113 gpu::gles2::GLES2Interface* gl) {
114 if (query_id) {
115 gl->DeleteQueriesEXT(1, &query_id);
116 query_id = 0;
117 }
118 if (image_id) {
119 gl->DestroyImageCHROMIUM(image_id);
120 image_id = 0;
121 }
122 if (texture_id) {
123 gl->DeleteTextures(1, &texture_id);
124 texture_id = 0;
125 }
126 }
127
128 void OneCopyTileTaskWorkerPool::StagingBuffer::OnMemoryDump(
129 base::trace_event::ProcessMemoryDump* pmd,
130 ResourceFormat format,
131 bool in_free_list) const {
132 if (!gpu_memory_buffer)
133 return;
134
135 gfx::GpuMemoryBufferId buffer_id = gpu_memory_buffer->GetId();
136 std::string buffer_dump_name =
137 base::StringPrintf("cc/one_copy/staging_memory/buffer_%d", buffer_id.id);
138 base::trace_event::MemoryAllocatorDump* buffer_dump =
139 pmd->CreateAllocatorDump(buffer_dump_name);
140
141 uint64_t buffer_size_in_bytes =
142 ResourceUtil::UncheckedSizeInBytes<uint64_t>(size, format);
143 buffer_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
144 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
145 buffer_size_in_bytes);
146 buffer_dump->AddScalar("free_size",
147 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
148 in_free_list ? buffer_size_in_bytes : 0);
149
150 // Emit an ownership edge towards a global allocator dump node.
151 const uint64 tracing_process_id =
152 base::trace_event::MemoryDumpManager::GetInstance()
153 ->GetTracingProcessId();
154 base::trace_event::MemoryAllocatorDumpGuid shared_buffer_guid =
155 gfx::GetGpuMemoryBufferGUIDForTracing(tracing_process_id, buffer_id);
156 pmd->CreateSharedGlobalAllocatorDump(shared_buffer_guid);
157
158 // By creating an edge with a higher |importance| (w.r.t. browser-side dumps)
159 // the tracing UI will account the effective size of the buffer to the child.
160 const int kImportance = 2;
161 pmd->AddOwnershipEdge(buffer_dump->guid(), shared_buffer_guid, kImportance);
130 } 162 }
131 163
132 // static 164 // static
133 scoped_ptr<TileTaskWorkerPool> OneCopyTileTaskWorkerPool::Create( 165 scoped_ptr<TileTaskWorkerPool> OneCopyTileTaskWorkerPool::Create(
134 base::SequencedTaskRunner* task_runner, 166 base::SequencedTaskRunner* task_runner,
135 TaskGraphRunner* task_graph_runner, 167 TaskGraphRunner* task_graph_runner,
136 ContextProvider* context_provider, 168 ContextProvider* context_provider,
137 ResourceProvider* resource_provider, 169 ResourceProvider* resource_provider,
138 ResourcePool* resource_pool,
139 int max_copy_texture_chromium_size, 170 int max_copy_texture_chromium_size,
140 bool have_persistent_gpu_memory_buffers) { 171 bool use_persistent_gpu_memory_buffers,
172 int max_staging_buffers) {
141 return make_scoped_ptr<TileTaskWorkerPool>(new OneCopyTileTaskWorkerPool( 173 return make_scoped_ptr<TileTaskWorkerPool>(new OneCopyTileTaskWorkerPool(
142 task_runner, task_graph_runner, context_provider, resource_provider, 174 task_runner, task_graph_runner, resource_provider,
143 resource_pool, max_copy_texture_chromium_size, 175 max_copy_texture_chromium_size, use_persistent_gpu_memory_buffers,
144 have_persistent_gpu_memory_buffers)); 176 max_staging_buffers));
145 } 177 }
146 178
147 OneCopyTileTaskWorkerPool::OneCopyTileTaskWorkerPool( 179 OneCopyTileTaskWorkerPool::OneCopyTileTaskWorkerPool(
148 base::SequencedTaskRunner* task_runner, 180 base::SequencedTaskRunner* task_runner,
149 TaskGraphRunner* task_graph_runner, 181 TaskGraphRunner* task_graph_runner,
150 ContextProvider* context_provider,
151 ResourceProvider* resource_provider, 182 ResourceProvider* resource_provider,
152 ResourcePool* resource_pool,
153 int max_copy_texture_chromium_size, 183 int max_copy_texture_chromium_size,
154 bool have_persistent_gpu_memory_buffers) 184 bool use_persistent_gpu_memory_buffers,
185 int max_staging_buffers)
155 : task_runner_(task_runner), 186 : task_runner_(task_runner),
156 task_graph_runner_(task_graph_runner), 187 task_graph_runner_(task_graph_runner),
157 namespace_token_(task_graph_runner->GetNamespaceToken()), 188 namespace_token_(task_graph_runner->GetNamespaceToken()),
158 context_provider_(context_provider),
159 resource_provider_(resource_provider), 189 resource_provider_(resource_provider),
160 resource_pool_(resource_pool),
161 max_bytes_per_copy_operation_( 190 max_bytes_per_copy_operation_(
162 max_copy_texture_chromium_size 191 max_copy_texture_chromium_size
163 ? std::min(kMaxBytesPerCopyOperation, 192 ? std::min(kMaxBytesPerCopyOperation,
164 max_copy_texture_chromium_size) 193 max_copy_texture_chromium_size)
165 : kMaxBytesPerCopyOperation), 194 : kMaxBytesPerCopyOperation),
166 have_persistent_gpu_memory_buffers_(have_persistent_gpu_memory_buffers), 195 use_persistent_gpu_memory_buffers_(use_persistent_gpu_memory_buffers),
167 last_issued_copy_operation_(0),
168 last_flushed_copy_operation_(0),
169 lock_(),
170 copy_operation_count_cv_(&lock_),
171 bytes_scheduled_since_last_flush_(0), 196 bytes_scheduled_since_last_flush_(0),
172 issued_copy_operation_count_(0), 197 max_staging_buffers_(max_staging_buffers),
173 next_copy_operation_sequence_(1), 198 staging_buffer_expiration_delay_(
174 check_for_completed_copy_operations_pending_(false), 199 base::TimeDelta::FromMilliseconds(kStagingBufferExpirationDelayMs)),
175 shutdown_(false), 200 reduce_memory_usage_pending_(false),
176 weak_ptr_factory_(this), 201 weak_ptr_factory_(this),
177 task_set_finished_weak_ptr_factory_(this) { 202 task_set_finished_weak_ptr_factory_(this) {
178 DCHECK(context_provider_); 203 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
204 this, base::ThreadTaskRunnerHandle::Get());
205 reduce_memory_usage_callback_ =
206 base::Bind(&OneCopyTileTaskWorkerPool::ReduceMemoryUsage,
207 weak_ptr_factory_.GetWeakPtr());
179 } 208 }
180 209
181 OneCopyTileTaskWorkerPool::~OneCopyTileTaskWorkerPool() { 210 OneCopyTileTaskWorkerPool::~OneCopyTileTaskWorkerPool() {
182 DCHECK_EQ(pending_copy_operations_.size(), 0u); 211 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
212 this);
183 } 213 }
184 214
185 TileTaskRunner* OneCopyTileTaskWorkerPool::AsTileTaskRunner() { 215 TileTaskRunner* OneCopyTileTaskWorkerPool::AsTileTaskRunner() {
186 return this; 216 return this;
187 } 217 }
188 218
189 void OneCopyTileTaskWorkerPool::SetClient(TileTaskRunnerClient* client) { 219 void OneCopyTileTaskWorkerPool::SetClient(TileTaskRunnerClient* client) {
190 client_ = client; 220 client_ = client;
191 } 221 }
192 222
193 void OneCopyTileTaskWorkerPool::Shutdown() { 223 void OneCopyTileTaskWorkerPool::Shutdown() {
194 TRACE_EVENT0("cc", "OneCopyTileTaskWorkerPool::Shutdown"); 224 TRACE_EVENT0("cc", "OneCopyTileTaskWorkerPool::Shutdown");
195 225
196 {
197 base::AutoLock lock(lock_);
198
199 shutdown_ = true;
200 copy_operation_count_cv_.Signal();
201 }
202
203 TaskGraph empty; 226 TaskGraph empty;
204 task_graph_runner_->ScheduleTasks(namespace_token_, &empty); 227 task_graph_runner_->ScheduleTasks(namespace_token_, &empty);
205 task_graph_runner_->WaitForTasksToFinishRunning(namespace_token_); 228 task_graph_runner_->WaitForTasksToFinishRunning(namespace_token_);
229
230 base::AutoLock lock(lock_);
231
232 if (buffers_.empty())
233 return;
234
235 ReleaseBuffersNotUsedSince(base::TimeTicks() + base::TimeDelta::Max());
206 } 236 }
207 237
208 void OneCopyTileTaskWorkerPool::ScheduleTasks(TileTaskQueue* queue) { 238 void OneCopyTileTaskWorkerPool::ScheduleTasks(TileTaskQueue* queue) {
209 TRACE_EVENT0("cc", "OneCopyTileTaskWorkerPool::ScheduleTasks"); 239 TRACE_EVENT0("cc", "OneCopyTileTaskWorkerPool::ScheduleTasks");
210 240
211 #if DCHECK_IS_ON()
212 {
213 base::AutoLock lock(lock_);
214 DCHECK(!shutdown_);
215 }
216 #endif
217
218 if (tasks_pending_.none()) 241 if (tasks_pending_.none())
219 TRACE_EVENT_ASYNC_BEGIN0("cc", "ScheduledTasks", this); 242 TRACE_EVENT_ASYNC_BEGIN0("cc", "ScheduledTasks", this);
220 243
221 // Mark all task sets as pending. 244 // Mark all task sets as pending.
222 tasks_pending_.set(); 245 tasks_pending_.set();
223 246
224 size_t priority = kTileTaskPriorityBase; 247 size_t priority = kTileTaskPriorityBase;
225 248
226 graph_.Reset(); 249 graph_.Reset();
227 250
228 // Cancel existing OnTaskSetFinished callbacks. 251 // Cancel existing OnTaskSetFinished callbacks.
229 task_set_finished_weak_ptr_factory_.InvalidateWeakPtrs(); 252 task_set_finished_weak_ptr_factory_.InvalidateWeakPtrs();
230 253
231 scoped_refptr<TileTask> new_task_set_finished_tasks[kNumberOfTaskSets]; 254 scoped_refptr<TileTask> new_task_set_finished_tasks[kNumberOfTaskSets];
232 255
233 size_t task_count[kNumberOfTaskSets] = {0}; 256 size_t task_count[kNumberOfTaskSets] = {0};
234 257
235 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) { 258 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) {
236 new_task_set_finished_tasks[task_set] = CreateTaskSetFinishedTask( 259 new_task_set_finished_tasks[task_set] = CreateTaskSetFinishedTask(
237 task_runner_.get(), 260 task_runner_.get(),
238 base::Bind(&OneCopyTileTaskWorkerPool::OnTaskSetFinished, 261 base::Bind(&OneCopyTileTaskWorkerPool::OnTaskSetFinished,
239 task_set_finished_weak_ptr_factory_.GetWeakPtr(), task_set)); 262 task_set_finished_weak_ptr_factory_.GetWeakPtr(), task_set));
240 } 263 }
241 264
242 resource_pool_->CheckBusyResources(false);
243
244 for (TileTaskQueue::Item::Vector::const_iterator it = queue->items.begin(); 265 for (TileTaskQueue::Item::Vector::const_iterator it = queue->items.begin();
245 it != queue->items.end(); ++it) { 266 it != queue->items.end(); ++it) {
246 const TileTaskQueue::Item& item = *it; 267 const TileTaskQueue::Item& item = *it;
247 RasterTask* task = item.task; 268 RasterTask* task = item.task;
248 DCHECK(!task->HasCompleted()); 269 DCHECK(!task->HasCompleted());
249 270
250 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) { 271 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) {
251 if (!item.task_sets[task_set]) 272 if (!item.task_sets[task_set])
252 continue; 273 continue;
253 274
254 ++task_count[task_set]; 275 ++task_count[task_set];
255 276
256 graph_.edges.push_back( 277 graph_.edges.push_back(
257 TaskGraph::Edge(task, new_task_set_finished_tasks[task_set].get())); 278 TaskGraph::Edge(task, new_task_set_finished_tasks[task_set].get()));
258 } 279 }
259 280
260 InsertNodesForRasterTask(&graph_, task, task->dependencies(), priority++); 281 InsertNodesForRasterTask(&graph_, task, task->dependencies(), priority++);
261 } 282 }
262 283
263 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) { 284 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) {
264 InsertNodeForTask(&graph_, new_task_set_finished_tasks[task_set].get(), 285 InsertNodeForTask(&graph_, new_task_set_finished_tasks[task_set].get(),
265 kTaskSetFinishedTaskPriorityBase + task_set, 286 kTaskSetFinishedTaskPriorityBase + task_set,
266 task_count[task_set]); 287 task_count[task_set]);
267 } 288 }
268 289
269 ScheduleTasksOnOriginThread(this, &graph_); 290 ScheduleTasksOnOriginThread(this, &graph_);
291
292 // Barrier to sync any new resources to the worker context.
293 resource_provider_->output_surface()
294 ->context_provider()
295 ->ContextGL()
296 ->OrderingBarrierCHROMIUM();
297
270 task_graph_runner_->ScheduleTasks(namespace_token_, &graph_); 298 task_graph_runner_->ScheduleTasks(namespace_token_, &graph_);
271 299
272 std::copy(new_task_set_finished_tasks, 300 std::copy(new_task_set_finished_tasks,
273 new_task_set_finished_tasks + kNumberOfTaskSets, 301 new_task_set_finished_tasks + kNumberOfTaskSets,
274 task_set_finished_tasks_); 302 task_set_finished_tasks_);
275 303
276 resource_pool_->ReduceResourceUsage();
277
278 TRACE_EVENT_ASYNC_STEP_INTO1("cc", "ScheduledTasks", this, "running", "state", 304 TRACE_EVENT_ASYNC_STEP_INTO1("cc", "ScheduledTasks", this, "running", "state",
279 StateAsValue()); 305 StateAsValue());
280 } 306 }
281 307
282 void OneCopyTileTaskWorkerPool::CheckForCompletedTasks() { 308 void OneCopyTileTaskWorkerPool::CheckForCompletedTasks() {
283 TRACE_EVENT0("cc", "OneCopyTileTaskWorkerPool::CheckForCompletedTasks"); 309 TRACE_EVENT0("cc", "OneCopyTileTaskWorkerPool::CheckForCompletedTasks");
284 310
285 task_graph_runner_->CollectCompletedTasks(namespace_token_, 311 task_graph_runner_->CollectCompletedTasks(namespace_token_,
286 &completed_tasks_); 312 &completed_tasks_);
287 313
(...skipping 18 matching lines...) Expand all
306 return !PlatformColor::SameComponentOrder(GetResourceFormat()); 332 return !PlatformColor::SameComponentOrder(GetResourceFormat());
307 } 333 }
308 334
309 scoped_ptr<RasterBuffer> OneCopyTileTaskWorkerPool::AcquireBufferForRaster( 335 scoped_ptr<RasterBuffer> OneCopyTileTaskWorkerPool::AcquireBufferForRaster(
310 const Resource* resource, 336 const Resource* resource,
311 uint64_t resource_content_id, 337 uint64_t resource_content_id,
312 uint64_t previous_content_id) { 338 uint64_t previous_content_id) {
313 // TODO(danakj): If resource_content_id != 0, we only need to copy/upload 339 // TODO(danakj): If resource_content_id != 0, we only need to copy/upload
314 // the dirty rect. 340 // the dirty rect.
315 DCHECK_EQ(resource->format(), resource_provider_->best_texture_format()); 341 DCHECK_EQ(resource->format(), resource_provider_->best_texture_format());
316 return make_scoped_ptr<RasterBuffer>( 342 return make_scoped_ptr<RasterBuffer>(new RasterBufferImpl(
317 new RasterBufferImpl(this, resource_provider_, resource_pool_, 343 this, resource_provider_, resource_provider_->best_texture_format(),
318 resource_provider_->best_texture_format(), resource, 344 resource, previous_content_id));
319 previous_content_id));
320 } 345 }
321 346
322 void OneCopyTileTaskWorkerPool::ReleaseBufferForRaster( 347 void OneCopyTileTaskWorkerPool::ReleaseBufferForRaster(
323 scoped_ptr<RasterBuffer> buffer) { 348 scoped_ptr<RasterBuffer> buffer) {
324 // Nothing to do here. RasterBufferImpl destructor cleans up after itself. 349 // Nothing to do here. RasterBufferImpl destructor cleans up after itself.
325 } 350 }
326 351
327 CopySequenceNumber 352 void OneCopyTileTaskWorkerPool::PlaybackAndCopyOnWorkerThread(
328 OneCopyTileTaskWorkerPool::PlaybackAndScheduleCopyOnWorkerThread( 353 const Resource* resource,
329 bool reusing_raster_resource, 354 const ResourceProvider::ScopedWriteLockGL* resource_lock,
330 scoped_ptr<ResourceProvider::ScopedWriteLockGpuMemoryBuffer>
331 raster_resource_write_lock,
332 const Resource* raster_resource,
333 const Resource* output_resource,
334 const RasterSource* raster_source, 355 const RasterSource* raster_source,
335 const gfx::Rect& raster_full_rect, 356 const gfx::Rect& raster_full_rect,
336 const gfx::Rect& raster_dirty_rect, 357 const gfx::Rect& raster_dirty_rect,
337 float scale, 358 float scale,
338 bool include_images) { 359 bool include_images,
339 gfx::GpuMemoryBuffer* gpu_memory_buffer = 360 uint64_t previous_content_id,
340 raster_resource_write_lock->GetGpuMemoryBuffer(); 361 uint64_t new_content_id) {
341 if (gpu_memory_buffer) { 362 base::AutoLock lock(lock_);
342 DCHECK_EQ( 363
343 1u, gfx::NumberOfPlanesForBufferFormat(gpu_memory_buffer->GetFormat())); 364 scoped_ptr<StagingBuffer> staging_buffer =
344 void* data = NULL; 365 AcquireStagingBuffer(resource, previous_content_id);
345 bool rv = gpu_memory_buffer->Map(&data); 366 DCHECK(staging_buffer);
346 DCHECK(rv); 367
347 int stride; 368 {
348 gpu_memory_buffer->GetStride(&stride); 369 base::AutoUnlock unlock(lock_);
349 // TileTaskWorkerPool::PlaybackToMemory only supports unsigned strides. 370
350 DCHECK_GE(stride, 0); 371 // Allocate GpuMemoryBuffer if necessary.
372 if (!staging_buffer->gpu_memory_buffer) {
373 staging_buffer->gpu_memory_buffer =
374 resource_provider_->gpu_memory_buffer_manager()
375 ->AllocateGpuMemoryBuffer(
376 staging_buffer->size,
377 BufferFormat(resource_provider_->best_texture_format()),
378 use_persistent_gpu_memory_buffers_
379 ? gfx::BufferUsage::PERSISTENT_MAP
380 : gfx::BufferUsage::MAP);
381 DCHECK_EQ(gfx::NumberOfPlanesForBufferFormat(
382 staging_buffer->gpu_memory_buffer->GetFormat()),
383 1u);
384 }
351 385
352 gfx::Rect playback_rect = raster_full_rect; 386 gfx::Rect playback_rect = raster_full_rect;
353 if (reusing_raster_resource) { 387 if (use_persistent_gpu_memory_buffers_ && previous_content_id) {
354 playback_rect.Intersect(raster_dirty_rect); 388 // Reduce playback rect to dirty region if the content id of the staging
355 } 389 // buffer matches the prevous content id.
356 DCHECK(!playback_rect.IsEmpty()) 390 if (previous_content_id == staging_buffer->content_id)
357 << "Why are we rastering a tile that's not dirty?"; 391 playback_rect.Intersect(raster_dirty_rect);
358 TileTaskWorkerPool::PlaybackToMemory( 392 }
359 data, raster_resource->format(), raster_resource->size(), 393
360 static_cast<size_t>(stride), raster_source, raster_full_rect, 394 if (staging_buffer->gpu_memory_buffer) {
361 playback_rect, scale, include_images); 395 void* data = nullptr;
362 gpu_memory_buffer->Unmap(); 396 bool rv = staging_buffer->gpu_memory_buffer->Map(&data);
363 } 397 DCHECK(rv);
364 398 int stride;
399 staging_buffer->gpu_memory_buffer->GetStride(&stride);
400 // TileTaskWorkerPool::PlaybackToMemory only supports unsigned strides.
401 DCHECK_GE(stride, 0);
402
403 DCHECK(!playback_rect.IsEmpty())
404 << "Why are we rastering a tile that's not dirty?";
405 TileTaskWorkerPool::PlaybackToMemory(
406 data, resource_provider_->best_texture_format(), staging_buffer->size,
407 static_cast<size_t>(stride), raster_source, raster_full_rect,
408 playback_rect, scale, include_images);
409 staging_buffer->gpu_memory_buffer->Unmap();
410 staging_buffer->content_id = new_content_id;
411 }
412 }
413
414 ContextProvider* context_provider =
415 resource_provider_->output_surface()->worker_context_provider();
416 DCHECK(context_provider);
417
418 {
419 ContextProvider::ScopedContextLock scoped_context(context_provider);
420
421 gpu::gles2::GLES2Interface* gl = scoped_context.ContextGL();
422 DCHECK(gl);
423
424 unsigned image_target = resource_provider_->GetImageTextureTarget(
425 resource_provider_->best_texture_format());
426
427 // Create and bind staging texture.
428 if (!staging_buffer->texture_id) {
429 gl->GenTextures(1, &staging_buffer->texture_id);
430 gl->BindTexture(image_target, staging_buffer->texture_id);
431 gl->TexParameteri(image_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
432 gl->TexParameteri(image_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
433 gl->TexParameteri(image_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
434 gl->TexParameteri(image_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
435 } else {
436 gl->BindTexture(image_target, staging_buffer->texture_id);
437 }
438
439 // Create and bind image.
440 if (!staging_buffer->image_id) {
441 if (staging_buffer->gpu_memory_buffer) {
442 staging_buffer->image_id = gl->CreateImageCHROMIUM(
443 staging_buffer->gpu_memory_buffer->AsClientBuffer(),
444 staging_buffer->size.width(), staging_buffer->size.height(),
445 GLInternalFormat(resource_provider_->best_texture_format()));
446 gl->BindTexImage2DCHROMIUM(image_target, staging_buffer->image_id);
447 }
448 } else {
449 gl->ReleaseTexImage2DCHROMIUM(image_target, staging_buffer->image_id);
450 gl->BindTexImage2DCHROMIUM(image_target, staging_buffer->image_id);
451 }
452
453 // Unbind staging texture.
454 gl->BindTexture(image_target, 0);
455
456 if (resource_provider_->use_sync_query()) {
457 if (!staging_buffer->query_id)
458 gl->GenQueriesEXT(1, &staging_buffer->query_id);
459
460 #if defined(OS_CHROMEOS)
461 // TODO(reveman): This avoids a performance problem on some ChromeOS
462 // devices. This needs to be removed to support native GpuMemoryBuffer
463 // implementations. crbug.com/436314
464 gl->BeginQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM, staging_buffer->query_id);
465 #else
466 gl->BeginQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM,
467 staging_buffer->query_id);
468 #endif
469 }
470
471 int bytes_per_row =
472 (BitsPerPixel(resource_provider_->best_texture_format()) *
473 resource->size().width()) /
474 8;
475 int chunk_size_in_rows =
476 std::max(1, max_bytes_per_copy_operation_ / bytes_per_row);
477 // Align chunk size to 4. Required to support compressed texture formats.
478 chunk_size_in_rows = MathUtil::UncheckedRoundUp(chunk_size_in_rows, 4);
479 int y = 0;
480 int height = resource->size().height();
481 while (y < height) {
482 // Copy at most |chunk_size_in_rows|.
483 int rows_to_copy = std::min(chunk_size_in_rows, height - y);
484 DCHECK_GT(rows_to_copy, 0);
485
486 gl->CopySubTextureCHROMIUM(GL_TEXTURE_2D, staging_buffer->texture_id,
487 resource_lock->texture_id(), 0, y, 0, y,
488 resource->size().width(), rows_to_copy, false,
489 false, false);
490 y += rows_to_copy;
491
492 // Increment |bytes_scheduled_since_last_flush_| by the amount of memory
493 // used for this copy operation.
494 bytes_scheduled_since_last_flush_ += rows_to_copy * bytes_per_row;
495
496 if (bytes_scheduled_since_last_flush_ >= max_bytes_per_copy_operation_) {
497 gl->ShallowFlushCHROMIUM();
498 bytes_scheduled_since_last_flush_ = 0;
499 }
500 }
501
502 if (resource_provider_->use_sync_query()) {
503 #if defined(OS_CHROMEOS)
504 gl->EndQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM);
505 #else
506 gl->EndQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM);
507 #endif
508 }
509
510 // Barrier to sync worker context output to cc context.
511 gl->OrderingBarrierCHROMIUM();
512 }
513
514 staging_buffer->last_usage = base::TimeTicks::Now();
515 busy_buffers_.push_back(staging_buffer.Pass());
516
517 ScheduleReduceMemoryUsage();
518 }
519
520 bool OneCopyTileTaskWorkerPool::OnMemoryDump(
521 const base::trace_event::MemoryDumpArgs& args,
522 base::trace_event::ProcessMemoryDump* pmd) {
365 base::AutoLock lock(lock_); 523 base::AutoLock lock(lock_);
366 524
367 CopySequenceNumber sequence = 0; 525 for (const auto& buffer : buffers_) {
368 int bytes_per_row = (BitsPerPixel(raster_resource->format()) * 526 buffer->OnMemoryDump(pmd, resource_provider_->best_texture_format(),
369 raster_resource->size().width()) / 527 std::find(free_buffers_.begin(), free_buffers_.end(),
370 8; 528 buffer) != free_buffers_.end());
371 int chunk_size_in_rows = 529 }
372 std::max(1, max_bytes_per_copy_operation_ / bytes_per_row); 530
373 // Align chunk size to 4. Required to support compressed texture formats. 531 return true;
374 chunk_size_in_rows = MathUtil::UncheckedRoundUp(chunk_size_in_rows, 4); 532 }
375 int y = 0; 533
376 int height = raster_resource->size().height(); 534 scoped_ptr<OneCopyTileTaskWorkerPool::StagingBuffer>
377 while (y < height) { 535 OneCopyTileTaskWorkerPool::AcquireStagingBuffer(const Resource* resource,
378 int failed_attempts = 0; 536 uint64_t previous_content_id) {
379 while ((pending_copy_operations_.size() + issued_copy_operation_count_) >= 537 lock_.AssertAcquired();
380 kMaxCopyOperations) { 538
381 // Ignore limit when shutdown is set. 539 scoped_ptr<StagingBuffer> staging_buffer;
382 if (shutdown_) 540
541 ContextProvider* context_provider =
542 resource_provider_->output_surface()->worker_context_provider();
543 DCHECK(context_provider);
544
545 ContextProvider::ScopedContextLock scoped_context(context_provider);
546
547 gpu::gles2::GLES2Interface* gl = scoped_context.ContextGL();
548 DCHECK(gl);
549
550 // Check if any busy buffers have become available.
551 if (resource_provider_->use_sync_query()) {
552 while (!busy_buffers_.empty()) {
553 if (!CheckForQueryResult(gl, busy_buffers_.front()->query_id))
383 break; 554 break;
384 555
385 ++failed_attempts; 556 free_buffers_.push_back(busy_buffers_.take_front());
386 557 }
387 // Schedule a check that will also wait for operations to complete 558 }
388 // after too many failed attempts. 559
389 bool wait_if_needed = failed_attempts > kFailedAttemptsBeforeWaitIfNeeded; 560 // Wait for number of non-free buffers to become less than the limit.
390 561 while ((buffers_.size() - free_buffers_.size()) >= max_staging_buffers_) {
391 // Schedule a check for completed copy operations if too many operations 562 // Stop when there are no more busy buffers to wait for.
392 // are currently in-flight. 563 if (busy_buffers_.empty())
393 ScheduleCheckForCompletedCopyOperationsWithLockAcquired(wait_if_needed); 564 break;
394 565
395 { 566 if (resource_provider_->use_sync_query()) {
396 TRACE_EVENT0("cc", "WaitingForCopyOperationsToComplete"); 567 WaitForQueryResult(gl, busy_buffers_.front()->query_id);
397 568 free_buffers_.push_back(busy_buffers_.take_front());
398 // Wait for in-flight copy operations to drop below limit. 569 } else {
399 copy_operation_count_cv_.Wait(); 570 // Fall-back to glFinish if CHROMIUM_sync_query is not available.
400 } 571 gl->Finish();
401 } 572 while (!busy_buffers_.empty())
402 573 free_buffers_.push_back(busy_buffers_.take_front());
403 // There may be more work available, so wake up another worker thread. 574 }
404 copy_operation_count_cv_.Signal(); 575 }
405 576
406 // Copy at most |chunk_size_in_rows|. 577 // Find a staging buffer that allows us to perform partial raster when
407 int rows_to_copy = std::min(chunk_size_in_rows, height - y); 578 // using persistent GpuMemoryBuffers.
408 DCHECK_GT(rows_to_copy, 0); 579 if (use_persistent_gpu_memory_buffers_ && previous_content_id) {
409 580 StagingBufferDeque::iterator it =
410 // |raster_resource_write_lock| is passed to the first copy operation as it 581 std::find_if(free_buffers_.begin(), free_buffers_.end(),
411 // needs to be released before we can issue a copy. 582 [previous_content_id](const StagingBuffer* buffer) {
412 pending_copy_operations_.push_back(make_scoped_ptr(new CopyOperation( 583 return buffer->content_id == previous_content_id;
413 raster_resource_write_lock.Pass(), raster_resource, output_resource, 584 });
414 gfx::Rect(0, y, raster_resource->size().width(), rows_to_copy)))); 585 if (it != free_buffers_.end())
415 y += rows_to_copy; 586 staging_buffer = free_buffers_.take(it);
416 587 }
417 // Acquire a sequence number for this copy operation. 588
418 sequence = next_copy_operation_sequence_++; 589 // Find staging buffer of correct size.
419 590 if (!staging_buffer) {
420 // Increment |bytes_scheduled_since_last_flush_| by the amount of memory 591 StagingBufferDeque::iterator it =
421 // used for this copy operation. 592 std::find_if(free_buffers_.begin(), free_buffers_.end(),
422 bytes_scheduled_since_last_flush_ += rows_to_copy * bytes_per_row; 593 [resource](const StagingBuffer* buffer) {
423 594 return buffer->size == resource->size();
424 // Post task that will advance last flushed copy operation to |sequence| 595 });
425 // when |bytes_scheduled_since_last_flush_| has reached 596 if (it != free_buffers_.end())
426 // |max_bytes_per_copy_operation_|. 597 staging_buffer = free_buffers_.take(it);
427 if (bytes_scheduled_since_last_flush_ >= max_bytes_per_copy_operation_) { 598 }
428 task_runner_->PostTask( 599
429 FROM_HERE, 600 // Create new staging buffer if necessary.
430 base::Bind(&OneCopyTileTaskWorkerPool::AdvanceLastFlushedCopyTo, 601 if (!staging_buffer) {
431 weak_ptr_factory_.GetWeakPtr(), sequence)); 602 staging_buffer = make_scoped_ptr(new StagingBuffer(resource->size()));
432 bytes_scheduled_since_last_flush_ = 0; 603 buffers_.insert(staging_buffer.get());
433 } 604 }
434 } 605
435 606 // Release enough free buffers to stay within the limit.
436 return sequence; 607 while (buffers_.size() > max_staging_buffers_) {
437 } 608 if (free_buffers_.empty())
438 609 break;
439 void OneCopyTileTaskWorkerPool::AdvanceLastIssuedCopyTo( 610
440 CopySequenceNumber sequence) { 611 free_buffers_.front()->DestroyGLResources(gl);
441 if (last_issued_copy_operation_ >= sequence) 612 buffers_.erase(free_buffers_.front());
613 free_buffers_.take_front();
614 }
615
616 return staging_buffer.Pass();
617 }
618
619 base::TimeTicks OneCopyTileTaskWorkerPool::GetUsageTimeForLRUBuffer() {
620 lock_.AssertAcquired();
621
622 if (!free_buffers_.empty())
623 return free_buffers_.front()->last_usage;
624
625 if (!busy_buffers_.empty())
626 return busy_buffers_.front()->last_usage;
627
628 return base::TimeTicks();
629 }
630
631 void OneCopyTileTaskWorkerPool::ScheduleReduceMemoryUsage() {
632 lock_.AssertAcquired();
633
634 if (reduce_memory_usage_pending_)
442 return; 635 return;
443 636
444 IssueCopyOperations(sequence - last_issued_copy_operation_); 637 reduce_memory_usage_pending_ = true;
445 last_issued_copy_operation_ = sequence; 638
446 } 639 // Schedule a call to ReduceMemoryUsage at the time when the LRU buffer
447 640 // should be released.
448 void OneCopyTileTaskWorkerPool::AdvanceLastFlushedCopyTo( 641 base::TimeTicks reduce_memory_usage_time =
449 CopySequenceNumber sequence) { 642 GetUsageTimeForLRUBuffer() + staging_buffer_expiration_delay_;
450 if (last_flushed_copy_operation_ >= sequence) 643 task_runner_->PostDelayedTask(
644 FROM_HERE, reduce_memory_usage_callback_,
645 reduce_memory_usage_time - base::TimeTicks::Now());
646 }
647
648 void OneCopyTileTaskWorkerPool::ReduceMemoryUsage() {
649 base::AutoLock lock(lock_);
650
651 reduce_memory_usage_pending_ = false;
652
653 if (free_buffers_.empty() && busy_buffers_.empty())
451 return; 654 return;
452 655
453 AdvanceLastIssuedCopyTo(sequence); 656 base::TimeTicks current_time = base::TimeTicks::Now();
454 657 ReleaseBuffersNotUsedSince(current_time - staging_buffer_expiration_delay_);
455 // Flush all issued copy operations. 658
456 context_provider_->ContextGL()->ShallowFlushCHROMIUM(); 659 if (free_buffers_.empty() && busy_buffers_.empty())
457 last_flushed_copy_operation_ = last_issued_copy_operation_; 660 return;
661
662 reduce_memory_usage_pending_ = true;
663
664 // Schedule another call to ReduceMemoryUsage at the time when the next
665 // buffer should be released.
666 base::TimeTicks reduce_memory_usage_time =
667 GetUsageTimeForLRUBuffer() + staging_buffer_expiration_delay_;
668 task_runner_->PostDelayedTask(FROM_HERE, reduce_memory_usage_callback_,
669 reduce_memory_usage_time - current_time);
670 }
671
672 void OneCopyTileTaskWorkerPool::ReleaseBuffersNotUsedSince(
673 base::TimeTicks time) {
674 lock_.AssertAcquired();
675
676 ContextProvider* context_provider =
677 resource_provider_->output_surface()->worker_context_provider();
678 DCHECK(context_provider);
679
680 {
681 ContextProvider::ScopedContextLock scoped_context(context_provider);
682
683 gpu::gles2::GLES2Interface* gl = scoped_context.ContextGL();
684 DCHECK(gl);
685
686 // Note: Front buffer is guaranteed to be LRU so we can stop releasing
687 // buffers as soon as we find a buffer that has been used since |time|.
688 while (!free_buffers_.empty()) {
689 if (free_buffers_.front()->last_usage > time)
690 return;
691
692 free_buffers_.front()->DestroyGLResources(gl);
693 buffers_.erase(free_buffers_.front());
694 free_buffers_.take_front();
695 }
696
697 while (!busy_buffers_.empty()) {
698 if (busy_buffers_.front()->last_usage > time)
699 return;
700
701 busy_buffers_.front()->DestroyGLResources(gl);
702 buffers_.erase(busy_buffers_.front());
703 busy_buffers_.take_front();
704 }
705 }
458 } 706 }
459 707
460 void OneCopyTileTaskWorkerPool::OnTaskSetFinished(TaskSet task_set) { 708 void OneCopyTileTaskWorkerPool::OnTaskSetFinished(TaskSet task_set) {
461 TRACE_EVENT1("cc", "OneCopyTileTaskWorkerPool::OnTaskSetFinished", "task_set", 709 TRACE_EVENT1("cc", "OneCopyTileTaskWorkerPool::OnTaskSetFinished", "task_set",
462 task_set); 710 task_set);
463 711
464 DCHECK(tasks_pending_[task_set]); 712 DCHECK(tasks_pending_[task_set]);
465 tasks_pending_[task_set] = false; 713 tasks_pending_[task_set] = false;
466 if (tasks_pending_.any()) { 714 if (tasks_pending_.any()) {
467 TRACE_EVENT_ASYNC_STEP_INTO1("cc", "ScheduledTasks", this, "running", 715 TRACE_EVENT_ASYNC_STEP_INTO1("cc", "ScheduledTasks", this, "running",
468 "state", StateAsValue()); 716 "state", StateAsValue());
469 } else { 717 } else {
470 TRACE_EVENT_ASYNC_END0("cc", "ScheduledTasks", this); 718 TRACE_EVENT_ASYNC_END0("cc", "ScheduledTasks", this);
471 } 719 }
472 client_->DidFinishRunningTileTasks(task_set); 720 client_->DidFinishRunningTileTasks(task_set);
473 } 721 }
474 722
475 void OneCopyTileTaskWorkerPool::IssueCopyOperations(int64 count) {
476 TRACE_EVENT1("cc", "OneCopyTileTaskWorkerPool::IssueCopyOperations", "count",
477 count);
478
479 CopyOperation::Deque copy_operations;
480
481 {
482 base::AutoLock lock(lock_);
483
484 for (int64 i = 0; i < count; ++i) {
485 DCHECK(!pending_copy_operations_.empty());
486 copy_operations.push_back(pending_copy_operations_.take_front());
487 }
488
489 // Increment |issued_copy_operation_count_| to reflect the transition of
490 // copy operations from "pending" to "issued" state.
491 issued_copy_operation_count_ += copy_operations.size();
492 }
493
494 while (!copy_operations.empty()) {
495 scoped_ptr<CopyOperation> copy_operation = copy_operations.take_front();
496
497 // Remove the write lock.
498 copy_operation->src_write_lock.reset();
499
500 // Copy contents of source resource to destination resource.
501 resource_provider_->CopyResource(copy_operation->src->id(),
502 copy_operation->dst->id(),
503 copy_operation->rect);
504 }
505 }
506
507 void OneCopyTileTaskWorkerPool::
508 ScheduleCheckForCompletedCopyOperationsWithLockAcquired(
509 bool wait_if_needed) {
510 lock_.AssertAcquired();
511
512 if (check_for_completed_copy_operations_pending_)
513 return;
514
515 base::TimeTicks now = base::TimeTicks::Now();
516
517 // Schedule a check for completed copy operations as soon as possible but
518 // don't allow two consecutive checks to be scheduled to run less than the
519 // tick rate apart.
520 base::TimeTicks next_check_for_completed_copy_operations_time =
521 std::max(last_check_for_completed_copy_operations_time_ +
522 base::TimeDelta::FromMilliseconds(
523 kCheckForCompletedCopyOperationsTickRateMs),
524 now);
525
526 task_runner_->PostDelayedTask(
527 FROM_HERE,
528 base::Bind(&OneCopyTileTaskWorkerPool::CheckForCompletedCopyOperations,
529 weak_ptr_factory_.GetWeakPtr(), wait_if_needed),
530 next_check_for_completed_copy_operations_time - now);
531
532 last_check_for_completed_copy_operations_time_ =
533 next_check_for_completed_copy_operations_time;
534 check_for_completed_copy_operations_pending_ = true;
535 }
536
537 void OneCopyTileTaskWorkerPool::CheckForCompletedCopyOperations(
538 bool wait_if_needed) {
539 TRACE_EVENT1("cc",
540 "OneCopyTileTaskWorkerPool::CheckForCompletedCopyOperations",
541 "wait_if_needed", wait_if_needed);
542
543 resource_pool_->CheckBusyResources(wait_if_needed);
544
545 {
546 base::AutoLock lock(lock_);
547
548 DCHECK(check_for_completed_copy_operations_pending_);
549 check_for_completed_copy_operations_pending_ = false;
550
551 // The number of busy resources in the pool reflects the number of issued
552 // copy operations that have not yet completed.
553 issued_copy_operation_count_ = resource_pool_->busy_resource_count();
554
555 // There may be work blocked on too many in-flight copy operations, so wake
556 // up a worker thread.
557 copy_operation_count_cv_.Signal();
558 }
559 }
560
561 scoped_refptr<base::trace_event::ConvertableToTraceFormat> 723 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
562 OneCopyTileTaskWorkerPool::StateAsValue() const { 724 OneCopyTileTaskWorkerPool::StateAsValue() const {
563 scoped_refptr<base::trace_event::TracedValue> state = 725 scoped_refptr<base::trace_event::TracedValue> state =
564 new base::trace_event::TracedValue(); 726 new base::trace_event::TracedValue();
565 727
566 state->BeginArray("tasks_pending"); 728 state->BeginArray("tasks_pending");
567 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set) 729 for (TaskSet task_set = 0; task_set < kNumberOfTaskSets; ++task_set)
568 state->AppendBoolean(tasks_pending_[task_set]); 730 state->AppendBoolean(tasks_pending_[task_set]);
569 state->EndArray(); 731 state->EndArray();
570 state->BeginDictionary("staging_state"); 732 state->BeginDictionary("staging_state");
571 StagingStateAsValueInto(state.get()); 733 StagingStateAsValueInto(state.get());
572 state->EndDictionary(); 734 state->EndDictionary();
573 735
574 return state; 736 return state;
575 } 737 }
576 738
577 void OneCopyTileTaskWorkerPool::StagingStateAsValueInto( 739 void OneCopyTileTaskWorkerPool::StagingStateAsValueInto(
578 base::trace_event::TracedValue* staging_state) const { 740 base::trace_event::TracedValue* staging_state) const {
579 staging_state->SetInteger( 741 base::AutoLock lock(lock_);
580 "staging_resource_count", 742
581 static_cast<int>(resource_pool_->total_resource_count())); 743 staging_state->SetInteger("staging_buffer_count",
582 staging_state->SetInteger( 744 static_cast<int>(buffers_.size()));
583 "bytes_used_for_staging_resources", 745 staging_state->SetInteger("busy_count",
584 static_cast<int>(resource_pool_->total_memory_usage_bytes())); 746 static_cast<int>(busy_buffers_.size()));
585 staging_state->SetInteger( 747 staging_state->SetInteger("free_count",
586 "pending_copy_count", 748 static_cast<int>(free_buffers_.size()));
587 static_cast<int>(resource_pool_->total_resource_count() -
588 resource_pool_->acquired_resource_count()));
589 staging_state->SetInteger(
590 "bytes_pending_copy",
591 static_cast<int>(resource_pool_->total_memory_usage_bytes() -
592 resource_pool_->acquired_memory_usage_bytes()));
593 } 749 }
594 750
595 } // namespace cc 751 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698