• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "ui/gl/gl_surface.h"
6 
7 #include "base/logging.h"
8 #include "base/memory/ref_counted.h"
9 #include "ui/gfx/native_widget_types.h"
10 #include "ui/gl/gl_implementation.h"
11 #include "ui/gl/gl_surface_egl.h"
12 #include "ui/gl/gl_surface_osmesa.h"
13 #include "ui/gl/gl_surface_stub.h"
14 #include "ui/ozone/public/surface_factory_ozone.h"
15 #include "ui/ozone/public/surface_ozone_egl.h"
16 
17 namespace gfx {
18 
19 namespace {
20 
21 // A thin wrapper around GLSurfaceEGL that owns the EGLNativeWindow
22 class GL_EXPORT GLSurfaceOzoneEGL : public NativeViewGLSurfaceEGL {
23  public:
GLSurfaceOzoneEGL(scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface)24   GLSurfaceOzoneEGL(scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface)
25       : NativeViewGLSurfaceEGL(ozone_surface->GetNativeWindow()),
26         ozone_surface_(ozone_surface.Pass()) {}
27 
Resize(const gfx::Size & size)28   virtual bool Resize(const gfx::Size& size) OVERRIDE {
29     if (!ozone_surface_->ResizeNativeWindow(size))
30       return false;
31 
32     return NativeViewGLSurfaceEGL::Resize(size);
33   }
SwapBuffers()34   virtual bool SwapBuffers() OVERRIDE {
35     if (!NativeViewGLSurfaceEGL::SwapBuffers())
36       return false;
37 
38     return ozone_surface_->OnSwapBuffers();
39   }
40 
41  private:
~GLSurfaceOzoneEGL()42   virtual ~GLSurfaceOzoneEGL() {
43     Destroy();  // EGL surface must be destroyed before SurfaceOzone
44   }
45 
46   // The native surface. Deleting this is allowed to free the EGLNativeWindow.
47   scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface_;
48 
49   DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneEGL);
50 };
51 
52 }  // namespace
53 
54 // static
InitializeOneOffInternal()55 bool GLSurface::InitializeOneOffInternal() {
56   switch (GetGLImplementation()) {
57     case kGLImplementationEGLGLES2:
58       if (ui::SurfaceFactoryOzone::GetInstance()->InitializeHardware() !=
59           ui::SurfaceFactoryOzone::INITIALIZED) {
60         LOG(ERROR) << "Ozone failed to initialize hardware";
61         return false;
62       }
63 
64       if (!GLSurfaceEGL::InitializeOneOff()) {
65         LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed.";
66         return false;
67       }
68 
69       return true;
70     case kGLImplementationOSMesaGL:
71     case kGLImplementationMockGL:
72       return true;
73     default:
74       return false;
75   }
76 }
77 
78 // static
CreateViewGLSurface(gfx::AcceleratedWidget window)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())
84       return NULL;
85     return surface;
86   }
87   DCHECK(GetGLImplementation() == kGLImplementationEGLGLES2);
88   if (window != kNullAcceleratedWidget) {
89     scoped_ptr<ui::SurfaceOzoneEGL> surface_ozone =
90         ui::SurfaceFactoryOzone::GetInstance()->CreateEGLSurfaceForWidget(
91             window);
92     if (!surface_ozone)
93       return NULL;
94 
95     scoped_ptr<VSyncProvider> vsync_provider =
96         surface_ozone->CreateVSyncProvider();
97     scoped_refptr<GLSurfaceOzoneEGL> surface =
98         new GLSurfaceOzoneEGL(surface_ozone.Pass());
99     if (!surface->Initialize(vsync_provider.Pass()))
100       return NULL;
101     return surface;
102   } else {
103     scoped_refptr<GLSurface> surface = new GLSurfaceStub();
104     if (surface->Initialize())
105       return surface;
106   }
107   return NULL;
108 }
109 
110 // static
CreateOffscreenGLSurface(const gfx::Size & size)111 scoped_refptr<GLSurface> GLSurface::CreateOffscreenGLSurface(
112     const gfx::Size& size) {
113   switch (GetGLImplementation()) {
114     case kGLImplementationOSMesaGL: {
115       scoped_refptr<GLSurface> surface(new GLSurfaceOSMesa(1, size));
116       if (!surface->Initialize())
117         return NULL;
118 
119       return surface;
120     }
121     case kGLImplementationEGLGLES2: {
122       scoped_refptr<GLSurface> surface;
123       if (GLSurfaceEGL::IsEGLSurfacelessContextSupported() &&
124           (size.width() == 0 && size.height() == 0)) {
125         surface = new SurfacelessEGL(size);
126       } else
127         surface = new PbufferGLSurfaceEGL(size);
128 
129       if (!surface->Initialize())
130         return NULL;
131       return surface;
132     }
133     default:
134       NOTREACHED();
135       return NULL;
136   }
137 }
138 
GetPlatformDefaultEGLNativeDisplay()139 EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() {
140   return ui::SurfaceFactoryOzone::GetInstance()->GetNativeDisplay();
141 }
142 
143 }  // namespace gfx
144