This source file includes following definitions.
- Create
- CreateFromOther
- CreateCloneForDrawing
- clones_for_drawing_
- clones_for_drawing_
- GetCloneForDrawingOnThread
- RasterDirect
- RasterForAnalysis
- RasterToBitmap
- CoalesceRasters
- RasterCommon
- GetFlattenedPicture
- AnalyzeInRect
- AnalyzeInRect
- has_text
- tile_iterator_
- AdvanceToTilePictureWithPixelRefs
- DidBeginTracing
#include <algorithm>
#include <limits>
#include "base/debug/trace_event.h"
#include "cc/base/region.h"
#include "cc/debug/debug_colors.h"
#include "cc/resources/picture_pile_impl.h"
#include "cc/resources/raster_worker_pool.h"
#include "skia/ext/analysis_canvas.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkSize.h"
#include "ui/gfx/rect_conversions.h"
#include "ui/gfx/size_conversions.h"
#include "ui/gfx/skia_util.h"
namespace cc {
PicturePileImpl::ClonesForDrawing::ClonesForDrawing(
const PicturePileImpl* pile, int num_threads) {
for (int i = 0; i < num_threads; i++) {
scoped_refptr<PicturePileImpl> clone =
PicturePileImpl::CreateCloneForDrawing(pile, i);
clones_.push_back(clone);
}
}
PicturePileImpl::ClonesForDrawing::~ClonesForDrawing() {
}
scoped_refptr<PicturePileImpl> PicturePileImpl::Create() {
return make_scoped_refptr(new PicturePileImpl);
}
scoped_refptr<PicturePileImpl> PicturePileImpl::CreateFromOther(
const PicturePileBase* other) {
return make_scoped_refptr(new PicturePileImpl(other));
}
scoped_refptr<PicturePileImpl> PicturePileImpl::CreateCloneForDrawing(
const PicturePileImpl* other, unsigned thread_index) {
return make_scoped_refptr(new PicturePileImpl(other, thread_index));
}
PicturePileImpl::PicturePileImpl()
: clones_for_drawing_(ClonesForDrawing(this, 0)) {
}
PicturePileImpl::PicturePileImpl(const PicturePileBase* other)
: PicturePileBase(other),
clones_for_drawing_(ClonesForDrawing(
this, RasterWorkerPool::GetNumRasterThreads())) {
}
PicturePileImpl::PicturePileImpl(
const PicturePileImpl* other, unsigned thread_index)
: PicturePileBase(other, thread_index),
clones_for_drawing_(ClonesForDrawing(this, 0)) {
}
PicturePileImpl::~PicturePileImpl() {
}
PicturePileImpl* PicturePileImpl::GetCloneForDrawingOnThread(
unsigned thread_index) const {
CHECK_GT(clones_for_drawing_.clones_.size(), thread_index);
return clones_for_drawing_.clones_[thread_index].get();
}
void PicturePileImpl::RasterDirect(
SkCanvas* canvas,
const gfx::Rect& canvas_rect,
float contents_scale,
RenderingStatsInstrumentation* rendering_stats_instrumentation) {
RasterCommon(canvas,
NULL,
canvas_rect,
contents_scale,
rendering_stats_instrumentation,
false);
}
void PicturePileImpl::RasterForAnalysis(
skia::AnalysisCanvas* canvas,
const gfx::Rect& canvas_rect,
float contents_scale,
RenderingStatsInstrumentation* stats_instrumentation) {
RasterCommon(
canvas, canvas, canvas_rect, contents_scale, stats_instrumentation, true);
}
void PicturePileImpl::RasterToBitmap(
SkCanvas* canvas,
const gfx::Rect& canvas_rect,
float contents_scale,
RenderingStatsInstrumentation* rendering_stats_instrumentation) {
if (clear_canvas_with_debug_color_) {
canvas->clear(DebugColors::NonPaintedFillColor());
}
if (contents_opaque_ || contents_fill_bounds_completely_) {
gfx::SizeF total_content_size =
gfx::ScaleSize(tiling_.total_size(), contents_scale);
gfx::Rect content_rect(gfx::ToCeiledSize(total_content_size));
gfx::Rect deflated_content_rect = content_rect;
deflated_content_rect.Inset(0, 0, 1, 1);
if (!deflated_content_rect.Contains(canvas_rect)) {
canvas->save();
canvas->translate(-canvas_rect.x(), -canvas_rect.y());
gfx::Rect inflated_content_rect = content_rect;
inflated_content_rect.Inset(0, 0, -1, -1);
canvas->clipRect(gfx::RectToSkRect(inflated_content_rect),
SkRegion::kReplace_Op);
canvas->clipRect(gfx::RectToSkRect(deflated_content_rect),
SkRegion::kDifference_Op);
canvas->drawColor(background_color_, SkXfermode::kSrc_Mode);
canvas->restore();
}
} else {
TRACE_EVENT_INSTANT0("cc", "SkCanvas::clear", TRACE_EVENT_SCOPE_THREAD);
canvas->clear(SK_ColorTRANSPARENT);
}
RasterCommon(canvas,
NULL,
canvas_rect,
contents_scale,
rendering_stats_instrumentation,
false);
}
void PicturePileImpl::CoalesceRasters(const gfx::Rect& canvas_rect,
const gfx::Rect& content_rect,
float contents_scale,
PictureRegionMap* results) {
DCHECK(results);
gfx::Rect layer_rect = gfx::ScaleToEnclosingRect(
content_rect, 1.f / contents_scale);
int min_content_left = -1;
int min_content_top = -1;
int last_row_index = -1;
int last_col_index = -1;
gfx::Rect last_content_rect;
bool include_borders = true;
for (TilingData::Iterator tile_iter(&tiling_, layer_rect, include_borders);
tile_iter;
++tile_iter) {
PictureMap::iterator map_iter = picture_map_.find(tile_iter.index());
if (map_iter == picture_map_.end())
continue;
PictureInfo& info = map_iter->second;
Picture* picture = info.GetPicture();
if (!picture)
continue;
gfx::Rect chunk_rect = PaddedRect(tile_iter.index());
gfx::Rect content_clip =
gfx::ScaleToEnclosedRect(chunk_rect, contents_scale);
DCHECK(!content_clip.IsEmpty()) << "Layer rect: "
<< picture->LayerRect().ToString()
<< "Contents scale: " << contents_scale;
content_clip.Intersect(canvas_rect);
DCHECK_GE(tile_iter.index_y(), last_row_index);
if (tile_iter.index_y() > last_row_index) {
min_content_left = content_clip.x();
min_content_top = last_content_rect.bottom();
} else {
DCHECK_GT(tile_iter.index_x(), last_col_index);
min_content_left = last_content_rect.right();
min_content_top = last_content_rect.y();
}
last_col_index = tile_iter.index_x();
last_row_index = tile_iter.index_y();
int inset_left = std::max(0, min_content_left - content_clip.x());
int inset_top = std::max(0, min_content_top - content_clip.y());
content_clip.Inset(inset_left, inset_top, 0, 0);
PictureRegionMap::iterator it = results->find(picture);
Region* clip_region;
if (it == results->end()) {
clip_region = &(*results)[picture];
*clip_region = canvas_rect;
} else {
clip_region = &it->second;
}
DCHECK(clip_region->Contains(content_clip))
<< "Content clips should not overlap.";
clip_region->Subtract(content_clip);
last_content_rect = content_clip;
}
}
void PicturePileImpl::RasterCommon(
SkCanvas* canvas,
SkDrawPictureCallback* callback,
const gfx::Rect& canvas_rect,
float contents_scale,
RenderingStatsInstrumentation* rendering_stats_instrumentation,
bool is_analysis) {
DCHECK(contents_scale >= min_contents_scale_);
canvas->translate(-canvas_rect.x(), -canvas_rect.y());
gfx::SizeF total_content_size = gfx::ScaleSize(tiling_.total_size(),
contents_scale);
gfx::Rect total_content_rect(gfx::ToCeiledSize(total_content_size));
gfx::Rect content_rect = total_content_rect;
content_rect.Intersect(canvas_rect);
canvas->clipRect(gfx::RectToSkRect(content_rect),
SkRegion::kIntersect_Op);
PictureRegionMap picture_region_map;
CoalesceRasters(
canvas_rect, content_rect, contents_scale, &picture_region_map);
#ifndef NDEBUG
Region total_clip;
#endif
for (PictureRegionMap::iterator it = picture_region_map.begin();
it != picture_region_map.end();
++it) {
Picture* picture = it->first;
Region negated_clip_region = it->second;
#ifndef NDEBUG
Region positive_clip = content_rect;
positive_clip.Subtract(negated_clip_region);
DCHECK(!total_clip.Intersects(positive_clip));
total_clip.Union(positive_clip);
#endif
base::TimeDelta best_duration = base::TimeDelta::Max();
int repeat_count = std::max(1, slow_down_raster_scale_factor_for_debug_);
int rasterized_pixel_count = 0;
for (int j = 0; j < repeat_count; ++j) {
base::TimeTicks start_time;
if (rendering_stats_instrumentation)
start_time = rendering_stats_instrumentation->StartRecording();
rasterized_pixel_count = picture->Raster(
canvas, callback, negated_clip_region, contents_scale);
if (rendering_stats_instrumentation) {
base::TimeDelta duration =
rendering_stats_instrumentation->EndRecording(start_time);
best_duration = std::min(best_duration, duration);
}
}
if (rendering_stats_instrumentation) {
if (is_analysis) {
rendering_stats_instrumentation->AddAnalysis(best_duration,
rasterized_pixel_count);
} else {
rendering_stats_instrumentation->AddRaster(best_duration,
rasterized_pixel_count);
}
}
}
#ifndef NDEBUG
SkPaint paint;
for (Region::Iterator it(total_clip); it.has_rect(); it.next())
canvas->clipRect(gfx::RectToSkRect(it.rect()), SkRegion::kDifference_Op);
paint.setColor(DebugColors::MissingPictureFillColor());
paint.setXfermodeMode(SkXfermode::kSrc_Mode);
canvas->drawPaint(paint);
#endif
}
skia::RefPtr<SkPicture> PicturePileImpl::GetFlattenedPicture() {
TRACE_EVENT0("cc", "PicturePileImpl::GetFlattenedPicture");
gfx::Rect layer_rect(tiling_.total_size());
skia::RefPtr<SkPicture> picture = skia::AdoptRef(new SkPicture);
if (layer_rect.IsEmpty())
return picture;
SkCanvas* canvas = picture->beginRecording(
layer_rect.width(),
layer_rect.height(),
SkPicture::kUsePathBoundsForClip_RecordingFlag);
RasterToBitmap(canvas, layer_rect, 1.0, NULL);
picture->endRecording();
return picture;
}
void PicturePileImpl::AnalyzeInRect(
const gfx::Rect& content_rect,
float contents_scale,
PicturePileImpl::Analysis* analysis) {
AnalyzeInRect(content_rect, contents_scale, analysis, NULL);
}
void PicturePileImpl::AnalyzeInRect(
const gfx::Rect& content_rect,
float contents_scale,
PicturePileImpl::Analysis* analysis,
RenderingStatsInstrumentation* stats_instrumentation) {
DCHECK(analysis);
TRACE_EVENT0("cc", "PicturePileImpl::AnalyzeInRect");
gfx::Rect layer_rect = gfx::ScaleToEnclosingRect(
content_rect, 1.0f / contents_scale);
layer_rect.Intersect(gfx::Rect(tiling_.total_size()));
skia::AnalysisCanvas canvas(layer_rect.width(), layer_rect.height());
RasterForAnalysis(&canvas, layer_rect, 1.0f, stats_instrumentation);
analysis->is_solid_color = canvas.GetColorIfSolid(&analysis->solid_color);
analysis->has_text = canvas.HasText();
}
PicturePileImpl::Analysis::Analysis() : is_solid_color(false), has_text(true) {}
PicturePileImpl::Analysis::~Analysis() {
}
PicturePileImpl::PixelRefIterator::PixelRefIterator(
const gfx::Rect& content_rect,
float contents_scale,
const PicturePileImpl* picture_pile)
: picture_pile_(picture_pile),
layer_rect_(
gfx::ScaleToEnclosingRect(content_rect, 1.f / contents_scale)),
tile_iterator_(&picture_pile_->tiling_,
layer_rect_,
false ) {
if (!tile_iterator_)
return;
AdvanceToTilePictureWithPixelRefs();
}
PicturePileImpl::PixelRefIterator::~PixelRefIterator() {
}
PicturePileImpl::PixelRefIterator&
PicturePileImpl::PixelRefIterator::operator++() {
++pixel_ref_iterator_;
if (pixel_ref_iterator_)
return *this;
++tile_iterator_;
AdvanceToTilePictureWithPixelRefs();
return *this;
}
void PicturePileImpl::PixelRefIterator::AdvanceToTilePictureWithPixelRefs() {
for (; tile_iterator_; ++tile_iterator_) {
PictureMap::const_iterator it =
picture_pile_->picture_map_.find(tile_iterator_.index());
if (it == picture_pile_->picture_map_.end())
continue;
const Picture* picture = it->second.GetPicture();
if (!picture || (processed_pictures_.count(picture) != 0) ||
!picture->WillPlayBackBitmaps())
continue;
processed_pictures_.insert(picture);
pixel_ref_iterator_ = Picture::PixelRefIterator(layer_rect_, picture);
if (pixel_ref_iterator_)
break;
}
}
void PicturePileImpl::DidBeginTracing() {
gfx::Rect layer_rect(tiling_.total_size());
std::set<void*> processed_pictures;
for (PictureMap::iterator it = picture_map_.begin();
it != picture_map_.end();
++it) {
Picture* picture = it->second.GetPicture();
if (picture && (processed_pictures.count(picture) == 0)) {
picture->EmitTraceSnapshot();
processed_pictures.insert(picture);
}
}
}
}