Home | History | Annotate | Download | only in layers
      1 // Copyright 2010 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_LAYERS_TEXTURE_LAYER_H_
      6 #define CC_LAYERS_TEXTURE_LAYER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/callback.h"
     11 #include "cc/base/cc_export.h"
     12 #include "cc/layers/layer.h"
     13 #include "cc/resources/texture_mailbox.h"
     14 
     15 namespace WebKit { class WebGraphicsContext3D; }
     16 
     17 namespace base {
     18 class MessageLoopProxy;
     19 }
     20 
     21 namespace cc {
     22 
     23 class TextureLayerClient;
     24 
     25 // A Layer containing a the rendered output of a plugin instance.
     26 class CC_EXPORT TextureLayer : public Layer {
     27  public:
     28   // If this texture layer requires special preparation logic for each frame
     29   // driven by the compositor, pass in a non-nil client. Pass in a nil client
     30   // pointer if texture updates are driven by an external process.
     31   static scoped_refptr<TextureLayer> Create(TextureLayerClient* client);
     32 
     33   // Used when mailbox names are specified instead of texture IDs.
     34   static scoped_refptr<TextureLayer> CreateForMailbox(
     35       TextureLayerClient* client);
     36 
     37   void ClearClient();
     38 
     39   virtual scoped_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl)
     40       OVERRIDE;
     41 
     42   // Sets whether this texture should be Y-flipped at draw time. Defaults to
     43   // true.
     44   void SetFlipped(bool flipped);
     45 
     46   // Sets a UV transform to be used at draw time. Defaults to (0, 0) and (1, 1).
     47   void SetUV(gfx::PointF top_left, gfx::PointF bottom_right);
     48 
     49   // Sets an opacity value per vertex. It will be multiplied by the layer
     50   // opacity value.
     51   void SetVertexOpacity(float bottom_left,
     52                         float top_left,
     53                         float top_right,
     54                         float bottom_right);
     55 
     56   // Sets whether the alpha channel is premultiplied or unpremultiplied.
     57   // Defaults to true.
     58   void SetPremultipliedAlpha(bool premultiplied_alpha);
     59 
     60   // Sets whether the texture should be blended with the background color
     61   // at draw time. Defaults to false.
     62   void SetBlendBackgroundColor(bool blend);
     63 
     64   // Sets whether this context should rate limit on damage to prevent too many
     65   // frames from being queued up before the compositor gets a chance to run.
     66   // Requires a non-nil client.  Defaults to false.
     67   void SetRateLimitContext(bool rate_limit);
     68 
     69   // Code path for plugins which supply their own texture ID.
     70   void SetTextureId(unsigned texture_id);
     71 
     72   // Code path for plugins which supply their own mailbox.
     73   bool uses_mailbox() const { return uses_mailbox_; }
     74   void SetTextureMailbox(const TextureMailbox& mailbox);
     75 
     76   void WillModifyTexture();
     77 
     78   virtual void SetNeedsDisplayRect(const gfx::RectF& dirty_rect) OVERRIDE;
     79 
     80   virtual void SetLayerTreeHost(LayerTreeHost* layer_tree_host) OVERRIDE;
     81   virtual bool DrawsContent() const OVERRIDE;
     82   virtual bool Update(ResourceUpdateQueue* queue,
     83                       const OcclusionTracker* occlusion) OVERRIDE;
     84   virtual void PushPropertiesTo(LayerImpl* layer) OVERRIDE;
     85   virtual Region VisibleContentOpaqueRegion() const OVERRIDE;
     86   virtual bool BlocksPendingCommit() const OVERRIDE;
     87 
     88   virtual bool CanClipSelf() const OVERRIDE;
     89 
     90  protected:
     91   TextureLayer(TextureLayerClient* client, bool uses_mailbox);
     92   virtual ~TextureLayer();
     93 
     94  private:
     95   class MailboxHolder : public base::RefCountedThreadSafe<MailboxHolder> {
     96    public:
     97     class MainThreadReference {
     98      public:
     99       explicit MainThreadReference(MailboxHolder* holder);
    100       ~MainThreadReference();
    101       MailboxHolder* holder() { return holder_.get(); }
    102 
    103      private:
    104       scoped_refptr<MailboxHolder> holder_;
    105       DISALLOW_COPY_AND_ASSIGN(MainThreadReference);
    106     };
    107 
    108     static scoped_ptr<MainThreadReference> Create(
    109         const TextureMailbox& mailbox);
    110 
    111     const TextureMailbox& mailbox() const { return mailbox_; }
    112     void Return(unsigned sync_point, bool is_lost);
    113 
    114     // Gets a ReleaseCallback that can be called from another thread. Note: the
    115     // caller must ensure the callback is called.
    116     TextureMailbox::ReleaseCallback GetCallbackForImplThread();
    117 
    118    private:
    119     friend class base::RefCountedThreadSafe<MailboxHolder>;
    120     friend class MainThreadReference;
    121     explicit MailboxHolder(const TextureMailbox& mailbox);
    122     ~MailboxHolder();
    123     void InternalAddRef();
    124     void InternalRelease();
    125     void ReturnAndReleaseOnMainThread(unsigned sync_point, bool is_lost);
    126     void ReturnAndReleaseOnImplThread(unsigned sync_point, bool is_lost);
    127 
    128     // Thread safety notes: except for the thread-safe message_loop_, all fields
    129     // are only used on the main thread, or on the impl thread during commit
    130     // where the main thread is blocked.
    131     const scoped_refptr<base::MessageLoopProxy> message_loop_;
    132     unsigned internal_references_;
    133     TextureMailbox mailbox_;
    134     unsigned sync_point_;
    135     bool is_lost_;
    136     DISALLOW_COPY_AND_ASSIGN(MailboxHolder);
    137   };
    138 
    139   TextureLayerClient* client_;
    140   bool uses_mailbox_;
    141 
    142   bool flipped_;
    143   gfx::PointF uv_top_left_;
    144   gfx::PointF uv_bottom_right_;
    145   // [bottom left, top left, top right, bottom right]
    146   float vertex_opacity_[4];
    147   bool premultiplied_alpha_;
    148   bool blend_background_color_;
    149   bool rate_limit_context_;
    150   bool content_committed_;
    151 
    152   unsigned texture_id_;
    153   scoped_ptr<MailboxHolder::MainThreadReference> holder_ref_;
    154   bool needs_set_mailbox_;
    155 
    156   DISALLOW_COPY_AND_ASSIGN(TextureLayer);
    157 };
    158 
    159 }  // namespace cc
    160 #endif  // CC_LAYERS_TEXTURE_LAYER_H_
    161