Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / compositor / test / in_process_context_provider.cc
blob76cd19dc191ff8624982205a2d93b573b0058ecb
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.
5 #include "ui/compositor/test/in_process_context_provider.h"
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/lazy_instance.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/trace_event/trace_event.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"
20 namespace ui {
22 namespace {
24 // Singleton used to initialize and terminate the gles2 library.
25 class GLES2Initializer {
26 public:
27 GLES2Initializer() { gles2::Initialize(); }
29 ~GLES2Initializer() { gles2::Terminate(); }
31 private:
32 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer);
35 base::LazyInstance<GLES2Initializer> g_gles2_initializer =
36 LAZY_INSTANCE_INITIALIZER;
38 } // namespace
40 // static
41 scoped_refptr<InProcessContextProvider> InProcessContextProvider::Create(
42 const gpu::gles2::ContextCreationAttribHelper& attribs,
43 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
44 gpu::ImageFactory* image_factory,
45 gfx::AcceleratedWidget window,
46 const std::string& debug_name) {
47 return new InProcessContextProvider(
48 attribs, gpu_memory_buffer_manager, image_factory, window, debug_name);
51 // static
52 scoped_refptr<InProcessContextProvider>
53 InProcessContextProvider::CreateOffscreen(
54 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
55 gpu::ImageFactory* image_factory) {
56 gpu::gles2::ContextCreationAttribHelper attribs;
57 attribs.alpha_size = 8;
58 attribs.blue_size = 8;
59 attribs.green_size = 8;
60 attribs.red_size = 8;
61 attribs.depth_size = 0;
62 attribs.stencil_size = 8;
63 attribs.samples = 0;
64 attribs.sample_buffers = 0;
65 attribs.fail_if_major_perf_caveat = false;
66 attribs.bind_generates_resource = false;
67 return new InProcessContextProvider(attribs, gpu_memory_buffer_manager,
68 image_factory,
69 gfx::kNullAcceleratedWidget, "Offscreen");
72 InProcessContextProvider::InProcessContextProvider(
73 const gpu::gles2::ContextCreationAttribHelper& attribs,
74 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
75 gpu::ImageFactory* image_factory,
76 gfx::AcceleratedWidget window,
77 const std::string& debug_name)
78 : attribs_(attribs),
79 gpu_memory_buffer_manager_(gpu_memory_buffer_manager),
80 image_factory_(image_factory),
81 window_(window),
82 debug_name_(debug_name),
83 destroyed_(false) {
84 DCHECK(main_thread_checker_.CalledOnValidThread());
85 context_thread_checker_.DetachFromThread();
88 InProcessContextProvider::~InProcessContextProvider() {
89 DCHECK(main_thread_checker_.CalledOnValidThread() ||
90 context_thread_checker_.CalledOnValidThread());
93 bool InProcessContextProvider::BindToCurrentThread() {
94 // This is called on the thread the context will be used.
95 DCHECK(context_thread_checker_.CalledOnValidThread());
97 if (!context_) {
98 gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu;
99 context_.reset(gpu::GLInProcessContext::Create(
100 nullptr, /* service */
101 nullptr, /* surface */
102 !window_, /* is_offscreen */
103 window_, gfx::Size(1, 1), nullptr, /* share_context */
104 true, /* share_resources */
105 attribs_, gpu_preference, gpu::GLInProcessContextSharedMemoryLimits(),
106 gpu_memory_buffer_manager_, image_factory_));
108 if (!context_)
109 return false;
111 context_->SetContextLostCallback(base::Bind(
112 &InProcessContextProvider::OnLostContext, base::Unretained(this)));
115 capabilities_.gpu = context_->GetImplementation()->capabilities();
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());
122 return true;
125 void InProcessContextProvider::DetachFromThread() {
126 context_thread_checker_.DetachFromThread();
129 cc::ContextProvider::Capabilities
130 InProcessContextProvider::ContextCapabilities() {
131 DCHECK(context_thread_checker_.CalledOnValidThread());
132 return capabilities_;
135 gpu::gles2::GLES2Interface* InProcessContextProvider::ContextGL() {
136 DCHECK(context_thread_checker_.CalledOnValidThread());
138 return context_->GetImplementation();
141 gpu::ContextSupport* InProcessContextProvider::ContextSupport() {
142 DCHECK(context_thread_checker_.CalledOnValidThread());
144 return context_->GetImplementation();
147 static void BindGrContextCallback(const GrGLInterface* interface) {
148 cc::ContextProvider* context_provider =
149 reinterpret_cast<InProcessContextProvider*>(interface->fCallbackData);
151 gles2::SetGLContext(context_provider->ContextGL());
154 class GrContext* InProcessContextProvider::GrContext() {
155 DCHECK(context_thread_checker_.CalledOnValidThread());
157 if (gr_context_)
158 return gr_context_.get();
160 // The GrGLInterface factory will make GL calls using the C GLES2 interface.
161 // Make sure the gles2 library is initialized first on exactly one thread.
162 g_gles2_initializer.Get();
163 gles2::SetGLContext(ContextGL());
165 skia::RefPtr<GrGLInterface> interface =
166 skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding());
167 interface->fCallback = BindGrContextCallback;
168 interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(this);
170 gr_context_ = skia::AdoptRef(GrContext::Create(
171 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get())));
173 return gr_context_.get();
176 void InProcessContextProvider::InvalidateGrContext(uint32_t state) {
177 DCHECK(context_thread_checker_.CalledOnValidThread());
179 if (gr_context_)
180 gr_context_.get()->resetContext(state);
183 void InProcessContextProvider::SetupLock() {
186 base::Lock* InProcessContextProvider::GetLock() {
187 return &context_lock_;
190 void InProcessContextProvider::VerifyContexts() {
193 void InProcessContextProvider::DeleteCachedResources() {
194 DCHECK(context_thread_checker_.CalledOnValidThread());
196 if (gr_context_) {
197 TRACE_EVENT_INSTANT0("gpu", "GrContext::freeGpuResources",
198 TRACE_EVENT_SCOPE_THREAD);
199 gr_context_->freeGpuResources();
203 bool InProcessContextProvider::DestroyedOnMainThread() {
204 DCHECK(main_thread_checker_.CalledOnValidThread());
206 base::AutoLock lock(destroyed_lock_);
207 return destroyed_;
210 void InProcessContextProvider::SetLostContextCallback(
211 const LostContextCallback& lost_context_callback) {
212 lost_context_callback_ = lost_context_callback;
215 void InProcessContextProvider::SetMemoryPolicyChangedCallback(
216 const MemoryPolicyChangedCallback& memory_policy_changed_callback) {
217 // There's no memory manager for the in-process implementation.
220 void InProcessContextProvider::OnLostContext() {
221 DCHECK(context_thread_checker_.CalledOnValidThread());
223 base::AutoLock lock(destroyed_lock_);
224 if (destroyed_)
225 return;
226 destroyed_ = true;
228 if (!lost_context_callback_.is_null())
229 base::ResetAndReturn(&lost_context_callback_).Run();
230 if (gr_context_)
231 gr_context_->abandonContext();
234 } // namespace ui