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.
5 #ifndef CC_LAYERS_TEXTURE_LAYER_H_
6 #define CC_LAYERS_TEXTURE_LAYER_H_
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"
17 class BlockingTaskRunner
;
18 class SingleReleaseCallback
;
19 class TextureLayerClient
;
21 // A Layer containing a the rendered output of a plugin instance.
22 class CC_EXPORT TextureLayer
: public Layer
{
24 class CC_EXPORT TextureMailboxHolder
25 : public base::RefCountedThreadSafe
<TextureMailboxHolder
> {
27 class CC_EXPORT MainThreadReference
{
29 explicit MainThreadReference(TextureMailboxHolder
* holder
);
30 ~MainThreadReference();
31 TextureMailboxHolder
* holder() { return holder_
.get(); }
34 scoped_refptr
<TextureMailboxHolder
> holder_
;
35 DISALLOW_COPY_AND_ASSIGN(MainThreadReference
);
38 const TextureMailbox
& mailbox() const { return mailbox_
; }
39 void Return(uint32 sync_point
, bool is_lost
);
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();
46 friend class TextureLayer
;
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();
55 friend class base::RefCountedThreadSafe
<TextureMailboxHolder
>;
56 friend class MainThreadReference
;
57 explicit TextureMailboxHolder(
58 const TextureMailbox
& mailbox
,
59 scoped_ptr
<SingleReleaseCallback
> release_callback
);
61 void InternalAddRef();
62 void InternalRelease();
63 void ReturnAndReleaseOnImplThread(uint32 sync_point
, bool is_lost
);
65 // This member is thread safe, and is accessed on main and impl threads.
66 const scoped_refptr
<BlockingTaskRunner
> message_loop_
;
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_
;
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_
;
81 DISALLOW_COPY_AND_ASSIGN(TextureMailboxHolder
);
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
);
89 // Used when mailbox names are specified instead of texture IDs.
90 static scoped_refptr
<TextureLayer
> CreateForMailbox(
91 TextureLayerClient
* client
);
95 virtual scoped_ptr
<LayerImpl
> CreateLayerImpl(LayerTreeImpl
* tree_impl
)
98 // Sets whether this texture should be Y-flipped at draw time. Defaults to
100 void SetFlipped(bool flipped
);
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
);
105 // Sets an opacity value per vertex. It will be multiplied by the layer
107 void SetVertexOpacity(float bottom_left
,
112 // Sets whether the alpha channel is premultiplied or unpremultiplied.
114 void SetPremultipliedAlpha(bool premultiplied_alpha
);
116 // Sets whether the texture should be blended with the background color
117 // at draw time. Defaults to false.
118 void SetBlendBackgroundColor(bool blend
);
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
);
125 // Code path for plugins which supply their own texture ID.
126 // DEPRECATED. DO NOT USE.
127 void SetTextureId(unsigned texture_id
);
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
);
134 void WillModifyTexture();
136 virtual void SetNeedsDisplayRect(const gfx::RectF
& dirty_rect
) OVERRIDE
;
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
;
146 TextureLayer(TextureLayerClient
* client
, bool uses_mailbox
);
147 virtual ~TextureLayer();
150 void SetTextureMailboxInternal(
151 const TextureMailbox
& mailbox
,
152 scoped_ptr
<SingleReleaseCallback
> release_callback
,
153 bool requires_commit
);
155 TextureLayerClient
* client_
;
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_
;
168 unsigned texture_id_
;
169 scoped_ptr
<TextureMailboxHolder::MainThreadReference
> holder_ref_
;
170 bool needs_set_mailbox_
;
172 DISALLOW_COPY_AND_ASSIGN(TextureLayer
);
176 #endif // CC_LAYERS_TEXTURE_LAYER_H_