// // Copyright (c) 2014 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // Error.h: Defines the egl::Error and gl::Error classes which encapsulate API errors // and optional error messages. #ifndef LIBANGLE_ERROR_H_ #define LIBANGLE_ERROR_H_ #include #include #include "angle_gl.h" #include "common/angleutils.h" #include "common/debug.h" #include #include #include namespace angle { template class ANGLE_NO_DISCARD ErrorOrResultBase { public: ErrorOrResultBase(const ErrorT &error) : mError(error) {} ErrorOrResultBase(ErrorT &&error) : mError(std::move(error)) {} ErrorOrResultBase(ResultT &&result) : mError(NoErrorVal), mResult(std::forward(result)) { } ErrorOrResultBase(const ResultT &result) : mError(NoErrorVal), mResult(result) {} bool isError() const { return mError.isError(); } const ErrorT &getError() const { return mError; } ResultT &&getResult() { return std::move(mResult); } private: ErrorT mError; ResultT mResult; }; template class ErrorStreamBase : angle::NonCopyable { public: ErrorStreamBase() : mID(EnumT) {} ErrorStreamBase(GLuint id) : mID(id) {} template ErrorStreamBase &operator<<(T value) { mErrorStream << value; return *this; } operator ErrorT() { return ErrorT(EnumT, mID, mErrorStream.str()); } template operator ErrorOrResultBase() { return static_cast(*this); } private: GLuint mID; std::ostringstream mErrorStream; }; } // namespace angle namespace egl { class Error; } // namespace egl namespace gl { class ANGLE_NO_DISCARD Error final { public: explicit inline Error(GLenum errorCode); Error(GLenum errorCode, std::string &&message); Error(GLenum errorCode, GLuint id, std::string &&message); inline Error(const Error &other); inline Error(Error &&other); inline ~Error() = default; // automatic error type conversion inline Error(egl::Error &&eglErr); inline Error(egl::Error eglErr); inline Error &operator=(const Error &other); inline Error &operator=(Error &&other); inline GLenum getCode() const; inline GLuint getID() const; inline bool isError() const; const std::string &getMessage() const; // Useful for mocking and testing bool operator==(const Error &other) const; bool operator!=(const Error &other) const; private: void createMessageString() const; friend std::ostream &operator<<(std::ostream &os, const Error &err); friend class egl::Error; GLenum mCode; GLuint mID; mutable std::unique_ptr mMessage; }; template using ErrorOrResult = angle::ErrorOrResultBase; namespace priv { template using ErrorStream = angle::ErrorStreamBase; } // namespace priv using InternalError = priv::ErrorStream; using InvalidEnum = priv::ErrorStream; using InvalidValue = priv::ErrorStream; using InvalidOperation = priv::ErrorStream; using StackOverflow = priv::ErrorStream; using StackUnderflow = priv::ErrorStream; using OutOfMemory = priv::ErrorStream; using InvalidFramebufferOperation = priv::ErrorStream; inline Error NoError() { return Error(GL_NO_ERROR); } using LinkResult = ErrorOrResult; } // namespace gl namespace egl { class ANGLE_NO_DISCARD Error final { public: explicit inline Error(EGLint errorCode); Error(EGLint errorCode, std::string &&message); Error(EGLint errorCode, EGLint id, std::string &&message); inline Error(const Error &other); inline Error(Error &&other); inline ~Error() = default; // automatic error type conversion inline Error(gl::Error &&glErr); inline Error(gl::Error glErr); inline Error &operator=(const Error &other); inline Error &operator=(Error &&other); inline EGLint getCode() const; inline EGLint getID() const; inline bool isError() const; const std::string &getMessage() const; private: void createMessageString() const; friend std::ostream &operator<<(std::ostream &os, const Error &err); friend class gl::Error; EGLint mCode; EGLint mID; mutable std::unique_ptr mMessage; }; template using ErrorOrResult = angle::ErrorOrResultBase; namespace priv { template using ErrorStream = angle::ErrorStreamBase; } // namespace priv using EglNotInitialized = priv::ErrorStream; using EglBadAccess = priv::ErrorStream; using EglBadAlloc = priv::ErrorStream; using EglBadAttribute = priv::ErrorStream; using EglBadConfig = priv::ErrorStream; using EglBadContext = priv::ErrorStream; using EglBadCurrentSurface = priv::ErrorStream; using EglBadDisplay = priv::ErrorStream; using EglBadMatch = priv::ErrorStream; using EglBadNativeWindow = priv::ErrorStream; using EglBadParameter = priv::ErrorStream; using EglBadSurface = priv::ErrorStream; using EglContextLost = priv::ErrorStream; using EglBadStream = priv::ErrorStream; using EglBadState = priv::ErrorStream; using EglBadDevice = priv::ErrorStream; inline Error NoError() { return Error(EGL_SUCCESS); } } // namespace egl #define ANGLE_CONCAT1(x, y) x##y #define ANGLE_CONCAT2(x, y) ANGLE_CONCAT1(x, y) #define ANGLE_LOCAL_VAR ANGLE_CONCAT2(_localVar, __LINE__) #define ANGLE_TRY_TEMPLATE(EXPR, FUNC) \ { \ auto ANGLE_LOCAL_VAR = EXPR; \ if (ANGLE_LOCAL_VAR.isError()) \ { \ FUNC(ANGLE_LOCAL_VAR); \ } \ } \ ANGLE_EMPTY_STATEMENT #define ANGLE_RETURN(X) return X; #define ANGLE_TRY(EXPR) ANGLE_TRY_TEMPLATE(EXPR, ANGLE_RETURN); #define ANGLE_TRY_RESULT(EXPR, RESULT) \ { \ auto ANGLE_LOCAL_VAR = EXPR; \ if (ANGLE_LOCAL_VAR.isError()) \ { \ return ANGLE_LOCAL_VAR.getError(); \ } \ RESULT = ANGLE_LOCAL_VAR.getResult(); \ } \ ANGLE_EMPTY_STATEMENT // TODO(jmadill): Introduce way to store errors to a const Context. #define ANGLE_SWALLOW_ERR(EXPR) \ { \ auto ANGLE_LOCAL_VAR = EXPR; \ if (ANGLE_LOCAL_VAR.isError()) \ { \ ERR() << "Unhandled internal error: " << ANGLE_LOCAL_VAR; \ } \ } \ ANGLE_EMPTY_STATEMENT #undef ANGLE_LOCAL_VAR #undef ANGLE_CONCAT2 #undef ANGLE_CONCAT1 #include "Error.inl" #endif // LIBANGLE_ERROR_H_