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