Remove build warnings
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / egl-graphics-controller-debug.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <dali/internal/graphics/gles-impl/egl-graphics-controller-debug.h>
18 #include <cstdio>
19 #include <queue>
20
21 namespace Dali::Graphics
22 {
23 std::string DumpCompareOp(Graphics::CompareOp compareOp)
24 {
25   switch(compareOp)
26   {
27     case Graphics::CompareOp::NEVER:
28       return "Graphics::CompareOp::NEVER";
29       break;
30     case Graphics::CompareOp::LESS:
31       return "Graphics::CompareOp::LESS";
32       break;
33     case Graphics::CompareOp::EQUAL:
34       return "Graphics::CompareOp::EQUAL";
35       break;
36     case Graphics::CompareOp::LESS_OR_EQUAL:
37       return "Graphics::CompareOp::LESS_OR_EQUAL";
38       break;
39     case Graphics::CompareOp::GREATER:
40       return "Graphics::CompareOp::GREATER";
41       break;
42     case Graphics::CompareOp::NOT_EQUAL:
43       return "Graphics::CompareOp::NOT_EQUAL";
44       break;
45     case Graphics::CompareOp::GREATER_OR_EQUAL:
46       return "Graphics::CompareOp::GREATER_OR_EQUAL";
47       break;
48     case Graphics::CompareOp::ALWAYS:
49       return "Graphics::CompareOp::ALWAYS";
50       break;
51   }
52   return "UNKNOWN";
53 }
54
55 std::string DumpStencilOp(Graphics::StencilOp stencilOp)
56 {
57   switch(stencilOp)
58   {
59     case Graphics::StencilOp::KEEP:
60       return "Graphics::StencilOp::KEEP";
61       break;
62     case Graphics::StencilOp::ZERO:
63       return "Graphics::StencilOp::ZERO";
64       break;
65     case Graphics::StencilOp::REPLACE:
66       return "Graphics::StencilOp::REPLACE";
67       break;
68     case Graphics::StencilOp::INCREMENT_AND_CLAMP:
69       return "Graphics::StencilOp::INCREMENT_AND_CLAMP";
70       break;
71     case Graphics::StencilOp::DECREMENT_AND_CLAMP:
72       return "Graphics::StencilOp::DECREMENT_AND_CLAMP";
73       break;
74     case Graphics::StencilOp::INVERT:
75       return "Graphics::StencilOp::INVERT";
76       break;
77     case Graphics::StencilOp::INCREMENT_AND_WRAP:
78       return "Graphics::StencilOp::INCREMENT_AND_WRAP";
79       break;
80     case Graphics::StencilOp::DECREMENT_AND_WRAP:
81       return "Graphics::StencilOp::DECREMENT_AND_WRAP";
82       break;
83   }
84   return "UNKNOWN";
85 }
86
87 void DumpCommandBuffer(FILE* output, const GLES::CommandBuffer* commandBuffer)
88 {
89   bool       first{true};
90   uint32_t   count   = 0u;
91   const auto command = commandBuffer->GetCommands(count);
92   for(auto i = 0u; i < count; ++i)
93   {
94     auto& cmd = command[i];
95     if(!first)
96     {
97       fprintf(output, ",");
98     }
99     first = false;
100     // process command
101     switch(cmd.type)
102     {
103       case GLES::CommandType::FLUSH:
104       {
105         fprintf(output, "{\"Cmd\":\"FLUSH\"}\n");
106         break;
107       }
108       case GLES::CommandType::BIND_TEXTURES:
109       {
110         fprintf(output, "{\"Cmd\":\"BIND_TEXTURES\"}\n");
111         break;
112       }
113       case GLES::CommandType::BIND_VERTEX_BUFFERS:
114       {
115         fprintf(output, "{\"Cmd\":\"BIND_VERTEX_BUFFERS\"}\n");
116         break;
117       }
118       case GLES::CommandType::BIND_UNIFORM_BUFFER:
119       {
120         fprintf(output, "{\"Cmd\":\"BIND_UNIFORM_BUFFERS\"}\n");
121         break;
122       }
123       case GLES::CommandType::BIND_INDEX_BUFFER:
124       {
125         fprintf(output, "{\"Cmd\":\"BIND_INDEX_BUFFERS\"}\n");
126         break;
127       }
128       case GLES::CommandType::BIND_SAMPLERS:
129       {
130         fprintf(output, "{\"Cmd\":\"BIND_SAMPLERS\"}\n");
131         break;
132       }
133       case GLES::CommandType::BIND_PIPELINE:
134       {
135         fprintf(output, "{\"Cmd\":\"BIND_PIPELINE\"}\n");
136         break;
137       }
138       case GLES::CommandType::DRAW:
139       {
140         fprintf(output, "{\"Cmd\":\"DRAW\"}\n");
141         break;
142       }
143       case GLES::CommandType::DRAW_INDEXED:
144       {
145         fprintf(output, "{\"Cmd\":\"DRAW_INDEXED\"}\n");
146         break;
147       }
148       case GLES::CommandType::DRAW_NATIVE:
149       {
150         fprintf(output, "{\"Cmd\":\"DRAW_NATIVE\"}\n");
151         break;
152       }
153       case GLES::CommandType::DRAW_INDEXED_INDIRECT:
154       {
155         fprintf(output, "{\"Cmd\":\"DRAW_INDEXED_INDIRECT\"}\n");
156         break;
157       }
158       case GLES::CommandType::SET_SCISSOR: // @todo Consider correcting for orientation here?
159       {
160         fprintf(output, "{\"Cmd\":\"SET_SCISSOR\",\n\"region\":[%d,%d,%d,%d]\n}\n", cmd.scissor.region.x, cmd.scissor.region.y, cmd.scissor.region.width, cmd.scissor.region.height);
161         break;
162       }
163       case GLES::CommandType::SET_SCISSOR_TEST:
164       {
165         fprintf(output, "{\"Cmd\":\"SET_SCISSOR_TEST\",\n\"enable\":%s\n}\n", (cmd.scissorTest.enable ? "\"true\"" : "\"false\""));
166         break;
167       }
168       case GLES::CommandType::SET_VIEWPORT: // @todo Consider correcting for orientation here?
169       {
170         fprintf(output, "{\"Cmd\":\"SET_VIEWPORT\",\n\"region\":[%f,%f,%f,%f]\n}\n", cmd.viewport.region.x, cmd.viewport.region.y, cmd.viewport.region.width, cmd.viewport.region.height);
171         break;
172       }
173       case GLES::CommandType::SET_COLOR_MASK:
174       {
175         fprintf(output, "{\"Cmd\":\"SET_COLOR_MASK\",\n\"enable\":%s\n}\n", (cmd.colorMask.enabled ? "\"true\"" : "\"false\""));
176         break;
177       }
178       case GLES::CommandType::CLEAR_STENCIL_BUFFER:
179       {
180         fprintf(output, "{\"Cmd\":\"CLEAR_STENCIL_BUFFER\"}\n");
181         break;
182       }
183       case GLES::CommandType::CLEAR_DEPTH_BUFFER:
184       {
185         fprintf(output, "{\"Cmd\":\"CLEAR_DEPTH_BUFFER\"}\n");
186         break;
187       }
188
189       case GLES::CommandType::SET_STENCIL_TEST_ENABLE:
190       {
191         fprintf(output, "{\"Cmd\":\"SET_STENCIL_TEST_ENABLE\",\n\"enable\":%s\n}\n", (cmd.stencilTest.enabled ? "\"true\"" : "\"false\""));
192         break;
193       }
194
195       case GLES::CommandType::SET_STENCIL_FUNC:
196       {
197         fprintf(output,
198                 "{\"Cmd\":\"STENCIL_FUNC\",\n"
199                 "\"compareOp\":\"%s\",\n"
200                 "\"reference\":\"0x%x\",\n"
201                 "\"compareMask\":\"0x%x\"\n}",
202                 DumpCompareOp(cmd.stencilFunc.compareOp).c_str(),
203                 cmd.stencilFunc.reference,
204                 cmd.stencilFunc.compareMask);
205         break;
206       }
207
208       case GLES::CommandType::SET_STENCIL_WRITE_MASK:
209       {
210         fprintf(output, "{\"Cmd\":\"SET_STENCIL_WRITE_MASK\",\n\"mask\":%d\n}\n", cmd.stencilWriteMask.mask);
211         break;
212       }
213
214       case GLES::CommandType::SET_STENCIL_OP:
215       {
216         fprintf(output,
217                 "{\"Cmd\":\"SET_STENCIL_OP\",\n"
218                 "\"failOp\":\"%s\",\n"
219                 "\"depthFailOp\":\"%s\",\n"
220                 "\"passOp\":\"%s\"\n}",
221
222                 DumpStencilOp(cmd.stencilOp.failOp).c_str(),
223                 DumpStencilOp(cmd.stencilOp.depthFailOp).c_str(),
224                 DumpStencilOp(cmd.stencilOp.passOp).c_str());
225         break;
226       }
227
228       case GLES::CommandType::SET_DEPTH_COMPARE_OP:
229       {
230         fprintf(output,
231                 "{\"Cmd\":\"SET_DEPTH_COMPARE_OP\",\n"
232                 "\"compareOp\":\"%s\"\n}\n",
233                 DumpCompareOp(cmd.depth.compareOp).c_str());
234         break;
235       }
236       case GLES::CommandType::SET_DEPTH_TEST_ENABLE:
237       {
238         fprintf(output, "{\"Cmd\":\"SET_DEPTH_TEST_ENABLE\",\n\"enable\":%s\n}\n", (cmd.depth.testEnabled ? "\"true\"" : "\"false\""));
239         break;
240       }
241       case GLES::CommandType::SET_DEPTH_WRITE_ENABLE:
242       {
243         fprintf(output, "{\"Cmd\":\"SET_DEPTH_WRITE_ENABLE\",\n\"enable\":%s\n}\n", (cmd.depth.writeEnabled ? "\"true\"" : "\"false\""));
244         break;
245       }
246
247       case GLES::CommandType::BEGIN_RENDERPASS:
248       {
249         fprintf(output,
250                 "{\"Cmd\":\"BEGIN_RENDER_PASS\",\n"
251                 "\"renderTarget\":\"%p\",\n"
252                 "\"renderPass\":\"%p\",\n"
253                 "\"renderArea\":[%d,%d,%d,%d],\n",
254                 cmd.beginRenderPass.renderTarget,
255                 cmd.beginRenderPass.renderPass,
256                 cmd.beginRenderPass.renderArea.x,
257                 cmd.beginRenderPass.renderArea.y,
258                 cmd.beginRenderPass.renderArea.width,
259                 cmd.beginRenderPass.renderArea.height);
260         fprintf(output, "\"clearValues\":[");
261         bool firstV = true;
262         for(auto i = 0u; i < cmd.beginRenderPass.clearValuesCount; ++i)
263         {
264           auto value = cmd.beginRenderPass.clearValues.Ptr()[i];
265           if(!firstV)
266           {
267             fprintf(output, ",");
268           }
269           firstV = false;
270           fprintf(output, "[%f,%f,%f,%f]", value.color.r, value.color.g, value.color.b, value.color.a);
271         }
272         fprintf(output, "]\n}");
273         break;
274       }
275       case GLES::CommandType::END_RENDERPASS:
276       {
277         fprintf(output, "{\"Cmd\":\"END_RENDER_PASS\"}\n");
278         break;
279       }
280       case GLES::CommandType::PRESENT_RENDER_TARGET:
281       {
282         fprintf(output, "{\"Cmd\":\"PRESENT_RENDER_TARGET\"}\n");
283         break;
284       }
285       case GLES::CommandType::EXECUTE_COMMAND_BUFFERS:
286       {
287         fprintf(output, "{\"Cmd\":\"EXECUTE_COMMAND_BUFFERS\",\n\"buffers\":[");
288         bool firstBuf{true};
289         for(auto i = 0u; i < cmd.executeCommandBuffers.buffersCount; ++i)
290         {
291           const auto buf = cmd.executeCommandBuffers.buffers.Ptr()[i];
292           if(!firstBuf)
293           {
294             fprintf(output, ", ");
295           }
296           firstBuf = false;
297           DumpCommandBuffer(output, buf);
298         }
299         fprintf(output, "]\n}");
300         break;
301       }
302     }
303   }
304 }
305
306 GraphicsFrameDump::GraphicsFrameDump()
307 : outputStream(nullptr, nullptr)
308 {
309   char* outfile = getenv("GRAPHICS_CMDBUF_OUTFILE");
310   if(outfile)
311   {
312     outputStream = UniqueFilePtr(std::fopen(outfile, "w"), std::fclose);
313     output       = outputStream.get();
314   }
315   if(!output)
316     output = stderr;
317 }
318
319 void GraphicsFrameDump::Start()
320 {
321   if(IsDumpFrame())
322   {
323     if(!firstFrame)
324     {
325       fprintf(output, ", \n");
326     }
327     firstFrame   = false;
328     firstBuffer  = true;
329     dumpingFrame = true;
330     fprintf(output, "{\"Queue #%d\":[\n", frameCount);
331   }
332 }
333
334 void GraphicsFrameDump::DumpCommandBuffer(const GLES::CommandBuffer* cmdBuf)
335 {
336   if(dumpingFrame)
337   {
338     if(!firstBuffer)
339     {
340       fprintf(output, ", \n");
341     }
342     firstBuffer = false;
343     fprintf(output, "[\n");
344     Graphics::DumpCommandBuffer(output, cmdBuf);
345     fprintf(output, "]\n");
346   }
347 }
348
349 void GraphicsFrameDump::End()
350 {
351   if(dumpingFrame)
352   {
353     fprintf(output, "]}\n");
354   }
355   firstBuffer  = true;
356   dumpingFrame = false;
357 }
358
359 bool GraphicsFrameDump::IsDumpFrame()
360 {
361   bool dump = false;
362
363   frameCount++;
364
365   dump = (frameCount < NTH_FRAME);
366
367   // Or, could also use an enviroment variable as a trigger
368   // e.g. if getenv(X) is set, then start dumping again, and clear X.
369
370   return dump;
371 }
372
373 } // namespace Dali::Graphics