Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / android_webview / browser / aw_render_thread_context_provider.cc
blobe92897590ed05a603685cc5d2213e62af5b049ee
1 // Copyright 2015 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 "android_webview/browser/aw_render_thread_context_provider.h"
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/lazy_instance.h"
10 #include "base/trace_event/trace_event.h"
11 #include "cc/output/managed_memory_policy.h"
12 #include "gpu/blink/webgraphicscontext3d_impl.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 android_webview {
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<AwRenderThreadContextProvider>
42 AwRenderThreadContextProvider::Create(
43 scoped_refptr<gfx::GLSurface> surface,
44 scoped_refptr<gpu::InProcessCommandBuffer::Service> service) {
45 return new AwRenderThreadContextProvider(surface, service);
48 AwRenderThreadContextProvider::AwRenderThreadContextProvider(
49 scoped_refptr<gfx::GLSurface> surface,
50 scoped_refptr<gpu::InProcessCommandBuffer::Service> service)
51 : destroyed_(false) {
52 DCHECK(main_thread_checker_.CalledOnValidThread());
54 blink::WebGraphicsContext3D::Attributes attributes;
55 attributes.antialias = false;
56 attributes.depth = false;
57 attributes.stencil = false;
58 attributes.shareResources = true;
59 attributes.noAutomaticFlushes = true;
60 gpu::gles2::ContextCreationAttribHelper attribs_for_gles2;
61 gpu_blink::WebGraphicsContext3DImpl::ConvertAttributes(attributes,
62 &attribs_for_gles2);
63 attribs_for_gles2.lose_context_when_out_of_memory = true;
65 context_.reset(gpu::GLInProcessContext::Create(
66 service,
67 surface,
68 surface->IsOffscreen(),
69 gfx::kNullAcceleratedWidget,
70 surface->GetSize(),
71 NULL /* share_context */,
72 false /* share_resources */,
73 attribs_for_gles2,
74 gfx::PreferDiscreteGpu,
75 gpu::GLInProcessContextSharedMemoryLimits(),
76 nullptr,
77 nullptr));
79 context_->SetContextLostCallback(base::Bind(
80 &AwRenderThreadContextProvider::OnLostContext, base::Unretained(this)));
82 capabilities_.gpu = context_->GetImplementation()->capabilities();
85 AwRenderThreadContextProvider::~AwRenderThreadContextProvider() {
86 DCHECK(main_thread_checker_.CalledOnValidThread());
89 bool AwRenderThreadContextProvider::BindToCurrentThread() {
90 // This is called on the thread the context will be used.
91 DCHECK(main_thread_checker_.CalledOnValidThread());
93 return true;
96 cc::ContextProvider::Capabilities
97 AwRenderThreadContextProvider::ContextCapabilities() {
98 DCHECK(main_thread_checker_.CalledOnValidThread());
100 return capabilities_;
103 gpu::gles2::GLES2Interface* AwRenderThreadContextProvider::ContextGL() {
104 DCHECK(main_thread_checker_.CalledOnValidThread());
106 return context_->GetImplementation();
109 gpu::ContextSupport* AwRenderThreadContextProvider::ContextSupport() {
110 DCHECK(main_thread_checker_.CalledOnValidThread());
112 return context_->GetImplementation();
115 static void BindGrContextCallback(const GrGLInterface* interface) {
116 cc::ContextProvider* context_provider =
117 reinterpret_cast<AwRenderThreadContextProvider*>(
118 interface->fCallbackData);
120 gles2::SetGLContext(context_provider->ContextGL());
123 class GrContext* AwRenderThreadContextProvider::GrContext() {
124 DCHECK(main_thread_checker_.CalledOnValidThread());
126 if (gr_context_)
127 return gr_context_.get();
129 // The GrGLInterface factory will make GL calls using the C GLES2 interface.
130 // Make sure the gles2 library is initialized first on exactly one thread.
131 g_gles2_initializer.Get();
132 gles2::SetGLContext(ContextGL());
134 skia::RefPtr<GrGLInterface> interface =
135 skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding());
136 interface->fCallback = BindGrContextCallback;
137 interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(this);
139 gr_context_ = skia::AdoptRef(GrContext::Create(
140 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get())));
142 return gr_context_.get();
145 void AwRenderThreadContextProvider::InvalidateGrContext(uint32_t state) {
146 DCHECK(main_thread_checker_.CalledOnValidThread());
148 if (gr_context_)
149 gr_context_.get()->resetContext(state);
152 void AwRenderThreadContextProvider::SetupLock() {
153 context_->SetLock(&context_lock_);
156 base::Lock* AwRenderThreadContextProvider::GetLock() {
157 return &context_lock_;
160 void AwRenderThreadContextProvider::VerifyContexts() {
163 void AwRenderThreadContextProvider::DeleteCachedResources() {
164 DCHECK(main_thread_checker_.CalledOnValidThread());
166 if (gr_context_) {
167 TRACE_EVENT_INSTANT0("gpu", "GrContext::freeGpuResources",
168 TRACE_EVENT_SCOPE_THREAD);
169 gr_context_->freeGpuResources();
173 bool AwRenderThreadContextProvider::DestroyedOnMainThread() {
174 DCHECK(main_thread_checker_.CalledOnValidThread());
176 return destroyed_;
179 void AwRenderThreadContextProvider::SetLostContextCallback(
180 const LostContextCallback& lost_context_callback) {
181 lost_context_callback_ = lost_context_callback;
184 void AwRenderThreadContextProvider::SetMemoryPolicyChangedCallback(
185 const MemoryPolicyChangedCallback& memory_policy_changed_callback) {
186 // There's no memory manager for the in-process implementation.
189 void AwRenderThreadContextProvider::OnLostContext() {
190 DCHECK(main_thread_checker_.CalledOnValidThread());
192 if (destroyed_)
193 return;
194 destroyed_ = true;
196 if (!lost_context_callback_.is_null())
197 base::ResetAndReturn(&lost_context_callback_).Run();
198 if (gr_context_)
199 gr_context_->abandonContext();
202 } // namespace android_webview