Fix race condition in gyp/ninja builds.
[chromium-blink-merge.git] / ui / gl / gl_surface_ozone.cc
blob437226163db521e483f7c322ac549c23918d08ec
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/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"
17 namespace gfx {
19 namespace {
21 // A thin wrapper around GLSurfaceEGL that owns the EGLNativeWindow
22 class GL_EXPORT GLSurfaceOzoneEGL : public NativeViewGLSurfaceEGL {
23 public:
24 GLSurfaceOzoneEGL(scoped_ptr<ui::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))
30 return false;
32 return NativeViewGLSurfaceEGL::Resize(size);
34 virtual bool SwapBuffers() OVERRIDE {
35 if (!NativeViewGLSurfaceEGL::SwapBuffers())
36 return false;
38 return ozone_surface_->OnSwapBuffers();
41 private:
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<ui::SurfaceOzoneEGL> ozone_surface_;
49 DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneEGL);
52 } // namespace
54 // static
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;
64 if (!GLSurfaceEGL::InitializeOneOff()) {
65 LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed.";
66 return false;
69 return true;
70 case kGLImplementationOSMesaGL:
71 case kGLImplementationMockGL:
72 return true;
73 default:
74 return false;
78 // static
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;
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;
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;
107 return NULL;
110 // static
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;
119 return surface;
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);
129 if (!surface->Initialize())
130 return NULL;
131 return surface;
133 default:
134 NOTREACHED();
135 return NULL;
139 EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() {
140 return ui::SurfaceFactoryOzone::GetInstance()->GetNativeDisplay();
143 } // namespace gfx