This source file includes following definitions.
- layer_needs_to_register_itself_
- LayerTypeAsString
- CreateLayerImpl
- PushPropertiesTo
- AppendQuads
- DidUnregisterLayer
- UpdateTilePriorities
- DidBecomeActive
- DidBeginTracing
- ReleaseResources
- CalculateContentsScale
- GetPicture
- SetHasGpuRasterizationHint
- ShouldUseGpuRasterization
- CreateTile
- UpdatePile
- GetInvalidation
- GetTwinTiling
- GetMaxTilesForInterestArea
- GetSkewportTargetTimeInSeconds
- GetSkewportExtrapolationLimitInContentPixels
- CalculateTileSize
- SyncFromActiveLayer
- SyncTiling
- SetIsMask
- ContentsResourceId
- MarkVisibleResourcesAsRequired
- MarkVisibleTilesAsRequired
- DoPostCommitInitialization
- AddTiling
- RemoveTiling
- RemoveAllTilings
- PositiveRatio
- ManageTilings
- ShouldAdjustRasterScale
- SnappedContentsScale
- RecalculateRasterScales
- CleanUpTilingsOnActiveLayer
- MinimumContentsScale
- UpdateLCDTextStatus
- ResetRasterScale
- CanHaveTilings
- CanHaveTilingWithScale
- SanityCheckTilingState
- GetDebugBorderProperties
- AsValueInto
- GPUMemoryUsageInBytes
- RunMicroBenchmark
- GetTree
- IsOnActiveOrPendingTree
- current_stage_
#include "cc/layers/picture_layer_impl.h"
#include <algorithm>
#include <limits>
#include "base/time/time.h"
#include "cc/base/math_util.h"
#include "cc/base/util.h"
#include "cc/debug/debug_colors.h"
#include "cc/debug/micro_benchmark_impl.h"
#include "cc/debug/traced_value.h"
#include "cc/layers/append_quads_data.h"
#include "cc/layers/quad_sink.h"
#include "cc/quads/checkerboard_draw_quad.h"
#include "cc/quads/debug_border_draw_quad.h"
#include "cc/quads/picture_draw_quad.h"
#include "cc/quads/solid_color_draw_quad.h"
#include "cc/quads/tile_draw_quad.h"
#include "cc/resources/tile_manager.h"
#include "cc/trees/layer_tree_impl.h"
#include "ui/gfx/quad_f.h"
#include "ui/gfx/rect_conversions.h"
#include "ui/gfx/size_conversions.h"
namespace {
const float kMaxScaleRatioDuringPinch = 2.0f;
const float kSnapToExistingTilingRatio = 0.2f;
}
namespace cc {
PictureLayerImpl::PictureLayerImpl(LayerTreeImpl* tree_impl, int id)
: LayerImpl(tree_impl, id),
twin_layer_(NULL),
pile_(PicturePileImpl::Create()),
is_mask_(false),
ideal_page_scale_(0.f),
ideal_device_scale_(0.f),
ideal_source_scale_(0.f),
ideal_contents_scale_(0.f),
raster_page_scale_(0.f),
raster_device_scale_(0.f),
raster_source_scale_(0.f),
raster_contents_scale_(0.f),
low_res_raster_contents_scale_(0.f),
raster_source_scale_was_animating_(false),
is_using_lcd_text_(tree_impl->settings().can_use_lcd_text),
needs_post_commit_initialization_(true),
should_update_tile_priorities_(false),
has_gpu_rasterization_hint_(false),
should_use_low_res_tiling_(tree_impl->settings().create_low_res_tiling),
layer_needs_to_register_itself_(true) {}
PictureLayerImpl::~PictureLayerImpl() {
if (!layer_needs_to_register_itself_)
layer_tree_impl()->tile_manager()->UnregisterPictureLayerImpl(this);
}
const char* PictureLayerImpl::LayerTypeAsString() const {
return "cc::PictureLayerImpl";
}
scoped_ptr<LayerImpl> PictureLayerImpl::CreateLayerImpl(
LayerTreeImpl* tree_impl) {
return PictureLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
}
void PictureLayerImpl::PushPropertiesTo(LayerImpl* base_layer) {
DoPostCommitInitializationIfNeeded();
PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
if (layer_impl->tilings_)
layer_impl->tilings_->DidBecomeRecycled();
LayerImpl::PushPropertiesTo(base_layer);
layer_impl->twin_layer_ = NULL;
twin_layer_ = NULL;
layer_impl->SetIsMask(is_mask_);
layer_impl->pile_ = pile_;
layer_impl->SetHasGpuRasterizationHint(has_gpu_rasterization_hint_);
layer_impl->tilings_.swap(tilings_);
layer_impl->tilings_->SetClient(layer_impl);
if (tilings_)
tilings_->SetClient(this);
layer_impl->raster_page_scale_ = raster_page_scale_;
layer_impl->raster_device_scale_ = raster_device_scale_;
layer_impl->raster_source_scale_ = raster_source_scale_;
layer_impl->raster_contents_scale_ = raster_contents_scale_;
layer_impl->low_res_raster_contents_scale_ = low_res_raster_contents_scale_;
layer_impl->UpdateLCDTextStatus(is_using_lcd_text_);
layer_impl->needs_post_commit_initialization_ = false;
layer_impl->invalidation_.Swap(&invalidation_);
invalidation_.Clear();
needs_post_commit_initialization_ = true;
needs_push_properties_ = true;
}
void PictureLayerImpl::AppendQuads(QuadSink* quad_sink,
AppendQuadsData* append_quads_data) {
DCHECK(!needs_post_commit_initialization_);
gfx::Rect rect(visible_content_rect());
gfx::Rect content_rect(content_bounds());
SharedQuadState* shared_quad_state =
quad_sink->UseSharedQuadState(CreateSharedQuadState());
if (current_draw_mode_ == DRAW_MODE_RESOURCELESS_SOFTWARE) {
AppendDebugBorderQuad(
quad_sink,
shared_quad_state,
append_quads_data,
DebugColors::DirectPictureBorderColor(),
DebugColors::DirectPictureBorderWidth(layer_tree_impl()));
gfx::Rect geometry_rect = rect;
gfx::Rect opaque_rect = contents_opaque() ? geometry_rect : gfx::Rect();
gfx::Rect visible_geometry_rect = geometry_rect;
gfx::Size texture_size = rect.size();
gfx::RectF texture_rect = gfx::RectF(texture_size);
gfx::Rect quad_content_rect = rect;
float contents_scale = contents_scale_x();
scoped_ptr<PictureDrawQuad> quad = PictureDrawQuad::Create();
quad->SetNew(shared_quad_state,
geometry_rect,
opaque_rect,
visible_geometry_rect,
texture_rect,
texture_size,
RGBA_8888,
quad_content_rect,
contents_scale,
pile_);
if (quad_sink->MaybeAppend(quad.PassAs<DrawQuad>()))
append_quads_data->num_missing_tiles++;
return;
}
AppendDebugBorderQuad(quad_sink, shared_quad_state, append_quads_data);
if (ShowDebugBorders()) {
for (PictureLayerTilingSet::CoverageIterator iter(
tilings_.get(), contents_scale_x(), rect, ideal_contents_scale_);
iter;
++iter) {
SkColor color;
float width;
if (*iter && iter->IsReadyToDraw()) {
ManagedTileState::TileVersion::Mode mode =
iter->GetTileVersionForDrawing().mode();
if (mode == ManagedTileState::TileVersion::SOLID_COLOR_MODE) {
color = DebugColors::SolidColorTileBorderColor();
width = DebugColors::SolidColorTileBorderWidth(layer_tree_impl());
} else if (mode == ManagedTileState::TileVersion::PICTURE_PILE_MODE) {
color = DebugColors::PictureTileBorderColor();
width = DebugColors::PictureTileBorderWidth(layer_tree_impl());
} else if (iter->priority(ACTIVE_TREE).resolution == HIGH_RESOLUTION) {
color = DebugColors::HighResTileBorderColor();
width = DebugColors::HighResTileBorderWidth(layer_tree_impl());
} else if (iter->priority(ACTIVE_TREE).resolution == LOW_RESOLUTION) {
color = DebugColors::LowResTileBorderColor();
width = DebugColors::LowResTileBorderWidth(layer_tree_impl());
} else if (iter->contents_scale() > contents_scale_x()) {
color = DebugColors::ExtraHighResTileBorderColor();
width = DebugColors::ExtraHighResTileBorderWidth(layer_tree_impl());
} else {
color = DebugColors::ExtraLowResTileBorderColor();
width = DebugColors::ExtraLowResTileBorderWidth(layer_tree_impl());
}
} else {
color = DebugColors::MissingTileBorderColor();
width = DebugColors::MissingTileBorderWidth(layer_tree_impl());
}
scoped_ptr<DebugBorderDrawQuad> debug_border_quad =
DebugBorderDrawQuad::Create();
gfx::Rect geometry_rect = iter.geometry_rect();
gfx::Rect visible_geometry_rect = geometry_rect;
debug_border_quad->SetNew(shared_quad_state,
geometry_rect,
visible_geometry_rect,
color,
width);
quad_sink->MaybeAppend(debug_border_quad.PassAs<DrawQuad>());
}
}
std::vector<PictureLayerTiling*> seen_tilings;
for (PictureLayerTilingSet::CoverageIterator iter(
tilings_.get(), contents_scale_x(), rect, ideal_contents_scale_);
iter;
++iter) {
gfx::Rect geometry_rect = iter.geometry_rect();
gfx::Rect visible_geometry_rect = geometry_rect;
if (!*iter || !iter->IsReadyToDraw()) {
if (draw_checkerboard_for_missing_tiles()) {
scoped_ptr<CheckerboardDrawQuad> quad = CheckerboardDrawQuad::Create();
SkColor color = DebugColors::DefaultCheckerboardColor();
quad->SetNew(
shared_quad_state, geometry_rect, visible_geometry_rect, color);
if (quad_sink->MaybeAppend(quad.PassAs<DrawQuad>()))
append_quads_data->num_missing_tiles++;
} else {
SkColor color = SafeOpaqueBackgroundColor();
scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
quad->SetNew(shared_quad_state,
geometry_rect,
visible_geometry_rect,
color,
false);
if (quad_sink->MaybeAppend(quad.PassAs<DrawQuad>()))
append_quads_data->num_missing_tiles++;
}
append_quads_data->had_incomplete_tile = true;
continue;
}
const ManagedTileState::TileVersion& tile_version =
iter->GetTileVersionForDrawing();
scoped_ptr<DrawQuad> draw_quad;
switch (tile_version.mode()) {
case ManagedTileState::TileVersion::RESOURCE_MODE: {
gfx::RectF texture_rect = iter.texture_rect();
gfx::Rect opaque_rect = iter->opaque_rect();
opaque_rect.Intersect(geometry_rect);
if (iter->contents_scale() != ideal_contents_scale_)
append_quads_data->had_incomplete_tile = true;
scoped_ptr<TileDrawQuad> quad = TileDrawQuad::Create();
quad->SetNew(shared_quad_state,
geometry_rect,
opaque_rect,
visible_geometry_rect,
tile_version.get_resource_id(),
texture_rect,
iter.texture_size(),
tile_version.contents_swizzled());
draw_quad = quad.PassAs<DrawQuad>();
break;
}
case ManagedTileState::TileVersion::PICTURE_PILE_MODE: {
gfx::RectF texture_rect = iter.texture_rect();
gfx::Rect opaque_rect = iter->opaque_rect();
opaque_rect.Intersect(geometry_rect);
ResourceProvider* resource_provider =
layer_tree_impl()->resource_provider();
ResourceFormat format =
resource_provider->memory_efficient_texture_format();
scoped_ptr<PictureDrawQuad> quad = PictureDrawQuad::Create();
quad->SetNew(shared_quad_state,
geometry_rect,
opaque_rect,
visible_geometry_rect,
texture_rect,
iter.texture_size(),
format,
iter->content_rect(),
iter->contents_scale(),
pile_);
draw_quad = quad.PassAs<DrawQuad>();
break;
}
case ManagedTileState::TileVersion::SOLID_COLOR_MODE: {
scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
quad->SetNew(shared_quad_state,
geometry_rect,
visible_geometry_rect,
tile_version.get_solid_color(),
false);
draw_quad = quad.PassAs<DrawQuad>();
break;
}
}
DCHECK(draw_quad);
quad_sink->MaybeAppend(draw_quad.Pass());
if (seen_tilings.empty() || seen_tilings.back() != iter.CurrentTiling())
seen_tilings.push_back(iter.CurrentTiling());
}
CleanUpTilingsOnActiveLayer(seen_tilings);
}
void PictureLayerImpl::DidUnregisterLayer() {
layer_needs_to_register_itself_ = true;
}
void PictureLayerImpl::UpdateTilePriorities() {
DCHECK(!needs_post_commit_initialization_);
CHECK(should_update_tile_priorities_);
if (layer_needs_to_register_itself_) {
layer_tree_impl()->tile_manager()->RegisterPictureLayerImpl(this);
layer_needs_to_register_itself_ = false;
}
if (!layer_tree_impl()->device_viewport_valid_for_tile_management()) {
for (size_t i = 0; i < tilings_->num_tilings(); ++i)
DCHECK(tilings_->tiling_at(i)->has_ever_been_updated());
return;
}
if (!tilings_->num_tilings())
return;
double current_frame_time_in_seconds =
(layer_tree_impl()->CurrentFrameTimeTicks() -
base::TimeTicks()).InSecondsF();
bool tiling_needs_update = false;
for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
if (tilings_->tiling_at(i)->NeedsUpdateForFrameAtTime(
current_frame_time_in_seconds)) {
tiling_needs_update = true;
break;
}
}
if (!tiling_needs_update)
return;
UpdateLCDTextStatus(can_use_lcd_text());
gfx::Rect visible_rect_in_content_space = visible_content_rect();
if (visible_rect_in_content_space.IsEmpty()) {
gfx::Transform screen_to_layer(gfx::Transform::kSkipInitialization);
if (screen_space_transform().GetInverse(&screen_to_layer)) {
gfx::Size viewport_size = layer_tree_impl()->DrawViewportSize();
visible_rect_in_content_space =
gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(
screen_to_layer, gfx::Rect(viewport_size)));
visible_rect_in_content_space.Intersect(gfx::Rect(content_bounds()));
}
}
WhichTree tree =
layer_tree_impl()->IsActiveTree() ? ACTIVE_TREE : PENDING_TREE;
tilings_->UpdateTilePriorities(tree,
visible_rect_in_content_space,
contents_scale_x(),
current_frame_time_in_seconds);
if (layer_tree_impl()->IsPendingTree())
MarkVisibleResourcesAsRequired();
layer_tree_impl()->DidModifyTilePriorities();
}
void PictureLayerImpl::DidBecomeActive() {
LayerImpl::DidBecomeActive();
tilings_->DidBecomeActive();
layer_tree_impl()->DidModifyTilePriorities();
}
void PictureLayerImpl::DidBeginTracing() {
pile_->DidBeginTracing();
}
void PictureLayerImpl::ReleaseResources() {
if (tilings_)
RemoveAllTilings();
ResetRasterScale();
}
void PictureLayerImpl::CalculateContentsScale(
float ideal_contents_scale,
float device_scale_factor,
float page_scale_factor,
bool animating_transform_to_screen,
float* contents_scale_x,
float* contents_scale_y,
gfx::Size* content_bounds) {
DoPostCommitInitializationIfNeeded();
should_update_tile_priorities_ = true;
if (!CanHaveTilings()) {
ideal_page_scale_ = page_scale_factor;
ideal_device_scale_ = device_scale_factor;
ideal_contents_scale_ = ideal_contents_scale;
ideal_source_scale_ =
ideal_contents_scale_ / ideal_page_scale_ / ideal_device_scale_;
*contents_scale_x = ideal_contents_scale_;
*contents_scale_y = ideal_contents_scale_;
*content_bounds = gfx::ToCeiledSize(gfx::ScaleSize(bounds(),
ideal_contents_scale_,
ideal_contents_scale_));
return;
}
float min_contents_scale = MinimumContentsScale();
DCHECK_GT(min_contents_scale, 0.f);
float min_page_scale = layer_tree_impl()->min_page_scale_factor();
DCHECK_GT(min_page_scale, 0.f);
float min_device_scale = 1.f;
float min_source_scale =
min_contents_scale / min_page_scale / min_device_scale;
float ideal_page_scale = page_scale_factor;
float ideal_device_scale = device_scale_factor;
float ideal_source_scale =
ideal_contents_scale / ideal_page_scale / ideal_device_scale;
ideal_contents_scale_ = std::max(ideal_contents_scale, min_contents_scale);
ideal_page_scale_ = ideal_page_scale;
ideal_device_scale_ = ideal_device_scale;
ideal_source_scale_ = std::max(ideal_source_scale, min_source_scale);
ManageTilings(animating_transform_to_screen);
float max_contents_scale = min_contents_scale;
for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
const PictureLayerTiling* tiling = tilings_->tiling_at(i);
max_contents_scale = std::max(max_contents_scale, tiling->contents_scale());
}
*contents_scale_x = max_contents_scale;
*contents_scale_y = max_contents_scale;
*content_bounds = gfx::ToCeiledSize(
gfx::ScaleSize(bounds(), max_contents_scale, max_contents_scale));
}
skia::RefPtr<SkPicture> PictureLayerImpl::GetPicture() {
return pile_->GetFlattenedPicture();
}
void PictureLayerImpl::SetHasGpuRasterizationHint(bool has_hint) {
bool old_should_use_gpu_rasterization = ShouldUseGpuRasterization();
has_gpu_rasterization_hint_ = has_hint;
if (ShouldUseGpuRasterization() != old_should_use_gpu_rasterization)
RemoveAllTilings();
}
bool PictureLayerImpl::ShouldUseGpuRasterization() const {
switch (layer_tree_impl()->settings().rasterization_site) {
case LayerTreeSettings::CpuRasterization:
return false;
case LayerTreeSettings::HybridRasterization:
return has_gpu_rasterization_hint_;
case LayerTreeSettings::GpuRasterization:
return true;
}
NOTREACHED();
return false;
}
scoped_refptr<Tile> PictureLayerImpl::CreateTile(PictureLayerTiling* tiling,
const gfx::Rect& content_rect) {
if (!pile_->CanRaster(tiling->contents_scale(), content_rect))
return scoped_refptr<Tile>();
int flags = 0;
if (is_using_lcd_text_)
flags |= Tile::USE_LCD_TEXT;
if (ShouldUseGpuRasterization())
flags |= Tile::USE_GPU_RASTERIZATION;
return layer_tree_impl()->tile_manager()->CreateTile(
pile_.get(),
content_rect.size(),
content_rect,
contents_opaque() ? content_rect : gfx::Rect(),
tiling->contents_scale(),
id(),
layer_tree_impl()->source_frame_number(),
flags);
}
void PictureLayerImpl::UpdatePile(Tile* tile) {
tile->set_picture_pile(pile_);
}
const Region* PictureLayerImpl::GetInvalidation() {
return &invalidation_;
}
const PictureLayerTiling* PictureLayerImpl::GetTwinTiling(
const PictureLayerTiling* tiling) const {
if (!twin_layer_ ||
twin_layer_->ShouldUseGpuRasterization() != ShouldUseGpuRasterization())
return NULL;
for (size_t i = 0; i < twin_layer_->tilings_->num_tilings(); ++i)
if (twin_layer_->tilings_->tiling_at(i)->contents_scale() ==
tiling->contents_scale())
return twin_layer_->tilings_->tiling_at(i);
return NULL;
}
size_t PictureLayerImpl::GetMaxTilesForInterestArea() const {
return layer_tree_impl()->settings().max_tiles_for_interest_area;
}
float PictureLayerImpl::GetSkewportTargetTimeInSeconds() const {
return layer_tree_impl()->settings().skewport_target_time_in_seconds;
}
int PictureLayerImpl::GetSkewportExtrapolationLimitInContentPixels() const {
return layer_tree_impl()
->settings()
.skewport_extrapolation_limit_in_content_pixels;
}
gfx::Size PictureLayerImpl::CalculateTileSize(
const gfx::Size& content_bounds) const {
if (is_mask_) {
int max_size = layer_tree_impl()->MaxTextureSize();
return gfx::Size(
std::min(max_size, content_bounds.width()),
std::min(max_size, content_bounds.height()));
}
int max_texture_size =
layer_tree_impl()->resource_provider()->max_texture_size();
gfx::Size default_tile_size = layer_tree_impl()->settings().default_tile_size;
default_tile_size.SetToMin(gfx::Size(max_texture_size, max_texture_size));
gfx::Size max_untiled_content_size =
layer_tree_impl()->settings().max_untiled_layer_size;
max_untiled_content_size.SetToMin(
gfx::Size(max_texture_size, max_texture_size));
bool any_dimension_too_large =
content_bounds.width() > max_untiled_content_size.width() ||
content_bounds.height() > max_untiled_content_size.height();
bool any_dimension_one_tile =
content_bounds.width() <= default_tile_size.width() ||
content_bounds.height() <= default_tile_size.height();
if (any_dimension_one_tile || !any_dimension_too_large) {
int width =
std::min(max_untiled_content_size.width(), content_bounds.width());
int height =
std::min(max_untiled_content_size.height(), content_bounds.height());
bool avoid_pow2 =
layer_tree_impl()->GetRendererCapabilities().avoid_pow2_textures;
int round_up_to = avoid_pow2 ? 56 : 64;
width = RoundUp(width, round_up_to);
height = RoundUp(height, round_up_to);
return gfx::Size(width, height);
}
return default_tile_size;
}
void PictureLayerImpl::SyncFromActiveLayer(const PictureLayerImpl* other) {
DCHECK(!other->needs_post_commit_initialization_);
DCHECK(other->tilings_);
UpdateLCDTextStatus(other->is_using_lcd_text_);
if (!DrawsContent()) {
RemoveAllTilings();
return;
}
raster_page_scale_ = other->raster_page_scale_;
raster_device_scale_ = other->raster_device_scale_;
raster_source_scale_ = other->raster_source_scale_;
raster_contents_scale_ = other->raster_contents_scale_;
low_res_raster_contents_scale_ = other->low_res_raster_contents_scale_;
for (int x = 0; x < pile_->num_tiles_x(); ++x) {
for (int y = 0; y < pile_->num_tiles_y(); ++y) {
bool previously_had = other->pile_->HasRecordingAt(x, y);
bool now_has = pile_->HasRecordingAt(x, y);
if (now_has || !previously_had)
continue;
gfx::Rect layer_rect = pile_->tile_bounds(x, y);
invalidation_.Union(layer_rect);
}
}
Region difference_region = Region(gfx::Rect(bounds()));
difference_region.Subtract(gfx::Rect(other->bounds()));
invalidation_.Union(difference_region);
if (CanHaveTilings()) {
Region tiling_invalidation = other->invalidation_;
tiling_invalidation.Union(invalidation_);
tilings_->SyncTilings(*other->tilings_,
bounds(),
tiling_invalidation,
MinimumContentsScale());
} else {
RemoveAllTilings();
}
SanityCheckTilingState();
}
void PictureLayerImpl::SyncTiling(
const PictureLayerTiling* tiling) {
if (!CanHaveTilingWithScale(tiling->contents_scale()))
return;
tilings_->AddTiling(tiling->contents_scale());
if (!layer_tree_impl()->needs_update_draw_properties() &&
should_update_tile_priorities_)
UpdateTilePriorities();
}
void PictureLayerImpl::SetIsMask(bool is_mask) {
if (is_mask_ == is_mask)
return;
is_mask_ = is_mask;
if (tilings_)
tilings_->RemoveAllTiles();
}
ResourceProvider::ResourceId PictureLayerImpl::ContentsResourceId() const {
gfx::Rect content_rect(content_bounds());
float scale = contents_scale_x();
PictureLayerTilingSet::CoverageIterator iter(
tilings_.get(), scale, content_rect, ideal_contents_scale_);
if (!iter || !*iter)
return 0;
if (iter.geometry_rect() != content_rect)
return 0;
const ManagedTileState::TileVersion& tile_version =
iter->GetTileVersionForDrawing();
if (!tile_version.IsReadyToDraw() ||
tile_version.mode() != ManagedTileState::TileVersion::RESOURCE_MODE)
return 0;
return tile_version.get_resource_id();
}
void PictureLayerImpl::MarkVisibleResourcesAsRequired() const {
DCHECK(layer_tree_impl()->IsPendingTree());
DCHECK(!layer_tree_impl()->needs_update_draw_properties());
DCHECK(ideal_contents_scale_);
DCHECK_GT(tilings_->num_tilings(), 0u);
gfx::Rect rect(visible_content_rect());
float min_acceptable_scale =
std::min(raster_contents_scale_, ideal_contents_scale_);
if (PictureLayerImpl* twin = twin_layer_) {
float twin_min_acceptable_scale =
std::min(twin->ideal_contents_scale_, twin->raster_contents_scale_);
if (twin_min_acceptable_scale != 0.0f) {
min_acceptable_scale =
std::min(min_acceptable_scale, twin_min_acceptable_scale);
}
}
PictureLayerTiling* high_res = NULL;
PictureLayerTiling* low_res = NULL;
Region missing_region = rect;
for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
PictureLayerTiling* tiling = tilings_->tiling_at(i);
DCHECK(tiling->has_ever_been_updated());
if (tiling->resolution() == LOW_RESOLUTION) {
DCHECK(!low_res) << "There can only be one low res tiling";
low_res = tiling;
}
if (tiling->contents_scale() < min_acceptable_scale)
continue;
if (tiling->resolution() == HIGH_RESOLUTION) {
DCHECK(!high_res) << "There can only be one high res tiling";
high_res = tiling;
continue;
}
for (PictureLayerTiling::CoverageIterator iter(tiling,
contents_scale_x(),
rect);
iter;
++iter) {
if (!*iter || !iter->IsReadyToDraw())
continue;
missing_region.Subtract(iter.geometry_rect());
iter->MarkRequiredForActivation();
}
}
DCHECK(high_res) << "There must be one high res tiling";
const PictureLayerTiling* twin_high_res = NULL;
const PictureLayerTiling* twin_low_res = NULL;
if (high_res && low_res && tilings_->num_tilings() == 2 &&
twin_layer_ && twin_layer_->tilings_->num_tilings() == 2) {
twin_low_res = GetTwinTiling(low_res);
if (twin_low_res)
twin_high_res = GetTwinTiling(high_res);
}
if (!twin_high_res || !twin_low_res ||
twin_layer_->layer_tree_impl()->RequiresHighResToDraw() ||
draw_properties().screen_space_transform !=
twin_layer_->draw_properties().screen_space_transform) {
twin_high_res = NULL;
twin_low_res = NULL;
}
if (MarkVisibleTilesAsRequired(
high_res, twin_high_res, contents_scale_x(), rect, missing_region)) {
MarkVisibleTilesAsRequired(
low_res, twin_low_res, contents_scale_x(), rect, missing_region);
}
}
bool PictureLayerImpl::MarkVisibleTilesAsRequired(
PictureLayerTiling* tiling,
const PictureLayerTiling* optional_twin_tiling,
float contents_scale,
const gfx::Rect& rect,
const Region& missing_region) const {
bool twin_had_missing_tile = false;
for (PictureLayerTiling::CoverageIterator iter(tiling,
contents_scale,
rect);
iter;
++iter) {
Tile* tile = *iter;
if (!tile)
continue;
if (!missing_region.Intersects(iter.geometry_rect()))
continue;
if (optional_twin_tiling) {
Tile* twin_tile = optional_twin_tiling->TileAt(iter.i(), iter.j());
if (!twin_tile || twin_tile == tile) {
twin_had_missing_tile = true;
continue;
}
}
tile->MarkRequiredForActivation();
}
return twin_had_missing_tile;
}
void PictureLayerImpl::DoPostCommitInitialization() {
DCHECK(needs_post_commit_initialization_);
DCHECK(layer_tree_impl()->IsPendingTree());
if (!tilings_)
tilings_.reset(new PictureLayerTilingSet(this, bounds()));
DCHECK(!twin_layer_);
twin_layer_ = static_cast<PictureLayerImpl*>(
layer_tree_impl()->FindActiveTreeLayerById(id()));
if (twin_layer_) {
DCHECK(!twin_layer_->twin_layer_);
twin_layer_->twin_layer_ = this;
if (!twin_layer_->needs_post_commit_initialization_)
SyncFromActiveLayer(twin_layer_);
}
needs_post_commit_initialization_ = false;
}
PictureLayerTiling* PictureLayerImpl::AddTiling(float contents_scale) {
DCHECK(CanHaveTilingWithScale(contents_scale)) <<
"contents_scale: " << contents_scale;
PictureLayerTiling* tiling = tilings_->AddTiling(contents_scale);
DCHECK(pile_->HasRecordings());
if (twin_layer_ &&
twin_layer_->ShouldUseGpuRasterization() == ShouldUseGpuRasterization())
twin_layer_->SyncTiling(tiling);
return tiling;
}
void PictureLayerImpl::RemoveTiling(float contents_scale) {
for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
PictureLayerTiling* tiling = tilings_->tiling_at(i);
if (tiling->contents_scale() == contents_scale) {
tilings_->Remove(tiling);
break;
}
}
if (tilings_->num_tilings() == 0)
ResetRasterScale();
SanityCheckTilingState();
}
void PictureLayerImpl::RemoveAllTilings() {
if (tilings_)
tilings_->RemoveAllTilings();
ResetRasterScale();
}
namespace {
inline float PositiveRatio(float float1, float float2) {
DCHECK_GT(float1, 0);
DCHECK_GT(float2, 0);
return float1 > float2 ? float1 / float2 : float2 / float1;
}
}
void PictureLayerImpl::ManageTilings(bool animating_transform_to_screen) {
DCHECK(ideal_contents_scale_);
DCHECK(ideal_page_scale_);
DCHECK(ideal_device_scale_);
DCHECK(ideal_source_scale_);
DCHECK(CanHaveTilings());
DCHECK(!needs_post_commit_initialization_);
bool change_target_tiling =
raster_page_scale_ == 0.f ||
raster_device_scale_ == 0.f ||
raster_source_scale_ == 0.f ||
raster_contents_scale_ == 0.f ||
low_res_raster_contents_scale_ == 0.f ||
ShouldAdjustRasterScale(animating_transform_to_screen);
if (tilings_->num_tilings() == 0) {
DCHECK(change_target_tiling)
<< "A layer with no tilings shouldn't have valid raster scales";
}
raster_source_scale_was_animating_ = animating_transform_to_screen;
if (!change_target_tiling)
return;
if (!layer_tree_impl()->device_viewport_valid_for_tile_management())
return;
RecalculateRasterScales(animating_transform_to_screen);
PictureLayerTiling* high_res = NULL;
PictureLayerTiling* low_res = NULL;
PictureLayerTiling* previous_low_res = NULL;
for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
PictureLayerTiling* tiling = tilings_->tiling_at(i);
if (tiling->contents_scale() == raster_contents_scale_)
high_res = tiling;
if (tiling->contents_scale() == low_res_raster_contents_scale_)
low_res = tiling;
if (tiling->resolution() == LOW_RESOLUTION)
previous_low_res = tiling;
tiling->set_resolution(NON_IDEAL_RESOLUTION);
}
if (!high_res) {
high_res = AddTiling(raster_contents_scale_);
if (raster_contents_scale_ == low_res_raster_contents_scale_)
low_res = high_res;
}
bool is_pinching = layer_tree_impl()->PinchGestureActive();
if (ShouldHaveLowResTiling() && !is_pinching &&
!animating_transform_to_screen &&
!low_res && low_res != high_res)
low_res = AddTiling(low_res_raster_contents_scale_);
if (!low_res)
low_res = previous_low_res;
if (low_res && low_res != high_res)
low_res->set_resolution(LOW_RESOLUTION);
high_res->set_resolution(HIGH_RESOLUTION);
SanityCheckTilingState();
}
bool PictureLayerImpl::ShouldAdjustRasterScale(
bool animating_transform_to_screen) const {
if (raster_source_scale_was_animating_ && !animating_transform_to_screen)
return true;
if (animating_transform_to_screen &&
raster_contents_scale_ != ideal_contents_scale_ &&
ShouldUseGpuRasterization())
return true;
bool is_pinching = layer_tree_impl()->PinchGestureActive();
if (is_pinching && raster_page_scale_) {
float ratio = ideal_page_scale_ / raster_page_scale_;
if (raster_page_scale_ > ideal_page_scale_ ||
ratio > kMaxScaleRatioDuringPinch)
return true;
}
if (!is_pinching) {
if (raster_page_scale_ != ideal_page_scale_)
return true;
}
if (raster_device_scale_ != ideal_device_scale_)
return true;
return false;
}
float PictureLayerImpl::SnappedContentsScale(float scale) {
float snapped_contents_scale = scale;
float snapped_ratio = kSnapToExistingTilingRatio;
for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
float tiling_contents_scale = tilings_->tiling_at(i)->contents_scale();
float ratio = PositiveRatio(tiling_contents_scale, scale);
if (ratio < snapped_ratio) {
snapped_contents_scale = tiling_contents_scale;
snapped_ratio = ratio;
}
}
return snapped_contents_scale;
}
void PictureLayerImpl::RecalculateRasterScales(
bool animating_transform_to_screen) {
raster_device_scale_ = ideal_device_scale_;
raster_source_scale_ = ideal_source_scale_;
bool is_pinching = layer_tree_impl()->PinchGestureActive();
if (!is_pinching || raster_contents_scale_ == 0.f) {
raster_page_scale_ = ideal_page_scale_;
raster_contents_scale_ = ideal_contents_scale_;
} else {
bool zooming_out = raster_page_scale_ > ideal_page_scale_;
float desired_contents_scale =
zooming_out ? raster_contents_scale_ / kMaxScaleRatioDuringPinch
: raster_contents_scale_ * kMaxScaleRatioDuringPinch;
raster_contents_scale_ = SnappedContentsScale(desired_contents_scale);
raster_page_scale_ = raster_contents_scale_ / raster_device_scale_;
}
raster_contents_scale_ =
std::max(raster_contents_scale_, MinimumContentsScale());
if (animating_transform_to_screen && !ShouldUseGpuRasterization()) {
raster_contents_scale_ = std::max(
raster_contents_scale_, 1.f * ideal_page_scale_ * ideal_device_scale_);
}
gfx::Size content_bounds =
gfx::ToCeiledSize(gfx::ScaleSize(bounds(), raster_contents_scale_));
gfx::Size tile_size = CalculateTileSize(content_bounds);
if (tile_size.width() >= content_bounds.width() &&
tile_size.height() >= content_bounds.height()) {
low_res_raster_contents_scale_ = raster_contents_scale_;
return;
}
float low_res_factor =
layer_tree_impl()->settings().low_res_contents_scale_factor;
low_res_raster_contents_scale_ = std::max(
raster_contents_scale_ * low_res_factor,
MinimumContentsScale());
}
void PictureLayerImpl::CleanUpTilingsOnActiveLayer(
std::vector<PictureLayerTiling*> used_tilings) {
DCHECK(layer_tree_impl()->IsActiveTree());
if (tilings_->num_tilings() == 0)
return;
float min_acceptable_high_res_scale = std::min(
raster_contents_scale_, ideal_contents_scale_);
float max_acceptable_high_res_scale = std::max(
raster_contents_scale_, ideal_contents_scale_);
PictureLayerImpl* twin = twin_layer_;
if (twin) {
min_acceptable_high_res_scale = std::min(
min_acceptable_high_res_scale,
std::min(twin->raster_contents_scale_, twin->ideal_contents_scale_));
max_acceptable_high_res_scale = std::max(
max_acceptable_high_res_scale,
std::max(twin->raster_contents_scale_, twin->ideal_contents_scale_));
}
std::vector<PictureLayerTiling*> to_remove;
for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
PictureLayerTiling* tiling = tilings_->tiling_at(i);
if (tiling->contents_scale() >= min_acceptable_high_res_scale &&
tiling->contents_scale() <= max_acceptable_high_res_scale)
continue;
if (tiling->resolution() == LOW_RESOLUTION && ShouldHaveLowResTiling())
continue;
if (std::find(used_tilings.begin(), used_tilings.end(), tiling) !=
used_tilings.end())
continue;
to_remove.push_back(tiling);
}
for (size_t i = 0; i < to_remove.size(); ++i) {
const PictureLayerTiling* twin_tiling = GetTwinTiling(to_remove[i]);
if (twin_tiling && twin_tiling->resolution() == NON_IDEAL_RESOLUTION)
twin->RemoveTiling(to_remove[i]->contents_scale());
tilings_->Remove(to_remove[i]);
}
DCHECK_GT(tilings_->num_tilings(), 0u);
SanityCheckTilingState();
}
float PictureLayerImpl::MinimumContentsScale() const {
float setting_min = layer_tree_impl()->settings().minimum_contents_scale;
int min_dimension = std::min(bounds().width(), bounds().height());
if (!min_dimension)
return setting_min;
return std::max(1.f / min_dimension, setting_min);
}
void PictureLayerImpl::UpdateLCDTextStatus(bool new_status) {
if (!is_using_lcd_text_)
return;
if (is_using_lcd_text_ == new_status)
return;
is_using_lcd_text_ = new_status;
tilings_->SetCanUseLCDText(is_using_lcd_text_);
}
void PictureLayerImpl::ResetRasterScale() {
raster_page_scale_ = 0.f;
raster_device_scale_ = 0.f;
raster_source_scale_ = 0.f;
raster_contents_scale_ = 0.f;
low_res_raster_contents_scale_ = 0.f;
should_update_tile_priorities_ = false;
}
bool PictureLayerImpl::CanHaveTilings() const {
if (!DrawsContent())
return false;
if (!pile_->HasRecordings())
return false;
return true;
}
bool PictureLayerImpl::CanHaveTilingWithScale(float contents_scale) const {
if (!CanHaveTilings())
return false;
if (contents_scale < MinimumContentsScale())
return false;
return true;
}
void PictureLayerImpl::SanityCheckTilingState() const {
#if DCHECK_IS_ON
if (!CanHaveTilings()) {
DCHECK_EQ(0u, tilings_->num_tilings());
return;
}
if (tilings_->num_tilings() == 0)
return;
DCHECK_EQ(1, tilings_->NumHighResTilings());
#endif
}
void PictureLayerImpl::GetDebugBorderProperties(
SkColor* color,
float* width) const {
*color = DebugColors::TiledContentLayerBorderColor();
*width = DebugColors::TiledContentLayerBorderWidth(layer_tree_impl());
}
void PictureLayerImpl::AsValueInto(base::DictionaryValue* state) const {
const_cast<PictureLayerImpl*>(this)->DoPostCommitInitializationIfNeeded();
LayerImpl::AsValueInto(state);
state->SetDouble("ideal_contents_scale", ideal_contents_scale_);
state->SetDouble("geometry_contents_scale", contents_scale_x());
state->Set("tilings", tilings_->AsValue().release());
state->Set("pictures", pile_->AsValue().release());
state->Set("invalidation", invalidation_.AsValue().release());
scoped_ptr<base::ListValue> coverage_tiles(new base::ListValue);
for (PictureLayerTilingSet::CoverageIterator iter(tilings_.get(),
contents_scale_x(),
gfx::Rect(content_bounds()),
ideal_contents_scale_);
iter;
++iter) {
scoped_ptr<base::DictionaryValue> tile_data(new base::DictionaryValue);
tile_data->Set("geometry_rect",
MathUtil::AsValue(iter.geometry_rect()).release());
if (*iter)
tile_data->Set("tile", TracedValue::CreateIDRef(*iter).release());
coverage_tiles->Append(tile_data.release());
}
state->Set("coverage_tiles", coverage_tiles.release());
state->SetBoolean("is_using_lcd_text", is_using_lcd_text_);
state->SetBoolean("using_gpu_rasterization", ShouldUseGpuRasterization());
}
size_t PictureLayerImpl::GPUMemoryUsageInBytes() const {
const_cast<PictureLayerImpl*>(this)->DoPostCommitInitializationIfNeeded();
return tilings_->GPUMemoryUsageInBytes();
}
void PictureLayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) {
benchmark->RunOnLayer(this);
}
WhichTree PictureLayerImpl::GetTree() const {
return layer_tree_impl()->IsActiveTree() ? ACTIVE_TREE : PENDING_TREE;
}
bool PictureLayerImpl::IsOnActiveOrPendingTree() const {
return !layer_tree_impl()->IsRecycleTree();
}
PictureLayerImpl::LayerRasterTileIterator::LayerRasterTileIterator()
: layer_(NULL) {}
PictureLayerImpl::LayerRasterTileIterator::LayerRasterTileIterator(
PictureLayerImpl* layer,
bool prioritize_low_res)
: layer_(layer), current_stage_(0) {
DCHECK(layer_);
if (!layer_->tilings_ || !layer_->tilings_->num_tilings())
return;
WhichTree tree =
layer_->layer_tree_impl()->IsActiveTree() ? ACTIVE_TREE : PENDING_TREE;
for (size_t i = 0; i < layer_->tilings_->num_tilings(); ++i) {
PictureLayerTiling* tiling = layer_->tilings_->tiling_at(i);
if (tiling->resolution() == HIGH_RESOLUTION) {
iterators_[HIGH_RES] =
PictureLayerTiling::TilingRasterTileIterator(tiling, tree);
}
if (tiling->resolution() == LOW_RESOLUTION) {
iterators_[LOW_RES] =
PictureLayerTiling::TilingRasterTileIterator(tiling, tree);
}
}
if (prioritize_low_res) {
stages_[0].iterator_type = LOW_RES;
stages_[0].tile_type = TilePriority::NOW;
stages_[1].iterator_type = HIGH_RES;
stages_[1].tile_type = TilePriority::NOW;
} else {
stages_[0].iterator_type = HIGH_RES;
stages_[0].tile_type = TilePriority::NOW;
stages_[1].iterator_type = LOW_RES;
stages_[1].tile_type = TilePriority::NOW;
}
stages_[2].iterator_type = HIGH_RES;
stages_[2].tile_type = TilePriority::SOON;
stages_[3].iterator_type = HIGH_RES;
stages_[3].tile_type = TilePriority::EVENTUALLY;
IteratorType index = stages_[current_stage_].iterator_type;
TilePriority::PriorityBin tile_type = stages_[current_stage_].tile_type;
if (!iterators_[index] || iterators_[index].get_type() != tile_type)
++(*this);
}
PictureLayerImpl::LayerRasterTileIterator::~LayerRasterTileIterator() {}
PictureLayerImpl::LayerRasterTileIterator::operator bool() const {
return layer_ && static_cast<size_t>(current_stage_) < arraysize(stages_);
}
PictureLayerImpl::LayerRasterTileIterator&
PictureLayerImpl::LayerRasterTileIterator::
operator++() {
IteratorType index = stages_[current_stage_].iterator_type;
TilePriority::PriorityBin tile_type = stages_[current_stage_].tile_type;
if (iterators_[index])
++iterators_[index];
if (iterators_[index] && iterators_[index].get_type() == tile_type)
return *this;
int stage_count = arraysize(stages_);
++current_stage_;
while (current_stage_ < stage_count) {
index = stages_[current_stage_].iterator_type;
tile_type = stages_[current_stage_].tile_type;
if (iterators_[index] && iterators_[index].get_type() == tile_type)
break;
++current_stage_;
}
return *this;
}
Tile* PictureLayerImpl::LayerRasterTileIterator::operator*() {
DCHECK(*this);
IteratorType index = stages_[current_stage_].iterator_type;
DCHECK(iterators_[index]);
DCHECK(iterators_[index].get_type() == stages_[current_stage_].tile_type);
return *iterators_[index];
}
}