OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "blimp/client/compositor/blimp_context_provider.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" |
| 9 #include "base/lazy_instance.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/skia_bindings/gl_bindings_skia_cmd_buffer.h" |
| 14 #include "third_party/skia/include/gpu/GrContext.h" |
| 15 #include "third_party/skia/include/gpu/gl/GrGLInterface.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 // Singleton used to initialize and terminate the gles2 library. |
| 20 class GLES2Initializer { |
| 21 public: |
| 22 GLES2Initializer() { gles2::Initialize(); } |
| 23 |
| 24 ~GLES2Initializer() { gles2::Terminate(); } |
| 25 |
| 26 private: |
| 27 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer); |
| 28 }; |
| 29 |
| 30 base::LazyInstance<GLES2Initializer> g_gles2_initializer = |
| 31 LAZY_INSTANCE_INITIALIZER; |
| 32 |
| 33 static void BindGrContextCallback(const GrGLInterface* interface) { |
| 34 blimp::BlimpContextProvider* context_provider = |
| 35 reinterpret_cast<blimp::BlimpContextProvider*>(interface->fCallbackData); |
| 36 |
| 37 gles2::SetGLContext(context_provider->ContextGL()); |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
| 42 namespace blimp { |
| 43 |
| 44 // static |
| 45 scoped_refptr<BlimpContextProvider> BlimpContextProvider::Create( |
| 46 gfx::AcceleratedWidget widget) { |
| 47 return new BlimpContextProvider(widget); |
| 48 } |
| 49 |
| 50 BlimpContextProvider::BlimpContextProvider(gfx::AcceleratedWidget widget) { |
| 51 DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 52 context_thread_checker_.DetachFromThread(); |
| 53 |
| 54 gpu::gles2::ContextCreationAttribHelper attribs_for_gles2; |
| 55 attribs_for_gles2.alpha_size = 8; |
| 56 attribs_for_gles2.depth_size = 0; |
| 57 attribs_for_gles2.stencil_size = 0; |
| 58 attribs_for_gles2.samples = 0; |
| 59 attribs_for_gles2.sample_buffers = 0; |
| 60 attribs_for_gles2.fail_if_major_perf_caveat = false; |
| 61 attribs_for_gles2.bind_generates_resource = false; |
| 62 attribs_for_gles2.webgl_version = 0; |
| 63 attribs_for_gles2.lose_context_when_out_of_memory = true; |
| 64 |
| 65 context_.reset(gpu::GLInProcessContext::Create( |
| 66 nullptr, nullptr, false, widget, gfx::Size(1, 1), |
| 67 NULL /* share_context */, false /* share_resources */, attribs_for_gles2, |
| 68 gfx::PreferDiscreteGpu, gpu::GLInProcessContextSharedMemoryLimits(), |
| 69 nullptr, nullptr)); |
| 70 |
| 71 context_->SetContextLostCallback( |
| 72 base::Bind(&BlimpContextProvider::OnLostContext, base::Unretained(this))); |
| 73 capabilities_.gpu = context_->GetImplementation()->capabilities(); |
| 74 capabilities_.gpu.image = true; |
| 75 } |
| 76 |
| 77 BlimpContextProvider::~BlimpContextProvider() { |
| 78 DCHECK(main_thread_checker_.CalledOnValidThread() || |
| 79 context_thread_checker_.CalledOnValidThread()); |
| 80 } |
| 81 |
| 82 void BlimpContextProvider::OnLostContext() { |
| 83 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 84 |
| 85 { |
| 86 base::AutoLock lock(destroyed_lock_); |
| 87 if (destroyed_) |
| 88 return; |
| 89 destroyed_ = true; |
| 90 } |
| 91 |
| 92 if (!lost_context_callback_.is_null()) |
| 93 base::ResetAndReturn(&lost_context_callback_).Run(); |
| 94 if (gr_context_) |
| 95 gr_context_->abandonContext(); |
| 96 } |
| 97 |
| 98 bool BlimpContextProvider::BindToCurrentThread() { |
| 99 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 100 capabilities_.gpu = context_->GetImplementation()->capabilities(); |
| 101 capabilities_.gpu.image = true; |
| 102 return true; |
| 103 } |
| 104 |
| 105 void BlimpContextProvider::DetachFromThread() { |
| 106 context_thread_checker_.DetachFromThread(); |
| 107 } |
| 108 |
| 109 cc::ContextProvider::Capabilities BlimpContextProvider::ContextCapabilities() { |
| 110 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 111 return capabilities_; |
| 112 } |
| 113 |
| 114 gpu::gles2::GLES2Interface* BlimpContextProvider::ContextGL() { |
| 115 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 116 return context_->GetImplementation(); |
| 117 } |
| 118 |
| 119 gpu::ContextSupport* BlimpContextProvider::ContextSupport() { |
| 120 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 121 return context_->GetImplementation(); |
| 122 } |
| 123 |
| 124 class GrContext* BlimpContextProvider::GrContext() { |
| 125 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 126 |
| 127 if (gr_context_) |
| 128 return gr_context_.get(); |
| 129 |
| 130 // The GrGLInterface factory will make GL calls using the C GLES2 interface. |
| 131 // Make sure the gles2 library is initialized first on exactly one thread. |
| 132 g_gles2_initializer.Get(); |
| 133 gles2::SetGLContext(ContextGL()); |
| 134 |
| 135 skia::RefPtr<GrGLInterface> interface = |
| 136 skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding()); |
| 137 interface->fCallback = BindGrContextCallback; |
| 138 interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(this); |
| 139 |
| 140 gr_context_ = skia::AdoptRef(GrContext::Create( |
| 141 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get()))); |
| 142 |
| 143 return gr_context_.get(); |
| 144 } |
| 145 |
| 146 void BlimpContextProvider::InvalidateGrContext(uint32_t state) { |
| 147 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 148 |
| 149 if (gr_context_) |
| 150 gr_context_.get()->resetContext(state); |
| 151 } |
| 152 |
| 153 void BlimpContextProvider::SetupLock() { |
| 154 context_->SetLock(&context_lock_); |
| 155 } |
| 156 |
| 157 base::Lock* BlimpContextProvider::GetLock() { |
| 158 return &context_lock_; |
| 159 } |
| 160 |
| 161 void BlimpContextProvider::VerifyContexts() { |
| 162 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 163 } |
| 164 |
| 165 void BlimpContextProvider::DeleteCachedResources() { |
| 166 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 167 |
| 168 if (gr_context_) |
| 169 gr_context_->freeGpuResources(); |
| 170 } |
| 171 |
| 172 bool BlimpContextProvider::DestroyedOnMainThread() { |
| 173 DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 174 |
| 175 base::AutoLock lock(destroyed_lock_); |
| 176 return destroyed_; |
| 177 } |
| 178 |
| 179 void BlimpContextProvider::SetLostContextCallback( |
| 180 const LostContextCallback& lost_context_callback) { |
| 181 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 182 lost_context_callback_ = lost_context_callback; |
| 183 } |
| 184 |
| 185 void BlimpContextProvider::SetMemoryPolicyChangedCallback( |
| 186 const MemoryPolicyChangedCallback& memory_policy_changed_callback) { |
| 187 // There's no memory manager for the in-process implementation. |
| 188 } |
| 189 |
| 190 } // namespace blimp |
OLD | NEW |