Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / gl / gl_surface_ozone.cc
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/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"
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:
24   GLSurfaceOzoneEGL(scoped_ptr<SurfaceOzoneEGL> ozone_surface)
25       : NativeViewGLSurfaceEGL(ozone_surface->GetNativeWindow()),
26         ozone_surface_(ozone_surface.Pass()) {}
27
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   }
34
35  private:
36   virtual ~GLSurfaceOzoneEGL() {
37     Destroy();  // EGL surface must be destroyed before SurfaceOzone
38   }
39
40   // The native surface. Deleting this is allowed to free the EGLNativeWindow.
41   scoped_ptr<SurfaceOzoneEGL> ozone_surface_;
42
43   DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneEGL);
44 };
45
46 }  // namespace
47
48 // static
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";
55         return false;
56       }
57
58       if (!GLSurfaceEGL::InitializeOneOff()) {
59         LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed.";
60         return false;
61       }
62
63       return true;
64     case kGLImplementationOSMesaGL:
65     case kGLImplementationMockGL:
66       return true;
67     default:
68       return false;
69   }
70 }
71
72 // static
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())
78       return NULL;
79     return surface;
80   }
81   DCHECK(GetGLImplementation() == kGLImplementationEGLGLES2);
82   if (window != kNullAcceleratedWidget) {
83     scoped_ptr<SurfaceOzoneEGL> surface_ozone =
84         SurfaceFactoryOzone::GetInstance()->CreateEGLSurfaceForWidget(window);
85     if (!surface_ozone)
86       return NULL;
87
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()))
93       return NULL;
94     return surface;
95   } else {
96     scoped_refptr<GLSurface> surface = new GLSurfaceStub();
97     if (surface->Initialize())
98       return surface;
99   }
100   return NULL;
101 }
102
103 // static
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())
110         return NULL;
111
112       return surface;
113     }
114     case kGLImplementationEGLGLES2: {
115       scoped_refptr<GLSurface> surface;
116       if (GLSurfaceEGL::IsEGLSurfacelessContextSupported() &&
117           (size.width() == 1 && size.height() == 1)) {
118         surface = new SurfacelessEGL(size);
119       } else
120         surface = new PbufferGLSurfaceEGL(size);
121
122       if (!surface->Initialize())
123         return NULL;
124       return surface;
125     }
126     default:
127       NOTREACHED();
128       return NULL;
129   }
130 }
131
132 EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() {
133   return SurfaceFactoryOzone::GetInstance()->GetNativeDisplay();
134 }
135
136 }  // namespace gfx