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 // Used when mailbox names are specified instead of texture IDs.
85 static scoped_refptr
<TextureLayer
> CreateForMailbox(
86 TextureLayerClient
* client
);
88 // Resets the client, which also resets the texture.
91 // Resets the texture.
94 virtual scoped_ptr
<LayerImpl
> CreateLayerImpl(LayerTreeImpl
* tree_impl
)
97 // Sets whether this texture should be Y-flipped at draw time. Defaults to
99 void SetFlipped(bool flipped
);
101 // Sets a UV transform to be used at draw time. Defaults to (0, 0) and (1, 1).
102 void SetUV(const gfx::PointF
& top_left
, const gfx::PointF
& bottom_right
);
104 // Sets an opacity value per vertex. It will be multiplied by the layer
106 void SetVertexOpacity(float bottom_left
,
111 // Sets whether the alpha channel is premultiplied or unpremultiplied.
113 void SetPremultipliedAlpha(bool premultiplied_alpha
);
115 // Sets whether the texture should be blended with the background color
116 // at draw time. Defaults to false.
117 void SetBlendBackgroundColor(bool blend
);
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
);
124 // Code path for plugins which supply their own mailbox.
125 void SetTextureMailbox(const TextureMailbox
& mailbox
,
126 scoped_ptr
<SingleReleaseCallback
> release_callback
);
128 // Use this for special cases where the same texture is used to back the
129 // TextureLayer across all frames.
130 // WARNING: DON'T ACTUALLY USE THIS WHAT YOU ARE DOING IS WRONG.
131 // TODO(danakj): Remove this when pepper doesn't need it. crbug.com/350204
132 void SetTextureMailboxWithoutReleaseCallback(const TextureMailbox
& mailbox
);
134 virtual void SetNeedsDisplayRect(const gfx::RectF
& dirty_rect
) OVERRIDE
;
136 virtual void SetLayerTreeHost(LayerTreeHost
* layer_tree_host
) OVERRIDE
;
137 virtual bool DrawsContent() const OVERRIDE
;
138 virtual bool Update(ResourceUpdateQueue
* queue
,
139 const OcclusionTracker
<Layer
>* occlusion
) OVERRIDE
;
140 virtual void PushPropertiesTo(LayerImpl
* layer
) OVERRIDE
;
141 virtual Region
VisibleContentOpaqueRegion() const OVERRIDE
;
144 explicit TextureLayer(TextureLayerClient
* client
);
145 virtual ~TextureLayer();
148 void SetTextureMailboxInternal(
149 const TextureMailbox
& mailbox
,
150 scoped_ptr
<SingleReleaseCallback
> release_callback
,
151 bool requires_commit
,
152 bool allow_mailbox_reuse
);
154 TextureLayerClient
* client_
;
157 gfx::PointF uv_top_left_
;
158 gfx::PointF uv_bottom_right_
;
159 // [bottom left, top left, top right, bottom right]
160 float vertex_opacity_
[4];
161 bool premultiplied_alpha_
;
162 bool blend_background_color_
;
163 bool rate_limit_context_
;
165 scoped_ptr
<TextureMailboxHolder::MainThreadReference
> holder_ref_
;
166 bool needs_set_mailbox_
;
168 DISALLOW_COPY_AND_ASSIGN(TextureLayer
);
172 #endif // CC_LAYERS_TEXTURE_LAYER_H_