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 const bool is_offscreen
= true;
25 const bool share_resources
= true;
26 gpu::gles2::ContextCreationAttribHelper attribs
;
27 attribs
.alpha_size
= 8;
28 attribs
.blue_size
= 8;
29 attribs
.green_size
= 8;
31 attribs
.depth_size
= 24;
32 attribs
.stencil_size
= 8;
34 attribs
.sample_buffers
= 0;
35 attribs
.fail_if_major_perf_caveat
= false;
36 attribs
.bind_generates_resource
= false;
37 gfx::GpuPreference gpu_preference
= gfx::PreferDiscreteGpu
;
39 scoped_ptr
<gpu::GLInProcessContext
> context
=
40 make_scoped_ptr(gpu::GLInProcessContext::Create(
44 gfx::kNullAcceleratedWidget
,
50 gpu::GLInProcessContextSharedMemoryLimits()));
53 return context
.Pass();
56 TestInProcessContextProvider::TestInProcessContextProvider()
57 : context_(CreateTestInProcessContext()) {}
59 TestInProcessContextProvider::~TestInProcessContextProvider() {
62 bool TestInProcessContextProvider::BindToCurrentThread() { return true; }
64 gpu::gles2::GLES2Interface
* TestInProcessContextProvider::ContextGL() {
65 return context_
->GetImplementation();
68 gpu::ContextSupport
* TestInProcessContextProvider::ContextSupport() {
69 return context_
->GetImplementation();
74 // Singleton used to initialize and terminate the gles2 library.
75 class GLES2Initializer
{
77 GLES2Initializer() { ::gles2::Initialize(); }
79 ~GLES2Initializer() { ::gles2::Terminate(); }
82 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer
);
85 static base::LazyInstance
<GLES2Initializer
> g_gles2_initializer
=
86 LAZY_INSTANCE_INITIALIZER
;
90 static void BindGrContextCallback(const GrGLInterface
* interface
) {
91 TestInProcessContextProvider
* context_provider
=
92 reinterpret_cast<TestInProcessContextProvider
*>(interface
->fCallbackData
);
94 gles2::SetGLContext(context_provider
->ContextGL());
97 class GrContext
* TestInProcessContextProvider::GrContext() {
99 return gr_context_
.get();
101 // The GrGLInterface factory will make GL calls using the C GLES2 interface.
102 // Make sure the gles2 library is initialized first on exactly one thread.
103 g_gles2_initializer
.Get();
104 gles2::SetGLContext(ContextGL());
106 skia::RefPtr
<GrGLInterface
> interface
=
107 skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding());
108 interface
->fCallback
= BindGrContextCallback
;
109 interface
->fCallbackData
= reinterpret_cast<GrGLInterfaceCallbackData
>(this);
111 gr_context_
= skia::AdoptRef(GrContext::Create(
112 kOpenGL_GrBackend
, reinterpret_cast<GrBackendContext
>(interface
.get())));
114 return gr_context_
.get();
117 ContextProvider::Capabilities
118 TestInProcessContextProvider::ContextCapabilities() {
119 return ContextProvider::Capabilities();
122 bool TestInProcessContextProvider::IsContextLost() { return false; }
124 void TestInProcessContextProvider::VerifyContexts() {}
126 void TestInProcessContextProvider::DeleteCachedResources() {
128 gr_context_
->freeGpuResources();
131 bool TestInProcessContextProvider::DestroyedOnMainThread() { return false; }
133 void TestInProcessContextProvider::SetLostContextCallback(
134 const LostContextCallback
& lost_context_callback
) {}
136 void TestInProcessContextProvider::SetMemoryPolicyChangedCallback(
137 const MemoryPolicyChangedCallback
& memory_policy_changed_callback
) {}