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 "components/view_manager/gles2/command_buffer_local.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "components/view_manager/gles2/command_buffer_local_client.h" | |
9 #include "components/view_manager/gles2/gpu_memory_tracker.h" | |
10 #include "components/view_manager/gles2/mojo_gpu_memory_buffer.h" | |
11 #include "gpu/command_buffer/service/command_buffer_service.h" | |
12 #include "gpu/command_buffer/service/context_group.h" | |
13 #include "gpu/command_buffer/service/gpu_scheduler.h" | |
14 #include "gpu/command_buffer/service/image_factory.h" | |
15 #include "gpu/command_buffer/service/image_manager.h" | |
16 #include "gpu/command_buffer/service/memory_tracking.h" | |
17 #include "gpu/command_buffer/service/shader_translator_cache.h" | |
18 #include "gpu/command_buffer/service/transfer_buffer_manager.h" | |
19 #include "gpu/command_buffer/service/valuebuffer_manager.h" | |
20 #include "ui/gfx/vsync_provider.h" | |
21 #include "ui/gl/gl_context.h" | |
22 #include "ui/gl/gl_image_memory.h" | |
23 #include "ui/gl/gl_surface.h" | |
24 | |
25 namespace gles2 { | |
26 | |
27 const unsigned int GL_MAP_CHROMIUM = 0x78F1; | |
28 | |
29 CommandBufferLocal::CommandBufferLocal( | |
30 CommandBufferLocalClient* client, | |
31 gfx::AcceleratedWidget widget, | |
32 const scoped_refptr<gles2::GpuState>& gpu_state) | |
33 : widget_(widget), | |
34 gpu_state_(gpu_state), | |
35 client_(client), | |
36 weak_factory_(this) { | |
37 } | |
38 | |
39 CommandBufferLocal::~CommandBufferLocal() { | |
40 command_buffer_.reset(); | |
41 if (decoder_.get()) { | |
42 bool have_context = decoder_->GetGLContext()->MakeCurrent(surface_.get()); | |
43 decoder_->Destroy(have_context); | |
44 decoder_.reset(); | |
45 } | |
46 } | |
47 | |
48 bool CommandBufferLocal::Initialize() { | |
49 if (widget_ == gfx::kNullAcceleratedWidget) { | |
50 surface_ = gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size(1, 1)); | |
51 } else { | |
52 surface_ = gfx::GLSurface::CreateViewGLSurface(widget_); | |
53 gfx::VSyncProvider* vsync_provider = | |
54 surface_ ? surface_->GetVSyncProvider() : nullptr; | |
55 if (vsync_provider) { | |
56 vsync_provider->GetVSyncParameters( | |
57 base::Bind(&CommandBufferLocal::OnUpdateVSyncParameters, | |
58 weak_factory_.GetWeakPtr())); | |
59 } | |
60 } | |
61 | |
62 if (!surface_.get()) | |
63 return false; | |
64 | |
65 // TODO(piman): virtual contexts, gpu preference. | |
66 context_ = gfx::GLContext::CreateGLContext( | |
67 gpu_state_->share_group(), surface_.get(), gfx::PreferIntegratedGpu); | |
68 if (!context_.get()) | |
69 return false; | |
70 | |
71 if (!context_->MakeCurrent(surface_.get())) | |
72 return false; | |
73 | |
74 // TODO(piman): ShaderTranslatorCache is currently per-ContextGroup but | |
75 // only needs to be per-thread. | |
76 bool bind_generates_resource = false; | |
77 scoped_refptr<gpu::gles2::ContextGroup> context_group = | |
78 new gpu::gles2::ContextGroup( | |
79 gpu_state_->mailbox_manager(), new gles2::GpuMemoryTracker, | |
80 new gpu::gles2::ShaderTranslatorCache, | |
81 new gpu::gles2::FramebufferCompletenessCache, nullptr, nullptr, | |
82 nullptr, bind_generates_resource); | |
83 | |
84 command_buffer_.reset( | |
85 new gpu::CommandBufferService(context_group->transfer_buffer_manager())); | |
86 bool result = command_buffer_->Initialize(); | |
87 DCHECK(result); | |
88 | |
89 decoder_.reset(::gpu::gles2::GLES2Decoder::Create(context_group.get())); | |
90 scheduler_.reset(new gpu::GpuScheduler(command_buffer_.get(), decoder_.get(), | |
91 decoder_.get())); | |
92 decoder_->set_engine(scheduler_.get()); | |
93 decoder_->SetResizeCallback( | |
94 base::Bind(&CommandBufferLocal::OnResize, base::Unretained(this))); | |
95 decoder_->SetWaitSyncPointCallback(base::Bind( | |
96 &CommandBufferLocal::OnWaitSyncPoint, base::Unretained(this))); | |
97 | |
98 gpu::gles2::DisallowedFeatures disallowed_features; | |
99 | |
100 // TODO(piman): attributes. | |
101 std::vector<int32> attrib_vector; | |
102 if (!decoder_->Initialize(surface_, context_, false /* offscreen */, | |
103 gfx::Size(1, 1), disallowed_features, | |
104 attrib_vector)) | |
105 return false; | |
106 | |
107 command_buffer_->SetPutOffsetChangeCallback(base::Bind( | |
108 &CommandBufferLocal::PumpCommands, base::Unretained(this))); | |
109 command_buffer_->SetGetBufferChangeCallback(base::Bind( | |
110 &gpu::GpuScheduler::SetGetBuffer, base::Unretained(scheduler_.get()))); | |
111 command_buffer_->SetParseErrorCallback( | |
112 base::Bind(&CommandBufferLocal::OnParseError, base::Unretained(this))); | |
113 return true; | |
114 } | |
115 | |
116 gpu::CommandBuffer* CommandBufferLocal::GetCommandBuffer() { | |
117 return command_buffer_.get(); | |
118 } | |
119 | |
120 /******************************************************************************/ | |
121 // gpu::GpuControl: | |
122 /******************************************************************************/ | |
123 | |
124 gpu::Capabilities CommandBufferLocal::GetCapabilities() { | |
125 return decoder_->GetCapabilities(); | |
126 } | |
127 | |
128 int32_t CommandBufferLocal::CreateImage(ClientBuffer buffer, | |
129 size_t width, | |
130 size_t height, | |
131 unsigned internalformat) { | |
132 gles2::MojoGpuMemoryBufferImpl* gpu_memory_buffer = | |
133 gles2::MojoGpuMemoryBufferImpl::FromClientBuffer(buffer); | |
134 | |
135 scoped_refptr<gfx::GLImageMemory> image(new gfx::GLImageMemory( | |
136 gfx::Size(static_cast<int>(width), static_cast<int>(height)), | |
137 internalformat)); | |
138 if (!image->Initialize( | |
139 static_cast<const unsigned char*>(gpu_memory_buffer->GetMemory()), | |
140 gpu_memory_buffer->GetFormat())) { | |
141 return -1; | |
142 } | |
143 | |
144 static int32 next_id = 1; | |
145 int32 new_id = next_id++; | |
146 | |
147 gpu::gles2::ImageManager* image_manager = decoder_->GetImageManager(); | |
148 DCHECK(image_manager); | |
149 image_manager->AddImage(image.get(), new_id); | |
150 return new_id; | |
151 } | |
152 | |
153 void CommandBufferLocal::DestroyImage(int32 id) { | |
154 gpu::gles2::ImageManager* image_manager = decoder_->GetImageManager(); | |
155 DCHECK(image_manager); | |
156 image_manager->RemoveImage(id); | |
157 } | |
158 | |
159 int32_t CommandBufferLocal::CreateGpuMemoryBufferImage( | |
160 size_t width, | |
161 size_t height, | |
162 unsigned internalformat, | |
163 unsigned usage) { | |
164 DCHECK_EQ(usage, static_cast<unsigned>(GL_MAP_CHROMIUM)); | |
165 scoped_ptr<gfx::GpuMemoryBuffer> buffer( | |
166 gles2::MojoGpuMemoryBufferImpl::Create( | |
167 gfx::Size(static_cast<int>(width), static_cast<int>(height)), | |
168 gpu::ImageFactory::DefaultBufferFormatForImageFormat(internalformat), | |
169 gpu::ImageFactory::ImageUsageToGpuMemoryBufferUsage(usage))); | |
170 if (!buffer) | |
171 return -1; | |
172 return CreateImage(buffer->AsClientBuffer(), width, height, internalformat); | |
173 } | |
174 | |
175 uint32_t CommandBufferLocal::InsertSyncPoint() { | |
176 return 0; | |
177 } | |
178 | |
179 uint32_t CommandBufferLocal::InsertFutureSyncPoint() { | |
180 NOTIMPLEMENTED(); | |
181 return 0; | |
182 } | |
183 | |
184 void CommandBufferLocal::RetireSyncPoint(uint32_t sync_point) { | |
185 NOTIMPLEMENTED(); | |
186 } | |
187 | |
188 void CommandBufferLocal::SignalSyncPoint(uint32_t sync_point, | |
189 const base::Closure& callback) { | |
190 } | |
191 | |
192 void CommandBufferLocal::SignalQuery(uint32_t query, | |
193 const base::Closure& callback) { | |
194 // TODO(piman) | |
195 NOTIMPLEMENTED(); | |
196 } | |
197 | |
198 void CommandBufferLocal::SetSurfaceVisible(bool visible) { | |
199 // TODO(piman) | |
200 NOTIMPLEMENTED(); | |
201 } | |
202 | |
203 uint32_t CommandBufferLocal::CreateStreamTexture(uint32_t texture_id) { | |
204 // TODO(piman) | |
205 NOTIMPLEMENTED(); | |
206 return 0; | |
207 } | |
208 | |
209 void CommandBufferLocal::SetLock(base::Lock* lock) { | |
210 NOTIMPLEMENTED(); | |
211 } | |
212 | |
213 bool CommandBufferLocal::IsGpuChannelLost() { | |
214 // This is only possible for out-of-process command buffers. | |
215 return false; | |
216 } | |
217 | |
218 void CommandBufferLocal::PumpCommands() { | |
219 if (!decoder_->MakeCurrent()) { | |
220 command_buffer_->SetContextLostReason(decoder_->GetContextLostReason()); | |
221 command_buffer_->SetParseError(::gpu::error::kLostContext); | |
222 return; | |
223 } | |
224 scheduler_->PutChanged(); | |
225 } | |
226 | |
227 void CommandBufferLocal::OnResize(gfx::Size size, float scale_factor) { | |
228 surface_->Resize(size); | |
229 } | |
230 | |
231 void CommandBufferLocal::OnUpdateVSyncParameters( | |
232 const base::TimeTicks timebase, | |
233 const base::TimeDelta interval) { | |
234 if (client_) | |
235 client_->UpdateVSyncParameters(timebase.ToInternalValue(), | |
236 interval.ToInternalValue()); | |
237 | |
238 } | |
239 | |
240 bool CommandBufferLocal::OnWaitSyncPoint(uint32_t sync_point) { | |
241 if (!sync_point) | |
242 return true; | |
243 if (gpu_state_->sync_point_manager()->IsSyncPointRetired(sync_point)) | |
244 return true; | |
245 scheduler_->SetScheduled(false); | |
246 gpu_state_->sync_point_manager()->AddSyncPointCallback( | |
247 sync_point, base::Bind(&CommandBufferLocal::OnSyncPointRetired, | |
248 weak_factory_.GetWeakPtr())); | |
249 return scheduler_->IsScheduled(); | |
250 } | |
251 | |
252 void CommandBufferLocal::OnParseError() { | |
253 gpu::CommandBuffer::State state = command_buffer_->GetLastState(); | |
254 OnContextLost(state.context_lost_reason); | |
255 } | |
256 | |
257 void CommandBufferLocal::OnContextLost(uint32_t reason) { | |
258 if (client_) | |
259 client_->DidLoseContext(); | |
260 } | |
261 | |
262 void CommandBufferLocal::OnSyncPointRetired() { | |
263 scheduler_->SetScheduled(true); | |
264 } | |
265 | |
266 } // namespace gles2 | |
OLD | NEW |