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 context_thread_checker_.DetachFromThread(); |
| 52 |
| 53 gpu::gles2::ContextCreationAttribHelper attribs_for_gles2; |
| 54 attribs_for_gles2.alpha_size = 8; |
| 55 attribs_for_gles2.depth_size = 0; |
| 56 attribs_for_gles2.stencil_size = 0; |
| 57 attribs_for_gles2.samples = 0; |
| 58 attribs_for_gles2.sample_buffers = 0; |
| 59 attribs_for_gles2.fail_if_major_perf_caveat = false; |
| 60 attribs_for_gles2.bind_generates_resource = false; |
| 61 attribs_for_gles2.webgl_version = 0; |
| 62 attribs_for_gles2.lose_context_when_out_of_memory = true; |
| 63 |
| 64 context_.reset(gpu::GLInProcessContext::Create( |
| 65 nullptr /* service */, nullptr /* surface */, false /* is_offscreen */, |
| 66 widget, gfx::Size(1, 1), nullptr /* share_context */, |
| 67 false /* share_resources */, attribs_for_gles2, gfx::PreferDiscreteGpu, |
| 68 gpu::GLInProcessContextSharedMemoryLimits(), |
| 69 nullptr /* gpu_memory_buffer_manager */, nullptr /* memory_limits */)); |
| 70 context_->SetContextLostCallback( |
| 71 base::Bind(&BlimpContextProvider::OnLostContext, base::Unretained(this))); |
| 72 } |
| 73 |
| 74 BlimpContextProvider::~BlimpContextProvider() { |
| 75 DCHECK(main_thread_checker_.CalledOnValidThread() || |
| 76 context_thread_checker_.CalledOnValidThread()); |
| 77 } |
| 78 |
| 79 bool BlimpContextProvider::BindToCurrentThread() { |
| 80 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 81 capabilities_.gpu = context_->GetImplementation()->capabilities(); |
| 82 capabilities_.gpu.image = true; |
| 83 return true; |
| 84 } |
| 85 |
| 86 void BlimpContextProvider::DetachFromThread() { |
| 87 context_thread_checker_.DetachFromThread(); |
| 88 } |
| 89 |
| 90 cc::ContextProvider::Capabilities BlimpContextProvider::ContextCapabilities() { |
| 91 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 92 return capabilities_; |
| 93 } |
| 94 |
| 95 gpu::gles2::GLES2Interface* BlimpContextProvider::ContextGL() { |
| 96 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 97 return context_->GetImplementation(); |
| 98 } |
| 99 |
| 100 gpu::ContextSupport* BlimpContextProvider::ContextSupport() { |
| 101 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 102 return context_->GetImplementation(); |
| 103 } |
| 104 |
| 105 class GrContext* BlimpContextProvider::GrContext() { |
| 106 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 107 |
| 108 if (gr_context_) |
| 109 return gr_context_.get(); |
| 110 |
| 111 // The GrGLInterface factory will make GL calls using the C GLES2 interface. |
| 112 // Make sure the gles2 library is initialized first on exactly one thread. |
| 113 g_gles2_initializer.Get(); |
| 114 gles2::SetGLContext(ContextGL()); |
| 115 |
| 116 skia::RefPtr<GrGLInterface> interface = |
| 117 skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding()); |
| 118 interface->fCallback = BindGrContextCallback; |
| 119 interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(this); |
| 120 |
| 121 gr_context_ = skia::AdoptRef(GrContext::Create( |
| 122 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get()))); |
| 123 |
| 124 return gr_context_.get(); |
| 125 } |
| 126 |
| 127 void BlimpContextProvider::InvalidateGrContext(uint32_t state) { |
| 128 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 129 |
| 130 if (gr_context_) |
| 131 gr_context_.get()->resetContext(state); |
| 132 } |
| 133 |
| 134 void BlimpContextProvider::SetupLock() { |
| 135 context_->SetLock(&context_lock_); |
| 136 } |
| 137 |
| 138 base::Lock* BlimpContextProvider::GetLock() { |
| 139 return &context_lock_; |
| 140 } |
| 141 |
| 142 void BlimpContextProvider::VerifyContexts() { |
| 143 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 144 } |
| 145 |
| 146 void BlimpContextProvider::DeleteCachedResources() { |
| 147 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 148 |
| 149 if (gr_context_) |
| 150 gr_context_->freeGpuResources(); |
| 151 } |
| 152 |
| 153 bool BlimpContextProvider::DestroyedOnMainThread() { |
| 154 DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 155 |
| 156 base::AutoLock lock(destroyed_lock_); |
| 157 return destroyed_; |
| 158 } |
| 159 |
| 160 void BlimpContextProvider::SetLostContextCallback( |
| 161 const LostContextCallback& lost_context_callback) { |
| 162 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 163 lost_context_callback_ = lost_context_callback; |
| 164 } |
| 165 |
| 166 void BlimpContextProvider::SetMemoryPolicyChangedCallback( |
| 167 const MemoryPolicyChangedCallback& memory_policy_changed_callback) { |
| 168 // There's no memory manager for the in-process implementation. |
| 169 // TODO(dtrainor): Figure out if we need a memory manager for Blimp. |
| 170 } |
| 171 |
| 172 void BlimpContextProvider::OnLostContext() { |
| 173 DCHECK(context_thread_checker_.CalledOnValidThread()); |
| 174 |
| 175 { |
| 176 base::AutoLock lock(destroyed_lock_); |
| 177 if (destroyed_) |
| 178 return; |
| 179 destroyed_ = true; |
| 180 } |
| 181 |
| 182 if (!lost_context_callback_.is_null()) |
| 183 base::ResetAndReturn(&lost_context_callback_).Run(); |
| 184 if (gr_context_) |
| 185 gr_context_->abandonContext(); |
| 186 } |
| 187 |
| 188 } // namespace blimp |
OLD | NEW |