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/gl_errors.h"
17
18 #include <string>
19 #include <vector>
20
21 #include "absl/strings/str_join.h"
22 #include "tensorflow/lite/delegates/gpu/common/status.h"
23 #include "tensorflow/lite/delegates/gpu/gl/portable_egl.h"
24 #include "tensorflow/lite/delegates/gpu/gl/portable_gl31.h"
25
26 namespace tflite {
27 namespace gpu {
28 namespace gl {
29 namespace {
30
ErrorToString(GLenum error)31 const char* ErrorToString(GLenum error) {
32 switch (error) {
33 case GL_INVALID_ENUM:
34 return "[GL_INVALID_ENUM]: An unacceptable value is specified for an "
35 "enumerated argument.";
36 case GL_INVALID_VALUE:
37 return "[GL_INVALID_VALUE]: A numeric argument is out of range.";
38 case GL_INVALID_OPERATION:
39 return "[GL_INVALID_OPERATION]: The specified operation is not allowed "
40 "in the current state.";
41 case GL_INVALID_FRAMEBUFFER_OPERATION:
42 return "[GL_INVALID_FRAMEBUFFER_OPERATION]: The framebuffer object is "
43 "not complete.";
44 case GL_OUT_OF_MEMORY:
45 return "[GL_OUT_OF_MEMORY]: There is not enough memory left to execute "
46 "the command.";
47 }
48 return "[UNKNOWN_GL_ERROR]";
49 }
50
51 struct ErrorFormatter {
operator ()tflite::gpu::gl::__anon40d0c1160111::ErrorFormatter52 void operator()(std::string* out, GLenum error) const {
53 absl::StrAppend(out, ErrorToString(error));
54 }
55 };
56
57 } // namespace
58
59 // TODO(akulik): create new error space for GL error.
60
GetOpenGlErrors()61 absl::Status GetOpenGlErrors() {
62 auto error = glGetError();
63 if (error == GL_NO_ERROR) {
64 return absl::OkStatus();
65 }
66 auto error2 = glGetError();
67 if (error2 == GL_NO_ERROR) {
68 return absl::InternalError(ErrorToString(error));
69 }
70 std::vector<GLenum> errors = {error, error2};
71 for (error = glGetError(); error != GL_NO_ERROR; error = glGetError()) {
72 errors.push_back(error);
73 }
74 return absl::InternalError(absl::StrJoin(errors, ",", ErrorFormatter()));
75 }
76
GetEglError()77 absl::Status GetEglError() {
78 EGLint error = eglGetError();
79 switch (error) {
80 case EGL_SUCCESS:
81 return absl::OkStatus();
82 case EGL_NOT_INITIALIZED:
83 return absl::InternalError(
84 "EGL is not initialized, or could not be initialized, for the "
85 "specified EGL display connection.");
86 case EGL_BAD_ACCESS:
87 return absl::InternalError(
88 "EGL cannot access a requested resource (for example a context is "
89 "bound in another thread).");
90 case EGL_BAD_ALLOC:
91 return absl::InternalError(
92 "EGL failed to allocate resources for the requested operation.");
93 case EGL_BAD_ATTRIBUTE:
94 return absl::InternalError(
95 "An unrecognized attribute or attribute value was passed in the "
96 "attribute list.");
97 case EGL_BAD_CONTEXT:
98 return absl::InternalError(
99 "An EGLContext argument does not name a valid EGL rendering "
100 "context.");
101 case EGL_BAD_CONFIG:
102 return absl::InternalError(
103 "An EGLConfig argument does not name a valid EGL frame buffer "
104 "configuration.");
105 case EGL_BAD_CURRENT_SURFACE:
106 return absl::InternalError(
107 "The current surface of the calling thread is a window, pixel buffer "
108 "or pixmap that is no longer valid.");
109 case EGL_BAD_DISPLAY:
110 return absl::InternalError(
111 "An EGLDisplay argument does not name a valid EGL display "
112 "connection.");
113 case EGL_BAD_SURFACE:
114 return absl::InternalError(
115 "An EGLSurface argument does not name a valid surface (window, pixel "
116 "buffer or pixmap) configured for GL rendering.");
117 case EGL_BAD_MATCH:
118 return absl::InternalError(
119 "Arguments are inconsistent (for example, a valid context requires "
120 "buffers not supplied by a valid surface).");
121 case EGL_BAD_PARAMETER:
122 return absl::InternalError("One or more argument values are invalid.");
123 case EGL_BAD_NATIVE_PIXMAP:
124 return absl::InternalError(
125 "A NativePixmapType argument does not refer to a valid native "
126 "pixmap.");
127 case EGL_BAD_NATIVE_WINDOW:
128 return absl::InternalError(
129 "A NativeWindowType argument does not refer to a valid native "
130 "window.");
131 case EGL_CONTEXT_LOST:
132 return absl::InternalError(
133 "A power management event has occurred. The application must destroy "
134 "all contexts and reinitialize OpenGL ES state and objects to "
135 "continue rendering.");
136 }
137 return absl::UnknownError("EGL error: " + std::to_string(error));
138 }
139
140 } // namespace gl
141 } // namespace gpu
142 } // namespace tflite
143