cc: Implement shared worker contexts.
[chromium-blink-merge.git] / cc / test / test_context_provider.cc
blob5e43ee3fcc5892c73b5afa24124d42c2479a57b5
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());
25 // static
26 scoped_refptr<TestContextProvider> TestContextProvider::CreateWorker() {
27 scoped_refptr<TestContextProvider> worker_context_provider =
28 Create(TestWebGraphicsContext3D::Create());
29 if (!worker_context_provider)
30 return nullptr;
31 // Worker contexts are bound to the thread they are created on.
32 if (!worker_context_provider->BindToCurrentThread())
33 return nullptr;
34 worker_context_provider->SetupLock();
35 return worker_context_provider;
38 // static
39 scoped_refptr<TestContextProvider> TestContextProvider::Create(
40 scoped_ptr<TestWebGraphicsContext3D> context) {
41 if (!context)
42 return NULL;
43 return new TestContextProvider(context.Pass());
46 TestContextProvider::TestContextProvider(
47 scoped_ptr<TestWebGraphicsContext3D> context)
48 : context3d_(context.Pass()),
49 context_gl_(new TestGLES2Interface(context3d_.get())),
50 bound_(false),
51 destroyed_(false),
52 weak_ptr_factory_(this) {
53 DCHECK(main_thread_checker_.CalledOnValidThread());
54 DCHECK(context3d_);
55 context_thread_checker_.DetachFromThread();
56 context3d_->set_test_support(&support_);
59 TestContextProvider::~TestContextProvider() {
60 DCHECK(main_thread_checker_.CalledOnValidThread() ||
61 context_thread_checker_.CalledOnValidThread());
64 bool TestContextProvider::BindToCurrentThread() {
65 // This is called on the thread the context will be used.
66 DCHECK(context_thread_checker_.CalledOnValidThread());
68 if (bound_)
69 return true;
71 if (context_gl_->GetGraphicsResetStatusKHR() != GL_NO_ERROR) {
72 base::AutoLock lock(destroyed_lock_);
73 destroyed_ = true;
74 return false;
76 bound_ = true;
78 context3d_->set_context_lost_callback(
79 base::Bind(&TestContextProvider::OnLostContext,
80 base::Unretained(this)));
82 return true;
85 void TestContextProvider::DetachFromThread() {
86 context_thread_checker_.DetachFromThread();
89 ContextProvider::Capabilities TestContextProvider::ContextCapabilities() {
90 DCHECK(bound_);
91 DCHECK(context_thread_checker_.CalledOnValidThread());
93 return context3d_->test_capabilities();
96 gpu::gles2::GLES2Interface* TestContextProvider::ContextGL() {
97 DCHECK(context3d_);
98 DCHECK(bound_);
99 DCHECK(context_thread_checker_.CalledOnValidThread());
101 return context_gl_.get();
104 gpu::ContextSupport* TestContextProvider::ContextSupport() {
105 return &support_;
108 class GrContext* TestContextProvider::GrContext() {
109 DCHECK(bound_);
110 DCHECK(context_thread_checker_.CalledOnValidThread());
112 if (gr_context_)
113 return gr_context_.get();
115 skia::RefPtr<class SkGLContext> gl_context =
116 skia::AdoptRef(SkNullGLContext::Create(kNone_GrGLStandard));
117 gl_context->makeCurrent();
118 gr_context_ = skia::AdoptRef(GrContext::Create(
119 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(gl_context->gl())));
121 // If GlContext is already lost, also abandon the new GrContext.
122 if (ContextGL()->GetGraphicsResetStatusKHR() != GL_NO_ERROR)
123 gr_context_->abandonContext();
125 return gr_context_.get();
128 void TestContextProvider::InvalidateGrContext(uint32_t state) {
129 DCHECK(bound_);
130 DCHECK(context_thread_checker_.CalledOnValidThread());
132 if (gr_context_)
133 gr_context_.get()->resetContext(state);
136 void TestContextProvider::SetupLock() {
139 base::Lock* TestContextProvider::GetLock() {
140 return &context_lock_;
143 void TestContextProvider::VerifyContexts() {
144 DCHECK(bound_);
145 DCHECK(context_thread_checker_.CalledOnValidThread());
147 if (ContextGL()->GetGraphicsResetStatusKHR() != GL_NO_ERROR) {
148 base::AutoLock lock(destroyed_lock_);
149 destroyed_ = true;
153 void TestContextProvider::DeleteCachedResources() {
156 bool TestContextProvider::DestroyedOnMainThread() {
157 DCHECK(main_thread_checker_.CalledOnValidThread());
159 base::AutoLock lock(destroyed_lock_);
160 return destroyed_;
163 void TestContextProvider::OnLostContext() {
164 DCHECK(context_thread_checker_.CalledOnValidThread());
166 base::AutoLock lock(destroyed_lock_);
167 if (destroyed_)
168 return;
169 destroyed_ = true;
171 if (!lost_context_callback_.is_null())
172 base::ResetAndReturn(&lost_context_callback_).Run();
173 if (gr_context_)
174 gr_context_->abandonContext();
177 TestWebGraphicsContext3D* TestContextProvider::TestContext3d() {
178 DCHECK(bound_);
179 DCHECK(context_thread_checker_.CalledOnValidThread());
181 return context3d_.get();
184 TestWebGraphicsContext3D* TestContextProvider::UnboundTestContext3d() {
185 return context3d_.get();
188 void TestContextProvider::SetMemoryAllocation(
189 const ManagedMemoryPolicy& policy) {
190 if (memory_policy_changed_callback_.is_null())
191 return;
192 memory_policy_changed_callback_.Run(policy);
195 void TestContextProvider::SetLostContextCallback(
196 const LostContextCallback& cb) {
197 DCHECK(context_thread_checker_.CalledOnValidThread());
198 DCHECK(lost_context_callback_.is_null() || cb.is_null());
199 lost_context_callback_ = cb;
202 void TestContextProvider::SetMemoryPolicyChangedCallback(
203 const MemoryPolicyChangedCallback& cb) {
204 DCHECK(context_thread_checker_.CalledOnValidThread());
205 DCHECK(memory_policy_changed_callback_.is_null() || cb.is_null());
206 memory_policy_changed_callback_ = cb;
209 void TestContextProvider::SetMaxTransferBufferUsageBytes(
210 size_t max_transfer_buffer_usage_bytes) {
211 context3d_->SetMaxTransferBufferUsageBytes(max_transfer_buffer_usage_bytes);
214 } // namespace cc