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 base {
     33 namespace debug {
     34 class TracedValue;
     35 }
     36 }
     37 
     38 namespace cc {
     39 
     40 class ContextProvider;
     41 class DebugRectHistory;
     42 class FrameRateCounter;
     43 class HeadsUpDisplayLayerImpl;
     44 class LayerScrollOffsetDelegateProxy;
     45 class LayerTreeDebugState;
     46 class LayerTreeHostImpl;
     47 class LayerTreeImpl;
     48 class LayerTreeSettings;
     49 class MemoryHistory;
     50 class OutputSurface;
     51 class PageScaleAnimation;
     52 class PaintTimeCounter;
     53 class PictureLayerImpl;
     54 class Proxy;
     55 class ResourceProvider;
     56 class TileManager;
     57 class UIResourceRequest;
     58 struct RendererCapabilities;
     59 struct SelectionHandle;
     60 
     61 typedef std::list<UIResourceRequest> UIResourceRequestQueue;
     62 
     63 class CC_EXPORT LayerTreeImpl {
     64  public:
     65   static scoped_ptr<LayerTreeImpl> create(
     66       LayerTreeHostImpl* layer_tree_host_impl) {
     67     return make_scoped_ptr(new LayerTreeImpl(layer_tree_host_impl));
     68   }
     69   virtual ~LayerTreeImpl();
     70 
     71   void Shutdown();
     72   void ReleaseResources();
     73 
     74   // Methods called by the layer tree that pass-through or access LTHI.
     75   // ---------------------------------------------------------------------------
     76   const LayerTreeSettings& settings() const;
     77   const RendererCapabilitiesImpl& GetRendererCapabilities() const;
     78   ContextProvider* context_provider() const;
     79   OutputSurface* output_surface() const;
     80   ResourceProvider* resource_provider() const;
     81   TileManager* tile_manager() const;
     82   FrameRateCounter* frame_rate_counter() const;
     83   PaintTimeCounter* paint_time_counter() const;
     84   MemoryHistory* memory_history() const;
     85   gfx::Size device_viewport_size() const;
     86   bool IsActiveTree() const;
     87   bool IsPendingTree() const;
     88   bool IsRecycleTree() const;
     89   LayerImpl* FindActiveTreeLayerById(int id);
     90   LayerImpl* FindPendingTreeLayerById(int id);
     91   LayerImpl* FindRecycleTreeLayerById(int id);
     92   bool PinchGestureActive() const;
     93   BeginFrameArgs CurrentBeginFrameArgs() const;
     94   base::TimeDelta begin_impl_frame_interval() const;
     95   void SetNeedsCommit();
     96   gfx::Rect DeviceViewport() const;
     97   gfx::Size DrawViewportSize() const;
     98   const gfx::Rect ViewportRectForTilePriority() const;
     99   scoped_ptr<ScrollbarAnimationController> CreateScrollbarAnimationController(
    100       LayerImpl* scrolling_layer);
    101   void DidAnimateScrollOffset();
    102   void InputScrollAnimationFinished();
    103   bool use_gpu_rasterization() const;
    104   bool create_low_res_tiling() const;
    105   BlockingTaskRunner* BlockingMainThreadTaskRunner() const;
    106 
    107   // Tree specific methods exposed to layer-impl tree.
    108   // ---------------------------------------------------------------------------
    109   void SetNeedsRedraw();
    110 
    111   // TODO(nduca): These are implemented in cc files temporarily, but will become
    112   // trivial accessors in a followup patch.
    113   const LayerTreeDebugState& debug_state() const;
    114   float device_scale_factor() const;
    115   DebugRectHistory* debug_rect_history() const;
    116   void GetAllTilesForTracing(std::set<const Tile*>* tiles) const;
    117   void AsValueInto(base::debug::TracedValue* dict) const;
    118 
    119   // Other public methods
    120   // ---------------------------------------------------------------------------
    121   LayerImpl* root_layer() const { return root_layer_.get(); }
    122   void SetRootLayer(scoped_ptr<LayerImpl>);
    123   scoped_ptr<LayerImpl> DetachLayerTree();
    124 
    125   void PushPropertiesTo(LayerTreeImpl* tree_impl);
    126 
    127   int source_frame_number() const { return source_frame_number_; }
    128   void set_source_frame_number(int frame_number) {
    129     source_frame_number_ = frame_number;
    130   }
    131 
    132   HeadsUpDisplayLayerImpl* hud_layer() { return hud_layer_; }
    133   void set_hud_layer(HeadsUpDisplayLayerImpl* layer_impl) {
    134     hud_layer_ = layer_impl;
    135   }
    136 
    137   LayerImpl* InnerViewportScrollLayer() const;
    138   // This function may return NULL, it is the caller's responsibility to check.
    139   LayerImpl* OuterViewportScrollLayer() const;
    140   gfx::Vector2dF TotalScrollOffset() const;
    141   gfx::Vector2dF TotalMaxScrollOffset() const;
    142   gfx::Vector2dF TotalScrollDelta() const;
    143 
    144   LayerImpl* InnerViewportContainerLayer() const;
    145   LayerImpl* CurrentlyScrollingLayer() const;
    146   void SetCurrentlyScrollingLayer(LayerImpl* layer);
    147   void ClearCurrentlyScrollingLayer();
    148 
    149   void SetViewportLayersFromIds(int page_scale_layer_id,
    150                                 int inner_viewport_scroll_layer_id,
    151                                 int outer_viewport_scroll_layer_id);
    152   void ClearViewportLayers();
    153   LayerImpl* page_scale_layer() { return page_scale_layer_; }
    154   void ApplySentScrollAndScaleDeltasFromAbortedCommit();
    155   void ApplyScrollDeltasSinceBeginMainFrame();
    156 
    157   SkColor background_color() const { return background_color_; }
    158   void set_background_color(SkColor color) { background_color_ = color; }
    159 
    160   bool has_transparent_background() const {
    161     return has_transparent_background_;
    162   }
    163   void set_has_transparent_background(bool transparent) {
    164     has_transparent_background_ = transparent;
    165   }
    166 
    167   void SetPageScaleFactorAndLimits(float page_scale_factor,
    168       float min_page_scale_factor, float max_page_scale_factor);
    169   void SetPageScaleDelta(float delta);
    170   void SetPageScaleValues(float page_scale_factor,
    171       float min_page_scale_factor, float max_page_scale_factor,
    172       float page_scale_delta);
    173   float total_page_scale_factor() const {
    174     return page_scale_factor_ * page_scale_delta_;
    175   }
    176   float page_scale_factor() const { return page_scale_factor_; }
    177   float min_page_scale_factor() const { return min_page_scale_factor_; }
    178   float max_page_scale_factor() const { return max_page_scale_factor_; }
    179   float page_scale_delta() const  { return page_scale_delta_; }
    180   void set_sent_page_scale_delta(float delta) {
    181     sent_page_scale_delta_ = delta;
    182   }
    183   float sent_page_scale_delta() const { return sent_page_scale_delta_; }
    184 
    185   // Updates draw properties and render surface layer list, as well as tile
    186   // priorities. Returns false if it was unable to update.
    187   bool UpdateDrawProperties();
    188 
    189   void set_needs_update_draw_properties() {
    190     needs_update_draw_properties_ = true;
    191   }
    192   bool needs_update_draw_properties() const {
    193     return needs_update_draw_properties_;
    194   }
    195 
    196   void set_needs_full_tree_sync(bool needs) { needs_full_tree_sync_ = needs; }
    197   bool needs_full_tree_sync() const { return needs_full_tree_sync_; }
    198 
    199   void ForceRedrawNextActivation() { next_activation_forces_redraw_ = true; }
    200 
    201   void set_has_ever_been_drawn(bool has_drawn) {
    202     has_ever_been_drawn_ = has_drawn;
    203   }
    204   bool has_ever_been_drawn() const { return has_ever_been_drawn_; }
    205 
    206   void set_ui_resource_request_queue(const UIResourceRequestQueue& queue);
    207 
    208   const LayerImplList& RenderSurfaceLayerList() const;
    209 
    210   // These return the size of the root scrollable area and the size of
    211   // the user-visible scrolling viewport, in CSS layout coordinates.
    212   gfx::Size ScrollableSize() const;
    213   gfx::SizeF ScrollableViewportSize() const;
    214 
    215   gfx::Rect RootScrollLayerDeviceViewportBounds() const;
    216 
    217   LayerImpl* LayerById(int id);
    218 
    219   // These should be called by LayerImpl's ctor/dtor.
    220   void RegisterLayer(LayerImpl* layer);
    221   void UnregisterLayer(LayerImpl* layer);
    222 
    223   size_t NumLayers();
    224 
    225   AnimationRegistrar* animationRegistrar() const;
    226 
    227   void PushPersistedState(LayerTreeImpl* pending_tree);
    228 
    229   void DidBecomeActive();
    230 
    231   bool ContentsTexturesPurged() const;
    232   void SetContentsTexturesPurged();
    233   void ResetContentsTexturesPurged();
    234 
    235   void SetRequiresHighResToDraw();
    236   void ResetRequiresHighResToDraw();
    237   bool RequiresHighResToDraw() const;
    238 
    239   // Set on the active tree when the viewport size recently changed
    240   // and the active tree's size is now out of date.
    241   bool ViewportSizeInvalid() const;
    242   void SetViewportSizeInvalid();
    243   void ResetViewportSizeInvalid();
    244 
    245   // Useful for debug assertions, probably shouldn't be used for anything else.
    246   Proxy* proxy() const;
    247 
    248   void SetRootLayerScrollOffsetDelegate(
    249       LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate);
    250   void UpdateScrollOffsetDelegate();
    251   gfx::Vector2dF GetDelegatedScrollOffset(LayerImpl* layer);
    252 
    253   // Call this function when you expect there to be a swap buffer.
    254   // See swap_promise.h for how to use SwapPromise.
    255   void QueueSwapPromise(scoped_ptr<SwapPromise> swap_promise);
    256 
    257   // Take the |new_swap_promise| and append it to |swap_promise_list_|.
    258   void PassSwapPromises(ScopedPtrVector<SwapPromise>* new_swap_promise);
    259   void FinishSwapPromises(CompositorFrameMetadata* metadata);
    260   void BreakSwapPromises(SwapPromise::DidNotSwapReason reason);
    261 
    262   void DidModifyTilePriorities();
    263 
    264   ResourceProvider::ResourceId ResourceIdForUIResource(UIResourceId uid) const;
    265   void ProcessUIResourceRequestQueue();
    266 
    267   bool IsUIResourceOpaque(UIResourceId uid) const;
    268 
    269   void AddLayerWithCopyOutputRequest(LayerImpl* layer);
    270   void RemoveLayerWithCopyOutputRequest(LayerImpl* layer);
    271   const std::vector<LayerImpl*>& LayersWithCopyOutputRequest() const;
    272 
    273   int current_render_surface_list_id() const {
    274     return render_surface_layer_list_id_;
    275   }
    276 
    277   LayerImpl* FindFirstScrollingLayerThatIsHitByPoint(
    278       const gfx::PointF& screen_space_point);
    279 
    280   LayerImpl* FindLayerThatIsHitByPoint(const gfx::PointF& screen_space_point);
    281 
    282   LayerImpl* FindLayerThatIsHitByPointInTouchHandlerRegion(
    283       const gfx::PointF& screen_space_point);
    284 
    285   void RegisterSelection(const LayerSelectionBound& start,
    286                          const LayerSelectionBound& end);
    287 
    288   // Compute the current selection handle location and visbility with respect to
    289   // the viewport.
    290   void GetViewportSelection(ViewportSelectionBound* start,
    291                             ViewportSelectionBound* end);
    292 
    293   void RegisterPictureLayerImpl(PictureLayerImpl* layer);
    294   void UnregisterPictureLayerImpl(PictureLayerImpl* layer);
    295 
    296   void set_top_controls_layout_height(float height) {
    297     top_controls_layout_height_ = height;
    298   }
    299   void set_top_controls_content_offset(float offset) {
    300     top_controls_content_offset_ = offset;
    301   }
    302   void set_top_controls_delta(float delta) {
    303     top_controls_delta_ = delta;
    304   }
    305   void set_sent_top_controls_delta(float sent_delta) {
    306     sent_top_controls_delta_ = sent_delta;
    307   }
    308 
    309   float top_controls_layout_height() const {
    310     return top_controls_layout_height_;
    311   }
    312   float top_controls_content_offset() const {
    313     return top_controls_content_offset_;
    314   }
    315   float top_controls_delta() const {
    316     return top_controls_delta_;
    317   }
    318   float sent_top_controls_delta() const {
    319     return sent_top_controls_delta_;
    320   }
    321   float total_top_controls_content_offset() const {
    322     return top_controls_content_offset_ + top_controls_delta_;
    323   }
    324 
    325   void SetPageScaleAnimation(const gfx::Vector2d& target_offset,
    326                              bool anchor_point,
    327                              float page_scale,
    328                              base::TimeDelta duration);
    329   scoped_ptr<PageScaleAnimation> TakePageScaleAnimation();
    330 
    331  protected:
    332   explicit LayerTreeImpl(LayerTreeHostImpl* layer_tree_host_impl);
    333   void ReleaseResourcesRecursive(LayerImpl* current);
    334 
    335   LayerTreeHostImpl* layer_tree_host_impl_;
    336   int source_frame_number_;
    337   scoped_ptr<LayerImpl> root_layer_;
    338   HeadsUpDisplayLayerImpl* hud_layer_;
    339   LayerImpl* currently_scrolling_layer_;
    340   LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate_;
    341   scoped_ptr<LayerScrollOffsetDelegateProxy>
    342       inner_viewport_scroll_delegate_proxy_;
    343   scoped_ptr<LayerScrollOffsetDelegateProxy>
    344       outer_viewport_scroll_delegate_proxy_;
    345   SkColor background_color_;
    346   bool has_transparent_background_;
    347 
    348   LayerImpl* page_scale_layer_;
    349   LayerImpl* inner_viewport_scroll_layer_;
    350   LayerImpl* outer_viewport_scroll_layer_;
    351 
    352   LayerSelectionBound selection_start_;
    353   LayerSelectionBound selection_end_;
    354 
    355   float page_scale_factor_;
    356   float page_scale_delta_;
    357   float sent_page_scale_delta_;
    358   float min_page_scale_factor_;
    359   float max_page_scale_factor_;
    360 
    361   typedef base::hash_map<int, LayerImpl*> LayerIdMap;
    362   LayerIdMap layer_id_map_;
    363 
    364   std::vector<LayerImpl*> layers_with_copy_output_request_;
    365 
    366   // Persisted state for non-impl-side-painting.
    367   int scrolling_layer_id_from_previous_tree_;
    368 
    369   // List of visible layers for the most recently prepared frame.
    370   LayerImplList render_surface_layer_list_;
    371 
    372   bool contents_textures_purged_;
    373   bool requires_high_res_to_draw_;
    374   bool viewport_size_invalid_;
    375   bool needs_update_draw_properties_;
    376 
    377   // In impl-side painting mode, this is true when the tree may contain
    378   // structural differences relative to the active tree.
    379   bool needs_full_tree_sync_;
    380 
    381   bool next_activation_forces_redraw_;
    382 
    383   bool has_ever_been_drawn_;
    384 
    385   ScopedPtrVector<SwapPromise> swap_promise_list_;
    386 
    387   UIResourceRequestQueue ui_resource_request_queue_;
    388 
    389   int render_surface_layer_list_id_;
    390 
    391   // The top controls content offset at the time of the last layout (and thus,
    392   // viewport resize) in Blink. i.e. How much the viewport was shrunk by the top
    393   // controls.
    394   float top_controls_layout_height_;
    395 
    396   // The up-to-date content offset of the top controls, i.e. the amount that the
    397   // web contents have been shifted down from the top of the device viewport.
    398   float top_controls_content_offset_;
    399   float top_controls_delta_;
    400   float sent_top_controls_delta_;
    401 
    402   scoped_ptr<PageScaleAnimation> page_scale_animation_;
    403 
    404  private:
    405   DISALLOW_COPY_AND_ASSIGN(LayerTreeImpl);
    406 };
    407 
    408 }  // namespace cc
    409 
    410 #endif  // CC_TREES_LAYER_TREE_IMPL_H_
    411