Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / compositor / test / in_process_context_provider.cc
blobc5f950f580eacb213905ee6593dc571ea915bff3
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 cc::ContextProvider::Capabilities
126 InProcessContextProvider::ContextCapabilities() {
127 DCHECK(context_thread_checker_.CalledOnValidThread());
128 return capabilities_;
131 gpu::gles2::GLES2Interface* InProcessContextProvider::ContextGL() {
132 DCHECK(context_thread_checker_.CalledOnValidThread());
134 return context_->GetImplementation();
137 gpu::ContextSupport* InProcessContextProvider::ContextSupport() {
138 DCHECK(context_thread_checker_.CalledOnValidThread());
140 return context_->GetImplementation();
143 static void BindGrContextCallback(const GrGLInterface* interface) {
144 cc::ContextProvider* context_provider =
145 reinterpret_cast<InProcessContextProvider*>(interface->fCallbackData);
147 gles2::SetGLContext(context_provider->ContextGL());
150 class GrContext* InProcessContextProvider::GrContext() {
151 DCHECK(context_thread_checker_.CalledOnValidThread());
153 if (gr_context_)
154 return gr_context_.get();
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());
161 skia::RefPtr<GrGLInterface> interface =
162 skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding());
163 interface->fCallback = BindGrContextCallback;
164 interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(this);
166 gr_context_ = skia::AdoptRef(GrContext::Create(
167 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get())));
169 return gr_context_.get();
172 void InProcessContextProvider::InvalidateGrContext(uint32_t state) {
173 DCHECK(context_thread_checker_.CalledOnValidThread());
175 if (gr_context_)
176 gr_context_.get()->resetContext(state);
179 void InProcessContextProvider::SetupLock() {
182 base::Lock* InProcessContextProvider::GetLock() {
183 return &context_lock_;
186 void InProcessContextProvider::VerifyContexts() {
189 void InProcessContextProvider::DeleteCachedResources() {
190 DCHECK(context_thread_checker_.CalledOnValidThread());
192 if (gr_context_) {
193 TRACE_EVENT_INSTANT0("gpu", "GrContext::freeGpuResources",
194 TRACE_EVENT_SCOPE_THREAD);
195 gr_context_->freeGpuResources();
199 bool InProcessContextProvider::DestroyedOnMainThread() {
200 DCHECK(main_thread_checker_.CalledOnValidThread());
202 base::AutoLock lock(destroyed_lock_);
203 return destroyed_;
206 void InProcessContextProvider::SetLostContextCallback(
207 const LostContextCallback& lost_context_callback) {
208 lost_context_callback_ = lost_context_callback;
211 void InProcessContextProvider::SetMemoryPolicyChangedCallback(
212 const MemoryPolicyChangedCallback& memory_policy_changed_callback) {
213 // There's no memory manager for the in-process implementation.
216 void InProcessContextProvider::OnLostContext() {
217 DCHECK(context_thread_checker_.CalledOnValidThread());
219 base::AutoLock lock(destroyed_lock_);
220 if (destroyed_)
221 return;
222 destroyed_ = true;
224 if (!lost_context_callback_.is_null())
225 base::ResetAndReturn(&lost_context_callback_).Run();
226 if (gr_context_)
227 gr_context_->abandonContext();
230 } // namespace ui