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_in_process_context_provider.h"
7 #include "base/lazy_instance.h"
8 #include "gpu/GLES2/gl2extchromium.h"
9 #include "gpu/command_buffer/client/gl_in_process_context.h"
10 #include "gpu/command_buffer/client/gles2_implementation.h"
11 #include "gpu/command_buffer/client/gles2_lib.h"
12 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
13 #include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h"
14 #include "third_party/khronos/GLES2/gl2.h"
15 #include "third_party/khronos/GLES2/gl2ext.h"
16 #include "third_party/skia/include/gpu/GrContext.h"
17 #include "third_party/skia/include/gpu/gl/GrGLInterface.h"
18 #include "ui/gfx/native_widget_types.h"
23 scoped_ptr
<gpu::GLInProcessContext
> CreateTestInProcessContext(
24 TestGpuMemoryBufferManager
* gpu_memory_buffer_manager
,
25 TestImageFactory
* image_factory
) {
26 const bool is_offscreen
= true;
27 const bool share_resources
= true;
28 gpu::gles2::ContextCreationAttribHelper attribs
;
29 attribs
.alpha_size
= 8;
30 attribs
.blue_size
= 8;
31 attribs
.green_size
= 8;
33 attribs
.depth_size
= 24;
34 attribs
.stencil_size
= 8;
36 attribs
.sample_buffers
= 0;
37 attribs
.fail_if_major_perf_caveat
= false;
38 attribs
.bind_generates_resource
= false;
39 gfx::GpuPreference gpu_preference
= gfx::PreferDiscreteGpu
;
41 scoped_ptr
<gpu::GLInProcessContext
> context
=
42 make_scoped_ptr(gpu::GLInProcessContext::Create(
46 gfx::kNullAcceleratedWidget
,
52 gpu::GLInProcessContextSharedMemoryLimits(),
53 gpu_memory_buffer_manager
,
57 return context
.Pass();
60 scoped_ptr
<gpu::GLInProcessContext
> CreateTestInProcessContext() {
61 return CreateTestInProcessContext(nullptr, nullptr);
64 TestInProcessContextProvider::TestInProcessContextProvider()
65 : context_(CreateTestInProcessContext(&gpu_memory_buffer_manager_
,
69 TestInProcessContextProvider::~TestInProcessContextProvider() {
72 bool TestInProcessContextProvider::BindToCurrentThread() { return true; }
74 gpu::gles2::GLES2Interface
* TestInProcessContextProvider::ContextGL() {
75 return context_
->GetImplementation();
78 gpu::ContextSupport
* TestInProcessContextProvider::ContextSupport() {
79 return context_
->GetImplementation();
84 // Singleton used to initialize and terminate the gles2 library.
85 class GLES2Initializer
{
87 GLES2Initializer() { ::gles2::Initialize(); }
89 ~GLES2Initializer() { ::gles2::Terminate(); }
92 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer
);
95 static base::LazyInstance
<GLES2Initializer
> g_gles2_initializer
=
96 LAZY_INSTANCE_INITIALIZER
;
100 static void BindGrContextCallback(const GrGLInterface
* interface
) {
101 TestInProcessContextProvider
* context_provider
=
102 reinterpret_cast<TestInProcessContextProvider
*>(interface
->fCallbackData
);
104 gles2::SetGLContext(context_provider
->ContextGL());
107 class GrContext
* TestInProcessContextProvider::GrContext() {
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 TestInProcessContextProvider::SetupLock() {
130 base::Lock
* TestInProcessContextProvider::GetLock() {
131 return &context_lock_
;
134 ContextProvider::Capabilities
135 TestInProcessContextProvider::ContextCapabilities() {
136 ContextProvider::Capabilities capabilities
;
137 capabilities
.gpu
.image
= true;
138 capabilities
.gpu
.texture_rectangle
= true;
143 bool TestInProcessContextProvider::IsContextLost() { return false; }
145 void TestInProcessContextProvider::VerifyContexts() {}
147 void TestInProcessContextProvider::DeleteCachedResources() {
149 gr_context_
->freeGpuResources();
152 bool TestInProcessContextProvider::DestroyedOnMainThread() { return false; }
154 void TestInProcessContextProvider::SetLostContextCallback(
155 const LostContextCallback
& lost_context_callback
) {}
157 void TestInProcessContextProvider::SetMemoryPolicyChangedCallback(
158 const MemoryPolicyChangedCallback
& memory_policy_changed_callback
) {}