| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "cc/tile_manager.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/debug/trace_event.h" | |
| 11 #include "base/json/json_writer.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/metrics/histogram.h" | |
| 14 #include "cc/debug/devtools_instrumentation.h" | |
| 15 #include "cc/platform_color.h" | |
| 16 #include "cc/raster_worker_pool.h" | |
| 17 #include "cc/resource_pool.h" | |
| 18 #include "cc/tile.h" | |
| 19 #include "third_party/skia/include/core/SkDevice.h" | |
| 20 | |
| 21 namespace cc { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // If we raster too fast we become upload bound, and pending | |
| 26 // uploads consume memory. For maximum upload throughput, we would | |
| 27 // want to allow for upload_throughput * pipeline_time of pending | |
| 28 // uploads, after which we are just wasting memory. Since we don't | |
| 29 // know our upload throughput yet, this just caps our memory usage. | |
| 30 #if defined(OS_ANDROID) | |
| 31 // For reference, the Nexus10 can upload 1MB in about 2.5ms. | |
| 32 // Assuming a three frame deep pipeline this implies ~20MB. | |
| 33 const int kMaxPendingUploadBytes = 20 * 1024 * 1024; | |
| 34 // TODO(epenner): We should remove this upload limit (crbug.com/176197) | |
| 35 const int kMaxPendingUploads = 72; | |
| 36 #else | |
| 37 const int kMaxPendingUploadBytes = 100 * 1024 * 1024; | |
| 38 const int kMaxPendingUploads = 1000; | |
| 39 #endif | |
| 40 | |
| 41 #if defined(OS_ANDROID) | |
| 42 const int kMaxNumPendingTasksPerThread = 8; | |
| 43 #else | |
| 44 const int kMaxNumPendingTasksPerThread = 40; | |
| 45 #endif | |
| 46 | |
| 47 // Limit for time spent running cheap tasks during a single frame. | |
| 48 // TODO(skyostil): Determine this limit more dynamically. | |
| 49 const int kRunCheapTasksTimeMs = 6; | |
| 50 | |
| 51 // Determine bin based on three categories of tiles: things we need now, | |
| 52 // things we need soon, and eventually. | |
| 53 inline TileManagerBin BinFromTilePriority(const TilePriority& prio) { | |
| 54 if (!prio.is_live) | |
| 55 return NEVER_BIN; | |
| 56 | |
| 57 // The amount of time for which we want to have prepainting coverage. | |
| 58 const float kPrepaintingWindowTimeSeconds = 1.0f; | |
| 59 const float kBackflingGuardDistancePixels = 314.0f; | |
| 60 | |
| 61 // Explicitly limit how far ahead we will prepaint to limit memory usage. | |
| 62 if (prio.distance_to_visible_in_pixels > | |
| 63 TilePriority::kMaxDistanceInContentSpace) | |
| 64 return NEVER_BIN; | |
| 65 | |
| 66 if (prio.time_to_visible_in_seconds == 0 || | |
| 67 prio.distance_to_visible_in_pixels < kBackflingGuardDistancePixels) | |
| 68 return NOW_BIN; | |
| 69 | |
| 70 if (prio.resolution == NON_IDEAL_RESOLUTION) | |
| 71 return EVENTUALLY_BIN; | |
| 72 | |
| 73 if (prio.time_to_visible_in_seconds < kPrepaintingWindowTimeSeconds) | |
| 74 return SOON_BIN; | |
| 75 | |
| 76 return EVENTUALLY_BIN; | |
| 77 } | |
| 78 | |
| 79 std::string ValueToString(scoped_ptr<base::Value> value) | |
| 80 { | |
| 81 std::string str; | |
| 82 base::JSONWriter::Write(value.get(), &str); | |
| 83 return str; | |
| 84 } | |
| 85 | |
| 86 } // namespace | |
| 87 | |
| 88 scoped_ptr<base::Value> TileManagerBinAsValue(TileManagerBin bin) { | |
| 89 switch (bin) { | |
| 90 case NOW_BIN: | |
| 91 return scoped_ptr<base::Value>(base::Value::CreateStringValue( | |
| 92 "NOW_BIN")); | |
| 93 case SOON_BIN: | |
| 94 return scoped_ptr<base::Value>(base::Value::CreateStringValue( | |
| 95 "SOON_BIN")); | |
| 96 case EVENTUALLY_BIN: | |
| 97 return scoped_ptr<base::Value>(base::Value::CreateStringValue( | |
| 98 "EVENTUALLY_BIN")); | |
| 99 case NEVER_BIN: | |
| 100 return scoped_ptr<base::Value>(base::Value::CreateStringValue( | |
| 101 "NEVER_BIN")); | |
| 102 default: | |
| 103 DCHECK(false) << "Unrecognized TileManagerBin value " << bin; | |
| 104 return scoped_ptr<base::Value>(base::Value::CreateStringValue( | |
| 105 "<unknown TileManagerBin value>")); | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 scoped_ptr<base::Value> TileManagerBinPriorityAsValue( | |
| 110 TileManagerBinPriority bin_priority) { | |
| 111 switch (bin_priority) { | |
| 112 case HIGH_PRIORITY_BIN: | |
| 113 return scoped_ptr<base::Value>(base::Value::CreateStringValue( | |
| 114 "HIGH_PRIORITY_BIN")); | |
| 115 case LOW_PRIORITY_BIN: | |
| 116 return scoped_ptr<base::Value>(base::Value::CreateStringValue( | |
| 117 "LOW_PRIORITY_BIN")); | |
| 118 default: | |
| 119 DCHECK(false) << "Unrecognized TileManagerBinPriority value"; | |
| 120 return scoped_ptr<base::Value>(base::Value::CreateStringValue( | |
| 121 "<unknown TileManagerBinPriority value>")); | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 scoped_ptr<base::Value> TileRasterStateAsValue( | |
| 126 TileRasterState raster_state) { | |
| 127 switch (raster_state) { | |
| 128 case IDLE_STATE: | |
| 129 return scoped_ptr<base::Value>(base::Value::CreateStringValue( | |
| 130 "IDLE_STATE")); | |
| 131 case WAITING_FOR_RASTER_STATE: | |
| 132 return scoped_ptr<base::Value>(base::Value::CreateStringValue( | |
| 133 "WAITING_FOR_RASTER_STATE")); | |
| 134 case RASTER_STATE: | |
| 135 return scoped_ptr<base::Value>(base::Value::CreateStringValue( | |
| 136 "RASTER_STATE")); | |
| 137 case UPLOAD_STATE: | |
| 138 return scoped_ptr<base::Value>(base::Value::CreateStringValue( | |
| 139 "UPLOAD_STATE")); | |
| 140 case FORCED_UPLOAD_COMPLETION_STATE: | |
| 141 return scoped_ptr<base::Value>(base::Value::CreateStringValue( | |
| 142 "FORCED_UPLOAD_COMPLETION_STATE")); | |
| 143 default: | |
| 144 DCHECK(false) << "Unrecognized TileRasterState value"; | |
| 145 return scoped_ptr<base::Value>(base::Value::CreateStringValue( | |
| 146 "<unknown TileRasterState value>")); | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 TileManager::TileManager( | |
| 151 TileManagerClient* client, | |
| 152 ResourceProvider* resource_provider, | |
| 153 size_t num_raster_threads, | |
| 154 bool use_cheapness_estimator, | |
| 155 bool use_color_estimator, | |
| 156 bool prediction_benchmarking) | |
| 157 : client_(client), | |
| 158 resource_pool_(ResourcePool::Create(resource_provider)), | |
| 159 raster_worker_pool_(RasterWorkerPool::Create(this, num_raster_threads)), | |
| 160 manage_tiles_pending_(false), | |
| 161 manage_tiles_call_count_(0), | |
| 162 bytes_pending_upload_(0), | |
| 163 has_performed_uploads_since_last_flush_(false), | |
| 164 ever_exceeded_memory_budget_(false), | |
| 165 record_rendering_stats_(false), | |
| 166 use_cheapness_estimator_(use_cheapness_estimator), | |
| 167 use_color_estimator_(use_color_estimator), | |
| 168 did_schedule_cheap_tasks_(false), | |
| 169 allow_cheap_tasks_(true), | |
| 170 prediction_benchmarking_(prediction_benchmarking), | |
| 171 pending_tasks_(0), | |
| 172 max_pending_tasks_(kMaxNumPendingTasksPerThread * num_raster_threads) { | |
| 173 for (int i = 0; i < NUM_STATES; ++i) { | |
| 174 for (int j = 0; j < NUM_TREES; ++j) { | |
| 175 for (int k = 0; k < NUM_BINS; ++k) | |
| 176 raster_state_count_[i][j][k] = 0; | |
| 177 } | |
| 178 } | |
| 179 } | |
| 180 | |
| 181 TileManager::~TileManager() { | |
| 182 // Reset global state and manage. This should cause | |
| 183 // our memory usage to drop to zero. | |
| 184 global_state_ = GlobalStateThatImpactsTilePriority(); | |
| 185 AssignGpuMemoryToTiles(); | |
| 186 // This should finish all pending tasks and release any uninitialized | |
| 187 // resources. | |
| 188 raster_worker_pool_.reset(); | |
| 189 AbortPendingTileUploads(); | |
| 190 DCHECK_EQ(tiles_with_pending_upload_.size(), 0); | |
| 191 DCHECK_EQ(all_tiles_.size(), 0); | |
| 192 DCHECK_EQ(live_or_allocated_tiles_.size(), 0); | |
| 193 } | |
| 194 | |
| 195 void TileManager::SetGlobalState( | |
| 196 const GlobalStateThatImpactsTilePriority& global_state) { | |
| 197 global_state_ = global_state; | |
| 198 resource_pool_->SetMaxMemoryUsageBytes(global_state_.memory_limit_in_bytes); | |
| 199 ScheduleManageTiles(); | |
| 200 } | |
| 201 | |
| 202 void TileManager::RegisterTile(Tile* tile) { | |
| 203 all_tiles_.insert(tile); | |
| 204 | |
| 205 const ManagedTileState& mts = tile->managed_state(); | |
| 206 for (int i = 0; i < NUM_TREES; ++i) | |
| 207 ++raster_state_count_[mts.raster_state][i][mts.tree_bin[i]]; | |
| 208 | |
| 209 ScheduleManageTiles(); | |
| 210 } | |
| 211 | |
| 212 void TileManager::UnregisterTile(Tile* tile) { | |
| 213 for (TileList::iterator it = tiles_with_image_decoding_tasks_.begin(); | |
| 214 it != tiles_with_image_decoding_tasks_.end(); it++) { | |
| 215 if (*it == tile) { | |
| 216 tiles_with_image_decoding_tasks_.erase(it); | |
| 217 break; | |
| 218 } | |
| 219 } | |
| 220 for (TileVector::iterator it = tiles_that_need_to_be_rasterized_.begin(); | |
| 221 it != tiles_that_need_to_be_rasterized_.end(); it++) { | |
| 222 if (*it == tile) { | |
| 223 tiles_that_need_to_be_rasterized_.erase(it); | |
| 224 break; | |
| 225 } | |
| 226 } | |
| 227 for (TileVector::iterator it = live_or_allocated_tiles_.begin(); | |
| 228 it != live_or_allocated_tiles_.end(); it++) { | |
| 229 if (*it == tile) { | |
| 230 live_or_allocated_tiles_.erase(it); | |
| 231 break; | |
| 232 } | |
| 233 } | |
| 234 TileSet::iterator it = all_tiles_.find(tile); | |
| 235 DCHECK(it != all_tiles_.end()); | |
| 236 const ManagedTileState& mts = tile->managed_state(); | |
| 237 for (int i = 0; i < NUM_TREES; ++i) | |
| 238 --raster_state_count_[mts.raster_state][i][mts.tree_bin[i]]; | |
| 239 FreeResourcesForTile(tile); | |
| 240 all_tiles_.erase(it); | |
| 241 } | |
| 242 | |
| 243 class BinComparator { | |
| 244 public: | |
| 245 bool operator() (const Tile* a, const Tile* b) const { | |
| 246 const ManagedTileState& ams = a->managed_state(); | |
| 247 const ManagedTileState& bms = b->managed_state(); | |
| 248 if (ams.bin[HIGH_PRIORITY_BIN] != bms.bin[HIGH_PRIORITY_BIN]) | |
| 249 return ams.bin[HIGH_PRIORITY_BIN] < bms.bin[HIGH_PRIORITY_BIN]; | |
| 250 | |
| 251 if (ams.bin[LOW_PRIORITY_BIN] != bms.bin[LOW_PRIORITY_BIN]) | |
| 252 return ams.bin[LOW_PRIORITY_BIN] < bms.bin[LOW_PRIORITY_BIN]; | |
| 253 | |
| 254 if (ams.resolution != bms.resolution) | |
| 255 return ams.resolution < bms.resolution; | |
| 256 | |
| 257 if (ams.time_to_needed_in_seconds != bms.time_to_needed_in_seconds) | |
| 258 return ams.time_to_needed_in_seconds < bms.time_to_needed_in_seconds; | |
| 259 | |
| 260 if (ams.distance_to_visible_in_pixels != bms.distance_to_visible_in_pixels) | |
| 261 return ams.distance_to_visible_in_pixels < bms.distance_to_visible_in_pixe
ls; | |
| 262 | |
| 263 gfx::Rect a_rect = a->content_rect(); | |
| 264 gfx::Rect b_rect = b->content_rect(); | |
| 265 if (a_rect.y() != b_rect.y()) | |
| 266 return a_rect.y() < b_rect.y(); | |
| 267 return a_rect.x() < b_rect.x(); | |
| 268 } | |
| 269 }; | |
| 270 | |
| 271 void TileManager::SortTiles() { | |
| 272 TRACE_EVENT0("cc", "TileManager::SortTiles"); | |
| 273 TRACE_COUNTER_ID1("cc", "LiveTileCount", this, live_or_allocated_tiles_.size()
); | |
| 274 | |
| 275 // Sort by bin, resolution and time until needed. | |
| 276 std::sort(live_or_allocated_tiles_.begin(), | |
| 277 live_or_allocated_tiles_.end(), BinComparator()); | |
| 278 } | |
| 279 | |
| 280 void TileManager::ManageTiles() { | |
| 281 TRACE_EVENT0("cc", "TileManager::ManageTiles"); | |
| 282 manage_tiles_pending_ = false; | |
| 283 ++manage_tiles_call_count_; | |
| 284 | |
| 285 const TreePriority tree_priority = global_state_.tree_priority; | |
| 286 TRACE_COUNTER_ID1("cc", "TileCount", this, all_tiles_.size()); | |
| 287 | |
| 288 // Memory limit policy works by mapping some bin states to the NEVER bin. | |
| 289 TileManagerBin bin_map[NUM_BINS]; | |
| 290 if (global_state_.memory_limit_policy == ALLOW_NOTHING) { | |
| 291 bin_map[NOW_BIN] = NEVER_BIN; | |
| 292 bin_map[SOON_BIN] = NEVER_BIN; | |
| 293 bin_map[EVENTUALLY_BIN] = NEVER_BIN; | |
| 294 bin_map[NEVER_BIN] = NEVER_BIN; | |
| 295 } else if (global_state_.memory_limit_policy == ALLOW_ABSOLUTE_MINIMUM) { | |
| 296 bin_map[NOW_BIN] = NOW_BIN; | |
| 297 bin_map[SOON_BIN] = NEVER_BIN; | |
| 298 bin_map[EVENTUALLY_BIN] = NEVER_BIN; | |
| 299 bin_map[NEVER_BIN] = NEVER_BIN; | |
| 300 } else if (global_state_.memory_limit_policy == ALLOW_PREPAINT_ONLY) { | |
| 301 bin_map[NOW_BIN] = NOW_BIN; | |
| 302 bin_map[SOON_BIN] = SOON_BIN; | |
| 303 bin_map[EVENTUALLY_BIN] = NEVER_BIN; | |
| 304 bin_map[NEVER_BIN] = NEVER_BIN; | |
| 305 } else { | |
| 306 bin_map[NOW_BIN] = NOW_BIN; | |
| 307 bin_map[SOON_BIN] = SOON_BIN; | |
| 308 bin_map[EVENTUALLY_BIN] = EVENTUALLY_BIN; | |
| 309 bin_map[NEVER_BIN] = NEVER_BIN; | |
| 310 } | |
| 311 | |
| 312 live_or_allocated_tiles_.clear(); | |
| 313 // For each tree, bin into different categories of tiles. | |
| 314 for (TileSet::iterator it = all_tiles_.begin(); | |
| 315 it != all_tiles_.end(); ++it) { | |
| 316 Tile* tile = *it; | |
| 317 ManagedTileState& mts = tile->managed_state(); | |
| 318 | |
| 319 TilePriority prio[NUM_BIN_PRIORITIES]; | |
| 320 switch (tree_priority) { | |
| 321 case SAME_PRIORITY_FOR_BOTH_TREES: | |
| 322 prio[HIGH_PRIORITY_BIN] = prio[LOW_PRIORITY_BIN] = | |
| 323 tile->combined_priority(); | |
| 324 break; | |
| 325 case SMOOTHNESS_TAKES_PRIORITY: | |
| 326 prio[HIGH_PRIORITY_BIN] = tile->priority(ACTIVE_TREE); | |
| 327 prio[LOW_PRIORITY_BIN] = tile->priority(PENDING_TREE); | |
| 328 break; | |
| 329 case NEW_CONTENT_TAKES_PRIORITY: | |
| 330 prio[HIGH_PRIORITY_BIN] = tile->priority(PENDING_TREE); | |
| 331 prio[LOW_PRIORITY_BIN] = tile->priority(ACTIVE_TREE); | |
| 332 break; | |
| 333 } | |
| 334 | |
| 335 mts.resolution = prio[HIGH_PRIORITY_BIN].resolution; | |
| 336 mts.time_to_needed_in_seconds = | |
| 337 prio[HIGH_PRIORITY_BIN].time_to_visible_in_seconds; | |
| 338 mts.distance_to_visible_in_pixels = | |
| 339 prio[HIGH_PRIORITY_BIN].distance_to_visible_in_pixels; | |
| 340 mts.bin[HIGH_PRIORITY_BIN] = BinFromTilePriority(prio[HIGH_PRIORITY_BIN]); | |
| 341 mts.bin[LOW_PRIORITY_BIN] = BinFromTilePriority(prio[LOW_PRIORITY_BIN]); | |
| 342 mts.gpu_memmgr_stats_bin = BinFromTilePriority(tile->combined_priority()); | |
| 343 | |
| 344 DidTileTreeBinChange(tile, | |
| 345 bin_map[BinFromTilePriority(tile->priority(ACTIVE_TREE))], | |
| 346 ACTIVE_TREE); | |
| 347 DidTileTreeBinChange(tile, | |
| 348 bin_map[BinFromTilePriority(tile->priority(PENDING_TREE))], | |
| 349 PENDING_TREE); | |
| 350 | |
| 351 for (int i = 0; i < NUM_BIN_PRIORITIES; ++i) | |
| 352 mts.bin[i] = bin_map[mts.bin[i]]; | |
| 353 | |
| 354 if (!mts.drawing_info.resource_ && | |
| 355 !mts.drawing_info.resource_is_being_initialized_ && | |
| 356 !tile->priority(ACTIVE_TREE).is_live && | |
| 357 !tile->priority(PENDING_TREE).is_live) | |
| 358 continue; | |
| 359 | |
| 360 live_or_allocated_tiles_.push_back(tile); | |
| 361 } | |
| 362 TRACE_COUNTER_ID1("cc", "LiveOrAllocatedTileCount", this, | |
| 363 live_or_allocated_tiles_.size()); | |
| 364 | |
| 365 SortTiles(); | |
| 366 | |
| 367 // Assign gpu memory and determine what tiles need to be rasterized. | |
| 368 AssignGpuMemoryToTiles(); | |
| 369 | |
| 370 TRACE_EVENT_INSTANT1("cc", "DidManage", "state", | |
| 371 ValueToString(BasicStateAsValue())); | |
| 372 | |
| 373 // Finally, kick the rasterizer. | |
| 374 DispatchMoreTasks(); | |
| 375 } | |
| 376 | |
| 377 void TileManager::CheckForCompletedTileUploads() { | |
| 378 while (!tiles_with_pending_upload_.empty()) { | |
| 379 Tile* tile = tiles_with_pending_upload_.front(); | |
| 380 ManagedTileState& managed_tile_state = tile->managed_state(); | |
| 381 DCHECK(managed_tile_state.drawing_info.resource_); | |
| 382 | |
| 383 // Set pixel tasks complete in the order they are posted. | |
| 384 if (!resource_pool_->resource_provider()->DidSetPixelsComplete( | |
| 385 managed_tile_state.drawing_info.resource_->id())) { | |
| 386 break; | |
| 387 } | |
| 388 | |
| 389 // It's now safe to release the pixel buffer. | |
| 390 resource_pool_->resource_provider()->ReleasePixelBuffer( | |
| 391 managed_tile_state.drawing_info.resource_->id()); | |
| 392 | |
| 393 managed_tile_state.drawing_info.can_be_freed_ = true; | |
| 394 | |
| 395 bytes_pending_upload_ -= tile->bytes_consumed_if_allocated(); | |
| 396 DidTileRasterStateChange(tile, IDLE_STATE); | |
| 397 DidFinishTileInitialization(tile); | |
| 398 | |
| 399 tiles_with_pending_upload_.pop(); | |
| 400 } | |
| 401 | |
| 402 DispatchMoreTasks(); | |
| 403 } | |
| 404 | |
| 405 void TileManager::AbortPendingTileUploads() { | |
| 406 while (!tiles_with_pending_upload_.empty()) { | |
| 407 Tile* tile = tiles_with_pending_upload_.front(); | |
| 408 ManagedTileState& managed_tile_state = tile->managed_state(); | |
| 409 DCHECK(managed_tile_state.drawing_info.resource_); | |
| 410 | |
| 411 resource_pool_->resource_provider()->AbortSetPixels( | |
| 412 managed_tile_state.drawing_info.resource_->id()); | |
| 413 resource_pool_->resource_provider()->ReleasePixelBuffer( | |
| 414 managed_tile_state.drawing_info.resource_->id()); | |
| 415 | |
| 416 managed_tile_state.drawing_info.resource_is_being_initialized_ = false; | |
| 417 managed_tile_state.drawing_info.can_be_freed_ = true; | |
| 418 managed_tile_state.can_use_gpu_memory = false; | |
| 419 FreeResourcesForTile(tile); | |
| 420 | |
| 421 bytes_pending_upload_ -= tile->bytes_consumed_if_allocated(); | |
| 422 DidTileRasterStateChange(tile, IDLE_STATE); | |
| 423 tiles_with_pending_upload_.pop(); | |
| 424 } | |
| 425 } | |
| 426 | |
| 427 void TileManager::DidCompleteFrame() { | |
| 428 allow_cheap_tasks_ = true; | |
| 429 did_schedule_cheap_tasks_ = false; | |
| 430 } | |
| 431 | |
| 432 void TileManager::ForceTileUploadToComplete(Tile* tile) { | |
| 433 ManagedTileState& managed_tile_state = tile->managed_state(); | |
| 434 if (managed_tile_state.raster_state == UPLOAD_STATE) { | |
| 435 Resource* resource = tile->drawing_info().resource_.get(); | |
| 436 DCHECK(resource); | |
| 437 resource_pool_->resource_provider()-> | |
| 438 ForceSetPixelsToComplete(resource->id()); | |
| 439 DidTileRasterStateChange(tile, FORCED_UPLOAD_COMPLETION_STATE); | |
| 440 DidFinishTileInitialization(tile); | |
| 441 } | |
| 442 } | |
| 443 | |
| 444 void TileManager::GetMemoryStats( | |
| 445 size_t* memoryRequiredBytes, | |
| 446 size_t* memoryNiceToHaveBytes, | |
| 447 size_t* memoryUsedBytes) const { | |
| 448 *memoryRequiredBytes = 0; | |
| 449 *memoryNiceToHaveBytes = 0; | |
| 450 *memoryUsedBytes = 0; | |
| 451 for (size_t i = 0; i < live_or_allocated_tiles_.size(); i++) { | |
| 452 const Tile* tile = live_or_allocated_tiles_[i]; | |
| 453 const ManagedTileState& mts = tile->managed_state(); | |
| 454 if (!tile->drawing_info().requires_resource()) | |
| 455 continue; | |
| 456 | |
| 457 size_t tile_bytes = tile->bytes_consumed_if_allocated(); | |
| 458 if (mts.gpu_memmgr_stats_bin == NOW_BIN) | |
| 459 *memoryRequiredBytes += tile_bytes; | |
| 460 if (mts.gpu_memmgr_stats_bin != NEVER_BIN) | |
| 461 *memoryNiceToHaveBytes += tile_bytes; | |
| 462 if (mts.can_use_gpu_memory) | |
| 463 *memoryUsedBytes += tile_bytes; | |
| 464 } | |
| 465 } | |
| 466 | |
| 467 scoped_ptr<base::Value> TileManager::BasicStateAsValue() const { | |
| 468 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue()); | |
| 469 state->SetInteger("tile_count", all_tiles_.size()); | |
| 470 state->Set("global_state", global_state_.AsValue().release()); | |
| 471 state->Set("memory_requirements", GetMemoryRequirementsAsValue().release()); | |
| 472 return state.PassAs<base::Value>(); | |
| 473 } | |
| 474 scoped_ptr<base::Value> TileManager::AllTilesAsValue() const { | |
| 475 scoped_ptr<base::ListValue> state(new base::ListValue()); | |
| 476 for (TileSet::const_iterator it = all_tiles_.begin(); | |
| 477 it != all_tiles_.end(); it++) { | |
| 478 state->Append((*it)->AsValue().release()); | |
| 479 } | |
| 480 return state.PassAs<base::Value>(); | |
| 481 } | |
| 482 | |
| 483 scoped_ptr<base::Value> TileManager::GetMemoryRequirementsAsValue() const { | |
| 484 scoped_ptr<base::DictionaryValue> requirements( | |
| 485 new base::DictionaryValue()); | |
| 486 | |
| 487 size_t memoryRequiredBytes; | |
| 488 size_t memoryNiceToHaveBytes; | |
| 489 size_t memoryUsedBytes; | |
| 490 GetMemoryStats(&memoryRequiredBytes, | |
| 491 &memoryNiceToHaveBytes, | |
| 492 &memoryUsedBytes); | |
| 493 requirements->SetInteger("memory_required_bytes", memoryRequiredBytes); | |
| 494 requirements->SetInteger("memory_nice_to_have_bytes", memoryNiceToHaveBytes); | |
| 495 requirements->SetInteger("memory_used_bytes", memoryUsedBytes); | |
| 496 return requirements.PassAs<base::Value>(); | |
| 497 } | |
| 498 | |
| 499 void TileManager::SetRecordRenderingStats(bool record_rendering_stats) { | |
| 500 if (record_rendering_stats_ == record_rendering_stats) | |
| 501 return; | |
| 502 | |
| 503 record_rendering_stats_ = record_rendering_stats; | |
| 504 raster_worker_pool_->SetRecordRenderingStats(record_rendering_stats); | |
| 505 } | |
| 506 | |
| 507 void TileManager::GetRenderingStats(RenderingStats* stats) { | |
| 508 CHECK(record_rendering_stats_); | |
| 509 raster_worker_pool_->GetRenderingStats(stats); | |
| 510 stats->totalDeferredImageCacheHitCount = | |
| 511 rendering_stats_.totalDeferredImageCacheHitCount; | |
| 512 stats->totalImageGatheringCount = rendering_stats_.totalImageGatheringCount; | |
| 513 stats->totalImageGatheringTime = | |
| 514 rendering_stats_.totalImageGatheringTime; | |
| 515 } | |
| 516 | |
| 517 bool TileManager::HasPendingWorkScheduled(WhichTree tree) const { | |
| 518 // Always true when ManageTiles() call is pending. | |
| 519 if (manage_tiles_pending_) | |
| 520 return true; | |
| 521 | |
| 522 for (int i = 0; i < NUM_STATES; ++i) { | |
| 523 switch (i) { | |
| 524 case WAITING_FOR_RASTER_STATE: | |
| 525 case RASTER_STATE: | |
| 526 case UPLOAD_STATE: | |
| 527 case FORCED_UPLOAD_COMPLETION_STATE: | |
| 528 for (int j = 0; j < NEVER_BIN; ++j) { | |
| 529 if (raster_state_count_[i][tree][j]) | |
| 530 return true; | |
| 531 } | |
| 532 break; | |
| 533 case IDLE_STATE: | |
| 534 break; | |
| 535 default: | |
| 536 NOTREACHED(); | |
| 537 } | |
| 538 } | |
| 539 | |
| 540 return false; | |
| 541 } | |
| 542 | |
| 543 void TileManager::DidFinishDispatchingWorkerPoolCompletionCallbacks() { | |
| 544 // If a flush is needed, do it now before starting to dispatch more tasks. | |
| 545 if (has_performed_uploads_since_last_flush_) { | |
| 546 resource_pool_->resource_provider()->ShallowFlushIfSupported(); | |
| 547 has_performed_uploads_since_last_flush_ = false; | |
| 548 } | |
| 549 | |
| 550 DispatchMoreTasks(); | |
| 551 } | |
| 552 | |
| 553 void TileManager::AssignGpuMemoryToTiles() { | |
| 554 TRACE_EVENT0("cc", "TileManager::AssignGpuMemoryToTiles"); | |
| 555 size_t unreleasable_bytes = 0; | |
| 556 | |
| 557 // Now give memory out to the tiles until we're out, and build | |
| 558 // the needs-to-be-rasterized queue. | |
| 559 tiles_that_need_to_be_rasterized_.clear(); | |
| 560 | |
| 561 // Reset the image decoding list so that we don't mess up with tile | |
| 562 // priorities. Tiles will be added to the image decoding list again | |
| 563 // when DispatchMoreTasks() is called. | |
| 564 tiles_with_image_decoding_tasks_.clear(); | |
| 565 | |
| 566 // By clearing the tiles_that_need_to_be_rasterized_ vector and | |
| 567 // tiles_with_image_decoding_tasks_ list above we move all tiles | |
| 568 // currently waiting for raster to idle state. | |
| 569 // Call DidTileRasterStateChange() for each of these tiles to | |
| 570 // have this state change take effect. | |
| 571 // Some memory cannot be released. We figure out how much in this | |
| 572 // loop as well. | |
| 573 for (TileVector::iterator it = live_or_allocated_tiles_.begin(); | |
| 574 it != live_or_allocated_tiles_.end(); ++it) { | |
| 575 Tile* tile = *it; | |
| 576 ManagedTileState& mts = tile->managed_state(); | |
| 577 if (!tile->drawing_info().requires_resource()) | |
| 578 continue; | |
| 579 | |
| 580 if (!mts.drawing_info.can_be_freed_) | |
| 581 unreleasable_bytes += tile->bytes_consumed_if_allocated(); | |
| 582 if (mts.raster_state == WAITING_FOR_RASTER_STATE) | |
| 583 DidTileRasterStateChange(tile, IDLE_STATE); | |
| 584 } | |
| 585 | |
| 586 size_t bytes_allocatable = global_state_.memory_limit_in_bytes - unreleasable_
bytes; | |
| 587 size_t bytes_that_exceeded_memory_budget_in_now_bin = 0; | |
| 588 size_t bytes_left = bytes_allocatable; | |
| 589 for (TileVector::iterator it = live_or_allocated_tiles_.begin(); it != live_or
_allocated_tiles_.end(); ++it) { | |
| 590 Tile* tile = *it; | |
| 591 ManagedTileState& mts = tile->managed_state(); | |
| 592 if (!tile->drawing_info().requires_resource()) | |
| 593 continue; | |
| 594 | |
| 595 size_t tile_bytes = tile->bytes_consumed_if_allocated(); | |
| 596 if (!mts.drawing_info.can_be_freed_) | |
| 597 continue; | |
| 598 if (mts.bin[HIGH_PRIORITY_BIN] == NEVER_BIN && | |
| 599 mts.bin[LOW_PRIORITY_BIN] == NEVER_BIN) { | |
| 600 mts.can_use_gpu_memory = false; | |
| 601 FreeResourcesForTile(tile); | |
| 602 continue; | |
| 603 } | |
| 604 if (tile_bytes > bytes_left) { | |
| 605 mts.can_use_gpu_memory = false; | |
| 606 if (mts.bin[HIGH_PRIORITY_BIN] == NOW_BIN || | |
| 607 mts.bin[LOW_PRIORITY_BIN] == NOW_BIN) | |
| 608 bytes_that_exceeded_memory_budget_in_now_bin += tile_bytes; | |
| 609 FreeResourcesForTile(tile); | |
| 610 continue; | |
| 611 } | |
| 612 bytes_left -= tile_bytes; | |
| 613 mts.can_use_gpu_memory = true; | |
| 614 if (!mts.drawing_info.resource_ && | |
| 615 !mts.drawing_info.resource_is_being_initialized_) { | |
| 616 tiles_that_need_to_be_rasterized_.push_back(tile); | |
| 617 DidTileRasterStateChange(tile, WAITING_FOR_RASTER_STATE); | |
| 618 } | |
| 619 } | |
| 620 | |
| 621 ever_exceeded_memory_budget_ |= | |
| 622 bytes_that_exceeded_memory_budget_in_now_bin > 0; | |
| 623 if (ever_exceeded_memory_budget_) { | |
| 624 TRACE_COUNTER_ID2("cc", "over_memory_budget", this, | |
| 625 "budget", global_state_.memory_limit_in_bytes, | |
| 626 "over", bytes_that_exceeded_memory_budget_in_now_bin); | |
| 627 } | |
| 628 memory_stats_from_last_assign_.total_budget_in_bytes = | |
| 629 global_state_.memory_limit_in_bytes; | |
| 630 memory_stats_from_last_assign_.bytes_allocated = | |
| 631 bytes_allocatable - bytes_left; | |
| 632 memory_stats_from_last_assign_.bytes_unreleasable = unreleasable_bytes; | |
| 633 memory_stats_from_last_assign_.bytes_over = | |
| 634 bytes_that_exceeded_memory_budget_in_now_bin; | |
| 635 | |
| 636 // Reverse two tiles_that_need_* vectors such that pop_back gets | |
| 637 // the highest priority tile. | |
| 638 std::reverse( | |
| 639 tiles_that_need_to_be_rasterized_.begin(), | |
| 640 tiles_that_need_to_be_rasterized_.end()); | |
| 641 } | |
| 642 | |
| 643 void TileManager::FreeResourcesForTile(Tile* tile) { | |
| 644 ManagedTileState& managed_tile_state = tile->managed_state(); | |
| 645 DCHECK(managed_tile_state.drawing_info.can_be_freed_); | |
| 646 if (managed_tile_state.drawing_info.resource_) | |
| 647 resource_pool_->ReleaseResource( | |
| 648 managed_tile_state.drawing_info.resource_.Pass()); | |
| 649 } | |
| 650 | |
| 651 bool TileManager::CanDispatchRasterTask(Tile* tile) const { | |
| 652 if (pending_tasks_ >= max_pending_tasks_) | |
| 653 return false; | |
| 654 size_t new_bytes_pending = bytes_pending_upload_; | |
| 655 new_bytes_pending += tile->bytes_consumed_if_allocated(); | |
| 656 return new_bytes_pending <= kMaxPendingUploadBytes && | |
| 657 tiles_with_pending_upload_.size() < kMaxPendingUploads; | |
| 658 } | |
| 659 | |
| 660 void TileManager::DispatchMoreTasks() { | |
| 661 if (did_schedule_cheap_tasks_) | |
| 662 allow_cheap_tasks_ = false; | |
| 663 | |
| 664 // Because tiles in the image decoding list have higher priorities, we | |
| 665 // need to process those tiles first before we start to handle the tiles | |
| 666 // in the need_to_be_rasterized queue. Note that solid/transparent tiles | |
| 667 // will not be put into the decoding list. | |
| 668 for(TileList::iterator it = tiles_with_image_decoding_tasks_.begin(); | |
| 669 it != tiles_with_image_decoding_tasks_.end(); ) { | |
| 670 ManagedTileState& managed_tile_state = (*it)->managed_state(); | |
| 671 DispatchImageDecodeTasksForTile(*it); | |
| 672 if (managed_tile_state.pending_pixel_refs.empty()) { | |
| 673 if (!CanDispatchRasterTask(*it)) | |
| 674 return; | |
| 675 DispatchOneRasterTask(*it); | |
| 676 tiles_with_image_decoding_tasks_.erase(it++); | |
| 677 } else { | |
| 678 ++it; | |
| 679 } | |
| 680 } | |
| 681 | |
| 682 // Process all tiles in the need_to_be_rasterized queue. If a tile is | |
| 683 // solid/transparent, then we are done (we don't need to rasterize | |
| 684 // the tile). If a tile has image decoding tasks, put it to the back | |
| 685 // of the image decoding list. | |
| 686 while (!tiles_that_need_to_be_rasterized_.empty()) { | |
| 687 Tile* tile = tiles_that_need_to_be_rasterized_.back(); | |
| 688 ManagedTileState& mts = tile->managed_state(); | |
| 689 | |
| 690 AnalyzeTile(tile); | |
| 691 if (!tile->drawing_info().requires_resource()) { | |
| 692 DidTileRasterStateChange(tile, IDLE_STATE); | |
| 693 tiles_that_need_to_be_rasterized_.pop_back(); | |
| 694 continue; | |
| 695 } | |
| 696 | |
| 697 DispatchImageDecodeTasksForTile(tile); | |
| 698 if (!mts.pending_pixel_refs.empty()) { | |
| 699 tiles_with_image_decoding_tasks_.push_back(tile); | |
| 700 } else { | |
| 701 if (!CanDispatchRasterTask(tile)) | |
| 702 return; | |
| 703 DispatchOneRasterTask(tile); | |
| 704 } | |
| 705 tiles_that_need_to_be_rasterized_.pop_back(); | |
| 706 } | |
| 707 } | |
| 708 | |
| 709 void TileManager::AnalyzeTile(Tile* tile) { | |
| 710 ManagedTileState& managed_tile_state = tile->managed_state(); | |
| 711 if ((use_cheapness_estimator_ || use_color_estimator_) && | |
| 712 !managed_tile_state.picture_pile_analyzed) { | |
| 713 tile->picture_pile()->AnalyzeInRect( | |
| 714 tile->content_rect(), | |
| 715 tile->contents_scale(), | |
| 716 &managed_tile_state.picture_pile_analysis); | |
| 717 managed_tile_state.picture_pile_analysis.is_cheap_to_raster &= | |
| 718 use_cheapness_estimator_; | |
| 719 managed_tile_state.picture_pile_analysis.is_solid_color &= | |
| 720 use_color_estimator_; | |
| 721 managed_tile_state.picture_pile_analysis.is_transparent &= | |
| 722 use_color_estimator_; | |
| 723 managed_tile_state.picture_pile_analyzed = true; | |
| 724 | |
| 725 if (managed_tile_state.picture_pile_analysis.is_solid_color) { | |
| 726 tile->drawing_info().set_solid_color( | |
| 727 managed_tile_state.picture_pile_analysis.solid_color); | |
| 728 DidFinishTileInitialization(tile); | |
| 729 } else if (managed_tile_state.picture_pile_analysis.is_transparent) { | |
| 730 tile->drawing_info().set_transparent(); | |
| 731 DidFinishTileInitialization(tile); | |
| 732 } | |
| 733 } | |
| 734 } | |
| 735 | |
| 736 void TileManager::GatherPixelRefsForTile(Tile* tile) { | |
| 737 TRACE_EVENT0("cc", "TileManager::GatherPixelRefsForTile"); | |
| 738 ManagedTileState& managed_tile_state = tile->managed_state(); | |
| 739 if (managed_tile_state.need_to_gather_pixel_refs) { | |
| 740 base::TimeTicks gather_begin_time; | |
| 741 if (record_rendering_stats_) | |
| 742 gather_begin_time = base::TimeTicks::HighResNow(); | |
| 743 tile->picture_pile()->GatherPixelRefs( | |
| 744 tile->content_rect_, | |
| 745 tile->contents_scale_, | |
| 746 managed_tile_state.pending_pixel_refs); | |
| 747 managed_tile_state.need_to_gather_pixel_refs = false; | |
| 748 if (record_rendering_stats_) { | |
| 749 rendering_stats_.totalImageGatheringCount++; | |
| 750 rendering_stats_.totalImageGatheringTime += | |
| 751 base::TimeTicks::HighResNow() - gather_begin_time; | |
| 752 } | |
| 753 } | |
| 754 } | |
| 755 | |
| 756 void TileManager::DispatchImageDecodeTasksForTile(Tile* tile) { | |
| 757 GatherPixelRefsForTile(tile); | |
| 758 std::list<skia::LazyPixelRef*>& pending_pixel_refs = | |
| 759 tile->managed_state().pending_pixel_refs; | |
| 760 std::list<skia::LazyPixelRef*>::iterator it = pending_pixel_refs.begin(); | |
| 761 while (it != pending_pixel_refs.end()) { | |
| 762 if (pending_decode_tasks_.end() != pending_decode_tasks_.find( | |
| 763 (*it)->getGenerationID())) { | |
| 764 ++it; | |
| 765 continue; | |
| 766 } | |
| 767 // TODO(qinmin): passing correct image size to PrepareToDecode(). | |
| 768 if ((*it)->PrepareToDecode(skia::LazyPixelRef::PrepareParams())) { | |
| 769 rendering_stats_.totalDeferredImageCacheHitCount++; | |
| 770 pending_pixel_refs.erase(it++); | |
| 771 } else { | |
| 772 if (pending_tasks_ >= max_pending_tasks_) | |
| 773 return; | |
| 774 DispatchOneImageDecodeTask(tile, *it); | |
| 775 ++it; | |
| 776 } | |
| 777 } | |
| 778 } | |
| 779 | |
| 780 void TileManager::DispatchOneImageDecodeTask( | |
| 781 scoped_refptr<Tile> tile, skia::LazyPixelRef* pixel_ref) { | |
| 782 TRACE_EVENT0("cc", "TileManager::DispatchOneImageDecodeTask"); | |
| 783 uint32_t pixel_ref_id = pixel_ref->getGenerationID(); | |
| 784 DCHECK(pending_decode_tasks_.end() == | |
| 785 pending_decode_tasks_.find(pixel_ref_id)); | |
| 786 pending_decode_tasks_[pixel_ref_id] = pixel_ref; | |
| 787 | |
| 788 raster_worker_pool_->PostTaskAndReply( | |
| 789 base::Bind(&TileManager::RunImageDecodeTask, pixel_ref), | |
| 790 base::Bind(&TileManager::OnImageDecodeTaskCompleted, | |
| 791 base::Unretained(this), | |
| 792 tile, | |
| 793 pixel_ref_id)); | |
| 794 pending_tasks_++; | |
| 795 } | |
| 796 | |
| 797 void TileManager::OnImageDecodeTaskCompleted( | |
| 798 scoped_refptr<Tile> tile, uint32_t pixel_ref_id) { | |
| 799 TRACE_EVENT0("cc", "TileManager::OnImageDecodeTaskCompleted"); | |
| 800 pending_decode_tasks_.erase(pixel_ref_id); | |
| 801 pending_tasks_--; | |
| 802 | |
| 803 for (TileList::iterator it = tiles_with_image_decoding_tasks_.begin(); | |
| 804 it != tiles_with_image_decoding_tasks_.end(); ++it) { | |
| 805 std::list<skia::LazyPixelRef*>& pixel_refs = | |
| 806 (*it)->managed_state().pending_pixel_refs; | |
| 807 for (std::list<skia::LazyPixelRef*>::iterator pixel_it = | |
| 808 pixel_refs.begin(); pixel_it != pixel_refs.end(); ++pixel_it) { | |
| 809 if (pixel_ref_id == (*pixel_it)->getGenerationID()) { | |
| 810 pixel_refs.erase(pixel_it); | |
| 811 break; | |
| 812 } | |
| 813 } | |
| 814 } | |
| 815 } | |
| 816 | |
| 817 scoped_ptr<ResourcePool::Resource> TileManager::PrepareTileForRaster( | |
| 818 Tile* tile) { | |
| 819 ManagedTileState& managed_tile_state = tile->managed_state(); | |
| 820 DCHECK(managed_tile_state.can_use_gpu_memory); | |
| 821 scoped_ptr<ResourcePool::Resource> resource = | |
| 822 resource_pool_->AcquireResource(tile->tile_size_.size(), tile->format_); | |
| 823 resource_pool_->resource_provider()->AcquirePixelBuffer(resource->id()); | |
| 824 | |
| 825 managed_tile_state.drawing_info.resource_is_being_initialized_ = true; | |
| 826 managed_tile_state.drawing_info.can_be_freed_ = false; | |
| 827 | |
| 828 DidTileRasterStateChange(tile, RASTER_STATE); | |
| 829 return resource.Pass(); | |
| 830 } | |
| 831 | |
| 832 void TileManager::DispatchOneRasterTask(scoped_refptr<Tile> tile) { | |
| 833 TRACE_EVENT0("cc", "TileManager::DispatchOneRasterTask"); | |
| 834 scoped_ptr<ResourcePool::Resource> resource = PrepareTileForRaster(tile); | |
| 835 ResourceProvider::ResourceId resource_id = resource->id(); | |
| 836 uint8* buffer = | |
| 837 resource_pool_->resource_provider()->MapPixelBuffer(resource_id); | |
| 838 | |
| 839 ManagedTileState& managed_tile_state = tile->managed_state(); | |
| 840 // TODO(skyostil): Post all cheap tasks as cheap and instead use the time | |
| 841 // limit to control their execution. | |
| 842 bool is_cheap_task = | |
| 843 allow_cheap_tasks_ && | |
| 844 global_state_.tree_priority != SMOOTHNESS_TAKES_PRIORITY && | |
| 845 managed_tile_state.picture_pile_analysis.is_cheap_to_raster; | |
| 846 raster_worker_pool_->PostRasterTaskAndReply( | |
| 847 tile->picture_pile(), | |
| 848 is_cheap_task, | |
| 849 base::Bind(&TileManager::RunRasterTask, | |
| 850 buffer, | |
| 851 tile->content_rect(), | |
| 852 tile->contents_scale(), | |
| 853 GetRasterTaskMetadata(*tile)), | |
| 854 base::Bind(&TileManager::OnRasterTaskCompleted, | |
| 855 base::Unretained(this), | |
| 856 tile, | |
| 857 base::Passed(&resource), | |
| 858 manage_tiles_call_count_)); | |
| 859 if (is_cheap_task && !did_schedule_cheap_tasks_) { | |
| 860 raster_worker_pool_->SetRunCheapTasksTimeLimit( | |
| 861 base::TimeTicks::Now() + | |
| 862 base::TimeDelta::FromMilliseconds(kRunCheapTasksTimeMs)); | |
| 863 did_schedule_cheap_tasks_ = true; | |
| 864 } | |
| 865 pending_tasks_++; | |
| 866 } | |
| 867 | |
| 868 TileManager::RasterTaskMetadata TileManager::GetRasterTaskMetadata( | |
| 869 const Tile& tile) const { | |
| 870 RasterTaskMetadata metadata; | |
| 871 const ManagedTileState& mts = tile.managed_state(); | |
| 872 metadata.prediction_benchmarking = prediction_benchmarking_; | |
| 873 metadata.is_tile_in_pending_tree_now_bin = | |
| 874 mts.tree_bin[PENDING_TREE] == NOW_BIN; | |
| 875 metadata.tile_resolution = mts.resolution; | |
| 876 metadata.layer_id = tile.layer_id(); | |
| 877 return metadata; | |
| 878 } | |
| 879 | |
| 880 void TileManager::OnRasterTaskCompleted( | |
| 881 scoped_refptr<Tile> tile, | |
| 882 scoped_ptr<ResourcePool::Resource> resource, | |
| 883 int manage_tiles_call_count_when_dispatched) { | |
| 884 TRACE_EVENT0("cc", "TileManager::OnRasterTaskCompleted"); | |
| 885 | |
| 886 pending_tasks_--; | |
| 887 | |
| 888 // Release raster resources. | |
| 889 resource_pool_->resource_provider()->UnmapPixelBuffer(resource->id()); | |
| 890 | |
| 891 ManagedTileState& managed_tile_state = tile->managed_state(); | |
| 892 managed_tile_state.drawing_info.can_be_freed_ = true; | |
| 893 | |
| 894 // Tile can be freed after the completion of the raster task. Call | |
| 895 // AssignGpuMemoryToTiles() to re-assign gpu memory to highest priority | |
| 896 // tiles if ManageTiles() was called since task was dispatched. The result | |
| 897 // of this could be that this tile is no longer allowed to use gpu | |
| 898 // memory and in that case we need to abort initialization and free all | |
| 899 // associated resources before calling DispatchMoreTasks(). | |
| 900 if (manage_tiles_call_count_when_dispatched != manage_tiles_call_count_) | |
| 901 AssignGpuMemoryToTiles(); | |
| 902 | |
| 903 // Finish resource initialization if |can_use_gpu_memory| is true. | |
| 904 if (managed_tile_state.can_use_gpu_memory) { | |
| 905 // The component order may be bgra if we're uploading bgra pixels to rgba | |
| 906 // texture. Mark contents as swizzled if image component order is | |
| 907 // different than texture format. | |
| 908 managed_tile_state.drawing_info.contents_swizzled_ = | |
| 909 !PlatformColor::SameComponentOrder(tile->format_); | |
| 910 | |
| 911 // Tile resources can't be freed until upload has completed. | |
| 912 managed_tile_state.drawing_info.can_be_freed_ = false; | |
| 913 | |
| 914 resource_pool_->resource_provider()->BeginSetPixels(resource->id()); | |
| 915 has_performed_uploads_since_last_flush_ = true; | |
| 916 | |
| 917 managed_tile_state.drawing_info.resource_ = resource.Pass(); | |
| 918 | |
| 919 bytes_pending_upload_ += tile->bytes_consumed_if_allocated(); | |
| 920 DidTileRasterStateChange(tile, UPLOAD_STATE); | |
| 921 tiles_with_pending_upload_.push(tile); | |
| 922 } else { | |
| 923 resource_pool_->resource_provider()->ReleasePixelBuffer(resource->id()); | |
| 924 resource_pool_->ReleaseResource(resource.Pass()); | |
| 925 managed_tile_state.drawing_info.resource_is_being_initialized_ = false; | |
| 926 DidTileRasterStateChange(tile, IDLE_STATE); | |
| 927 } | |
| 928 } | |
| 929 | |
| 930 void TileManager::DidFinishTileInitialization(Tile* tile) { | |
| 931 ManagedTileState& managed_state = tile->managed_state(); | |
| 932 managed_state.drawing_info.resource_is_being_initialized_ = false; | |
| 933 if (tile->priority(ACTIVE_TREE).distance_to_visible_in_pixels == 0) | |
| 934 client_->DidInitializeVisibleTile(); | |
| 935 } | |
| 936 | |
| 937 void TileManager::DidTileRasterStateChange(Tile* tile, TileRasterState state) { | |
| 938 ManagedTileState& mts = tile->managed_state(); | |
| 939 DCHECK_LT(state, NUM_STATES); | |
| 940 | |
| 941 for (int i = 0; i < NUM_TREES; ++i) { | |
| 942 // Decrement count for current state. | |
| 943 --raster_state_count_[mts.raster_state][i][mts.tree_bin[i]]; | |
| 944 DCHECK_GE(raster_state_count_[mts.raster_state][i][mts.tree_bin[i]], 0); | |
| 945 | |
| 946 // Increment count for new state. | |
| 947 ++raster_state_count_[state][i][mts.tree_bin[i]]; | |
| 948 } | |
| 949 | |
| 950 mts.raster_state = state; | |
| 951 } | |
| 952 | |
| 953 void TileManager::DidTileTreeBinChange(Tile* tile, | |
| 954 TileManagerBin new_tree_bin, | |
| 955 WhichTree tree) { | |
| 956 ManagedTileState& mts = tile->managed_state(); | |
| 957 | |
| 958 // Decrement count for current bin. | |
| 959 --raster_state_count_[mts.raster_state][tree][mts.tree_bin[tree]]; | |
| 960 DCHECK_GE(raster_state_count_[mts.raster_state][tree][mts.tree_bin[tree]], 0); | |
| 961 | |
| 962 // Increment count for new bin. | |
| 963 ++raster_state_count_[mts.raster_state][tree][new_tree_bin]; | |
| 964 | |
| 965 mts.tree_bin[tree] = new_tree_bin; | |
| 966 } | |
| 967 | |
| 968 // static | |
| 969 void TileManager::RunRasterTask(uint8* buffer, | |
| 970 const gfx::Rect& rect, | |
| 971 float contents_scale, | |
| 972 const RasterTaskMetadata& metadata, | |
| 973 PicturePileImpl* picture_pile, | |
| 974 RenderingStats* stats) { | |
| 975 TRACE_EVENT2( | |
| 976 "cc", "TileManager::RunRasterTask", | |
| 977 "is_on_pending_tree", | |
| 978 metadata.is_tile_in_pending_tree_now_bin, | |
| 979 "is_low_res", | |
| 980 metadata.tile_resolution == LOW_RESOLUTION); | |
| 981 devtools_instrumentation::ScopedRasterTask raster_task(metadata.layer_id); | |
| 982 | |
| 983 DCHECK(picture_pile); | |
| 984 DCHECK(buffer); | |
| 985 | |
| 986 SkBitmap bitmap; | |
| 987 bitmap.setConfig(SkBitmap::kARGB_8888_Config, rect.width(), rect.height()); | |
| 988 bitmap.setPixels(buffer); | |
| 989 SkDevice device(bitmap); | |
| 990 SkCanvas canvas(&device); | |
| 991 | |
| 992 base::TimeTicks begin_time; | |
| 993 if (stats) | |
| 994 begin_time = base::TimeTicks::HighResNow(); | |
| 995 | |
| 996 int64 total_pixels_rasterized = 0; | |
| 997 picture_pile->Raster(&canvas, rect, contents_scale, | |
| 998 &total_pixels_rasterized); | |
| 999 | |
| 1000 if (stats) { | |
| 1001 stats->totalPixelsRasterized += total_pixels_rasterized; | |
| 1002 | |
| 1003 base::TimeTicks end_time = base::TimeTicks::HighResNow(); | |
| 1004 base::TimeDelta duration = end_time - begin_time; | |
| 1005 stats->totalRasterizeTime += duration; | |
| 1006 if (metadata.is_tile_in_pending_tree_now_bin) | |
| 1007 stats->totalRasterizeTimeForNowBinsOnPendingTree += duration; | |
| 1008 | |
| 1009 UMA_HISTOGRAM_CUSTOM_COUNTS("Renderer4.PictureRasterTimeMS", | |
| 1010 duration.InMilliseconds(), | |
| 1011 0, | |
| 1012 10, | |
| 1013 10); | |
| 1014 | |
| 1015 if (metadata.prediction_benchmarking) { | |
| 1016 PicturePileImpl::Analysis analysis; | |
| 1017 picture_pile->AnalyzeInRect(rect, contents_scale, &analysis); | |
| 1018 bool is_predicted_cheap = analysis.is_cheap_to_raster; | |
| 1019 bool is_actually_cheap = duration.InMillisecondsF() <= 1.0f; | |
| 1020 RecordCheapnessPredictorResults(is_predicted_cheap, is_actually_cheap); | |
| 1021 | |
| 1022 DCHECK_EQ(bitmap.rowBytes(), | |
| 1023 bitmap.width() * bitmap.bytesPerPixel()); | |
| 1024 | |
| 1025 RecordSolidColorPredictorResults( | |
| 1026 reinterpret_cast<SkColor*>(bitmap.getPixels()), | |
| 1027 bitmap.getSize() / bitmap.bytesPerPixel(), | |
| 1028 analysis.is_solid_color, | |
| 1029 analysis.solid_color, | |
| 1030 analysis.is_transparent); | |
| 1031 } | |
| 1032 } | |
| 1033 } | |
| 1034 | |
| 1035 // static | |
| 1036 void TileManager::RecordCheapnessPredictorResults(bool is_predicted_cheap, | |
| 1037 bool is_actually_cheap) { | |
| 1038 if (is_predicted_cheap && !is_actually_cheap) | |
| 1039 UMA_HISTOGRAM_BOOLEAN("Renderer4.CheapPredictorBadlyWrong", true); | |
| 1040 else if (!is_predicted_cheap && is_actually_cheap) | |
| 1041 UMA_HISTOGRAM_BOOLEAN("Renderer4.CheapPredictorSafelyWrong", true); | |
| 1042 | |
| 1043 UMA_HISTOGRAM_BOOLEAN("Renderer4.CheapPredictorAccuracy", | |
| 1044 is_predicted_cheap == is_actually_cheap); | |
| 1045 } | |
| 1046 | |
| 1047 // static | |
| 1048 void TileManager::RecordSolidColorPredictorResults( | |
| 1049 const SkColor* actual_colors, | |
| 1050 size_t color_count, | |
| 1051 bool is_predicted_solid, | |
| 1052 SkColor predicted_color, | |
| 1053 bool is_predicted_transparent) { | |
| 1054 DCHECK_GT(color_count, 0u); | |
| 1055 | |
| 1056 bool is_actually_solid = true; | |
| 1057 bool is_transparent = true; | |
| 1058 | |
| 1059 SkColor actual_color = *actual_colors; | |
| 1060 for (unsigned int i = 0; i < color_count; ++i) { | |
| 1061 SkColor current_color = actual_colors[i]; | |
| 1062 if (current_color != actual_color || | |
| 1063 SkColorGetA(current_color) != 255) | |
| 1064 is_actually_solid = false; | |
| 1065 | |
| 1066 if (SkColorGetA(current_color) != 0) | |
| 1067 is_transparent = false; | |
| 1068 | |
| 1069 if(!is_actually_solid && !is_transparent) | |
| 1070 break; | |
| 1071 } | |
| 1072 | |
| 1073 if (is_predicted_solid && !is_actually_solid) | |
| 1074 UMA_HISTOGRAM_BOOLEAN("Renderer4.ColorPredictor.WrongActualNotSolid", true); | |
| 1075 else if (is_predicted_solid && | |
| 1076 is_actually_solid && | |
| 1077 predicted_color != actual_color) | |
| 1078 UMA_HISTOGRAM_BOOLEAN("Renderer4.ColorPredictor.WrongColor", true); | |
| 1079 else if (!is_predicted_solid && is_actually_solid) | |
| 1080 UMA_HISTOGRAM_BOOLEAN("Renderer4.ColorPredictor.WrongActualSolid", true); | |
| 1081 | |
| 1082 bool correct_guess = (is_predicted_solid && is_actually_solid && | |
| 1083 predicted_color == actual_color) || | |
| 1084 (!is_predicted_solid && !is_actually_solid); | |
| 1085 UMA_HISTOGRAM_BOOLEAN("Renderer4.ColorPredictor.Accuracy", correct_guess); | |
| 1086 | |
| 1087 if (correct_guess) | |
| 1088 UMA_HISTOGRAM_BOOLEAN("Renderer4.ColorPredictor.IsCorrectSolid", | |
| 1089 is_predicted_solid); | |
| 1090 | |
| 1091 if (is_predicted_transparent) | |
| 1092 UMA_HISTOGRAM_BOOLEAN( | |
| 1093 "Renderer4.ColorPredictor.PredictedTransparentIsActually", | |
| 1094 is_transparent); | |
| 1095 UMA_HISTOGRAM_BOOLEAN("Renderer4.ColorPredictor.IsActuallyTransparent", | |
| 1096 is_transparent); | |
| 1097 } | |
| 1098 | |
| 1099 // static | |
| 1100 void TileManager::RunImageDecodeTask(skia::LazyPixelRef* pixel_ref, | |
| 1101 RenderingStats* stats) { | |
| 1102 TRACE_EVENT0("cc", "TileManager::RunImageDecodeTask"); | |
| 1103 base::TimeTicks decode_begin_time; | |
| 1104 if (stats) | |
| 1105 decode_begin_time = base::TimeTicks::HighResNow(); | |
| 1106 pixel_ref->Decode(); | |
| 1107 if (stats) { | |
| 1108 stats->totalDeferredImageDecodeCount++; | |
| 1109 stats->totalDeferredImageDecodeTime += | |
| 1110 base::TimeTicks::HighResNow() - decode_begin_time; | |
| 1111 } | |
| 1112 } | |
| 1113 | |
| 1114 } // namespace cc | |
| OLD | NEW |