OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/resources/texture_mailbox_deleter.h" | 5 #include "cc/resources/texture_mailbox_deleter.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
11 #include "cc/output/context_provider.h" | 11 #include "cc/output/context_provider.h" |
| 12 #include "cc/resources/scoped_release_callback.h" |
12 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h" | 13 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h" |
13 | 14 |
14 namespace cc { | 15 namespace cc { |
15 | 16 |
16 static void DeleteTextureOnImplThread( | 17 static void DeleteTextureOnImplThread( |
17 const scoped_refptr<ContextProvider>& context_provider, | 18 const scoped_refptr<ContextProvider>& context_provider, |
18 unsigned texture_id, | 19 unsigned texture_id, |
19 unsigned sync_point, | 20 unsigned sync_point, |
20 bool is_lost) { | 21 bool is_lost) { |
21 if (sync_point) | 22 if (sync_point) |
22 context_provider->Context3d()->waitSyncPoint(sync_point); | 23 context_provider->Context3d()->waitSyncPoint(sync_point); |
23 context_provider->Context3d()->deleteTexture(texture_id); | 24 context_provider->Context3d()->deleteTexture(texture_id); |
24 } | 25 } |
25 | 26 |
26 static void PostTaskFromMainToImplThread( | 27 static void PostTaskFromMainToImplThread( |
27 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner, | 28 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner, |
28 TextureMailbox::ReleaseCallback run_impl_callback, | 29 ReleaseCallback run_impl_callback, |
29 unsigned sync_point, | 30 unsigned sync_point, |
30 bool is_lost) { | 31 bool is_lost) { |
31 // This posts the task to RunDeleteTextureOnImplThread(). | 32 // This posts the task to RunDeleteTextureOnImplThread(). |
32 impl_task_runner->PostTask( | 33 impl_task_runner->PostTask( |
33 FROM_HERE, base::Bind(run_impl_callback, sync_point, is_lost)); | 34 FROM_HERE, base::Bind(run_impl_callback, sync_point, is_lost)); |
34 } | 35 } |
35 | 36 |
36 TextureMailboxDeleter::TextureMailboxDeleter() : weak_ptr_factory_(this) {} | 37 TextureMailboxDeleter::TextureMailboxDeleter() : weak_ptr_factory_(this) {} |
37 | 38 |
38 TextureMailboxDeleter::~TextureMailboxDeleter() { | 39 TextureMailboxDeleter::~TextureMailboxDeleter() { |
39 for (size_t i = 0; i < impl_callbacks_.size(); ++i) | 40 for (size_t i = 0; i < impl_callbacks_.size(); ++i) |
40 impl_callbacks_.at(i)->Run(0, true); | 41 impl_callbacks_.at(i)->Run(0, true); |
41 } | 42 } |
42 | 43 |
43 TextureMailbox::ReleaseCallback TextureMailboxDeleter::GetReleaseCallback( | 44 scoped_ptr<ScopedReleaseCallback> TextureMailboxDeleter::GetReleaseCallback( |
44 const scoped_refptr<ContextProvider>& context_provider, | 45 const scoped_refptr<ContextProvider>& context_provider, |
45 unsigned texture_id) { | 46 unsigned texture_id) { |
46 // This callback owns a reference on the |context_provider|. It must be | 47 // This callback owns a reference on the |context_provider|. It must be |
47 // destroyed on the impl thread. Upon destruction of this class, the | 48 // destroyed on the impl thread. Upon destruction of this class, the |
48 // callback must immediately be destroyed. | 49 // callback must immediately be destroyed. |
49 scoped_ptr<TextureMailbox::ReleaseCallback> impl_callback( | 50 scoped_ptr<ScopedReleaseCallback> impl_callback = |
50 new TextureMailbox::ReleaseCallback(base::Bind( | 51 ScopedReleaseCallback::Create(base::Bind(&DeleteTextureOnImplThread, |
51 &DeleteTextureOnImplThread, context_provider, texture_id))); | 52 context_provider, |
| 53 texture_id)); |
52 | 54 |
53 impl_callbacks_.push_back(impl_callback.Pass()); | 55 impl_callbacks_.push_back(impl_callback.Pass()); |
54 | 56 |
55 // The raw pointer to the impl-side callback is valid as long as this | 57 // The raw pointer to the impl-side callback is valid as long as this |
56 // class is alive. So we guard it with a WeakPtr. | 58 // class is alive. So we guard it with a WeakPtr. |
57 TextureMailbox::ReleaseCallback run_impl_callback = | 59 ReleaseCallback run_impl_callback( |
58 base::Bind(&TextureMailboxDeleter::RunDeleteTextureOnImplThread, | 60 base::Bind(&TextureMailboxDeleter::RunDeleteTextureOnImplThread, |
59 weak_ptr_factory_.GetWeakPtr(), | 61 weak_ptr_factory_.GetWeakPtr(), |
60 impl_callbacks_.back()); | 62 impl_callbacks_.back())); |
61 | 63 |
62 // Provide a callback for the main thread that posts back to the impl | 64 // Provide a callback for the main thread that posts back to the impl |
63 // thread. | 65 // thread. |
64 TextureMailbox::ReleaseCallback main_callback = | 66 scoped_ptr<ScopedReleaseCallback> main_callback = |
65 base::Bind(&PostTaskFromMainToImplThread, | 67 ScopedReleaseCallback::Create(base::Bind( |
66 base::MessageLoopProxy::current(), | 68 &PostTaskFromMainToImplThread, |
67 run_impl_callback); | 69 base::MessageLoopProxy::current(), |
| 70 run_impl_callback)); |
68 | 71 |
69 return main_callback; | 72 return main_callback.Pass(); |
70 } | 73 } |
71 | 74 |
72 void TextureMailboxDeleter::RunDeleteTextureOnImplThread( | 75 void TextureMailboxDeleter::RunDeleteTextureOnImplThread( |
73 TextureMailbox::ReleaseCallback* impl_callback, | 76 ScopedReleaseCallback* impl_callback, |
74 unsigned sync_point, | 77 unsigned sync_point, |
75 bool is_lost) { | 78 bool is_lost) { |
76 for (size_t i = 0; i < impl_callbacks_.size(); ++i) { | 79 for (size_t i = 0; i < impl_callbacks_.size(); ++i) { |
77 if (impl_callbacks_.at(i)->Equals(*impl_callback)) { | 80 if (impl_callbacks_.at(i) == impl_callback) { |
78 // Run the callback, then destroy it here on the impl thread. | 81 // Run the callback, then destroy it here on the impl thread. |
79 impl_callback->Run(sync_point, is_lost); | 82 impl_callbacks_.at(i)->Run(sync_point, is_lost); |
80 impl_callbacks_.erase(impl_callbacks_.begin() + i); | 83 impl_callbacks_.erase(impl_callbacks_.begin() + i); |
81 return; | 84 return; |
82 } | 85 } |
83 } | 86 } |
84 | 87 |
85 NOTREACHED() << "The Callback returned by GetDeleteCallback() was called " | 88 NOTREACHED() << "The Callback returned by GetDeleteCallback() was called " |
86 << "more than once."; | 89 << "more than once."; |
87 } | 90 } |
88 | 91 |
89 } // namespace cc | 92 } // namespace cc |
OLD | NEW |