OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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/resources/tile_manager.h" | 5 #include "cc/resources/tile_manager.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 skia::RefPtr<SkPixelRef> pixel_ref_; | 218 skia::RefPtr<SkPixelRef> pixel_ref_; |
219 int layer_id_; | 219 int layer_id_; |
220 RenderingStatsInstrumentation* rendering_stats_; | 220 RenderingStatsInstrumentation* rendering_stats_; |
221 const base::Callback<void(bool was_canceled)> reply_; | 221 const base::Callback<void(bool was_canceled)> reply_; |
222 | 222 |
223 DISALLOW_COPY_AND_ASSIGN(ImageDecodeTaskImpl); | 223 DISALLOW_COPY_AND_ASSIGN(ImageDecodeTaskImpl); |
224 }; | 224 }; |
225 | 225 |
226 const size_t kScheduledRasterTasksLimit = 32u; | 226 const size_t kScheduledRasterTasksLimit = 32u; |
227 | 227 |
228 // Memory limit policy works by mapping some bin states to the NEVER bin. | |
229 const ManagedTileBin kBinPolicyMap[NUM_TILE_MEMORY_LIMIT_POLICIES][NUM_BINS] = { | |
230 // [ALLOW_NOTHING] | |
231 {NEVER_BIN, // [NOW_AND_READY_TO_DRAW_BIN] | |
232 NEVER_BIN, // [NOW_BIN] | |
233 NEVER_BIN, // [SOON_BIN] | |
234 NEVER_BIN, // [EVENTUALLY_AND_ACTIVE_BIN] | |
235 NEVER_BIN, // [EVENTUALLY_BIN] | |
236 NEVER_BIN, // [AT_LAST_AND_ACTIVE_BIN] | |
237 NEVER_BIN, // [AT_LAST_BIN] | |
238 NEVER_BIN // [NEVER_BIN] | |
239 }, | |
240 // [ALLOW_ABSOLUTE_MINIMUM] | |
241 {NOW_AND_READY_TO_DRAW_BIN, // [NOW_AND_READY_TO_DRAW_BIN] | |
242 NOW_BIN, // [NOW_BIN] | |
243 NEVER_BIN, // [SOON_BIN] | |
244 NEVER_BIN, // [EVENTUALLY_AND_ACTIVE_BIN] | |
245 NEVER_BIN, // [EVENTUALLY_BIN] | |
246 NEVER_BIN, // [AT_LAST_AND_ACTIVE_BIN] | |
247 NEVER_BIN, // [AT_LAST_BIN] | |
248 NEVER_BIN // [NEVER_BIN] | |
249 }, | |
250 // [ALLOW_PREPAINT_ONLY] | |
251 {NOW_AND_READY_TO_DRAW_BIN, // [NOW_AND_READY_TO_DRAW_BIN] | |
252 NOW_BIN, // [NOW_BIN] | |
253 SOON_BIN, // [SOON_BIN] | |
254 NEVER_BIN, // [EVENTUALLY_AND_ACTIVE_BIN] | |
255 NEVER_BIN, // [EVENTUALLY_BIN] | |
256 NEVER_BIN, // [AT_LAST_AND_ACTIVE_BIN] | |
257 NEVER_BIN, // [AT_LAST_BIN] | |
258 NEVER_BIN // [NEVER_BIN] | |
259 }, | |
260 // [ALLOW_ANYTHING] | |
261 {NOW_AND_READY_TO_DRAW_BIN, // [NOW_AND_READY_TO_DRAW_BIN] | |
262 NOW_BIN, // [NOW_BIN] | |
263 SOON_BIN, // [SOON_BIN] | |
264 EVENTUALLY_AND_ACTIVE_BIN, // [EVENTUALLY_AND_ACTIVE_BIN] | |
265 EVENTUALLY_BIN, // [EVENTUALLY_BIN] | |
266 AT_LAST_AND_ACTIVE_BIN, // [AT_LAST_AND_ACTIVE_BIN] | |
267 AT_LAST_BIN, // [AT_LAST_BIN] | |
268 NEVER_BIN // [NEVER_BIN] | |
269 }}; | |
270 | |
271 // Ready to draw works by mapping NOW_BIN to NOW_AND_READY_TO_DRAW_BIN. | |
272 const ManagedTileBin kBinReadyToDrawMap[2][NUM_BINS] = { | |
273 // Not ready | |
274 {NOW_AND_READY_TO_DRAW_BIN, // [NOW_AND_READY_TO_DRAW_BIN] | |
275 NOW_BIN, // [NOW_BIN] | |
276 SOON_BIN, // [SOON_BIN] | |
277 EVENTUALLY_AND_ACTIVE_BIN, // [EVENTUALLY_AND_ACTIVE_BIN] | |
278 EVENTUALLY_BIN, // [EVENTUALLY_BIN] | |
279 AT_LAST_AND_ACTIVE_BIN, // [AT_LAST_AND_ACTIVE_BIN] | |
280 AT_LAST_BIN, // [AT_LAST_BIN] | |
281 NEVER_BIN // [NEVER_BIN] | |
282 }, | |
283 // Ready | |
284 {NOW_AND_READY_TO_DRAW_BIN, // [NOW_AND_READY_TO_DRAW_BIN] | |
285 NOW_AND_READY_TO_DRAW_BIN, // [NOW_BIN] | |
286 SOON_BIN, // [SOON_BIN] | |
287 EVENTUALLY_AND_ACTIVE_BIN, // [EVENTUALLY_AND_ACTIVE_BIN] | |
288 EVENTUALLY_BIN, // [EVENTUALLY_BIN] | |
289 AT_LAST_AND_ACTIVE_BIN, // [AT_LAST_AND_ACTIVE_BIN] | |
290 AT_LAST_BIN, // [AT_LAST_BIN] | |
291 NEVER_BIN // [NEVER_BIN] | |
292 }}; | |
293 | |
294 // Active works by mapping some bin stats to equivalent _ACTIVE_BIN state. | |
295 const ManagedTileBin kBinIsActiveMap[2][NUM_BINS] = { | |
296 // Inactive | |
297 {NOW_AND_READY_TO_DRAW_BIN, // [NOW_AND_READY_TO_DRAW_BIN] | |
298 NOW_BIN, // [NOW_BIN] | |
299 SOON_BIN, // [SOON_BIN] | |
300 EVENTUALLY_AND_ACTIVE_BIN, // [EVENTUALLY_AND_ACTIVE_BIN] | |
301 EVENTUALLY_BIN, // [EVENTUALLY_BIN] | |
302 AT_LAST_AND_ACTIVE_BIN, // [AT_LAST_AND_ACTIVE_BIN] | |
303 AT_LAST_BIN, // [AT_LAST_BIN] | |
304 NEVER_BIN // [NEVER_BIN] | |
305 }, | |
306 // Active | |
307 {NOW_AND_READY_TO_DRAW_BIN, // [NOW_AND_READY_TO_DRAW_BIN] | |
308 NOW_BIN, // [NOW_BIN] | |
309 SOON_BIN, // [SOON_BIN] | |
310 EVENTUALLY_AND_ACTIVE_BIN, // [EVENTUALLY_AND_ACTIVE_BIN] | |
311 EVENTUALLY_AND_ACTIVE_BIN, // [EVENTUALLY_BIN] | |
312 AT_LAST_AND_ACTIVE_BIN, // [AT_LAST_AND_ACTIVE_BIN] | |
313 AT_LAST_AND_ACTIVE_BIN, // [AT_LAST_BIN] | |
314 NEVER_BIN // [NEVER_BIN] | |
315 }}; | |
316 | |
317 // Determine bin based on three categories of tiles: things we need now, | |
318 // things we need soon, and eventually. | |
319 inline ManagedTileBin BinFromTilePriority(const TilePriority& prio) { | |
320 if (prio.priority_bin == TilePriority::NOW) | |
321 return NOW_BIN; | |
322 | |
323 if (prio.priority_bin == TilePriority::SOON) | |
324 return SOON_BIN; | |
325 | |
326 if (prio.distance_to_visible == std::numeric_limits<float>::infinity()) | |
327 return NEVER_BIN; | |
328 | |
329 return EVENTUALLY_BIN; | |
330 } | |
331 | |
332 } // namespace | 228 } // namespace |
333 | 229 |
334 RasterTaskCompletionStats::RasterTaskCompletionStats() | 230 RasterTaskCompletionStats::RasterTaskCompletionStats() |
335 : completed_count(0u), canceled_count(0u) {} | 231 : completed_count(0u), canceled_count(0u) {} |
336 | 232 |
337 scoped_refptr<base::debug::ConvertableToTraceFormat> | 233 scoped_refptr<base::debug::ConvertableToTraceFormat> |
338 RasterTaskCompletionStatsAsValue(const RasterTaskCompletionStats& stats) { | 234 RasterTaskCompletionStatsAsValue(const RasterTaskCompletionStats& stats) { |
339 scoped_refptr<base::debug::TracedValue> state = | 235 scoped_refptr<base::debug::TracedValue> state = |
340 new base::debug::TracedValue(); | 236 new base::debug::TracedValue(); |
341 state->SetInteger("completed_count", stats.completed_count); | 237 state->SetInteger("completed_count", stats.completed_count); |
(...skipping 18 matching lines...) Expand all Loading... |
360 TileManager::TileManager( | 256 TileManager::TileManager( |
361 TileManagerClient* client, | 257 TileManagerClient* client, |
362 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | 258 const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
363 ResourcePool* resource_pool, | 259 ResourcePool* resource_pool, |
364 Rasterizer* rasterizer, | 260 Rasterizer* rasterizer, |
365 RenderingStatsInstrumentation* rendering_stats_instrumentation) | 261 RenderingStatsInstrumentation* rendering_stats_instrumentation) |
366 : client_(client), | 262 : client_(client), |
367 task_runner_(task_runner), | 263 task_runner_(task_runner), |
368 resource_pool_(resource_pool), | 264 resource_pool_(resource_pool), |
369 rasterizer_(rasterizer), | 265 rasterizer_(rasterizer), |
370 prioritized_tiles_dirty_(false), | 266 all_tiles_that_need_to_be_rasterized_are_scheduled_(true), |
371 all_tiles_that_need_to_be_rasterized_have_memory_(true), | |
372 all_tiles_required_for_activation_have_memory_(true), | |
373 bytes_releasable_(0), | |
374 resources_releasable_(0), | |
375 ever_exceeded_memory_budget_(false), | |
376 rendering_stats_instrumentation_(rendering_stats_instrumentation), | 267 rendering_stats_instrumentation_(rendering_stats_instrumentation), |
377 did_initialize_visible_tile_(false), | 268 did_initialize_visible_tile_(false), |
378 did_check_for_completed_tasks_since_last_schedule_tasks_(true), | 269 did_check_for_completed_tasks_since_last_schedule_tasks_(true), |
379 did_oom_on_last_assign_(false), | 270 did_oom_on_last_assign_(false), |
380 ready_to_activate_check_notifier_( | 271 ready_to_activate_check_notifier_( |
381 task_runner_.get(), | 272 task_runner_.get(), |
382 base::Bind(&TileManager::CheckIfReadyToActivate, | 273 base::Bind(&TileManager::CheckIfReadyToActivate, |
383 base::Unretained(this))) { | 274 base::Unretained(this))) { |
384 rasterizer_->SetClient(this); | 275 rasterizer_->SetClient(this); |
385 } | 276 } |
386 | 277 |
387 TileManager::~TileManager() { | 278 TileManager::~TileManager() { |
388 // Reset global state and manage. This should cause | 279 // Reset global state and manage. This should cause |
389 // our memory usage to drop to zero. | 280 // our memory usage to drop to zero. |
390 global_state_ = GlobalStateThatImpactsTilePriority(); | 281 global_state_ = GlobalStateThatImpactsTilePriority(); |
391 | 282 |
392 RasterTaskQueue empty; | 283 RasterTaskQueue empty; |
393 rasterizer_->ScheduleTasks(&empty); | 284 rasterizer_->ScheduleTasks(&empty); |
394 orphan_raster_tasks_.clear(); | 285 orphan_raster_tasks_.clear(); |
395 | 286 |
396 // This should finish all pending tasks and release any uninitialized | 287 // This should finish all pending tasks and release any uninitialized |
397 // resources. | 288 // resources. |
398 rasterizer_->Shutdown(); | 289 rasterizer_->Shutdown(); |
399 rasterizer_->CheckForCompletedTasks(); | 290 rasterizer_->CheckForCompletedTasks(); |
400 | 291 |
401 prioritized_tiles_.Clear(); | |
402 | |
403 FreeResourcesForReleasedTiles(); | 292 FreeResourcesForReleasedTiles(); |
404 CleanUpReleasedTiles(); | 293 CleanUpReleasedTiles(); |
405 | |
406 DCHECK_EQ(0u, bytes_releasable_); | |
407 DCHECK_EQ(0u, resources_releasable_); | |
408 } | 294 } |
409 | 295 |
410 void TileManager::Release(Tile* tile) { | 296 void TileManager::Release(Tile* tile) { |
411 DCHECK(TilePriority() == tile->combined_priority()); | |
412 | |
413 prioritized_tiles_dirty_ = true; | |
414 released_tiles_.push_back(tile); | 297 released_tiles_.push_back(tile); |
415 } | 298 } |
416 | 299 |
417 void TileManager::DidChangeTilePriority(Tile* tile) { | |
418 prioritized_tiles_dirty_ = true; | |
419 } | |
420 | |
421 TaskSetCollection TileManager::TasksThatShouldBeForcedToComplete() const { | 300 TaskSetCollection TileManager::TasksThatShouldBeForcedToComplete() const { |
422 TaskSetCollection tasks_that_should_be_forced_to_complete; | 301 TaskSetCollection tasks_that_should_be_forced_to_complete; |
423 if (global_state_.tree_priority != SMOOTHNESS_TAKES_PRIORITY) | 302 if (global_state_.tree_priority != SMOOTHNESS_TAKES_PRIORITY) |
424 tasks_that_should_be_forced_to_complete[REQUIRED_FOR_ACTIVATION] = true; | 303 tasks_that_should_be_forced_to_complete[REQUIRED_FOR_ACTIVATION] = true; |
425 return tasks_that_should_be_forced_to_complete; | 304 return tasks_that_should_be_forced_to_complete; |
426 } | 305 } |
427 | 306 |
428 void TileManager::FreeResourcesForReleasedTiles() { | 307 void TileManager::FreeResourcesForReleasedTiles() { |
429 for (std::vector<Tile*>::iterator it = released_tiles_.begin(); | 308 for (std::vector<Tile*>::iterator it = released_tiles_.begin(); |
430 it != released_tiles_.end(); | 309 it != released_tiles_.end(); |
431 ++it) { | 310 ++it) { |
432 Tile* tile = *it; | 311 Tile* tile = *it; |
433 FreeResourcesForTile(tile); | 312 FreeResourcesForTile(tile); |
434 } | 313 } |
435 } | 314 } |
436 | 315 |
437 void TileManager::CleanUpReleasedTiles() { | 316 void TileManager::CleanUpReleasedTiles() { |
438 // Make sure |prioritized_tiles_| doesn't contain any of the tiles | |
439 // we're about to delete. | |
440 DCHECK(prioritized_tiles_.IsEmpty()); | |
441 | |
442 std::vector<Tile*>::iterator it = released_tiles_.begin(); | 317 std::vector<Tile*>::iterator it = released_tiles_.begin(); |
443 while (it != released_tiles_.end()) { | 318 while (it != released_tiles_.end()) { |
444 Tile* tile = *it; | 319 Tile* tile = *it; |
445 | 320 |
446 if (tile->HasRasterTask()) { | 321 if (tile->HasRasterTask()) { |
447 ++it; | 322 ++it; |
448 continue; | 323 continue; |
449 } | 324 } |
450 | 325 |
451 DCHECK(!tile->HasResources()); | 326 DCHECK(!tile->HasResources()); |
452 DCHECK(tiles_.find(tile->id()) != tiles_.end()); | 327 DCHECK(tiles_.find(tile->id()) != tiles_.end()); |
453 tiles_.erase(tile->id()); | 328 tiles_.erase(tile->id()); |
454 | 329 |
455 LayerCountMap::iterator layer_it = | 330 LayerCountMap::iterator layer_it = |
456 used_layer_counts_.find(tile->layer_id()); | 331 used_layer_counts_.find(tile->layer_id()); |
457 DCHECK_GT(layer_it->second, 0); | 332 DCHECK_GT(layer_it->second, 0); |
458 if (--layer_it->second == 0) { | 333 if (--layer_it->second == 0) { |
459 used_layer_counts_.erase(layer_it); | 334 used_layer_counts_.erase(layer_it); |
460 image_decode_tasks_.erase(tile->layer_id()); | 335 image_decode_tasks_.erase(tile->layer_id()); |
461 } | 336 } |
462 | 337 |
463 delete tile; | 338 delete tile; |
464 it = released_tiles_.erase(it); | 339 it = released_tiles_.erase(it); |
465 } | 340 } |
466 } | 341 } |
467 | 342 |
468 void TileManager::UpdatePrioritizedTileSetIfNeeded() { | |
469 if (!prioritized_tiles_dirty_) | |
470 return; | |
471 | |
472 prioritized_tiles_.Clear(); | |
473 | |
474 FreeResourcesForReleasedTiles(); | |
475 CleanUpReleasedTiles(); | |
476 | |
477 GetTilesWithAssignedBins(&prioritized_tiles_); | |
478 prioritized_tiles_dirty_ = false; | |
479 } | |
480 | |
481 void TileManager::DidFinishRunningTasks(TaskSet task_set) { | 343 void TileManager::DidFinishRunningTasks(TaskSet task_set) { |
482 if (task_set == ALL) { | 344 if (task_set == ALL) { |
483 TRACE_EVENT1("cc", "TileManager::DidFinishRunningTasks", "task_set", "ALL"); | 345 TRACE_EVENT1("cc", "TileManager::DidFinishRunningTasks", "task_set", "ALL"); |
484 | 346 |
485 bool memory_usage_above_limit = resource_pool_->total_memory_usage_bytes() > | 347 bool memory_usage_above_limit = resource_pool_->total_memory_usage_bytes() > |
486 global_state_.soft_memory_limit_in_bytes; | 348 global_state_.soft_memory_limit_in_bytes; |
487 | 349 |
488 // When OOM, keep re-assigning memory until we reach a steady state | 350 // When OOM, keep re-assigning memory until we reach a steady state |
489 // where top-priority tiles are initialized. | 351 // where top-priority tiles are initialized. |
490 if (all_tiles_that_need_to_be_rasterized_have_memory_ && | 352 if (all_tiles_that_need_to_be_rasterized_are_scheduled_ && |
491 !memory_usage_above_limit) | 353 !memory_usage_above_limit) |
492 return; | 354 return; |
493 | 355 |
494 rasterizer_->CheckForCompletedTasks(); | 356 rasterizer_->CheckForCompletedTasks(); |
495 did_check_for_completed_tasks_since_last_schedule_tasks_ = true; | 357 did_check_for_completed_tasks_since_last_schedule_tasks_ = true; |
496 | 358 |
497 TileVector tiles_that_need_to_be_rasterized; | 359 TileVector tiles_that_need_to_be_rasterized; |
498 AssignGpuMemoryToTiles(&prioritized_tiles_, | 360 AssignGpuMemoryToTiles(&tiles_that_need_to_be_rasterized); |
499 &tiles_that_need_to_be_rasterized); | |
500 | 361 |
501 // |tiles_that_need_to_be_rasterized| will be empty when we reach a | 362 // |tiles_that_need_to_be_rasterized| will be empty when we reach a |
502 // steady memory state. Keep scheduling tasks until we reach this state. | 363 // steady memory state. Keep scheduling tasks until we reach this state. |
503 if (!tiles_that_need_to_be_rasterized.empty()) { | 364 if (!tiles_that_need_to_be_rasterized.empty()) { |
504 ScheduleTasks(tiles_that_need_to_be_rasterized); | 365 ScheduleTasks(tiles_that_need_to_be_rasterized); |
505 return; | 366 return; |
506 } | 367 } |
507 | 368 |
508 FreeResourcesForReleasedTiles(); | 369 FreeResourcesForReleasedTiles(); |
509 | 370 |
510 resource_pool_->ReduceResourceUsage(); | 371 resource_pool_->ReduceResourceUsage(); |
511 | 372 |
512 // We don't reserve memory for required-for-activation tiles during | 373 // We don't reserve memory for required-for-activation tiles during |
513 // accelerated gestures, so we just postpone activation when we don't | 374 // accelerated gestures, so we just postpone activation when we don't |
514 // have these tiles, and activate after the accelerated gesture. | 375 // have these tiles, and activate after the accelerated gesture. |
515 bool allow_rasterize_on_demand = | 376 bool allow_rasterize_on_demand = |
516 global_state_.tree_priority != SMOOTHNESS_TAKES_PRIORITY; | 377 global_state_.tree_priority != SMOOTHNESS_TAKES_PRIORITY; |
517 | 378 |
518 // Use on-demand raster for any required-for-activation tiles that have not | 379 // Use on-demand raster for any required-for-activation tiles that have not |
519 // been been assigned memory after reaching a steady memory state. This | 380 // been been assigned memory after reaching a steady memory state. This |
520 // ensures that we activate even when OOM. | 381 // ensures that we activate even when OOM. Note that we have to rebuilt the |
521 for (TileMap::iterator it = tiles_.begin(); it != tiles_.end(); ++it) { | 382 // queue in case the last AssignGpuMemoryToTiles evicted some tiles that |
522 Tile* tile = it->second; | 383 // would otherwise not be picked up by the old raster queue. |
| 384 raster_priority_queue_.Reset(); |
| 385 client_->BuildRasterQueue(&raster_priority_queue_, |
| 386 global_state_.tree_priority); |
| 387 while (!raster_priority_queue_.IsEmpty()) { |
| 388 Tile* tile = raster_priority_queue_.Top(); |
523 ManagedTileState& mts = tile->managed_state(); | 389 ManagedTileState& mts = tile->managed_state(); |
524 ManagedTileState::TileVersion& tile_version = | 390 ManagedTileState::TileVersion& tile_version = |
525 mts.tile_versions[mts.raster_mode]; | 391 mts.tile_versions[mts.raster_mode]; |
526 | 392 |
527 if (tile->required_for_activation() && !tile_version.IsReadyToDraw()) { | 393 if (tile->required_for_activation() && !tile_version.IsReadyToDraw()) { |
528 // If we can't raster on demand, give up early (and don't activate). | 394 // If we can't raster on demand, give up early (and don't activate). |
529 if (!allow_rasterize_on_demand) | 395 if (!allow_rasterize_on_demand) |
530 return; | 396 return; |
531 | 397 |
532 tile_version.set_rasterize_on_demand(); | 398 tile_version.set_rasterize_on_demand(); |
533 client_->NotifyTileStateChanged(tile); | 399 client_->NotifyTileStateChanged(tile); |
534 } | 400 } |
| 401 raster_priority_queue_.Pop(); |
535 } | 402 } |
536 | 403 |
537 DCHECK(IsReadyToActivate()); | 404 DCHECK(IsReadyToActivate()); |
538 ready_to_activate_check_notifier_.Schedule(); | 405 ready_to_activate_check_notifier_.Schedule(); |
539 return; | 406 return; |
540 } | 407 } |
541 | 408 |
542 if (task_set == REQUIRED_FOR_ACTIVATION) { | 409 if (task_set == REQUIRED_FOR_ACTIVATION) { |
543 TRACE_EVENT2("cc", | 410 TRACE_EVENT1("cc", |
544 "TileManager::DidFinishRunningTasks", | 411 "TileManager::DidFinishRunningTasks", |
545 "task_set", | 412 "task_set", |
546 "REQUIRED_FOR_ACTIVATION", | 413 "REQUIRED_FOR_ACTIVATION"); |
547 "all_tiles_required_for_activation_have_memory", | |
548 all_tiles_required_for_activation_have_memory_); | |
549 // This is only a true indication that all tiles required for | |
550 // activation are initialized when no tiles are OOM. We need to | |
551 // wait for DidFinishRunningTasks() to be called, try to re-assign | |
552 // memory and in worst case use on-demand raster when tiles | |
553 // required for activation are OOM. | |
554 if (!all_tiles_required_for_activation_have_memory_) | |
555 return; | |
556 | |
557 ready_to_activate_check_notifier_.Schedule(); | 414 ready_to_activate_check_notifier_.Schedule(); |
558 } | 415 } |
559 } | 416 } |
560 | 417 |
561 void TileManager::GetTilesWithAssignedBins(PrioritizedTileSet* tiles) { | |
562 TRACE_EVENT0("cc", "TileManager::GetTilesWithAssignedBins"); | |
563 | |
564 const TileMemoryLimitPolicy memory_policy = global_state_.memory_limit_policy; | |
565 const TreePriority tree_priority = global_state_.tree_priority; | |
566 | |
567 // For each tree, bin into different categories of tiles. | |
568 for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end(); ++it) { | |
569 Tile* tile = it->second; | |
570 ManagedTileState& mts = tile->managed_state(); | |
571 | |
572 const ManagedTileState::TileVersion& tile_version = | |
573 tile->GetTileVersionForDrawing(); | |
574 bool tile_is_ready_to_draw = tile_version.IsReadyToDraw(); | |
575 bool tile_is_active = tile_is_ready_to_draw || | |
576 mts.tile_versions[mts.raster_mode].raster_task_.get(); | |
577 | |
578 // Get the active priority and bin. | |
579 TilePriority active_priority = tile->priority(ACTIVE_TREE); | |
580 ManagedTileBin active_bin = BinFromTilePriority(active_priority); | |
581 | |
582 // Get the pending priority and bin. | |
583 TilePriority pending_priority = tile->priority(PENDING_TREE); | |
584 ManagedTileBin pending_bin = BinFromTilePriority(pending_priority); | |
585 | |
586 bool pending_is_low_res = pending_priority.resolution == LOW_RESOLUTION; | |
587 bool pending_is_non_ideal = | |
588 pending_priority.resolution == NON_IDEAL_RESOLUTION; | |
589 bool active_is_non_ideal = | |
590 active_priority.resolution == NON_IDEAL_RESOLUTION; | |
591 | |
592 // Adjust bin state based on if ready to draw. | |
593 active_bin = kBinReadyToDrawMap[tile_is_ready_to_draw][active_bin]; | |
594 pending_bin = kBinReadyToDrawMap[tile_is_ready_to_draw][pending_bin]; | |
595 | |
596 // Adjust bin state based on if active. | |
597 active_bin = kBinIsActiveMap[tile_is_active][active_bin]; | |
598 pending_bin = kBinIsActiveMap[tile_is_active][pending_bin]; | |
599 | |
600 // We never want to paint new non-ideal tiles, as we always have | |
601 // a high-res tile covering that content (paint that instead). | |
602 if (!tile_is_ready_to_draw && active_is_non_ideal) | |
603 active_bin = NEVER_BIN; | |
604 if (!tile_is_ready_to_draw && pending_is_non_ideal) | |
605 pending_bin = NEVER_BIN; | |
606 | |
607 ManagedTileBin tree_bin[NUM_TREES]; | |
608 tree_bin[ACTIVE_TREE] = kBinPolicyMap[memory_policy][active_bin]; | |
609 tree_bin[PENDING_TREE] = kBinPolicyMap[memory_policy][pending_bin]; | |
610 | |
611 // Adjust pending bin state for low res tiles. This prevents pending tree | |
612 // low-res tiles from being initialized before high-res tiles. | |
613 if (pending_is_low_res) | |
614 tree_bin[PENDING_TREE] = std::max(tree_bin[PENDING_TREE], EVENTUALLY_BIN); | |
615 | |
616 TilePriority tile_priority; | |
617 switch (tree_priority) { | |
618 case SAME_PRIORITY_FOR_BOTH_TREES: | |
619 mts.bin = std::min(tree_bin[ACTIVE_TREE], tree_bin[PENDING_TREE]); | |
620 tile_priority = tile->combined_priority(); | |
621 break; | |
622 case SMOOTHNESS_TAKES_PRIORITY: | |
623 mts.bin = tree_bin[ACTIVE_TREE]; | |
624 tile_priority = active_priority; | |
625 break; | |
626 case NEW_CONTENT_TAKES_PRIORITY: | |
627 mts.bin = tree_bin[PENDING_TREE]; | |
628 tile_priority = pending_priority; | |
629 break; | |
630 default: | |
631 NOTREACHED(); | |
632 } | |
633 | |
634 // Bump up the priority if we determined it's NEVER_BIN on one tree, | |
635 // but is still required on the other tree. | |
636 bool is_in_never_bin_on_both_trees = tree_bin[ACTIVE_TREE] == NEVER_BIN && | |
637 tree_bin[PENDING_TREE] == NEVER_BIN; | |
638 | |
639 if (mts.bin == NEVER_BIN && !is_in_never_bin_on_both_trees) | |
640 mts.bin = tile_is_active ? AT_LAST_AND_ACTIVE_BIN : AT_LAST_BIN; | |
641 | |
642 mts.resolution = tile_priority.resolution; | |
643 mts.priority_bin = tile_priority.priority_bin; | |
644 mts.distance_to_visible = tile_priority.distance_to_visible; | |
645 mts.required_for_activation = tile_priority.required_for_activation; | |
646 | |
647 mts.visible_and_ready_to_draw = | |
648 tree_bin[ACTIVE_TREE] == NOW_AND_READY_TO_DRAW_BIN; | |
649 | |
650 // Tiles that are required for activation shouldn't be in NEVER_BIN unless | |
651 // smoothness takes priority or memory policy allows nothing to be | |
652 // initialized. | |
653 DCHECK(!mts.required_for_activation || mts.bin != NEVER_BIN || | |
654 tree_priority == SMOOTHNESS_TAKES_PRIORITY || | |
655 memory_policy == ALLOW_NOTHING); | |
656 | |
657 // If the tile is in NEVER_BIN and it does not have an active task, then we | |
658 // can release the resources early. If it does have the task however, we | |
659 // should keep it in the prioritized tile set to ensure that AssignGpuMemory | |
660 // can visit it. | |
661 if (mts.bin == NEVER_BIN && | |
662 !mts.tile_versions[mts.raster_mode].raster_task_.get()) { | |
663 FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(tile); | |
664 continue; | |
665 } | |
666 | |
667 // Insert the tile into a priority set. | |
668 tiles->InsertTile(tile, mts.bin); | |
669 } | |
670 } | |
671 | |
672 void TileManager::ManageTiles(const GlobalStateThatImpactsTilePriority& state) { | 418 void TileManager::ManageTiles(const GlobalStateThatImpactsTilePriority& state) { |
673 TRACE_EVENT0("cc", "TileManager::ManageTiles"); | 419 TRACE_EVENT0("cc", "TileManager::ManageTiles"); |
674 | 420 |
675 // Update internal state. | 421 global_state_ = state; |
676 if (state != global_state_) { | |
677 global_state_ = state; | |
678 prioritized_tiles_dirty_ = true; | |
679 } | |
680 | 422 |
681 // We need to call CheckForCompletedTasks() once in-between each call | 423 // We need to call CheckForCompletedTasks() once in-between each call |
682 // to ScheduleTasks() to prevent canceled tasks from being scheduled. | 424 // to ScheduleTasks() to prevent canceled tasks from being scheduled. |
683 if (!did_check_for_completed_tasks_since_last_schedule_tasks_) { | 425 if (!did_check_for_completed_tasks_since_last_schedule_tasks_) { |
684 rasterizer_->CheckForCompletedTasks(); | 426 rasterizer_->CheckForCompletedTasks(); |
685 did_check_for_completed_tasks_since_last_schedule_tasks_ = true; | 427 did_check_for_completed_tasks_since_last_schedule_tasks_ = true; |
686 } | 428 } |
687 | 429 |
688 UpdatePrioritizedTileSetIfNeeded(); | 430 FreeResourcesForReleasedTiles(); |
| 431 CleanUpReleasedTiles(); |
689 | 432 |
690 TileVector tiles_that_need_to_be_rasterized; | 433 TileVector tiles_that_need_to_be_rasterized; |
691 AssignGpuMemoryToTiles(&prioritized_tiles_, | 434 AssignGpuMemoryToTiles(&tiles_that_need_to_be_rasterized); |
692 &tiles_that_need_to_be_rasterized); | |
693 | 435 |
694 // Finally, schedule rasterizer tasks. | 436 // Finally, schedule rasterizer tasks. |
695 ScheduleTasks(tiles_that_need_to_be_rasterized); | 437 ScheduleTasks(tiles_that_need_to_be_rasterized); |
696 | 438 |
697 TRACE_EVENT_INSTANT1("cc", | 439 TRACE_EVENT_INSTANT1("cc", |
698 "DidManage", | 440 "DidManage", |
699 TRACE_EVENT_SCOPE_THREAD, | 441 TRACE_EVENT_SCOPE_THREAD, |
700 "state", | 442 "state", |
701 BasicStateAsValue()); | 443 BasicStateAsValue()); |
702 | 444 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
735 } | 477 } |
736 | 478 |
737 void TileManager::BasicStateAsValueInto(base::debug::TracedValue* state) const { | 479 void TileManager::BasicStateAsValueInto(base::debug::TracedValue* state) const { |
738 state->SetInteger("tile_count", tiles_.size()); | 480 state->SetInteger("tile_count", tiles_.size()); |
739 state->SetBoolean("did_oom_on_last_assign", did_oom_on_last_assign_); | 481 state->SetBoolean("did_oom_on_last_assign", did_oom_on_last_assign_); |
740 state->BeginDictionary("global_state"); | 482 state->BeginDictionary("global_state"); |
741 global_state_.AsValueInto(state); | 483 global_state_.AsValueInto(state); |
742 state->EndDictionary(); | 484 state->EndDictionary(); |
743 } | 485 } |
744 | 486 |
| 487 void TileManager::RebuildEvictionQueueIfNeeded() { |
| 488 if (eviction_priority_queue_is_up_to_date_) |
| 489 return; |
| 490 |
| 491 eviction_priority_queue_.Reset(); |
| 492 client_->BuildEvictionQueue(&eviction_priority_queue_, |
| 493 global_state_.tree_priority); |
| 494 eviction_priority_queue_is_up_to_date_ = true; |
| 495 } |
| 496 |
| 497 bool TileManager::FreeTileResourcesUntilUsageIsWithinLimit( |
| 498 const MemoryUsage& limit, |
| 499 MemoryUsage* usage) { |
| 500 while (usage->Exceeds(limit)) { |
| 501 RebuildEvictionQueueIfNeeded(); |
| 502 if (eviction_priority_queue_.IsEmpty()) |
| 503 return false; |
| 504 |
| 505 Tile* tile = eviction_priority_queue_.Top(); |
| 506 *usage -= MemoryUsage::FromTile(tile); |
| 507 FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(tile); |
| 508 eviction_priority_queue_.Pop(); |
| 509 } |
| 510 return true; |
| 511 } |
| 512 |
| 513 bool TileManager::FreeTileResourcesWithLowerPriorityUntilUsageIsWithinLimit( |
| 514 const MemoryUsage& limit, |
| 515 const TilePriority& other_priority, |
| 516 MemoryUsage* usage) { |
| 517 while (usage->Exceeds(limit)) { |
| 518 RebuildEvictionQueueIfNeeded(); |
| 519 if (eviction_priority_queue_.IsEmpty()) |
| 520 return false; |
| 521 |
| 522 Tile* tile = eviction_priority_queue_.Top(); |
| 523 if (!other_priority.IsHigherPriorityThan( |
| 524 tile->priority_for_tree_priority(global_state_.tree_priority))) { |
| 525 return false; |
| 526 } |
| 527 |
| 528 *usage -= MemoryUsage::FromTile(tile); |
| 529 FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(tile); |
| 530 eviction_priority_queue_.Pop(); |
| 531 } |
| 532 return true; |
| 533 } |
| 534 |
| 535 bool TileManager::TilePriorityViolatesMemoryPolicy( |
| 536 const TilePriority& priority) { |
| 537 switch (global_state_.memory_limit_policy) { |
| 538 case ALLOW_NOTHING: |
| 539 return true; |
| 540 case ALLOW_ABSOLUTE_MINIMUM: |
| 541 return priority.priority_bin > TilePriority::NOW; |
| 542 case ALLOW_PREPAINT_ONLY: |
| 543 return priority.priority_bin > TilePriority::SOON; |
| 544 case ALLOW_ANYTHING: |
| 545 return priority.distance_to_visible == |
| 546 std::numeric_limits<float>::infinity(); |
| 547 } |
| 548 NOTREACHED(); |
| 549 return true; |
| 550 } |
| 551 |
745 void TileManager::AssignGpuMemoryToTiles( | 552 void TileManager::AssignGpuMemoryToTiles( |
746 PrioritizedTileSet* tiles, | |
747 TileVector* tiles_that_need_to_be_rasterized) { | 553 TileVector* tiles_that_need_to_be_rasterized) { |
748 TRACE_EVENT0("cc", "TileManager::AssignGpuMemoryToTiles"); | 554 TRACE_EVENT0("cc", "TileManager::AssignGpuMemoryToTiles"); |
749 | 555 |
750 // Maintain the list of released resources that can potentially be re-used | 556 // Maintain the list of released resources that can potentially be re-used |
751 // or deleted. | 557 // or deleted. |
752 // If this operation becomes expensive too, only do this after some | 558 // If this operation becomes expensive too, only do this after some |
753 // resource(s) was returned. Note that in that case, one also need to | 559 // resource(s) was returned. Note that in that case, one also need to |
754 // invalidate when releasing some resource from the pool. | 560 // invalidate when releasing some resource from the pool. |
755 resource_pool_->CheckBusyResources(); | 561 resource_pool_->CheckBusyResources(); |
756 | 562 |
757 // Now give memory out to the tiles until we're out, and build | 563 // Now give memory out to the tiles until we're out, and build |
758 // the needs-to-be-rasterized queue. | 564 // the needs-to-be-rasterized queue. |
759 all_tiles_that_need_to_be_rasterized_have_memory_ = true; | 565 unsigned schedule_priority = 1u; |
760 all_tiles_required_for_activation_have_memory_ = true; | 566 all_tiles_that_need_to_be_rasterized_are_scheduled_ = true; |
| 567 bool had_enough_memory_to_schedule_tiles_needed_now = true; |
761 | 568 |
762 // Cast to prevent overflow. | 569 MemoryUsage hard_memory_limit(global_state_.hard_memory_limit_in_bytes, |
763 int64 soft_bytes_available = | 570 global_state_.num_resources_limit); |
764 static_cast<int64>(bytes_releasable_) + | 571 MemoryUsage soft_memory_limit(global_state_.soft_memory_limit_in_bytes, |
765 static_cast<int64>(global_state_.soft_memory_limit_in_bytes) - | 572 global_state_.num_resources_limit); |
766 static_cast<int64>(resource_pool_->acquired_memory_usage_bytes()); | 573 MemoryUsage memory_usage(resource_pool_->acquired_memory_usage_bytes(), |
767 int64 hard_bytes_available = | 574 resource_pool_->acquired_resource_count()); |
768 static_cast<int64>(bytes_releasable_) + | |
769 static_cast<int64>(global_state_.hard_memory_limit_in_bytes) - | |
770 static_cast<int64>(resource_pool_->acquired_memory_usage_bytes()); | |
771 int resources_available = resources_releasable_ + | |
772 global_state_.num_resources_limit - | |
773 resource_pool_->acquired_resource_count(); | |
774 size_t soft_bytes_allocatable = | |
775 std::max(static_cast<int64>(0), soft_bytes_available); | |
776 size_t hard_bytes_allocatable = | |
777 std::max(static_cast<int64>(0), hard_bytes_available); | |
778 size_t resources_allocatable = std::max(0, resources_available); | |
779 | 575 |
780 size_t bytes_that_exceeded_memory_budget = 0; | 576 eviction_priority_queue_is_up_to_date_ = false; |
781 size_t soft_bytes_left = soft_bytes_allocatable; | 577 raster_priority_queue_.Reset(); |
782 size_t hard_bytes_left = hard_bytes_allocatable; | 578 client_->BuildRasterQueue(&raster_priority_queue_, |
| 579 global_state_.tree_priority); |
783 | 580 |
784 size_t resources_left = resources_allocatable; | 581 while (!raster_priority_queue_.IsEmpty()) { |
785 bool oomed_soft = false; | 582 Tile* tile = raster_priority_queue_.Top(); |
786 bool oomed_hard = false; | 583 TilePriority priority = |
787 bool have_hit_soft_memory = false; // Soft memory comes after hard. | 584 tile->priority_for_tree_priority(global_state_.tree_priority); |
788 | 585 |
789 unsigned schedule_priority = 1u; | 586 if (TilePriorityViolatesMemoryPolicy(priority)) |
790 for (PrioritizedTileSet::Iterator it(tiles, true); it; ++it) { | 587 break; |
791 Tile* tile = *it; | 588 |
| 589 // We won't be able to schedule this tile, so break out early. |
| 590 if (tiles_that_need_to_be_rasterized->size() >= |
| 591 kScheduledRasterTasksLimit) { |
| 592 all_tiles_that_need_to_be_rasterized_are_scheduled_ = false; |
| 593 break; |
| 594 } |
| 595 |
792 ManagedTileState& mts = tile->managed_state(); | 596 ManagedTileState& mts = tile->managed_state(); |
793 | |
794 mts.scheduled_priority = schedule_priority++; | 597 mts.scheduled_priority = schedule_priority++; |
795 | |
796 mts.raster_mode = tile->DetermineOverallRasterMode(); | 598 mts.raster_mode = tile->DetermineOverallRasterMode(); |
797 | |
798 ManagedTileState::TileVersion& tile_version = | 599 ManagedTileState::TileVersion& tile_version = |
799 mts.tile_versions[mts.raster_mode]; | 600 mts.tile_versions[mts.raster_mode]; |
800 | 601 |
801 // If this tile doesn't need a resource, then nothing to do. | 602 DCHECK(tile_version.mode() == |
802 if (!tile_version.requires_resource()) | 603 ManagedTileState::TileVersion::PICTURE_PILE_MODE || |
803 continue; | 604 !tile_version.IsReadyToDraw()); |
804 | 605 |
805 // If the tile is not needed, free it up. | 606 // If the tile already has a raster_task, then the memory used by it is |
806 if (mts.bin == NEVER_BIN) { | 607 // already accounted for in memory_usage. Otherwise, we'll have to acquire |
807 FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(tile); | 608 // more memory to create a raster task. |
808 continue; | 609 MemoryUsage memory_required_by_tile_to_be_scheduled; |
| 610 if (!tile_version.raster_task_.get()) { |
| 611 memory_required_by_tile_to_be_scheduled = MemoryUsage::FromConfig( |
| 612 tile->size(), resource_pool_->resource_format()); |
809 } | 613 } |
810 | 614 |
811 const bool tile_uses_hard_limit = mts.bin <= NOW_BIN; | 615 bool tile_is_needed_now = priority.priority_bin == TilePriority::NOW; |
812 const size_t bytes_if_allocated = BytesConsumedIfAllocated(tile); | |
813 const size_t tile_bytes_left = | |
814 (tile_uses_hard_limit) ? hard_bytes_left : soft_bytes_left; | |
815 | 616 |
816 // Hard-limit is reserved for tiles that would cause a calamity | 617 // This is the memory limit that will be used by this tile. Depending on |
817 // if they were to go away, so by definition they are the highest | 618 // the tile priority, it will be one of hard_memory_limit or |
818 // priority memory, and must be at the front of the list. | 619 // soft_memory_limit. |
819 DCHECK(!(have_hit_soft_memory && tile_uses_hard_limit)); | 620 MemoryUsage& tile_memory_limit = |
820 have_hit_soft_memory |= !tile_uses_hard_limit; | 621 tile_is_needed_now ? hard_memory_limit : soft_memory_limit; |
821 | 622 |
822 size_t tile_bytes = 0; | 623 bool memory_usage_is_within_limit = |
823 size_t tile_resources = 0; | 624 FreeTileResourcesWithLowerPriorityUntilUsageIsWithinLimit( |
| 625 tile_memory_limit - memory_required_by_tile_to_be_scheduled, |
| 626 priority, |
| 627 &memory_usage); |
824 | 628 |
825 // It costs to maintain a resource. | 629 // If we couldn't fit the tile into our current memory limit, then we're |
826 for (int mode = 0; mode < NUM_RASTER_MODES; ++mode) { | 630 // done. |
827 if (mts.tile_versions[mode].resource_) { | 631 if (!memory_usage_is_within_limit) { |
828 tile_bytes += bytes_if_allocated; | 632 if (tile_is_needed_now) |
829 tile_resources++; | 633 had_enough_memory_to_schedule_tiles_needed_now = false; |
830 } | 634 all_tiles_that_need_to_be_rasterized_are_scheduled_ = false; |
| 635 break; |
831 } | 636 } |
832 | 637 |
833 // Allow lower priority tiles with initialized resources to keep | 638 memory_usage += memory_required_by_tile_to_be_scheduled; |
834 // their memory by only assigning memory to new raster tasks if | |
835 // they can be scheduled. | |
836 bool reached_scheduled_raster_tasks_limit = | |
837 tiles_that_need_to_be_rasterized->size() >= kScheduledRasterTasksLimit; | |
838 if (!reached_scheduled_raster_tasks_limit) { | |
839 // If we don't have the required version, and it's not in flight | |
840 // then we'll have to pay to create a new task. | |
841 if (!tile_version.resource_ && !tile_version.raster_task_.get()) { | |
842 tile_bytes += bytes_if_allocated; | |
843 tile_resources++; | |
844 } | |
845 } | |
846 | |
847 // Tile is OOM. | |
848 if (tile_bytes > tile_bytes_left || tile_resources > resources_left) { | |
849 FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(tile); | |
850 | |
851 // This tile was already on screen and now its resources have been | |
852 // released. In order to prevent checkerboarding, set this tile as | |
853 // rasterize on demand immediately. | |
854 if (mts.visible_and_ready_to_draw) | |
855 tile_version.set_rasterize_on_demand(); | |
856 | |
857 oomed_soft = true; | |
858 if (tile_uses_hard_limit) { | |
859 oomed_hard = true; | |
860 bytes_that_exceeded_memory_budget += tile_bytes; | |
861 } | |
862 } else { | |
863 resources_left -= tile_resources; | |
864 hard_bytes_left -= tile_bytes; | |
865 soft_bytes_left = | |
866 (soft_bytes_left > tile_bytes) ? soft_bytes_left - tile_bytes : 0; | |
867 if (tile_version.resource_) | |
868 continue; | |
869 } | |
870 | |
871 DCHECK(!tile_version.resource_); | |
872 | |
873 // Tile shouldn't be rasterized if |tiles_that_need_to_be_rasterized| | |
874 // has reached it's limit or we've failed to assign gpu memory to this | |
875 // or any higher priority tile. Preventing tiles that fit into memory | |
876 // budget to be rasterized when higher priority tile is oom is | |
877 // important for two reasons: | |
878 // 1. Tile size should not impact raster priority. | |
879 // 2. Tiles with existing raster task could otherwise incorrectly | |
880 // be added as they are not affected by |bytes_allocatable|. | |
881 bool can_schedule_tile = | |
882 !oomed_soft && !reached_scheduled_raster_tasks_limit; | |
883 | |
884 if (!can_schedule_tile) { | |
885 all_tiles_that_need_to_be_rasterized_have_memory_ = false; | |
886 if (tile->required_for_activation()) | |
887 all_tiles_required_for_activation_have_memory_ = false; | |
888 it.DisablePriorityOrdering(); | |
889 continue; | |
890 } | |
891 | |
892 tiles_that_need_to_be_rasterized->push_back(tile); | 639 tiles_that_need_to_be_rasterized->push_back(tile); |
| 640 raster_priority_queue_.Pop(); |
893 } | 641 } |
894 | 642 |
895 // OOM reporting uses hard-limit, soft-OOM is normal depending on limit. | 643 // Note that we should try and further reduce memory in case the above loop |
896 ever_exceeded_memory_budget_ |= oomed_hard; | 644 // didn't reduce memory. This ensures that we always release as many resources |
897 if (ever_exceeded_memory_budget_) { | 645 // as possible to stay within the memory limit. |
898 TRACE_COUNTER_ID2("cc", | 646 FreeTileResourcesUntilUsageIsWithinLimit(hard_memory_limit, &memory_usage); |
899 "over_memory_budget", | 647 |
900 this, | 648 UMA_HISTOGRAM_BOOLEAN("TileManager.ExceededMemoryBudget", |
901 "budget", | 649 !had_enough_memory_to_schedule_tiles_needed_now); |
902 global_state_.hard_memory_limit_in_bytes, | 650 did_oom_on_last_assign_ = !had_enough_memory_to_schedule_tiles_needed_now; |
903 "over", | 651 |
904 bytes_that_exceeded_memory_budget); | |
905 } | |
906 did_oom_on_last_assign_ = oomed_hard; | |
907 UMA_HISTOGRAM_BOOLEAN("TileManager.ExceededMemoryBudget", oomed_hard); | |
908 memory_stats_from_last_assign_.total_budget_in_bytes = | 652 memory_stats_from_last_assign_.total_budget_in_bytes = |
909 global_state_.hard_memory_limit_in_bytes; | 653 global_state_.hard_memory_limit_in_bytes; |
910 memory_stats_from_last_assign_.bytes_allocated = | 654 memory_stats_from_last_assign_.total_bytes_used = memory_usage.memory_bytes(); |
911 hard_bytes_allocatable - hard_bytes_left; | 655 memory_stats_from_last_assign_.had_enough_memory = |
912 memory_stats_from_last_assign_.bytes_unreleasable = | 656 had_enough_memory_to_schedule_tiles_needed_now; |
913 resource_pool_->acquired_memory_usage_bytes() - bytes_releasable_; | |
914 memory_stats_from_last_assign_.bytes_over = bytes_that_exceeded_memory_budget; | |
915 } | 657 } |
916 | 658 |
917 void TileManager::FreeResourceForTile(Tile* tile, RasterMode mode) { | 659 void TileManager::FreeResourceForTile(Tile* tile, RasterMode mode) { |
918 ManagedTileState& mts = tile->managed_state(); | 660 ManagedTileState& mts = tile->managed_state(); |
919 if (mts.tile_versions[mode].resource_) { | 661 if (mts.tile_versions[mode].resource_) |
920 resource_pool_->ReleaseResource(mts.tile_versions[mode].resource_.Pass()); | 662 resource_pool_->ReleaseResource(mts.tile_versions[mode].resource_.Pass()); |
921 | |
922 DCHECK_GE(bytes_releasable_, BytesConsumedIfAllocated(tile)); | |
923 DCHECK_GE(resources_releasable_, 1u); | |
924 | |
925 bytes_releasable_ -= BytesConsumedIfAllocated(tile); | |
926 --resources_releasable_; | |
927 } | |
928 } | 663 } |
929 | 664 |
930 void TileManager::FreeResourcesForTile(Tile* tile) { | 665 void TileManager::FreeResourcesForTile(Tile* tile) { |
931 for (int mode = 0; mode < NUM_RASTER_MODES; ++mode) { | 666 for (int mode = 0; mode < NUM_RASTER_MODES; ++mode) { |
932 FreeResourceForTile(tile, static_cast<RasterMode>(mode)); | 667 FreeResourceForTile(tile, static_cast<RasterMode>(mode)); |
933 } | 668 } |
934 } | 669 } |
935 | 670 |
936 void TileManager::FreeUnusedResourcesForTile(Tile* tile) { | 671 void TileManager::FreeUnusedResourcesForTile(Tile* tile) { |
937 DCHECK(tile->IsReadyToDraw()); | 672 DCHECK(tile->IsReadyToDraw()); |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1116 } | 851 } |
1117 | 852 |
1118 ++update_visible_tiles_stats_.completed_count; | 853 ++update_visible_tiles_stats_.completed_count; |
1119 | 854 |
1120 if (analysis.is_solid_color) { | 855 if (analysis.is_solid_color) { |
1121 tile_version.set_solid_color(analysis.solid_color); | 856 tile_version.set_solid_color(analysis.solid_color); |
1122 resource_pool_->ReleaseResource(resource.Pass()); | 857 resource_pool_->ReleaseResource(resource.Pass()); |
1123 } else { | 858 } else { |
1124 tile_version.set_use_resource(); | 859 tile_version.set_use_resource(); |
1125 tile_version.resource_ = resource.Pass(); | 860 tile_version.resource_ = resource.Pass(); |
1126 | |
1127 bytes_releasable_ += BytesConsumedIfAllocated(tile); | |
1128 ++resources_releasable_; | |
1129 } | 861 } |
1130 | 862 |
1131 FreeUnusedResourcesForTile(tile); | 863 FreeUnusedResourcesForTile(tile); |
1132 if (tile->priority(ACTIVE_TREE).distance_to_visible == 0.f) | 864 if (tile->priority(ACTIVE_TREE).distance_to_visible == 0.f) |
1133 did_initialize_visible_tile_ = true; | 865 did_initialize_visible_tile_ = true; |
1134 | 866 |
1135 client_->NotifyTileStateChanged(tile); | 867 client_->NotifyTileStateChanged(tile); |
1136 } | 868 } |
1137 | 869 |
1138 scoped_refptr<Tile> TileManager::CreateTile(PicturePileImpl* picture_pile, | 870 scoped_refptr<Tile> TileManager::CreateTile(PicturePileImpl* picture_pile, |
1139 const gfx::Size& tile_size, | 871 const gfx::Size& tile_size, |
1140 const gfx::Rect& content_rect, | 872 const gfx::Rect& content_rect, |
1141 float contents_scale, | 873 float contents_scale, |
1142 int layer_id, | 874 int layer_id, |
1143 int source_frame_number, | 875 int source_frame_number, |
1144 int flags) { | 876 int flags) { |
1145 scoped_refptr<Tile> tile = make_scoped_refptr(new Tile(this, | 877 scoped_refptr<Tile> tile = make_scoped_refptr(new Tile(this, |
1146 picture_pile, | 878 picture_pile, |
1147 tile_size, | 879 tile_size, |
1148 content_rect, | 880 content_rect, |
1149 contents_scale, | 881 contents_scale, |
1150 layer_id, | 882 layer_id, |
1151 source_frame_number, | 883 source_frame_number, |
1152 flags)); | 884 flags)); |
1153 DCHECK(tiles_.find(tile->id()) == tiles_.end()); | 885 DCHECK(tiles_.find(tile->id()) == tiles_.end()); |
1154 | 886 |
1155 tiles_[tile->id()] = tile.get(); | 887 tiles_[tile->id()] = tile.get(); |
1156 used_layer_counts_[tile->layer_id()]++; | 888 used_layer_counts_[tile->layer_id()]++; |
1157 prioritized_tiles_dirty_ = true; | |
1158 return tile; | 889 return tile; |
1159 } | 890 } |
1160 | 891 |
1161 void TileManager::SetRasterizerForTesting(Rasterizer* rasterizer) { | 892 void TileManager::SetRasterizerForTesting(Rasterizer* rasterizer) { |
1162 rasterizer_ = rasterizer; | 893 rasterizer_ = rasterizer; |
1163 rasterizer_->SetClient(this); | 894 rasterizer_->SetClient(this); |
1164 } | 895 } |
1165 | 896 |
1166 bool TileManager::IsReadyToActivate() const { | 897 bool TileManager::IsReadyToActivate() const { |
1167 const std::vector<PictureLayerImpl*>& layers = client_->GetPictureLayers(); | 898 const std::vector<PictureLayerImpl*>& layers = client_->GetPictureLayers(); |
(...skipping 11 matching lines...) Expand all Loading... |
1179 void TileManager::CheckIfReadyToActivate() { | 910 void TileManager::CheckIfReadyToActivate() { |
1180 TRACE_EVENT0("cc", "TileManager::CheckIfReadyToActivate"); | 911 TRACE_EVENT0("cc", "TileManager::CheckIfReadyToActivate"); |
1181 | 912 |
1182 rasterizer_->CheckForCompletedTasks(); | 913 rasterizer_->CheckForCompletedTasks(); |
1183 did_check_for_completed_tasks_since_last_schedule_tasks_ = true; | 914 did_check_for_completed_tasks_since_last_schedule_tasks_ = true; |
1184 | 915 |
1185 if (IsReadyToActivate()) | 916 if (IsReadyToActivate()) |
1186 client_->NotifyReadyToActivate(); | 917 client_->NotifyReadyToActivate(); |
1187 } | 918 } |
1188 | 919 |
| 920 TileManager::MemoryUsage::MemoryUsage() : memory_bytes_(0), resource_count_(0) { |
| 921 } |
| 922 |
| 923 TileManager::MemoryUsage::MemoryUsage(int64 memory_bytes, int resource_count) |
| 924 : memory_bytes_(memory_bytes), resource_count_(resource_count) { |
| 925 } |
| 926 |
| 927 // static |
| 928 TileManager::MemoryUsage TileManager::MemoryUsage::FromConfig( |
| 929 const gfx::Size& size, |
| 930 ResourceFormat format) { |
| 931 return MemoryUsage(Resource::MemorySizeBytes(size, format), 1); |
| 932 } |
| 933 |
| 934 // static |
| 935 TileManager::MemoryUsage TileManager::MemoryUsage::FromTile(const Tile* tile) { |
| 936 const ManagedTileState& mts = tile->managed_state(); |
| 937 MemoryUsage total_usage; |
| 938 for (int mode = 0; mode < NUM_RASTER_MODES; ++mode) { |
| 939 if (mts.tile_versions[mode].resource_) { |
| 940 total_usage += MemoryUsage::FromConfig( |
| 941 tile->size(), mts.tile_versions[mode].resource_->format()); |
| 942 } |
| 943 } |
| 944 return total_usage; |
| 945 } |
| 946 |
| 947 TileManager::MemoryUsage& TileManager::MemoryUsage::operator+=( |
| 948 const MemoryUsage& other) { |
| 949 memory_bytes_ += other.memory_bytes_; |
| 950 resource_count_ += other.resource_count_; |
| 951 return *this; |
| 952 } |
| 953 |
| 954 TileManager::MemoryUsage& TileManager::MemoryUsage::operator-=( |
| 955 const MemoryUsage& other) { |
| 956 memory_bytes_ -= other.memory_bytes_; |
| 957 resource_count_ -= other.resource_count_; |
| 958 return *this; |
| 959 } |
| 960 |
| 961 TileManager::MemoryUsage TileManager::MemoryUsage::operator-( |
| 962 const MemoryUsage& other) { |
| 963 MemoryUsage result = *this; |
| 964 result -= other; |
| 965 return result; |
| 966 } |
| 967 |
| 968 bool TileManager::MemoryUsage::Exceeds(const MemoryUsage& limit) const { |
| 969 return memory_bytes_ > limit.memory_bytes_ || |
| 970 resource_count_ > limit.resource_count_; |
| 971 } |
| 972 |
1189 } // namespace cc | 973 } // namespace cc |
OLD | NEW |