1 #include "AndroidGLContext.h" 2 #include "caffe2/core/logging.h" 8 static const std::unordered_map<std::string, GL_Renderer>& renderer_map() {
9 static std::unordered_map<std::string, GL_Renderer> m = {
18 EGLContext AndroidGLContext::create_opengl_thread_context() {
19 EGLSurface surface = EGL_NO_SURFACE;
20 EGLContext context = EGL_NO_CONTEXT;
21 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
22 if (display == EGL_NO_DISPLAY) {
24 CAFFE_THROW(
"Problem with OpenGL context");
30 eglInitialize(display, &major, &minor);
32 const EGLint configAttr[] = {EGL_RENDERABLE_TYPE,
40 if (!eglChooseConfig(display, configAttr, &eglConfig, 1, &numConfig)) {
42 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
43 eglTerminate(display);
44 display = EGL_NO_DISPLAY;
45 CAFFE_THROW(
"Problem with OpenGL context");
49 const EGLint ctxAttr[] = {EGL_CONTEXT_CLIENT_VERSION,
54 context = eglCreateContext(display, eglConfig, EGL_NO_CONTEXT, ctxAttr);
63 const EGLint surfaceAttr[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
65 surface = eglCreatePbufferSurface(display, eglConfig, surfaceAttr);
68 eglMakeCurrent(display, surface, surface, context);
71 eglBindAPI(EGL_OPENGL_ES_API);
75 bool AndroidGLContext::opengl_thread_context_exists() {
76 return eglGetCurrentContext() != EGL_NO_CONTEXT;
79 bool AndroidGLContext::release_opengl_thread_context() {
80 EGLContext display = eglGetCurrentDisplay();
81 if (display != EGL_NO_DISPLAY) {
82 if (_eglcontext != EGL_NO_CONTEXT) {
83 eglDestroyContext(display, _eglcontext);
84 _eglcontext = EGL_NO_CONTEXT;
86 EGLSurface surface = eglGetCurrentSurface(EGL_DRAW);
87 if (surface != EGL_NO_SURFACE) {
88 eglDestroySurface(display, surface);
89 surface = EGL_NO_SURFACE;
91 surface = eglGetCurrentSurface(EGL_READ);
92 if (surface != EGL_NO_SURFACE) {
93 eglDestroySurface(display, surface);
94 surface = EGL_NO_SURFACE;
96 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
97 eglTerminate(display);
98 display = EGL_NO_DISPLAY;
104 void AndroidGLContext::init_gles3() {
105 if (!gl3stubInit()) {
106 CAFFE_THROW(
"OpenGL ES 3 not initialized");
108 LOG(INFO) <<
"OpenGL ES 3 successfully enabled";
112 GL_Renderer AndroidGLContext::get_platform() {
113 std::string rendererStr((
const char*)glGetString(GL_RENDERER));
114 std::regex regStr(
"^[A-Za-z]*");
116 if (std::regex_search(rendererStr, matchs, regStr)) {
117 const std::string renderer = *matchs.begin();
118 auto found = renderer_map().find(renderer);
119 if (found != renderer_map().end()) {
120 return found->second;
123 CAFFE_THROW(
"Unsupported GPU renderer");
126 AndroidGLContext::AndroidGLContext() {
127 if (!opengl_thread_context_exists()) {
128 _eglcontext = create_opengl_thread_context();
129 LOG(INFO) <<
"New EGLContext created";
131 if (!supportOpenGLES3(&half_float_supported)) {
132 CAFFE_THROW(
"OpenGL ES 3 not supported");
135 if (!isSupportedDevice()) {
136 LOG(ERROR) <<
"Device not fully supported";
139 _eglcontext = EGL_NO_CONTEXT;
140 LOG(INFO) <<
"Reusing EGLContext, make sure OpenGL ES 3 is supported";
142 static std::once_flag once;
143 std::call_once(once, [&]() { init_gles3(); });
146 AndroidGLContext::~AndroidGLContext() {
147 if (_eglcontext != EGL_NO_CONTEXT) {
148 release_opengl_thread_context();
152 void AndroidGLContext::set_context() {}
154 void AndroidGLContext::reset_context() {}
156 void AndroidGLContext::flush_context() {}