OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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_android.h" | |
6 | |
7 #include <EGL/egl.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/memory/ref_counted.h" | |
11 #include "ui/gfx/gl/egl_util.h" | |
12 #include "ui/gfx/gl/gl_bindings.h" | |
13 #include "ui/gfx/gl/gl_context.h" | |
14 #include "ui/gfx/gl/gl_implementation.h" | |
15 #include "ui/gfx/gl/android_native_window.h" | |
16 | |
17 namespace gfx { | |
18 | |
19 bool GLSurface::InitializeOneOffInternal() { | |
20 static bool initialized = false; | |
21 if (initialized) | |
22 return true; | |
23 | |
24 switch (GetGLImplementation()) { | |
25 case kGLImplementationEGLGLES2: | |
26 if (!GLSurfaceEGL::InitializeOneOff()) { | |
27 LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed."; | |
28 return false; | |
29 } | |
30 break; | |
31 default: | |
32 NOTREACHED(); | |
33 break; | |
34 } | |
35 | |
36 initialized = true; | |
37 return true; | |
38 } | |
39 // static | |
40 scoped_refptr<GLSurface> | |
41 GLSurface::CreateViewGLSurface(bool software, gfx::AcceleratedWidget window) { | |
42 if (software) | |
43 return NULL; | |
44 | |
45 switch (GetGLImplementation()) { | |
46 case kGLImplementationEGLGLES2: { | |
47 // window is unused | |
48 scoped_refptr<AndroidViewSurface> surface(new AndroidViewSurface()); | |
49 if (!surface->Initialize()) | |
50 return NULL; | |
51 return surface; | |
52 } | |
53 default: | |
54 NOTREACHED(); | |
55 return NULL; | |
56 } | |
57 } | |
58 | |
59 // static | |
60 scoped_refptr<GLSurface> | |
61 GLSurface::CreateOffscreenGLSurface(bool software, const gfx::Size& size) { | |
62 if (software) | |
63 return NULL; | |
64 | |
65 switch (GetGLImplementation()) { | |
66 case kGLImplementationEGLGLES2: { | |
67 scoped_refptr<PbufferGLSurfaceEGL> surface( | |
68 new PbufferGLSurfaceEGL(false, size)); | |
69 if (!surface->Initialize()) | |
70 return NULL; | |
71 return surface; | |
72 } | |
73 default: | |
74 NOTREACHED(); | |
75 return NULL; | |
76 } | |
77 } | |
78 | |
79 AndroidViewSurface::AndroidViewSurface() | |
80 : NativeViewGLSurfaceEGL(false, 0), | |
81 pbuffer_surface_(new PbufferGLSurfaceEGL(false, Size(1, 1))), | |
82 window_(NULL) { | |
83 } | |
84 | |
85 AndroidViewSurface::~AndroidViewSurface() { | |
86 } | |
87 | |
88 bool AndroidViewSurface::Initialize() { | |
89 DCHECK(pbuffer_surface_.get()); | |
90 return pbuffer_surface_->Initialize(); | |
91 } | |
92 | |
93 void AndroidViewSurface::Destroy() { | |
94 if (pbuffer_surface_.get()) { | |
95 pbuffer_surface_->Destroy(); | |
96 } else { | |
97 window_ = NULL; | |
98 } | |
99 NativeViewGLSurfaceEGL::Destroy(); | |
100 } | |
101 | |
102 bool AndroidViewSurface::IsOffscreen() { | |
103 return false; | |
104 } | |
105 | |
106 bool AndroidViewSurface::SwapBuffers() { | |
107 if (!pbuffer_surface_.get()) | |
108 return NativeViewGLSurfaceEGL::SwapBuffers(); | |
109 return true; | |
110 } | |
111 | |
112 gfx::Size AndroidViewSurface::GetSize() { | |
113 if (pbuffer_surface_.get()) | |
114 return pbuffer_surface_->GetSize(); | |
115 else | |
116 return NativeViewGLSurfaceEGL::GetSize(); | |
117 } | |
118 | |
119 EGLSurface AndroidViewSurface::GetHandle() { | |
120 if (pbuffer_surface_.get()) | |
121 return pbuffer_surface_->GetHandle(); | |
122 else | |
123 return NativeViewGLSurfaceEGL::GetHandle(); | |
124 } | |
125 | |
126 bool AndroidViewSurface::Resize(const gfx::Size& size) { | |
127 if (pbuffer_surface_.get()) | |
128 return pbuffer_surface_->Resize(size); | |
129 else if (GetHandle()) { | |
130 DCHECK(window_ && window_->GetNativeHandle()); | |
131 // Deactivate and restore any currently active context. | |
132 EGLContext context = eglGetCurrentContext(); | |
133 if (context != EGL_NO_CONTEXT) { | |
134 eglMakeCurrent(GetDisplay(), EGL_NO_SURFACE, EGL_NO_SURFACE, | |
135 EGL_NO_CONTEXT); | |
136 } | |
137 NativeViewGLSurfaceEGL::Destroy(); | |
138 if (CreateWindowSurface(window_)) { | |
139 if (context != EGL_NO_CONTEXT) | |
140 eglMakeCurrent(GetDisplay(), GetHandle(), GetHandle(), context); | |
141 } | |
142 } | |
143 return true; | |
144 } | |
145 | |
146 bool AndroidViewSurface::CreateWindowSurface(AndroidNativeWindow* window) { | |
147 DCHECK(window->GetNativeHandle()); | |
148 window_ = window; | |
149 EGLSurface surface = eglCreateWindowSurface(GetDisplay(), | |
150 GetConfig(), | |
151 window->GetNativeHandle(), | |
152 NULL); | |
153 if (surface == EGL_NO_SURFACE) { | |
154 LOG(ERROR) << "eglCreateWindowSurface failed with error " | |
155 << GetLastEGLErrorString(); | |
156 Destroy(); | |
157 return false; | |
158 } | |
159 | |
160 SetHandle(surface); | |
161 return true; | |
162 } | |
163 | |
164 void AndroidViewSurface::SetNativeWindow(AndroidNativeWindow* window) { | |
165 if (window->GetNativeHandle()) { | |
166 DCHECK(pbuffer_surface_.get()); | |
167 pbuffer_surface_->Destroy(); | |
168 pbuffer_surface_ = NULL; | |
169 | |
170 CreateWindowSurface(window); | |
171 } else { | |
172 DCHECK(GetHandle()); | |
173 NativeViewGLSurfaceEGL::Destroy(); | |
174 window_ = NULL; | |
175 | |
176 pbuffer_surface_ = new PbufferGLSurfaceEGL(false, Size(1,1)); | |
177 pbuffer_surface_->Initialize(); | |
178 } | |
179 } | |
180 | |
181 } // namespace gfx | |
OLD | NEW |