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_ENVIRONMENT_H_ 17 #define TENSORFLOW_LITE_DELEGATES_GPU_GL_EGL_ENVIRONMENT_H_ 18 19 #include <memory> 20 21 #include "tensorflow/lite/delegates/gpu/common/status.h" 22 #include "tensorflow/lite/delegates/gpu/gl/egl_context.h" 23 #include "tensorflow/lite/delegates/gpu/gl/egl_surface.h" 24 #include "tensorflow/lite/delegates/gpu/gl/portable_egl.h" 25 #include "tensorflow/lite/delegates/gpu/gl/portable_gl31.h" 26 #include "tensorflow/lite/delegates/gpu/gl/request_gpu_info.h" 27 28 namespace tflite { 29 namespace gpu { 30 namespace gl { 31 32 // Class encapsulates creation of OpenGL objects needed before starting working 33 // with OpenGL: binds OpenGL ES API, creates new EGL context, binds it to EGL 34 // display and creates surfaces if needed. 35 // 36 // EGL environment needs to be created once per thread. 37 class EglEnvironment { 38 public: 39 static absl::Status NewEglEnvironment( 40 std::unique_ptr<EglEnvironment>* egl_environment); 41 42 EglEnvironment() = default; 43 ~EglEnvironment(); 44 context()45 const EglContext& context() const { return context_; } display()46 EGLDisplay display() const { return display_; } gpu_info()47 const GpuInfo& gpu_info() const { return gpu_info_; } 48 49 private: 50 absl::Status Init(); 51 absl::Status InitConfiglessContext(); 52 absl::Status InitSurfacelessContext(); 53 absl::Status InitPBufferContext(); 54 55 EGLDisplay display_ = EGL_NO_DISPLAY; 56 EglSurface surface_draw_; 57 EglSurface surface_read_; 58 EglContext context_; 59 GpuInfo gpu_info_; 60 61 // Strange hack that helps on Mali GPUs 62 // without it glFinish and glFenceSync don't work 63 void ForceSyncTurning(); 64 GLuint dummy_framebuffer_ = GL_INVALID_INDEX; 65 GLuint dummy_texture_ = GL_INVALID_INDEX; 66 }; 67 68 } // namespace gl 69 } // namespace gpu 70 } // namespace tflite 71 72 #endif // TENSORFLOW_LITE_DELEGATES_GPU_GL_EGL_ENVIRONMENT_H_ 73