1 // Copyright (c) 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 "ui/compositor/test/in_process_context_provider.h"
8 #include "base/callback_helpers.h"
9 #include "base/lazy_instance.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/trace_event/trace_event.h"
12 #include "cc/output/managed_memory_policy.h"
13 #include "gpu/command_buffer/client/gl_in_process_context.h"
14 #include "gpu/command_buffer/client/gles2_implementation.h"
15 #include "gpu/command_buffer/client/gles2_lib.h"
16 #include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h"
17 #include "third_party/skia/include/gpu/GrContext.h"
18 #include "third_party/skia/include/gpu/gl/GrGLInterface.h"
24 // Singleton used to initialize and terminate the gles2 library.
25 class GLES2Initializer
{
27 GLES2Initializer() { gles2::Initialize(); }
29 ~GLES2Initializer() { gles2::Terminate(); }
32 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer
);
35 base::LazyInstance
<GLES2Initializer
> g_gles2_initializer
=
36 LAZY_INSTANCE_INITIALIZER
;
41 scoped_refptr
<InProcessContextProvider
> InProcessContextProvider::Create(
42 const gpu::gles2::ContextCreationAttribHelper
& attribs
,
43 bool lose_context_when_out_of_memory
,
44 gfx::AcceleratedWidget window
,
45 const std::string
& debug_name
) {
46 return new InProcessContextProvider(
47 attribs
, lose_context_when_out_of_memory
, window
, debug_name
);
51 scoped_refptr
<InProcessContextProvider
>
52 InProcessContextProvider::CreateOffscreen(
53 bool lose_context_when_out_of_memory
) {
54 gpu::gles2::ContextCreationAttribHelper attribs
;
55 attribs
.alpha_size
= 8;
56 attribs
.blue_size
= 8;
57 attribs
.green_size
= 8;
59 attribs
.depth_size
= 0;
60 attribs
.stencil_size
= 8;
62 attribs
.sample_buffers
= 0;
63 attribs
.fail_if_major_perf_caveat
= false;
64 attribs
.bind_generates_resource
= false;
65 return new InProcessContextProvider(
66 attribs
, lose_context_when_out_of_memory
, gfx::kNullAcceleratedWidget
,
70 InProcessContextProvider::InProcessContextProvider(
71 const gpu::gles2::ContextCreationAttribHelper
& attribs
,
72 bool lose_context_when_out_of_memory
,
73 gfx::AcceleratedWidget window
,
74 const std::string
& debug_name
)
76 lose_context_when_out_of_memory_(lose_context_when_out_of_memory
),
78 debug_name_(debug_name
),
80 DCHECK(main_thread_checker_
.CalledOnValidThread());
81 context_thread_checker_
.DetachFromThread();
84 InProcessContextProvider::~InProcessContextProvider() {
85 DCHECK(main_thread_checker_
.CalledOnValidThread() ||
86 context_thread_checker_
.CalledOnValidThread());
89 bool InProcessContextProvider::BindToCurrentThread() {
90 // This is called on the thread the context will be used.
91 DCHECK(context_thread_checker_
.CalledOnValidThread());
94 gfx::GpuPreference gpu_preference
= gfx::PreferDiscreteGpu
;
95 context_
.reset(gpu::GLInProcessContext::Create(
96 nullptr, /* service */
97 nullptr, /* surface */
98 !window_
, /* is_offscreen */
99 window_
, gfx::Size(1, 1), nullptr, /* share_context */
100 true, /* share_resources */
101 attribs_
, gpu_preference
, gpu::GLInProcessContextSharedMemoryLimits(),
107 context_
->SetContextLostCallback(base::Bind(
108 &InProcessContextProvider::OnLostContext
, base::Unretained(this)));
111 capabilities_
.gpu
= context_
->GetImplementation()->capabilities();
113 std::string unique_context_name
=
114 base::StringPrintf("%s-%p", debug_name_
.c_str(), context_
.get());
115 context_
->GetImplementation()->TraceBeginCHROMIUM(
116 "gpu_toplevel", unique_context_name
.c_str());
121 cc::ContextProvider::Capabilities
122 InProcessContextProvider::ContextCapabilities() {
123 DCHECK(context_thread_checker_
.CalledOnValidThread());
124 return capabilities_
;
127 gpu::gles2::GLES2Interface
* InProcessContextProvider::ContextGL() {
128 DCHECK(context_thread_checker_
.CalledOnValidThread());
130 return context_
->GetImplementation();
133 gpu::ContextSupport
* InProcessContextProvider::ContextSupport() {
134 DCHECK(context_thread_checker_
.CalledOnValidThread());
136 return context_
->GetImplementation();
139 static void BindGrContextCallback(const GrGLInterface
* interface
) {
140 cc::ContextProvider
* context_provider
=
141 reinterpret_cast<InProcessContextProvider
*>(interface
->fCallbackData
);
143 gles2::SetGLContext(context_provider
->ContextGL());
146 class GrContext
* InProcessContextProvider::GrContext() {
147 DCHECK(context_thread_checker_
.CalledOnValidThread());
150 return gr_context_
.get();
152 // The GrGLInterface factory will make GL calls using the C GLES2 interface.
153 // Make sure the gles2 library is initialized first on exactly one thread.
154 g_gles2_initializer
.Get();
155 gles2::SetGLContext(ContextGL());
157 skia::RefPtr
<GrGLInterface
> interface
=
158 skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding());
159 interface
->fCallback
= BindGrContextCallback
;
160 interface
->fCallbackData
= reinterpret_cast<GrGLInterfaceCallbackData
>(this);
162 gr_context_
= skia::AdoptRef(GrContext::Create(
163 kOpenGL_GrBackend
, reinterpret_cast<GrBackendContext
>(interface
.get())));
165 return gr_context_
.get();
168 void InProcessContextProvider::SetupLock() {
171 base::Lock
* InProcessContextProvider::GetLock() {
172 return &context_lock_
;
175 bool InProcessContextProvider::IsContextLost() {
176 DCHECK(context_thread_checker_
.CalledOnValidThread());
178 base::AutoLock
lock(destroyed_lock_
);
182 void InProcessContextProvider::VerifyContexts() {
185 void InProcessContextProvider::DeleteCachedResources() {
186 DCHECK(context_thread_checker_
.CalledOnValidThread());
189 TRACE_EVENT_INSTANT0("gpu", "GrContext::freeGpuResources",
190 TRACE_EVENT_SCOPE_THREAD
);
191 gr_context_
->freeGpuResources();
195 bool InProcessContextProvider::DestroyedOnMainThread() {
196 DCHECK(main_thread_checker_
.CalledOnValidThread());
198 base::AutoLock
lock(destroyed_lock_
);
202 void InProcessContextProvider::SetLostContextCallback(
203 const LostContextCallback
& lost_context_callback
) {
204 lost_context_callback_
= lost_context_callback
;
207 void InProcessContextProvider::SetMemoryPolicyChangedCallback(
208 const MemoryPolicyChangedCallback
& memory_policy_changed_callback
) {
209 // There's no memory manager for the in-process implementation.
212 void InProcessContextProvider::OnLostContext() {
213 DCHECK(context_thread_checker_
.CalledOnValidThread());
215 base::AutoLock
lock(destroyed_lock_
);
220 if (!lost_context_callback_
.is_null())
221 base::ResetAndReturn(&lost_context_callback_
).Run();
223 gr_context_
->abandonContext();