Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / cc / test / test_context_provider.cc
blob4c24fe1efebaaa4bcdc376212aa606f4345270fb
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/GrGLInterface.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 ContextProvider::Capabilities TestContextProvider::ContextCapabilities() {
73 DCHECK(bound_);
74 DCHECK(context_thread_checker_.CalledOnValidThread());
76 return context3d_->test_capabilities();
79 gpu::gles2::GLES2Interface* TestContextProvider::ContextGL() {
80 DCHECK(context3d_);
81 DCHECK(bound_);
82 DCHECK(context_thread_checker_.CalledOnValidThread());
84 return context_gl_.get();
87 gpu::ContextSupport* TestContextProvider::ContextSupport() {
88 return &support_;
91 class GrContext* TestContextProvider::GrContext() {
92 DCHECK(bound_);
93 DCHECK(context_thread_checker_.CalledOnValidThread());
95 if (gr_context_)
96 return gr_context_.get();
98 skia::RefPtr<const GrGLInterface> null_interface =
99 skia::AdoptRef(GrGLCreateNullInterface());
100 gr_context_ = skia::AdoptRef(GrContext::Create(
101 kOpenGL_GrBackend,
102 reinterpret_cast<GrBackendContext>(null_interface.get())));
103 return gr_context_.get();
106 bool TestContextProvider::IsContextLost() {
107 DCHECK(bound_);
108 DCHECK(context_thread_checker_.CalledOnValidThread());
110 return context3d_->isContextLost();
113 void TestContextProvider::VerifyContexts() {
114 DCHECK(bound_);
115 DCHECK(context_thread_checker_.CalledOnValidThread());
117 if (context3d_->isContextLost()) {
118 base::AutoLock lock(destroyed_lock_);
119 destroyed_ = true;
123 void TestContextProvider::DeleteCachedResources() {
126 bool TestContextProvider::DestroyedOnMainThread() {
127 DCHECK(main_thread_checker_.CalledOnValidThread());
129 base::AutoLock lock(destroyed_lock_);
130 return destroyed_;
133 void TestContextProvider::OnLostContext() {
134 DCHECK(context_thread_checker_.CalledOnValidThread());
136 base::AutoLock lock(destroyed_lock_);
137 if (destroyed_)
138 return;
139 destroyed_ = true;
141 if (!lost_context_callback_.is_null())
142 base::ResetAndReturn(&lost_context_callback_).Run();
145 TestWebGraphicsContext3D* TestContextProvider::TestContext3d() {
146 DCHECK(bound_);
147 DCHECK(context_thread_checker_.CalledOnValidThread());
149 return context3d_.get();
152 TestWebGraphicsContext3D* TestContextProvider::UnboundTestContext3d() {
153 return context3d_.get();
156 void TestContextProvider::SetMemoryAllocation(
157 const ManagedMemoryPolicy& policy) {
158 if (memory_policy_changed_callback_.is_null())
159 return;
160 memory_policy_changed_callback_.Run(policy);
163 void TestContextProvider::SetLostContextCallback(
164 const LostContextCallback& cb) {
165 DCHECK(context_thread_checker_.CalledOnValidThread());
166 DCHECK(lost_context_callback_.is_null() || cb.is_null());
167 lost_context_callback_ = cb;
170 void TestContextProvider::SetMemoryPolicyChangedCallback(
171 const MemoryPolicyChangedCallback& cb) {
172 DCHECK(context_thread_checker_.CalledOnValidThread());
173 DCHECK(memory_policy_changed_callback_.is_null() || cb.is_null());
174 memory_policy_changed_callback_ = cb;
177 void TestContextProvider::SetMaxTransferBufferUsageBytes(
178 size_t max_transfer_buffer_usage_bytes) {
179 context3d_->SetMaxTransferBufferUsageBytes(max_transfer_buffer_usage_bytes);
182 } // namespace cc