1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218

cc / trees / damage_tracker.h [blame]

// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CC_TREES_DAMAGE_TRACKER_H_
#define CC_TREES_DAMAGE_TRACKER_H_

#include <algorithm>
#include <memory>
#include <utility>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "cc/cc_export.h"
#include "cc/layers/layer_collections.h"
#include "cc/layers/view_transition_content_layer_impl.h"
#include "cc/paint/element_id.h"
#include "cc/trees/damage_reason.h"
#include "ui/gfx/geometry/rect.h"

namespace gfx {
class Rect;
}

namespace cc {

class FilterOperations;
class LayerImpl;
class LayerTreeImpl;
class RenderSurfaceImpl;

// Computes the region where pixels have actually changed on a
// RenderSurfaceImpl. This region is used to scissor what is actually drawn to
// the screen to save GPU computation and bandwidth.
class CC_EXPORT DamageTracker {
 public:
  static std::unique_ptr<DamageTracker> Create();
  DamageTracker(const DamageTracker&) = delete;
  ~DamageTracker();

  DamageTracker& operator=(const DamageTracker&) = delete;

  static void UpdateDamageTracking(LayerTreeImpl* layer_tree_impl);

  void DidDrawDamagedArea() {
    current_damage_ = DamageAccumulator();
    has_damage_from_contributing_content_ = false;
  }
  void AddDamageNextUpdate(const gfx::Rect& dmg) {
    current_damage_.Union(dmg, {DamageReason::kUntracked});
  }

  bool GetDamageRectIfValid(gfx::Rect* rect);
  DamageReasonSet GetDamageReasons();

  bool has_damage_from_contributing_content() const {
    return has_damage_from_contributing_content_;
  }

 private:
  using ViewTransitionElementResourceIdToRenderSurfaceMap =
      base::flat_map<viz::ViewTransitionElementResourceId, RenderSurfaceImpl*>;

  DamageTracker();

  class DamageAccumulator {
   public:
    template <typename Type>
    void Union(const Type& rect, DamageReasonSet reasons) {
      if (rect.IsEmpty())
        return;

      // Can skip updating reasons only if the other rect is empty so this Union
      // is no-op. In particular, cannot skip updating reasons if this is
      // invalid, input rect is invalid, or input rect is a subrect of this.
      reasons_.PutAll(reasons);

      if (!is_valid_rect_) {
        return;
      }

      if (IsEmpty()) {
        x_ = rect.x();
        y_ = rect.y();
        right_ = rect.right();
        bottom_ = rect.bottom();
        return;
      }

      x_ = std::min(x_, rect.x());
      y_ = std::min(y_, rect.y());
      right_ = std::max(right_, rect.right());
      bottom_ = std::max(bottom_, rect.bottom());
    }

    void UnionReasons(DamageReasonSet reasons) { reasons_.PutAll(reasons); }

    int x() const { return x_; }
    int y() const { return y_; }
    int right() const { return right_; }
    int bottom() const { return bottom_; }
    bool IsEmpty() const { return x_ == right_ || y_ == bottom_; }

    bool GetAsRect(gfx::Rect* rect);

    DamageReasonSet reasons() const { return reasons_; }

   private:
    bool is_valid_rect_ = true;
    int x_ = 0;
    int y_ = 0;
    int right_ = 0;
    int bottom_ = 0;

    DamageReasonSet reasons_;
  };

  DamageAccumulator TrackDamageFromLeftoverRects();

  static void InitializeUpdateDamageTracking(
      LayerTreeImpl* layer_tree_impl,
      ViewTransitionElementResourceIdToRenderSurfaceMap&
          id_to_render_surface_map);

  // These helper functions are used only during UpdateDamageTracking().
  void PrepareForUpdate();
  // view_transition_content_surface corresponds to the render surface which
  // produces content drawn by a ViewTransitionContentLayer. Must be provided if
  // layer has a valid view transition resource id.
  void AccumulateDamageFromLayer(
      LayerImpl* layer,
      ViewTransitionElementResourceIdToRenderSurfaceMap&
          id_to_render_surface_map);
  void AccumulateDamageFromRenderSurface(RenderSurfaceImpl* render_surface);
  void ComputeSurfaceDamage(RenderSurfaceImpl* render_surface);
  void ExpandDamageInsideRectWithFilters(const gfx::Rect& pre_filter_rect,
                                         const FilterOperations& filters);

  gfx::Rect GetViewTransitionContentSurfaceDamageInSharedElementLayerSpace(
      LayerImpl* layer,
      ViewTransitionElementResourceIdToRenderSurfaceMap&
          id_to_render_surface_map);

  struct LayerRectMapData {
    LayerRectMapData() = default;
    explicit LayerRectMapData(int layer_id) : layer_id_(layer_id) {}
    void Update(const gfx::Rect& rect, unsigned int mailbox_id) {
      mailbox_id_ = mailbox_id;
      rect_ = rect;
    }

    bool operator<(const LayerRectMapData& other) const {
      return layer_id_ < other.layer_id_;
    }

    int layer_id_ = 0;
    unsigned int mailbox_id_ = 0;
    gfx::Rect rect_;
  };

  struct SurfaceRectMapData {
    SurfaceRectMapData() = default;
    explicit SurfaceRectMapData(ElementId surface_id)
        : surface_id_(surface_id) {}
    void Update(const gfx::Rect& rect, unsigned int mailbox_id) {
      mailbox_id_ = mailbox_id;
      rect_ = rect;
    }

    bool operator<(const SurfaceRectMapData& other) const {
      return surface_id_ < other.surface_id_;
    }

    ElementId surface_id_;
    unsigned int mailbox_id_ = 0;
    gfx::Rect rect_;
  };
  typedef std::vector<LayerRectMapData> SortedRectMapForLayers;
  typedef std::vector<SurfaceRectMapData> SortedRectMapForSurfaces;

  LayerRectMapData& RectDataForLayer(int layer_id, bool* layer_is_new);
  SurfaceRectMapData& RectDataForSurface(ElementId surface_id,
                                         bool* layer_is_new);

  SortedRectMapForLayers rect_history_for_layers_;
  SortedRectMapForSurfaces rect_history_for_surfaces_;

  unsigned int mailbox_id_ = 0;
  DamageAccumulator current_damage_;
  // Damage from contributing render surface and layer
  bool has_damage_from_contributing_content_ = false;

  // Damage accumulated since the last call to PrepareForUpdate().
  DamageAccumulator damage_for_this_update_;

  struct SurfaceWithRect {
    SurfaceWithRect(RenderSurfaceImpl* rs, const gfx::Rect& rect)
        : render_surface(rs), rect_in_target_space(rect) {}
    raw_ptr<RenderSurfaceImpl> render_surface;
    const gfx::Rect rect_in_target_space;
  };

  std::vector<SurfaceWithRect> contributing_surfaces_;

  // Track the view transition content render surfaces.
  // The corresponding content surface of a view transition layer might be
  // omitted. Surface appearing and disappearing should cause full damage on the
  // view transition layer. Tracking previous/current content surfaces to
  // determine surface appearing and disappearing.
  std::vector<viz::ViewTransitionElementResourceId>
      previous_view_transition_content_surfaces_by_id_;
  std::vector<viz::ViewTransitionElementResourceId>
      current_view_transition_content_surfaces_by_id_;
};

}  // namespace cc

#endif  // CC_TREES_DAMAGE_TRACKER_H_