Disable flaky SelectFileDialogExtensionBrowserTest tests
[chromium-blink-merge.git] / cc / test / test_context_provider.cc
blob7c9bb2ce07592171a5f7f91db0420bd916668e0d
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"
15 #include "third_party/skia/include/gpu/GrContext.h"
16 #include "third_party/skia/include/gpu/gl/SkNullGLContext.h"
18 namespace cc {
20 // static
21 scoped_refptr<TestContextProvider> TestContextProvider::Create() {
22 return Create(TestWebGraphicsContext3D::Create().Pass());
25 // static
26 scoped_refptr<TestContextProvider> TestContextProvider::Create(
27 scoped_ptr<TestWebGraphicsContext3D> context) {
28 if (!context)
29 return NULL;
30 return new TestContextProvider(context.Pass());
33 TestContextProvider::TestContextProvider(
34 scoped_ptr<TestWebGraphicsContext3D> context)
35 : context3d_(context.Pass()),
36 context_gl_(new TestGLES2Interface(context3d_.get())),
37 bound_(false),
38 destroyed_(false),
39 weak_ptr_factory_(this) {
40 DCHECK(main_thread_checker_.CalledOnValidThread());
41 DCHECK(context3d_);
42 context_thread_checker_.DetachFromThread();
43 context3d_->set_test_support(&support_);
46 TestContextProvider::~TestContextProvider() {
47 DCHECK(main_thread_checker_.CalledOnValidThread() ||
48 context_thread_checker_.CalledOnValidThread());
51 bool TestContextProvider::BindToCurrentThread() {
52 // This is called on the thread the context will be used.
53 DCHECK(context_thread_checker_.CalledOnValidThread());
55 if (bound_)
56 return true;
58 if (context3d_->isContextLost()) {
59 base::AutoLock lock(destroyed_lock_);
60 destroyed_ = true;
61 return false;
63 bound_ = true;
65 context3d_->set_context_lost_callback(
66 base::Bind(&TestContextProvider::OnLostContext,
67 base::Unretained(this)));
69 return true;
72 void TestContextProvider::DetachFromThread() {
73 context_thread_checker_.DetachFromThread();
76 ContextProvider::Capabilities TestContextProvider::ContextCapabilities() {
77 DCHECK(bound_);
78 DCHECK(context_thread_checker_.CalledOnValidThread());
80 return context3d_->test_capabilities();
83 gpu::gles2::GLES2Interface* TestContextProvider::ContextGL() {
84 DCHECK(context3d_);
85 DCHECK(bound_);
86 DCHECK(context_thread_checker_.CalledOnValidThread());
88 return context_gl_.get();
91 gpu::ContextSupport* TestContextProvider::ContextSupport() {
92 return &support_;
95 class GrContext* TestContextProvider::GrContext() {
96 DCHECK(bound_);
97 DCHECK(context_thread_checker_.CalledOnValidThread());
99 if (gr_context_)
100 return gr_context_.get();
102 skia::RefPtr<class SkGLContext> gl_context =
103 skia::AdoptRef(SkNullGLContext::Create(kNone_GrGLStandard));
104 gl_context->makeCurrent();
105 gr_context_ = skia::AdoptRef(GrContext::Create(
106 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(gl_context->gl())));
108 // If GlContext is already lost, also abandon the new GrContext.
109 if (IsContextLost())
110 gr_context_->abandonContext();
112 return gr_context_.get();
115 void TestContextProvider::SetupLock() {
118 base::Lock* TestContextProvider::GetLock() {
119 return &context_lock_;
122 bool TestContextProvider::IsContextLost() {
123 DCHECK(bound_);
124 DCHECK(context_thread_checker_.CalledOnValidThread());
126 return context3d_->isContextLost();
129 void TestContextProvider::VerifyContexts() {
130 DCHECK(bound_);
131 DCHECK(context_thread_checker_.CalledOnValidThread());
133 if (context3d_->isContextLost()) {
134 base::AutoLock lock(destroyed_lock_);
135 destroyed_ = true;
139 void TestContextProvider::DeleteCachedResources() {
142 bool TestContextProvider::DestroyedOnMainThread() {
143 DCHECK(main_thread_checker_.CalledOnValidThread());
145 base::AutoLock lock(destroyed_lock_);
146 return destroyed_;
149 void TestContextProvider::OnLostContext() {
150 DCHECK(context_thread_checker_.CalledOnValidThread());
152 base::AutoLock lock(destroyed_lock_);
153 if (destroyed_)
154 return;
155 destroyed_ = true;
157 if (!lost_context_callback_.is_null())
158 base::ResetAndReturn(&lost_context_callback_).Run();
159 if (gr_context_)
160 gr_context_->abandonContext();
163 TestWebGraphicsContext3D* TestContextProvider::TestContext3d() {
164 DCHECK(bound_);
165 DCHECK(context_thread_checker_.CalledOnValidThread());
167 return context3d_.get();
170 TestWebGraphicsContext3D* TestContextProvider::UnboundTestContext3d() {
171 return context3d_.get();
174 void TestContextProvider::SetMemoryAllocation(
175 const ManagedMemoryPolicy& policy) {
176 if (memory_policy_changed_callback_.is_null())
177 return;
178 memory_policy_changed_callback_.Run(policy);
181 void TestContextProvider::SetLostContextCallback(
182 const LostContextCallback& cb) {
183 DCHECK(context_thread_checker_.CalledOnValidThread());
184 DCHECK(lost_context_callback_.is_null() || cb.is_null());
185 lost_context_callback_ = cb;
188 void TestContextProvider::SetMemoryPolicyChangedCallback(
189 const MemoryPolicyChangedCallback& cb) {
190 DCHECK(context_thread_checker_.CalledOnValidThread());
191 DCHECK(memory_policy_changed_callback_.is_null() || cb.is_null());
192 memory_policy_changed_callback_ = cb;
195 void TestContextProvider::SetMaxTransferBufferUsageBytes(
196 size_t max_transfer_buffer_usage_bytes) {
197 context3d_->SetMaxTransferBufferUsageBytes(max_transfer_buffer_usage_bytes);
200 } // namespace cc