| 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 blimp { | |
| 18 namespace client { | |
| 19 namespace { | |
| 20 | |
| 21 // Singleton used to initialize and terminate the gles2 library. | |
| 22 class GLES2Initializer { | |
| 23 public: | |
| 24 GLES2Initializer() { gles2::Initialize(); } | |
| 25 | |
| 26 ~GLES2Initializer() { gles2::Terminate(); } | |
| 27 | |
| 28 private: | |
| 29 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer); | |
| 30 }; | |
| 31 | |
| 32 base::LazyInstance<GLES2Initializer> g_gles2_initializer = | |
| 33 LAZY_INSTANCE_INITIALIZER; | |
| 34 | |
| 35 static void BindGrContextCallback(const GrGLInterface* interface) { | |
| 36 BlimpContextProvider* context_provider = | |
| 37 reinterpret_cast<BlimpContextProvider*>(interface->fCallbackData); | |
| 38 | |
| 39 gles2::SetGLContext(context_provider->ContextGL()); | |
| 40 } | |
| 41 | |
| 42 } // namespace | |
| 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.context_type = gpu::gles2::CONTEXT_TYPE_OPENGLES2; | |
| 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 = skia::AdoptRef(new GrGLInterface); | |
| 117 skia_bindings::InitCommandBufferSkiaGLBinding(interface.get()); | |
| 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::DeleteCachedResources() { | |
| 143 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 144 | |
| 145 if (gr_context_) | |
| 146 gr_context_->freeGpuResources(); | |
| 147 } | |
| 148 | |
| 149 void BlimpContextProvider::SetLostContextCallback( | |
| 150 const LostContextCallback& lost_context_callback) { | |
| 151 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 152 lost_context_callback_ = lost_context_callback; | |
| 153 } | |
| 154 | |
| 155 void BlimpContextProvider::OnLostContext() { | |
| 156 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 157 if (!lost_context_callback_.is_null()) | |
| 158 base::ResetAndReturn(&lost_context_callback_).Run(); | |
| 159 if (gr_context_) | |
| 160 gr_context_->abandonContext(); | |
| 161 } | |
| 162 | |
| 163 } // namespace client | |
| 164 } // namespace blimp | |
| OLD | NEW |