This source file includes following definitions.
- Create
- ExpandRectWithFilters
- ExpandDamageRectInsideRectWithFilters
- UpdateDamageTrackingState
- RectDataForLayer
- TrackDamageFromActiveLayers
- TrackDamageFromSurfaceMask
- PrepareRectHistoryForUpdate
- TrackDamageFromLeftoverRects
- ExtendDamageForLayer
- ExtendDamageForRenderSurface
#include "cc/trees/damage_tracker.h"
#include <algorithm>
#include "cc/base/math_util.h"
#include "cc/layers/heads_up_display_layer_impl.h"
#include "cc/layers/layer_impl.h"
#include "cc/layers/render_surface_impl.h"
#include "cc/output/filter_operations.h"
#include "cc/trees/layer_tree_host_common.h"
#include "cc/trees/layer_tree_impl.h"
namespace cc {
scoped_ptr<DamageTracker> DamageTracker::Create() {
return make_scoped_ptr(new DamageTracker());
}
DamageTracker::DamageTracker()
: mailboxId_(0) {}
DamageTracker::~DamageTracker() {}
static inline void ExpandRectWithFilters(
gfx::RectF* rect, const FilterOperations& filters) {
int top, right, bottom, left;
filters.GetOutsets(&top, &right, &bottom, &left);
rect->Inset(-left, -top, -right, -bottom);
}
static inline void ExpandDamageRectInsideRectWithFilters(
gfx::RectF* damage_rect,
const gfx::RectF& pre_filter_rect,
const FilterOperations& filters) {
gfx::RectF expanded_damage_rect = *damage_rect;
ExpandRectWithFilters(&expanded_damage_rect, filters);
gfx::RectF filter_rect = pre_filter_rect;
ExpandRectWithFilters(&filter_rect, filters);
expanded_damage_rect.Intersect(filter_rect);
damage_rect->Union(expanded_damage_rect);
}
void DamageTracker::UpdateDamageTrackingState(
const LayerImplList& layer_list,
int target_surface_layer_id,
bool target_surface_property_changed_only_from_descendant,
const gfx::Rect& target_surface_content_rect,
LayerImpl* target_surface_mask_layer,
const FilterOperations& filters) {
PrepareRectHistoryForUpdate();
gfx::RectF damage_from_active_layers =
TrackDamageFromActiveLayers(layer_list, target_surface_layer_id);
gfx::RectF damage_from_surface_mask =
TrackDamageFromSurfaceMask(target_surface_mask_layer);
gfx::RectF damage_from_leftover_rects = TrackDamageFromLeftoverRects();
gfx::RectF damage_rect_for_this_update;
if (target_surface_property_changed_only_from_descendant) {
damage_rect_for_this_update = target_surface_content_rect;
} else {
damage_rect_for_this_update = damage_from_active_layers;
damage_rect_for_this_update.Union(damage_from_surface_mask);
damage_rect_for_this_update.Union(damage_from_leftover_rects);
if (filters.HasReferenceFilter()) {
damage_rect_for_this_update = target_surface_content_rect;
} else if (filters.HasFilterThatMovesPixels()) {
ExpandRectWithFilters(&damage_rect_for_this_update, filters);
}
}
current_damage_rect_.Union(damage_rect_for_this_update);
}
DamageTracker::RectMapData& DamageTracker::RectDataForLayer(
int layer_id,
bool* layer_is_new) {
RectMapData data(layer_id);
SortedRectMap::iterator it = std::lower_bound(rect_history_.begin(),
rect_history_.end(), data);
if (it == rect_history_.end() || it->layer_id_ != layer_id) {
*layer_is_new = true;
it = rect_history_.insert(it, data);
}
return *it;
}
gfx::RectF DamageTracker::TrackDamageFromActiveLayers(
const LayerImplList& layer_list,
int target_surface_layer_id) {
gfx::RectF damage_rect = gfx::RectF();
for (size_t layer_index = 0; layer_index < layer_list.size(); ++layer_index) {
LayerImpl* layer = layer_list[layer_index];
if (layer == layer->layer_tree_impl()->hud_layer())
continue;
if (LayerTreeHostCommon::RenderSurfaceContributesToTarget<LayerImpl>(
layer, target_surface_layer_id))
ExtendDamageForRenderSurface(layer, &damage_rect);
else
ExtendDamageForLayer(layer, &damage_rect);
}
return damage_rect;
}
gfx::RectF DamageTracker::TrackDamageFromSurfaceMask(
LayerImpl* target_surface_mask_layer) {
gfx::RectF damage_rect = gfx::RectF();
if (!target_surface_mask_layer)
return damage_rect;
if (target_surface_mask_layer->LayerPropertyChanged() ||
!target_surface_mask_layer->update_rect().IsEmpty()) {
damage_rect = gfx::RectF(gfx::PointF(),
target_surface_mask_layer->bounds());
}
return damage_rect;
}
void DamageTracker::PrepareRectHistoryForUpdate() {
mailboxId_++;
}
gfx::RectF DamageTracker::TrackDamageFromLeftoverRects() {
gfx::RectF damage_rect = gfx::RectF();
SortedRectMap::iterator cur_pos = rect_history_.begin();
SortedRectMap::iterator copy_pos = cur_pos;
while (cur_pos < rect_history_.end()) {
if (cur_pos->mailboxId_ == mailboxId_) {
if (cur_pos != copy_pos)
*copy_pos = *cur_pos;
++copy_pos;
} else {
damage_rect.Union(cur_pos->rect_);
}
++cur_pos;
}
if (copy_pos != rect_history_.end())
rect_history_.erase(copy_pos, rect_history_.end());
if (rect_history_.capacity() > rect_history_.size() * 4)
SortedRectMap(rect_history_).swap(rect_history_);
return damage_rect;
}
void DamageTracker::ExtendDamageForLayer(LayerImpl* layer,
gfx::RectF* target_damage_rect) {
bool layer_is_new = false;
RectMapData& data = RectDataForLayer(layer->id(), &layer_is_new);
gfx::RectF old_rect_in_target_space = data.rect_;
gfx::RectF rect_in_target_space = MathUtil::MapClippedRect(
layer->draw_transform(),
gfx::RectF(gfx::PointF(), layer->content_bounds()));
data.Update(rect_in_target_space, mailboxId_);
if (layer_is_new || layer->LayerPropertyChanged()) {
target_damage_rect->Union(rect_in_target_space);
target_damage_rect->Union(old_rect_in_target_space);
} else if (!layer->update_rect().IsEmpty()) {
gfx::RectF update_content_rect =
layer->LayerRectToContentRect(layer->update_rect());
gfx::RectF update_rect_in_target_space =
MathUtil::MapClippedRect(layer->draw_transform(), update_content_rect);
target_damage_rect->Union(update_rect_in_target_space);
}
}
void DamageTracker::ExtendDamageForRenderSurface(
LayerImpl* layer, gfx::RectF* target_damage_rect) {
RenderSurfaceImpl* render_surface = layer->render_surface();
bool surface_is_new = false;
RectMapData& data = RectDataForLayer(layer->id(), &surface_is_new);
gfx::RectF old_surface_rect = data.rect_;
gfx::RectF surface_rect_in_target_space =
render_surface->DrawableContentRect();
data.Update(surface_rect_in_target_space, mailboxId_);
gfx::RectF damage_rect_in_local_space;
if (surface_is_new || render_surface->SurfacePropertyChanged()) {
damage_rect_in_local_space = render_surface->content_rect();
target_damage_rect->Union(old_surface_rect);
} else {
damage_rect_in_local_space =
render_surface->damage_tracker()->current_damage_rect();
}
if (!damage_rect_in_local_space.IsEmpty()) {
const gfx::Transform& draw_transform = render_surface->draw_transform();
gfx::RectF damage_rect_in_target_space =
MathUtil::MapClippedRect(draw_transform, damage_rect_in_local_space);
target_damage_rect->Union(damage_rect_in_target_space);
if (layer->replica_layer()) {
const gfx::Transform& replica_draw_transform =
render_surface->replica_draw_transform();
target_damage_rect->Union(MathUtil::MapClippedRect(
replica_draw_transform, damage_rect_in_local_space));
}
}
if (layer->replica_layer() && layer->replica_layer()->mask_layer()) {
LayerImpl* replica_mask_layer = layer->replica_layer()->mask_layer();
bool replica_is_new = false;
RectMapData& data =
RectDataForLayer(replica_mask_layer->id(), &replica_is_new);
const gfx::Transform& replica_draw_transform =
render_surface->replica_draw_transform();
gfx::RectF replica_mask_layer_rect = MathUtil::MapClippedRect(
replica_draw_transform,
gfx::RectF(gfx::PointF(), replica_mask_layer->bounds()));
data.Update(replica_mask_layer_rect, mailboxId_);
if (replica_is_new ||
replica_mask_layer->LayerPropertyChanged() ||
!replica_mask_layer->update_rect().IsEmpty())
target_damage_rect->Union(replica_mask_layer_rect);
}
if (layer->background_filters().HasFilterThatMovesPixels()) {
ExpandDamageRectInsideRectWithFilters(target_damage_rect,
surface_rect_in_target_space,
layer->background_filters());
}
}
}