xref: /aosp_15_r20/external/angle/src/libANGLE/entry_points_utils.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2018 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 // entry_point_utils:
7 //   These helpers are used in GL/GLES entry point routines.
8 
9 #ifndef LIBANGLE_ENTRY_POINT_UTILS_H_
10 #define LIBANGLE_ENTRY_POINT_UTILS_H_
11 
12 #include "angle_gl.h"
13 #include "common/Optional.h"
14 #include "common/PackedEnums.h"
15 #include "common/angleutils.h"
16 #include "common/entry_points_enum_autogen.h"
17 #include "common/mathutil.h"
18 #include "libANGLE/Context.h"
19 #include "libANGLE/Display.h"
20 
21 namespace gl
22 {
23 // A template struct for determining the default value to return for each entry point.
24 template <angle::EntryPoint EP, typename ReturnType>
25 struct DefaultReturnValue;
26 
27 // Default return values for each basic return type.
28 template <angle::EntryPoint EP>
29 struct DefaultReturnValue<EP, GLint>
30 {
31     static constexpr GLint kValue = -1;
32 };
33 
34 // This doubles as the GLenum return value.
35 template <angle::EntryPoint EP>
36 struct DefaultReturnValue<EP, GLuint>
37 {
38     static constexpr GLuint kValue = 0;
39 };
40 
41 template <angle::EntryPoint EP>
42 struct DefaultReturnValue<EP, GLboolean>
43 {
44     static constexpr GLboolean kValue = GL_FALSE;
45 };
46 
47 template <angle::EntryPoint EP>
48 struct DefaultReturnValue<EP, ShaderProgramID>
49 {
50     static constexpr ShaderProgramID kValue = {0};
51 };
52 
53 // Catch-all rules for pointer types.
54 template <angle::EntryPoint EP, typename PointerType>
55 struct DefaultReturnValue<EP, const PointerType *>
56 {
57     static constexpr const PointerType *kValue = nullptr;
58 };
59 
60 template <angle::EntryPoint EP, typename PointerType>
61 struct DefaultReturnValue<EP, PointerType *>
62 {
63     static constexpr PointerType *kValue = nullptr;
64 };
65 
66 // Overloaded to return invalid index
67 template <>
68 struct DefaultReturnValue<angle::EntryPoint::GLGetUniformBlockIndex, GLuint>
69 {
70     static constexpr GLuint kValue = GL_INVALID_INDEX;
71 };
72 
73 // Specialized enum error value.
74 template <>
75 struct DefaultReturnValue<angle::EntryPoint::GLClientWaitSync, GLenum>
76 {
77     static constexpr GLenum kValue = GL_WAIT_FAILED;
78 };
79 
80 // glTestFenceNV should still return TRUE for an invalid fence.
81 template <>
82 struct DefaultReturnValue<angle::EntryPoint::GLTestFenceNV, GLboolean>
83 {
84     static constexpr GLboolean kValue = GL_TRUE;
85 };
86 
87 template <angle::EntryPoint EP, typename ReturnType>
88 constexpr ANGLE_INLINE ReturnType GetDefaultReturnValue()
89 {
90     return DefaultReturnValue<EP, ReturnType>::kValue;
91 }
92 
93 #if ANGLE_CAPTURE_ENABLED
94 #    define ANGLE_CAPTURE_GL(Func, ...) CaptureGLCallToFrameCapture(Capture##Func, __VA_ARGS__)
95 #else
96 #    define ANGLE_CAPTURE_GL(...)
97 #endif  // ANGLE_CAPTURE_ENABLED
98 
99 #define EGL_EVENT(EP, FMT, ...) EVENT(nullptr, EGL##EP, FMT, ##__VA_ARGS__)
100 
101 inline int CID(const Context *context)
102 {
103     return context == nullptr ? 0 : static_cast<int>(context->id().value);
104 }
105 
106 bool GeneratePixelLocalStorageActiveError(const PrivateState &state,
107                                           ErrorSet *errors,
108                                           angle::EntryPoint entryPoint);
109 
110 ANGLE_INLINE bool ValidatePixelLocalStorageInactive(const PrivateState &state,
111                                                     ErrorSet *errors,
112                                                     angle::EntryPoint entryPoint)
113 {
114     return state.getPixelLocalStorageActivePlanes() == 0 ||
115            GeneratePixelLocalStorageActiveError(state, errors, entryPoint);
116 }
117 }  // namespace gl
118 
119 namespace egl
120 {
121 inline int CID(EGLDisplay display, EGLContext context)
122 {
123     const egl::Display *displayPtr = reinterpret_cast<const egl::Display *>(display);
124     if (!Display::isValidDisplay(displayPtr))
125     {
126         return -1;
127     }
128     gl::ContextID contextID = {static_cast<GLuint>(reinterpret_cast<uintptr_t>(context))};
129     if (!displayPtr->isValidContext(contextID))
130     {
131         return -1;
132     }
133     return contextID.value;
134 }
135 
136 #if ANGLE_CAPTURE_ENABLED
137 #    define ANGLE_CAPTURE_EGL(Func, ...) CaptureEGLCallToFrameCapture(Capture##Func, __VA_ARGS__)
138 #else
139 #    define ANGLE_CAPTURE_EGL(...)
140 #endif  // ANGLE_CAPTURE_ENABLED
141 }  // namespace egl
142 
143 #endif  // LIBANGLE_ENTRY_POINT_UTILS_H_
144