1 // Copyright 2015 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 "blimp/client/compositor/blimp_context_provider.h"
8 #include "base/callback_helpers.h"
9 #include "base/lazy_instance.h"
10 #include "gpu/command_buffer/client/gl_in_process_context.h"
11 #include "gpu/command_buffer/client/gles2_implementation.h"
12 #include "gpu/command_buffer/client/gles2_lib.h"
13 #include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h"
14 #include "third_party/skia/include/gpu/GrContext.h"
15 #include "third_party/skia/include/gpu/gl/GrGLInterface.h"
19 // Singleton used to initialize and terminate the gles2 library.
20 class GLES2Initializer
{
22 GLES2Initializer() { gles2::Initialize(); }
24 ~GLES2Initializer() { gles2::Terminate(); }
27 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer
);
30 base::LazyInstance
<GLES2Initializer
> g_gles2_initializer
=
31 LAZY_INSTANCE_INITIALIZER
;
33 static void BindGrContextCallback(const GrGLInterface
* interface
) {
34 blimp::BlimpContextProvider
* context_provider
=
35 reinterpret_cast<blimp::BlimpContextProvider
*>(interface
->fCallbackData
);
37 gles2::SetGLContext(context_provider
->ContextGL());
45 scoped_refptr
<BlimpContextProvider
> BlimpContextProvider::Create(
46 gfx::AcceleratedWidget widget
) {
47 return new BlimpContextProvider(widget
);
50 BlimpContextProvider::BlimpContextProvider(gfx::AcceleratedWidget widget
) {
51 context_thread_checker_
.DetachFromThread();
53 gpu::gles2::ContextCreationAttribHelper attribs_for_gles2
;
54 attribs_for_gles2
.alpha_size
= 8;
55 attribs_for_gles2
.depth_size
= 0;
56 attribs_for_gles2
.stencil_size
= 0;
57 attribs_for_gles2
.samples
= 0;
58 attribs_for_gles2
.sample_buffers
= 0;
59 attribs_for_gles2
.fail_if_major_perf_caveat
= false;
60 attribs_for_gles2
.bind_generates_resource
= false;
61 attribs_for_gles2
.webgl_version
= 0;
62 attribs_for_gles2
.lose_context_when_out_of_memory
= true;
64 context_
.reset(gpu::GLInProcessContext::Create(
65 nullptr /* service */, nullptr /* surface */, false /* is_offscreen */,
66 widget
, gfx::Size(1, 1), nullptr /* share_context */,
67 false /* share_resources */, attribs_for_gles2
, gfx::PreferDiscreteGpu
,
68 gpu::GLInProcessContextSharedMemoryLimits(),
69 nullptr /* gpu_memory_buffer_manager */, nullptr /* memory_limits */));
70 context_
->SetContextLostCallback(
71 base::Bind(&BlimpContextProvider::OnLostContext
, base::Unretained(this)));
74 BlimpContextProvider::~BlimpContextProvider() {
75 DCHECK(main_thread_checker_
.CalledOnValidThread() ||
76 context_thread_checker_
.CalledOnValidThread());
79 bool BlimpContextProvider::BindToCurrentThread() {
80 DCHECK(context_thread_checker_
.CalledOnValidThread());
81 capabilities_
.gpu
= context_
->GetImplementation()->capabilities();
82 capabilities_
.gpu
.image
= true;
86 void BlimpContextProvider::DetachFromThread() {
87 context_thread_checker_
.DetachFromThread();
90 cc::ContextProvider::Capabilities
BlimpContextProvider::ContextCapabilities() {
91 DCHECK(context_thread_checker_
.CalledOnValidThread());
95 gpu::gles2::GLES2Interface
* BlimpContextProvider::ContextGL() {
96 DCHECK(context_thread_checker_
.CalledOnValidThread());
97 return context_
->GetImplementation();
100 gpu::ContextSupport
* BlimpContextProvider::ContextSupport() {
101 DCHECK(context_thread_checker_
.CalledOnValidThread());
102 return context_
->GetImplementation();
105 class GrContext
* BlimpContextProvider::GrContext() {
106 DCHECK(context_thread_checker_
.CalledOnValidThread());
109 return gr_context_
.get();
111 // The GrGLInterface factory will make GL calls using the C GLES2 interface.
112 // Make sure the gles2 library is initialized first on exactly one thread.
113 g_gles2_initializer
.Get();
114 gles2::SetGLContext(ContextGL());
116 skia::RefPtr
<GrGLInterface
> interface
=
117 skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding());
118 interface
->fCallback
= BindGrContextCallback
;
119 interface
->fCallbackData
= reinterpret_cast<GrGLInterfaceCallbackData
>(this);
121 gr_context_
= skia::AdoptRef(GrContext::Create(
122 kOpenGL_GrBackend
, reinterpret_cast<GrBackendContext
>(interface
.get())));
124 return gr_context_
.get();
127 void BlimpContextProvider::InvalidateGrContext(uint32_t state
) {
128 DCHECK(context_thread_checker_
.CalledOnValidThread());
131 gr_context_
.get()->resetContext(state
);
134 void BlimpContextProvider::SetupLock() {
135 context_
->SetLock(&context_lock_
);
138 base::Lock
* BlimpContextProvider::GetLock() {
139 return &context_lock_
;
142 void BlimpContextProvider::VerifyContexts() {
143 DCHECK(context_thread_checker_
.CalledOnValidThread());
146 void BlimpContextProvider::DeleteCachedResources() {
147 DCHECK(context_thread_checker_
.CalledOnValidThread());
150 gr_context_
->freeGpuResources();
153 bool BlimpContextProvider::DestroyedOnMainThread() {
154 DCHECK(main_thread_checker_
.CalledOnValidThread());
156 base::AutoLock
lock(destroyed_lock_
);
160 void BlimpContextProvider::SetLostContextCallback(
161 const LostContextCallback
& lost_context_callback
) {
162 DCHECK(context_thread_checker_
.CalledOnValidThread());
163 lost_context_callback_
= lost_context_callback
;
166 void BlimpContextProvider::SetMemoryPolicyChangedCallback(
167 const MemoryPolicyChangedCallback
& memory_policy_changed_callback
) {
168 // There's no memory manager for the in-process implementation.
169 // TODO(dtrainor): Figure out if we need a memory manager for Blimp.
172 void BlimpContextProvider::OnLostContext() {
173 DCHECK(context_thread_checker_
.CalledOnValidThread());
176 base::AutoLock
lock(destroyed_lock_
);
182 if (!lost_context_callback_
.is_null())
183 base::ResetAndReturn(&lost_context_callback_
).Run();
185 gr_context_
->abandonContext();