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"
16 namespace WebKit
{ class WebGraphicsContext3D
; }
19 class BlockingTaskRunner
;
20 class SingleReleaseCallback
;
21 class TextureLayerClient
;
23 // A Layer containing a the rendered output of a plugin instance.
24 class CC_EXPORT TextureLayer
: public Layer
{
26 class CC_EXPORT MailboxHolder
27 : public base::RefCountedThreadSafe
<MailboxHolder
> {
29 class CC_EXPORT MainThreadReference
{
31 explicit MainThreadReference(MailboxHolder
* holder
);
32 ~MainThreadReference();
33 MailboxHolder
* holder() { return holder_
.get(); }
36 scoped_refptr
<MailboxHolder
> holder_
;
37 DISALLOW_COPY_AND_ASSIGN(MainThreadReference
);
40 const TextureMailbox
& mailbox() const { return mailbox_
; }
41 void Return(unsigned sync_point
, bool is_lost
);
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();
48 friend class TextureLayer
;
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();
57 friend class base::RefCountedThreadSafe
<MailboxHolder
>;
58 friend class MainThreadReference
;
59 explicit MailboxHolder(const TextureMailbox
& mailbox
,
60 scoped_ptr
<SingleReleaseCallback
> release_callback
);
62 void InternalAddRef();
63 void InternalRelease();
64 void ReturnAndReleaseOnImplThread(unsigned sync_point
, bool is_lost
);
66 // This member is thread safe, and is accessed on main and impl threads.
67 const scoped_refptr
<BlockingTaskRunner
> message_loop_
;
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_
;
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_
;
82 DISALLOW_COPY_AND_ASSIGN(MailboxHolder
);
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
);
90 // Used when mailbox names are specified instead of texture IDs.
91 static scoped_refptr
<TextureLayer
> CreateForMailbox(
92 TextureLayerClient
* client
);
96 virtual scoped_ptr
<LayerImpl
> CreateLayerImpl(LayerTreeImpl
* tree_impl
)
99 // Sets whether this texture should be Y-flipped at draw time. Defaults to
101 void SetFlipped(bool flipped
);
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
);
106 // Sets an opacity value per vertex. It will be multiplied by the layer
108 void SetVertexOpacity(float bottom_left
,
113 // Sets whether the alpha channel is premultiplied or unpremultiplied.
115 void SetPremultipliedAlpha(bool premultiplied_alpha
);
117 // Sets whether the texture should be blended with the background color
118 // at draw time. Defaults to false.
119 void SetBlendBackgroundColor(bool blend
);
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
);
126 // Code path for plugins which supply their own texture ID.
127 // DEPRECATED. DO NOT USE.
128 void SetTextureId(unsigned texture_id
);
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
);
135 void WillModifyTexture();
137 virtual void SetNeedsDisplayRect(const gfx::RectF
& dirty_rect
) OVERRIDE
;
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
;
147 TextureLayer(TextureLayerClient
* client
, bool uses_mailbox
);
148 virtual ~TextureLayer();
151 TextureLayerClient
* client_
;
155 gfx::PointF uv_top_left_
;
156 gfx::PointF uv_bottom_right_
;
157 // [bottom left, top left, top right, bottom right]
158 float vertex_opacity_
[4];
159 bool premultiplied_alpha_
;
160 bool blend_background_color_
;
161 bool rate_limit_context_
;
162 bool content_committed_
;
164 unsigned texture_id_
;
165 scoped_ptr
<MailboxHolder::MainThreadReference
> holder_ref_
;
166 bool needs_set_mailbox_
;
168 DISALLOW_COPY_AND_ASSIGN(TextureLayer
);
172 #endif // CC_LAYERS_TEXTURE_LAYER_H_