cc: Added inline to Tile::IsReadyToDraw
[chromium-blink-merge.git] / cc / debug / test_context_provider.cc
blob0d6fc010a3096a19f5c441408250273251daf87c
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.
5 #include "cc/debug/test_context_provider.h"
7 #include <set>
8 #include <vector>
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"
16 namespace cc {
18 class TestContextProvider::LostContextCallbackProxy
19 : public WebKit::WebGraphicsContext3D::WebGraphicsContextLostCallback {
20 public:
21 explicit LostContextCallbackProxy(TestContextProvider* provider)
22 : provider_(provider) {
23 provider_->context3d_->setContextLostCallback(this);
26 virtual ~LostContextCallbackProxy() {
27 provider_->context3d_->setContextLostCallback(NULL);
30 virtual void onContextLost() {
31 provider_->OnLostContext();
34 private:
35 TestContextProvider* provider_;
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);
47 virtual ~SwapBuffersCompleteCallbackProxy() {
48 provider_->context3d_->setSwapBuffersCompleteCallbackCHROMIUM(NULL);
51 virtual void onSwapBuffersComplete() {
52 provider_->OnSwapBuffersComplete();
55 private:
56 TestContextProvider* provider_;
59 // static
60 scoped_refptr<TestContextProvider> TestContextProvider::Create() {
61 return Create(TestWebGraphicsContext3D::Create().Pass());
64 // static
65 scoped_refptr<TestContextProvider> TestContextProvider::Create(
66 const CreateCallback& create_callback) {
67 scoped_refptr<TestContextProvider> provider = new TestContextProvider;
68 if (!provider->InitializeOnMainThread(create_callback))
69 return NULL;
70 return provider;
73 scoped_ptr<TestWebGraphicsContext3D> ReturnScopedContext(
74 scoped_ptr<TestWebGraphicsContext3D> context) {
75 return context.Pass();
78 // static
79 scoped_refptr<TestContextProvider> TestContextProvider::Create(
80 scoped_ptr<TestWebGraphicsContext3D> context) {
81 return Create(base::Bind(&ReturnScopedContext, base::Passed(&context)));
84 TestContextProvider::TestContextProvider()
85 : bound_(false),
86 destroyed_(false) {
87 DCHECK(main_thread_checker_.CalledOnValidThread());
88 context_thread_checker_.DetachFromThread();
91 TestContextProvider::~TestContextProvider() {
92 DCHECK(main_thread_checker_.CalledOnValidThread() ||
93 context_thread_checker_.CalledOnValidThread());
96 bool TestContextProvider::InitializeOnMainThread(
97 const CreateCallback& create_callback) {
98 DCHECK(main_thread_checker_.CalledOnValidThread());
100 DCHECK(!context3d_);
101 DCHECK(!create_callback.is_null());
102 context3d_ = create_callback.Run();
103 return context3d_;
106 bool TestContextProvider::BindToCurrentThread() {
107 DCHECK(context3d_);
109 // This is called on the thread the context will be used.
110 DCHECK(context_thread_checker_.CalledOnValidThread());
112 if (bound_)
113 return true;
115 bound_ = true;
116 if (!context3d_->makeContextCurrent()) {
117 base::AutoLock lock(destroyed_lock_);
118 destroyed_ = true;
119 return false;
122 lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this));
123 swap_buffers_complete_callback_proxy_.reset(
124 new SwapBuffersCompleteCallbackProxy(this));
126 return true;
129 ContextProvider::Capabilities TestContextProvider::ContextCapabilities() {
130 DCHECK(context3d_);
131 DCHECK(bound_);
132 DCHECK(context_thread_checker_.CalledOnValidThread());
134 return context3d_->test_capabilities();
137 WebKit::WebGraphicsContext3D* TestContextProvider::Context3d() {
138 DCHECK(context3d_);
139 DCHECK(bound_);
140 DCHECK(context_thread_checker_.CalledOnValidThread());
142 return context3d_.get();
145 class GrContext* TestContextProvider::GrContext() {
146 DCHECK(context3d_);
147 DCHECK(bound_);
148 DCHECK(context_thread_checker_.CalledOnValidThread());
150 // TODO(danakj): Make a test GrContext that works with a test Context3d.
151 return NULL;
154 void TestContextProvider::VerifyContexts() {
155 DCHECK(context3d_);
156 DCHECK(bound_);
157 DCHECK(context_thread_checker_.CalledOnValidThread());
159 if (context3d_->isContextLost()) {
160 base::AutoLock lock(destroyed_lock_);
161 destroyed_ = true;
165 bool TestContextProvider::DestroyedOnMainThread() {
166 DCHECK(main_thread_checker_.CalledOnValidThread());
168 base::AutoLock lock(destroyed_lock_);
169 return destroyed_;
172 void TestContextProvider::OnLostContext() {
173 DCHECK(context_thread_checker_.CalledOnValidThread());
175 base::AutoLock lock(destroyed_lock_);
176 if (destroyed_)
177 return;
178 destroyed_ = true;
180 if (!lost_context_callback_.is_null())
181 base::ResetAndReturn(&lost_context_callback_).Run();
184 void TestContextProvider::OnSwapBuffersComplete() {
185 DCHECK(context_thread_checker_.CalledOnValidThread());
186 if (!swap_buffers_complete_callback_.is_null())
187 swap_buffers_complete_callback_.Run();
190 TestWebGraphicsContext3D* TestContextProvider::TestContext3d() {
191 DCHECK(context3d_);
192 DCHECK(bound_);
193 DCHECK(context_thread_checker_.CalledOnValidThread());
195 return context3d_.get();
198 TestWebGraphicsContext3D* TestContextProvider::UnboundTestContext3d() {
199 DCHECK(context3d_);
200 DCHECK(context_thread_checker_.CalledOnValidThread());
202 return context3d_.get();
205 void TestContextProvider::SetMemoryAllocation(
206 const ManagedMemoryPolicy& policy,
207 bool discard_backbuffer_when_not_visible) {
208 if (memory_policy_changed_callback_.is_null())
209 return;
210 memory_policy_changed_callback_.Run(
211 policy, discard_backbuffer_when_not_visible);
214 void TestContextProvider::SetLostContextCallback(
215 const LostContextCallback& cb) {
216 DCHECK(context_thread_checker_.CalledOnValidThread());
217 DCHECK(lost_context_callback_.is_null() || cb.is_null());
218 lost_context_callback_ = cb;
221 void TestContextProvider::SetSwapBuffersCompleteCallback(
222 const SwapBuffersCompleteCallback& cb) {
223 DCHECK(context_thread_checker_.CalledOnValidThread());
224 DCHECK(swap_buffers_complete_callback_.is_null() || cb.is_null());
225 swap_buffers_complete_callback_ = cb;
228 void TestContextProvider::SetMemoryPolicyChangedCallback(
229 const MemoryPolicyChangedCallback& cb) {
230 DCHECK(context_thread_checker_.CalledOnValidThread());
231 DCHECK(memory_policy_changed_callback_.is_null() || cb.is_null());
232 memory_policy_changed_callback_ = cb;
235 } // namespace cc