Dismiss autofill popup on screen orientation change.
[chromium-blink-merge.git] / cc / debug / test_context_provider.cc
blob55fc74fead1a866b35952056d50006c8aa9c88ae
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 "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/logging.h"
10 #include "cc/debug/test_web_graphics_context_3d.h"
12 namespace cc {
14 class TestContextProvider::LostContextCallbackProxy
15 : public WebKit::WebGraphicsContext3D::WebGraphicsContextLostCallback {
16 public:
17 explicit LostContextCallbackProxy(TestContextProvider* provider)
18 : provider_(provider) {
19 provider_->context3d_->setContextLostCallback(this);
22 virtual ~LostContextCallbackProxy() {
23 provider_->context3d_->setContextLostCallback(NULL);
26 virtual void onContextLost() {
27 provider_->OnLostContext();
30 private:
31 TestContextProvider* provider_;
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);
43 virtual ~SwapBuffersCompleteCallbackProxy() {
44 provider_->context3d_->setSwapBuffersCompleteCallbackCHROMIUM(NULL);
47 virtual void onSwapBuffersComplete() {
48 provider_->OnSwapBuffersComplete();
51 private:
52 TestContextProvider* provider_;
55 // static
56 scoped_refptr<TestContextProvider> TestContextProvider::Create() {
57 return Create(TestWebGraphicsContext3D::Create().Pass());
60 // static
61 scoped_refptr<TestContextProvider> TestContextProvider::Create(
62 const CreateCallback& create_callback) {
63 scoped_refptr<TestContextProvider> provider = new TestContextProvider;
64 if (!provider->InitializeOnMainThread(create_callback))
65 return NULL;
66 return provider;
69 scoped_ptr<TestWebGraphicsContext3D> ReturnScopedContext(
70 scoped_ptr<TestWebGraphicsContext3D> context) {
71 return context.Pass();
74 // static
75 scoped_refptr<TestContextProvider> TestContextProvider::Create(
76 scoped_ptr<TestWebGraphicsContext3D> context) {
77 return Create(base::Bind(&ReturnScopedContext, base::Passed(&context)));
80 TestContextProvider::TestContextProvider()
81 : bound_(false),
82 destroyed_(false) {
83 DCHECK(main_thread_checker_.CalledOnValidThread());
84 context_thread_checker_.DetachFromThread();
87 TestContextProvider::~TestContextProvider() {
88 DCHECK(main_thread_checker_.CalledOnValidThread() ||
89 context_thread_checker_.CalledOnValidThread());
92 bool TestContextProvider::InitializeOnMainThread(
93 const CreateCallback& create_callback) {
94 DCHECK(main_thread_checker_.CalledOnValidThread());
96 DCHECK(!context3d_);
97 DCHECK(!create_callback.is_null());
98 context3d_ = create_callback.Run();
99 return context3d_;
102 bool TestContextProvider::BindToCurrentThread() {
103 DCHECK(context3d_);
105 // This is called on the thread the context will be used.
106 DCHECK(context_thread_checker_.CalledOnValidThread());
108 if (bound_)
109 return true;
111 bound_ = true;
112 if (!context3d_->makeContextCurrent()) {
113 base::AutoLock lock(destroyed_lock_);
114 destroyed_ = true;
115 return false;
118 lost_context_callback_proxy_.reset(new LostContextCallbackProxy(this));
119 swap_buffers_complete_callback_proxy_.reset(
120 new SwapBuffersCompleteCallbackProxy(this));
122 return true;
125 WebKit::WebGraphicsContext3D* TestContextProvider::Context3d() {
126 DCHECK(context3d_);
127 DCHECK(bound_);
128 DCHECK(context_thread_checker_.CalledOnValidThread());
130 return context3d_.get();
133 class GrContext* TestContextProvider::GrContext() {
134 DCHECK(context3d_);
135 DCHECK(bound_);
136 DCHECK(context_thread_checker_.CalledOnValidThread());
138 // TODO(danakj): Make a test GrContext that works with a test Context3d.
139 return NULL;
142 void TestContextProvider::VerifyContexts() {
143 DCHECK(context3d_);
144 DCHECK(bound_);
145 DCHECK(context_thread_checker_.CalledOnValidThread());
147 if (context3d_->isContextLost()) {
148 base::AutoLock lock(destroyed_lock_);
149 destroyed_ = true;
153 bool TestContextProvider::DestroyedOnMainThread() {
154 DCHECK(main_thread_checker_.CalledOnValidThread());
156 base::AutoLock lock(destroyed_lock_);
157 return destroyed_;
160 void TestContextProvider::OnLostContext() {
161 DCHECK(context_thread_checker_.CalledOnValidThread());
163 base::AutoLock lock(destroyed_lock_);
164 if (destroyed_)
165 return;
166 destroyed_ = true;
168 if (!lost_context_callback_.is_null())
169 base::ResetAndReturn(&lost_context_callback_).Run();
172 void TestContextProvider::OnSwapBuffersComplete() {
173 DCHECK(context_thread_checker_.CalledOnValidThread());
174 if (!swap_buffers_complete_callback_.is_null())
175 swap_buffers_complete_callback_.Run();
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);
187 void TestContextProvider::SetLostContextCallback(
188 const LostContextCallback& cb) {
189 DCHECK(context_thread_checker_.CalledOnValidThread());
190 DCHECK(lost_context_callback_.is_null() || cb.is_null());
191 lost_context_callback_ = cb;
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;
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;
208 } // namespace cc