OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 <EGL/egl.h> |
| 6 |
| 7 #include "base/scoped_ptr.h" |
| 8 #include "app/gfx/gl/gl_bindings.h" |
| 9 #include "app/gfx/gl/gl_context_egl.h" |
| 10 |
| 11 namespace gfx { |
| 12 |
| 13 namespace { |
| 14 |
| 15 // The EGL configuration to use. |
| 16 EGLDisplay g_display; |
| 17 EGLConfig g_config; |
| 18 |
| 19 bool InitializeOneOff() { |
| 20 static bool initialized = false; |
| 21 if (initialized) |
| 22 return true; |
| 23 |
| 24 g_display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 25 if (!g_display) |
| 26 return false; |
| 27 |
| 28 if (!eglInitialize(g_display, NULL, NULL) == EGL_TRUE) |
| 29 return false; |
| 30 |
| 31 // Choose an EGL configuration. |
| 32 static const EGLint kConfigAttribs[] = { |
| 33 EGL_BUFFER_SIZE, 32, |
| 34 EGL_ALPHA_SIZE, 8, |
| 35 EGL_BLUE_SIZE, 8, |
| 36 EGL_RED_SIZE, 8, |
| 37 EGL_DEPTH_SIZE, 24, |
| 38 EGL_STENCIL_SIZE, 8, |
| 39 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 40 EGL_NONE |
| 41 }; |
| 42 |
| 43 EGLint num_configs; |
| 44 if (!eglChooseConfig(g_display, |
| 45 kConfigAttribs, |
| 46 NULL, |
| 47 0, |
| 48 &num_configs)) { |
| 49 return false; |
| 50 } |
| 51 |
| 52 if (num_configs == 0) |
| 53 return false; |
| 54 |
| 55 scoped_array<EGLConfig> configs(new EGLConfig[num_configs]); |
| 56 if (!eglChooseConfig(g_display, |
| 57 kConfigAttribs, |
| 58 configs.get(), |
| 59 num_configs, |
| 60 &num_configs)) { |
| 61 return false; |
| 62 } |
| 63 |
| 64 g_config = configs[0]; |
| 65 |
| 66 initialized = true; |
| 67 return true; |
| 68 } |
| 69 } // namespace anonymous |
| 70 |
| 71 NativeViewEGLContext::NativeViewEGLContext(void* window) |
| 72 : window_(window), |
| 73 surface_(NULL), |
| 74 context_(NULL) |
| 75 { |
| 76 } |
| 77 |
| 78 NativeViewEGLContext::~NativeViewEGLContext() { |
| 79 } |
| 80 |
| 81 bool NativeViewEGLContext::Initialize() { |
| 82 DCHECK(!context_); |
| 83 |
| 84 if (!InitializeOneOff()) |
| 85 return NULL; |
| 86 |
| 87 // Create a surface for the native window. |
| 88 surface_ = eglCreateWindowSurface(g_display, |
| 89 g_config, |
| 90 static_cast<EGLNativeWindowType>(window_), |
| 91 NULL); |
| 92 if (!surface_) { |
| 93 Destroy(); |
| 94 return false; |
| 95 } |
| 96 |
| 97 // Create a context. |
| 98 context_ = eglCreateContext(g_display, g_config, NULL, NULL); |
| 99 if (!context_) { |
| 100 Destroy(); |
| 101 return false; |
| 102 } |
| 103 |
| 104 if (!MakeCurrent()) { |
| 105 Destroy(); |
| 106 return false; |
| 107 } |
| 108 |
| 109 if (!InitializeCommon()) { |
| 110 Destroy(); |
| 111 return false; |
| 112 } |
| 113 |
| 114 return true; |
| 115 } |
| 116 |
| 117 void NativeViewEGLContext::Destroy() { |
| 118 if (context_) { |
| 119 eglDestroyContext(g_display, context_); |
| 120 context_ = NULL; |
| 121 } |
| 122 |
| 123 if (surface_) { |
| 124 eglDestroySurface(g_display, surface_); |
| 125 surface_ = NULL; |
| 126 } |
| 127 } |
| 128 |
| 129 bool NativeViewEGLContext::MakeCurrent() { |
| 130 DCHECK(context_); |
| 131 return eglMakeCurrent(g_display, |
| 132 surface_, surface_, |
| 133 context_) == GL_TRUE; |
| 134 } |
| 135 |
| 136 bool NativeViewEGLContext::IsCurrent() { |
| 137 DCHECK(context_); |
| 138 return context_ == eglGetCurrentContext(); |
| 139 } |
| 140 |
| 141 bool NativeViewEGLContext::IsOffscreen() { |
| 142 return false; |
| 143 } |
| 144 |
| 145 void NativeViewEGLContext::SwapBuffers() { |
| 146 eglSwapBuffers(g_display, surface_); |
| 147 } |
| 148 |
| 149 gfx::Size NativeViewEGLContext::GetSize() { |
| 150 #if defined(OS_WIN) |
| 151 RECT rect; |
| 152 CHECK(GetClientRect(static_cast<HWND>(window_), &rect)); |
| 153 return gfx::Size(rect.right - rect.left, rect.bottom - rect.top); |
| 154 #else |
| 155 NOTREACHED() |
| 156 << "NativeViewEGLContext::GetSize not implemented on this platform."; |
| 157 #endif |
| 158 } |
| 159 |
| 160 void* NativeViewEGLContext::GetHandle() { |
| 161 return context_; |
| 162 } |
| 163 |
| 164 EGLSurface NativeViewEGLContext::GetSurface() { |
| 165 return surface_; |
| 166 } |
| 167 |
| 168 SecondaryEGLContext::SecondaryEGLContext() |
| 169 : surface_(NULL), |
| 170 context_(NULL) |
| 171 { |
| 172 } |
| 173 |
| 174 SecondaryEGLContext::~SecondaryEGLContext() { |
| 175 } |
| 176 |
| 177 bool SecondaryEGLContext::Initialize(GLContext* shared_context) { |
| 178 DCHECK(shared_context); |
| 179 DCHECK(!context_); |
| 180 |
| 181 if (!InitializeOneOff()) |
| 182 return NULL; |
| 183 |
| 184 surface_ = static_cast<BaseEGLContext*>(shared_context)->GetSurface(); |
| 185 |
| 186 // Create a context. |
| 187 context_ = eglCreateContext(g_display, |
| 188 g_config, |
| 189 shared_context->GetHandle(), |
| 190 NULL); |
| 191 if (!context_) { |
| 192 Destroy(); |
| 193 return false; |
| 194 } |
| 195 |
| 196 return true; |
| 197 } |
| 198 |
| 199 void SecondaryEGLContext::Destroy() { |
| 200 surface_ = NULL; |
| 201 |
| 202 if (context_) { |
| 203 eglDestroyContext(g_display, context_); |
| 204 context_ = NULL; |
| 205 } |
| 206 } |
| 207 |
| 208 bool SecondaryEGLContext::MakeCurrent() { |
| 209 DCHECK(context_); |
| 210 return eglMakeCurrent(g_display, |
| 211 surface_, surface_, |
| 212 context_) == GL_TRUE; |
| 213 } |
| 214 |
| 215 bool SecondaryEGLContext::IsCurrent() { |
| 216 DCHECK(context_); |
| 217 return context_ == eglGetCurrentContext(); |
| 218 } |
| 219 |
| 220 bool SecondaryEGLContext::IsOffscreen() { |
| 221 return true; |
| 222 } |
| 223 |
| 224 void SecondaryEGLContext::SwapBuffers() { |
| 225 NOTREACHED() << "Attempted to call SwapBuffers on a SecondaryEGLContext."; |
| 226 } |
| 227 |
| 228 gfx::Size SecondaryEGLContext::GetSize() { |
| 229 NOTREACHED() << "Should not be requesting size of this SecondaryEGLContext."; |
| 230 return gfx::Size(1, 1); |
| 231 } |
| 232 |
| 233 void* SecondaryEGLContext::GetHandle() { |
| 234 return context_; |
| 235 } |
| 236 |
| 237 EGLSurface SecondaryEGLContext::GetSurface() { |
| 238 return surface_; |
| 239 } |
| 240 |
| 241 } // namespace gfx |
OLD | NEW |