Home | History | Annotate | Download | only in trees
      1 // Copyright 2011 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/trees/damage_tracker.h"
      6 
      7 #include <algorithm>
      8 
      9 #include "cc/base/math_util.h"
     10 #include "cc/layers/heads_up_display_layer_impl.h"
     11 #include "cc/layers/layer_impl.h"
     12 #include "cc/layers/render_surface_impl.h"
     13 #include "cc/output/filter_operations.h"
     14 #include "cc/trees/layer_tree_host_common.h"
     15 #include "cc/trees/layer_tree_impl.h"
     16 
     17 namespace cc {
     18 
     19 scoped_ptr<DamageTracker> DamageTracker::Create() {
     20   return make_scoped_ptr(new DamageTracker());
     21 }
     22 
     23 DamageTracker::DamageTracker()
     24   : mailboxId_(0) {}
     25 
     26 DamageTracker::~DamageTracker() {}
     27 
     28 static inline void ExpandRectWithFilters(
     29     gfx::RectF* rect, const FilterOperations& filters) {
     30   int top, right, bottom, left;
     31   filters.GetOutsets(&top, &right, &bottom, &left);
     32   rect->Inset(-left, -top, -right, -bottom);
     33 }
     34 
     35 static inline void ExpandDamageRectInsideRectWithFilters(
     36     gfx::RectF* damage_rect,
     37     const gfx::RectF& pre_filter_rect,
     38     const FilterOperations& filters) {
     39   gfx::RectF expanded_damage_rect = *damage_rect;
     40   ExpandRectWithFilters(&expanded_damage_rect, filters);
     41   gfx::RectF filter_rect = pre_filter_rect;
     42   ExpandRectWithFilters(&filter_rect, filters);
     43 
     44   expanded_damage_rect.Intersect(filter_rect);
     45   damage_rect->Union(expanded_damage_rect);
     46 }
     47 
     48 void DamageTracker::UpdateDamageTrackingState(
     49     const LayerImplList& layer_list,
     50     int target_surface_layer_id,
     51     bool target_surface_property_changed_only_from_descendant,
     52     gfx::Rect target_surface_content_rect,
     53     LayerImpl* target_surface_mask_layer,
     54     const FilterOperations& filters) {
     55   //
     56   // This function computes the "damage rect" of a target surface, and updates
     57   // the state that is used to correctly track damage across frames. The damage
     58   // rect is the region of the surface that may have changed and needs to be
     59   // redrawn. This can be used to scissor what is actually drawn, to save GPU
     60   // computation and bandwidth.
     61   //
     62   // The surface's damage rect is computed as the union of all possible changes
     63   // that have happened to the surface since the last frame was drawn. This
     64   // includes:
     65   //   - any changes for existing layers/surfaces that contribute to the target
     66   //     surface
     67   //   - layers/surfaces that existed in the previous frame, but no longer exist
     68   //
     69   // The basic algorithm for computing the damage region is as follows:
     70   //
     71   //   1. compute damage caused by changes in active/new layers
     72   //       for each layer in the layer_list:
     73   //           if the layer is actually a render_surface:
     74   //               add the surface's damage to our target surface.
     75   //           else
     76   //               add the layer's damage to the target surface.
     77   //
     78   //   2. compute damage caused by the target surface's mask, if it exists.
     79   //
     80   //   3. compute damage caused by old layers/surfaces that no longer exist
     81   //       for each leftover layer:
     82   //           add the old layer/surface bounds to the target surface damage.
     83   //
     84   //   4. combine all partial damage rects to get the full damage rect.
     85   //
     86   // Additional important points:
     87   //
     88   // - This algorithm is implicitly recursive; it assumes that descendant
     89   //   surfaces have already computed their damage.
     90   //
     91   // - Changes to layers/surfaces indicate "damage" to the target surface; If a
     92   //   layer is not changed, it does NOT mean that the layer can skip drawing.
     93   //   All layers that overlap the damaged region still need to be drawn. For
     94   //   example, if a layer changed its opacity, then layers underneath must be
     95   //   re-drawn as well, even if they did not change.
     96   //
     97   // - If a layer/surface property changed, the old bounds and new bounds may
     98   //   overlap... i.e. some of the exposed region may not actually be exposing
     99   //   anything. But this does not artificially inflate the damage rect. If the
    100   //   layer changed, its entire old bounds would always need to be redrawn,
    101   //   regardless of how much it overlaps with the layer's new bounds, which
    102   //   also need to be entirely redrawn.
    103   //
    104   // - See comments in the rest of the code to see what exactly is considered a
    105   //   "change" in a layer/surface.
    106   //
    107   // - To correctly manage exposed rects, SortedRectMap is maintained:
    108   //
    109   //      1. All existing rects from the previous frame are marked as
    110   //         not updated.
    111   //      2. The map contains all the layer bounds that contributed to
    112   //         the previous frame (even outside the previous damaged area). If a
    113   //         layer changes or does not exist anymore, those regions are then
    114   //         exposed and damage the target surface. As the algorithm progresses,
    115   //         entries are updated in the map until only leftover layers
    116   //         that no longer exist stay marked not updated.
    117   //
    118   //      3. After the damage rect is computed, the leftover not marked regions
    119   //         in a map are used to compute are damaged by deleted layers and
    120   //         erased from map.
    121   //
    122 
    123   PrepareRectHistoryForUpdate();
    124   // These functions cannot be bypassed with early-exits, even if we know what
    125   // the damage will be for this frame, because we need to update the damage
    126   // tracker state to correctly track the next frame.
    127   gfx::RectF damage_from_active_layers =
    128       TrackDamageFromActiveLayers(layer_list, target_surface_layer_id);
    129   gfx::RectF damage_from_surface_mask =
    130       TrackDamageFromSurfaceMask(target_surface_mask_layer);
    131   gfx::RectF damage_from_leftover_rects = TrackDamageFromLeftoverRects();
    132 
    133   gfx::RectF damage_rect_for_this_update;
    134 
    135   if (target_surface_property_changed_only_from_descendant) {
    136     damage_rect_for_this_update = target_surface_content_rect;
    137   } else {
    138     // TODO(shawnsingh): can we clamp this damage to the surface's content rect?
    139     // (affects performance, but not correctness)
    140     damage_rect_for_this_update = damage_from_active_layers;
    141     damage_rect_for_this_update.Union(damage_from_surface_mask);
    142     damage_rect_for_this_update.Union(damage_from_leftover_rects);
    143 
    144     if (filters.HasReferenceFilter()) {
    145       // TODO(senorblanco):  Once SkImageFilter reports its outsets, use
    146       // those here to limit damage.
    147       damage_rect_for_this_update = target_surface_content_rect;
    148     } else if (filters.HasFilterThatMovesPixels()) {
    149       ExpandRectWithFilters(&damage_rect_for_this_update, filters);
    150     }
    151   }
    152 
    153   // Damage accumulates until we are notified that we actually did draw on that
    154   // frame.
    155   current_damage_rect_.Union(damage_rect_for_this_update);
    156 }
    157 
    158 DamageTracker::RectMapData& DamageTracker::RectDataForLayer(
    159     int layer_id,
    160     bool* layer_is_new) {
    161 
    162   RectMapData data(layer_id);
    163 
    164   SortedRectMap::iterator it = std::lower_bound(rect_history_.begin(),
    165     rect_history_.end(), data);
    166 
    167   if (it == rect_history_.end() || it->layer_id_ != layer_id) {
    168     *layer_is_new = true;
    169     it = rect_history_.insert(it, data);
    170   }
    171 
    172   return *it;
    173 }
    174 
    175 gfx::RectF DamageTracker::TrackDamageFromActiveLayers(
    176     const LayerImplList& layer_list,
    177     int target_surface_layer_id) {
    178   gfx::RectF damage_rect = gfx::RectF();
    179 
    180   for (size_t layer_index = 0; layer_index < layer_list.size(); ++layer_index) {
    181     // Visit layers in back-to-front order.
    182     LayerImpl* layer = layer_list[layer_index];
    183 
    184     // We skip damage from the HUD layer because (a) the HUD layer damages the
    185     // whole frame and (b) we don't want HUD layer damage to be shown by the
    186     // HUD damage rect visualization.
    187     if (layer == layer->layer_tree_impl()->hud_layer())
    188       continue;
    189 
    190     if (LayerTreeHostCommon::RenderSurfaceContributesToTarget<LayerImpl>(
    191             layer, target_surface_layer_id))
    192       ExtendDamageForRenderSurface(layer, &damage_rect);
    193     else
    194       ExtendDamageForLayer(layer, &damage_rect);
    195   }
    196 
    197   return damage_rect;
    198 }
    199 
    200 gfx::RectF DamageTracker::TrackDamageFromSurfaceMask(
    201     LayerImpl* target_surface_mask_layer) {
    202   gfx::RectF damage_rect = gfx::RectF();
    203 
    204   if (!target_surface_mask_layer)
    205     return damage_rect;
    206 
    207   // Currently, if there is any change to the mask, we choose to damage the
    208   // entire surface. This could potentially be optimized later, but it is not
    209   // expected to be a common case.
    210   if (target_surface_mask_layer->LayerPropertyChanged() ||
    211       !target_surface_mask_layer->update_rect().IsEmpty()) {
    212     damage_rect = gfx::RectF(gfx::PointF(),
    213                              target_surface_mask_layer->bounds());
    214   }
    215 
    216   return damage_rect;
    217 }
    218 
    219 void DamageTracker::PrepareRectHistoryForUpdate() {
    220   mailboxId_++;
    221 }
    222 
    223 gfx::RectF DamageTracker::TrackDamageFromLeftoverRects() {
    224   // After computing damage for all active layers, any leftover items in the
    225   // current rect history correspond to layers/surfaces that no longer exist.
    226   // So, these regions are now exposed on the target surface.
    227 
    228   gfx::RectF damage_rect = gfx::RectF();
    229   SortedRectMap::iterator cur_pos = rect_history_.begin();
    230   SortedRectMap::iterator copy_pos = cur_pos;
    231 
    232   // Loop below basically implements std::remove_if loop with and extra
    233   // processing (adding deleted rect to damage_rect) for deleted items.
    234   // cur_pos iterator runs through all elements of the vector, but copy_pos
    235   // always points to the element after the last not deleted element. If new
    236   // not deleted element found then it is copied to the *copy_pos and copy_pos
    237   // moved to the next position.
    238   // If there are no deleted elements then copy_pos iterator is in sync with
    239   // cur_pos and no copy happens.
    240   while (cur_pos < rect_history_.end()) {
    241     if (cur_pos->mailboxId_ == mailboxId_) {
    242       if (cur_pos != copy_pos)
    243         *copy_pos = *cur_pos;
    244 
    245       ++copy_pos;
    246     } else {
    247       damage_rect.Union(cur_pos->rect_);
    248     }
    249 
    250     ++cur_pos;
    251   }
    252 
    253   if (copy_pos != rect_history_.end())
    254     rect_history_.erase(copy_pos, rect_history_.end());
    255 
    256   // If the vector has excessive storage, shrink it
    257   if (rect_history_.capacity() > rect_history_.size() * 4)
    258     SortedRectMap(rect_history_).swap(rect_history_);
    259 
    260   return damage_rect;
    261 }
    262 
    263 void DamageTracker::ExtendDamageForLayer(LayerImpl* layer,
    264                                          gfx::RectF* target_damage_rect) {
    265   // There are two ways that a layer can damage a region of the target surface:
    266   //   1. Property change (e.g. opacity, position, transforms):
    267   //        - the entire region of the layer itself damages the surface.
    268   //        - the old layer region also damages the surface, because this region
    269   //          is now exposed.
    270   //        - note that in many cases the old and new layer rects may overlap,
    271   //          which is fine.
    272   //
    273   //   2. Repaint/update: If a region of the layer that was repainted/updated,
    274   //      that region damages the surface.
    275   //
    276   // Property changes take priority over update rects.
    277   //
    278   // This method is called when we want to consider how a layer contributes to
    279   // its target RenderSurface, even if that layer owns the target RenderSurface
    280   // itself. To consider how a layer's target surface contributes to the
    281   // ancestor surface, ExtendDamageForRenderSurface() must be called instead.
    282 
    283   bool layer_is_new = false;
    284   RectMapData& data = RectDataForLayer(layer->id(), &layer_is_new);
    285   gfx::RectF old_rect_in_target_space = data.rect_;
    286 
    287   gfx::RectF rect_in_target_space = MathUtil::MapClippedRect(
    288       layer->draw_transform(),
    289       gfx::RectF(gfx::PointF(), layer->content_bounds()));
    290   data.Update(rect_in_target_space, mailboxId_);
    291 
    292   if (layer_is_new || layer->LayerPropertyChanged()) {
    293     // If a layer is new or has changed, then its entire layer rect affects the
    294     // target surface.
    295     target_damage_rect->Union(rect_in_target_space);
    296 
    297     // The layer's old region is now exposed on the target surface, too.
    298     // Note old_rect_in_target_space is already in target space.
    299     target_damage_rect->Union(old_rect_in_target_space);
    300   } else if (!layer->update_rect().IsEmpty()) {
    301     // If the layer properties haven't changed, then the the target surface is
    302     // only affected by the layer's update area, which could be empty.
    303     gfx::RectF update_content_rect =
    304         layer->LayerRectToContentRect(layer->update_rect());
    305     gfx::RectF update_rect_in_target_space =
    306         MathUtil::MapClippedRect(layer->draw_transform(), update_content_rect);
    307     target_damage_rect->Union(update_rect_in_target_space);
    308   }
    309 }
    310 
    311 void DamageTracker::ExtendDamageForRenderSurface(
    312     LayerImpl* layer, gfx::RectF* target_damage_rect) {
    313   // There are two ways a "descendant surface" can damage regions of the "target
    314   // surface":
    315   //   1. Property change:
    316   //        - a surface's geometry can change because of
    317   //            - changes to descendants (i.e. the subtree) that affect the
    318   //              surface's content rect
    319   //            - changes to ancestor layers that propagate their property
    320   //              changes to their entire subtree.
    321   //        - just like layers, both the old surface rect and new surface rect
    322   //          will damage the target surface in this case.
    323   //
    324   //   2. Damage rect: This surface may have been damaged by its own layer_list
    325   //      as well, and that damage should propagate to the target surface.
    326   //
    327 
    328   RenderSurfaceImpl* render_surface = layer->render_surface();
    329 
    330   bool surface_is_new = false;
    331   RectMapData& data = RectDataForLayer(layer->id(), &surface_is_new);
    332   gfx::RectF old_surface_rect = data.rect_;
    333 
    334   // The drawableContextRect() already includes the replica if it exists.
    335   gfx::RectF surface_rect_in_target_space =
    336       render_surface->DrawableContentRect();
    337   data.Update(surface_rect_in_target_space, mailboxId_);
    338 
    339   gfx::RectF damage_rect_in_local_space;
    340   if (surface_is_new || render_surface->SurfacePropertyChanged()) {
    341     // The entire surface contributes damage.
    342     damage_rect_in_local_space = render_surface->content_rect();
    343 
    344     // The surface's old region is now exposed on the target surface, too.
    345     target_damage_rect->Union(old_surface_rect);
    346   } else {
    347     // Only the surface's damage_rect will damage the target surface.
    348     damage_rect_in_local_space =
    349         render_surface->damage_tracker()->current_damage_rect();
    350   }
    351 
    352   // If there was damage, transform it to target space, and possibly contribute
    353   // its reflection if needed.
    354   if (!damage_rect_in_local_space.IsEmpty()) {
    355     const gfx::Transform& draw_transform = render_surface->draw_transform();
    356     gfx::RectF damage_rect_in_target_space =
    357         MathUtil::MapClippedRect(draw_transform, damage_rect_in_local_space);
    358     target_damage_rect->Union(damage_rect_in_target_space);
    359 
    360     if (layer->replica_layer()) {
    361       const gfx::Transform& replica_draw_transform =
    362           render_surface->replica_draw_transform();
    363       target_damage_rect->Union(MathUtil::MapClippedRect(
    364           replica_draw_transform, damage_rect_in_local_space));
    365     }
    366   }
    367 
    368   // If there was damage on the replica's mask, then the target surface receives
    369   // that damage as well.
    370   if (layer->replica_layer() && layer->replica_layer()->mask_layer()) {
    371     LayerImpl* replica_mask_layer = layer->replica_layer()->mask_layer();
    372 
    373     bool replica_is_new = false;
    374     RectMapData& data =
    375         RectDataForLayer(replica_mask_layer->id(), &replica_is_new);
    376 
    377     const gfx::Transform& replica_draw_transform =
    378         render_surface->replica_draw_transform();
    379     gfx::RectF replica_mask_layer_rect = MathUtil::MapClippedRect(
    380         replica_draw_transform,
    381         gfx::RectF(gfx::PointF(), replica_mask_layer->bounds()));
    382     data.Update(replica_mask_layer_rect, mailboxId_);
    383 
    384     // In the current implementation, a change in the replica mask damages the
    385     // entire replica region.
    386     if (replica_is_new ||
    387         replica_mask_layer->LayerPropertyChanged() ||
    388         !replica_mask_layer->update_rect().IsEmpty())
    389       target_damage_rect->Union(replica_mask_layer_rect);
    390   }
    391 
    392   // If the layer has a background filter, this may cause pixels in our surface
    393   // to be expanded, so we will need to expand any damage at or below this
    394   // layer. We expand the damage from this layer too, as we need to readback
    395   // those pixels from the surface with only the contents of layers below this
    396   // one in them. This means we need to redraw any pixels in the surface being
    397   // used for the blur in this layer this frame.
    398   if (layer->background_filters().HasFilterThatMovesPixels()) {
    399     ExpandDamageRectInsideRectWithFilters(target_damage_rect,
    400                                           surface_rect_in_target_space,
    401                                           layer->background_filters());
    402   }
    403 }
    404 
    405 }  // namespace cc
    406