OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gl/gl_surface.h" | 5 #include "ui/gl/gl_surface.h" |
6 | 6 |
7 #include "base/bind.h" | |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
9 #include "ui/gfx/native_widget_types.h" | 10 #include "ui/gfx/native_widget_types.h" |
10 #include "ui/gl/gl_context.h" | 11 #include "ui/gl/gl_context.h" |
11 #include "ui/gl/gl_image.h" | 12 #include "ui/gl/gl_image.h" |
13 #include "ui/gl/gl_image_linux_dma_buffer.h" | |
12 #include "ui/gl/gl_implementation.h" | 14 #include "ui/gl/gl_implementation.h" |
13 #include "ui/gl/gl_surface_egl.h" | 15 #include "ui/gl/gl_surface_egl.h" |
14 #include "ui/gl/gl_surface_osmesa.h" | 16 #include "ui/gl/gl_surface_osmesa.h" |
15 #include "ui/gl/gl_surface_stub.h" | 17 #include "ui/gl/gl_surface_stub.h" |
16 #include "ui/gl/scoped_make_current.h" | 18 #include "ui/gl/scoped_make_current.h" |
19 #include "ui/ozone/public/native_pixmap.h" | |
17 #include "ui/ozone/public/surface_factory_ozone.h" | 20 #include "ui/ozone/public/surface_factory_ozone.h" |
18 #include "ui/ozone/public/surface_ozone_egl.h" | 21 #include "ui/ozone/public/surface_ozone_egl.h" |
19 | 22 |
20 namespace gfx { | 23 namespace gfx { |
21 | 24 |
22 namespace { | 25 namespace { |
23 | 26 |
27 static void EmptyCallback() { | |
dnicoara
2015/02/23 22:48:26
Use base::DoNothing()
| |
28 } | |
29 | |
24 // A thin wrapper around GLSurfaceEGL that owns the EGLNativeWindow | 30 // A thin wrapper around GLSurfaceEGL that owns the EGLNativeWindow |
25 class GL_EXPORT GLSurfaceOzoneEGL : public NativeViewGLSurfaceEGL { | 31 class GL_EXPORT GLSurfaceOzoneEGL : public NativeViewGLSurfaceEGL { |
26 public: | 32 public: |
27 GLSurfaceOzoneEGL(scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface, | 33 GLSurfaceOzoneEGL(scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface, |
28 AcceleratedWidget widget) | 34 AcceleratedWidget widget) |
29 : NativeViewGLSurfaceEGL(ozone_surface->GetNativeWindow()), | 35 : NativeViewGLSurfaceEGL(ozone_surface->GetNativeWindow()), |
30 ozone_surface_(ozone_surface.Pass()), | 36 ozone_surface_(ozone_surface.Pass()), |
31 widget_(widget) {} | 37 widget_(widget) {} |
32 | 38 |
33 bool Initialize() override { | 39 bool Initialize() override { |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 return ozone_surface_->OnSwapBuffersAsync(callback); | 156 return ozone_surface_->OnSwapBuffersAsync(callback); |
151 } | 157 } |
152 bool PostSubBufferAsync(int x, | 158 bool PostSubBufferAsync(int x, |
153 int y, | 159 int y, |
154 int width, | 160 int width, |
155 int height, | 161 int height, |
156 const SwapCompletionCallback& callback) override { | 162 const SwapCompletionCallback& callback) override { |
157 return SwapBuffersAsync(callback); | 163 return SwapBuffersAsync(callback); |
158 } | 164 } |
159 | 165 |
160 private: | 166 protected: |
161 ~GLSurfaceOzoneSurfaceless() override { | 167 ~GLSurfaceOzoneSurfaceless() override { |
162 Destroy(); // EGL surface must be destroyed before SurfaceOzone | 168 Destroy(); // EGL surface must be destroyed before SurfaceOzone |
163 } | 169 } |
164 | 170 |
165 bool Flush() { | 171 bool Flush() { |
166 glFlush(); | 172 glFlush(); |
167 // TODO: the following should be replaced by a per surface flush as it gets | 173 // TODO: the following should be replaced by a per surface flush as it gets |
168 // implemented in GL drivers. | 174 // implemented in GL drivers. |
169 if (has_implicit_external_sync_) { | 175 if (has_implicit_external_sync_) { |
170 const EGLint attrib_list[] = { | 176 const EGLint attrib_list[] = { |
(...skipping 16 matching lines...) Expand all Loading... | |
187 } | 193 } |
188 | 194 |
189 // The native surface. Deleting this is allowed to free the EGLNativeWindow. | 195 // The native surface. Deleting this is allowed to free the EGLNativeWindow. |
190 scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface_; | 196 scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface_; |
191 AcceleratedWidget widget_; | 197 AcceleratedWidget widget_; |
192 scoped_ptr<VSyncProvider> vsync_provider_; | 198 scoped_ptr<VSyncProvider> vsync_provider_; |
193 bool has_implicit_external_sync_; | 199 bool has_implicit_external_sync_; |
194 DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneSurfaceless); | 200 DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneSurfaceless); |
195 }; | 201 }; |
196 | 202 |
203 // This provides surface-like semantics implemented through surfaceless. | |
204 // A framebuffer is bound automatically and re-bound at each swap. | |
205 class GL_EXPORT GLSurfaceOzoneSurfacelessSurfaceImpl | |
Pawel Osciak
2015/02/24 00:13:47
Please define methods outside of the class body (h
achaulk
2015/02/24 19:07:21
The rest of this file uses the inline-class style
| |
206 : public GLSurfaceOzoneSurfaceless { | |
207 public: | |
208 GLSurfaceOzoneSurfacelessSurfaceImpl( | |
209 scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface, | |
210 AcceleratedWidget widget) | |
211 : GLSurfaceOzoneSurfaceless(ozone_surface.Pass(), widget), | |
212 fbo_(0), | |
213 current_surface_(0) { | |
214 textures_[0] = 0; | |
215 textures_[1] = 0; | |
216 } | |
217 | |
218 unsigned int GetBackingFrameBufferObject() override { return fbo_; } | |
Pawel Osciak
2015/02/24 00:13:47
fbo_ is a GLuint and here we use unsigned int, per
| |
219 | |
220 bool OnMakeCurrent(GLContext* context) override { | |
221 if (!fbo_) { | |
222 glGenFramebuffersEXT(1, &fbo_); | |
223 if (!fbo_) | |
224 return false; | |
225 glGenTextures(2, textures_); | |
226 // Create and bind our pixmaps. | |
227 if (!ResizePixmaps()) | |
Pawel Osciak
2015/02/24 00:13:47
Please rename ResizePixmaps to something more desc
achaulk
2015/02/24 19:07:21
Done.
| |
228 return false; | |
229 } | |
230 BindFramebuffer(); | |
231 return SurfacelessEGL::OnMakeCurrent(context); | |
232 } | |
233 | |
234 bool Resize(const gfx::Size& size) override { | |
235 return GLSurfaceOzoneSurfaceless::Resize(size) && ResizePixmaps(); | |
236 } | |
237 | |
238 bool SupportsPostSubBuffer() override { return false; } | |
239 | |
240 bool SwapBuffers() override { | |
241 return SwapBuffersAsync(base::Bind(&EmptyCallback)); | |
dnicoara
2015/02/23 22:48:26
This is problematic since we're not guaranteeing t
piman
2015/02/24 03:00:57
Right, we need the ordering guarantee here (furthe
achaulk
2015/02/24 19:07:21
Can we block waiting for it?
| |
242 } | |
243 | |
244 bool SwapBuffersAsync(const SwapCompletionCallback& callback) override { | |
245 bool ret = images_[current_surface_]->ScheduleOverlayPlane( | |
246 widget_, 0, OverlayTransform::OVERLAY_TRANSFORM_NONE, | |
247 gfx::Rect(GetSize()), gfx::RectF(1, 1)) && | |
248 GLSurfaceOzoneSurfaceless::SwapBuffersAsync(callback); | |
Pawel Osciak
2015/02/24 00:13:47
This is difficult to read. Please split this state
achaulk
2015/02/24 19:07:21
Done.
| |
249 current_surface_ ^= 1; | |
250 BindFramebuffer(); | |
Pawel Osciak
2015/02/24 00:13:47
Do we still want to do this if ret==false?
achaulk
2015/02/24 19:07:21
Hmm, probably not
| |
251 return ret; | |
252 } | |
253 | |
254 void Destroy() override { | |
255 GLContext* current_context = GLContext::GetCurrent(); | |
256 DCHECK(current_context && current_context->IsCurrent(this)); | |
257 glBindFramebufferEXT(GL_FRAMEBUFFER, 0); | |
258 if (fbo_) { | |
dnicoara
2015/02/24 16:46:53
Should probably reset these values and DCHECK in t
| |
259 glDeleteTextures(2, textures_); | |
260 glDeleteFramebuffersEXT(1, &fbo_); | |
261 } | |
262 for (int i = 0; i < 2; i++) { | |
263 if (images_[i]) | |
264 images_[i]->Destroy(true); | |
265 } | |
266 } | |
267 | |
268 private: | |
269 class SurfaceImage : public GLImageLinuxDMABuffer { | |
270 public: | |
271 SurfaceImage(const gfx::Size& size, unsigned internalformat) | |
272 : GLImageLinuxDMABuffer(size, internalformat) {} | |
273 | |
274 bool Initialize(ui::NativePixmap* pixmap, | |
275 gfx::GpuMemoryBuffer::Format format) { | |
276 base::FileDescriptor handle(pixmap->GetDmaBufFd(), false); | |
277 if (!GLImageLinuxDMABuffer::Initialize(handle, format, | |
278 pixmap->GetDmaBufPitch())) | |
279 return false; | |
280 pixmap_ = pixmap; | |
281 return true; | |
282 } | |
283 bool ScheduleOverlayPlane(gfx::AcceleratedWidget widget, | |
284 int z_order, | |
285 gfx::OverlayTransform transform, | |
286 const gfx::Rect& bounds_rect, | |
287 const gfx::RectF& crop_rect) override { | |
288 return ui::SurfaceFactoryOzone::GetInstance()->ScheduleOverlayPlane( | |
289 widget, z_order, transform, pixmap_, bounds_rect, crop_rect); | |
290 } | |
291 | |
292 private: | |
293 scoped_refptr<ui::NativePixmap> pixmap_; | |
294 }; | |
295 | |
296 ~GLSurfaceOzoneSurfacelessSurfaceImpl() override {} | |
297 | |
298 void BindFramebuffer() { | |
299 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo_); | |
300 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | |
301 GL_TEXTURE_2D, textures_[current_surface_], 0); | |
302 } | |
303 | |
304 bool ResizePixmaps() { | |
305 if (!fbo_) | |
306 return true; | |
Pawel Osciak
2015/02/24 00:13:47
Is this ok to succeed if !fbo_ ?
achaulk
2015/02/24 19:07:21
Resizing before binding should be fine - this will
| |
307 for (int i = 0; i < 2; i++) { | |
308 scoped_refptr<ui::NativePixmap> pixmap = | |
309 ui::SurfaceFactoryOzone::GetInstance()->CreateNativePixmap( | |
310 widget_, GetSize(), ui::SurfaceFactoryOzone::RGBA_8888, | |
311 ui::SurfaceFactoryOzone::SCANOUT); | |
312 if (!pixmap) | |
313 return false; | |
314 scoped_refptr<SurfaceImage> image = new SurfaceImage(GetSize(), GL_RGBA); | |
315 if (!image->Initialize(pixmap.get(), | |
Pawel Osciak
2015/02/24 00:13:47
Please pass const scoped_refptr& instead of a poin
achaulk
2015/02/24 19:07:21
Done.
| |
316 gfx::GpuMemoryBuffer::Format::BGRA_8888)) | |
317 return false; | |
318 images_[i] = image; | |
319 // Bind image to texture. | |
320 glBindTexture(GL_TEXTURE_2D, textures_[i]); | |
Pawel Osciak
2015/02/24 00:13:47
Should we use ScopedTextureBinder instead?
achaulk
2015/02/24 19:07:21
It probably won't matter since this is at init, bu
| |
321 if (!images_[i]->BindTexImage(GL_TEXTURE_2D)) | |
322 return false; | |
323 } | |
324 return true; | |
325 } | |
326 | |
327 GLuint fbo_; | |
328 GLuint textures_[2]; | |
Pawel Osciak
2015/02/24 00:13:47
Please use std::vectors here and below.
piman
2015/02/24 02:57:32
Why? This is fine to me. Avoids extra indirections
| |
329 scoped_refptr<GLImage> images_[2]; | |
330 int current_surface_; | |
331 DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneSurfacelessSurfaceImpl); | |
332 }; | |
333 | |
197 } // namespace | 334 } // namespace |
198 | 335 |
199 // static | 336 // static |
200 bool GLSurface::InitializeOneOffInternal() { | 337 bool GLSurface::InitializeOneOffInternal() { |
201 switch (GetGLImplementation()) { | 338 switch (GetGLImplementation()) { |
202 case kGLImplementationEGLGLES2: | 339 case kGLImplementationEGLGLES2: |
203 if (!GLSurfaceEGL::InitializeOneOff()) { | 340 if (!GLSurfaceEGL::InitializeOneOff()) { |
204 LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed."; | 341 LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed."; |
205 return false; | 342 return false; |
206 } | 343 } |
207 | 344 |
208 return true; | 345 return true; |
209 case kGLImplementationOSMesaGL: | 346 case kGLImplementationOSMesaGL: |
210 case kGLImplementationMockGL: | 347 case kGLImplementationMockGL: |
211 return true; | 348 return true; |
212 default: | 349 default: |
213 return false; | 350 return false; |
214 } | 351 } |
215 } | 352 } |
216 | 353 |
217 // static | 354 // static |
355 scoped_refptr<GLSurface> GLSurface::CreateSurfacelessViewGLSurface( | |
356 gfx::AcceleratedWidget window) { | |
357 if (GetGLImplementation() == kGLImplementationEGLGLES2 && | |
358 window != kNullAcceleratedWidget && | |
359 GLSurfaceEGL::IsEGLSurfacelessContextSupported() && | |
360 ui::SurfaceFactoryOzone::GetInstance()->CanShowPrimaryPlaneAsOverlay()) { | |
361 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone = | |
362 ui::SurfaceFactoryOzone::GetInstance() | |
363 ->CreateSurfacelessEGLSurfaceForWidget(window); | |
364 if (!surface_ozone) | |
365 return nullptr; | |
366 scoped_refptr<GLSurface> surface; | |
367 surface = new GLSurfaceOzoneSurfaceless(surface_ozone.Pass(), window); | |
368 if (surface->Initialize()) | |
369 return surface; | |
370 } | |
371 | |
372 return nullptr; | |
373 } | |
374 | |
375 // static | |
218 scoped_refptr<GLSurface> GLSurface::CreateViewGLSurface( | 376 scoped_refptr<GLSurface> GLSurface::CreateViewGLSurface( |
219 gfx::AcceleratedWidget window) { | 377 gfx::AcceleratedWidget window) { |
220 if (GetGLImplementation() == kGLImplementationOSMesaGL) { | 378 if (GetGLImplementation() == kGLImplementationOSMesaGL) { |
221 scoped_refptr<GLSurface> surface(new GLSurfaceOSMesaHeadless()); | 379 scoped_refptr<GLSurface> surface(new GLSurfaceOSMesaHeadless()); |
222 if (!surface->Initialize()) | 380 if (!surface->Initialize()) |
223 return NULL; | 381 return NULL; |
224 return surface; | 382 return surface; |
225 } | 383 } |
226 DCHECK(GetGLImplementation() == kGLImplementationEGLGLES2); | 384 DCHECK(GetGLImplementation() == kGLImplementationEGLGLES2); |
227 if (window != kNullAcceleratedWidget) { | 385 if (window != kNullAcceleratedWidget) { |
228 scoped_refptr<GLSurface> surface; | 386 scoped_refptr<GLSurface> surface; |
229 if (GLSurfaceEGL::IsEGLSurfacelessContextSupported() && | 387 if (GLSurfaceEGL::IsEGLSurfacelessContextSupported() && |
230 ui::SurfaceFactoryOzone::GetInstance() | 388 ui::SurfaceFactoryOzone::GetInstance() |
231 ->CanShowPrimaryPlaneAsOverlay()) { | 389 ->CanShowPrimaryPlaneAsOverlay()) { |
232 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone = | 390 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone = |
233 ui::SurfaceFactoryOzone::GetInstance() | 391 ui::SurfaceFactoryOzone::GetInstance() |
234 ->CreateSurfacelessEGLSurfaceForWidget(window); | 392 ->CreateSurfacelessEGLSurfaceForWidget(window); |
235 if (!surface_ozone) | 393 if (!surface_ozone) |
236 return NULL; | 394 return NULL; |
237 surface = new GLSurfaceOzoneSurfaceless(surface_ozone.Pass(), window); | 395 surface = new GLSurfaceOzoneSurfacelessSurfaceImpl(surface_ozone.Pass(), |
396 window); | |
238 } else { | 397 } else { |
239 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone = | 398 scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone = |
240 ui::SurfaceFactoryOzone::GetInstance()->CreateEGLSurfaceForWidget( | 399 ui::SurfaceFactoryOzone::GetInstance()->CreateEGLSurfaceForWidget( |
241 window); | 400 window); |
242 if (!surface_ozone) | 401 if (!surface_ozone) |
243 return NULL; | 402 return NULL; |
244 | 403 |
245 surface = new GLSurfaceOzoneEGL(surface_ozone.Pass(), window); | 404 surface = new GLSurfaceOzoneEGL(surface_ozone.Pass(), window); |
246 } | 405 } |
247 if (!surface->Initialize()) | 406 if (!surface->Initialize()) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
283 NOTREACHED(); | 442 NOTREACHED(); |
284 return NULL; | 443 return NULL; |
285 } | 444 } |
286 } | 445 } |
287 | 446 |
288 EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() { | 447 EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() { |
289 return ui::SurfaceFactoryOzone::GetInstance()->GetNativeDisplay(); | 448 return ui::SurfaceFactoryOzone::GetInstance()->GetNativeDisplay(); |
290 } | 449 } |
291 | 450 |
292 } // namespace gfx | 451 } // namespace gfx |
OLD | NEW |