Chromium Code Reviews| 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_ || !mailbox.Equals(holder_->mailbox())); |
| 151 DCHECK(!mailbox.IsValid() || !mailbox.Equals(texture_mailbox_)); | 130 // If we never commited the mailbox, we need to release it here. |
| 152 // If we never commited the mailbox, we need to release it here | 131 holder_ = mailbox.IsValid() ? new MailboxHolder(mailbox) : NULL; |
| 153 if (own_mailbox_) | 132 needs_set_mailbox_ = true; |
| 154 texture_mailbox_.RunReleaseCallback(texture_mailbox_.sync_point(), false); | |
| 155 texture_mailbox_ = mailbox; | |
| 156 own_mailbox_ = true; | |
| 157 SetNeedsCommit(); | 133 SetNeedsCommit(); |
| 158 } | 134 } |
| 159 | 135 |
| 160 void TextureLayer::WillModifyTexture() { | 136 void TextureLayer::WillModifyTexture() { |
| 161 if (layer_tree_host() && (DrawsContent() || content_committed_)) { | 137 if (layer_tree_host() && (DrawsContent() || content_committed_)) { |
| 162 layer_tree_host()->AcquireLayerTextures(); | 138 layer_tree_host()->AcquireLayerTextures(); |
| 163 content_committed_ = false; | 139 content_committed_ = false; |
| 164 } | 140 } |
| 165 } | 141 } |
| 166 | 142 |
| 167 void TextureLayer::SetNeedsDisplayRect(const gfx::RectF& dirty_rect) { | 143 void TextureLayer::SetNeedsDisplayRect(const gfx::RectF& dirty_rect) { |
| 168 Layer::SetNeedsDisplayRect(dirty_rect); | 144 Layer::SetNeedsDisplayRect(dirty_rect); |
| 169 | 145 |
| 170 if (rate_limit_context_ && client_ && layer_tree_host() && DrawsContent()) | 146 if (rate_limit_context_ && client_ && layer_tree_host() && DrawsContent()) |
| 171 layer_tree_host()->StartRateLimiter(client_->Context3d()); | 147 layer_tree_host()->StartRateLimiter(client_->Context3d()); |
| 172 } | 148 } |
| 173 | 149 |
| 174 void TextureLayer::SetLayerTreeHost(LayerTreeHost* host) { | 150 void TextureLayer::SetLayerTreeHost(LayerTreeHost* host) { |
| 175 if (texture_id_ && layer_tree_host() && host != layer_tree_host()) | 151 if (texture_id_ && layer_tree_host() && host != layer_tree_host()) |
| 176 layer_tree_host()->AcquireLayerTextures(); | 152 layer_tree_host()->AcquireLayerTextures(); |
| 153 // If we're removed from the tree, the TextureLayerImpl will be destroyed, and | |
| 154 // we will need to set the mailbox again on a new TextureLayerImpl the next | |
| 155 // time we push. | |
| 156 if (!host && uses_mailbox_ && holder_) | |
| 157 needs_set_mailbox_ = true; | |
| 177 Layer::SetLayerTreeHost(host); | 158 Layer::SetLayerTreeHost(host); |
| 178 } | 159 } |
| 179 | 160 |
| 180 bool TextureLayer::DrawsContent() const { | 161 bool TextureLayer::DrawsContent() const { |
| 181 return (client_ || texture_id_ || texture_mailbox_.IsValid()) && | 162 return (client_ || texture_id_ || holder_.get()) && |
| 182 !context_lost_ && Layer::DrawsContent(); | 163 !context_lost_ && Layer::DrawsContent(); |
| 183 } | 164 } |
| 184 | 165 |
| 185 void TextureLayer::Update(ResourceUpdateQueue* queue, | 166 void TextureLayer::Update(ResourceUpdateQueue* queue, |
| 186 const OcclusionTracker* occlusion, | 167 const OcclusionTracker* occlusion, |
| 187 RenderingStats* stats) { | 168 RenderingStats* stats) { |
| 188 if (client_) { | 169 if (client_) { |
| 189 if (uses_mailbox_) { | 170 if (uses_mailbox_) { |
| 190 TextureMailbox mailbox; | 171 TextureMailbox mailbox; |
| 191 if (client_->PrepareTextureMailbox(&mailbox)) { | 172 if (client_->PrepareTextureMailbox(&mailbox)) { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 206 | 187 |
| 207 void TextureLayer::PushPropertiesTo(LayerImpl* layer) { | 188 void TextureLayer::PushPropertiesTo(LayerImpl* layer) { |
| 208 Layer::PushPropertiesTo(layer); | 189 Layer::PushPropertiesTo(layer); |
| 209 | 190 |
| 210 TextureLayerImpl* texture_layer = static_cast<TextureLayerImpl*>(layer); | 191 TextureLayerImpl* texture_layer = static_cast<TextureLayerImpl*>(layer); |
| 211 texture_layer->set_flipped(flipped_); | 192 texture_layer->set_flipped(flipped_); |
| 212 texture_layer->set_uv_top_left(uv_top_left_); | 193 texture_layer->set_uv_top_left(uv_top_left_); |
| 213 texture_layer->set_uv_bottom_right(uv_bottom_right_); | 194 texture_layer->set_uv_bottom_right(uv_bottom_right_); |
| 214 texture_layer->set_vertex_opacity(vertex_opacity_); | 195 texture_layer->set_vertex_opacity(vertex_opacity_); |
| 215 texture_layer->set_premultiplied_alpha(premultiplied_alpha_); | 196 texture_layer->set_premultiplied_alpha(premultiplied_alpha_); |
| 216 if (uses_mailbox_ && own_mailbox_) { | 197 if (uses_mailbox_ && needs_set_mailbox_) { |
| 217 Thread* main_thread = layer_tree_host()->proxy()->MainThread(); | 198 TextureMailbox texture_mailbox; |
| 218 TextureMailbox::ReleaseCallback callback = base::Bind( | 199 if (holder_) { |
| 219 &PostCallbackToThread, main_thread, texture_mailbox_.callback()); | 200 TextureMailbox::ReleaseCallback callback = |
| 220 texture_layer->SetTextureMailbox( | 201 holder_->GetCallbackForImplThread(); |
| 221 texture_mailbox_.CopyWithNewCallback(callback)); | 202 texture_mailbox = holder_->mailbox().CopyWithNewCallback(callback); |
| 222 own_mailbox_ = false; | 203 } |
| 204 texture_layer->SetTextureMailbox(texture_mailbox); | |
| 205 needs_set_mailbox_ = false; | |
| 223 } else { | 206 } else { |
| 224 texture_layer->set_texture_id(texture_id_); | 207 texture_layer->set_texture_id(texture_id_); |
| 225 } | 208 } |
| 226 content_committed_ = DrawsContent(); | 209 content_committed_ = DrawsContent(); |
| 227 } | 210 } |
| 228 | 211 |
| 229 bool TextureLayer::BlocksPendingCommit() const { | 212 bool TextureLayer::BlocksPendingCommit() const { |
| 230 // Double-buffered texture layers need to be blocked until they can be made | 213 // Double-buffered texture layers need to be blocked until they can be made |
| 231 // triple-buffered. Single-buffered layers already prevent draws, so | 214 // triple-buffered. Single-buffered layers already prevent draws, so |
| 232 // can block too for simplicity. | 215 // can block too for simplicity. |
| 233 return DrawsContent(); | 216 return DrawsContent(); |
| 234 } | 217 } |
| 235 | 218 |
| 236 bool TextureLayer::CanClipSelf() const { | 219 bool TextureLayer::CanClipSelf() const { |
| 237 return true; | 220 return true; |
| 238 } | 221 } |
| 239 | 222 |
| 223 TextureLayer::MailboxHolder::MailboxHolder(const TextureMailbox& mailbox) | |
| 224 : message_loop_(base::MessageLoopProxy::current()), | |
| 225 mailbox_(mailbox), | |
| 226 sync_point_(mailbox.sync_point()), | |
| 227 is_lost_(false) { | |
| 228 } | |
| 229 | |
| 230 TextureLayer::MailboxHolder::~MailboxHolder() { | |
| 231 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 232 if (mailbox_.callback().is_null()) | |
| 233 return; | |
| 234 mailbox_.RunReleaseCallback(sync_point_, is_lost_); | |
| 235 } | |
| 236 | |
| 237 void TextureLayer::MailboxHolder::Return(unsigned sync_point, bool is_lost) { | |
| 238 sync_point_ = sync_point; | |
| 239 is_lost_ = is_lost; | |
| 240 } | |
| 241 | |
| 242 TextureMailbox::ReleaseCallback | |
| 243 TextureLayer::MailboxHolder::GetCallbackForImplThread() { | |
| 244 // Note: we refcount manually here and pass Unretained, because otherwise the | |
| 245 // callback would keep a reference on the MailboxHolder, which may be released | |
| 246 // when the callback is destroyed on the impl thread. | |
| 247 AddRef(); | |
| 248 return base::Bind(&MailboxHolder::ReturnAndReleaseOnImplThread, | |
| 249 Unretained(this)); | |
| 250 } | |
| 251 | |
| 252 void TextureLayer::MailboxHolder::ReturnAndReleaseOnMainThread( | |
| 253 unsigned sync_point, bool is_lost) { | |
| 254 DCHECK(message_loop_->BelongsToCurrentThread()); | |
| 255 Return(sync_point, is_lost); | |
| 256 Release(); | |
| 257 } | |
| 258 | |
| 259 void TextureLayer::MailboxHolder::ReturnAndReleaseOnImplThread( | |
| 260 unsigned sync_point, bool is_lost) { | |
| 261 message_loop_->PostTask(FROM_HERE, base::Bind( | |
|
danakj
2013/06/18 00:53:24
I guess the downside here is we can't DCHECK that
piman
2013/06/18 02:10:58
Following our discussion, see the new version. Not
| |
| 262 &MailboxHolder::ReturnAndReleaseOnMainThread, | |
| 263 Unretained(this), sync_point, is_lost)); | |
| 264 } | |
| 265 | |
| 240 } // namespace cc | 266 } // namespace cc |
| OLD | NEW |