Revert 264226 "Reduce dependency of TiclInvalidationService on P..."
[chromium-blink-merge.git] / cc / test / test_context_provider.cc
blob887317aee8f359eb520ea0b67203283d61903796
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/test/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 "cc/test/test_gles2_interface.h"
14 #include "cc/test/test_web_graphics_context_3d.h"
16 namespace cc {
18 // static
19 scoped_refptr<TestContextProvider> TestContextProvider::Create() {
20 return Create(TestWebGraphicsContext3D::Create().Pass());
23 // static
24 scoped_refptr<TestContextProvider> TestContextProvider::Create(
25 scoped_ptr<TestWebGraphicsContext3D> context) {
26 if (!context)
27 return NULL;
28 return new TestContextProvider(context.Pass());
31 TestContextProvider::TestContextProvider(
32 scoped_ptr<TestWebGraphicsContext3D> context)
33 : context3d_(context.Pass()),
34 context_gl_(new TestGLES2Interface(context3d_.get())),
35 bound_(false),
36 destroyed_(false),
37 weak_ptr_factory_(this) {
38 DCHECK(main_thread_checker_.CalledOnValidThread());
39 DCHECK(context3d_);
40 context_thread_checker_.DetachFromThread();
41 context3d_->set_test_support(&support_);
44 TestContextProvider::~TestContextProvider() {
45 DCHECK(main_thread_checker_.CalledOnValidThread() ||
46 context_thread_checker_.CalledOnValidThread());
49 bool TestContextProvider::BindToCurrentThread() {
50 // This is called on the thread the context will be used.
51 DCHECK(context_thread_checker_.CalledOnValidThread());
53 if (bound_)
54 return true;
56 if (context3d_->isContextLost()) {
57 base::AutoLock lock(destroyed_lock_);
58 destroyed_ = true;
59 return false;
61 bound_ = true;
63 context3d_->set_context_lost_callback(
64 base::Bind(&TestContextProvider::OnLostContext,
65 base::Unretained(this)));
67 return true;
70 ContextProvider::Capabilities TestContextProvider::ContextCapabilities() {
71 DCHECK(bound_);
72 DCHECK(context_thread_checker_.CalledOnValidThread());
74 return context3d_->test_capabilities();
77 gpu::gles2::GLES2Interface* TestContextProvider::ContextGL() {
78 DCHECK(context3d_);
79 DCHECK(bound_);
80 DCHECK(context_thread_checker_.CalledOnValidThread());
82 return context_gl_.get();
85 gpu::ContextSupport* TestContextProvider::ContextSupport() {
86 return &support_;
89 class GrContext* TestContextProvider::GrContext() {
90 DCHECK(bound_);
91 DCHECK(context_thread_checker_.CalledOnValidThread());
93 // TODO(danakj): Make a test GrContext that works with a test Context3d.
94 return NULL;
97 bool TestContextProvider::IsContextLost() {
98 DCHECK(bound_);
99 DCHECK(context_thread_checker_.CalledOnValidThread());
101 return context3d_->isContextLost();
104 void TestContextProvider::VerifyContexts() {
105 DCHECK(bound_);
106 DCHECK(context_thread_checker_.CalledOnValidThread());
108 if (context3d_->isContextLost()) {
109 base::AutoLock lock(destroyed_lock_);
110 destroyed_ = true;
114 bool TestContextProvider::DestroyedOnMainThread() {
115 DCHECK(main_thread_checker_.CalledOnValidThread());
117 base::AutoLock lock(destroyed_lock_);
118 return destroyed_;
121 void TestContextProvider::OnLostContext() {
122 DCHECK(context_thread_checker_.CalledOnValidThread());
124 base::AutoLock lock(destroyed_lock_);
125 if (destroyed_)
126 return;
127 destroyed_ = true;
129 if (!lost_context_callback_.is_null())
130 base::ResetAndReturn(&lost_context_callback_).Run();
133 TestWebGraphicsContext3D* TestContextProvider::TestContext3d() {
134 DCHECK(bound_);
135 DCHECK(context_thread_checker_.CalledOnValidThread());
137 return context3d_.get();
140 TestWebGraphicsContext3D* TestContextProvider::UnboundTestContext3d() {
141 return context3d_.get();
144 void TestContextProvider::SetMemoryAllocation(
145 const ManagedMemoryPolicy& policy) {
146 if (memory_policy_changed_callback_.is_null())
147 return;
148 memory_policy_changed_callback_.Run(policy);
151 void TestContextProvider::SetLostContextCallback(
152 const LostContextCallback& cb) {
153 DCHECK(context_thread_checker_.CalledOnValidThread());
154 DCHECK(lost_context_callback_.is_null() || cb.is_null());
155 lost_context_callback_ = cb;
158 void TestContextProvider::SetMemoryPolicyChangedCallback(
159 const MemoryPolicyChangedCallback& cb) {
160 DCHECK(context_thread_checker_.CalledOnValidThread());
161 DCHECK(memory_policy_changed_callback_.is_null() || cb.is_null());
162 memory_policy_changed_callback_ = cb;
165 void TestContextProvider::SetMaxTransferBufferUsageBytes(
166 size_t max_transfer_buffer_usage_bytes) {
167 context3d_->SetMaxTransferBufferUsageBytes(max_transfer_buffer_usage_bytes);
170 } // namespace cc