Upstream version 6.35.121.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/gl/gl_implementation.h"
12 #include "ui/gl/gl_surface_egl.h"
13 #include "ui/gl/gl_surface_osmesa.h"
14 #include "ui/gl/gl_surface_stub.h"
15
16 namespace gfx {
17
18 // static
19 bool GLSurface::InitializeOneOffInternal() {
20   switch (GetGLImplementation()) {
21     case kGLImplementationEGLGLES2:
22       if (gfx::SurfaceFactoryOzone::GetInstance()->InitializeHardware() !=
23           gfx::SurfaceFactoryOzone::INITIALIZED) {
24         LOG(ERROR) << "Ozone failed to initialize hardware";
25         return false;
26       }
27
28       if (!GLSurfaceEGL::InitializeOneOff()) {
29         LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed.";
30         return false;
31       }
32     default:
33       break;
34   }
35   return true;
36 }
37
38 // static
39 scoped_refptr<GLSurface> GLSurface::CreateViewGLSurface(
40     gfx::AcceleratedWidget window) {
41   if (GetGLImplementation() == kGLImplementationOSMesaGL) {
42     scoped_refptr<GLSurface> surface(new GLSurfaceOSMesaHeadless());
43     if (!surface->Initialize())
44       return NULL;
45     return surface;
46   }
47   DCHECK(GetGLImplementation() == kGLImplementationEGLGLES2);
48   if (window != kNullAcceleratedWidget) {
49     EGLNativeWindowType egl_window =
50         gfx::SurfaceFactoryOzone::GetInstance()->RealizeAcceleratedWidget(
51             window);
52     scoped_ptr<VSyncProvider> sync_provider =
53         gfx::SurfaceFactoryOzone::GetInstance()->CreateVSyncProvider(
54             egl_window);
55     scoped_refptr<NativeViewGLSurfaceEGL> surface =
56         new NativeViewGLSurfaceEGL(egl_window);
57     if (surface->Initialize(sync_provider.Pass()))
58       return surface;
59   } else {
60     scoped_refptr<GLSurface> surface = new GLSurfaceStub();
61     if (surface->Initialize())
62       return surface;
63   }
64   return NULL;
65 }
66
67 // static
68 scoped_refptr<GLSurface> GLSurface::CreateOffscreenGLSurface(
69     const gfx::Size& size) {
70   switch (GetGLImplementation()) {
71     case kGLImplementationOSMesaGL: {
72       scoped_refptr<GLSurface> surface(new GLSurfaceOSMesa(1, size));
73       if (!surface->Initialize())
74         return NULL;
75
76       return surface;
77     }
78     case kGLImplementationEGLGLES2: {
79       scoped_refptr<GLSurface> surface;
80       if (GLSurfaceEGL::IsEGLSurfacelessContextSupported() &&
81           (size.width() == 1 && size.height() == 1)) {
82         surface = new SurfacelessEGL(size);
83       } else
84         surface = new PbufferGLSurfaceEGL(size);
85
86       if (!surface->Initialize())
87         return NULL;
88       return surface;
89     }
90     default:
91       NOTREACHED();
92       return NULL;
93   }
94 }
95
96 EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() {
97   return SurfaceFactoryOzone::GetInstance()->GetNativeDisplay();
98 }
99
100 }  // namespace gfx