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_LAYER_TREE_IMPL_H_
      6 #define CC_TREES_LAYER_TREE_IMPL_H_
      7 
      8 #include <list>
      9 #include <set>
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "base/containers/hash_tables.h"
     14 #include "base/values.h"
     15 #include "cc/base/scoped_ptr_vector.h"
     16 #include "cc/base/swap_promise.h"
     17 #include "cc/layers/layer_impl.h"
     18 #include "cc/output/renderer.h"
     19 #include "cc/resources/ui_resource_client.h"
     20 
     21 #if defined(COMPILER_GCC)
     22 namespace BASE_HASH_NAMESPACE {
     23 template<>
     24 struct hash<cc::LayerImpl*> {
     25   size_t operator()(cc::LayerImpl* ptr) const {
     26     return hash<size_t>()(reinterpret_cast<size_t>(ptr));
     27   }
     28 };
     29 }  // namespace BASE_HASH_NAMESPACE
     30 #endif  // COMPILER
     31 
     32 namespace cc {
     33 
     34 class ContextProvider;
     35 class DebugRectHistory;
     36 class FrameRateCounter;
     37 class HeadsUpDisplayLayerImpl;
     38 class LayerScrollOffsetDelegateProxy;
     39 class LayerTreeDebugState;
     40 class LayerTreeHostImpl;
     41 class LayerTreeImpl;
     42 class LayerTreeSettings;
     43 class MemoryHistory;
     44 class OutputSurface;
     45 class PaintTimeCounter;
     46 class PictureLayerImpl;
     47 class Proxy;
     48 class ResourceProvider;
     49 class TileManager;
     50 class UIResourceRequest;
     51 struct RendererCapabilities;
     52 
     53 typedef std::list<UIResourceRequest> UIResourceRequestQueue;
     54 
     55 class CC_EXPORT LayerTreeImpl {
     56  public:
     57   static scoped_ptr<LayerTreeImpl> create(
     58       LayerTreeHostImpl* layer_tree_host_impl) {
     59     return make_scoped_ptr(new LayerTreeImpl(layer_tree_host_impl));
     60   }
     61   virtual ~LayerTreeImpl();
     62 
     63   void Shutdown();
     64   void ReleaseResources();
     65 
     66   // Methods called by the layer tree that pass-through or access LTHI.
     67   // ---------------------------------------------------------------------------
     68   const LayerTreeSettings& settings() const;
     69   const RendererCapabilitiesImpl& GetRendererCapabilities() const;
     70   ContextProvider* context_provider() const;
     71   OutputSurface* output_surface() const;
     72   ResourceProvider* resource_provider() const;
     73   TileManager* tile_manager() const;
     74   FrameRateCounter* frame_rate_counter() const;
     75   PaintTimeCounter* paint_time_counter() const;
     76   MemoryHistory* memory_history() const;
     77   bool resourceless_software_draw() const;
     78   gfx::Size device_viewport_size() const;
     79   bool IsActiveTree() const;
     80   bool IsPendingTree() const;
     81   bool IsRecycleTree() const;
     82   LayerImpl* FindActiveTreeLayerById(int id);
     83   LayerImpl* FindPendingTreeLayerById(int id);
     84   LayerImpl* FindRecycleTreeLayerById(int id);
     85   int MaxTextureSize() const;
     86   bool PinchGestureActive() const;
     87   base::TimeTicks CurrentFrameTimeTicks() const;
     88   base::TimeDelta begin_impl_frame_interval() const;
     89   void SetNeedsCommit();
     90   gfx::Rect DeviceViewport() const;
     91   gfx::Size DrawViewportSize() const;
     92   const gfx::Rect ViewportRectForTilePriority() const;
     93   scoped_ptr<ScrollbarAnimationController> CreateScrollbarAnimationController(
     94       LayerImpl* scrolling_layer);
     95   void DidAnimateScrollOffset();
     96   bool use_gpu_rasterization() const;
     97   bool create_low_res_tiling() const;
     98 
     99   // Tree specific methods exposed to layer-impl tree.
    100   // ---------------------------------------------------------------------------
    101   void SetNeedsRedraw();
    102 
    103   // TODO(nduca): These are implemented in cc files temporarily, but will become
    104   // trivial accessors in a followup patch.
    105   const LayerTreeDebugState& debug_state() const;
    106   float device_scale_factor() const;
    107   DebugRectHistory* debug_rect_history() const;
    108   void GetAllTilesForTracing(std::set<const Tile*>* tiles) const;
    109   scoped_ptr<base::Value> AsValue() const;
    110 
    111   // Other public methods
    112   // ---------------------------------------------------------------------------
    113   LayerImpl* root_layer() const { return root_layer_.get(); }
    114   void SetRootLayer(scoped_ptr<LayerImpl>);
    115   scoped_ptr<LayerImpl> DetachLayerTree();
    116 
    117   void PushPropertiesTo(LayerTreeImpl* tree_impl);
    118 
    119   int source_frame_number() const { return source_frame_number_; }
    120   void set_source_frame_number(int frame_number) {
    121     source_frame_number_ = frame_number;
    122   }
    123 
    124   HeadsUpDisplayLayerImpl* hud_layer() { return hud_layer_; }
    125   void set_hud_layer(HeadsUpDisplayLayerImpl* layer_impl) {
    126     hud_layer_ = layer_impl;
    127   }
    128 
    129   LayerImpl* InnerViewportScrollLayer() const;
    130   // This function may return NULL, it is the caller's responsibility to check.
    131   LayerImpl* OuterViewportScrollLayer() const;
    132   gfx::Vector2dF TotalScrollOffset() const;
    133   gfx::Vector2dF TotalMaxScrollOffset() const;
    134   gfx::Vector2dF TotalScrollDelta() const;
    135 
    136   LayerImpl* InnerViewportContainerLayer() const;
    137   LayerImpl* CurrentlyScrollingLayer() const;
    138   void SetCurrentlyScrollingLayer(LayerImpl* layer);
    139   void ClearCurrentlyScrollingLayer();
    140   float VerticalAdjust(const int clip_layer_id) const;
    141 
    142   void SetViewportLayersFromIds(int page_scale_layer_id,
    143                                 int inner_viewport_scroll_layer_id,
    144                                 int outer_viewport_scroll_layer_id);
    145   void ClearViewportLayers();
    146   LayerImpl* page_scale_layer() { return page_scale_layer_; }
    147   void ApplySentScrollAndScaleDeltasFromAbortedCommit();
    148   void ApplyScrollDeltasSinceBeginMainFrame();
    149 
    150   SkColor background_color() const { return background_color_; }
    151   void set_background_color(SkColor color) { background_color_ = color; }
    152 
    153   bool has_transparent_background() const {
    154     return has_transparent_background_;
    155   }
    156   void set_has_transparent_background(bool transparent) {
    157     has_transparent_background_ = transparent;
    158   }
    159 
    160   void SetPageScaleFactorAndLimits(float page_scale_factor,
    161       float min_page_scale_factor, float max_page_scale_factor);
    162   void SetPageScaleDelta(float delta);
    163   void SetPageScaleValues(float page_scale_factor,
    164       float min_page_scale_factor, float max_page_scale_factor,
    165       float page_scale_delta);
    166   float total_page_scale_factor() const {
    167     return page_scale_factor_ * page_scale_delta_;
    168   }
    169   float page_scale_factor() const { return page_scale_factor_; }
    170   float min_page_scale_factor() const { return min_page_scale_factor_; }
    171   float max_page_scale_factor() const { return max_page_scale_factor_; }
    172   float page_scale_delta() const  { return page_scale_delta_; }
    173   void set_sent_page_scale_delta(float delta) {
    174     sent_page_scale_delta_ = delta;
    175   }
    176   float sent_page_scale_delta() const { return sent_page_scale_delta_; }
    177 
    178   // Updates draw properties and render surface layer list, as well as tile
    179   // priorities. Returns false if it was unable to update.
    180   bool UpdateDrawProperties();
    181 
    182   void set_needs_update_draw_properties() {
    183     needs_update_draw_properties_ = true;
    184   }
    185   bool needs_update_draw_properties() const {
    186     return needs_update_draw_properties_;
    187   }
    188 
    189   void set_needs_full_tree_sync(bool needs) { needs_full_tree_sync_ = needs; }
    190   bool needs_full_tree_sync() const { return needs_full_tree_sync_; }
    191 
    192   void ForceRedrawNextActivation() { next_activation_forces_redraw_ = true; }
    193 
    194   void set_ui_resource_request_queue(const UIResourceRequestQueue& queue);
    195 
    196   const LayerImplList& RenderSurfaceLayerList() const;
    197 
    198   // These return the size of the root scrollable area and the size of
    199   // the user-visible scrolling viewport, in CSS layout coordinates.
    200   gfx::Size ScrollableSize() const;
    201   gfx::SizeF ScrollableViewportSize() const;
    202 
    203   gfx::Rect RootScrollLayerDeviceViewportBounds() const;
    204 
    205   LayerImpl* LayerById(int id);
    206 
    207   // These should be called by LayerImpl's ctor/dtor.
    208   void RegisterLayer(LayerImpl* layer);
    209   void UnregisterLayer(LayerImpl* layer);
    210 
    211   AnimationRegistrar* animationRegistrar() const;
    212 
    213   void PushPersistedState(LayerTreeImpl* pending_tree);
    214 
    215   void DidBecomeActive();
    216 
    217   bool ContentsTexturesPurged() const;
    218   void SetContentsTexturesPurged();
    219   void ResetContentsTexturesPurged();
    220 
    221   void SetRequiresHighResToDraw();
    222   void ResetRequiresHighResToDraw();
    223   bool RequiresHighResToDraw() const;
    224 
    225   // Set on the active tree when the viewport size recently changed
    226   // and the active tree's size is now out of date.
    227   bool ViewportSizeInvalid() const;
    228   void SetViewportSizeInvalid();
    229   void ResetViewportSizeInvalid();
    230 
    231   // Useful for debug assertions, probably shouldn't be used for anything else.
    232   Proxy* proxy() const;
    233 
    234   void SetRootLayerScrollOffsetDelegate(
    235       LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate);
    236   void UpdateScrollOffsetDelegate();
    237   gfx::Vector2dF GetDelegatedScrollOffset(LayerImpl* layer);
    238 
    239   // Call this function when you expect there to be a swap buffer.
    240   // See swap_promise.h for how to use SwapPromise.
    241   void QueueSwapPromise(scoped_ptr<SwapPromise> swap_promise);
    242 
    243   // Take the |new_swap_promise| and append it to |swap_promise_list_|.
    244   void PassSwapPromises(ScopedPtrVector<SwapPromise>* new_swap_promise);
    245   void FinishSwapPromises(CompositorFrameMetadata* metadata);
    246   void BreakSwapPromises(SwapPromise::DidNotSwapReason reason);
    247 
    248   void DidModifyTilePriorities();
    249 
    250   ResourceProvider::ResourceId ResourceIdForUIResource(UIResourceId uid) const;
    251   void ProcessUIResourceRequestQueue();
    252 
    253   bool IsUIResourceOpaque(UIResourceId uid) const;
    254 
    255   void AddLayerWithCopyOutputRequest(LayerImpl* layer);
    256   void RemoveLayerWithCopyOutputRequest(LayerImpl* layer);
    257   const std::vector<LayerImpl*>& LayersWithCopyOutputRequest() const;
    258 
    259   int current_render_surface_list_id() const {
    260     return render_surface_layer_list_id_;
    261   }
    262 
    263   LayerImpl* FindFirstScrollingLayerThatIsHitByPoint(
    264       const gfx::PointF& screen_space_point);
    265 
    266   LayerImpl* FindLayerThatIsHitByPoint(const gfx::PointF& screen_space_point);
    267 
    268   LayerImpl* FindLayerThatIsHitByPointInTouchHandlerRegion(
    269       const gfx::PointF& screen_space_point);
    270 
    271   void RegisterPictureLayerImpl(PictureLayerImpl* layer);
    272   void UnregisterPictureLayerImpl(PictureLayerImpl* layer);
    273 
    274  protected:
    275   explicit LayerTreeImpl(LayerTreeHostImpl* layer_tree_host_impl);
    276   void ReleaseResourcesRecursive(LayerImpl* current);
    277 
    278   LayerTreeHostImpl* layer_tree_host_impl_;
    279   int source_frame_number_;
    280   scoped_ptr<LayerImpl> root_layer_;
    281   HeadsUpDisplayLayerImpl* hud_layer_;
    282   LayerImpl* currently_scrolling_layer_;
    283   LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate_;
    284   scoped_ptr<LayerScrollOffsetDelegateProxy>
    285       inner_viewport_scroll_delegate_proxy_;
    286   scoped_ptr<LayerScrollOffsetDelegateProxy>
    287       outer_viewport_scroll_delegate_proxy_;
    288   SkColor background_color_;
    289   bool has_transparent_background_;
    290 
    291   LayerImpl* page_scale_layer_;
    292   LayerImpl* inner_viewport_scroll_layer_;
    293   LayerImpl* outer_viewport_scroll_layer_;
    294 
    295   float page_scale_factor_;
    296   float page_scale_delta_;
    297   float sent_page_scale_delta_;
    298   float min_page_scale_factor_;
    299   float max_page_scale_factor_;
    300 
    301   typedef base::hash_map<int, LayerImpl*> LayerIdMap;
    302   LayerIdMap layer_id_map_;
    303 
    304   std::vector<LayerImpl*> layers_with_copy_output_request_;
    305 
    306   // Persisted state for non-impl-side-painting.
    307   int scrolling_layer_id_from_previous_tree_;
    308 
    309   // List of visible layers for the most recently prepared frame.
    310   LayerImplList render_surface_layer_list_;
    311 
    312   bool contents_textures_purged_;
    313   bool requires_high_res_to_draw_;
    314   bool viewport_size_invalid_;
    315   bool needs_update_draw_properties_;
    316 
    317   // In impl-side painting mode, this is true when the tree may contain
    318   // structural differences relative to the active tree.
    319   bool needs_full_tree_sync_;
    320 
    321   bool next_activation_forces_redraw_;
    322 
    323   ScopedPtrVector<SwapPromise> swap_promise_list_;
    324 
    325   UIResourceRequestQueue ui_resource_request_queue_;
    326 
    327   int render_surface_layer_list_id_;
    328 
    329  private:
    330   DISALLOW_COPY_AND_ASSIGN(LayerTreeImpl);
    331 };
    332 
    333 }  // namespace cc
    334 
    335 #endif  // CC_TREES_LAYER_TREE_IMPL_H_
    336