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 "cc/raster/one_copy_tile_task_worker_pool.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/trace_event/memory_dump_manager.h"
13 #include "base/trace_event/trace_event.h"
14 #include "base/trace_event/trace_event_argument.h"
15 #include "cc/base/math_util.h"
16 #include "cc/debug/traced_value.h"
17 #include "cc/raster/raster_buffer.h"
18 #include "cc/resources/platform_color.h"
19 #include "cc/resources/resource_format.h"
20 #include "cc/resources/resource_util.h"
21 #include "cc/resources/scoped_resource.h"
22 #include "gpu/GLES2/gl2extchromium.h"
23 #include "gpu/command_buffer/client/gles2_interface.h"
24 #include "gpu/command_buffer/client/gpu_memory_buffer_manager.h"
25 #include "ui/gfx/buffer_format_util.h"
30 class RasterBufferImpl
: public RasterBuffer
{
32 RasterBufferImpl(OneCopyTileTaskWorkerPool
* worker_pool
,
33 ResourceProvider
* resource_provider
,
34 ResourceFormat resource_format
,
35 const Resource
* resource
,
36 uint64_t previous_content_id
)
37 : worker_pool_(worker_pool
),
39 lock_(resource_provider
, resource
->id()),
40 previous_content_id_(previous_content_id
) {}
42 ~RasterBufferImpl() override
{}
44 // Overridden from RasterBuffer:
45 void Playback(const RasterSource
* raster_source
,
46 const gfx::Rect
& raster_full_rect
,
47 const gfx::Rect
& raster_dirty_rect
,
48 uint64_t new_content_id
,
50 bool include_images
) override
{
51 worker_pool_
->PlaybackAndCopyOnWorkerThread(
52 resource_
, &lock_
, raster_source
, raster_full_rect
, raster_dirty_rect
,
53 scale
, include_images
, previous_content_id_
, new_content_id
);
57 OneCopyTileTaskWorkerPool
* worker_pool_
;
58 const Resource
* resource_
;
59 ResourceProvider::ScopedWriteLockGL lock_
;
60 uint64_t previous_content_id_
;
62 DISALLOW_COPY_AND_ASSIGN(RasterBufferImpl
);
65 // Delay between checking for query result to be available.
66 const int kCheckForQueryResultAvailableTickRateMs
= 1;
68 // Number of attempts to allow before we perform a check that will wait for
70 const int kMaxCheckForQueryResultAvailableAttempts
= 256;
72 // 4MiB is the size of 4 512x512 tiles, which has proven to be a good
73 // default batch size for copy operations.
74 const int kMaxBytesPerCopyOperation
= 1024 * 1024 * 4;
76 // Delay before a staging buffer might be released.
77 const int kStagingBufferExpirationDelayMs
= 1000;
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
);
85 void WaitForQueryResult(gpu::gles2::GLES2Interface
* gl
, unsigned query_id
) {
86 TRACE_EVENT0("cc", "WaitForQueryResult");
88 int attempts_left
= kMaxCheckForQueryResultAvailableAttempts
;
89 while (attempts_left
--) {
90 if (CheckForQueryResult(gl
, query_id
))
93 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
94 kCheckForQueryResultAvailableTickRateMs
));
98 gl
->GetQueryObjectuivEXT(query_id
, GL_QUERY_RESULT_EXT
, &result
);
103 OneCopyTileTaskWorkerPool::StagingBuffer::StagingBuffer(const gfx::Size
& size
)
104 : size(size
), texture_id(0), image_id(0), query_id(0), content_id(0) {}
106 OneCopyTileTaskWorkerPool::StagingBuffer::~StagingBuffer() {
107 DCHECK_EQ(texture_id
, 0u);
108 DCHECK_EQ(image_id
, 0u);
109 DCHECK_EQ(query_id
, 0u);
112 void OneCopyTileTaskWorkerPool::StagingBuffer::DestroyGLResources(
113 gpu::gles2::GLES2Interface
* gl
) {
115 gl
->DeleteQueriesEXT(1, &query_id
);
119 gl
->DestroyImageCHROMIUM(image_id
);
123 gl
->DeleteTextures(1, &texture_id
);
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
)
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
);
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);
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
);
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
);
165 scoped_ptr
<TileTaskWorkerPool
> OneCopyTileTaskWorkerPool::Create(
166 base::SequencedTaskRunner
* task_runner
,
167 TaskGraphRunner
* task_graph_runner
,
168 ContextProvider
* context_provider
,
169 ResourceProvider
* resource_provider
,
170 int max_copy_texture_chromium_size
,
171 bool use_persistent_gpu_memory_buffers
,
172 int max_staging_buffer_usage_in_bytes
) {
173 return make_scoped_ptr
<TileTaskWorkerPool
>(new OneCopyTileTaskWorkerPool(
174 task_runner
, task_graph_runner
, resource_provider
,
175 max_copy_texture_chromium_size
, use_persistent_gpu_memory_buffers
,
176 max_staging_buffer_usage_in_bytes
));
179 OneCopyTileTaskWorkerPool::OneCopyTileTaskWorkerPool(
180 base::SequencedTaskRunner
* task_runner
,
181 TaskGraphRunner
* task_graph_runner
,
182 ResourceProvider
* resource_provider
,
183 int max_copy_texture_chromium_size
,
184 bool use_persistent_gpu_memory_buffers
,
185 int max_staging_buffer_usage_in_bytes
)
186 : task_runner_(task_runner
),
187 task_graph_runner_(task_graph_runner
),
188 namespace_token_(task_graph_runner
->GetNamespaceToken()),
189 resource_provider_(resource_provider
),
190 max_bytes_per_copy_operation_(
191 max_copy_texture_chromium_size
192 ? std::min(kMaxBytesPerCopyOperation
,
193 max_copy_texture_chromium_size
)
194 : kMaxBytesPerCopyOperation
),
195 use_persistent_gpu_memory_buffers_(use_persistent_gpu_memory_buffers
),
196 bytes_scheduled_since_last_flush_(0),
197 max_staging_buffer_usage_in_bytes_(max_staging_buffer_usage_in_bytes
),
198 staging_buffer_usage_in_bytes_(0),
199 free_staging_buffer_usage_in_bytes_(0),
200 staging_buffer_expiration_delay_(
201 base::TimeDelta::FromMilliseconds(kStagingBufferExpirationDelayMs
)),
202 reduce_memory_usage_pending_(false),
203 weak_ptr_factory_(this),
204 task_set_finished_weak_ptr_factory_(this) {
205 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
206 this, base::ThreadTaskRunnerHandle::Get());
207 reduce_memory_usage_callback_
=
208 base::Bind(&OneCopyTileTaskWorkerPool::ReduceMemoryUsage
,
209 weak_ptr_factory_
.GetWeakPtr());
212 OneCopyTileTaskWorkerPool::~OneCopyTileTaskWorkerPool() {
213 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
217 TileTaskRunner
* OneCopyTileTaskWorkerPool::AsTileTaskRunner() {
221 void OneCopyTileTaskWorkerPool::SetClient(TileTaskRunnerClient
* client
) {
225 void OneCopyTileTaskWorkerPool::Shutdown() {
226 TRACE_EVENT0("cc", "OneCopyTileTaskWorkerPool::Shutdown");
229 task_graph_runner_
->ScheduleTasks(namespace_token_
, &empty
);
230 task_graph_runner_
->WaitForTasksToFinishRunning(namespace_token_
);
232 base::AutoLock
lock(lock_
);
234 if (buffers_
.empty())
237 ReleaseBuffersNotUsedSince(base::TimeTicks() + base::TimeDelta::Max());
238 DCHECK_EQ(staging_buffer_usage_in_bytes_
, 0);
239 DCHECK_EQ(free_staging_buffer_usage_in_bytes_
, 0);
242 void OneCopyTileTaskWorkerPool::ScheduleTasks(TileTaskQueue
* queue
) {
243 TRACE_EVENT0("cc", "OneCopyTileTaskWorkerPool::ScheduleTasks");
245 if (tasks_pending_
.none())
246 TRACE_EVENT_ASYNC_BEGIN0("cc", "ScheduledTasks", this);
248 // Mark all task sets as pending.
249 tasks_pending_
.set();
251 size_t priority
= kTileTaskPriorityBase
;
255 // Cancel existing OnTaskSetFinished callbacks.
256 task_set_finished_weak_ptr_factory_
.InvalidateWeakPtrs();
258 scoped_refptr
<TileTask
> new_task_set_finished_tasks
[kNumberOfTaskSets
];
260 size_t task_count
[kNumberOfTaskSets
] = {0};
262 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
) {
263 new_task_set_finished_tasks
[task_set
] = CreateTaskSetFinishedTask(
265 base::Bind(&OneCopyTileTaskWorkerPool::OnTaskSetFinished
,
266 task_set_finished_weak_ptr_factory_
.GetWeakPtr(), task_set
));
269 for (TileTaskQueue::Item::Vector::const_iterator it
= queue
->items
.begin();
270 it
!= queue
->items
.end(); ++it
) {
271 const TileTaskQueue::Item
& item
= *it
;
272 RasterTask
* task
= item
.task
;
273 DCHECK(!task
->HasCompleted());
275 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
) {
276 if (!item
.task_sets
[task_set
])
279 ++task_count
[task_set
];
281 graph_
.edges
.push_back(
282 TaskGraph::Edge(task
, new_task_set_finished_tasks
[task_set
].get()));
285 InsertNodesForRasterTask(&graph_
, task
, task
->dependencies(), priority
++);
288 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
) {
289 InsertNodeForTask(&graph_
, new_task_set_finished_tasks
[task_set
].get(),
290 kTaskSetFinishedTaskPriorityBase
+ task_set
,
291 task_count
[task_set
]);
294 ScheduleTasksOnOriginThread(this, &graph_
);
296 // Barrier to sync any new resources to the worker context.
297 resource_provider_
->output_surface()
300 ->OrderingBarrierCHROMIUM();
302 task_graph_runner_
->ScheduleTasks(namespace_token_
, &graph_
);
304 std::copy(new_task_set_finished_tasks
,
305 new_task_set_finished_tasks
+ kNumberOfTaskSets
,
306 task_set_finished_tasks_
);
308 TRACE_EVENT_ASYNC_STEP_INTO1("cc", "ScheduledTasks", this, "running", "state",
312 void OneCopyTileTaskWorkerPool::CheckForCompletedTasks() {
313 TRACE_EVENT0("cc", "OneCopyTileTaskWorkerPool::CheckForCompletedTasks");
315 task_graph_runner_
->CollectCompletedTasks(namespace_token_
,
318 for (Task::Vector::const_iterator it
= completed_tasks_
.begin();
319 it
!= completed_tasks_
.end(); ++it
) {
320 TileTask
* task
= static_cast<TileTask
*>(it
->get());
322 task
->WillComplete();
323 task
->CompleteOnOriginThread(this);
326 completed_tasks_
.clear();
329 ResourceFormat
OneCopyTileTaskWorkerPool::GetResourceFormat() const {
330 return resource_provider_
->memory_efficient_texture_format();
333 bool OneCopyTileTaskWorkerPool::GetResourceRequiresSwizzle() const {
334 return !PlatformColor::SameComponentOrder(GetResourceFormat());
337 scoped_ptr
<RasterBuffer
> OneCopyTileTaskWorkerPool::AcquireBufferForRaster(
338 const Resource
* resource
,
339 uint64_t resource_content_id
,
340 uint64_t previous_content_id
) {
341 // TODO(danakj): If resource_content_id != 0, we only need to copy/upload
343 DCHECK_EQ(resource
->format(),
344 resource_provider_
->memory_efficient_texture_format());
345 return make_scoped_ptr
<RasterBuffer
>(new RasterBufferImpl(
346 this, resource_provider_
,
347 resource_provider_
->memory_efficient_texture_format(), resource
,
348 previous_content_id
));
351 void OneCopyTileTaskWorkerPool::ReleaseBufferForRaster(
352 scoped_ptr
<RasterBuffer
> buffer
) {
353 // Nothing to do here. RasterBufferImpl destructor cleans up after itself.
356 void OneCopyTileTaskWorkerPool::PlaybackAndCopyOnWorkerThread(
357 const Resource
* resource
,
358 const ResourceProvider::ScopedWriteLockGL
* resource_lock
,
359 const RasterSource
* raster_source
,
360 const gfx::Rect
& raster_full_rect
,
361 const gfx::Rect
& raster_dirty_rect
,
364 uint64_t previous_content_id
,
365 uint64_t new_content_id
) {
366 base::AutoLock
lock(lock_
);
368 scoped_ptr
<StagingBuffer
> staging_buffer
=
369 AcquireStagingBuffer(resource
, previous_content_id
);
370 DCHECK(staging_buffer
);
373 base::AutoUnlock
unlock(lock_
);
375 // Allocate GpuMemoryBuffer if necessary.
376 if (!staging_buffer
->gpu_memory_buffer
) {
377 staging_buffer
->gpu_memory_buffer
=
378 resource_provider_
->gpu_memory_buffer_manager()
379 ->AllocateGpuMemoryBuffer(
380 staging_buffer
->size
,
382 resource_provider_
->memory_efficient_texture_format()),
383 use_persistent_gpu_memory_buffers_
384 ? gfx::BufferUsage::PERSISTENT_MAP
385 : gfx::BufferUsage::MAP
);
386 DCHECK_EQ(gfx::NumberOfPlanesForBufferFormat(
387 staging_buffer
->gpu_memory_buffer
->GetFormat()),
391 gfx::Rect playback_rect
= raster_full_rect
;
392 if (use_persistent_gpu_memory_buffers_
&& previous_content_id
) {
393 // Reduce playback rect to dirty region if the content id of the staging
394 // buffer matches the prevous content id.
395 if (previous_content_id
== staging_buffer
->content_id
)
396 playback_rect
.Intersect(raster_dirty_rect
);
399 if (staging_buffer
->gpu_memory_buffer
) {
400 void* data
= nullptr;
401 bool rv
= staging_buffer
->gpu_memory_buffer
->Map(&data
);
404 staging_buffer
->gpu_memory_buffer
->GetStride(&stride
);
405 // TileTaskWorkerPool::PlaybackToMemory only supports unsigned strides.
406 DCHECK_GE(stride
, 0);
408 DCHECK(!playback_rect
.IsEmpty())
409 << "Why are we rastering a tile that's not dirty?";
410 TileTaskWorkerPool::PlaybackToMemory(
411 data
, resource_provider_
->memory_efficient_texture_format(),
412 staging_buffer
->size
, static_cast<size_t>(stride
), raster_source
,
413 raster_full_rect
, playback_rect
, scale
, include_images
);
414 staging_buffer
->gpu_memory_buffer
->Unmap();
415 staging_buffer
->content_id
= new_content_id
;
419 ContextProvider
* context_provider
=
420 resource_provider_
->output_surface()->worker_context_provider();
421 DCHECK(context_provider
);
424 ContextProvider::ScopedContextLock
scoped_context(context_provider
);
426 gpu::gles2::GLES2Interface
* gl
= scoped_context
.ContextGL();
429 unsigned image_target
= resource_provider_
->GetImageTextureTarget(
430 resource_provider_
->memory_efficient_texture_format());
432 // Create and bind staging texture.
433 if (!staging_buffer
->texture_id
) {
434 gl
->GenTextures(1, &staging_buffer
->texture_id
);
435 gl
->BindTexture(image_target
, staging_buffer
->texture_id
);
436 gl
->TexParameteri(image_target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
437 gl
->TexParameteri(image_target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
438 gl
->TexParameteri(image_target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
439 gl
->TexParameteri(image_target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
441 gl
->BindTexture(image_target
, staging_buffer
->texture_id
);
444 // Create and bind image.
445 if (!staging_buffer
->image_id
) {
446 if (staging_buffer
->gpu_memory_buffer
) {
447 staging_buffer
->image_id
= gl
->CreateImageCHROMIUM(
448 staging_buffer
->gpu_memory_buffer
->AsClientBuffer(),
449 staging_buffer
->size
.width(), staging_buffer
->size
.height(),
451 resource_provider_
->memory_efficient_texture_format()));
452 gl
->BindTexImage2DCHROMIUM(image_target
, staging_buffer
->image_id
);
455 gl
->ReleaseTexImage2DCHROMIUM(image_target
, staging_buffer
->image_id
);
456 gl
->BindTexImage2DCHROMIUM(image_target
, staging_buffer
->image_id
);
459 // Unbind staging texture.
460 gl
->BindTexture(image_target
, 0);
462 if (resource_provider_
->use_sync_query()) {
463 if (!staging_buffer
->query_id
)
464 gl
->GenQueriesEXT(1, &staging_buffer
->query_id
);
466 #if defined(OS_CHROMEOS)
467 // TODO(reveman): This avoids a performance problem on some ChromeOS
468 // devices. This needs to be removed to support native GpuMemoryBuffer
469 // implementations. crbug.com/436314
470 gl
->BeginQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM
, staging_buffer
->query_id
);
472 gl
->BeginQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM
,
473 staging_buffer
->query_id
);
478 (BitsPerPixel(resource_provider_
->memory_efficient_texture_format()) *
479 resource
->size().width()) /
481 int chunk_size_in_rows
=
482 std::max(1, max_bytes_per_copy_operation_
/ bytes_per_row
);
483 // Align chunk size to 4. Required to support compressed texture formats.
484 chunk_size_in_rows
= MathUtil::UncheckedRoundUp(chunk_size_in_rows
, 4);
486 int height
= resource
->size().height();
488 // Copy at most |chunk_size_in_rows|.
489 int rows_to_copy
= std::min(chunk_size_in_rows
, height
- y
);
490 DCHECK_GT(rows_to_copy
, 0);
492 gl
->CopySubTextureCHROMIUM(GL_TEXTURE_2D
, staging_buffer
->texture_id
,
493 resource_lock
->texture_id(), 0, y
, 0, y
,
494 resource
->size().width(), rows_to_copy
, false,
498 // Increment |bytes_scheduled_since_last_flush_| by the amount of memory
499 // used for this copy operation.
500 bytes_scheduled_since_last_flush_
+= rows_to_copy
* bytes_per_row
;
502 if (bytes_scheduled_since_last_flush_
>= max_bytes_per_copy_operation_
) {
503 gl
->ShallowFlushCHROMIUM();
504 bytes_scheduled_since_last_flush_
= 0;
508 if (resource_provider_
->use_sync_query()) {
509 #if defined(OS_CHROMEOS)
510 gl
->EndQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM
);
512 gl
->EndQueryEXT(GL_COMMANDS_COMPLETED_CHROMIUM
);
516 // Barrier to sync worker context output to cc context.
517 gl
->OrderingBarrierCHROMIUM();
520 staging_buffer
->last_usage
= base::TimeTicks::Now();
521 busy_buffers_
.push_back(staging_buffer
.Pass());
523 ScheduleReduceMemoryUsage();
526 bool OneCopyTileTaskWorkerPool::OnMemoryDump(
527 const base::trace_event::MemoryDumpArgs
& args
,
528 base::trace_event::ProcessMemoryDump
* pmd
) {
529 base::AutoLock
lock(lock_
);
531 for (const auto& buffer
: buffers_
) {
532 buffer
->OnMemoryDump(pmd
,
533 resource_provider_
->memory_efficient_texture_format(),
534 std::find(free_buffers_
.begin(), free_buffers_
.end(),
535 buffer
) != free_buffers_
.end());
541 void OneCopyTileTaskWorkerPool::AddStagingBuffer(
542 const StagingBuffer
* staging_buffer
) {
543 lock_
.AssertAcquired();
545 DCHECK(buffers_
.find(staging_buffer
) == buffers_
.end());
546 buffers_
.insert(staging_buffer
);
547 int buffer_usage_in_bytes
= ResourceUtil::UncheckedSizeInBytes
<int>(
548 staging_buffer
->size
,
549 resource_provider_
->memory_efficient_texture_format());
550 staging_buffer_usage_in_bytes_
+= buffer_usage_in_bytes
;
553 void OneCopyTileTaskWorkerPool::RemoveStagingBuffer(
554 const StagingBuffer
* staging_buffer
) {
555 lock_
.AssertAcquired();
557 DCHECK(buffers_
.find(staging_buffer
) != buffers_
.end());
558 buffers_
.erase(staging_buffer
);
559 int buffer_usage_in_bytes
= ResourceUtil::UncheckedSizeInBytes
<int>(
560 staging_buffer
->size
,
561 resource_provider_
->memory_efficient_texture_format());
562 DCHECK_GE(staging_buffer_usage_in_bytes_
, buffer_usage_in_bytes
);
563 staging_buffer_usage_in_bytes_
-= buffer_usage_in_bytes
;
566 void OneCopyTileTaskWorkerPool::MarkStagingBufferAsFree(
567 const StagingBuffer
* staging_buffer
) {
568 lock_
.AssertAcquired();
570 int buffer_usage_in_bytes
= ResourceUtil::UncheckedSizeInBytes
<int>(
571 staging_buffer
->size
,
572 resource_provider_
->memory_efficient_texture_format());
573 free_staging_buffer_usage_in_bytes_
+= buffer_usage_in_bytes
;
576 void OneCopyTileTaskWorkerPool::MarkStagingBufferAsBusy(
577 const StagingBuffer
* staging_buffer
) {
578 lock_
.AssertAcquired();
580 int buffer_usage_in_bytes
= ResourceUtil::UncheckedSizeInBytes
<int>(
581 staging_buffer
->size
,
582 resource_provider_
->memory_efficient_texture_format());
583 DCHECK_GE(free_staging_buffer_usage_in_bytes_
, buffer_usage_in_bytes
);
584 free_staging_buffer_usage_in_bytes_
-= buffer_usage_in_bytes
;
587 scoped_ptr
<OneCopyTileTaskWorkerPool::StagingBuffer
>
588 OneCopyTileTaskWorkerPool::AcquireStagingBuffer(const Resource
* resource
,
589 uint64_t previous_content_id
) {
590 lock_
.AssertAcquired();
592 scoped_ptr
<StagingBuffer
> staging_buffer
;
594 ContextProvider
* context_provider
=
595 resource_provider_
->output_surface()->worker_context_provider();
596 DCHECK(context_provider
);
598 ContextProvider::ScopedContextLock
scoped_context(context_provider
);
600 gpu::gles2::GLES2Interface
* gl
= scoped_context
.ContextGL();
603 // Check if any busy buffers have become available.
604 if (resource_provider_
->use_sync_query()) {
605 while (!busy_buffers_
.empty()) {
606 if (!CheckForQueryResult(gl
, busy_buffers_
.front()->query_id
))
609 MarkStagingBufferAsFree(busy_buffers_
.front());
610 free_buffers_
.push_back(busy_buffers_
.take_front());
614 // Wait for memory usage of non-free buffers to become less than the limit.
616 (staging_buffer_usage_in_bytes_
- free_staging_buffer_usage_in_bytes_
) >=
617 max_staging_buffer_usage_in_bytes_
) {
618 // Stop when there are no more busy buffers to wait for.
619 if (busy_buffers_
.empty())
622 if (resource_provider_
->use_sync_query()) {
623 WaitForQueryResult(gl
, busy_buffers_
.front()->query_id
);
624 MarkStagingBufferAsFree(busy_buffers_
.front());
625 free_buffers_
.push_back(busy_buffers_
.take_front());
627 // Fall-back to glFinish if CHROMIUM_sync_query is not available.
629 while (!busy_buffers_
.empty()) {
630 MarkStagingBufferAsFree(busy_buffers_
.front());
631 free_buffers_
.push_back(busy_buffers_
.take_front());
636 // Find a staging buffer that allows us to perform partial raster when
637 // using persistent GpuMemoryBuffers.
638 if (use_persistent_gpu_memory_buffers_
&& previous_content_id
) {
639 StagingBufferDeque::iterator it
=
640 std::find_if(free_buffers_
.begin(), free_buffers_
.end(),
641 [previous_content_id
](const StagingBuffer
* buffer
) {
642 return buffer
->content_id
== previous_content_id
;
644 if (it
!= free_buffers_
.end()) {
645 staging_buffer
= free_buffers_
.take(it
);
646 MarkStagingBufferAsBusy(staging_buffer
.get());
650 // Find staging buffer of correct size.
651 if (!staging_buffer
) {
652 StagingBufferDeque::iterator it
=
653 std::find_if(free_buffers_
.begin(), free_buffers_
.end(),
654 [resource
](const StagingBuffer
* buffer
) {
655 return buffer
->size
== resource
->size();
657 if (it
!= free_buffers_
.end()) {
658 staging_buffer
= free_buffers_
.take(it
);
659 MarkStagingBufferAsBusy(staging_buffer
.get());
663 // Create new staging buffer if necessary.
664 if (!staging_buffer
) {
665 staging_buffer
= make_scoped_ptr(new StagingBuffer(resource
->size()));
666 AddStagingBuffer(staging_buffer
.get());
669 // Release enough free buffers to stay within the limit.
670 while (staging_buffer_usage_in_bytes_
> max_staging_buffer_usage_in_bytes_
) {
671 if (free_buffers_
.empty())
674 free_buffers_
.front()->DestroyGLResources(gl
);
675 MarkStagingBufferAsBusy(free_buffers_
.front());
676 RemoveStagingBuffer(free_buffers_
.front());
677 free_buffers_
.take_front();
680 return staging_buffer
.Pass();
683 base::TimeTicks
OneCopyTileTaskWorkerPool::GetUsageTimeForLRUBuffer() {
684 lock_
.AssertAcquired();
686 if (!free_buffers_
.empty())
687 return free_buffers_
.front()->last_usage
;
689 if (!busy_buffers_
.empty())
690 return busy_buffers_
.front()->last_usage
;
692 return base::TimeTicks();
695 void OneCopyTileTaskWorkerPool::ScheduleReduceMemoryUsage() {
696 lock_
.AssertAcquired();
698 if (reduce_memory_usage_pending_
)
701 reduce_memory_usage_pending_
= true;
703 // Schedule a call to ReduceMemoryUsage at the time when the LRU buffer
704 // should be released.
705 base::TimeTicks reduce_memory_usage_time
=
706 GetUsageTimeForLRUBuffer() + staging_buffer_expiration_delay_
;
707 task_runner_
->PostDelayedTask(
708 FROM_HERE
, reduce_memory_usage_callback_
,
709 reduce_memory_usage_time
- base::TimeTicks::Now());
712 void OneCopyTileTaskWorkerPool::ReduceMemoryUsage() {
713 base::AutoLock
lock(lock_
);
715 reduce_memory_usage_pending_
= false;
717 if (free_buffers_
.empty() && busy_buffers_
.empty())
720 base::TimeTicks current_time
= base::TimeTicks::Now();
721 ReleaseBuffersNotUsedSince(current_time
- staging_buffer_expiration_delay_
);
723 if (free_buffers_
.empty() && busy_buffers_
.empty())
726 reduce_memory_usage_pending_
= true;
728 // Schedule another call to ReduceMemoryUsage at the time when the next
729 // buffer should be released.
730 base::TimeTicks reduce_memory_usage_time
=
731 GetUsageTimeForLRUBuffer() + staging_buffer_expiration_delay_
;
732 task_runner_
->PostDelayedTask(FROM_HERE
, reduce_memory_usage_callback_
,
733 reduce_memory_usage_time
- current_time
);
736 void OneCopyTileTaskWorkerPool::ReleaseBuffersNotUsedSince(
737 base::TimeTicks time
) {
738 lock_
.AssertAcquired();
740 ContextProvider
* context_provider
=
741 resource_provider_
->output_surface()->worker_context_provider();
742 DCHECK(context_provider
);
745 ContextProvider::ScopedContextLock
scoped_context(context_provider
);
747 gpu::gles2::GLES2Interface
* gl
= scoped_context
.ContextGL();
750 // Note: Front buffer is guaranteed to be LRU so we can stop releasing
751 // buffers as soon as we find a buffer that has been used since |time|.
752 while (!free_buffers_
.empty()) {
753 if (free_buffers_
.front()->last_usage
> time
)
756 free_buffers_
.front()->DestroyGLResources(gl
);
757 MarkStagingBufferAsBusy(free_buffers_
.front());
758 RemoveStagingBuffer(free_buffers_
.front());
759 free_buffers_
.take_front();
762 while (!busy_buffers_
.empty()) {
763 if (busy_buffers_
.front()->last_usage
> time
)
766 busy_buffers_
.front()->DestroyGLResources(gl
);
767 RemoveStagingBuffer(busy_buffers_
.front());
768 busy_buffers_
.take_front();
773 void OneCopyTileTaskWorkerPool::OnTaskSetFinished(TaskSet task_set
) {
774 TRACE_EVENT1("cc", "OneCopyTileTaskWorkerPool::OnTaskSetFinished", "task_set",
777 DCHECK(tasks_pending_
[task_set
]);
778 tasks_pending_
[task_set
] = false;
779 if (tasks_pending_
.any()) {
780 TRACE_EVENT_ASYNC_STEP_INTO1("cc", "ScheduledTasks", this, "running",
781 "state", StateAsValue());
783 TRACE_EVENT_ASYNC_END0("cc", "ScheduledTasks", this);
785 client_
->DidFinishRunningTileTasks(task_set
);
788 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
789 OneCopyTileTaskWorkerPool::StateAsValue() const {
790 scoped_refptr
<base::trace_event::TracedValue
> state
=
791 new base::trace_event::TracedValue();
793 state
->BeginArray("tasks_pending");
794 for (TaskSet task_set
= 0; task_set
< kNumberOfTaskSets
; ++task_set
)
795 state
->AppendBoolean(tasks_pending_
[task_set
]);
797 state
->BeginDictionary("staging_state");
798 StagingStateAsValueInto(state
.get());
799 state
->EndDictionary();
804 void OneCopyTileTaskWorkerPool::StagingStateAsValueInto(
805 base::trace_event::TracedValue
* staging_state
) const {
806 base::AutoLock
lock(lock_
);
808 staging_state
->SetInteger("staging_buffer_count",
809 static_cast<int>(buffers_
.size()));
810 staging_state
->SetInteger("busy_count",
811 static_cast<int>(busy_buffers_
.size()));
812 staging_state
->SetInteger("free_count",
813 static_cast<int>(free_buffers_
.size()));