Home | History | Annotate | Download | only in test
      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::Create(NULL,
     39                                       NULL,
     40                                       is_offscreen,
     41                                       gfx::kNullAcceleratedWidget,
     42                                       gfx::Size(1, 1),
     43                                       NULL,
     44                                       share_resources,
     45                                       attribs,
     46                                       gpu_preference));
     47 
     48   DCHECK(context);
     49   return context.Pass();
     50 }
     51 
     52 TestInProcessContextProvider::TestInProcessContextProvider()
     53     : context_(CreateTestInProcessContext()) {}
     54 
     55 TestInProcessContextProvider::~TestInProcessContextProvider() {
     56 }
     57 
     58 bool TestInProcessContextProvider::BindToCurrentThread() { return true; }
     59 
     60 gpu::gles2::GLES2Interface* TestInProcessContextProvider::ContextGL() {
     61   return context_->GetImplementation();
     62 }
     63 
     64 gpu::ContextSupport* TestInProcessContextProvider::ContextSupport() {
     65   return context_->GetImplementation();
     66 }
     67 
     68 namespace {
     69 
     70 // Singleton used to initialize and terminate the gles2 library.
     71 class GLES2Initializer {
     72  public:
     73   GLES2Initializer() { ::gles2::Initialize(); }
     74 
     75   ~GLES2Initializer() { ::gles2::Terminate(); }
     76 
     77  private:
     78   DISALLOW_COPY_AND_ASSIGN(GLES2Initializer);
     79 };
     80 
     81 static base::LazyInstance<GLES2Initializer> g_gles2_initializer =
     82     LAZY_INSTANCE_INITIALIZER;
     83 
     84 }  // namespace
     85 
     86 static void BindGrContextCallback(const GrGLInterface* interface) {
     87   TestInProcessContextProvider* context_provider =
     88       reinterpret_cast<TestInProcessContextProvider*>(interface->fCallbackData);
     89 
     90   gles2::SetGLContext(context_provider->ContextGL());
     91 }
     92 
     93 class GrContext* TestInProcessContextProvider::GrContext() {
     94   if (gr_context_)
     95     return gr_context_.get();
     96 
     97   // The GrGLInterface factory will make GL calls using the C GLES2 interface.
     98   // Make sure the gles2 library is initialized first on exactly one thread.
     99   g_gles2_initializer.Get();
    100   gles2::SetGLContext(ContextGL());
    101 
    102   skia::RefPtr<GrGLInterface> interface =
    103       skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding());
    104   interface->fCallback = BindGrContextCallback;
    105   interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(this);
    106 
    107   gr_context_ = skia::AdoptRef(GrContext::Create(
    108       kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get())));
    109 
    110   return gr_context_.get();
    111 }
    112 
    113 ContextProvider::Capabilities
    114 TestInProcessContextProvider::ContextCapabilities() {
    115   return ContextProvider::Capabilities();
    116 }
    117 
    118 bool TestInProcessContextProvider::IsContextLost() { return false; }
    119 
    120 void TestInProcessContextProvider::VerifyContexts() {}
    121 
    122 void TestInProcessContextProvider::DeleteCachedResources() {
    123   if (gr_context_)
    124     gr_context_->freeGpuResources();
    125 }
    126 
    127 bool TestInProcessContextProvider::DestroyedOnMainThread() { return false; }
    128 
    129 void TestInProcessContextProvider::SetLostContextCallback(
    130     const LostContextCallback& lost_context_callback) {}
    131 
    132 void TestInProcessContextProvider::SetMemoryPolicyChangedCallback(
    133     const MemoryPolicyChangedCallback& memory_policy_changed_callback) {}
    134 
    135 }  // namespace cc
    136