Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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 "ui/compositor/test/in_process_context_provider.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback_helpers.h" | |
| 9 #include "base/debug/trace_event.h" | |
| 10 #include "base/lazy_instance.h" | |
| 11 #include "base/strings/stringprintf.h" | |
| 12 #include "cc/output/managed_memory_policy.h" | |
| 13 #include "gpu/command_buffer/client/gl_in_process_context.h" | |
| 14 #include "gpu/command_buffer/client/gles2_implementation.h" | |
| 15 #include "gpu/command_buffer/client/gles2_lib.h" | |
| 16 #include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h" | |
| 17 #include "third_party/skia/include/gpu/GrContext.h" | |
| 18 #include "third_party/skia/include/gpu/gl/GrGLInterface.h" | |
| 19 | |
| 20 namespace ui { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // Singleton used to initialize and terminate the gles2 library. | |
| 25 class GLES2Initializer { | |
| 26 public: | |
| 27 GLES2Initializer() { gles2::Initialize(); } | |
| 28 | |
| 29 ~GLES2Initializer() { gles2::Terminate(); } | |
| 30 | |
| 31 private: | |
| 32 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer); | |
| 33 }; | |
| 34 | |
| 35 base::LazyInstance<GLES2Initializer> g_gles2_initializer = | |
| 36 LAZY_INSTANCE_INITIALIZER; | |
| 37 | |
| 38 } // namespace | |
| 39 | |
| 40 // static | |
| 41 scoped_refptr<InProcessContextProvider> InProcessContextProvider::Create( | |
| 42 const gpu::gles2::ContextCreationAttribHelper& attribs, | |
| 43 bool lose_context_when_out_of_memory, | |
| 44 gfx::AcceleratedWidget window, | |
| 45 const std::string& debug_name) { | |
| 46 return new InProcessContextProvider( | |
| 47 attribs, lose_context_when_out_of_memory, window, debug_name); | |
| 48 } | |
| 49 | |
| 50 // static | |
| 51 scoped_refptr<InProcessContextProvider> | |
| 52 InProcessContextProvider::CreateOffscreen( | |
| 53 bool lose_context_when_out_of_memory) { | |
| 54 gpu::gles2::ContextCreationAttribHelper attribs; | |
| 55 attribs.alpha_size = 8; | |
| 56 attribs.blue_size = 8; | |
| 57 attribs.green_size = 8; | |
| 58 attribs.red_size = 8; | |
| 59 attribs.depth_size = 0; | |
| 60 attribs.stencil_size = 8; | |
| 61 attribs.samples = 0; | |
| 62 attribs.sample_buffers = 0; | |
| 63 attribs.fail_if_major_perf_caveat = false; | |
| 64 attribs.bind_generates_resource = false; | |
| 65 return new InProcessContextProvider( | |
| 66 attribs, lose_context_when_out_of_memory, gfx::kNullAcceleratedWidget, | |
| 67 "Offscreen"); | |
| 68 } | |
| 69 | |
| 70 InProcessContextProvider::InProcessContextProvider( | |
| 71 const gpu::gles2::ContextCreationAttribHelper& attribs, | |
| 72 bool lose_context_when_out_of_memory, | |
| 73 gfx::AcceleratedWidget window, | |
| 74 const std::string& debug_name) | |
| 75 : attribs_(attribs), | |
| 76 lose_context_when_out_of_memory_(lose_context_when_out_of_memory), | |
| 77 window_(window), | |
| 78 debug_name_(debug_name), | |
| 79 destroyed_(false) { | |
| 80 DCHECK(main_thread_checker_.CalledOnValidThread()); | |
| 81 context_thread_checker_.DetachFromThread(); | |
| 82 } | |
| 83 | |
| 84 InProcessContextProvider::~InProcessContextProvider() { | |
| 85 DCHECK(main_thread_checker_.CalledOnValidThread() || | |
| 86 context_thread_checker_.CalledOnValidThread()); | |
| 87 } | |
| 88 | |
| 89 bool InProcessContextProvider::BindToCurrentThread() { | |
| 90 // This is called on the thread the context will be used. | |
| 91 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 92 | |
| 93 if (!context_) { | |
| 94 gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu; | |
| 95 scoped_ptr<gpu::GLInProcessContext> context(gpu::GLInProcessContext::Create( | |
| 96 nullptr, /* service */ | |
| 97 nullptr, /* surface */ | |
| 98 true, /* is_offscreen */ | |
| 99 window_, | |
| 100 gfx::Size(1, 1), | |
| 101 nullptr, /* share_context */ | |
| 102 true, /* share_resources */ | |
| 103 attribs_, | |
| 104 gpu_preference, | |
| 105 gpu::GLInProcessContextSharedMemoryLimits(), | |
| 106 nullptr, | |
| 107 nullptr)); | |
| 108 | |
| 109 if (!context) | |
| 110 return false; | |
| 111 | |
| 112 context_ = context.Pass(); | |
|
piman
2015/01/23 23:21:48
nit: you don't actually need the temporary |contex
tfarina
2015/01/24 14:57:11
Done.
| |
| 113 context_->SetContextLostCallback(base::Bind( | |
| 114 &InProcessContextProvider::OnLostContext, base::Unretained(this))); | |
| 115 } | |
| 116 | |
| 117 capabilities_.gpu = context_->GetImplementation()->capabilities(); | |
| 118 | |
| 119 std::string unique_context_name = | |
| 120 base::StringPrintf("%s-%p", debug_name_.c_str(), context_.get()); | |
| 121 context_->GetImplementation()->TraceBeginCHROMIUM( | |
| 122 "gpu_toplevel", unique_context_name.c_str()); | |
| 123 | |
| 124 return true; | |
| 125 } | |
| 126 | |
| 127 cc::ContextProvider::Capabilities | |
| 128 InProcessContextProvider::ContextCapabilities() { | |
| 129 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 130 return capabilities_; | |
| 131 } | |
| 132 | |
| 133 gpu::gles2::GLES2Interface* InProcessContextProvider::ContextGL() { | |
| 134 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 135 | |
| 136 return context_->GetImplementation(); | |
| 137 } | |
| 138 | |
| 139 gpu::ContextSupport* InProcessContextProvider::ContextSupport() { | |
| 140 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 141 | |
| 142 return context_->GetImplementation(); | |
| 143 } | |
| 144 | |
| 145 static void BindGrContextCallback(const GrGLInterface* interface) { | |
| 146 cc::ContextProvider* context_provider = | |
| 147 reinterpret_cast<InProcessContextProvider*>(interface->fCallbackData); | |
| 148 | |
| 149 gles2::SetGLContext(context_provider->ContextGL()); | |
| 150 } | |
| 151 | |
| 152 class GrContext* InProcessContextProvider::GrContext() { | |
| 153 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 154 | |
| 155 if (gr_context_) | |
| 156 return gr_context_.get(); | |
| 157 | |
| 158 // The GrGLInterface factory will make GL calls using the C GLES2 interface. | |
| 159 // Make sure the gles2 library is initialized first on exactly one thread. | |
| 160 g_gles2_initializer.Get(); | |
| 161 gles2::SetGLContext(ContextGL()); | |
| 162 | |
| 163 skia::RefPtr<GrGLInterface> interface = | |
| 164 skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding()); | |
| 165 interface->fCallback = BindGrContextCallback; | |
| 166 interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(this); | |
| 167 | |
| 168 gr_context_ = skia::AdoptRef(GrContext::Create( | |
| 169 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get()))); | |
| 170 | |
| 171 return gr_context_.get(); | |
| 172 } | |
| 173 | |
| 174 bool InProcessContextProvider::IsContextLost() { | |
| 175 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 176 | |
| 177 base::AutoLock lock(destroyed_lock_); | |
| 178 return destroyed_; | |
| 179 } | |
| 180 | |
| 181 void InProcessContextProvider::VerifyContexts() { | |
| 182 } | |
| 183 | |
| 184 void InProcessContextProvider::DeleteCachedResources() { | |
| 185 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 186 | |
| 187 if (gr_context_) { | |
| 188 TRACE_EVENT_INSTANT0("gpu", "GrContext::freeGpuResources", | |
| 189 TRACE_EVENT_SCOPE_THREAD); | |
| 190 gr_context_->freeGpuResources(); | |
| 191 } | |
| 192 } | |
| 193 | |
| 194 bool InProcessContextProvider::DestroyedOnMainThread() { | |
| 195 DCHECK(main_thread_checker_.CalledOnValidThread()); | |
| 196 | |
| 197 base::AutoLock lock(destroyed_lock_); | |
| 198 return destroyed_; | |
| 199 } | |
| 200 | |
| 201 void InProcessContextProvider::SetLostContextCallback( | |
| 202 const LostContextCallback& lost_context_callback) { | |
| 203 lost_context_callback_ = lost_context_callback; | |
| 204 } | |
| 205 | |
| 206 void InProcessContextProvider::SetMemoryPolicyChangedCallback( | |
| 207 const MemoryPolicyChangedCallback& memory_policy_changed_callback) { | |
| 208 // There's no memory manager for the in-process implementation. | |
| 209 } | |
| 210 | |
| 211 void InProcessContextProvider::OnLostContext() { | |
| 212 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 213 { | |
| 214 base::AutoLock lock(destroyed_lock_); | |
| 215 if (destroyed_) | |
| 216 return; | |
| 217 destroyed_ = true; | |
| 218 } | |
| 219 if (!lost_context_callback_.is_null()) | |
| 220 base::ResetAndReturn(&lost_context_callback_).Run(); | |
| 221 if (gr_context_) | |
| 222 gr_context_->abandonContext(); | |
| 223 } | |
| 224 | |
| 225 } // namespace ui | |
| OLD | NEW |