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 context_.reset(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 | |
| 110 if (context_) { | |
|
piman
2015/01/23 03:21:17
Actually we should return false if context_ is NUL
tfarina
2015/01/23 14:58:17
I moved the Set into the if above. I'm not sure I
piman
2015/01/23 18:08:52
gpu::GLInProcessContext::Create can fail if the co
| |
| 111 context_->SetContextLostCallback(base::Bind( | |
| 112 &InProcessContextProvider::OnLostContext, base::Unretained(this))); | |
| 113 } | |
| 114 | |
| 115 capabilities_.gpu = context_->GetImplementation()->capabilities(); | |
| 116 | |
| 117 std::string unique_context_name = | |
| 118 base::StringPrintf("%s-%p", debug_name_.c_str(), context_.get()); | |
| 119 context_->GetImplementation()->TraceBeginCHROMIUM( | |
| 120 "gpu_toplevel", unique_context_name.c_str()); | |
| 121 | |
| 122 return true; | |
| 123 } | |
| 124 | |
| 125 cc::ContextProvider::Capabilities | |
| 126 InProcessContextProvider::ContextCapabilities() { | |
| 127 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 128 return capabilities_; | |
| 129 } | |
| 130 | |
| 131 gpu::gles2::GLES2Interface* InProcessContextProvider::ContextGL() { | |
| 132 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 133 | |
| 134 return context_->GetImplementation(); | |
| 135 } | |
| 136 | |
| 137 gpu::ContextSupport* InProcessContextProvider::ContextSupport() { | |
| 138 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 139 | |
| 140 return context_->GetImplementation(); | |
| 141 } | |
| 142 | |
| 143 static void BindGrContextCallback(const GrGLInterface* interface) { | |
| 144 cc::ContextProvider* context_provider = | |
| 145 reinterpret_cast<InProcessContextProvider*>(interface->fCallbackData); | |
| 146 | |
| 147 gles2::SetGLContext(context_provider->ContextGL()); | |
| 148 } | |
| 149 | |
| 150 class GrContext* InProcessContextProvider::GrContext() { | |
| 151 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 152 | |
| 153 if (gr_context_) | |
| 154 return gr_context_.get(); | |
| 155 | |
| 156 // The GrGLInterface factory will make GL calls using the C GLES2 interface. | |
| 157 // Make sure the gles2 library is initialized first on exactly one thread. | |
| 158 g_gles2_initializer.Get(); | |
| 159 gles2::SetGLContext(ContextGL()); | |
| 160 | |
| 161 skia::RefPtr<GrGLInterface> interface = | |
| 162 skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding()); | |
| 163 interface->fCallback = BindGrContextCallback; | |
| 164 interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(this); | |
| 165 | |
| 166 gr_context_ = skia::AdoptRef(GrContext::Create( | |
| 167 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get()))); | |
| 168 | |
| 169 return gr_context_.get(); | |
| 170 } | |
| 171 | |
| 172 bool InProcessContextProvider::IsContextLost() { | |
| 173 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 174 | |
| 175 base::AutoLock lock(destroyed_lock_); | |
| 176 return destroyed_; | |
| 177 } | |
| 178 | |
| 179 void InProcessContextProvider::VerifyContexts() { | |
| 180 } | |
| 181 | |
| 182 void InProcessContextProvider::DeleteCachedResources() { | |
| 183 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 184 | |
| 185 if (gr_context_) { | |
| 186 TRACE_EVENT_INSTANT0("gpu", "GrContext::freeGpuResources", | |
| 187 TRACE_EVENT_SCOPE_THREAD); | |
| 188 gr_context_->freeGpuResources(); | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 bool InProcessContextProvider::DestroyedOnMainThread() { | |
| 193 DCHECK(main_thread_checker_.CalledOnValidThread()); | |
| 194 | |
| 195 base::AutoLock lock(destroyed_lock_); | |
| 196 return destroyed_; | |
| 197 } | |
| 198 | |
| 199 void InProcessContextProvider::SetLostContextCallback( | |
| 200 const LostContextCallback& lost_context_callback) { | |
| 201 lost_context_callback_ = lost_context_callback; | |
| 202 } | |
| 203 | |
| 204 void InProcessContextProvider::SetMemoryPolicyChangedCallback( | |
| 205 const MemoryPolicyChangedCallback& memory_policy_changed_callback) { | |
| 206 // There's no memory manager for the in-process implementation. | |
| 207 } | |
| 208 | |
| 209 void InProcessContextProvider::OnLostContext() { | |
| 210 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
| 211 { | |
| 212 base::AutoLock lock(destroyed_lock_); | |
| 213 if (destroyed_) | |
| 214 return; | |
| 215 destroyed_ = true; | |
| 216 } | |
| 217 if (!lost_context_callback_.is_null()) | |
| 218 base::ResetAndReturn(&lost_context_callback_).Run(); | |
| 219 if (gr_context_) | |
| 220 gr_context_->abandonContext(); | |
| 221 } | |
| 222 | |
| 223 } // namespace ui | |
| OLD | NEW |