EME test page application.
[chromium-blink-merge.git] / ui / gl / gl_surface_ozone.cc
blob3fa5958aa1508f8130ce1de04db367a5ea97a8a4
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);
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<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 (gfx::SurfaceFactoryOzone::GetInstance()->InitializeHardware() !=
59 gfx::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<SurfaceOzoneEGL> surface_ozone =
90 SurfaceFactoryOzone::GetInstance()->CreateEGLSurfaceForWidget(window);
91 if (!surface_ozone)
92 return NULL;
94 scoped_ptr<VSyncProvider> vsync_provider =
95 surface_ozone->CreateVSyncProvider();
96 scoped_refptr<GLSurfaceOzoneEGL> surface =
97 new GLSurfaceOzoneEGL(surface_ozone.Pass());
98 if (!surface->Initialize(vsync_provider.Pass()))
99 return NULL;
100 return surface;
101 } else {
102 scoped_refptr<GLSurface> surface = new GLSurfaceStub();
103 if (surface->Initialize())
104 return surface;
106 return NULL;
109 // static
110 scoped_refptr<GLSurface> GLSurface::CreateOffscreenGLSurface(
111 const gfx::Size& size) {
112 switch (GetGLImplementation()) {
113 case kGLImplementationOSMesaGL: {
114 scoped_refptr<GLSurface> surface(new GLSurfaceOSMesa(1, size));
115 if (!surface->Initialize())
116 return NULL;
118 return surface;
120 case kGLImplementationEGLGLES2: {
121 scoped_refptr<GLSurface> surface;
122 if (GLSurfaceEGL::IsEGLSurfacelessContextSupported() &&
123 (size.width() == 0 && size.height() == 0)) {
124 surface = new SurfacelessEGL(size);
125 } else
126 surface = new PbufferGLSurfaceEGL(size);
128 if (!surface->Initialize())
129 return NULL;
130 return surface;
132 default:
133 NOTREACHED();
134 return NULL;
138 EGLNativeDisplayType GetPlatformDefaultEGLNativeDisplay() {
139 return SurfaceFactoryOzone::GetInstance()->GetNativeDisplay();
142 } // namespace gfx