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