Home | History | Annotate | Download | only in compositor
      1 // Copyright (c) 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 UI_COMPOSITOR_LAYER_H_
      6 #define UI_COMPOSITOR_LAYER_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/compiler_specific.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/message_loop/message_loop.h"
     15 #include "cc/animation/animation_events.h"
     16 #include "cc/animation/layer_animation_event_observer.h"
     17 #include "cc/base/scoped_ptr_vector.h"
     18 #include "cc/layers/content_layer_client.h"
     19 #include "cc/layers/layer_client.h"
     20 #include "cc/layers/texture_layer_client.h"
     21 #include "cc/resources/texture_mailbox.h"
     22 #include "third_party/skia/include/core/SkColor.h"
     23 #include "third_party/skia/include/core/SkRegion.h"
     24 #include "ui/compositor/compositor.h"
     25 #include "ui/compositor/layer_animation_delegate.h"
     26 #include "ui/compositor/layer_delegate.h"
     27 #include "ui/compositor/layer_type.h"
     28 #include "ui/gfx/rect.h"
     29 #include "ui/gfx/transform.h"
     30 
     31 class SkCanvas;
     32 
     33 namespace cc {
     34 class ContentLayer;
     35 class CopyOutputRequest;
     36 class DelegatedFrameProvider;
     37 class DelegatedRendererLayer;
     38 class Layer;
     39 class ResourceUpdateQueue;
     40 class SolidColorLayer;
     41 class TextureLayer;
     42 struct ReturnedResource;
     43 typedef std::vector<ReturnedResource> ReturnedResourceArray;
     44 }
     45 
     46 namespace ui {
     47 
     48 class Compositor;
     49 class LayerAnimator;
     50 class Texture;
     51 
     52 // Layer manages a texture, transform and a set of child Layers. Any View that
     53 // has enabled layers ends up creating a Layer to manage the texture.
     54 // A Layer can also be created without a texture, in which case it renders
     55 // nothing and is simply used as a node in a hierarchy of layers.
     56 // Coordinate system used in layers is DIP (Density Independent Pixel)
     57 // coordinates unless explicitly mentioned as pixel coordinates.
     58 //
     59 // NOTE: unlike Views, each Layer does *not* own its children views. If you
     60 // delete a Layer and it has children, the parent of each child layer is set to
     61 // NULL, but the children are not deleted.
     62 class COMPOSITOR_EXPORT Layer
     63     : public LayerAnimationDelegate,
     64       NON_EXPORTED_BASE(public cc::ContentLayerClient),
     65       NON_EXPORTED_BASE(public cc::TextureLayerClient),
     66       NON_EXPORTED_BASE(public cc::LayerClient),
     67       NON_EXPORTED_BASE(public cc::LayerAnimationEventObserver) {
     68  public:
     69   Layer();
     70   explicit Layer(LayerType type);
     71   virtual ~Layer();
     72 
     73   // Retrieves the Layer's compositor. The Layer will walk up its parent chain
     74   // to locate it. Returns NULL if the Layer is not attached to a compositor.
     75   Compositor* GetCompositor();
     76 
     77   // Called by the compositor when the Layer is set as its root Layer. This can
     78   // only ever be called on the root layer.
     79   void SetCompositor(Compositor* compositor);
     80 
     81   LayerDelegate* delegate() { return delegate_; }
     82   void set_delegate(LayerDelegate* delegate) { delegate_ = delegate; }
     83 
     84   // Adds a new Layer to this Layer.
     85   void Add(Layer* child);
     86 
     87   // Removes a Layer from this Layer.
     88   void Remove(Layer* child);
     89 
     90   // Stacks |child| above all other children.
     91   void StackAtTop(Layer* child);
     92 
     93   // Stacks |child| directly above |other|.  Both must be children of this
     94   // layer.  Note that if |child| is initially stacked even higher, calling this
     95   // method will result in |child| being lowered in the stacking order.
     96   void StackAbove(Layer* child, Layer* other);
     97 
     98   // Stacks |child| below all other children.
     99   void StackAtBottom(Layer* child);
    100 
    101   // Stacks |child| directly below |other|.  Both must be children of this
    102   // layer.
    103   void StackBelow(Layer* child, Layer* other);
    104 
    105   // Returns the child Layers.
    106   const std::vector<Layer*>& children() const { return children_; }
    107 
    108   // The parent.
    109   const Layer* parent() const { return parent_; }
    110   Layer* parent() { return parent_; }
    111 
    112   LayerType type() const { return type_; }
    113 
    114   // Returns true if this Layer contains |other| somewhere in its children.
    115   bool Contains(const Layer* other) const;
    116 
    117   // The layer's animator is responsible for causing automatic animations when
    118   // properties are set. It also manages a queue of pending animations and
    119   // handles blending of animations. The layer takes ownership of the animator.
    120   void SetAnimator(LayerAnimator* animator);
    121 
    122   // Returns the layer's animator. Creates a default animator of one has not
    123   // been set. Will not return NULL.
    124   LayerAnimator* GetAnimator();
    125 
    126   // The transform, relative to the parent.
    127   void SetTransform(const gfx::Transform& transform);
    128   gfx::Transform transform() const;
    129 
    130   // Return the target transform if animator is running, or the current
    131   // transform otherwise.
    132   gfx::Transform GetTargetTransform() const;
    133 
    134   // The bounds, relative to the parent.
    135   void SetBounds(const gfx::Rect& bounds);
    136   const gfx::Rect& bounds() const { return bounds_; }
    137 
    138   // Return the target bounds if animator is running, or the current bounds
    139   // otherwise.
    140   gfx::Rect GetTargetBounds() const;
    141 
    142   // Sets/gets whether or not drawing of child layers should be clipped to the
    143   // bounds of this layer.
    144   void SetMasksToBounds(bool masks_to_bounds);
    145   bool GetMasksToBounds() const;
    146 
    147   // The opacity of the layer. The opacity is applied to each pixel of the
    148   // texture (resulting alpha = opacity * alpha).
    149   float opacity() const;
    150   void SetOpacity(float opacity);
    151 
    152   // Returns the actual opacity, which the opacity of this layer multipled by
    153   // the combined opacity of the parent.
    154   float GetCombinedOpacity() const;
    155 
    156   // Blur pixels by this amount in anything below the layer and visible through
    157   // the layer.
    158   int background_blur() const { return background_blur_radius_; }
    159   void SetBackgroundBlur(int blur_radius);
    160 
    161   // Saturate all pixels of this layer by this amount.
    162   // This effect will get "combined" with the inverted,
    163   // brightness and grayscale setting.
    164   float layer_saturation() const { return layer_saturation_; }
    165   void SetLayerSaturation(float saturation);
    166 
    167   // Change the brightness of all pixels from this layer by this amount.
    168   // This effect will get "combined" with the inverted, saturate
    169   // and grayscale setting.
    170   float layer_brightness() const { return layer_brightness_; }
    171   void SetLayerBrightness(float brightness);
    172 
    173   // Return the target brightness if animator is running, or the current
    174   // brightness otherwise.
    175   float GetTargetBrightness() const;
    176 
    177   // Change the grayscale of all pixels from this layer by this amount.
    178   // This effect will get "combined" with the inverted, saturate
    179   // and brightness setting.
    180   float layer_grayscale() const { return layer_grayscale_; }
    181   void SetLayerGrayscale(float grayscale);
    182 
    183   // Return the target grayscale if animator is running, or the current
    184   // grayscale otherwise.
    185   float GetTargetGrayscale() const;
    186 
    187   // Zoom the background by a factor of |zoom|. The effect is blended along the
    188   // edge across |inset| pixels.
    189   void SetBackgroundZoom(float zoom, int inset);
    190 
    191   // Invert the layer.
    192   bool layer_inverted() const { return layer_inverted_; }
    193   void SetLayerInverted(bool inverted);
    194 
    195   // Return the target opacity if animator is running, or the current opacity
    196   // otherwise.
    197   float GetTargetOpacity() const;
    198 
    199   // Set a layer mask for a layer.
    200   // Note the provided layer mask can neither have a layer mask itself nor can
    201   // it have any children. The ownership of |layer_mask| will not be
    202   // transferred with this call.
    203   // Furthermore: A mask layer can only be set to one layer.
    204   void SetMaskLayer(Layer* layer_mask);
    205   Layer* layer_mask_layer() { return layer_mask_; }
    206 
    207   // Sets the visibility of the Layer. A Layer may be visible but not
    208   // drawn. This happens if any ancestor of a Layer is not visible.
    209   void SetVisible(bool visible);
    210   bool visible() const { return visible_; }
    211 
    212   // Returns the target visibility if the animator is running. Otherwise, it
    213   // returns the current visibility.
    214   bool GetTargetVisibility() const;
    215 
    216   // Returns true if this Layer is drawn. A Layer is drawn only if all ancestors
    217   // are visible.
    218   bool IsDrawn() const;
    219 
    220   // Returns true if this layer can have a texture (has_texture_ is true)
    221   // and is not completely obscured by a child.
    222   bool ShouldDraw() const;
    223 
    224   // Converts a point from the coordinates of |source| to the coordinates of
    225   // |target|. Necessarily, |source| and |target| must inhabit the same Layer
    226   // tree.
    227   static void ConvertPointToLayer(const Layer* source,
    228                                   const Layer* target,
    229                                   gfx::Point* point);
    230 
    231   // Converts a transform to be relative to the given |ancestor|. Returns
    232   // whether success (that is, whether the given ancestor was really an
    233   // ancestor of this layer).
    234   bool GetTargetTransformRelativeTo(const Layer* ancestor,
    235                                     gfx::Transform* transform) const;
    236 
    237   // Converts a ui::Layer's transform to the transform on the corresponding
    238   // cc::Layer.
    239   static gfx::Transform ConvertTransformToCCTransform(
    240       const gfx::Transform& transform,
    241       float device_scale_factor);
    242 
    243   // See description in View for details
    244   void SetFillsBoundsOpaquely(bool fills_bounds_opaquely);
    245   bool fills_bounds_opaquely() const { return fills_bounds_opaquely_; }
    246 
    247   const std::string& name() const { return name_; }
    248   void set_name(const std::string& name) { name_ = name; }
    249 
    250   const ui::Texture* texture() const { return texture_.get(); }
    251 
    252   // Assigns a new external texture.  |texture| can be NULL to disable external
    253   // updates.
    254   void SetExternalTexture(ui::Texture* texture);
    255   ui::Texture* external_texture() { return texture_.get(); }
    256 
    257   // Set new TextureMailbox for this layer. Note that |mailbox| may hold a
    258   // shared memory resource or an actual mailbox for a texture.
    259   void SetTextureMailbox(const cc::TextureMailbox& mailbox,
    260                          scoped_ptr<cc::SingleReleaseCallback> release_callback,
    261                          float scale_factor);
    262   cc::TextureMailbox GetTextureMailbox(float* scale_factor);
    263 
    264   // Begins showing delegated frames from the |frame_provider|.
    265   void SetShowDelegatedContent(cc::DelegatedFrameProvider* frame_provider,
    266                                gfx::Size frame_size_in_dip);
    267 
    268   bool has_external_content() {
    269     return texture_layer_.get() || delegated_renderer_layer_.get();
    270   }
    271 
    272   void SetShowPaintedContent();
    273 
    274   // Sets the layer's fill color.  May only be called for LAYER_SOLID_COLOR.
    275   void SetColor(SkColor color);
    276 
    277   // Adds |invalid_rect| to the Layer's pending invalid rect and calls
    278   // ScheduleDraw(). Returns false if the paint request is ignored.
    279   bool SchedulePaint(const gfx::Rect& invalid_rect);
    280 
    281   // Schedules a redraw of the layer tree at the compositor.
    282   // Note that this _does not_ invalidate any region of this layer; use
    283   // SchedulePaint() for that.
    284   void ScheduleDraw();
    285 
    286   // Sends damaged rectangles recorded in |damaged_region_| to
    287   // |compostior_| to repaint the content.
    288   void SendDamagedRects();
    289 
    290   const SkRegion& damaged_region() const { return damaged_region_; }
    291 
    292   // Suppresses painting the content by disgarding damaged region and ignoring
    293   // new paint requests.
    294   void SuppressPaint();
    295 
    296   // Notifies the layer that the device scale factor has changed.
    297   void OnDeviceScaleFactorChanged(float device_scale_factor);
    298 
    299   // Sets whether the layer should scale its content. If true, the canvas will
    300   // be scaled in software rendering mode before it is passed to
    301   // |LayerDelegate::OnPaint|.
    302   // Set to false if the delegate handles scaling.
    303   // NOTE: if this is called during |LayerDelegate::OnPaint|, the new value will
    304   // not apply to the canvas passed to the pending draw.
    305   void set_scale_content(bool scale_content) { scale_content_ = scale_content; }
    306 
    307   // Returns true if the layer scales its content.
    308   bool scale_content() const { return scale_content_; }
    309 
    310   // Sometimes the Layer is being updated by something other than SetCanvas
    311   // (e.g. the GPU process on UI_COMPOSITOR_IMAGE_TRANSPORT).
    312   bool layer_updated_externally() const { return layer_updated_externally_; }
    313 
    314   // Requets a copy of the layer's output as a texture or bitmap.
    315   void RequestCopyOfOutput(scoped_ptr<cc::CopyOutputRequest> request);
    316 
    317   // ContentLayerClient
    318   virtual void PaintContents(
    319       SkCanvas* canvas, gfx::Rect clip, gfx::RectF* opaque) OVERRIDE;
    320   virtual void DidChangeLayerCanUseLCDText() OVERRIDE {}
    321 
    322   cc::Layer* cc_layer() { return cc_layer_; }
    323 
    324   // TextureLayerClient
    325   virtual unsigned PrepareTexture() OVERRIDE;
    326   virtual bool PrepareTextureMailbox(
    327       cc::TextureMailbox* mailbox,
    328       scoped_ptr<cc::SingleReleaseCallback>* release_callback,
    329       bool use_shared_memory) OVERRIDE;
    330 
    331   float device_scale_factor() const { return device_scale_factor_; }
    332 
    333   // Forces a render surface to be used on this layer. This has no positive
    334   // impact, and is only used for benchmarking/testing purpose.
    335   void SetForceRenderSurface(bool force);
    336   bool force_render_surface() const { return force_render_surface_; }
    337 
    338   // LayerClient
    339   virtual std::string DebugName() OVERRIDE;
    340 
    341   virtual scoped_refptr<base::debug::ConvertableToTraceFormat>
    342       TakeDebugInfo() OVERRIDE;
    343 
    344   // LayerAnimationEventObserver
    345   virtual void OnAnimationStarted(const cc::AnimationEvent& event) OVERRIDE;
    346 
    347   // Whether this layer has animations waiting to get sent to its cc::Layer.
    348   bool HasPendingThreadedAnimations() {
    349     return pending_threaded_animations_.size() != 0;
    350   }
    351 
    352   // Triggers a call to SwitchToLayer.
    353   void SwitchCCLayerForTest();
    354 
    355  private:
    356   // Stacks |child| above or below |other|.  Helper method for StackAbove() and
    357   // StackBelow().
    358   void StackRelativeTo(Layer* child, Layer* other, bool above);
    359 
    360   bool ConvertPointForAncestor(const Layer* ancestor, gfx::Point* point) const;
    361   bool ConvertPointFromAncestor(const Layer* ancestor, gfx::Point* point) const;
    362 
    363   // Following are invoked from the animation or if no animation exists to
    364   // update the values immediately.
    365   void SetBoundsImmediately(const gfx::Rect& bounds);
    366   void SetTransformImmediately(const gfx::Transform& transform);
    367   void SetOpacityImmediately(float opacity);
    368   void SetVisibilityImmediately(bool visibility);
    369   void SetBrightnessImmediately(float brightness);
    370   void SetGrayscaleImmediately(float grayscale);
    371   void SetColorImmediately(SkColor color);
    372 
    373   // Implementation of LayerAnimatorDelegate
    374   virtual void SetBoundsFromAnimation(const gfx::Rect& bounds) OVERRIDE;
    375   virtual void SetTransformFromAnimation(
    376       const gfx::Transform& transform) OVERRIDE;
    377   virtual void SetOpacityFromAnimation(float opacity) OVERRIDE;
    378   virtual void SetVisibilityFromAnimation(bool visibility) OVERRIDE;
    379   virtual void SetBrightnessFromAnimation(float brightness) OVERRIDE;
    380   virtual void SetGrayscaleFromAnimation(float grayscale) OVERRIDE;
    381   virtual void SetColorFromAnimation(SkColor color) OVERRIDE;
    382   virtual void ScheduleDrawForAnimation() OVERRIDE;
    383   virtual const gfx::Rect& GetBoundsForAnimation() const OVERRIDE;
    384   virtual gfx::Transform GetTransformForAnimation() const OVERRIDE;
    385   virtual float GetOpacityForAnimation() const OVERRIDE;
    386   virtual bool GetVisibilityForAnimation() const OVERRIDE;
    387   virtual float GetBrightnessForAnimation() const OVERRIDE;
    388   virtual float GetGrayscaleForAnimation() const OVERRIDE;
    389   virtual SkColor GetColorForAnimation() const OVERRIDE;
    390   virtual float GetDeviceScaleFactor() const OVERRIDE;
    391   virtual void AddThreadedAnimation(
    392       scoped_ptr<cc::Animation> animation) OVERRIDE;
    393   virtual void RemoveThreadedAnimation(int animation_id) OVERRIDE;
    394 
    395   void CreateWebLayer();
    396   void RecomputeCCTransformFromTransform(const gfx::Transform& transform);
    397   void RecomputeDrawsContentAndUVRect();
    398   void RecomputePosition();
    399 
    400   // Set all filters which got applied to the layer.
    401   void SetLayerFilters();
    402 
    403   // Set all filters which got applied to the layer background.
    404   void SetLayerBackgroundFilters();
    405 
    406   void UpdateIsDrawn();
    407 
    408   void SwitchToLayer(scoped_refptr<cc::Layer> new_layer);
    409 
    410   // We cannot send animations to our cc_layer_ until we have been added to a
    411   // layer tree. Instead, we hold on to these animations in
    412   // pending_threaded_animations_, and expect SendPendingThreadedAnimations to
    413   // be called once we have been added to a tree.
    414   void SendPendingThreadedAnimations();
    415 
    416   const LayerType type_;
    417 
    418   Compositor* compositor_;
    419 
    420   scoped_refptr<ui::Texture> texture_;
    421 
    422   Layer* parent_;
    423 
    424   // This layer's children, in bottom-to-top stacking order.
    425   std::vector<Layer*> children_;
    426 
    427   gfx::Rect bounds_;
    428 
    429   // Visibility of this layer. See SetVisible/IsDrawn for more details.
    430   bool visible_;
    431 
    432   bool force_render_surface_;
    433 
    434   bool fills_bounds_opaquely_;
    435 
    436   // If true the layer is always up to date.
    437   bool layer_updated_externally_;
    438 
    439   // Union of damaged rects, in pixel coordinates, to be used when
    440   // compositor is ready to paint the content.
    441   SkRegion damaged_region_;
    442 
    443   int background_blur_radius_;
    444 
    445   // Several variables which will change the visible representation of
    446   // the layer.
    447   float layer_saturation_;
    448   float layer_brightness_;
    449   float layer_grayscale_;
    450   bool layer_inverted_;
    451 
    452   // The associated mask layer with this layer.
    453   Layer* layer_mask_;
    454   // The back link from the mask layer to it's associated masked layer.
    455   // We keep this reference for the case that if the mask layer gets deleted
    456   // while attached to the main layer before the main layer is deleted.
    457   Layer* layer_mask_back_link_;
    458 
    459   // The zoom factor to scale the layer by.  Zooming is disabled when this is
    460   // set to 1.
    461   float zoom_;
    462 
    463   // Width of the border in pixels, where the scaling is blended.
    464   int zoom_inset_;
    465 
    466   std::string name_;
    467 
    468   LayerDelegate* delegate_;
    469 
    470   scoped_refptr<LayerAnimator> animator_;
    471 
    472   // Animations that are passed to AddThreadedAnimation before this layer is
    473   // added to a tree.
    474   cc::ScopedPtrVector<cc::Animation> pending_threaded_animations_;
    475 
    476   // Ownership of the layer is held through one of the strongly typed layer
    477   // pointers, depending on which sort of layer this is.
    478   scoped_refptr<cc::ContentLayer> content_layer_;
    479   scoped_refptr<cc::TextureLayer> texture_layer_;
    480   scoped_refptr<cc::SolidColorLayer> solid_color_layer_;
    481   scoped_refptr<cc::DelegatedRendererLayer> delegated_renderer_layer_;
    482   cc::Layer* cc_layer_;
    483 
    484   // If true, the layer scales the canvas and the texture with the device scale
    485   // factor as apporpriate. When true, the texture size is in DIP.
    486   bool scale_content_;
    487 
    488   // A cached copy of |Compositor::device_scale_factor()|.
    489   float device_scale_factor_;
    490 
    491   // A cached copy of the TextureMailbox given texture_layer_.
    492   cc::TextureMailbox mailbox_;
    493 
    494   // Device scale factor in which mailbox_ was rendered in.
    495   float mailbox_scale_factor_;
    496 
    497   // The size of the delegated frame in DIP, set when SetShowDelegatedContent
    498   // was called.
    499   gfx::Size delegated_frame_size_in_dip_;
    500 
    501   DISALLOW_COPY_AND_ASSIGN(Layer);
    502 };
    503 
    504 }  // namespace ui
    505 
    506 #endif  // UI_COMPOSITOR_LAYER_H_
    507