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
);
34 virtual bool SwapBuffers() OVERRIDE
{
35 if (!NativeViewGLSurfaceEGL::SwapBuffers())
38 return ozone_surface_
->OnSwapBuffers();
42 virtual ~GLSurfaceOzoneEGL() {
43 Destroy(); // EGL surface must be destroyed before SurfaceOzone
46 // The native surface. Deleting this is allowed to free the EGLNativeWindow.
47 scoped_ptr
<SurfaceOzoneEGL
> ozone_surface_
;
49 DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneEGL
);
55 bool GLSurface::InitializeOneOffInternal() {
56 switch (GetGLImplementation()) {
57 case kGLImplementationEGLGLES2
:
58 if (gfx::SurfaceFactoryOzone::GetInstance()->InitializeHardware() !=
59 gfx::SurfaceFactoryOzone::INITIALIZED
) {
60 LOG(ERROR
) << "Ozone failed to initialize hardware";
64 if (!GLSurfaceEGL::InitializeOneOff()) {
65 LOG(ERROR
) << "GLSurfaceEGL::InitializeOneOff failed.";
70 case kGLImplementationOSMesaGL
:
71 case kGLImplementationMockGL
:
79 scoped_refptr
<GLSurface
> GLSurface::CreateViewGLSurface(
80 gfx::AcceleratedWidget window
) {
81 if (GetGLImplementation() == kGLImplementationOSMesaGL
) {
82 scoped_refptr
<GLSurface
> surface(new GLSurfaceOSMesaHeadless());
83 if (!surface
->Initialize())
87 DCHECK(GetGLImplementation() == kGLImplementationEGLGLES2
);
88 if (window
!= kNullAcceleratedWidget
) {
89 scoped_ptr
<SurfaceOzoneEGL
> surface_ozone
=
90 SurfaceFactoryOzone::GetInstance()->CreateEGLSurfaceForWidget(window
);
94 scoped_ptr
<VSyncProvider
> vsync_provider
=
95 surface_ozone
->CreateVSyncProvider();
96 scoped_refptr
<GLSurfaceOzoneEGL
> surface
=
97 new GLSurfaceOzoneEGL(surface_ozone
.Pass());
98 if (!surface
->Initialize(vsync_provider
.Pass()))
102 scoped_refptr
<GLSurface
> surface
= new GLSurfaceStub();
103 if (surface
->Initialize())
110 scoped_refptr
<GLSurface
> GLSurface::CreateOffscreenGLSurface(
111 const gfx::Size
& size
) {
112 switch (GetGLImplementation()) {
113 case kGLImplementationOSMesaGL
: {
114 scoped_refptr
<GLSurface
> surface(new GLSurfaceOSMesa(1, size
));
115 if (!surface
->Initialize())
120 case kGLImplementationEGLGLES2
: {
121 scoped_refptr
<GLSurface
> surface
;
122 if (GLSurfaceEGL::IsEGLSurfacelessContextSupported() &&
123 (size
.width() == 0 && size
.height() == 0)) {
124 surface
= new SurfacelessEGL(size
);
126 surface
= new PbufferGLSurfaceEGL(size
);
128 if (!surface
->Initialize())
138 EGLNativeDisplayType
GetPlatformDefaultEGLNativeDisplay() {
139 return SurfaceFactoryOzone::GetInstance()->GetNativeDisplay();