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/debug/test_context_provider.h" | 5 #include "cc/debug/test_context_provider.h" |
6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 #include "cc/debug/test_web_graphics_context_3d.h" | 10 #include "cc/debug/test_web_graphics_context_3d.h" |
9 | 11 |
10 namespace cc { | 12 namespace cc { |
11 | 13 |
| 14 class TestContextProvider::LostContextCallbackProxy |
| 15 : public WebKit::WebGraphicsContext3D::WebGraphicsContextLostCallback { |
| 16 public: |
| 17 explicit LostContextCallbackProxy(TestContextProvider* provider) |
| 18 : provider_(provider) { |
| 19 provider_->context3d_->setContextLostCallback(this); |
| 20 } |
| 21 |
| 22 virtual ~LostContextCallbackProxy() { |
| 23 provider_->context3d_->setContextLostCallback(NULL); |
| 24 } |
| 25 |
| 26 virtual void onContextLost() { |
| 27 provider_->OnLostContext(); |
| 28 } |
| 29 |
| 30 private: |
| 31 TestContextProvider* provider_; |
| 32 }; |
| 33 |
| 34 class TestContextProvider::SwapBuffersCompleteCallbackProxy |
| 35 : public WebKit::WebGraphicsContext3D:: |
| 36 WebGraphicsSwapBuffersCompleteCallbackCHROMIUM { |
| 37 public: |
| 38 explicit SwapBuffersCompleteCallbackProxy(TestContextProvider* provider) |
| 39 : provider_(provider) { |
| 40 provider_->context3d_->setSwapBuffersCompleteCallbackCHROMIUM(this); |
| 41 } |
| 42 |
| 43 virtual ~SwapBuffersCompleteCallbackProxy() { |
| 44 provider_->context3d_->setSwapBuffersCompleteCallbackCHROMIUM(NULL); |
| 45 } |
| 46 |
| 47 virtual void onSwapBuffersComplete() { |
| 48 provider_->OnSwapBuffersComplete(); |
| 49 } |
| 50 |
| 51 private: |
| 52 TestContextProvider* provider_; |
| 53 }; |
| 54 |
12 // static | 55 // static |
13 scoped_refptr<TestContextProvider> TestContextProvider::Create() { | 56 scoped_refptr<TestContextProvider> TestContextProvider::Create() { |
14 return Create(TestWebGraphicsContext3D::CreateFactory()); | 57 return Create(TestWebGraphicsContext3D::Create().Pass()); |
15 } | 58 } |
16 | 59 |
17 // static | 60 // static |
18 scoped_refptr<TestContextProvider> TestContextProvider::Create( | 61 scoped_refptr<TestContextProvider> TestContextProvider::Create( |
19 const CreateCallback& create_callback) { | 62 const CreateCallback& create_callback) { |
20 scoped_refptr<TestContextProvider> provider = new TestContextProvider; | 63 scoped_refptr<TestContextProvider> provider = new TestContextProvider; |
21 if (!provider->InitializeOnMainThread(create_callback)) | 64 if (!provider->InitializeOnMainThread(create_callback)) |
22 return NULL; | 65 return NULL; |
23 return provider; | 66 return provider; |
24 } | 67 } |
25 | 68 |
| 69 scoped_ptr<TestWebGraphicsContext3D> ReturnScopedContext( |
| 70 scoped_ptr<TestWebGraphicsContext3D> context) { |
| 71 return context.Pass(); |
| 72 } |
| 73 |
| 74 // static |
| 75 scoped_refptr<TestContextProvider> TestContextProvider::Create( |
| 76 scoped_ptr<TestWebGraphicsContext3D> context) { |
| 77 return Create(base::Bind(&ReturnScopedContext, base::Passed(&context))); |
| 78 } |
| 79 |
26 TestContextProvider::TestContextProvider() | 80 TestContextProvider::TestContextProvider() |
27 : bound_(false), | 81 : bound_(false), |
28 destroyed_(false) { | 82 destroyed_(false) { |
29 DCHECK(main_thread_checker_.CalledOnValidThread()); | 83 DCHECK(main_thread_checker_.CalledOnValidThread()); |
30 context_thread_checker_.DetachFromThread(); | 84 context_thread_checker_.DetachFromThread(); |
31 } | 85 } |
32 | 86 |
33 TestContextProvider::~TestContextProvider() { | 87 TestContextProvider::~TestContextProvider() { |
34 DCHECK(main_thread_checker_.CalledOnValidThread() || | 88 DCHECK(main_thread_checker_.CalledOnValidThread() || |
35 context_thread_checker_.CalledOnValidThread()); | 89 context_thread_checker_.CalledOnValidThread()); |
(...skipping 17 matching lines...) Expand all Loading... |
53 | 107 |
54 if (bound_) | 108 if (bound_) |
55 return true; | 109 return true; |
56 | 110 |
57 bound_ = true; | 111 bound_ = true; |
58 if (!context3d_->makeContextCurrent()) { | 112 if (!context3d_->makeContextCurrent()) { |
59 base::AutoLock lock(destroyed_lock_); | 113 base::AutoLock lock(destroyed_lock_); |
60 destroyed_ = true; | 114 destroyed_ = true; |
61 return false; | 115 return false; |
62 } | 116 } |
| 117 |
| 118 lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this)); |
| 119 swap_buffers_complete_callback_proxy_.reset( |
| 120 new SwapBuffersCompleteCallbackProxy(this)); |
| 121 |
63 return true; | 122 return true; |
64 } | 123 } |
65 | 124 |
66 WebKit::WebGraphicsContext3D* TestContextProvider::Context3d() { | 125 WebKit::WebGraphicsContext3D* TestContextProvider::Context3d() { |
67 DCHECK(context3d_); | 126 DCHECK(context3d_); |
68 DCHECK(bound_); | 127 DCHECK(bound_); |
69 DCHECK(context_thread_checker_.CalledOnValidThread()); | 128 DCHECK(context_thread_checker_.CalledOnValidThread()); |
70 | 129 |
71 return context3d_.get(); | 130 return context3d_.get(); |
72 } | 131 } |
(...skipping 18 matching lines...) Expand all Loading... |
91 } | 150 } |
92 } | 151 } |
93 | 152 |
94 bool TestContextProvider::DestroyedOnMainThread() { | 153 bool TestContextProvider::DestroyedOnMainThread() { |
95 DCHECK(main_thread_checker_.CalledOnValidThread()); | 154 DCHECK(main_thread_checker_.CalledOnValidThread()); |
96 | 155 |
97 base::AutoLock lock(destroyed_lock_); | 156 base::AutoLock lock(destroyed_lock_); |
98 return destroyed_; | 157 return destroyed_; |
99 } | 158 } |
100 | 159 |
| 160 void TestContextProvider::OnLostContext() { |
| 161 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 162 { |
| 163 base::AutoLock lock(destroyed_lock_); |
| 164 if (destroyed_) |
| 165 return; |
| 166 destroyed_ = true; |
| 167 } |
| 168 if (!lost_context_callback_.is_null()) |
| 169 base::ResetAndReturn(&lost_context_callback_).Run(); |
| 170 } |
| 171 |
| 172 void TestContextProvider::OnSwapBuffersComplete() { |
| 173 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 174 if (!swap_buffers_complete_callback_.is_null()) |
| 175 swap_buffers_complete_callback_.Run(); |
| 176 } |
| 177 |
| 178 void TestContextProvider::SetMemoryAllocation( |
| 179 const ManagedMemoryPolicy& policy, |
| 180 bool discard_backbuffer_when_not_visible) { |
| 181 if (memory_policy_changed_callback_.is_null()) |
| 182 return; |
| 183 memory_policy_changed_callback_.Run( |
| 184 policy, discard_backbuffer_when_not_visible); |
| 185 } |
| 186 |
101 void TestContextProvider::SetLostContextCallback( | 187 void TestContextProvider::SetLostContextCallback( |
102 const LostContextCallback& cb) { | 188 const LostContextCallback& cb) { |
103 DCHECK(context_thread_checker_.CalledOnValidThread()); | 189 DCHECK(context_thread_checker_.CalledOnValidThread()); |
104 NOTIMPLEMENTED(); | 190 DCHECK(lost_context_callback_.is_null() || cb.is_null()); |
| 191 lost_context_callback_ = cb; |
| 192 } |
| 193 |
| 194 void TestContextProvider::SetSwapBuffersCompleteCallback( |
| 195 const SwapBuffersCompleteCallback& cb) { |
| 196 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 197 DCHECK(swap_buffers_complete_callback_.is_null() || cb.is_null()); |
| 198 swap_buffers_complete_callback_ = cb; |
| 199 } |
| 200 |
| 201 void TestContextProvider::SetMemoryPolicyChangedCallback( |
| 202 const MemoryPolicyChangedCallback& cb) { |
| 203 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 204 DCHECK(memory_policy_changed_callback_.is_null() || cb.is_null()); |
| 205 memory_policy_changed_callback_ = cb; |
105 } | 206 } |
106 | 207 |
107 } // namespace cc | 208 } // namespace cc |
OLD | NEW |