Home | History | Annotate | Download | only in trees
      1 // Copyright 2012 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 #ifndef CC_TREES_OCCLUSION_TRACKER_H_
      6 #define CC_TREES_OCCLUSION_TRACKER_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/basictypes.h"
     11 #include "cc/base/cc_export.h"
     12 #include "cc/base/region.h"
     13 #include "cc/layers/layer_iterator.h"
     14 #include "ui/gfx/rect.h"
     15 
     16 namespace cc {
     17 class LayerImpl;
     18 class RenderSurfaceImpl;
     19 class Layer;
     20 class RenderSurface;
     21 
     22 // This class is used to track occlusion of layers while traversing them in a
     23 // front-to-back order. As each layer is visited, one of the methods in this
     24 // class is called to notify it about the current target surface. Then,
     25 // occlusion in the content space of the current layer may be queried, via
     26 // methods such as Occluded() and UnoccludedContentRect(). If the current layer
     27 // owns a RenderSurfaceImpl, then occlusion on that RenderSurfaceImpl may also
     28 // be queried via surfaceOccluded() and surfaceUnoccludedContentRect(). Finally,
     29 // once finished with the layer, occlusion behind the layer should be marked by
     30 // calling MarkOccludedBehindLayer().
     31 template <typename LayerType>
     32 class CC_EXPORT OcclusionTracker {
     33  public:
     34   explicit OcclusionTracker(const gfx::Rect& screen_space_clip_rect);
     35   ~OcclusionTracker();
     36 
     37   // Called at the beginning of each step in the LayerIterator's front-to-back
     38   // traversal.
     39   void EnterLayer(const LayerIteratorPosition<LayerType>& layer_iterator);
     40   // Called at the end of each step in the LayerIterator's front-to-back
     41   // traversal.
     42   void LeaveLayer(const LayerIteratorPosition<LayerType>& layer_iterator);
     43 
     44   // Returns true if the given rect in content space for a layer is fully
     45   // occluded in either screen space or the layer's target surface.
     46   // |render_target| is the contributing layer's render target, and
     47   // |draw_transform| and |impl_draw_transform_is_unknown| are relative to that.
     48   bool Occluded(const LayerType* render_target,
     49                 const gfx::Rect& content_rect,
     50                 const gfx::Transform& draw_transform) const;
     51 
     52   // Gives an unoccluded sub-rect of |content_rect| in the content space of a
     53   // layer. Used when considering occlusion for a layer that paints/draws
     54   // something. |render_target| is the contributing layer's render target, and
     55   // |draw_transform| and |impl_draw_transform_is_unknown| are relative to that.
     56   gfx::Rect UnoccludedContentRect(const gfx::Rect& content_rect,
     57                                   const gfx::Transform& draw_transform) const;
     58 
     59   // Gives an unoccluded sub-rect of |content_rect| in the content space of the
     60   // render_target owned by the layer. Used when considering occlusion for a
     61   // contributing surface that is rendering into another target.
     62   gfx::Rect UnoccludedContributingSurfaceContentRect(
     63       const gfx::Rect& content_rect,
     64       const gfx::Transform& draw_transform) const;
     65 
     66   // Gives the region of the screen that is not occluded by something opaque.
     67   Region ComputeVisibleRegionInScreen() const {
     68     DCHECK(!stack_.back().target->parent());
     69     return SubtractRegions(screen_space_clip_rect_,
     70                            stack_.back().occlusion_from_inside_target);
     71   }
     72 
     73   void set_minimum_tracking_size(const gfx::Size& size) {
     74     minimum_tracking_size_ = size;
     75   }
     76 
     77   // The following is used for visualization purposes.
     78   void set_occluding_screen_space_rects_container(
     79       std::vector<gfx::Rect>* rects) {
     80     occluding_screen_space_rects_ = rects;
     81   }
     82   void set_non_occluding_screen_space_rects_container(
     83       std::vector<gfx::Rect>* rects) {
     84     non_occluding_screen_space_rects_ = rects;
     85   }
     86 
     87  protected:
     88   struct StackObject {
     89     StackObject() : target(0) {}
     90     explicit StackObject(const LayerType* target) : target(target) {}
     91     const LayerType* target;
     92     Region occlusion_from_outside_target;
     93     Region occlusion_from_inside_target;
     94   };
     95 
     96   // The stack holds occluded regions for subtrees in the
     97   // RenderSurfaceImpl-Layer tree, so that when we leave a subtree we may apply
     98   // a mask to it, but not to the parts outside the subtree.
     99   // - The first time we see a new subtree under a target, we add that target to
    100   // the top of the stack. This can happen as a layer representing itself, or as
    101   // a target surface.
    102   // - When we visit a target surface, we apply its mask to its subtree, which
    103   // is at the top of the stack.
    104   // - When we visit a layer representing itself, we add its occlusion to the
    105   // current subtree, which is at the top of the stack.
    106   // - When we visit a layer representing a contributing surface, the current
    107   // target will never be the top of the stack since we just came from the
    108   // contributing surface.
    109   // We merge the occlusion at the top of the stack with the new current
    110   // subtree. This new target is pushed onto the stack if not already there.
    111   std::vector<StackObject> stack_;
    112 
    113  private:
    114   // Called when visiting a layer representing itself. If the target was not
    115   // already current, then this indicates we have entered a new surface subtree.
    116   void EnterRenderTarget(const LayerType* new_target);
    117 
    118   // Called when visiting a layer representing a target surface. This indicates
    119   // we have visited all the layers within the surface, and we may perform any
    120   // surface-wide operations.
    121   void FinishedRenderTarget(const LayerType* finished_target);
    122 
    123   // Called when visiting a layer representing a contributing surface. This
    124   // indicates that we are leaving our current surface, and entering the new
    125   // one. We then perform any operations required for merging results from the
    126   // child subtree into its parent.
    127   void LeaveToRenderTarget(const LayerType* new_target);
    128 
    129   // Add the layer's occlusion to the tracked state.
    130   void MarkOccludedBehindLayer(const LayerType* layer);
    131 
    132   gfx::Rect screen_space_clip_rect_;
    133   gfx::Size minimum_tracking_size_;
    134 
    135   // This is used for visualizing the occlusion tracking process.
    136   std::vector<gfx::Rect>* occluding_screen_space_rects_;
    137   std::vector<gfx::Rect>* non_occluding_screen_space_rects_;
    138 
    139   DISALLOW_COPY_AND_ASSIGN(OcclusionTracker);
    140 };
    141 
    142 #if !defined(COMPILER_MSVC)
    143 extern template class OcclusionTracker<Layer>;
    144 extern template class OcclusionTracker<LayerImpl>;
    145 #endif
    146 
    147 }  // namespace cc
    148 
    149 #endif  // CC_TREES_OCCLUSION_TRACKER_H_
    150