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