OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "cc/debug/test_context_provider.h" | |
6 | |
7 #include <set> | |
8 #include <vector> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/callback_helpers.h" | |
12 #include "base/logging.h" | |
13 #include "base/strings/string_split.h" | |
14 #include "cc/debug/test_web_graphics_context_3d.h" | |
15 | |
16 namespace cc { | |
17 | |
18 class TestContextProvider::LostContextCallbackProxy | |
19 : public WebKit::WebGraphicsContext3D::WebGraphicsContextLostCallback { | |
20 public: | |
21 explicit LostContextCallbackProxy(TestContextProvider* provider) | |
22 : provider_(provider) { | |
23 provider_->context3d_->setContextLostCallback(this); | |
24 } | |
25 | |
26 virtual ~LostContextCallbackProxy() { | |
27 provider_->context3d_->setContextLostCallback(NULL); | |
28 } | |
29 | |
30 virtual void onContextLost() { | |
31 provider_->OnLostContext(); | |
32 } | |
33 | |
34 private: | |
35 TestContextProvider* provider_; | |
36 }; | |
37 | |
38 class TestContextProvider::SwapBuffersCompleteCallbackProxy | |
39 : public WebKit::WebGraphicsContext3D:: | |
40 WebGraphicsSwapBuffersCompleteCallbackCHROMIUM { | |
41 public: | |
42 explicit SwapBuffersCompleteCallbackProxy(TestContextProvider* provider) | |
43 : provider_(provider) { | |
44 provider_->context3d_->setSwapBuffersCompleteCallbackCHROMIUM(this); | |
45 } | |
46 | |
47 virtual ~SwapBuffersCompleteCallbackProxy() { | |
48 provider_->context3d_->setSwapBuffersCompleteCallbackCHROMIUM(NULL); | |
49 } | |
50 | |
51 virtual void onSwapBuffersComplete() { | |
52 provider_->OnSwapBuffersComplete(); | |
53 } | |
54 | |
55 private: | |
56 TestContextProvider* provider_; | |
57 }; | |
58 | |
59 // static | |
60 scoped_refptr<TestContextProvider> TestContextProvider::Create() { | |
61 return Create(TestWebGraphicsContext3D::Create().Pass()); | |
62 } | |
63 | |
64 // static | |
65 scoped_refptr<TestContextProvider> TestContextProvider::Create( | |
66 scoped_ptr<TestWebGraphicsContext3D> context) { | |
67 if (!context) | |
68 return NULL; | |
69 return new TestContextProvider(context.Pass()); | |
70 } | |
71 | |
72 TestContextProvider::TestContextProvider( | |
73 scoped_ptr<TestWebGraphicsContext3D> context) | |
74 : context3d_(context.Pass()), bound_(false), destroyed_(false) { | |
75 DCHECK(main_thread_checker_.CalledOnValidThread()); | |
76 DCHECK(context3d_); | |
77 context_thread_checker_.DetachFromThread(); | |
78 context3d_->set_test_support(&support_); | |
79 } | |
80 | |
81 TestContextProvider::~TestContextProvider() { | |
82 DCHECK(main_thread_checker_.CalledOnValidThread() || | |
83 context_thread_checker_.CalledOnValidThread()); | |
84 } | |
85 | |
86 bool TestContextProvider::BindToCurrentThread() { | |
87 DCHECK(context3d_); | |
88 | |
89 // This is called on the thread the context will be used. | |
90 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
91 | |
92 if (bound_) | |
93 return true; | |
94 | |
95 bound_ = true; | |
96 if (!context3d_->makeContextCurrent()) { | |
97 base::AutoLock lock(destroyed_lock_); | |
98 destroyed_ = true; | |
99 return false; | |
100 } | |
101 | |
102 lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this)); | |
103 swap_buffers_complete_callback_proxy_.reset( | |
104 new SwapBuffersCompleteCallbackProxy(this)); | |
105 | |
106 return true; | |
107 } | |
108 | |
109 ContextProvider::Capabilities TestContextProvider::ContextCapabilities() { | |
110 DCHECK(context3d_); | |
111 DCHECK(bound_); | |
112 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
113 | |
114 return context3d_->test_capabilities(); | |
115 } | |
116 | |
117 WebKit::WebGraphicsContext3D* TestContextProvider::Context3d() { | |
118 DCHECK(context3d_); | |
119 DCHECK(bound_); | |
120 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
121 | |
122 return context3d_.get(); | |
123 } | |
124 | |
125 gpu::ContextSupport* TestContextProvider::ContextSupport() { | |
126 DCHECK(context3d_); | |
127 DCHECK(bound_); | |
128 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
129 | |
130 return &support_; | |
131 } | |
132 | |
133 class GrContext* TestContextProvider::GrContext() { | |
134 DCHECK(context3d_); | |
135 DCHECK(bound_); | |
136 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
137 | |
138 // TODO(danakj): Make a test GrContext that works with a test Context3d. | |
139 return NULL; | |
140 } | |
141 | |
142 void TestContextProvider::VerifyContexts() { | |
143 DCHECK(context3d_); | |
144 DCHECK(bound_); | |
145 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
146 | |
147 if (context3d_->isContextLost()) { | |
148 base::AutoLock lock(destroyed_lock_); | |
149 destroyed_ = true; | |
150 } | |
151 } | |
152 | |
153 bool TestContextProvider::DestroyedOnMainThread() { | |
154 DCHECK(main_thread_checker_.CalledOnValidThread()); | |
155 | |
156 base::AutoLock lock(destroyed_lock_); | |
157 return destroyed_; | |
158 } | |
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 TestWebGraphicsContext3D* TestContextProvider::TestContext3d() { | |
179 DCHECK(context3d_); | |
180 DCHECK(bound_); | |
181 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
182 | |
183 return context3d_.get(); | |
184 } | |
185 | |
186 TestWebGraphicsContext3D* TestContextProvider::UnboundTestContext3d() { | |
187 DCHECK(context3d_); | |
188 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
189 | |
190 return context3d_.get(); | |
191 } | |
192 | |
193 void TestContextProvider::SetMemoryAllocation( | |
194 const ManagedMemoryPolicy& policy, | |
195 bool discard_backbuffer_when_not_visible) { | |
196 if (memory_policy_changed_callback_.is_null()) | |
197 return; | |
198 memory_policy_changed_callback_.Run( | |
199 policy, discard_backbuffer_when_not_visible); | |
200 } | |
201 | |
202 void TestContextProvider::SetLostContextCallback( | |
203 const LostContextCallback& cb) { | |
204 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
205 DCHECK(lost_context_callback_.is_null() || cb.is_null()); | |
206 lost_context_callback_ = cb; | |
207 } | |
208 | |
209 void TestContextProvider::SetSwapBuffersCompleteCallback( | |
210 const SwapBuffersCompleteCallback& cb) { | |
211 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
212 DCHECK(swap_buffers_complete_callback_.is_null() || cb.is_null()); | |
213 swap_buffers_complete_callback_ = cb; | |
214 } | |
215 | |
216 void TestContextProvider::SetMemoryPolicyChangedCallback( | |
217 const MemoryPolicyChangedCallback& cb) { | |
218 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
219 DCHECK(memory_policy_changed_callback_.is_null() || cb.is_null()); | |
220 memory_policy_changed_callback_ = cb; | |
221 } | |
222 | |
223 void TestContextProvider::SetMaxTransferBufferUsageBytes( | |
224 size_t max_transfer_buffer_usage_bytes) { | |
225 context3d_->SetMaxTransferBufferUsageBytes(max_transfer_buffer_usage_bytes); | |
226 } | |
227 | |
228 } // namespace cc | |
OLD | NEW |