xref: /aosp_15_r20/external/angle/src/common/frame_capture_utils.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2022 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 // frame_capture_utils.h:
7 //   ANGLE Frame capture common classes.
8 //
9 
10 #ifndef COMMON_FRAME_CAPTURE_UTILS_H_
11 #define COMMON_FRAME_CAPTURE_UTILS_H_
12 
13 #include "common/frame_capture_utils_autogen.h"
14 #include "common/gl_enum_utils_autogen.h"
15 
16 namespace angle
17 {
18 namespace
19 {
20 template <typename ParamValueType>
21 struct ParamValueTrait
22 {
23     static_assert(sizeof(ParamValueType) == 0, "invalid ParamValueType");
24 };
25 
26 template <>
27 struct ParamValueTrait<gl::FramebufferID>
28 {
29     static constexpr const char *name = "framebufferPacked";
30     static const ParamType typeID     = ParamType::TFramebufferID;
31 };
32 
33 template <>
34 struct ParamValueTrait<gl::BufferID>
35 {
36     static constexpr const char *name = "bufferPacked";
37     static const ParamType typeID     = ParamType::TBufferID;
38 };
39 
40 template <>
41 struct ParamValueTrait<gl::RenderbufferID>
42 {
43     static constexpr const char *name = "renderbufferPacked";
44     static const ParamType typeID     = ParamType::TRenderbufferID;
45 };
46 
47 template <>
48 struct ParamValueTrait<gl::TextureID>
49 {
50     static constexpr const char *name = "texturePacked";
51     static const ParamType typeID     = ParamType::TTextureID;
52 };
53 
54 template <>
55 struct ParamValueTrait<gl::ShaderProgramID>
56 {
57     static constexpr const char *name = "programPacked";
58     static const ParamType typeID     = ParamType::TShaderProgramID;
59 };
60 }  // namespace
61 
62 using ParamData = std::vector<std::vector<uint8_t>>;
63 struct ParamCapture : angle::NonCopyable
64 {
65     ParamCapture();
66     ParamCapture(const char *nameIn, ParamType typeIn);
67     ~ParamCapture();
68 
69     ParamCapture(ParamCapture &&other);
70     ParamCapture &operator=(ParamCapture &&other);
71 
72     std::string name;
73     ParamType type;
74     ParamValue value;
75     gl::GLESEnum enumGroup;   // only used for param type GLenum, GLboolean and GLbitfield
76     gl::BigGLEnum bigGLEnum;  // only used for param type GLenum, GLboolean and GLbitfield
77     ParamData data;
78     int dataNElements           = 0;
79     int arrayClientPointerIndex = -1;
80     size_t readBufferSizeBytes  = 0;
81 };
82 
83 using Captures = std::vector<ParamCapture>;
84 
85 class ParamBuffer final : angle::NonCopyable
86 {
87   public:
88     ParamBuffer();
89     ~ParamBuffer();
90 
91     ParamBuffer(ParamBuffer &&other);
92     ParamBuffer &operator=(ParamBuffer &&other);
93 
94     template <typename T>
95     void addValueParam(const char *paramName, ParamType paramType, T paramValue);
96     template <typename T>
97     void setValueParamAtIndex(const char *paramName, ParamType paramType, T paramValue, int index);
98     template <typename T>
99     void addEnumParam(const char *paramName,
100                       gl::GLESEnum enumGroup,
101                       ParamType paramType,
102                       T paramValue);
103     template <typename T>
104     void addEnumParam(const char *paramName,
105                       gl::BigGLEnum enumGroup,
106                       ParamType paramType,
107                       T paramValue);
108 
109     template <typename T>
110     void addUnnamedParam(ParamType paramType, T paramValue);
111 
112     ParamCapture &getParam(const char *paramName, ParamType paramType, int index);
113     const ParamCapture &getParam(const char *paramName, ParamType paramType, int index) const;
114     ParamCapture &getParamFlexName(const char *paramName1,
115                                    const char *paramName2,
116                                    ParamType paramType,
117                                    int index);
118     const ParamCapture &getParamFlexName(const char *paramName1,
119                                          const char *paramName2,
120                                          ParamType paramType,
121                                          int index) const;
122     const ParamCapture &getReturnValue() const { return mReturnValueCapture; }
123 
124     void addParam(ParamCapture &&param);
125     void addReturnValue(ParamCapture &&returnValue);
126     bool hasClientArrayData() const { return mClientArrayDataParam != -1; }
127     ParamCapture &getClientArrayPointerParameter();
128     size_t getReadBufferSize() const { return mReadBufferSize; }
129 
130     bool empty() const { return mParamCaptures.empty(); }
131     const std::vector<ParamCapture> &getParamCaptures() const { return mParamCaptures; }
132 
133     const char *getNextParamName();
134 
135   private:
136     std::vector<ParamCapture> mParamCaptures;
137     ParamCapture mReturnValueCapture;
138     int mClientArrayDataParam = -1;
139     size_t mReadBufferSize    = 0;
140 };
141 
142 struct CallCapture
143 {
144     CallCapture(EntryPoint entryPointIn, ParamBuffer &&paramsIn);
145     CallCapture(const std::string &customFunctionNameIn, ParamBuffer &&paramsIn);
146     ~CallCapture();
147 
148     CallCapture(CallCapture &&other);
149     CallCapture &operator=(CallCapture &&other);
150 
151     const char *name() const;
152 
153     EntryPoint entryPoint;
154     std::string customFunctionName;
155     ParamBuffer params;
156     bool isActive = true;
157     gl::ContextID contextID;
158     bool isSyncPoint = false;
159 };
160 
161 template <typename T>
162 void ParamBuffer::addValueParam(const char *paramName, ParamType paramType, T paramValue)
163 {
164     ParamCapture capture(paramName, paramType);
165     InitParamValue(paramType, paramValue, &capture.value);
166     mParamCaptures.emplace_back(std::move(capture));
167 }
168 
169 template <typename T>
170 void ParamBuffer::setValueParamAtIndex(const char *paramName,
171                                        ParamType paramType,
172                                        T paramValue,
173                                        int index)
174 {
175     ASSERT(mParamCaptures.size() > static_cast<size_t>(index));
176 
177     ParamCapture capture(paramName, paramType);
178     InitParamValue(paramType, paramValue, &capture.value);
179     mParamCaptures[index] = std::move(capture);
180 }
181 
182 template <typename T>
183 void ParamBuffer::addEnumParam(const char *paramName,
184                                gl::GLESEnum enumGroup,
185                                ParamType paramType,
186                                T paramValue)
187 {
188     ParamCapture capture(paramName, paramType);
189     InitParamValue(paramType, paramValue, &capture.value);
190     capture.enumGroup = enumGroup;
191     mParamCaptures.emplace_back(std::move(capture));
192 }
193 
194 template <typename T>
195 void ParamBuffer::addEnumParam(const char *paramName,
196                                gl::BigGLEnum enumGroup,
197                                ParamType paramType,
198                                T paramValue)
199 {
200     ParamCapture capture(paramName, paramType);
201     InitParamValue(paramType, paramValue, &capture.value);
202     capture.bigGLEnum = enumGroup;
203     mParamCaptures.emplace_back(std::move(capture));
204 }
205 
206 template <typename T>
207 void ParamBuffer::addUnnamedParam(ParamType paramType, T paramValue)
208 {
209     addValueParam(getNextParamName(), paramType, paramValue);
210 }
211 
212 template <ParamType ParamT, typename T>
213 void WriteParamValueReplay(std::ostream &os, const CallCapture &call, T value);
214 
215 template <>
216 void WriteParamValueReplay<ParamType::TGLboolean>(std::ostream &os,
217                                                   const CallCapture &call,
218                                                   GLboolean value);
219 
220 template <>
221 void WriteParamValueReplay<ParamType::TGLbooleanPointer>(std::ostream &os,
222                                                          const CallCapture &call,
223                                                          GLboolean *value);
224 
225 template <>
226 void WriteParamValueReplay<ParamType::TvoidConstPointer>(std::ostream &os,
227                                                          const CallCapture &call,
228                                                          const void *value);
229 
230 template <>
231 void WriteParamValueReplay<ParamType::TvoidPointer>(std::ostream &os,
232                                                     const CallCapture &call,
233                                                     void *value);
234 
235 template <>
236 void WriteParamValueReplay<ParamType::TGLfloatConstPointer>(std::ostream &os,
237                                                             const CallCapture &call,
238                                                             const GLfloat *value);
239 
240 template <>
241 void WriteParamValueReplay<ParamType::TGLintConstPointer>(std::ostream &os,
242                                                           const CallCapture &call,
243                                                           const GLint *value);
244 
245 template <>
246 void WriteParamValueReplay<ParamType::TGLsizeiPointer>(std::ostream &os,
247                                                        const CallCapture &call,
248                                                        GLsizei *value);
249 template <>
250 void WriteParamValueReplay<ParamType::TGLuintPointer>(std::ostream &os,
251                                                       const CallCapture &call,
252                                                       GLuint *value);
253 template <>
254 void WriteParamValueReplay<ParamType::TGLuintConstPointer>(std::ostream &os,
255                                                            const CallCapture &call,
256                                                            const GLuint *value);
257 
258 template <>
259 void WriteParamValueReplay<ParamType::TGLDEBUGPROCKHR>(std::ostream &os,
260                                                        const CallCapture &call,
261                                                        GLDEBUGPROCKHR value);
262 
263 template <>
264 void WriteParamValueReplay<ParamType::TGLDEBUGPROC>(std::ostream &os,
265                                                     const CallCapture &call,
266                                                     GLDEBUGPROC value);
267 
268 template <>
269 void WriteParamValueReplay<ParamType::TBufferID>(std::ostream &os,
270                                                  const CallCapture &call,
271                                                  gl::BufferID value);
272 
273 template <>
274 void WriteParamValueReplay<ParamType::TFenceNVID>(std::ostream &os,
275                                                   const CallCapture &call,
276                                                   gl::FenceNVID value);
277 
278 template <>
279 void WriteParamValueReplay<ParamType::TFramebufferID>(std::ostream &os,
280                                                       const CallCapture &call,
281                                                       gl::FramebufferID value);
282 
283 template <>
284 void WriteParamValueReplay<ParamType::TMemoryObjectID>(std::ostream &os,
285                                                        const CallCapture &call,
286                                                        gl::MemoryObjectID value);
287 
288 template <>
289 void WriteParamValueReplay<ParamType::TProgramPipelineID>(std::ostream &os,
290                                                           const CallCapture &call,
291                                                           gl::ProgramPipelineID value);
292 
293 template <>
294 void WriteParamValueReplay<ParamType::TQueryID>(std::ostream &os,
295                                                 const CallCapture &call,
296                                                 gl::QueryID value);
297 
298 template <>
299 void WriteParamValueReplay<ParamType::TRenderbufferID>(std::ostream &os,
300                                                        const CallCapture &call,
301                                                        gl::RenderbufferID value);
302 
303 template <>
304 void WriteParamValueReplay<ParamType::TSamplerID>(std::ostream &os,
305                                                   const CallCapture &call,
306                                                   gl::SamplerID value);
307 
308 template <>
309 void WriteParamValueReplay<ParamType::TSemaphoreID>(std::ostream &os,
310                                                     const CallCapture &call,
311                                                     gl::SemaphoreID value);
312 
313 template <>
314 void WriteParamValueReplay<ParamType::TShaderProgramID>(std::ostream &os,
315                                                         const CallCapture &call,
316                                                         gl::ShaderProgramID value);
317 
318 template <>
319 void WriteParamValueReplay<ParamType::TTextureID>(std::ostream &os,
320                                                   const CallCapture &call,
321                                                   gl::TextureID value);
322 
323 template <>
324 void WriteParamValueReplay<ParamType::TTransformFeedbackID>(std::ostream &os,
325                                                             const CallCapture &call,
326                                                             gl::TransformFeedbackID value);
327 
328 template <>
329 void WriteParamValueReplay<ParamType::TVertexArrayID>(std::ostream &os,
330                                                       const CallCapture &call,
331                                                       gl::VertexArrayID value);
332 
333 template <>
334 void WriteParamValueReplay<ParamType::TUniformLocation>(std::ostream &os,
335                                                         const CallCapture &call,
336                                                         gl::UniformLocation value);
337 
338 template <>
339 void WriteParamValueReplay<ParamType::TUniformBlockIndex>(std::ostream &os,
340                                                           const CallCapture &call,
341                                                           gl::UniformBlockIndex value);
342 
343 template <>
344 void WriteParamValueReplay<ParamType::TSyncID>(std::ostream &os,
345                                                const CallCapture &call,
346                                                gl::SyncID value);
347 
348 template <>
349 void WriteParamValueReplay<ParamType::TGLubyte>(std::ostream &os,
350                                                 const CallCapture &call,
351                                                 GLubyte value);
352 
353 template <>
354 void WriteParamValueReplay<ParamType::TContextID>(std::ostream &os,
355                                                   const CallCapture &call,
356                                                   gl::ContextID value);
357 
358 template <>
359 void WriteParamValueReplay<ParamType::Tegl_DisplayPointer>(std::ostream &os,
360                                                            const CallCapture &call,
361                                                            egl::Display *value);
362 
363 template <>
364 void WriteParamValueReplay<ParamType::TImageID>(std::ostream &os,
365                                                 const CallCapture &call,
366                                                 egl::ImageID value);
367 
368 template <>
369 void WriteParamValueReplay<ParamType::TSurfaceID>(std::ostream &os,
370                                                   const CallCapture &call,
371                                                   egl::SurfaceID value);
372 
373 template <>
374 void WriteParamValueReplay<ParamType::TEGLDEBUGPROCKHR>(std::ostream &os,
375                                                         const CallCapture &call,
376                                                         EGLDEBUGPROCKHR value);
377 
378 template <>
379 void WriteParamValueReplay<ParamType::TEGLGetBlobFuncANDROID>(std::ostream &os,
380                                                               const CallCapture &call,
381                                                               EGLGetBlobFuncANDROID value);
382 
383 template <>
384 void WriteParamValueReplay<ParamType::TEGLSetBlobFuncANDROID>(std::ostream &os,
385                                                               const CallCapture &call,
386                                                               EGLSetBlobFuncANDROID value);
387 template <>
388 void WriteParamValueReplay<ParamType::TEGLClientBuffer>(std::ostream &os,
389                                                         const CallCapture &call,
390                                                         EGLClientBuffer value);
391 
392 template <>
393 void WriteParamValueReplay<ParamType::TEGLAttribPointer>(std::ostream &os,
394                                                          const CallCapture &call,
395                                                          EGLAttrib *value);
396 
397 template <>
398 void WriteParamValueReplay<ParamType::TEGLAttribConstPointer>(std::ostream &os,
399                                                               const CallCapture &call,
400                                                               const EGLAttrib *value);
401 
402 template <>
403 void WriteParamValueReplay<ParamType::TEGLintPointer>(std::ostream &os,
404                                                       const CallCapture &call,
405                                                       EGLint *value);
406 
407 template <>
408 void WriteParamValueReplay<ParamType::TEGLintConstPointer>(std::ostream &os,
409                                                            const CallCapture &call,
410                                                            const EGLint *value);
411 
412 template <>
413 void WriteParamValueReplay<ParamType::Tegl_ConfigPointer>(std::ostream &os,
414                                                           const CallCapture &call,
415                                                           egl::Config *value);
416 
417 template <>
418 void WriteParamValueReplay<ParamType::Tegl_SyncID>(std::ostream &os,
419                                                    const CallCapture &call,
420                                                    egl::SyncID value);
421 
422 template <>
423 void WriteParamValueReplay<ParamType::TEGLTime>(std::ostream &os,
424                                                 const CallCapture &call,
425                                                 EGLTime value);
426 
427 template <>
428 void WriteParamValueReplay<ParamType::TEGLTimeKHR>(std::ostream &os,
429                                                    const CallCapture &call,
430                                                    EGLTimeKHR value);
431 
432 template <>
433 void WriteParamValueReplay<ParamType::TGLGETBLOBPROCANGLE>(std::ostream &os,
434                                                            const CallCapture &call,
435                                                            GLGETBLOBPROCANGLE value);
436 
437 template <>
438 void WriteParamValueReplay<ParamType::TGLSETBLOBPROCANGLE>(std::ostream &os,
439                                                            const CallCapture &call,
440                                                            GLSETBLOBPROCANGLE value);
441 
442 // General fallback for any unspecific type.
443 template <ParamType ParamT, typename T>
444 void WriteParamValueReplay(std::ostream &os, const CallCapture &call, T value)
445 {
446     os << value;
447 }
448 
449 struct FmtPointerIndex
450 {
451     FmtPointerIndex(const void *ptrIn) : ptr(ptrIn) {}
452     const void *ptr;
453 };
454 
455 inline std::ostream &operator<<(std::ostream &os, const FmtPointerIndex &fmt)
456 {
457     os << reinterpret_cast<uintptr_t>(fmt.ptr) << "ul";
458     return os;
459 }
460 
461 template <typename ParamValueType>
462 bool FindResourceIDsInCall(const CallCapture &call, std::vector<ParamValueType> &idsOut);
463 }  // namespace angle
464 
465 #endif  // COMMON_FRAME_CAPTURE_UTILS_H_
466