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