OLD | NEW |
1 // Copyright 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/layers/texture_layer.h" | 5 #include "cc/layers/texture_layer.h" |
6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/location.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" |
7 #include "cc/base/thread.h" | 10 #include "cc/base/thread.h" |
8 #include "cc/layers/texture_layer_client.h" | 11 #include "cc/layers/texture_layer_client.h" |
9 #include "cc/layers/texture_layer_impl.h" | 12 #include "cc/layers/texture_layer_impl.h" |
10 #include "cc/trees/layer_tree_host.h" | 13 #include "cc/trees/layer_tree_host.h" |
11 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h" | 14 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h" |
12 | 15 |
13 namespace cc { | 16 namespace cc { |
14 | 17 |
15 namespace { | |
16 | |
17 void RunCallback( | |
18 const TextureMailbox::ReleaseCallback& callback, | |
19 unsigned sync_point, | |
20 bool lost_resource) { | |
21 callback.Run(sync_point, lost_resource); | |
22 } | |
23 | |
24 void PostCallbackToThread( | |
25 Thread* thread, | |
26 const TextureMailbox::ReleaseCallback& callback, | |
27 unsigned sync_point, | |
28 bool lost_resource) { | |
29 if (!callback.is_null()) { | |
30 thread->PostTask(base::Bind(&RunCallback, callback, | |
31 sync_point, lost_resource)); | |
32 } | |
33 } | |
34 | |
35 } // namespace | |
36 | |
37 scoped_refptr<TextureLayer> TextureLayer::Create(TextureLayerClient* client) { | 18 scoped_refptr<TextureLayer> TextureLayer::Create(TextureLayerClient* client) { |
38 return scoped_refptr<TextureLayer>(new TextureLayer(client, false)); | 19 return scoped_refptr<TextureLayer>(new TextureLayer(client, false)); |
39 } | 20 } |
40 | 21 |
41 scoped_refptr<TextureLayer> TextureLayer::CreateForMailbox( | 22 scoped_refptr<TextureLayer> TextureLayer::CreateForMailbox( |
42 TextureLayerClient* client) { | 23 TextureLayerClient* client) { |
43 return scoped_refptr<TextureLayer>(new TextureLayer(client, true)); | 24 return scoped_refptr<TextureLayer>(new TextureLayer(client, true)); |
44 } | 25 } |
45 | 26 |
46 TextureLayer::TextureLayer(TextureLayerClient* client, bool uses_mailbox) | 27 TextureLayer::TextureLayer(TextureLayerClient* client, bool uses_mailbox) |
47 : Layer(), | 28 : Layer(), |
48 client_(client), | 29 client_(client), |
49 uses_mailbox_(uses_mailbox), | 30 uses_mailbox_(uses_mailbox), |
50 flipped_(true), | 31 flipped_(true), |
51 uv_top_left_(0.f, 0.f), | 32 uv_top_left_(0.f, 0.f), |
52 uv_bottom_right_(1.f, 1.f), | 33 uv_bottom_right_(1.f, 1.f), |
53 premultiplied_alpha_(true), | 34 premultiplied_alpha_(true), |
54 rate_limit_context_(false), | 35 rate_limit_context_(false), |
55 context_lost_(false), | 36 context_lost_(false), |
56 content_committed_(false), | 37 content_committed_(false), |
57 texture_id_(0), | 38 texture_id_(0), |
58 own_mailbox_(false) { | 39 needs_set_mailbox_(false) { |
59 vertex_opacity_[0] = 1.0f; | 40 vertex_opacity_[0] = 1.0f; |
60 vertex_opacity_[1] = 1.0f; | 41 vertex_opacity_[1] = 1.0f; |
61 vertex_opacity_[2] = 1.0f; | 42 vertex_opacity_[2] = 1.0f; |
62 vertex_opacity_[3] = 1.0f; | 43 vertex_opacity_[3] = 1.0f; |
63 } | 44 } |
64 | 45 |
65 TextureLayer::~TextureLayer() { | 46 TextureLayer::~TextureLayer() { |
66 if (layer_tree_host()) { | 47 if (layer_tree_host()) { |
67 if (texture_id_) | 48 if (texture_id_) |
68 layer_tree_host()->AcquireLayerTextures(); | 49 layer_tree_host()->AcquireLayerTextures(); |
69 if (rate_limit_context_ && client_) | 50 if (rate_limit_context_ && client_) |
70 layer_tree_host()->StopRateLimiter(client_->Context3d()); | 51 layer_tree_host()->StopRateLimiter(client_->Context3d()); |
71 } | 52 } |
72 if (own_mailbox_) | |
73 texture_mailbox_.RunReleaseCallback(texture_mailbox_.sync_point(), false); | |
74 } | 53 } |
75 | 54 |
76 void TextureLayer::ClearClient() { | 55 void TextureLayer::ClearClient() { |
77 client_ = NULL; | 56 client_ = NULL; |
78 if (uses_mailbox_) | 57 if (uses_mailbox_) |
79 SetTextureMailbox(TextureMailbox()); | 58 SetTextureMailbox(TextureMailbox()); |
80 else | 59 else |
81 SetTextureId(0); | 60 SetTextureId(0); |
82 } | 61 } |
83 | 62 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 if (texture_id_ == id) | 119 if (texture_id_ == id) |
141 return; | 120 return; |
142 if (texture_id_ && layer_tree_host()) | 121 if (texture_id_ && layer_tree_host()) |
143 layer_tree_host()->AcquireLayerTextures(); | 122 layer_tree_host()->AcquireLayerTextures(); |
144 texture_id_ = id; | 123 texture_id_ = id; |
145 SetNeedsCommit(); | 124 SetNeedsCommit(); |
146 } | 125 } |
147 | 126 |
148 void TextureLayer::SetTextureMailbox(const TextureMailbox& mailbox) { | 127 void TextureLayer::SetTextureMailbox(const TextureMailbox& mailbox) { |
149 DCHECK(uses_mailbox_); | 128 DCHECK(uses_mailbox_); |
150 if (own_mailbox_) | 129 DCHECK(!mailbox.IsValid() || !holder_ref_ || |
151 DCHECK(!mailbox.IsValid() || !mailbox.Equals(texture_mailbox_)); | 130 !mailbox.Equals(holder_ref_->holder()->mailbox())); |
152 // If we never commited the mailbox, we need to release it here | 131 // If we never commited the mailbox, we need to release it here. |
153 if (own_mailbox_) | 132 if (mailbox.IsValid()) |
154 texture_mailbox_.RunReleaseCallback(texture_mailbox_.sync_point(), false); | 133 holder_ref_ = MailboxHolder::Create(mailbox); |
155 texture_mailbox_ = mailbox; | 134 else |
156 own_mailbox_ = true; | 135 holder_ref_.reset(); |
| 136 needs_set_mailbox_ = true; |
157 SetNeedsCommit(); | 137 SetNeedsCommit(); |
158 } | 138 } |
159 | 139 |
160 void TextureLayer::WillModifyTexture() { | 140 void TextureLayer::WillModifyTexture() { |
161 if (layer_tree_host() && (DrawsContent() || content_committed_)) { | 141 if (layer_tree_host() && (DrawsContent() || content_committed_)) { |
162 layer_tree_host()->AcquireLayerTextures(); | 142 layer_tree_host()->AcquireLayerTextures(); |
163 content_committed_ = false; | 143 content_committed_ = false; |
164 } | 144 } |
165 } | 145 } |
166 | 146 |
167 void TextureLayer::SetNeedsDisplayRect(const gfx::RectF& dirty_rect) { | 147 void TextureLayer::SetNeedsDisplayRect(const gfx::RectF& dirty_rect) { |
168 Layer::SetNeedsDisplayRect(dirty_rect); | 148 Layer::SetNeedsDisplayRect(dirty_rect); |
169 | 149 |
170 if (rate_limit_context_ && client_ && layer_tree_host() && DrawsContent()) | 150 if (rate_limit_context_ && client_ && layer_tree_host() && DrawsContent()) |
171 layer_tree_host()->StartRateLimiter(client_->Context3d()); | 151 layer_tree_host()->StartRateLimiter(client_->Context3d()); |
172 } | 152 } |
173 | 153 |
174 void TextureLayer::SetLayerTreeHost(LayerTreeHost* host) { | 154 void TextureLayer::SetLayerTreeHost(LayerTreeHost* host) { |
175 if (texture_id_ && layer_tree_host() && host != layer_tree_host()) | 155 if (texture_id_ && layer_tree_host() && host != layer_tree_host()) |
176 layer_tree_host()->AcquireLayerTextures(); | 156 layer_tree_host()->AcquireLayerTextures(); |
| 157 // If we're removed from the tree, the TextureLayerImpl will be destroyed, and |
| 158 // we will need to set the mailbox again on a new TextureLayerImpl the next |
| 159 // time we push. |
| 160 if (!host && uses_mailbox_ && holder_ref_) |
| 161 needs_set_mailbox_ = true; |
177 Layer::SetLayerTreeHost(host); | 162 Layer::SetLayerTreeHost(host); |
178 } | 163 } |
179 | 164 |
180 bool TextureLayer::DrawsContent() const { | 165 bool TextureLayer::DrawsContent() const { |
181 return (client_ || texture_id_ || texture_mailbox_.IsValid()) && | 166 return (client_ || texture_id_ || holder_ref_) && |
182 !context_lost_ && Layer::DrawsContent(); | 167 !context_lost_ && Layer::DrawsContent(); |
183 } | 168 } |
184 | 169 |
185 void TextureLayer::Update(ResourceUpdateQueue* queue, | 170 void TextureLayer::Update(ResourceUpdateQueue* queue, |
186 const OcclusionTracker* occlusion, | 171 const OcclusionTracker* occlusion, |
187 RenderingStats* stats) { | 172 RenderingStats* stats) { |
188 if (client_) { | 173 if (client_) { |
189 if (uses_mailbox_) { | 174 if (uses_mailbox_) { |
190 TextureMailbox mailbox; | 175 TextureMailbox mailbox; |
191 if (client_->PrepareTextureMailbox(&mailbox)) { | 176 if (client_->PrepareTextureMailbox(&mailbox)) { |
(...skipping 14 matching lines...) Expand all Loading... |
206 | 191 |
207 void TextureLayer::PushPropertiesTo(LayerImpl* layer) { | 192 void TextureLayer::PushPropertiesTo(LayerImpl* layer) { |
208 Layer::PushPropertiesTo(layer); | 193 Layer::PushPropertiesTo(layer); |
209 | 194 |
210 TextureLayerImpl* texture_layer = static_cast<TextureLayerImpl*>(layer); | 195 TextureLayerImpl* texture_layer = static_cast<TextureLayerImpl*>(layer); |
211 texture_layer->set_flipped(flipped_); | 196 texture_layer->set_flipped(flipped_); |
212 texture_layer->set_uv_top_left(uv_top_left_); | 197 texture_layer->set_uv_top_left(uv_top_left_); |
213 texture_layer->set_uv_bottom_right(uv_bottom_right_); | 198 texture_layer->set_uv_bottom_right(uv_bottom_right_); |
214 texture_layer->set_vertex_opacity(vertex_opacity_); | 199 texture_layer->set_vertex_opacity(vertex_opacity_); |
215 texture_layer->set_premultiplied_alpha(premultiplied_alpha_); | 200 texture_layer->set_premultiplied_alpha(premultiplied_alpha_); |
216 if (uses_mailbox_ && own_mailbox_) { | 201 if (uses_mailbox_ && needs_set_mailbox_) { |
217 Thread* main_thread = layer_tree_host()->proxy()->MainThread(); | 202 TextureMailbox texture_mailbox; |
218 TextureMailbox::ReleaseCallback callback = base::Bind( | 203 if (holder_ref_) { |
219 &PostCallbackToThread, main_thread, texture_mailbox_.callback()); | 204 MailboxHolder* holder = holder_ref_->holder(); |
220 texture_layer->SetTextureMailbox( | 205 TextureMailbox::ReleaseCallback callback = |
221 texture_mailbox_.CopyWithNewCallback(callback)); | 206 holder->GetCallbackForImplThread(); |
222 own_mailbox_ = false; | 207 texture_mailbox = holder->mailbox().CopyWithNewCallback(callback); |
| 208 } |
| 209 texture_layer->SetTextureMailbox(texture_mailbox); |
| 210 needs_set_mailbox_ = false; |
223 } else { | 211 } else { |
224 texture_layer->set_texture_id(texture_id_); | 212 texture_layer->set_texture_id(texture_id_); |
225 } | 213 } |
226 content_committed_ = DrawsContent(); | 214 content_committed_ = DrawsContent(); |
227 } | 215 } |
228 | 216 |
229 bool TextureLayer::BlocksPendingCommit() const { | 217 bool TextureLayer::BlocksPendingCommit() const { |
230 // Double-buffered texture layers need to be blocked until they can be made | 218 // Double-buffered texture layers need to be blocked until they can be made |
231 // triple-buffered. Single-buffered layers already prevent draws, so | 219 // triple-buffered. Single-buffered layers already prevent draws, so |
232 // can block too for simplicity. | 220 // can block too for simplicity. |
233 return DrawsContent(); | 221 return DrawsContent(); |
234 } | 222 } |
235 | 223 |
236 bool TextureLayer::CanClipSelf() const { | 224 bool TextureLayer::CanClipSelf() const { |
237 return true; | 225 return true; |
238 } | 226 } |
239 | 227 |
| 228 TextureLayer::MailboxHolder::MainThreadReference::MainThreadReference( |
| 229 MailboxHolder* holder) |
| 230 : holder_(holder) { |
| 231 holder_->InternalAddRef(); |
| 232 } |
| 233 |
| 234 TextureLayer::MailboxHolder::MainThreadReference::~MainThreadReference() { |
| 235 holder_->InternalRelease(); |
| 236 } |
| 237 |
| 238 TextureLayer::MailboxHolder::MailboxHolder(const TextureMailbox& mailbox) |
| 239 : message_loop_(base::MessageLoopProxy::current()), |
| 240 internal_references_(0), |
| 241 mailbox_(mailbox), |
| 242 sync_point_(mailbox.sync_point()), |
| 243 is_lost_(false) { |
| 244 } |
| 245 |
| 246 TextureLayer::MailboxHolder::~MailboxHolder() { |
| 247 DCHECK_EQ(0u, internal_references_); |
| 248 } |
| 249 |
| 250 scoped_ptr<TextureLayer::MailboxHolder::MainThreadReference> |
| 251 TextureLayer::MailboxHolder::Create(const TextureMailbox& mailbox) { |
| 252 return scoped_ptr<MainThreadReference>(new MainThreadReference( |
| 253 new MailboxHolder(mailbox))); |
| 254 } |
| 255 |
| 256 void TextureLayer::MailboxHolder::Return(unsigned sync_point, bool is_lost) { |
| 257 sync_point_ = sync_point; |
| 258 is_lost_ = is_lost; |
| 259 } |
| 260 |
| 261 TextureMailbox::ReleaseCallback |
| 262 TextureLayer::MailboxHolder::GetCallbackForImplThread() { |
| 263 // We can't call GetCallbackForImplThread if we released the main thread |
| 264 // reference. |
| 265 DCHECK_GT(internal_references_, 0u); |
| 266 InternalAddRef(); |
| 267 return base::Bind(&MailboxHolder::ReturnAndReleaseOnImplThread, this); |
| 268 } |
| 269 |
| 270 void TextureLayer::MailboxHolder::InternalAddRef() { |
| 271 ++internal_references_; |
| 272 } |
| 273 |
| 274 void TextureLayer::MailboxHolder::InternalRelease() { |
| 275 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 276 if (!--internal_references_) { |
| 277 mailbox_.RunReleaseCallback(sync_point_, is_lost_); |
| 278 mailbox_ = TextureMailbox(); |
| 279 } |
| 280 } |
| 281 |
| 282 void TextureLayer::MailboxHolder::ReturnAndReleaseOnMainThread( |
| 283 unsigned sync_point, bool is_lost) { |
| 284 DCHECK(message_loop_->BelongsToCurrentThread()); |
| 285 Return(sync_point, is_lost); |
| 286 InternalRelease(); |
| 287 } |
| 288 |
| 289 void TextureLayer::MailboxHolder::ReturnAndReleaseOnImplThread( |
| 290 unsigned sync_point, bool is_lost) { |
| 291 message_loop_->PostTask(FROM_HERE, base::Bind( |
| 292 &MailboxHolder::ReturnAndReleaseOnMainThread, |
| 293 this, sync_point, is_lost)); |
| 294 } |
| 295 |
240 } // namespace cc | 296 } // namespace cc |
OLD | NEW |