Fix crash on key events in touchview.
[chromium-blink-merge.git] / ui / gl / gl_surface_ozone.cc
blob8d9e6ad1c78d3b9daf81df142d1782acc2a89344
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"
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<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);
35 private:
36 virtual ~GLSurfaceOzoneEGL() {
37 Destroy(); // EGL surface must be destroyed before SurfaceOzone
40 // The native surface. Deleting this is allowed to free the EGLNativeWindow.
41 scoped_ptr<SurfaceOzoneEGL> ozone_surface_;
43 DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneEGL);
46 } // namespace
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;
58 if (!GLSurfaceEGL::InitializeOneOff()) {
59 LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed.";
60 return false;
63 return true;
64 case kGLImplementationOSMesaGL:
65 case kGLImplementationMockGL:
66 return true;
67 default:
68 return false;
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;
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;
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;
100 return NULL;
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;
112 return surface;
114 case kGLImplementationEGLGLES2: {
115 scoped_refptr<GLSurface> surface;
116 if (GLSurfaceEGL::IsEGLSurfacelessContextSupported() &&
117 (size.width() == 0 && size.height() == 0)) {
118 surface = new SurfacelessEGL(size);
119 } else
120 surface = new PbufferGLSurfaceEGL(size);
122 if (!surface->Initialize())
123 return NULL;
124 return surface;
126 default:
127 NOTREACHED();
128 return NULL;
132 EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() {
133 return SurfaceFactoryOzone::GetInstance()->GetNativeDisplay();
136 } // namespace gfx