1 //
2 // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 // Error.h: Defines the egl::Error and gl::Error classes which encapsulate API errors
7 // and optional error messages.
8
9 #ifndef LIBANGLE_ERROR_H_
10 #define LIBANGLE_ERROR_H_
11
12 #include <EGL/egl.h>
13 #include <EGL/eglext.h>
14 #include "angle_gl.h"
15 #include "common/angleutils.h"
16 #include "common/debug.h"
17
18 #include <memory>
19 #include <ostream>
20 #include <string>
21
22 namespace egl
23 {
24 class Error;
25 } // namespace egl
26
27 namespace egl
28 {
29
30 class [[nodiscard]] Error final
31 {
32 public:
33 explicit inline Error(EGLint errorCode);
34 Error(EGLint errorCode, std::string &&message);
35 Error(EGLint errorCode, EGLint id, std::string &&message);
36 inline Error(const Error &other);
37 inline Error(Error &&other);
38 inline ~Error() = default;
39
40 inline Error &operator=(const Error &other);
41 inline Error &operator=(Error &&other);
42
43 inline EGLint getCode() const;
44 inline EGLint getID() const;
45 inline bool isError() const;
46
47 inline void setCode(EGLint code);
48
49 const std::string &getMessage() const;
50
51 static inline Error NoError();
52
53 private:
54 void createMessageString() const;
55
56 friend std::ostream &operator<<(std::ostream &os, const Error &err);
57
58 EGLint mCode;
59 EGLint mID;
60 mutable std::unique_ptr<std::string> mMessage;
61 };
62
NoError()63 inline Error NoError()
64 {
65 return Error::NoError();
66 }
67
68 } // namespace egl
69
70 #define ANGLE_CONCAT1(x, y) x##y
71 #define ANGLE_CONCAT2(x, y) ANGLE_CONCAT1(x, y)
72 #define ANGLE_LOCAL_VAR ANGLE_CONCAT2(_localVar, __LINE__)
73
74 #define ANGLE_TRY_TEMPLATE(EXPR, FUNC) \
75 do \
76 { \
77 auto ANGLE_LOCAL_VAR = EXPR; \
78 if (ANGLE_UNLIKELY(IsError(ANGLE_LOCAL_VAR))) \
79 { \
80 FUNC(ANGLE_LOCAL_VAR); \
81 } \
82 } while (0)
83
84 #define ANGLE_RETURN(X) return X;
85 #define ANGLE_TRY(EXPR) ANGLE_TRY_TEMPLATE(EXPR, ANGLE_RETURN)
86
87 // TODO(jmadill): Remove after EGL error refactor. http://anglebug.com/42261727
88 #define ANGLE_SWALLOW_ERR(EXPR) \
89 do \
90 { \
91 auto ANGLE_LOCAL_VAR = EXPR; \
92 if (ANGLE_UNLIKELY(IsError(ANGLE_LOCAL_VAR))) \
93 { \
94 ERR() << "Unhandled internal error: " << ANGLE_LOCAL_VAR; \
95 } \
96 } while (0)
97
98 #undef ANGLE_LOCAL_VAR
99 #undef ANGLE_CONCAT2
100 #undef ANGLE_CONCAT1
101
102 #define ANGLE_CHECK(CONTEXT, EXPR, MESSAGE, ERROR) \
103 do \
104 { \
105 if (ANGLE_UNLIKELY(!(EXPR))) \
106 { \
107 CONTEXT->handleError(ERROR, MESSAGE, __FILE__, ANGLE_FUNCTION, __LINE__); \
108 return angle::Result::Stop; \
109 } \
110 } while (0)
111
112 namespace angle
113 {
114 // Result implements an explicit exception handling mechanism. A value of Stop signifies an
115 // exception akin to |throw|.
116 // TODO: make incorrect usage of Stop consistent with the above expectation.
117 // http://anglebug.com/42266839
118 enum class [[nodiscard]] Result
119 {
120 Continue,
121 Stop,
122 };
123
124 // TODO(jmadill): Remove this when refactor is complete. http://anglebug.com/42261727
125 egl::Error ResultToEGL(Result result);
126 } // namespace angle
127
128 // TODO(jmadill): Remove this when refactor is complete. http://anglebug.com/42261727
IsError(angle::Result result)129 inline bool IsError(angle::Result result)
130 {
131 return result == angle::Result::Stop;
132 }
133
134 // TODO(jmadill): Remove this when refactor is complete. http://anglebug.com/42261727
IsError(const egl::Error & err)135 inline bool IsError(const egl::Error &err)
136 {
137 return err.isError();
138 }
139
140 // TODO(jmadill): Remove this when refactor is complete. http://anglebug.com/42261727
IsError(bool value)141 inline bool IsError(bool value)
142 {
143 return !value;
144 }
145
146 // Utility macro for handling implementation methods inside Validation.
147 #define ANGLE_HANDLE_VALIDATION_ERR(X) \
148 do \
149 { \
150 (void)(X); \
151 return false; \
152 } while (0)
153
154 #define ANGLE_VALIDATION_TRY(EXPR) ANGLE_TRY_TEMPLATE(EXPR, ANGLE_HANDLE_VALIDATION_ERR)
155
156 #include "Error.inc"
157
158 #endif // LIBANGLE_ERROR_H_
159