1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "cc/resources/managed_tile_state.h" 6 7 #include <limits> 8 9 #include "cc/base/math_util.h" 10 11 namespace cc { 12 13 scoped_ptr<base::Value> ManagedTileBinAsValue(ManagedTileBin bin) { 14 switch (bin) { 15 case NOW_AND_READY_TO_DRAW_BIN: 16 return scoped_ptr<base::Value>( 17 base::Value::CreateStringValue("NOW_AND_READY_TO_DRAW_BIN")); 18 case NOW_BIN: 19 return scoped_ptr<base::Value>( 20 base::Value::CreateStringValue("NOW_BIN")); 21 case SOON_BIN: 22 return scoped_ptr<base::Value>( 23 base::Value::CreateStringValue("SOON_BIN")); 24 case EVENTUALLY_AND_ACTIVE_BIN: 25 return scoped_ptr<base::Value>( 26 base::Value::CreateStringValue("EVENTUALLY_AND_ACTIVE_BIN")); 27 case EVENTUALLY_BIN: 28 return scoped_ptr<base::Value>( 29 base::Value::CreateStringValue("EVENTUALLY_BIN")); 30 case AT_LAST_AND_ACTIVE_BIN: 31 return scoped_ptr<base::Value>( 32 base::Value::CreateStringValue("AT_LAST_AND_ACTIVE_BIN")); 33 case AT_LAST_BIN: 34 return scoped_ptr<base::Value>( 35 base::Value::CreateStringValue("AT_LAST_BIN")); 36 case NEVER_BIN: 37 return scoped_ptr<base::Value>( 38 base::Value::CreateStringValue("NEVER_BIN")); 39 case NUM_BINS: 40 NOTREACHED(); 41 return scoped_ptr<base::Value>( 42 base::Value::CreateStringValue("Invalid Bin (NUM_BINS)")); 43 } 44 return scoped_ptr<base::Value>( 45 base::Value::CreateStringValue("Invalid Bin (UNKNOWN)")); 46 } 47 48 ManagedTileState::ManagedTileState() 49 : raster_mode(LOW_QUALITY_RASTER_MODE), 50 bin(NEVER_BIN), 51 resolution(NON_IDEAL_RESOLUTION), 52 required_for_activation(false), 53 time_to_needed_in_seconds(std::numeric_limits<float>::infinity()), 54 distance_to_visible_in_pixels(std::numeric_limits<float>::infinity()), 55 visible_and_ready_to_draw(false), 56 scheduled_priority(0) { 57 } 58 59 ManagedTileState::TileVersion::TileVersion() 60 : mode_(RESOURCE_MODE), 61 has_text_(false) { 62 } 63 64 ManagedTileState::TileVersion::~TileVersion() { 65 DCHECK(!resource_); 66 } 67 68 bool ManagedTileState::TileVersion::IsReadyToDraw() const { 69 switch (mode_) { 70 case RESOURCE_MODE: 71 return !!resource_; 72 case SOLID_COLOR_MODE: 73 case PICTURE_PILE_MODE: 74 return true; 75 } 76 NOTREACHED(); 77 return false; 78 } 79 80 size_t ManagedTileState::TileVersion::GPUMemoryUsageInBytes() const { 81 if (!resource_) 82 return 0; 83 return resource_->bytes(); 84 } 85 86 ManagedTileState::~ManagedTileState() { 87 } 88 89 scoped_ptr<base::Value> ManagedTileState::AsValue() const { 90 bool has_resource = false; 91 bool has_active_task = false; 92 for (int mode = 0; mode < NUM_RASTER_MODES; ++mode) { 93 has_resource |= (tile_versions[mode].resource_.get() != 0); 94 has_active_task |= !tile_versions[mode].raster_task_.is_null(); 95 } 96 97 bool is_using_gpu_memory = has_resource || has_active_task; 98 99 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue()); 100 state->SetBoolean("has_resource", has_resource); 101 state->SetBoolean("is_using_gpu_memory", is_using_gpu_memory); 102 state->Set("bin", ManagedTileBinAsValue(bin).release()); 103 state->Set("resolution", TileResolutionAsValue(resolution).release()); 104 state->Set("time_to_needed_in_seconds", 105 MathUtil::AsValueSafely(time_to_needed_in_seconds).release()); 106 state->Set("distance_to_visible_in_pixels", 107 MathUtil::AsValueSafely(distance_to_visible_in_pixels).release()); 108 state->SetBoolean("required_for_activation", required_for_activation); 109 state->SetBoolean( 110 "is_solid_color", 111 tile_versions[raster_mode].mode_ == TileVersion::SOLID_COLOR_MODE); 112 state->SetBoolean( 113 "is_transparent", 114 tile_versions[raster_mode].mode_ == TileVersion::SOLID_COLOR_MODE && 115 !SkColorGetA(tile_versions[raster_mode].solid_color_)); 116 state->SetInteger("scheduled_priority", scheduled_priority); 117 return state.PassAs<base::Value>(); 118 } 119 120 } // namespace cc 121