This source file includes following definitions.
- rect_sort_y
- rect_sort_x
- do_clustering
- ClusterTiles
- Update
#include "cc/resources/picture_pile.h"
#include <algorithm>
#include <limits>
#include <vector>
#include "cc/base/region.h"
#include "cc/debug/rendering_stats_instrumentation.h"
#include "cc/resources/picture_pile_impl.h"
#include "cc/resources/raster_worker_pool.h"
#include "cc/resources/tile_priority.h"
namespace {
const int kPixelDistanceToRecord = 8000;
const float kDensityThreshold = 0.5f;
bool rect_sort_y(const gfx::Rect &r1, const gfx::Rect &r2) {
return r1.y() < r2.y() || (r1.y() == r2.y() && r1.x() < r2.x());
}
bool rect_sort_x(const gfx::Rect &r1, const gfx::Rect &r2) {
return r1.x() < r2.x() || (r1.x() == r2.x() && r1.y() < r2.y());
}
float do_clustering(const std::vector<gfx::Rect>& tiles,
std::vector<gfx::Rect>* clustered_rects) {
int total_record_area = 0;
int total_invalid_area = 0;
gfx::Rect cur_record_rect;
int cluster_record_area = 0, cluster_invalid_area = 0;
for (std::vector<gfx::Rect>::const_iterator it = tiles.begin();
it != tiles.end();
it++) {
gfx::Rect invalid_tile = *it;
int tile_area = invalid_tile.width() * invalid_tile.height();
gfx::Rect proposed_union = cur_record_rect;
proposed_union.Union(invalid_tile);
int proposed_area = proposed_union.width() * proposed_union.height();
float proposed_density =
static_cast<float>(cluster_invalid_area + tile_area) /
static_cast<float>(proposed_area);
if (proposed_density >= kDensityThreshold) {
cur_record_rect = proposed_union;
cluster_record_area = proposed_area;
cluster_invalid_area += tile_area;
total_invalid_area += tile_area;
} else {
clustered_rects->push_back(cur_record_rect);
total_record_area += cluster_record_area;
cur_record_rect = invalid_tile;
cluster_invalid_area = tile_area;
cluster_record_area = tile_area;
}
}
DCHECK(!cur_record_rect.IsEmpty());
clustered_rects->push_back(cur_record_rect);
total_record_area += cluster_record_area;;
DCHECK_NE(total_record_area, 0);
return static_cast<float>(total_invalid_area) /
static_cast<float>(total_record_area);
}
float ClusterTiles(const std::vector<gfx::Rect>& invalid_tiles,
std::vector<gfx::Rect>* record_rects) {
TRACE_EVENT1("cc", "ClusterTiles",
"count",
invalid_tiles.size());
if (invalid_tiles.size() <= 1) {
*record_rects = invalid_tiles;
return 1;
}
std::vector<gfx::Rect> invalid_tiles_vertical = invalid_tiles;
std::sort(invalid_tiles_vertical.begin(),
invalid_tiles_vertical.end(),
rect_sort_y);
float vertical_density;
std::vector<gfx::Rect> vertical_clustering;
vertical_density = do_clustering(invalid_tiles_vertical,
&vertical_clustering);
std::vector<gfx::Rect> invalid_tiles_horizontal = invalid_tiles;
std::sort(invalid_tiles_vertical.begin(),
invalid_tiles_vertical.end(),
rect_sort_x);
float horizontal_density;
std::vector<gfx::Rect> horizontal_clustering;
horizontal_density = do_clustering(invalid_tiles_vertical,
&horizontal_clustering);
if (vertical_density < horizontal_density) {
*record_rects = horizontal_clustering;
return horizontal_density;
}
*record_rects = vertical_clustering;
return vertical_density;
}
}
namespace cc {
PicturePile::PicturePile() {
}
PicturePile::~PicturePile() {
}
bool PicturePile::Update(ContentLayerClient* painter,
SkColor background_color,
bool contents_opaque,
bool contents_fill_bounds_completely,
const Region& invalidation,
const gfx::Rect& visible_layer_rect,
int frame_number,
RenderingStatsInstrumentation* stats_instrumentation) {
background_color_ = background_color;
contents_opaque_ = contents_opaque;
contents_fill_bounds_completely_ = contents_fill_bounds_completely;
gfx::Rect interest_rect = visible_layer_rect;
interest_rect.Inset(
-kPixelDistanceToRecord,
-kPixelDistanceToRecord,
-kPixelDistanceToRecord,
-kPixelDistanceToRecord);
recorded_viewport_ = interest_rect;
recorded_viewport_.Intersect(gfx::Rect(size()));
bool invalidated = false;
for (Region::Iterator i(invalidation); i.has_rect(); i.next()) {
gfx::Rect invalidation = i.rect();
bool include_borders = true;
for (TilingData::Iterator iter(&tiling_, invalidation, include_borders);
iter;
++iter) {
const PictureMapKey& key = iter.index();
PictureMap::iterator picture_it = picture_map_.find(key);
if (picture_it == picture_map_.end())
continue;
invalidated = picture_it->second.Invalidate(frame_number) || invalidated;
}
}
std::vector<gfx::Rect> invalid_tiles;
bool include_borders = true;
for (TilingData::Iterator it(&tiling_, interest_rect, include_borders); it;
++it) {
const PictureMapKey& key = it.index();
PictureInfo& info = picture_map_[key];
gfx::Rect rect = PaddedRect(key);
int distance_to_visible =
rect.ManhattanInternalDistance(visible_layer_rect);
if (info.NeedsRecording(frame_number, distance_to_visible)) {
gfx::Rect tile = tiling_.TileBounds(key.first, key.second);
invalid_tiles.push_back(tile);
} else if (!info.GetPicture() && recorded_viewport_.Intersects(rect)) {
recorded_viewport_ = gfx::Rect();
}
}
std::vector<gfx::Rect> record_rects;
ClusterTiles(invalid_tiles, &record_rects);
if (record_rects.empty())
return invalidated;
for (std::vector<gfx::Rect>::iterator it = record_rects.begin();
it != record_rects.end();
it++) {
gfx::Rect record_rect = *it;
record_rect = PadRect(record_rect);
int repeat_count = std::max(1, slow_down_raster_scale_factor_for_debug_);
scoped_refptr<Picture> picture;
int num_raster_threads = RasterWorkerPool::GetNumRasterThreads();
bool gather_pixel_refs = num_raster_threads > 1;
{
base::TimeDelta best_duration = base::TimeDelta::Max();
for (int i = 0; i < repeat_count; i++) {
base::TimeTicks start_time = stats_instrumentation->StartRecording();
picture = Picture::Create(record_rect,
painter,
tile_grid_info_,
gather_pixel_refs,
num_raster_threads,
Picture::RECORD_NORMALLY);
base::TimeDelta duration =
stats_instrumentation->EndRecording(start_time);
best_duration = std::min(duration, best_duration);
}
int recorded_pixel_count =
picture->LayerRect().width() * picture->LayerRect().height();
stats_instrumentation->AddRecord(best_duration, recorded_pixel_count);
}
bool found_tile_for_recorded_picture = false;
bool include_borders = true;
for (TilingData::Iterator it(&tiling_, record_rect, include_borders); it;
++it) {
const PictureMapKey& key = it.index();
gfx::Rect tile = PaddedRect(key);
if (record_rect.Contains(tile)) {
PictureInfo& info = picture_map_[key];
info.SetPicture(picture);
found_tile_for_recorded_picture = true;
}
}
DCHECK(found_tile_for_recorded_picture);
}
has_any_recordings_ = true;
DCHECK(CanRasterSlowTileCheck(recorded_viewport_));
return true;
}
}