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, CLIENT_VERSION_ES_3)) { |
| 21 EGL_RED_SIZE, 8, | 21 client_version_ = CLIENT_VERSION_ES_3; |
| 22 EGL_GREEN_SIZE, 8, | 22 } else if (CreateContextWithClientVersion(EGL_OPENGL_ES2_BIT, |
| 23 EGL_BLUE_SIZE, 8, | 23 CLIENT_VERSION_ES_2)) { |
| 24 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, | 24 LOG(WARNING) << "OpenGL ES 3 context not supported." |
| 25 EGL_NONE | 25 << "Falled back to OpenGL ES 2"; |
| 26 }; | 26 client_version_ = CLIENT_VERSION_ES_2; |
| 27 | 27 } else { |
| 28 EGLint numConfigs; | 28 LOG(FATAL) << "Failed to create context: " << eglGetError(); |
| 29 if (!eglChooseConfig(display_, config_attribs, &config_, 1, &numConfigs)) { | |
| 30 LOG(FATAL) << "Failed to choose config."; | |
| 31 } | |
| 32 const EGLint context_attribs[] = { | |
| 33 EGL_CONTEXT_CLIENT_VERSION, 2, | |
| 34 EGL_NONE | |
| 35 }; | |
| 36 context_ = eglCreateContext(display_, config_, EGL_NO_CONTEXT, | |
| 37 context_attribs); | |
| 38 if (!context_) { | |
| 39 LOG(FATAL) << "Failed to create context."; | |
| 40 } | 29 } |
| 41 } | 30 } |
| 42 | 31 |
| 43 EglThreadContext::~EglThreadContext() { | 32 EglThreadContext::~EglThreadContext() { |
| 44 DCHECK(thread_checker_.CalledOnValidThread()); | 33 DCHECK(thread_checker_.CalledOnValidThread()); |
| 45 eglMakeCurrent(display_, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); | 34 eglMakeCurrent(display_, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 46 eglDestroyContext(display_, context_); | 35 eglDestroyContext(display_, context_); |
| 47 if (surface_) { | 36 if (surface_) { |
| 48 eglDestroySurface(display_, surface_); | 37 eglDestroySurface(display_, surface_); |
| 49 } | 38 } |
| 50 eglTerminate(display_); | 39 eglTerminate(display_); |
| 51 } | 40 } |
| 52 | 41 |
| 53 void EglThreadContext::BindToWindow(EGLNativeWindowType window) { | 42 void EglThreadContext::BindToWindow(EGLNativeWindowType window) { |
| 54 DCHECK(thread_checker_.CalledOnValidThread()); | 43 DCHECK(thread_checker_.CalledOnValidThread()); |
| 55 if (surface_) { | 44 if (surface_) { |
| 56 eglDestroySurface(display_, surface_); | 45 eglDestroySurface(display_, surface_); |
| 57 surface_ = EGL_NO_SURFACE; | 46 surface_ = EGL_NO_SURFACE; |
| 58 } | 47 } |
| 59 if (window) { | 48 if (window) { |
| 60 surface_ = eglCreateWindowSurface(display_, config_, window, NULL); | 49 surface_ = eglCreateWindowSurface(display_, config_, window, NULL); |
| 61 if (!surface_) { | 50 if (!surface_) { |
| 62 LOG(FATAL) << "Failed to create window surface."; | 51 LOG(FATAL) << "Failed to create window surface: " << eglGetError(); |
| 63 } | 52 } |
| 64 } else { | 53 } else { |
| 65 surface_ = EGL_NO_SURFACE; | 54 surface_ = EGL_NO_SURFACE; |
| 66 } | 55 } |
| 67 if (!eglMakeCurrent(display_, surface_, surface_, context_)) { | 56 if (!eglMakeCurrent(display_, surface_, surface_, context_)) { |
| 68 LOG(FATAL) << "Failed to make current."; | 57 LOG(FATAL) << "Failed to make current: " << eglGetError(); |
| 69 } | 58 } |
| 70 } | 59 } |
| 71 | 60 |
| 72 bool EglThreadContext::IsWindowBound() const { | 61 bool EglThreadContext::IsWindowBound() const { |
| 73 DCHECK(thread_checker_.CalledOnValidThread()); | 62 DCHECK(thread_checker_.CalledOnValidThread()); |
| 74 return surface_ != EGL_NO_SURFACE; | 63 return surface_ != EGL_NO_SURFACE; |
| 75 } | 64 } |
| 76 | 65 |
| 77 void EglThreadContext::SwapBuffers() { | 66 bool EglThreadContext::SwapBuffers() { |
| 78 DCHECK(thread_checker_.CalledOnValidThread()); | 67 DCHECK(thread_checker_.CalledOnValidThread()); |
| 79 DCHECK(IsWindowBound()); | 68 if (!IsWindowBound()) { |
| 69 return false; | |
| 70 } | |
| 80 if (!eglSwapBuffers(display_, surface_)) { | 71 if (!eglSwapBuffers(display_, surface_)) { |
| 81 LOG(FATAL) << "Failed to swap buffer."; | 72 // Not fatal since the surface may be destroyed on a different thread |
| 73 // earlier than the window is unbound. The context can still be reused | |
| 74 // after rebinding to the right window. | |
| 75 LOG(WARNING) << "Failed to swap buffer: " << eglGetError(); | |
| 76 return false; | |
| 82 } | 77 } |
| 78 return true; | |
| 79 } | |
| 80 | |
| 81 bool EglThreadContext::CreateContextWithClientVersion( | |
| 82 int renderable_type, | |
| 83 ClientVersion client_version) { | |
|
Sergey Ulanov
2016/07/25 19:47:11
DCHECK here that client_version != UNKNOWN
Yuwei
2016/07/25 20:59:51
Done.
| |
| 84 EGLint config_attribs[] = { | |
| 85 EGL_RED_SIZE, 8, | |
| 86 EGL_GREEN_SIZE, 8, | |
| 87 EGL_BLUE_SIZE, 8, | |
| 88 EGL_RENDERABLE_TYPE, renderable_type, | |
| 89 EGL_NONE | |
| 90 }; | |
| 91 | |
| 92 EGLint num_configs; | |
| 93 if (!eglChooseConfig(display_, config_attribs, &config_, 1, &num_configs)) { | |
| 94 LOG(WARNING) << "Failed to choose config: " << eglGetError(); | |
| 95 return false; | |
| 96 } | |
| 97 | |
| 98 EGLint context_attribs[] = { | |
| 99 EGL_CONTEXT_CLIENT_VERSION, client_version, | |
| 100 EGL_NONE | |
| 101 }; | |
| 102 context_ = eglCreateContext(display_, config_, EGL_NO_CONTEXT, | |
| 103 context_attribs); | |
| 104 return context_ != EGL_NO_CONTEXT; | |
| 83 } | 105 } |
| 84 | 106 |
| 85 } // namespace remoting | 107 } // namespace remoting |
| OLD | NEW |