• 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 #ifndef TENSORFLOW_LITE_DELEGATES_GPU_GL_GL_CALL_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_GL_GL_CALL_H_
18 
19 #include <string>
20 #include <type_traits>
21 
22 #include "tensorflow/lite/delegates/gpu/common/status.h"
23 #include "tensorflow/lite/delegates/gpu/gl/gl_errors.h"
24 
25 namespace tflite {
26 namespace gpu {
27 namespace gl {
28 
29 // Primary purpose of this file is to provide useful macro for calling GL
30 // functions and checking errors. It also attaches a context to status in case
31 // of a GL error.
32 //
33 // Use TFLITE_GPU_CALL_GL as follows:
34 //
35 //   For GL functions with a return value:
36 //     Before:
37 //       GLint result = glFunc(...);
38 //       RETURN_IF_ERROR(GetOpenGlErrors());
39 //     After:
40 //       GLint result;
41 //       RETURN_IF_ERROR(TFLITE_GPU_CALL_GL(glFunc, &result, ...));
42 //
43 //   For GL functions without a return value:
44 //     Before:
45 //       glFunc(...);
46 //       RETURN_IF_ERROR(GetOpenGlErrors());
47 //     After:
48 //       RETURN_IF_ERROR(TFLITE_GPU_CALL_GL(glFunc, ...));
49 
50 namespace gl_call_internal {
51 
52 // For GL functions with a return value.
53 template <typename T>
54 struct Caller {
55   template <typename F, typename ErrorF, typename... Params>
operatorCaller56   absl::Status operator()(const std::string& context, F func, ErrorF error_func,
57                           T* result, Params&&... params) {
58     *result = func(std::forward<Params>(params)...);
59     const auto status = error_func();
60     if (status.ok()) return absl::OkStatus();
61     return absl::Status(status.code(),
62                         std::string(status.message()) + ": " + context);
63   }
64 };
65 
66 // For GL functions without a return value.
67 template<>
68 struct Caller<void> {
69   template <typename F, typename ErrorF, typename... Params>
70   absl::Status operator()(const std::string& context, F func, ErrorF error_func,
71                           Params&&... params) {
72     func(std::forward<Params>(params)...);
73     const auto status = error_func();
74     if (status.ok()) return absl::OkStatus();
75     return absl::Status(status.code(),
76                         std::string(status.message()) + ": " + context);
77   }
78 };
79 
80 template <typename F, typename ErrorF, typename ResultT, typename... ParamsT>
81 absl::Status CallAndCheckError(const std::string& context, F func,
82                                ErrorF error_func, ResultT* result,
83                                ParamsT&&... params) {
84   return Caller<ResultT>()(context, func, error_func, result,
85                            std::forward<ParamsT>(params)...);
86 }
87 
88 template <typename F, typename ErrorF, typename... Params>
89 absl::Status CallAndCheckError(const std::string& context, F func,
90                                ErrorF error_func, Params&&... params) {
91   return Caller<void>()(context, func, error_func,
92                         std::forward<Params>(params)...);
93 }
94 
95 }  // namespace gl_call_internal
96 
97 // XX_STRINGIFY is a helper macro to effectively apply # operator to an
98 // arbitrary value.
99 #define TFLITE_GPU_INTERNAL_STRINGIFY_HELPER(x) #x
100 #define TFLITE_GPU_INTERNAL_STRINGIFY(x) TFLITE_GPU_INTERNAL_STRINGIFY_HELPER(x)
101 #define TFLITE_GPU_FILE_LINE \
102   __FILE__ ":" TFLITE_GPU_INTERNAL_STRINGIFY(__LINE__)
103 
104 #define TFLITE_GPU_CALL_GL(method, ...)                   \
105   ::tflite::gpu::gl::gl_call_internal::CallAndCheckError( \
106       #method " in " TFLITE_GPU_FILE_LINE, method,        \
107       ::tflite::gpu::gl::GetOpenGlErrors, __VA_ARGS__)
108 
109 #define TFLITE_GPU_CALL_EGL(method, ...)                  \
110   ::tflite::gpu::gl::gl_call_internal::CallAndCheckError( \
111       #method " in " TFLITE_GPU_FILE_LINE, method,        \
112       ::tflite::gpu::gl::GetEglError, __VA_ARGS__)
113 
114 }  // namespace gl
115 }  // namespace gpu
116 }  // namespace tflite
117 
118 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_GL_GL_CALL_H_
119