[Media Router] Fix typo in unit test (&& instead of &)
[chromium-blink-merge.git] / cc / test / test_in_process_context_provider.cc
blobefbbeec88f7a17ab94ffed2b90359bd42237a64a
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 "cc/resources/platform_color.h"
9 #include "gpu/GLES2/gl2extchromium.h"
10 #include "gpu/command_buffer/client/gl_in_process_context.h"
11 #include "gpu/command_buffer/client/gles2_implementation.h"
12 #include "gpu/command_buffer/client/gles2_lib.h"
13 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
14 #include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h"
15 #include "third_party/khronos/GLES2/gl2.h"
16 #include "third_party/khronos/GLES2/gl2ext.h"
17 #include "third_party/skia/include/gpu/GrContext.h"
18 #include "third_party/skia/include/gpu/gl/GrGLInterface.h"
19 #include "ui/gfx/native_widget_types.h"
21 namespace cc {
23 // static
24 scoped_ptr<gpu::GLInProcessContext> CreateTestInProcessContext(
25 TestGpuMemoryBufferManager* gpu_memory_buffer_manager,
26 TestImageFactory* image_factory) {
27 const bool is_offscreen = true;
28 const bool share_resources = true;
29 gpu::gles2::ContextCreationAttribHelper attribs;
30 attribs.alpha_size = 8;
31 attribs.blue_size = 8;
32 attribs.green_size = 8;
33 attribs.red_size = 8;
34 attribs.depth_size = 24;
35 attribs.stencil_size = 8;
36 attribs.samples = 0;
37 attribs.sample_buffers = 0;
38 attribs.fail_if_major_perf_caveat = false;
39 attribs.bind_generates_resource = false;
40 gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu;
42 scoped_ptr<gpu::GLInProcessContext> context =
43 make_scoped_ptr(gpu::GLInProcessContext::Create(
44 NULL,
45 NULL,
46 is_offscreen,
47 gfx::kNullAcceleratedWidget,
48 gfx::Size(1, 1),
49 NULL,
50 share_resources,
51 attribs,
52 gpu_preference,
53 gpu::GLInProcessContextSharedMemoryLimits(),
54 gpu_memory_buffer_manager,
55 image_factory));
57 DCHECK(context);
58 return context.Pass();
61 scoped_ptr<gpu::GLInProcessContext> CreateTestInProcessContext() {
62 return CreateTestInProcessContext(nullptr, nullptr);
65 TestInProcessContextProvider::TestInProcessContextProvider()
66 : context_(CreateTestInProcessContext(&gpu_memory_buffer_manager_,
67 &image_factory_)) {
70 TestInProcessContextProvider::~TestInProcessContextProvider() {
73 bool TestInProcessContextProvider::BindToCurrentThread() { return true; }
75 gpu::gles2::GLES2Interface* TestInProcessContextProvider::ContextGL() {
76 return context_->GetImplementation();
79 gpu::ContextSupport* TestInProcessContextProvider::ContextSupport() {
80 return context_->GetImplementation();
83 namespace {
85 // Singleton used to initialize and terminate the gles2 library.
86 class GLES2Initializer {
87 public:
88 GLES2Initializer() { ::gles2::Initialize(); }
90 ~GLES2Initializer() { ::gles2::Terminate(); }
92 private:
93 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer);
96 static base::LazyInstance<GLES2Initializer> g_gles2_initializer =
97 LAZY_INSTANCE_INITIALIZER;
99 } // namespace
101 static void BindGrContextCallback(const GrGLInterface* interface) {
102 TestInProcessContextProvider* context_provider =
103 reinterpret_cast<TestInProcessContextProvider*>(interface->fCallbackData);
105 gles2::SetGLContext(context_provider->ContextGL());
108 class GrContext* TestInProcessContextProvider::GrContext() {
109 if (gr_context_)
110 return gr_context_.get();
112 // The GrGLInterface factory will make GL calls using the C GLES2 interface.
113 // Make sure the gles2 library is initialized first on exactly one thread.
114 g_gles2_initializer.Get();
115 gles2::SetGLContext(ContextGL());
117 skia::RefPtr<GrGLInterface> interface =
118 skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding());
119 interface->fCallback = BindGrContextCallback;
120 interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(this);
122 gr_context_ = skia::AdoptRef(GrContext::Create(
123 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get())));
125 return gr_context_.get();
128 void TestInProcessContextProvider::InvalidateGrContext(uint32_t state) {
129 if (gr_context_)
130 gr_context_.get()->resetContext(state);
133 void TestInProcessContextProvider::SetupLock() {
136 base::Lock* TestInProcessContextProvider::GetLock() {
137 return &context_lock_;
140 ContextProvider::Capabilities
141 TestInProcessContextProvider::ContextCapabilities() {
142 ContextProvider::Capabilities capabilities;
143 capabilities.gpu.image = true;
144 capabilities.gpu.texture_rectangle = true;
146 switch (PlatformColor::Format()) {
147 case PlatformColor::SOURCE_FORMAT_RGBA8:
148 capabilities.gpu.texture_format_bgra8888 = false;
149 break;
150 case PlatformColor::SOURCE_FORMAT_BGRA8:
151 capabilities.gpu.texture_format_bgra8888 = true;
152 break;
155 return capabilities;
158 void TestInProcessContextProvider::VerifyContexts() {}
160 void TestInProcessContextProvider::DeleteCachedResources() {
161 if (gr_context_)
162 gr_context_->freeGpuResources();
165 bool TestInProcessContextProvider::DestroyedOnMainThread() { return false; }
167 void TestInProcessContextProvider::SetLostContextCallback(
168 const LostContextCallback& lost_context_callback) {}
170 void TestInProcessContextProvider::SetMemoryPolicyChangedCallback(
171 const MemoryPolicyChangedCallback& memory_policy_changed_callback) {}
173 } // namespace cc