Home | History | Annotate | Download | only in resources
      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           new base::StringValue("NOW_AND_READY_TO_DRAW_BIN"));
     18     case NOW_BIN:
     19       return scoped_ptr<base::Value>(new base::StringValue("NOW_BIN"));
     20     case SOON_BIN:
     21       return scoped_ptr<base::Value>(
     22           new base::StringValue("SOON_BIN"));
     23     case EVENTUALLY_AND_ACTIVE_BIN:
     24       return scoped_ptr<base::Value>(
     25           new base::StringValue("EVENTUALLY_AND_ACTIVE_BIN"));
     26     case EVENTUALLY_BIN:
     27       return scoped_ptr<base::Value>(
     28           new base::StringValue("EVENTUALLY_BIN"));
     29     case AT_LAST_AND_ACTIVE_BIN:
     30       return scoped_ptr<base::Value>(
     31           new base::StringValue("AT_LAST_AND_ACTIVE_BIN"));
     32     case AT_LAST_BIN:
     33       return scoped_ptr<base::Value>(
     34           new base::StringValue("AT_LAST_BIN"));
     35     case NEVER_BIN:
     36       return scoped_ptr<base::Value>(
     37           new base::StringValue("NEVER_BIN"));
     38     case NUM_BINS:
     39       NOTREACHED();
     40       return scoped_ptr<base::Value>(
     41           new base::StringValue("Invalid Bin (NUM_BINS)"));
     42   }
     43   return scoped_ptr<base::Value>(
     44       new base::StringValue("Invalid Bin (UNKNOWN)"));
     45 }
     46 
     47 ManagedTileState::ManagedTileState()
     48     : raster_mode(LOW_QUALITY_RASTER_MODE),
     49       bin(NEVER_BIN),
     50       resolution(NON_IDEAL_RESOLUTION),
     51       required_for_activation(false),
     52       priority_bin(TilePriority::EVENTUALLY),
     53       distance_to_visible(std::numeric_limits<float>::infinity()),
     54       visible_and_ready_to_draw(false),
     55       scheduled_priority(0) {}
     56 
     57 ManagedTileState::TileVersion::TileVersion() : mode_(RESOURCE_MODE) {
     58 }
     59 
     60 ManagedTileState::TileVersion::~TileVersion() { DCHECK(!resource_); }
     61 
     62 bool ManagedTileState::TileVersion::IsReadyToDraw() const {
     63   switch (mode_) {
     64     case RESOURCE_MODE:
     65       return !!resource_;
     66     case SOLID_COLOR_MODE:
     67     case PICTURE_PILE_MODE:
     68       return true;
     69   }
     70   NOTREACHED();
     71   return false;
     72 }
     73 
     74 size_t ManagedTileState::TileVersion::GPUMemoryUsageInBytes() const {
     75   if (!resource_)
     76     return 0;
     77   return resource_->bytes();
     78 }
     79 
     80 ManagedTileState::~ManagedTileState() {}
     81 
     82 scoped_ptr<base::Value> ManagedTileState::AsValue() const {
     83   bool has_resource = false;
     84   bool has_active_task = false;
     85   for (int mode = 0; mode < NUM_RASTER_MODES; ++mode) {
     86     has_resource |= (tile_versions[mode].resource_.get() != 0);
     87     has_active_task |= (tile_versions[mode].raster_task_.get() != 0);
     88   }
     89 
     90   bool is_using_gpu_memory = has_resource || has_active_task;
     91 
     92   scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
     93   state->SetBoolean("has_resource", has_resource);
     94   state->SetBoolean("is_using_gpu_memory", is_using_gpu_memory);
     95   state->Set("bin", ManagedTileBinAsValue(bin).release());
     96   state->Set("resolution", TileResolutionAsValue(resolution).release());
     97   state->Set("priority_bin", TilePriorityBinAsValue(priority_bin).release());
     98   state->Set("distance_to_visible",
     99              MathUtil::AsValueSafely(distance_to_visible).release());
    100   state->SetBoolean("required_for_activation", required_for_activation);
    101   state->SetBoolean(
    102       "is_solid_color",
    103       tile_versions[raster_mode].mode_ == TileVersion::SOLID_COLOR_MODE);
    104   state->SetBoolean(
    105       "is_transparent",
    106       tile_versions[raster_mode].mode_ == TileVersion::SOLID_COLOR_MODE &&
    107           !SkColorGetA(tile_versions[raster_mode].solid_color_));
    108   state->SetInteger("scheduled_priority", scheduled_priority);
    109   return state.PassAs<base::Value>();
    110 }
    111 
    112 }  // namespace cc
    113