Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/client/jni/egl_thread_context.h" | 5 #include "remoting/client/jni/egl_thread_context.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 |
| 9 namespace remoting { | 9 namespace remoting { |
| 10 | 10 |
| 11 EglThreadContext::EglThreadContext() { | 11 EglThreadContext::EglThreadContext() { |
| 12 DCHECK(thread_checker_.CalledOnValidThread()); | 12 DCHECK(thread_checker_.CalledOnValidThread()); |
| 13 CHECK(eglGetCurrentDisplay() == EGL_NO_DISPLAY); | 13 CHECK(eglGetCurrentDisplay() == EGL_NO_DISPLAY); |
| 14 CHECK(eglGetCurrentContext() == EGL_NO_CONTEXT); | 14 CHECK(eglGetCurrentContext() == EGL_NO_CONTEXT); |
| 15 display_ = eglGetDisplay(EGL_DEFAULT_DISPLAY); | 15 display_ = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 16 if (!display_ || !eglInitialize(display_, NULL, NULL)) { | 16 if (!display_ || !eglInitialize(display_, NULL, NULL)) { |
| 17 LOG(FATAL) << "Failed to initialize EGL display."; | 17 LOG(FATAL) << "Failed to initialize EGL display: " << eglGetError(); |
| 18 } | 18 } |
| 19 | 19 |
| 20 const EGLint config_attribs[] = { | 20 if (CreateContextWithClientVersion(EGL_OPENGL_ES3_BIT, 3)) { |
| 21 EGL_RED_SIZE, 8, | 21 client_version_ = 3; |
| 22 EGL_GREEN_SIZE, 8, | 22 return; |
| 23 EGL_BLUE_SIZE, 8, | |
| 24 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, | |
| 25 EGL_NONE | |
| 26 }; | |
| 27 | |
| 28 EGLint numConfigs; | |
| 29 if (!eglChooseConfig(display_, config_attribs, &config_, 1, &numConfigs)) { | |
| 30 LOG(FATAL) << "Failed to choose config."; | |
| 31 } | 23 } |
| 32 const EGLint context_attribs[] = { | 24 LOG(WARNING) << "Failed to create context for OpenGL ES 3. Falling back to " |
| 33 EGL_CONTEXT_CLIENT_VERSION, 2, | 25 << "OpenGL ES 2."; |
| 34 EGL_NONE | 26 if (CreateContextWithClientVersion(EGL_OPENGL_ES2_BIT, 2)) { |
| 35 }; | 27 client_version_ = 2; |
| 36 context_ = eglCreateContext(display_, config_, EGL_NO_CONTEXT, | 28 } else { |
|
Sergey Ulanov
2016/07/22 20:12:34
nit: for consistency return above and remove else.
Yuwei
2016/07/22 21:22:39
Done.
| |
| 37 context_attribs); | 29 LOG(FATAL) << "Failed to create context: " << eglGetError(); |
| 38 if (!context_) { | |
| 39 LOG(FATAL) << "Failed to create context."; | |
| 40 } | 30 } |
| 41 } | 31 } |
| 42 | 32 |
| 43 EglThreadContext::~EglThreadContext() { | 33 EglThreadContext::~EglThreadContext() { |
| 44 DCHECK(thread_checker_.CalledOnValidThread()); | 34 DCHECK(thread_checker_.CalledOnValidThread()); |
| 45 eglMakeCurrent(display_, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); | 35 eglMakeCurrent(display_, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 46 eglDestroyContext(display_, context_); | 36 eglDestroyContext(display_, context_); |
| 47 if (surface_) { | 37 if (surface_) { |
| 48 eglDestroySurface(display_, surface_); | 38 eglDestroySurface(display_, surface_); |
| 49 } | 39 } |
| 50 eglTerminate(display_); | 40 eglTerminate(display_); |
| 51 } | 41 } |
| 52 | 42 |
| 53 void EglThreadContext::BindToWindow(EGLNativeWindowType window) { | 43 void EglThreadContext::BindToWindow(EGLNativeWindowType window) { |
| 54 DCHECK(thread_checker_.CalledOnValidThread()); | 44 DCHECK(thread_checker_.CalledOnValidThread()); |
| 55 if (surface_) { | 45 if (surface_) { |
| 56 eglDestroySurface(display_, surface_); | 46 eglDestroySurface(display_, surface_); |
| 57 surface_ = EGL_NO_SURFACE; | 47 surface_ = EGL_NO_SURFACE; |
| 58 } | 48 } |
| 59 if (window) { | 49 if (window) { |
| 60 surface_ = eglCreateWindowSurface(display_, config_, window, NULL); | 50 surface_ = eglCreateWindowSurface(display_, config_, window, NULL); |
| 61 if (!surface_) { | 51 if (!surface_) { |
| 62 LOG(FATAL) << "Failed to create window surface."; | 52 LOG(FATAL) << "Failed to create window surface: " << eglGetError(); |
| 63 } | 53 } |
| 64 } else { | 54 } else { |
| 65 surface_ = EGL_NO_SURFACE; | 55 surface_ = EGL_NO_SURFACE; |
| 66 } | 56 } |
| 67 if (!eglMakeCurrent(display_, surface_, surface_, context_)) { | 57 if (!eglMakeCurrent(display_, surface_, surface_, context_)) { |
| 68 LOG(FATAL) << "Failed to make current."; | 58 LOG(FATAL) << "Failed to make current: " << eglGetError(); |
| 69 } | 59 } |
| 70 } | 60 } |
| 71 | 61 |
| 72 bool EglThreadContext::IsWindowBound() const { | 62 bool EglThreadContext::IsWindowBound() const { |
| 73 DCHECK(thread_checker_.CalledOnValidThread()); | 63 DCHECK(thread_checker_.CalledOnValidThread()); |
| 74 return surface_ != EGL_NO_SURFACE; | 64 return surface_ != EGL_NO_SURFACE; |
| 75 } | 65 } |
| 76 | 66 |
| 77 void EglThreadContext::SwapBuffers() { | 67 bool EglThreadContext::SwapBuffers() { |
| 78 DCHECK(thread_checker_.CalledOnValidThread()); | 68 DCHECK(thread_checker_.CalledOnValidThread()); |
| 79 DCHECK(IsWindowBound()); | 69 if (!IsWindowBound()) { |
| 70 return false; | |
| 71 } | |
| 80 if (!eglSwapBuffers(display_, surface_)) { | 72 if (!eglSwapBuffers(display_, surface_)) { |
| 81 LOG(FATAL) << "Failed to swap buffer."; | 73 // Not fatal since the surface may be destroyed on a different thread |
| 74 // earlier than the window is unbound. The context can still be reused | |
| 75 // after rebinding to the right window. | |
| 76 LOG(WARNING) << "Failed to swap buffer: " << eglGetError(); | |
| 77 return false; | |
| 82 } | 78 } |
| 79 return true; | |
| 80 } | |
| 81 | |
| 82 bool EglThreadContext::CreateContextWithClientVersion( | |
| 83 unsigned int renderable_type, | |
| 84 int client_version) { | |
| 85 EGLint config_attribs[] = { | |
| 86 EGL_RED_SIZE, 8, | |
| 87 EGL_GREEN_SIZE, 8, | |
| 88 EGL_BLUE_SIZE, 8, | |
| 89 EGL_RENDERABLE_TYPE, renderable_type, | |
| 90 EGL_NONE | |
| 91 }; | |
| 92 | |
| 93 EGLint numConfigs; | |
|
Sergey Ulanov
2016/07/22 20:12:34
num_configs
Yuwei
2016/07/22 21:22:39
Done.
| |
| 94 if (!eglChooseConfig(display_, config_attribs, &config_, 1, &numConfigs)) { | |
| 95 LOG(WARNING) << "Failed to choose config: " << eglGetError(); | |
| 96 return false; | |
| 97 } | |
| 98 | |
| 99 EGLint context_attribs[] = { | |
| 100 EGL_CONTEXT_CLIENT_VERSION, client_version, | |
| 101 EGL_NONE | |
| 102 }; | |
| 103 context_ = eglCreateContext(display_, config_, EGL_NO_CONTEXT, | |
| 104 context_attribs); | |
| 105 if (!context_) { | |
| 106 LOG(WARNING) << "Failed to create context: " << eglGetError(); | |
|
Lambros
2016/07/22 20:36:07
You can maybe remove this log, as you are already
Yuwei
2016/07/22 21:22:39
Done.
| |
| 107 return false; | |
| 108 } | |
| 109 return true; | |
| 110 } | |
| 111 | |
| 112 int EglThreadContext::GetClientVersion() const { | |
| 113 return client_version_; | |
| 83 } | 114 } |
| 84 | 115 |
| 85 } // namespace remoting | 116 } // namespace remoting |
| OLD | NEW |