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 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
44 gpu::ImageFactory
* image_factory
,
45 gfx::AcceleratedWidget window
,
46 const std::string
& debug_name
) {
47 return new InProcessContextProvider(
48 attribs
, gpu_memory_buffer_manager
, image_factory
, window
, debug_name
);
52 scoped_refptr
<InProcessContextProvider
>
53 InProcessContextProvider::CreateOffscreen(
54 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
55 gpu::ImageFactory
* image_factory
) {
56 gpu::gles2::ContextCreationAttribHelper attribs
;
57 attribs
.alpha_size
= 8;
58 attribs
.blue_size
= 8;
59 attribs
.green_size
= 8;
61 attribs
.depth_size
= 0;
62 attribs
.stencil_size
= 8;
64 attribs
.sample_buffers
= 0;
65 attribs
.fail_if_major_perf_caveat
= false;
66 attribs
.bind_generates_resource
= false;
67 return new InProcessContextProvider(attribs
, gpu_memory_buffer_manager
,
69 gfx::kNullAcceleratedWidget
, "Offscreen");
72 InProcessContextProvider::InProcessContextProvider(
73 const gpu::gles2::ContextCreationAttribHelper
& attribs
,
74 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
75 gpu::ImageFactory
* image_factory
,
76 gfx::AcceleratedWidget window
,
77 const std::string
& debug_name
)
79 gpu_memory_buffer_manager_(gpu_memory_buffer_manager
),
80 image_factory_(image_factory
),
82 debug_name_(debug_name
),
84 DCHECK(main_thread_checker_
.CalledOnValidThread());
85 context_thread_checker_
.DetachFromThread();
88 InProcessContextProvider::~InProcessContextProvider() {
89 DCHECK(main_thread_checker_
.CalledOnValidThread() ||
90 context_thread_checker_
.CalledOnValidThread());
93 bool InProcessContextProvider::BindToCurrentThread() {
94 // This is called on the thread the context will be used.
95 DCHECK(context_thread_checker_
.CalledOnValidThread());
98 gfx::GpuPreference gpu_preference
= gfx::PreferDiscreteGpu
;
99 context_
.reset(gpu::GLInProcessContext::Create(
100 nullptr, /* service */
101 nullptr, /* surface */
102 !window_
, /* is_offscreen */
103 window_
, gfx::Size(1, 1), nullptr, /* share_context */
104 true, /* share_resources */
105 attribs_
, gpu_preference
, gpu::GLInProcessContextSharedMemoryLimits(),
106 gpu_memory_buffer_manager_
, image_factory_
));
111 context_
->SetContextLostCallback(base::Bind(
112 &InProcessContextProvider::OnLostContext
, base::Unretained(this)));
115 capabilities_
.gpu
= context_
->GetImplementation()->capabilities();
117 std::string unique_context_name
=
118 base::StringPrintf("%s-%p", debug_name_
.c_str(), context_
.get());
119 context_
->GetImplementation()->TraceBeginCHROMIUM(
120 "gpu_toplevel", unique_context_name
.c_str());
125 cc::ContextProvider::Capabilities
126 InProcessContextProvider::ContextCapabilities() {
127 DCHECK(context_thread_checker_
.CalledOnValidThread());
128 return capabilities_
;
131 gpu::gles2::GLES2Interface
* InProcessContextProvider::ContextGL() {
132 DCHECK(context_thread_checker_
.CalledOnValidThread());
134 return context_
->GetImplementation();
137 gpu::ContextSupport
* InProcessContextProvider::ContextSupport() {
138 DCHECK(context_thread_checker_
.CalledOnValidThread());
140 return context_
->GetImplementation();
143 static void BindGrContextCallback(const GrGLInterface
* interface
) {
144 cc::ContextProvider
* context_provider
=
145 reinterpret_cast<InProcessContextProvider
*>(interface
->fCallbackData
);
147 gles2::SetGLContext(context_provider
->ContextGL());
150 class GrContext
* InProcessContextProvider::GrContext() {
151 DCHECK(context_thread_checker_
.CalledOnValidThread());
154 return gr_context_
.get();
156 // The GrGLInterface factory will make GL calls using the C GLES2 interface.
157 // Make sure the gles2 library is initialized first on exactly one thread.
158 g_gles2_initializer
.Get();
159 gles2::SetGLContext(ContextGL());
161 skia::RefPtr
<GrGLInterface
> interface
=
162 skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding());
163 interface
->fCallback
= BindGrContextCallback
;
164 interface
->fCallbackData
= reinterpret_cast<GrGLInterfaceCallbackData
>(this);
166 gr_context_
= skia::AdoptRef(GrContext::Create(
167 kOpenGL_GrBackend
, reinterpret_cast<GrBackendContext
>(interface
.get())));
169 return gr_context_
.get();
172 void InProcessContextProvider::InvalidateGrContext(uint32_t state
) {
173 DCHECK(context_thread_checker_
.CalledOnValidThread());
176 gr_context_
.get()->resetContext(state
);
179 void InProcessContextProvider::SetupLock() {
182 base::Lock
* InProcessContextProvider::GetLock() {
183 return &context_lock_
;
186 void InProcessContextProvider::VerifyContexts() {
189 void InProcessContextProvider::DeleteCachedResources() {
190 DCHECK(context_thread_checker_
.CalledOnValidThread());
193 TRACE_EVENT_INSTANT0("gpu", "GrContext::freeGpuResources",
194 TRACE_EVENT_SCOPE_THREAD
);
195 gr_context_
->freeGpuResources();
199 bool InProcessContextProvider::DestroyedOnMainThread() {
200 DCHECK(main_thread_checker_
.CalledOnValidThread());
202 base::AutoLock
lock(destroyed_lock_
);
206 void InProcessContextProvider::SetLostContextCallback(
207 const LostContextCallback
& lost_context_callback
) {
208 lost_context_callback_
= lost_context_callback
;
211 void InProcessContextProvider::SetMemoryPolicyChangedCallback(
212 const MemoryPolicyChangedCallback
& memory_policy_changed_callback
) {
213 // There's no memory manager for the in-process implementation.
216 void InProcessContextProvider::OnLostContext() {
217 DCHECK(context_thread_checker_
.CalledOnValidThread());
219 base::AutoLock
lock(destroyed_lock_
);
224 if (!lost_context_callback_
.is_null())
225 base::ResetAndReturn(&lost_context_callback_
).Run();
227 gr_context_
->abandonContext();