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
3 // found in the LICENSE file.
5 #include "ui/gl/gl_surface.h"
7 #include "base/logging.h"
8 #include "base/memory/ref_counted.h"
9 #include "ui/gfx/native_widget_types.h"
10 #include "ui/gfx/ozone/surface_factory_ozone.h"
11 #include "ui/gfx/ozone/surface_ozone_egl.h"
12 #include "ui/gl/gl_implementation.h"
13 #include "ui/gl/gl_surface_egl.h"
14 #include "ui/gl/gl_surface_osmesa.h"
15 #include "ui/gl/gl_surface_stub.h"
21 // A thin wrapper around GLSurfaceEGL that owns the EGLNativeWindow
22 class GL_EXPORT GLSurfaceOzoneEGL
: public NativeViewGLSurfaceEGL
{
24 GLSurfaceOzoneEGL(scoped_ptr
<SurfaceOzoneEGL
> ozone_surface
)
25 : NativeViewGLSurfaceEGL(ozone_surface
->GetNativeWindow()),
26 ozone_surface_(ozone_surface
.Pass()) {}
28 virtual bool Resize(const gfx::Size
& size
) OVERRIDE
{
29 if (!ozone_surface_
->ResizeNativeWindow(size
))
32 return NativeViewGLSurfaceEGL::Resize(size
);
36 virtual ~GLSurfaceOzoneEGL() {
37 Destroy(); // EGL surface must be destroyed before SurfaceOzone
40 // The native surface. Deleting this is allowed to free the EGLNativeWindow.
41 scoped_ptr
<SurfaceOzoneEGL
> ozone_surface_
;
43 DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneEGL
);
49 bool GLSurface::InitializeOneOffInternal() {
50 switch (GetGLImplementation()) {
51 case kGLImplementationEGLGLES2
:
52 if (gfx::SurfaceFactoryOzone::GetInstance()->InitializeHardware() !=
53 gfx::SurfaceFactoryOzone::INITIALIZED
) {
54 LOG(ERROR
) << "Ozone failed to initialize hardware";
58 if (!GLSurfaceEGL::InitializeOneOff()) {
59 LOG(ERROR
) << "GLSurfaceEGL::InitializeOneOff failed.";
64 case kGLImplementationOSMesaGL
:
65 case kGLImplementationMockGL
:
73 scoped_refptr
<GLSurface
> GLSurface::CreateViewGLSurface(
74 gfx::AcceleratedWidget window
) {
75 if (GetGLImplementation() == kGLImplementationOSMesaGL
) {
76 scoped_refptr
<GLSurface
> surface(new GLSurfaceOSMesaHeadless());
77 if (!surface
->Initialize())
81 DCHECK(GetGLImplementation() == kGLImplementationEGLGLES2
);
82 if (window
!= kNullAcceleratedWidget
) {
83 scoped_ptr
<SurfaceOzoneEGL
> surface_ozone
=
84 SurfaceFactoryOzone::GetInstance()->CreateEGLSurfaceForWidget(window
);
88 scoped_ptr
<VSyncProvider
> vsync_provider
=
89 surface_ozone
->CreateVSyncProvider();
90 scoped_refptr
<GLSurfaceOzoneEGL
> surface
=
91 new GLSurfaceOzoneEGL(surface_ozone
.Pass());
92 if (!surface
->Initialize(vsync_provider
.Pass()))
96 scoped_refptr
<GLSurface
> surface
= new GLSurfaceStub();
97 if (surface
->Initialize())
104 scoped_refptr
<GLSurface
> GLSurface::CreateOffscreenGLSurface(
105 const gfx::Size
& size
) {
106 switch (GetGLImplementation()) {
107 case kGLImplementationOSMesaGL
: {
108 scoped_refptr
<GLSurface
> surface(new GLSurfaceOSMesa(1, size
));
109 if (!surface
->Initialize())
114 case kGLImplementationEGLGLES2
: {
115 scoped_refptr
<GLSurface
> surface
;
116 if (GLSurfaceEGL::IsEGLSurfacelessContextSupported() &&
117 (size
.width() == 0 && size
.height() == 0)) {
118 surface
= new SurfacelessEGL(size
);
120 surface
= new PbufferGLSurfaceEGL(size
);
122 if (!surface
->Initialize())
132 EGLNativeDisplayType
GetPlatformDefaultEGLNativeDisplay() {
133 return SurfaceFactoryOzone::GetInstance()->GetNativeDisplay();