• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "tensorflow/lite/delegates/gpu/gl/egl_environment.h"
17 
18 #include "absl/memory/memory.h"
19 #include "tensorflow/lite/delegates/gpu/common/status.h"
20 #include "tensorflow/lite/delegates/gpu/gl/gl_call.h"
21 #include "tensorflow/lite/delegates/gpu/gl/request_gpu_info.h"
22 
23 namespace tflite {
24 namespace gpu {
25 namespace gl {
26 namespace {
27 
28 // TODO(akulik): detect power management event when all contexts are destroyed
29 // and OpenGL ES is reinitialized. See eglMakeCurrent
30 
InitDisplay(EGLDisplay * egl_display)31 absl::Status InitDisplay(EGLDisplay* egl_display) {
32   RETURN_IF_ERROR(
33       TFLITE_GPU_CALL_EGL(eglGetDisplay, egl_display, EGL_DEFAULT_DISPLAY));
34   if (*egl_display == EGL_NO_DISPLAY) {
35     return absl::UnavailableError("eglGetDisplay returned nullptr");
36   }
37   bool is_initialized;
38   RETURN_IF_ERROR(TFLITE_GPU_CALL_EGL(eglInitialize, &is_initialized,
39                                       *egl_display, nullptr, nullptr));
40   if (!is_initialized) {
41     return absl::InternalError("No EGL error, but eglInitialize failed");
42   }
43   return absl::OkStatus();
44 }
45 
46 }  // namespace
47 
NewEglEnvironment(std::unique_ptr<EglEnvironment> * egl_environment)48 absl::Status EglEnvironment::NewEglEnvironment(
49     std::unique_ptr<EglEnvironment>* egl_environment) {
50   *egl_environment = absl::make_unique<EglEnvironment>();
51   RETURN_IF_ERROR((*egl_environment)->Init());
52   return absl::OkStatus();
53 }
54 
~EglEnvironment()55 EglEnvironment::~EglEnvironment() {
56   if (dummy_framebuffer_ != GL_INVALID_INDEX) {
57     glDeleteFramebuffers(1, &dummy_framebuffer_);
58   }
59   if (dummy_texture_ != GL_INVALID_INDEX) {
60     glDeleteTextures(1, &dummy_texture_);
61   }
62 }
63 
Init()64 absl::Status EglEnvironment::Init() {
65   bool is_bound;
66   RETURN_IF_ERROR(
67       TFLITE_GPU_CALL_EGL(eglBindAPI, &is_bound, EGL_OPENGL_ES_API));
68   if (!is_bound) {
69     return absl::InternalError("No EGL error, but eglBindAPI failed");
70   }
71 
72   // Re-use context and display if it was created on this thread.
73   if (eglGetCurrentContext() != EGL_NO_CONTEXT) {
74     display_ = eglGetCurrentDisplay();
75     context_ =
76         EglContext(eglGetCurrentContext(), display_, EGL_NO_CONFIG_KHR, false);
77   } else {
78     RETURN_IF_ERROR(InitDisplay(&display_));
79 
80     absl::Status status = InitConfiglessContext();
81     if (!status.ok()) {
82       status = InitSurfacelessContext();
83     }
84     if (!status.ok()) {
85       status = InitPBufferContext();
86     }
87     if (!status.ok()) {
88       return status;
89     }
90   }
91 
92   if (gpu_info_.vendor == GpuVendor::kUnknown) {
93     RETURN_IF_ERROR(RequestGpuInfo(&gpu_info_));
94   }
95   // TODO(akulik): when do we need ForceSyncTurning?
96   ForceSyncTurning();
97   return absl::OkStatus();
98 }
99 
InitConfiglessContext()100 absl::Status EglEnvironment::InitConfiglessContext() {
101   RETURN_IF_ERROR(CreateConfiglessContext(display_, EGL_NO_CONTEXT, &context_));
102   return context_.MakeCurrentSurfaceless();
103 }
104 
InitSurfacelessContext()105 absl::Status EglEnvironment::InitSurfacelessContext() {
106   RETURN_IF_ERROR(
107       CreateSurfacelessContext(display_, EGL_NO_CONTEXT, &context_));
108   RETURN_IF_ERROR(context_.MakeCurrentSurfaceless());
109 
110   // PowerVR support EGL_KHR_surfaceless_context, but glFenceSync crashes on
111   // PowerVR when it is surface-less.
112   RETURN_IF_ERROR(RequestGpuInfo(&gpu_info_));
113   if (gpu_info_.IsPowerVR()) {
114     return absl::UnavailableError(
115         "Surface-less context is not properly supported on powervr.");
116   }
117   return absl::OkStatus();
118 }
119 
InitPBufferContext()120 absl::Status EglEnvironment::InitPBufferContext() {
121   RETURN_IF_ERROR(CreatePBufferContext(display_, EGL_NO_CONTEXT, &context_));
122   RETURN_IF_ERROR(CreatePbufferRGBSurface(context_.config(), display_, 1, 1,
123                                           &surface_read_));
124   RETURN_IF_ERROR(CreatePbufferRGBSurface(context_.config(), display_, 1, 1,
125                                           &surface_draw_));
126   return context_.MakeCurrent(surface_read_.surface(), surface_draw_.surface());
127 }
128 
ForceSyncTurning()129 void EglEnvironment::ForceSyncTurning() {
130   glGenFramebuffers(1, &dummy_framebuffer_);
131   glBindFramebuffer(GL_FRAMEBUFFER, dummy_framebuffer_);
132 
133   glGenTextures(1, &dummy_texture_);
134   glBindTexture(GL_TEXTURE_2D, dummy_texture_);
135   glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 4, 4);
136   glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
137                          dummy_texture_, 0);
138 
139   GLenum draw_buffers[1] = {GL_COLOR_ATTACHMENT0};
140   glDrawBuffers(1, draw_buffers);
141 
142   glViewport(0, 0, 4, 4);
143   glClear(GL_COLOR_BUFFER_BIT);
144 }
145 
146 }  // namespace gl
147 }  // namespace gpu
148 }  // namespace tflite
149