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 #ifndef CC_TREES_LAYER_TREE_HOST_COMMON_H_
      6 #define CC_TREES_LAYER_TREE_HOST_COMMON_H_
      7 
      8 #include <limits>
      9 #include <vector>
     10 
     11 #include "base/bind.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "cc/base/cc_export.h"
     14 #include "cc/base/scoped_ptr_vector.h"
     15 #include "cc/layers/layer_lists.h"
     16 #include "ui/gfx/rect.h"
     17 #include "ui/gfx/transform.h"
     18 #include "ui/gfx/vector2d.h"
     19 
     20 namespace cc {
     21 
     22 class LayerImpl;
     23 class Layer;
     24 
     25 class CC_EXPORT LayerTreeHostCommon {
     26  public:
     27   static gfx::Rect CalculateVisibleRect(gfx::Rect target_surface_rect,
     28                                         gfx::Rect layer_bound_rect,
     29                                         const gfx::Transform& transform);
     30 
     31   template <typename LayerType, typename RenderSurfaceLayerListType>
     32   struct CalcDrawPropsInputs {
     33    public:
     34     CalcDrawPropsInputs(LayerType* root_layer,
     35                         gfx::Size device_viewport_size,
     36                         const gfx::Transform& device_transform,
     37                         float device_scale_factor,
     38                         float page_scale_factor,
     39                         LayerType* page_scale_application_layer,
     40                         int max_texture_size,
     41                         bool can_use_lcd_text,
     42                         bool can_render_to_separate_surface,
     43                         bool can_adjust_raster_scales,
     44                         bool can_update_tile_priorities,
     45                         RenderSurfaceLayerListType* render_surface_layer_list)
     46         : root_layer(root_layer),
     47           device_viewport_size(device_viewport_size),
     48           device_transform(device_transform),
     49           device_scale_factor(device_scale_factor),
     50           page_scale_factor(page_scale_factor),
     51           page_scale_application_layer(page_scale_application_layer),
     52           max_texture_size(max_texture_size),
     53           can_use_lcd_text(can_use_lcd_text),
     54           can_render_to_separate_surface(can_render_to_separate_surface),
     55           can_adjust_raster_scales(can_adjust_raster_scales),
     56           can_update_tile_priorities(can_update_tile_priorities),
     57           render_surface_layer_list(render_surface_layer_list) {}
     58 
     59     LayerType* root_layer;
     60     gfx::Size device_viewport_size;
     61     const gfx::Transform& device_transform;
     62     float device_scale_factor;
     63     float page_scale_factor;
     64     LayerType* page_scale_application_layer;
     65     int max_texture_size;
     66     bool can_use_lcd_text;
     67     bool can_render_to_separate_surface;
     68     bool can_adjust_raster_scales;
     69     bool can_update_tile_priorities;
     70     RenderSurfaceLayerListType* render_surface_layer_list;
     71   };
     72 
     73   template <typename LayerType, typename RenderSurfaceLayerListType>
     74   struct CalcDrawPropsInputsForTesting
     75       : public CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType> {
     76     CalcDrawPropsInputsForTesting(
     77         LayerType* root_layer,
     78         gfx::Size device_viewport_size,
     79         const gfx::Transform& device_transform,
     80         RenderSurfaceLayerListType* render_surface_layer_list);
     81     CalcDrawPropsInputsForTesting(
     82         LayerType* root_layer,
     83         gfx::Size device_viewport_size,
     84         RenderSurfaceLayerListType* render_surface_layer_list);
     85 
     86    private:
     87     const gfx::Transform identity_transform_;
     88   };
     89 
     90   typedef CalcDrawPropsInputs<Layer, RenderSurfaceLayerList>
     91       CalcDrawPropsMainInputs;
     92   typedef CalcDrawPropsInputsForTesting<Layer, RenderSurfaceLayerList>
     93       CalcDrawPropsMainInputsForTesting;
     94   static void CalculateDrawProperties(CalcDrawPropsMainInputs* inputs);
     95 
     96   typedef CalcDrawPropsInputs<LayerImpl, LayerImplList> CalcDrawPropsImplInputs;
     97   typedef CalcDrawPropsInputsForTesting<LayerImpl, LayerImplList>
     98       CalcDrawPropsImplInputsForTesting;
     99   static void CalculateDrawProperties(CalcDrawPropsImplInputs* inputs);
    100 
    101   // Performs hit testing for a given render_surface_layer_list.
    102   static LayerImpl* FindLayerThatIsHitByPoint(
    103       gfx::PointF screen_space_point,
    104       const LayerImplList& render_surface_layer_list);
    105 
    106   static LayerImpl* FindLayerThatIsHitByPointInTouchHandlerRegion(
    107       gfx::PointF screen_space_point,
    108       const LayerImplList& render_surface_layer_list);
    109 
    110   static bool LayerHasTouchEventHandlersAt(gfx::PointF screen_space_point,
    111                                            LayerImpl* layer_impl);
    112 
    113   template <typename LayerType>
    114   static bool RenderSurfaceContributesToTarget(LayerType*,
    115                                                int target_surface_layer_id);
    116 
    117   template <typename LayerType>
    118   static void CallFunctionForSubtree(
    119       LayerType* root_layer,
    120       const base::Callback<void(LayerType* layer)>& function);
    121 
    122   // Returns a layer with the given id if one exists in the subtree starting
    123   // from the given root layer (including mask and replica layers).
    124   template <typename LayerType>
    125   static LayerType* FindLayerInSubtree(LayerType* root_layer, int layer_id);
    126 
    127   static Layer* get_child_as_raw_ptr(
    128       const LayerList& children,
    129       size_t index) {
    130     return children[index].get();
    131   }
    132 
    133   static LayerImpl* get_child_as_raw_ptr(
    134       const OwnedLayerImplList& children,
    135       size_t index) {
    136     return children[index];
    137   }
    138 
    139   struct ScrollUpdateInfo {
    140     int layer_id;
    141     gfx::Vector2d scroll_delta;
    142   };
    143 };
    144 
    145 struct CC_EXPORT ScrollAndScaleSet {
    146   ScrollAndScaleSet();
    147   ~ScrollAndScaleSet();
    148 
    149   std::vector<LayerTreeHostCommon::ScrollUpdateInfo> scrolls;
    150   float page_scale_delta;
    151 };
    152 
    153 template <typename LayerType>
    154 bool LayerTreeHostCommon::RenderSurfaceContributesToTarget(
    155     LayerType* layer,
    156     int target_surface_layer_id) {
    157   // A layer will either contribute its own content, or its render surface's
    158   // content, to the target surface. The layer contributes its surface's content
    159   // when both the following are true:
    160   //  (1) The layer actually has a render surface, and
    161   //  (2) The layer's render surface is not the same as the target surface.
    162   //
    163   // Otherwise, the layer just contributes itself to the target surface.
    164 
    165   return layer->render_surface() && layer->id() != target_surface_layer_id;
    166 }
    167 
    168 template <typename LayerType>
    169 LayerType* LayerTreeHostCommon::FindLayerInSubtree(LayerType* root_layer,
    170                                                    int layer_id) {
    171   if (!root_layer)
    172     return NULL;
    173 
    174   if (root_layer->id() == layer_id)
    175     return root_layer;
    176 
    177   if (root_layer->mask_layer() && root_layer->mask_layer()->id() == layer_id)
    178     return root_layer->mask_layer();
    179 
    180   if (root_layer->replica_layer() &&
    181       root_layer->replica_layer()->id() == layer_id)
    182     return root_layer->replica_layer();
    183 
    184   for (size_t i = 0; i < root_layer->children().size(); ++i) {
    185     if (LayerType* found = FindLayerInSubtree(
    186             get_child_as_raw_ptr(root_layer->children(), i), layer_id))
    187       return found;
    188   }
    189   return NULL;
    190 }
    191 
    192 template <typename LayerType>
    193 void LayerTreeHostCommon::CallFunctionForSubtree(
    194     LayerType* root_layer,
    195     const base::Callback<void(LayerType* layer)>& function) {
    196   function.Run(root_layer);
    197 
    198   if (LayerType* mask_layer = root_layer->mask_layer())
    199     function.Run(mask_layer);
    200   if (LayerType* replica_layer = root_layer->replica_layer()) {
    201     function.Run(replica_layer);
    202     if (LayerType* mask_layer = replica_layer->mask_layer())
    203       function.Run(mask_layer);
    204   }
    205 
    206   for (size_t i = 0; i < root_layer->children().size(); ++i) {
    207     CallFunctionForSubtree(get_child_as_raw_ptr(root_layer->children(), i),
    208                            function);
    209   }
    210 }
    211 
    212 template <typename LayerType, typename RenderSurfaceLayerListType>
    213 LayerTreeHostCommon::CalcDrawPropsInputsForTesting<LayerType,
    214                                                    RenderSurfaceLayerListType>::
    215     CalcDrawPropsInputsForTesting(
    216         LayerType* root_layer,
    217         gfx::Size device_viewport_size,
    218         const gfx::Transform& device_transform,
    219         RenderSurfaceLayerListType* render_surface_layer_list)
    220     : CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType>(
    221           root_layer,
    222           device_viewport_size,
    223           device_transform,
    224           1.f,
    225           1.f,
    226           NULL,
    227           std::numeric_limits<int>::max() / 2,
    228           false,
    229           true,
    230           false,
    231           render_surface_layer_list) {
    232   DCHECK(root_layer);
    233   DCHECK(render_surface_layer_list);
    234 }
    235 
    236 template <typename LayerType, typename RenderSurfaceLayerListType>
    237 LayerTreeHostCommon::CalcDrawPropsInputsForTesting<LayerType,
    238                                                    RenderSurfaceLayerListType>::
    239     CalcDrawPropsInputsForTesting(
    240         LayerType* root_layer,
    241         gfx::Size device_viewport_size,
    242         RenderSurfaceLayerListType* render_surface_layer_list)
    243     : CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType>(
    244           root_layer,
    245           device_viewport_size,
    246           identity_transform_,
    247           1.f,
    248           1.f,
    249           NULL,
    250           std::numeric_limits<int>::max() / 2,
    251           false,
    252           true,
    253           false,
    254           render_surface_layer_list) {
    255   DCHECK(root_layer);
    256   DCHECK(render_surface_layer_list);
    257 }
    258 
    259 }  // namespace cc
    260 
    261 #endif  // CC_TREES_LAYER_TREE_HOST_COMMON_H_
    262