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 #include "cc/layers/texture_layer.h"
8 #include "base/callback_helpers.h"
9 #include "base/location.h"
10 #include "base/synchronization/lock.h"
11 #include "cc/layers/texture_layer_client.h"
12 #include "cc/layers/texture_layer_impl.h"
13 #include "cc/resources/single_release_callback.h"
14 #include "cc/trees/blocking_task_runner.h"
15 #include "cc/trees/layer_tree_host.h"
19 scoped_refptr<TextureLayer> TextureLayer::CreateForMailbox(
20 TextureLayerClient* client) {
21 return scoped_refptr<TextureLayer>(new TextureLayer(client));
24 TextureLayer::TextureLayer(TextureLayerClient* client)
28 uv_top_left_(0.f, 0.f),
29 uv_bottom_right_(1.f, 1.f),
30 premultiplied_alpha_(true),
31 blend_background_color_(false),
32 rate_limit_context_(false),
33 needs_set_mailbox_(false) {
34 vertex_opacity_[0] = 1.0f;
35 vertex_opacity_[1] = 1.0f;
36 vertex_opacity_[2] = 1.0f;
37 vertex_opacity_[3] = 1.0f;
40 TextureLayer::~TextureLayer() {
43 void TextureLayer::ClearClient() {
44 if (rate_limit_context_ && client_ && layer_tree_host())
45 layer_tree_host()->StopRateLimiter();
50 void TextureLayer::ClearTexture() {
51 SetTextureMailbox(TextureMailbox(), scoped_ptr<SingleReleaseCallback>());
54 scoped_ptr<LayerImpl> TextureLayer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
55 return TextureLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
58 void TextureLayer::SetFlipped(bool flipped) {
59 if (flipped_ == flipped)
65 void TextureLayer::SetUV(const gfx::PointF& top_left,
66 const gfx::PointF& bottom_right) {
67 if (uv_top_left_ == top_left && uv_bottom_right_ == bottom_right)
69 uv_top_left_ = top_left;
70 uv_bottom_right_ = bottom_right;
74 void TextureLayer::SetVertexOpacity(float bottom_left,
78 // Indexing according to the quad vertex generation:
82 if (vertex_opacity_[0] == bottom_left &&
83 vertex_opacity_[1] == top_left &&
84 vertex_opacity_[2] == top_right &&
85 vertex_opacity_[3] == bottom_right)
87 vertex_opacity_[0] = bottom_left;
88 vertex_opacity_[1] = top_left;
89 vertex_opacity_[2] = top_right;
90 vertex_opacity_[3] = bottom_right;
94 void TextureLayer::SetPremultipliedAlpha(bool premultiplied_alpha) {
95 if (premultiplied_alpha_ == premultiplied_alpha)
97 premultiplied_alpha_ = premultiplied_alpha;
101 void TextureLayer::SetBlendBackgroundColor(bool blend) {
102 if (blend_background_color_ == blend)
104 blend_background_color_ = blend;
108 void TextureLayer::SetRateLimitContext(bool rate_limit) {
109 if (!rate_limit && rate_limit_context_ && client_ && layer_tree_host())
110 layer_tree_host()->StopRateLimiter();
112 rate_limit_context_ = rate_limit;
115 void TextureLayer::SetTextureMailboxInternal(
116 const TextureMailbox& mailbox,
117 scoped_ptr<SingleReleaseCallback> release_callback,
118 bool requires_commit,
119 bool allow_mailbox_reuse) {
120 DCHECK(!mailbox.IsValid() || !holder_ref_ ||
121 !mailbox.Equals(holder_ref_->holder()->mailbox()) ||
122 allow_mailbox_reuse);
123 DCHECK_EQ(mailbox.IsValid(), !!release_callback);
125 // If we never commited the mailbox, we need to release it here.
126 if (mailbox.IsValid()) {
128 TextureMailboxHolder::Create(mailbox, release_callback.Pass());
132 needs_set_mailbox_ = true;
133 // If we are within a commit, no need to do it again immediately after.
137 SetNeedsPushProperties();
139 // The active frame needs to be replaced and the mailbox returned before the
140 // commit is called complete.
141 SetNextCommitWaitsForActivation();
144 void TextureLayer::SetTextureMailbox(
145 const TextureMailbox& mailbox,
146 scoped_ptr<SingleReleaseCallback> release_callback) {
147 bool requires_commit = true;
148 bool allow_mailbox_reuse = false;
149 SetTextureMailboxInternal(
150 mailbox, release_callback.Pass(), requires_commit, allow_mailbox_reuse);
153 static void IgnoreReleaseCallback(uint32 sync_point, bool lost) {}
155 void TextureLayer::SetTextureMailboxWithoutReleaseCallback(
156 const TextureMailbox& mailbox) {
157 // We allow reuse of the mailbox if there is a new sync point signalling new
158 // content, and the release callback goes nowhere since we'll be calling it
159 // multiple times for the same mailbox.
160 DCHECK(!mailbox.IsValid() || !holder_ref_ ||
161 !mailbox.Equals(holder_ref_->holder()->mailbox()) ||
162 mailbox.sync_point() != holder_ref_->holder()->mailbox().sync_point());
163 scoped_ptr<SingleReleaseCallback> release;
164 bool requires_commit = true;
165 bool allow_mailbox_reuse = true;
166 if (mailbox.IsValid())
167 release = SingleReleaseCallback::Create(base::Bind(&IgnoreReleaseCallback));
168 SetTextureMailboxInternal(
169 mailbox, release.Pass(), requires_commit, allow_mailbox_reuse);
172 void TextureLayer::SetNeedsDisplayRect(const gfx::RectF& dirty_rect) {
173 Layer::SetNeedsDisplayRect(dirty_rect);
175 if (rate_limit_context_ && client_ && layer_tree_host() && DrawsContent())
176 layer_tree_host()->StartRateLimiter();
179 void TextureLayer::SetLayerTreeHost(LayerTreeHost* host) {
180 if (layer_tree_host() == host) {
181 Layer::SetLayerTreeHost(host);
185 if (layer_tree_host()) {
186 if (rate_limit_context_ && client_)
187 layer_tree_host()->StopRateLimiter();
189 // If we're removed from the tree, the TextureLayerImpl will be destroyed, and
190 // we will need to set the mailbox again on a new TextureLayerImpl the next
192 if (!host && holder_ref_) {
193 needs_set_mailbox_ = true;
194 // The active frame needs to be replaced and the mailbox returned before the
195 // commit is called complete.
196 SetNextCommitWaitsForActivation();
198 Layer::SetLayerTreeHost(host);
201 bool TextureLayer::DrawsContent() const {
202 return (client_ || holder_ref_) && Layer::DrawsContent();
205 bool TextureLayer::Update(ResourceUpdateQueue* queue,
206 const OcclusionTracker<Layer>* occlusion) {
207 bool updated = Layer::Update(queue, occlusion);
209 TextureMailbox mailbox;
210 scoped_ptr<SingleReleaseCallback> release_callback;
211 if (client_->PrepareTextureMailbox(
214 layer_tree_host()->UsingSharedMemoryResources())) {
215 // Already within a commit, no need to do another one immediately.
216 bool requires_commit = false;
217 bool allow_mailbox_reuse = false;
218 SetTextureMailboxInternal(mailbox,
219 release_callback.Pass(),
221 allow_mailbox_reuse);
226 // SetTextureMailbox could be called externally and the same mailbox used for
227 // different textures. Such callers notify this layer that the texture has
228 // changed by calling SetNeedsDisplay, so check for that here.
229 return updated || !update_rect_.IsEmpty();
232 void TextureLayer::PushPropertiesTo(LayerImpl* layer) {
233 Layer::PushPropertiesTo(layer);
235 TextureLayerImpl* texture_layer = static_cast<TextureLayerImpl*>(layer);
236 texture_layer->SetFlipped(flipped_);
237 texture_layer->SetUVTopLeft(uv_top_left_);
238 texture_layer->SetUVBottomRight(uv_bottom_right_);
239 texture_layer->SetVertexOpacity(vertex_opacity_);
240 texture_layer->SetPremultipliedAlpha(premultiplied_alpha_);
241 texture_layer->SetBlendBackgroundColor(blend_background_color_);
242 if (needs_set_mailbox_) {
243 TextureMailbox texture_mailbox;
244 scoped_ptr<SingleReleaseCallback> release_callback;
246 TextureMailboxHolder* holder = holder_ref_->holder();
247 texture_mailbox = holder->mailbox();
248 release_callback = holder->GetCallbackForImplThread();
250 texture_layer->SetTextureMailbox(texture_mailbox, release_callback.Pass());
251 needs_set_mailbox_ = false;
255 Region TextureLayer::VisibleContentOpaqueRegion() const {
256 if (contents_opaque())
257 return visible_content_rect();
259 if (blend_background_color_ && (SkColorGetA(background_color()) == 0xFF))
260 return visible_content_rect();
265 TextureLayer::TextureMailboxHolder::MainThreadReference::MainThreadReference(
266 TextureMailboxHolder* holder)
268 holder_->InternalAddRef();
271 TextureLayer::TextureMailboxHolder::MainThreadReference::
272 ~MainThreadReference() {
273 holder_->InternalRelease();
276 TextureLayer::TextureMailboxHolder::TextureMailboxHolder(
277 const TextureMailbox& mailbox,
278 scoped_ptr<SingleReleaseCallback> release_callback)
279 : message_loop_(BlockingTaskRunner::current()),
280 internal_references_(0),
282 release_callback_(release_callback.Pass()),
283 sync_point_(mailbox.sync_point()),
286 TextureLayer::TextureMailboxHolder::~TextureMailboxHolder() {
287 DCHECK_EQ(0u, internal_references_);
290 scoped_ptr<TextureLayer::TextureMailboxHolder::MainThreadReference>
291 TextureLayer::TextureMailboxHolder::Create(
292 const TextureMailbox& mailbox,
293 scoped_ptr<SingleReleaseCallback> release_callback) {
294 return scoped_ptr<MainThreadReference>(new MainThreadReference(
295 new TextureMailboxHolder(mailbox, release_callback.Pass())));
298 void TextureLayer::TextureMailboxHolder::Return(uint32 sync_point,
300 base::AutoLock lock(arguments_lock_);
301 sync_point_ = sync_point;
305 scoped_ptr<SingleReleaseCallback>
306 TextureLayer::TextureMailboxHolder::GetCallbackForImplThread() {
307 // We can't call GetCallbackForImplThread if we released the main thread
309 DCHECK_GT(internal_references_, 0u);
311 return SingleReleaseCallback::Create(
312 base::Bind(&TextureMailboxHolder::ReturnAndReleaseOnImplThread, this));
315 void TextureLayer::TextureMailboxHolder::InternalAddRef() {
316 ++internal_references_;
319 void TextureLayer::TextureMailboxHolder::InternalRelease() {
320 DCHECK(message_loop_->BelongsToCurrentThread());
321 if (!--internal_references_) {
322 release_callback_->Run(sync_point_, is_lost_);
323 mailbox_ = TextureMailbox();
324 release_callback_.reset();
328 void TextureLayer::TextureMailboxHolder::ReturnAndReleaseOnImplThread(
331 Return(sync_point, is_lost);
332 message_loop_->PostTask(
333 FROM_HERE, base::Bind(&TextureMailboxHolder::InternalRelease, this));