1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_LITE_DELEGATES_GPU_GL_EGL_CONTEXT_H_ 17 #define TENSORFLOW_LITE_DELEGATES_GPU_GL_EGL_CONTEXT_H_ 18 19 #include "tensorflow/lite/delegates/gpu/common/status.h" 20 #include "tensorflow/lite/delegates/gpu/gl/portable_egl.h" 21 22 namespace tflite { 23 namespace gpu { 24 namespace gl { 25 26 // EglContext is an RAII wrapper for an EGLContext. 27 // 28 // EglContext is moveable but not copyable. 29 // 30 // See https://www.khronos.org/registry/EGL/sdk/docs/man/html/eglIntro.xhtml for 31 // more info. 32 class EglContext { 33 public: 34 // Creates an invalid EglContext. EglContext()35 EglContext() 36 : context_(EGL_NO_CONTEXT), 37 display_(EGL_NO_DISPLAY), 38 config_(EGL_NO_CONFIG_KHR), 39 has_ownership_(false) {} 40 EglContext(EGLContext context,EGLDisplay display,EGLConfig config,bool has_ownership)41 EglContext(EGLContext context, EGLDisplay display, EGLConfig config, 42 bool has_ownership) 43 : context_(context), 44 display_(display), 45 config_(config), 46 has_ownership_(has_ownership) {} 47 48 // Move only 49 EglContext(EglContext&& other); 50 EglContext& operator=(EglContext&& other); 51 EglContext(const EglContext&) = delete; 52 EglContext& operator=(const EglContext&) = delete; 53 ~EglContext()54 ~EglContext() { Invalidate(); } 55 context()56 EGLContext context() const { return context_; } 57 display()58 EGLDisplay display() const { return display_; } 59 config()60 EGLConfig config() const { return config_; } 61 62 // Make this EglContext the current EGL context on this thread, replacing 63 // the existing current. 64 absl::Status MakeCurrent(EGLSurface read, EGLSurface write); 65 MakeCurrentSurfaceless()66 absl::Status MakeCurrentSurfaceless() { 67 return MakeCurrent(EGL_NO_SURFACE, EGL_NO_SURFACE); 68 } 69 70 // Returns true if this is the currently bound EGL context. 71 bool IsCurrent() const; 72 73 // Returns true if this object actually owns corresponding EGL context 74 // and manages it's lifetime. has_ownership()75 bool has_ownership() const { return has_ownership_; } 76 77 private: 78 void Invalidate(); 79 80 EGLContext context_; 81 EGLDisplay display_; 82 EGLConfig config_; 83 84 bool has_ownership_; 85 }; 86 87 // It uses the EGL_KHR_no_config_context extension to create a no config context 88 // since most modern hardware supports the extension. 89 absl::Status CreateConfiglessContext(EGLDisplay display, 90 EGLContext shared_context, 91 EglContext* egl_context); 92 93 absl::Status CreateSurfacelessContext(EGLDisplay display, 94 EGLContext shared_context, 95 EglContext* egl_context); 96 97 absl::Status CreatePBufferContext(EGLDisplay display, EGLContext shared_context, 98 EglContext* egl_context); 99 100 } // namespace gl 101 } // namespace gpu 102 } // namespace tflite 103 104 #endif // TENSORFLOW_LITE_DELEGATES_GPU_GL_EGL_CONTEXT_H_ 105