OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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/gfx/gl/gl_surface.h" | |
6 | |
7 #include <wayland-egl.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "ui/gfx/gl/egl_util.h" | |
12 #include "ui/gfx/gl/gl_bindings.h" | |
13 #include "ui/gfx/gl/gl_implementation.h" | |
14 #include "ui/gfx/gl/gl_surface_egl.h" | |
15 #include "ui/wayland/wayland_display.h" | |
16 | |
17 namespace { | |
18 | |
19 // Encapsulates a pixmap EGL surface. | |
20 class PixmapGLSurfaceEGL : public gfx::GLSurfaceEGL { | |
21 public: | |
22 explicit PixmapGLSurfaceEGL(bool software, const gfx::Size& size); | |
23 virtual ~PixmapGLSurfaceEGL(); | |
24 | |
25 // Implement GLSurface. | |
26 virtual bool Initialize(); | |
27 virtual void Destroy(); | |
28 virtual bool IsOffscreen(); | |
29 virtual bool SwapBuffers(); | |
30 virtual gfx::Size GetSize(); | |
31 virtual EGLSurface GetHandle(); | |
32 | |
33 private: | |
34 gfx::Size size_; | |
35 EGLSurface surface_; | |
36 EGLNativePixmapType pixmap_; | |
37 | |
38 DISALLOW_COPY_AND_ASSIGN(PixmapGLSurfaceEGL); | |
39 }; | |
40 | |
41 PixmapGLSurfaceEGL::PixmapGLSurfaceEGL(bool software, const gfx::Size& size) | |
42 : size_(size), | |
43 surface_(NULL), | |
44 pixmap_(NULL) { | |
45 software_ = software; | |
46 } | |
47 | |
48 PixmapGLSurfaceEGL::~PixmapGLSurfaceEGL() { | |
49 Destroy(); | |
50 } | |
51 | |
52 bool PixmapGLSurfaceEGL::Initialize() { | |
53 DCHECK(!surface_); | |
54 DCHECK(!pixmap_); | |
55 | |
56 pixmap_ = wl_egl_pixmap_create( | |
57 size_.width(), | |
58 size_.height(), | |
59 0); | |
60 surface_ = eglCreatePixmapSurface(gfx::GLSurfaceEGL::GetDisplay(), | |
61 gfx::GLSurfaceEGL::GetConfig(), | |
62 pixmap_, | |
63 NULL); | |
64 if (!surface_) { | |
65 LOG(ERROR) << "eglCreatePixmapSurface failed with error " | |
66 << gfx::GetLastEGLErrorString(); | |
67 Destroy(); | |
68 return false; | |
69 } | |
70 | |
71 return true; | |
72 } | |
73 | |
74 void PixmapGLSurfaceEGL::Destroy() { | |
75 if (surface_) { | |
76 if (!eglDestroySurface(gfx::GLSurfaceEGL::GetDisplay(), surface_)) { | |
77 LOG(ERROR) << "eglDestroySurface failed with error " | |
78 << gfx::GetLastEGLErrorString(); | |
79 } | |
80 surface_ = NULL; | |
81 } | |
82 if (pixmap_) { | |
83 wl_egl_pixmap_destroy(pixmap_); | |
84 pixmap_ = NULL; | |
85 } | |
86 } | |
87 | |
88 bool PixmapGLSurfaceEGL::IsOffscreen() { | |
89 return true; | |
90 } | |
91 | |
92 bool PixmapGLSurfaceEGL::SwapBuffers() { | |
93 NOTREACHED() << "Attempted to call SwapBuffers on a PixmapGLSurfaceEGL."; | |
94 return false; | |
95 } | |
96 | |
97 gfx::Size PixmapGLSurfaceEGL::GetSize() { | |
98 return size_; | |
99 } | |
100 | |
101 EGLSurface PixmapGLSurfaceEGL::GetHandle() { | |
102 return surface_; | |
103 } | |
104 | |
105 } | |
106 | |
107 namespace gfx { | |
108 | |
109 bool GLSurface::InitializeOneOff() { | |
110 static bool initialized = false; | |
111 if (initialized) | |
112 return true; | |
113 | |
114 static const GLImplementation kAllowedGLImplementations[] = { | |
115 kGLImplementationEGLGLES2 | |
116 }; | |
117 | |
118 if (!InitializeRequestedGLBindings( | |
119 kAllowedGLImplementations, | |
120 kAllowedGLImplementations + arraysize(kAllowedGLImplementations), | |
121 kGLImplementationEGLGLES2)) { | |
122 LOG(ERROR) << "InitializeRequestedGLBindings failed."; | |
123 return false; | |
124 } | |
125 | |
126 if (!GLSurfaceEGL::InitializeOneOff()) { | |
127 LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed."; | |
128 return false; | |
129 } | |
130 | |
131 initialized = true; | |
132 return true; | |
133 } | |
134 | |
135 scoped_refptr<GLSurface> GLSurface::CreateViewGLSurface( | |
136 bool software, | |
137 gfx::PluginWindowHandle window) { | |
138 | |
139 if (software) | |
140 return NULL; | |
141 | |
142 scoped_refptr<GLSurface> surface( | |
143 new NativeViewGLSurfaceEGL(software, window)); | |
144 if (!surface->Initialize()) | |
145 return NULL; | |
146 | |
147 return surface; | |
148 } | |
149 | |
150 scoped_refptr<GLSurface> GLSurface::CreateOffscreenGLSurface( | |
151 bool software, | |
152 const gfx::Size& size) { | |
153 if (software) | |
154 return NULL; | |
155 | |
156 scoped_refptr<GLSurface> surface(new PixmapGLSurfaceEGL(software, size)); | |
157 if (!surface->Initialize()) | |
158 return NULL; | |
159 | |
160 return surface; | |
161 } | |
162 | |
163 } // namespace gfx | |
OLD | NEW |