OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "blimp/client/compositor/blimp_context_provider.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/callback_helpers.h" | |
9 #include "base/lazy_instance.h" | |
10 #include "gpu/command_buffer/client/gl_in_process_context.h" | |
11 #include "gpu/command_buffer/client/gles2_implementation.h" | |
12 #include "gpu/command_buffer/client/gles2_lib.h" | |
13 #include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h" | |
14 #include "third_party/skia/include/gpu/GrContext.h" | |
15 #include "third_party/skia/include/gpu/gl/GrGLInterface.h" | |
16 | |
17 namespace { | |
18 | |
19 // Singleton used to initialize and terminate the gles2 library. | |
20 class GLES2Initializer { | |
21 public: | |
22 GLES2Initializer() { gles2::Initialize(); } | |
23 | |
24 ~GLES2Initializer() { gles2::Terminate(); } | |
25 | |
26 private: | |
27 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer); | |
28 }; | |
29 | |
30 base::LazyInstance<GLES2Initializer> g_gles2_initializer = | |
31 LAZY_INSTANCE_INITIALIZER; | |
32 | |
33 static void BindGrContextCallback(const GrGLInterface* interface) { | |
34 blimp::BlimpContextProvider* context_provider = | |
35 reinterpret_cast<blimp::BlimpContextProvider*>(interface->fCallbackData); | |
36 | |
37 gles2::SetGLContext(context_provider->ContextGL()); | |
38 } | |
39 | |
40 } // namespace | |
41 | |
42 namespace blimp { | |
43 | |
44 // static | |
45 scoped_refptr<BlimpContextProvider> BlimpContextProvider::Create( | |
46 gfx::AcceleratedWidget widget) { | |
47 return new BlimpContextProvider(widget); | |
48 } | |
49 | |
50 BlimpContextProvider::BlimpContextProvider(gfx::AcceleratedWidget widget) { | |
51 context_thread_checker_.DetachFromThread(); | |
52 | |
53 gpu::gles2::ContextCreationAttribHelper attribs_for_gles2; | |
54 attribs_for_gles2.alpha_size = 8; | |
55 attribs_for_gles2.depth_size = 0; | |
56 attribs_for_gles2.stencil_size = 0; | |
57 attribs_for_gles2.samples = 0; | |
58 attribs_for_gles2.sample_buffers = 0; | |
59 attribs_for_gles2.fail_if_major_perf_caveat = false; | |
60 attribs_for_gles2.bind_generates_resource = false; | |
61 attribs_for_gles2.webgl_version = 0; | |
62 attribs_for_gles2.lose_context_when_out_of_memory = true; | |
63 | |
64 context_.reset(gpu::GLInProcessContext::Create( | |
65 nullptr /* service */, nullptr /* surface */, false /* is_offscreen */, | |
66 widget, gfx::Size(1, 1), nullptr /* share_context */, | |
67 false /* share_resources */, attribs_for_gles2, gfx::PreferDiscreteGpu, | |
68 gpu::GLInProcessContextSharedMemoryLimits(), | |
69 nullptr /* gpu_memory_buffer_manager */, nullptr /* memory_limits */)); | |
70 context_->SetContextLostCallback( | |
71 base::Bind(&BlimpContextProvider::OnLostContext, base::Unretained(this))); | |
72 CHECK(context_); | |
danakj
2015/08/28 17:38:46
What's the point of this? You just crashed on the
David Trainor- moved to gerrit
2015/08/28 18:24:16
Gah I put it in the wrong place, I meant to put it
Wez
2015/08/31 16:27:18
Actually, if you're about to deref the variable, a
David Trainor- moved to gerrit
2015/08/31 21:25:43
Yeah that's true. I can make that change here bec
David Trainor- moved to gerrit
2015/08/31 21:27:28
scoped_ptr does have an assert, but it looks like
| |
73 } | |
74 | |
75 BlimpContextProvider::~BlimpContextProvider() { | |
76 DCHECK(main_thread_checker_.CalledOnValidThread() || | |
77 context_thread_checker_.CalledOnValidThread()); | |
78 } | |
79 | |
80 bool BlimpContextProvider::BindToCurrentThread() { | |
81 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
82 capabilities_.gpu = context_->GetImplementation()->capabilities(); | |
83 capabilities_.gpu.image = true; | |
84 return true; | |
85 } | |
86 | |
87 void BlimpContextProvider::DetachFromThread() { | |
88 context_thread_checker_.DetachFromThread(); | |
89 } | |
90 | |
91 cc::ContextProvider::Capabilities BlimpContextProvider::ContextCapabilities() { | |
92 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
93 return capabilities_; | |
94 } | |
95 | |
96 gpu::gles2::GLES2Interface* BlimpContextProvider::ContextGL() { | |
97 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
98 return context_->GetImplementation(); | |
99 } | |
100 | |
101 gpu::ContextSupport* BlimpContextProvider::ContextSupport() { | |
102 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
103 return context_->GetImplementation(); | |
104 } | |
105 | |
106 class GrContext* BlimpContextProvider::GrContext() { | |
107 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
108 | |
109 if (gr_context_) | |
110 return gr_context_.get(); | |
111 | |
112 // The GrGLInterface factory will make GL calls using the C GLES2 interface. | |
113 // Make sure the gles2 library is initialized first on exactly one thread. | |
114 g_gles2_initializer.Get(); | |
115 gles2::SetGLContext(ContextGL()); | |
116 | |
117 skia::RefPtr<GrGLInterface> interface = | |
118 skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding()); | |
119 interface->fCallback = BindGrContextCallback; | |
120 interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(this); | |
121 | |
122 gr_context_ = skia::AdoptRef(GrContext::Create( | |
123 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get()))); | |
124 | |
125 return gr_context_.get(); | |
126 } | |
127 | |
128 void BlimpContextProvider::InvalidateGrContext(uint32_t state) { | |
129 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
130 | |
131 if (gr_context_) | |
132 gr_context_.get()->resetContext(state); | |
133 } | |
134 | |
135 void BlimpContextProvider::SetupLock() { | |
136 context_->SetLock(&context_lock_); | |
137 } | |
138 | |
139 base::Lock* BlimpContextProvider::GetLock() { | |
140 return &context_lock_; | |
141 } | |
142 | |
143 void BlimpContextProvider::VerifyContexts() { | |
144 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
145 } | |
146 | |
147 void BlimpContextProvider::DeleteCachedResources() { | |
148 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
149 | |
150 if (gr_context_) | |
151 gr_context_->freeGpuResources(); | |
152 } | |
153 | |
154 bool BlimpContextProvider::DestroyedOnMainThread() { | |
155 DCHECK(main_thread_checker_.CalledOnValidThread()); | |
156 | |
157 base::AutoLock lock(destroyed_lock_); | |
158 return destroyed_; | |
159 } | |
160 | |
161 void BlimpContextProvider::SetLostContextCallback( | |
162 const LostContextCallback& lost_context_callback) { | |
163 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
164 lost_context_callback_ = lost_context_callback; | |
165 } | |
166 | |
167 void BlimpContextProvider::SetMemoryPolicyChangedCallback( | |
168 const MemoryPolicyChangedCallback& memory_policy_changed_callback) { | |
169 // There's no memory manager for the in-process implementation. | |
170 } | |
171 | |
172 void BlimpContextProvider::OnLostContext() { | |
173 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
174 | |
175 { | |
176 base::AutoLock lock(destroyed_lock_); | |
177 if (destroyed_) | |
178 return; | |
179 destroyed_ = true; | |
180 } | |
181 | |
182 if (!lost_context_callback_.is_null()) | |
183 base::ResetAndReturn(&lost_context_callback_).Run(); | |
184 if (gr_context_) | |
185 gr_context_->abandonContext(); | |
186 } | |
187 | |
188 } // namespace blimp | |
OLD | NEW |