Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / cc / test / test_in_process_context_provider.cc
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.
4
5 #include "cc/test/test_in_process_context_provider.h"
6
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/skia_bindings/gl_bindings_skia_cmd_buffer.h"
13 #include "third_party/khronos/GLES2/gl2.h"
14 #include "third_party/khronos/GLES2/gl2ext.h"
15 #include "third_party/skia/include/gpu/GrContext.h"
16 #include "third_party/skia/include/gpu/gl/GrGLInterface.h"
17 #include "ui/gfx/native_widget_types.h"
18
19 namespace cc {
20
21 // static
22 scoped_ptr<gpu::GLInProcessContext> CreateTestInProcessContext() {
23   const bool is_offscreen = true;
24   const bool share_resources = true;
25   gpu::GLInProcessContextAttribs attribs;
26   attribs.alpha_size = 8;
27   attribs.blue_size = 8;
28   attribs.green_size = 8;
29   attribs.red_size = 8;
30   attribs.depth_size = 24;
31   attribs.stencil_size = 8;
32   attribs.samples = 0;
33   attribs.sample_buffers = 0;
34   attribs.fail_if_major_perf_caveat = false;
35   gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu;
36
37   scoped_ptr<gpu::GLInProcessContext> context = make_scoped_ptr(
38       gpu::GLInProcessContext::CreateContext(is_offscreen,
39                                              gfx::AcceleratedWidget(),
40                                              gfx::Size(1, 1),
41                                              share_resources,
42                                              attribs,
43                                              gpu_preference));
44   DCHECK(context);
45   return context.Pass();
46 }
47
48 TestInProcessContextProvider::TestInProcessContextProvider()
49     : context_(CreateTestInProcessContext()) {}
50
51 TestInProcessContextProvider::~TestInProcessContextProvider() {
52 }
53
54 bool TestInProcessContextProvider::BindToCurrentThread() { return true; }
55
56 gpu::gles2::GLES2Interface* TestInProcessContextProvider::ContextGL() {
57   return context_->GetImplementation();
58 }
59
60 gpu::ContextSupport* TestInProcessContextProvider::ContextSupport() {
61   return context_->GetImplementation();
62 }
63
64 namespace {
65
66 // Singleton used to initialize and terminate the gles2 library.
67 class GLES2Initializer {
68  public:
69   GLES2Initializer() { ::gles2::Initialize(); }
70
71   ~GLES2Initializer() { ::gles2::Terminate(); }
72
73  private:
74   DISALLOW_COPY_AND_ASSIGN(GLES2Initializer);
75 };
76
77 static base::LazyInstance<GLES2Initializer> g_gles2_initializer =
78     LAZY_INSTANCE_INITIALIZER;
79
80 }  // namespace
81
82 static void BindGrContextCallback(const GrGLInterface* interface) {
83   TestInProcessContextProvider* context_provider =
84       reinterpret_cast<TestInProcessContextProvider*>(interface->fCallbackData);
85
86   gles2::SetGLContext(context_provider->ContextGL());
87 }
88
89 class GrContext* TestInProcessContextProvider::GrContext() {
90   if (gr_context_)
91     return gr_context_.get();
92
93   // The GrGLInterface factory will make GL calls using the C GLES2 interface.
94   // Make sure the gles2 library is initialized first on exactly one thread.
95   g_gles2_initializer.Get();
96   gles2::SetGLContext(ContextGL());
97
98   skia::RefPtr<GrGLInterface> interface =
99       skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding());
100   interface->fCallback = BindGrContextCallback;
101   interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(this);
102
103   gr_context_ = skia::AdoptRef(GrContext::Create(
104       kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get())));
105
106   return gr_context_.get();
107 }
108
109 ContextProvider::Capabilities
110 TestInProcessContextProvider::ContextCapabilities() {
111   return ContextProvider::Capabilities();
112 }
113
114 bool TestInProcessContextProvider::IsContextLost() { return false; }
115
116 void TestInProcessContextProvider::VerifyContexts() {}
117
118 void TestInProcessContextProvider::DeleteCachedResources() {
119   if (gr_context_)
120     gr_context_->freeGpuResources();
121 }
122
123 bool TestInProcessContextProvider::DestroyedOnMainThread() { return false; }
124
125 void TestInProcessContextProvider::SetLostContextCallback(
126     const LostContextCallback& lost_context_callback) {}
127
128 void TestInProcessContextProvider::SetMemoryPolicyChangedCallback(
129     const MemoryPolicyChangedCallback& memory_policy_changed_callback) {}
130
131 }  // namespace cc