Added assert() and assertInstanceof() declaration, added handling of assert() and...
[chromium-blink-merge.git] / cc / test / test_context_provider.cc
blobe7697f17d25ea69618c976a058cb81d1b1a350f4
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 void TestContextProvider::DeleteCachedResources() {
117 bool TestContextProvider::DestroyedOnMainThread() {
118 DCHECK(main_thread_checker_.CalledOnValidThread());
120 base::AutoLock lock(destroyed_lock_);
121 return destroyed_;
124 void TestContextProvider::OnLostContext() {
125 DCHECK(context_thread_checker_.CalledOnValidThread());
127 base::AutoLock lock(destroyed_lock_);
128 if (destroyed_)
129 return;
130 destroyed_ = true;
132 if (!lost_context_callback_.is_null())
133 base::ResetAndReturn(&lost_context_callback_).Run();
136 TestWebGraphicsContext3D* TestContextProvider::TestContext3d() {
137 DCHECK(bound_);
138 DCHECK(context_thread_checker_.CalledOnValidThread());
140 return context3d_.get();
143 TestWebGraphicsContext3D* TestContextProvider::UnboundTestContext3d() {
144 return context3d_.get();
147 void TestContextProvider::SetMemoryAllocation(
148 const ManagedMemoryPolicy& policy) {
149 if (memory_policy_changed_callback_.is_null())
150 return;
151 memory_policy_changed_callback_.Run(policy);
154 void TestContextProvider::SetLostContextCallback(
155 const LostContextCallback& cb) {
156 DCHECK(context_thread_checker_.CalledOnValidThread());
157 DCHECK(lost_context_callback_.is_null() || cb.is_null());
158 lost_context_callback_ = cb;
161 void TestContextProvider::SetMemoryPolicyChangedCallback(
162 const MemoryPolicyChangedCallback& cb) {
163 DCHECK(context_thread_checker_.CalledOnValidThread());
164 DCHECK(memory_policy_changed_callback_.is_null() || cb.is_null());
165 memory_policy_changed_callback_ = cb;
168 void TestContextProvider::SetMaxTransferBufferUsageBytes(
169 size_t max_transfer_buffer_usage_bytes) {
170 context3d_->SetMaxTransferBufferUsageBytes(max_transfer_buffer_usage_bytes);
173 } // namespace cc