Updated test harness
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor / dali-test-suite-utils / test-graphics-controller.cpp
1 /*
2  * Copyright (c) 2021 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 "test-graphics-controller.h"
18
19 #include "test-graphics-buffer.h"
20 #include "test-graphics-command-buffer.h"
21 #include "test-graphics-framebuffer.h"
22 #include "test-graphics-reflection.h"
23 #include "test-graphics-render-pass.h"
24 #include "test-graphics-render-target.h"
25 #include "test-graphics-sampler.h"
26 #include "test-graphics-shader.h"
27 #include "test-graphics-texture.h"
28
29 #include <dali/integration-api/gl-defines.h>
30 #include <cstdio>
31 #include <iostream>
32 #include <sstream>
33
34 namespace Dali
35 {
36 std::ostream& operator<<(std::ostream& o, const Graphics::BufferCreateInfo& bufferCreateInfo)
37 {
38   return o << "usage:" << std::hex << bufferCreateInfo.usage << ", size:" << std::dec << bufferCreateInfo.size;
39 }
40
41 std::ostream& operator<<(std::ostream& o, const Graphics::CommandBufferCreateInfo& commandBufferCreateInfo)
42 {
43   return o << "level:" << (commandBufferCreateInfo.level == Graphics::CommandBufferLevel::PRIMARY ? "PRIMARY" : "SECONDARY")
44            << ", fixedCapacity:" << std::dec << commandBufferCreateInfo.fixedCapacity;
45 }
46
47 std::ostream& operator<<(std::ostream& o, const Graphics::TextureType& textureType)
48 {
49   switch(textureType)
50   {
51     case Graphics::TextureType::TEXTURE_2D:
52       o << "TEXTURE_2D";
53       break;
54     case Graphics::TextureType::TEXTURE_3D:
55       o << "TEXTURE_3D";
56       break;
57     case Graphics::TextureType::TEXTURE_CUBEMAP:
58       o << "TEXTURE_CUBEMAP";
59       break;
60   }
61   return o;
62 }
63
64 std::ostream& operator<<(std::ostream& o, const Graphics::Extent2D extent)
65 {
66   o << "width:" << extent.width << ", height:" << extent.height;
67   return o;
68 }
69
70 std::ostream& operator<<(std::ostream& o, const Graphics::TextureCreateInfo& createInfo)
71 {
72   o << "textureType:" << createInfo.textureType
73     << " size:" << createInfo.size
74     << " format:" << static_cast<uint32_t>(createInfo.format)
75     << " mipMapFlag:" << createInfo.mipMapFlag
76     << " layout:" << (createInfo.layout == Graphics::TextureLayout::LINEAR ? "LINEAR" : "OPTIMAL")
77     << " usageFlags:" << std::hex << createInfo.usageFlags
78     << " data:" << std::hex << createInfo.data
79     << " dataSize:" << std::dec << createInfo.dataSize
80     << " nativeImagePtr:" << std::hex << createInfo.nativeImagePtr;
81   return o;
82 }
83
84 std::ostream& operator<<(std::ostream& o, Graphics::SamplerAddressMode addressMode)
85 {
86   switch(addressMode)
87   {
88     case Graphics::SamplerAddressMode::REPEAT:
89       o << "REPEAT";
90       break;
91     case Graphics::SamplerAddressMode::MIRRORED_REPEAT:
92       o << "MIRRORED_REPEAT";
93       break;
94     case Graphics::SamplerAddressMode::CLAMP_TO_EDGE:
95       o << "CLAMP_TO_EDGE";
96       break;
97     case Graphics::SamplerAddressMode::CLAMP_TO_BORDER:
98       o << "CLAMP_TO_BORDER";
99       break;
100     case Graphics::SamplerAddressMode::MIRROR_CLAMP_TO_EDGE:
101       o << "MIRROR_CLAMP_TO_EDGE";
102       break;
103   }
104   return o;
105 }
106
107 std::ostream& operator<<(std::ostream& o, Graphics::SamplerFilter filterMode)
108 {
109   switch(filterMode)
110   {
111     case Graphics::SamplerFilter::LINEAR:
112       o << "LINEAR";
113       break;
114     case Graphics::SamplerFilter::NEAREST:
115       o << "NEAREST";
116       break;
117   }
118   return o;
119 }
120
121 std::ostream& operator<<(std::ostream& o, Graphics::SamplerMipmapMode mipmapMode)
122 {
123   switch(mipmapMode)
124   {
125     case Graphics::SamplerMipmapMode::NONE:
126       o << "NONE";
127       break;
128     case Graphics::SamplerMipmapMode::LINEAR:
129       o << "LINEAR";
130       break;
131     case Graphics::SamplerMipmapMode::NEAREST:
132       o << "NEAREST";
133       break;
134   }
135   return o;
136 }
137
138 std::ostream& operator<<(std::ostream& o, const Graphics::SamplerCreateInfo& createInfo)
139 {
140   o << "minFilter:" << createInfo.minFilter
141     << " magFilter:" << createInfo.magFilter
142     << " wrapModeU:" << createInfo.addressModeU
143     << " wrapModeV:" << createInfo.addressModeV
144     << " wrapModeW:" << createInfo.addressModeW
145     << " mipMapMode:" << createInfo.mipMapMode;
146   return o;
147 }
148
149 std::ostream& operator<<(std::ostream& o, const Graphics::ColorAttachment& colorAttachment)
150 {
151   o << "attachmentId:" << colorAttachment.attachmentId
152     << " layerId:" << colorAttachment.layerId
153     << " levelId:" << colorAttachment.levelId
154     << " texture:" << colorAttachment.texture;
155   return o;
156 }
157
158 std::ostream& operator<<(std::ostream& o, const Graphics::DepthStencilAttachment& depthStencilAttachment)
159 {
160   o << "depthTexture:" << depthStencilAttachment.depthTexture
161     << "depthLevel:" << depthStencilAttachment.depthLevel
162     << "stencilTexture:" << depthStencilAttachment.stencilTexture
163     << "stencilLevel:" << depthStencilAttachment.stencilLevel;
164   return o;
165 }
166
167 std::ostream& operator<<(std::ostream& o, const Graphics::FramebufferCreateInfo& createInfo)
168 {
169   o << "colorAttachments:";
170   for(auto i = 0u; i < createInfo.colorAttachments.size(); ++i)
171   {
172     o << "[" << i << "]=" << createInfo.colorAttachments[i] << "  ";
173   }
174   o << "depthStencilAttachment:" << createInfo.depthStencilAttachment;
175   o << "size: " << createInfo.size;
176   return o;
177 }
178
179 int GetNumComponents(Graphics::VertexInputFormat vertexFormat)
180 {
181   switch(vertexFormat)
182   {
183     case Graphics::VertexInputFormat::UNDEFINED:
184     case Graphics::VertexInputFormat::FLOAT:
185     case Graphics::VertexInputFormat::INTEGER:
186       return 1;
187     case Graphics::VertexInputFormat::IVECTOR2:
188     case Graphics::VertexInputFormat::FVECTOR2:
189       return 2;
190     case Graphics::VertexInputFormat::IVECTOR3:
191     case Graphics::VertexInputFormat::FVECTOR3:
192       return 3;
193     case Graphics::VertexInputFormat::FVECTOR4:
194     case Graphics::VertexInputFormat::IVECTOR4:
195       return 4;
196   }
197   return 1;
198 }
199
200 GLint GetSize(Graphics::VertexInputFormat vertexFormat)
201 {
202   switch(vertexFormat)
203   {
204     case Graphics::VertexInputFormat::UNDEFINED:
205       return 1u;
206     case Graphics::VertexInputFormat::INTEGER:
207     case Graphics::VertexInputFormat::IVECTOR2:
208     case Graphics::VertexInputFormat::IVECTOR3:
209     case Graphics::VertexInputFormat::IVECTOR4:
210       return 2u;
211     case Graphics::VertexInputFormat::FLOAT:
212     case Graphics::VertexInputFormat::FVECTOR2:
213     case Graphics::VertexInputFormat::FVECTOR3:
214     case Graphics::VertexInputFormat::FVECTOR4:
215       return 4u;
216   }
217   return 1u;
218 }
219
220 GLint GetGlType(Graphics::VertexInputFormat vertexFormat)
221 {
222   switch(vertexFormat)
223   {
224     case Graphics::VertexInputFormat::UNDEFINED:
225       return GL_BYTE;
226     case Graphics::VertexInputFormat::INTEGER:
227     case Graphics::VertexInputFormat::IVECTOR2:
228     case Graphics::VertexInputFormat::IVECTOR3:
229     case Graphics::VertexInputFormat::IVECTOR4:
230       return GL_SHORT;
231     case Graphics::VertexInputFormat::FLOAT:
232     case Graphics::VertexInputFormat::FVECTOR2:
233     case Graphics::VertexInputFormat::FVECTOR3:
234     case Graphics::VertexInputFormat::FVECTOR4:
235       return GL_FLOAT;
236   }
237   return GL_BYTE;
238 }
239
240 GLenum GetTopology(Graphics::PrimitiveTopology topology)
241 {
242   switch(topology)
243   {
244     case Graphics::PrimitiveTopology::POINT_LIST:
245       return GL_POINTS;
246
247     case Graphics::PrimitiveTopology::LINE_LIST:
248       return GL_LINES;
249
250     case Graphics::PrimitiveTopology::LINE_LOOP:
251       return GL_LINE_LOOP;
252
253     case Graphics::PrimitiveTopology::LINE_STRIP:
254       return GL_LINE_STRIP;
255
256     case Graphics::PrimitiveTopology::TRIANGLE_LIST:
257       return GL_TRIANGLES;
258
259     case Graphics::PrimitiveTopology::TRIANGLE_STRIP:
260       return GL_TRIANGLE_STRIP;
261
262     case Graphics::PrimitiveTopology::TRIANGLE_FAN:
263       return GL_TRIANGLE_FAN;
264   }
265   return GL_TRIANGLES;
266 }
267
268 GLenum GetCullFace(Graphics::CullMode cullMode)
269 {
270   switch(cullMode)
271   {
272     case Graphics::CullMode::NONE:
273       return GL_NONE;
274     case Graphics::CullMode::FRONT:
275       return GL_FRONT;
276     case Graphics::CullMode::BACK:
277       return GL_BACK;
278     case Graphics::CullMode::FRONT_AND_BACK:
279       return GL_FRONT_AND_BACK;
280   }
281   return GL_NONE;
282 }
283
284 GLenum GetFrontFace(Graphics::FrontFace frontFace)
285 {
286   if(frontFace == Graphics::FrontFace::CLOCKWISE)
287   {
288     return GL_CW;
289   }
290   return GL_CCW;
291 }
292
293 GLenum GetBlendFactor(Graphics::BlendFactor blendFactor)
294 {
295   GLenum glFactor = GL_ZERO;
296
297   switch(blendFactor)
298   {
299     case Graphics::BlendFactor::ZERO:
300       glFactor = GL_ZERO;
301       break;
302     case Graphics::BlendFactor::ONE:
303       glFactor = GL_ONE;
304       break;
305     case Graphics::BlendFactor::SRC_COLOR:
306       glFactor = GL_SRC_COLOR;
307       break;
308     case Graphics::BlendFactor::ONE_MINUS_SRC_COLOR:
309       glFactor = GL_ONE_MINUS_SRC_COLOR;
310       break;
311     case Graphics::BlendFactor::DST_COLOR:
312       glFactor = GL_DST_COLOR;
313       break;
314     case Graphics::BlendFactor::ONE_MINUS_DST_COLOR:
315       glFactor = GL_ONE_MINUS_DST_COLOR;
316       break;
317     case Graphics::BlendFactor::SRC_ALPHA:
318       glFactor = GL_SRC_ALPHA;
319       break;
320     case Graphics::BlendFactor::ONE_MINUS_SRC_ALPHA:
321       glFactor = GL_ONE_MINUS_SRC_ALPHA;
322       break;
323     case Graphics::BlendFactor::DST_ALPHA:
324       glFactor = GL_DST_ALPHA;
325       break;
326     case Graphics::BlendFactor::ONE_MINUS_DST_ALPHA:
327       glFactor = GL_ONE_MINUS_DST_ALPHA;
328       break;
329     case Graphics::BlendFactor::CONSTANT_COLOR:
330       glFactor = GL_CONSTANT_COLOR;
331       break;
332     case Graphics::BlendFactor::ONE_MINUS_CONSTANT_COLOR:
333       glFactor = GL_ONE_MINUS_CONSTANT_COLOR;
334       break;
335     case Graphics::BlendFactor::CONSTANT_ALPHA:
336       glFactor = GL_CONSTANT_ALPHA;
337       break;
338     case Graphics::BlendFactor::ONE_MINUS_CONSTANT_ALPHA:
339       glFactor = GL_ONE_MINUS_CONSTANT_ALPHA;
340       break;
341     case Graphics::BlendFactor::SRC_ALPHA_SATURATE:
342       glFactor = GL_SRC_ALPHA_SATURATE;
343       break;
344       // GLES doesn't appear to have dual source blending.
345     case Graphics::BlendFactor::SRC1_COLOR:
346       glFactor = GL_SRC_COLOR;
347       break;
348     case Graphics::BlendFactor::ONE_MINUS_SRC1_COLOR:
349       glFactor = GL_ONE_MINUS_SRC_COLOR;
350       break;
351     case Graphics::BlendFactor::SRC1_ALPHA:
352       glFactor = GL_SRC_ALPHA;
353       break;
354     case Graphics::BlendFactor::ONE_MINUS_SRC1_ALPHA:
355       glFactor = GL_ONE_MINUS_SRC_ALPHA;
356       break;
357   }
358   return glFactor;
359 }
360
361 GLenum GetBlendOp(Graphics::BlendOp blendOp)
362 {
363   GLenum op = GL_FUNC_ADD;
364   switch(blendOp)
365   {
366     case Graphics::BlendOp::ADD:
367       op = GL_FUNC_ADD;
368       break;
369     case Graphics::BlendOp::SUBTRACT:
370       op = GL_FUNC_SUBTRACT;
371       break;
372     case Graphics::BlendOp::REVERSE_SUBTRACT:
373       op = GL_FUNC_REVERSE_SUBTRACT;
374       break;
375     case Graphics::BlendOp::MIN:
376       op = GL_MIN;
377       break;
378     case Graphics::BlendOp::MAX:
379       op = GL_MAX;
380       break;
381
382       // @todo Add advanced blend equations
383   }
384   return op;
385 }
386
387 class TestGraphicsMemory : public Graphics::Memory
388 {
389 public:
390   TestGraphicsMemory(TraceCallStack& callStack, TestGraphicsBuffer& buffer, uint32_t mappedOffset, uint32_t mappedSize)
391   : mCallStack(callStack),
392     mBuffer(buffer),
393     mMappedOffset(mappedOffset),
394     mMappedSize(mappedSize),
395     mLockedOffset(0u),
396     mLockedSize(0u)
397   {
398   }
399
400   void* LockRegion(uint32_t offset, uint32_t size) override
401   {
402     std::ostringstream o;
403     o << offset << ", " << size;
404     mCallStack.PushCall("Memory::LockRegion", o.str());
405
406     if(offset > mMappedOffset + mMappedSize ||
407        size + offset > mMappedOffset + mMappedSize)
408     {
409       fprintf(stderr, "TestGraphics.Memory::LockRegion() Out of bounds");
410       mBuffer.memory.resize(mMappedOffset + offset + size); // Grow to prevent memcpy from crashing
411     }
412     mLockedOffset = offset;
413     mLockedSize   = size;
414     return &mBuffer.memory[mMappedOffset + offset];
415   }
416
417   void Unlock(bool flush) override
418   {
419     mCallStack.PushCall("Memory::Unlock", (flush ? "Flush" : "NoFlush"));
420     if(flush)
421     {
422       Flush();
423     }
424   }
425
426   void Flush() override
427   {
428     mCallStack.PushCall("Memory::Flush", "");
429     mBuffer.Bind();
430     mBuffer.Upload(mMappedOffset + mLockedOffset, mLockedSize);
431     mBuffer.Unbind();
432   }
433
434   TraceCallStack&     mCallStack;
435   TestGraphicsBuffer& mBuffer;
436   uint32_t            mMappedOffset;
437   uint32_t            mMappedSize;
438   uint32_t            mLockedOffset;
439   uint32_t            mLockedSize;
440 };
441
442 TestGraphicsController::TestGraphicsController()
443 : mCallStack(true, "TestGraphicsController."),
444   mCommandBufferCallStack(true, "TestCommandBuffer."),
445   mFrameBufferCallStack(true, "TestFrameBuffer.")
446 {
447   mCallStack.Enable(true);
448   mCommandBufferCallStack.Enable(true);
449   auto& trace = mGl.GetTextureTrace();
450   trace.Enable(true);
451   trace.EnableLogging(true);
452 }
453
454 void TestGraphicsController::SubmitCommandBuffers(const Graphics::SubmitInfo& submitInfo)
455 {
456   TraceCallStack::NamedParams namedParams;
457   namedParams["submitInfo"] << "cmdBuffer[" << submitInfo.cmdBuffer.size()
458                             << "], flags:" << std::hex << submitInfo.flags;
459
460   mCallStack.PushCall("SubmitCommandBuffers", "", namedParams);
461
462   mSubmitStack.emplace_back(submitInfo);
463
464   for(auto& graphicsCommandBuffer : submitInfo.cmdBuffer)
465   {
466     auto commandBuffer = Uncast<TestGraphicsCommandBuffer>(graphicsCommandBuffer);
467     ProcessCommandBuffer(*commandBuffer);
468   }
469 }
470
471 void TestGraphicsController::ProcessCommandBuffer(TestGraphicsCommandBuffer& commandBuffer)
472 {
473   bool                     scissorEnabled = false;
474   TestGraphicsFramebuffer* currentFramebuffer{nullptr};
475   TestGraphicsPipeline*    currentPipeline{nullptr};
476
477   for(auto& cmd : commandBuffer.GetCommands())
478   {
479     // process command
480     switch(cmd.type)
481     {
482       case CommandType::FLUSH:
483       {
484         // Nothing to do here
485         break;
486       }
487       case CommandType::BIND_TEXTURES:
488       {
489         for(auto& binding : cmd.data.bindTextures.textureBindings)
490         {
491           if(binding.texture)
492           {
493             auto texture = Uncast<TestGraphicsTexture>(binding.texture);
494             texture->Bind(binding.binding);
495
496             if(binding.sampler)
497             {
498               auto sampler = Uncast<TestGraphicsSampler>(binding.sampler);
499               if(sampler)
500               {
501                 sampler->Apply(texture->GetTarget());
502               }
503             }
504
505             texture->Prepare(); // Ensure native texture is ready
506           }
507         }
508         break;
509       }
510       case CommandType::BIND_VERTEX_BUFFERS:
511       {
512         for(auto& binding : cmd.data.bindVertexBuffers.vertexBufferBindings)
513         {
514           auto graphicsBuffer = binding.buffer;
515           auto vertexBuffer   = Uncast<TestGraphicsBuffer>(graphicsBuffer);
516           vertexBuffer->Bind();
517         }
518         break;
519       }
520       case CommandType::BIND_INDEX_BUFFER:
521       {
522         auto& indexBufferBinding = cmd.data.bindIndexBuffer;
523         if(indexBufferBinding.buffer)
524         {
525           auto buffer = Uncast<TestGraphicsBuffer>(indexBufferBinding.buffer);
526           buffer->Bind();
527         }
528         break;
529       }
530       case CommandType::BIND_UNIFORM_BUFFER:
531       {
532         auto& bindings = cmd.data.bindUniformBuffers;
533         auto  buffer   = bindings.standaloneUniformsBufferBinding;
534
535         // based on reflection, issue gl calls
536         buffer.buffer->BindAsUniformBuffer(static_cast<const TestGraphicsProgram*>(currentPipeline->programState.program));
537         break;
538       }
539       case CommandType::BIND_SAMPLERS:
540       {
541         break;
542       }
543       case CommandType::BIND_PIPELINE:
544       {
545         currentPipeline = Uncast<TestGraphicsPipeline>(cmd.data.bindPipeline.pipeline);
546
547         // Bind framebuffer if different. @todo Move to RenderPass
548         auto framebuffer = currentPipeline->framebufferState.framebuffer;
549         if(framebuffer && framebuffer != currentFramebuffer)
550         {
551           auto graphicsFramebuffer = Uncast<TestGraphicsFramebuffer>(framebuffer);
552           graphicsFramebuffer->Bind();
553         }
554         else
555         {
556           if(currentFramebuffer)
557             currentFramebuffer->Bind();
558           else
559             mGl.BindFramebuffer(GL_FRAMEBUFFER, 0);
560         }
561         BindPipeline(currentPipeline);
562         break;
563       }
564       case CommandType::DRAW:
565       {
566         mGl.DrawArrays(GetTopology(currentPipeline->inputAssemblyState.topology),
567                        0,
568                        cmd.data.draw.draw.vertexCount);
569         break;
570       }
571       case CommandType::DRAW_INDEXED:
572       {
573         mGl.DrawElements(GetTopology(currentPipeline->inputAssemblyState.topology),
574                          static_cast<GLsizei>(cmd.data.draw.drawIndexed.indexCount),
575                          GL_UNSIGNED_SHORT,
576                          reinterpret_cast<void*>(cmd.data.draw.drawIndexed.firstIndex));
577         break;
578       }
579       case CommandType::DRAW_INDEXED_INDIRECT:
580       {
581         mGl.DrawElements(GetTopology(currentPipeline->inputAssemblyState.topology),
582                          static_cast<GLsizei>(cmd.data.draw.drawIndexed.indexCount),
583                          GL_UNSIGNED_SHORT,
584                          reinterpret_cast<void*>(cmd.data.draw.drawIndexed.firstIndex));
585         break;
586       }
587       case CommandType::SET_SCISSOR:
588       {
589         if(scissorEnabled)
590         {
591           auto& rect = cmd.data.scissor.region;
592           mGl.Scissor(rect.x, rect.y, rect.width, rect.height);
593         }
594         break;
595       }
596       case CommandType::SET_SCISSOR_TEST:
597       {
598         if(cmd.data.scissorTest.enable)
599         {
600           mGl.Enable(GL_SCISSOR_TEST);
601           scissorEnabled = true;
602         }
603         else
604         {
605           mGl.Disable(GL_SCISSOR_TEST);
606           scissorEnabled = false;
607         }
608         break;
609       }
610       case CommandType::SET_VIEWPORT_TEST:
611       {
612         break;
613       }
614       case CommandType::SET_VIEWPORT: // @todo Consider correcting for orientation here?
615       {
616         auto& rect = cmd.data.viewport.region;
617         mGl.Viewport(rect.x, rect.y, rect.width, rect.height);
618         break;
619       }
620       case CommandType::EXECUTE_COMMAND_BUFFERS:
621       {
622         // Process secondary command buffers
623         for(auto& buf : cmd.data.executeCommandBuffers.buffers)
624         {
625           ProcessCommandBuffer(*static_cast<TestGraphicsCommandBuffer*>(buf));
626         }
627         break;
628       }
629       case CommandType::BEGIN_RENDER_PASS:
630       {
631         auto renderTarget = Uncast<TestGraphicsRenderTarget>(cmd.data.beginRenderPass.renderTarget);
632
633         if(renderTarget)
634         {
635           auto fb = renderTarget->mCreateInfo.framebuffer;
636           if(fb)
637           {
638             if(currentFramebuffer != fb)
639             {
640               currentFramebuffer = Uncast<TestGraphicsFramebuffer>(fb);
641               currentFramebuffer->Bind();
642             }
643           }
644           else
645           {
646             mGl.BindFramebuffer(GL_FRAMEBUFFER, 0);
647           }
648         }
649         else
650         {
651           mGl.BindFramebuffer(GL_FRAMEBUFFER, 0);
652         }
653
654         auto& clearValues = cmd.data.beginRenderPass.clearValues;
655         if(clearValues.size() > 0)
656         {
657           const auto renderPass = static_cast<TestGraphicsRenderPass*>(cmd.data.beginRenderPass.renderPass);
658           if(renderPass)
659           {
660             const auto& color0 = renderPass->attachments[0];
661             GLuint      mask   = 0;
662             if(color0.loadOp == Graphics::AttachmentLoadOp::CLEAR)
663             {
664               mask |= GL_COLOR_BUFFER_BIT;
665
666               // Set clear color (todo: cache it!)
667               // Something goes wrong here if Alpha mask is GL_TRUE
668               mGl.ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
669               mGl.ClearColor(clearValues[0].color.r,
670                              clearValues[0].color.g,
671                              clearValues[0].color.b,
672                              clearValues[0].color.a);
673             }
674
675             // check for depth stencil
676             if(renderPass->attachments.size() > 1)
677             {
678               const auto& depthStencil = renderPass->attachments.back();
679               if(depthStencil.loadOp == Graphics::AttachmentLoadOp::CLEAR)
680               {
681                 mask |= GL_DEPTH_BUFFER_BIT;
682               }
683               if(depthStencil.stencilLoadOp == Graphics::AttachmentLoadOp::CLEAR)
684               {
685                 mask |= GL_STENCIL_BUFFER_BIT;
686               }
687             }
688
689             if(mask != 0)
690             {
691               // Test scissor area and RT size
692               const auto& area = cmd.data.beginRenderPass.renderArea;
693               if(area.x == 0 &&
694                  area.y == 0 &&
695                  area.width == renderTarget->mCreateInfo.extent.width &&
696                  area.height == renderTarget->mCreateInfo.extent.height)
697               {
698                 mGl.Disable(GL_SCISSOR_TEST);
699                 mGl.Clear(mask);
700               }
701               else
702               {
703                 mGl.Enable(GL_SCISSOR_TEST);
704                 mGl.Scissor(cmd.data.beginRenderPass.renderArea.x, cmd.data.beginRenderPass.renderArea.y, cmd.data.beginRenderPass.renderArea.width, cmd.data.beginRenderPass.renderArea.height);
705                 mGl.Clear(mask);
706                 mGl.Disable(GL_SCISSOR_TEST);
707               }
708             }
709           }
710           else
711           {
712             DALI_ASSERT_DEBUG(0 && "BeginRenderPass has no render pass");
713           }
714         }
715         break;
716       }
717       case CommandType::END_RENDER_PASS:
718       {
719         break;
720       }
721     }
722   }
723 }
724
725 void TestGraphicsController::BindPipeline(TestGraphicsPipeline* pipeline)
726 {
727   auto& vi = pipeline->vertexInputState;
728   for(auto& attribute : vi.attributes)
729   {
730     mGl.EnableVertexAttribArray(attribute.location);
731     uint32_t attributeOffset = attribute.offset;
732     GLsizei  stride          = vi.bufferBindings[attribute.binding].stride;
733
734     mGl.VertexAttribPointer(attribute.location,
735                             GetNumComponents(attribute.format),
736                             GetGlType(attribute.format),
737                             GL_FALSE, // Not normalized
738                             stride,
739                             reinterpret_cast<void*>(attributeOffset));
740   }
741
742   // Cull face setup
743   auto& rasterizationState = pipeline->rasterizationState;
744   if(rasterizationState.cullMode == Graphics::CullMode::NONE)
745   {
746     mGl.Disable(GL_CULL_FACE);
747   }
748   else
749   {
750     mGl.Enable(GL_CULL_FACE);
751     mGl.CullFace(GetCullFace(rasterizationState.cullMode));
752   }
753
754   mGl.FrontFace(GetFrontFace(rasterizationState.frontFace));
755
756   // Blending setup
757   auto& colorBlendState = pipeline->colorBlendState;
758   if(colorBlendState.blendEnable)
759   {
760     mGl.Enable(GL_BLEND);
761
762     mGl.BlendFuncSeparate(GetBlendFactor(colorBlendState.srcColorBlendFactor),
763                           GetBlendFactor(colorBlendState.dstColorBlendFactor),
764                           GetBlendFactor(colorBlendState.srcAlphaBlendFactor),
765                           GetBlendFactor(colorBlendState.dstAlphaBlendFactor));
766     if(colorBlendState.colorBlendOp != colorBlendState.alphaBlendOp)
767     {
768       mGl.BlendEquationSeparate(GetBlendOp(colorBlendState.colorBlendOp), GetBlendOp(colorBlendState.alphaBlendOp));
769     }
770     else
771     {
772       mGl.BlendEquation(GetBlendOp(colorBlendState.colorBlendOp));
773     }
774     mGl.BlendColor(colorBlendState.blendConstants[0],
775                    colorBlendState.blendConstants[1],
776                    colorBlendState.blendConstants[2],
777                    colorBlendState.blendConstants[3]);
778   }
779   else
780   {
781     mGl.Disable(GL_BLEND);
782   }
783 }
784
785 /**
786  * @brief Presents render target
787  * @param renderTarget render target to present
788  */
789 void TestGraphicsController::PresentRenderTarget(Graphics::RenderTarget* renderTarget)
790 {
791   TraceCallStack::NamedParams namedParams;
792   namedParams["renderTarget"] << std::hex << renderTarget;
793   mCallStack.PushCall("PresentRenderTarget", "", namedParams);
794 }
795
796 /**
797  * @brief Waits until the GPU is idle
798  */
799 void TestGraphicsController::WaitIdle()
800 {
801   mCallStack.PushCall("WaitIdle", "");
802 }
803
804 /**
805  * @brief Lifecycle pause event
806  */
807 void TestGraphicsController::Pause()
808 {
809   mCallStack.PushCall("Pause", "");
810 }
811
812 /**
813  * @brief Lifecycle resume event
814  */
815 void TestGraphicsController::Resume()
816 {
817   mCallStack.PushCall("Resume", "");
818 }
819
820 void TestGraphicsController::Shutdown()
821 {
822   mCallStack.PushCall("Shutdown", "");
823 }
824
825 void TestGraphicsController::Destroy()
826 {
827   mCallStack.PushCall("Destroy", "");
828 }
829
830 void TestGraphicsController::UpdateTextures(const std::vector<Graphics::TextureUpdateInfo>&       updateInfoList,
831                                             const std::vector<Graphics::TextureUpdateSourceInfo>& sourceList)
832 {
833   TraceCallStack::NamedParams namedParams;
834   namedParams["updateInfoList"] << "[" << updateInfoList.size() << "]:";
835   namedParams["sourceList"] << "[" << sourceList.size() << "]:";
836
837   mCallStack.PushCall("UpdateTextures", "", namedParams);
838
839   // Call either TexImage2D or TexSubImage2D
840   for(unsigned int i = 0; i < updateInfoList.size(); ++i)
841   {
842     auto& updateInfo = updateInfoList[i];
843     auto& source     = sourceList[i];
844
845     auto texture = static_cast<TestGraphicsTexture*>(updateInfo.dstTexture);
846     texture->Bind(0); // Use first texture unit during resource update
847     texture->Update(updateInfo, source);
848   }
849 }
850
851 bool TestGraphicsController::EnableDepthStencilBuffer(bool enableDepth, bool enableStencil)
852 {
853   TraceCallStack::NamedParams namedParams;
854   namedParams["enableDepth"] << (enableDepth ? "T" : "F");
855   namedParams["enableStencil"] << (enableStencil ? "T" : "F");
856   mCallStack.PushCall("EnableDepthStencilBuffer", "", namedParams);
857   return false;
858 }
859
860 void TestGraphicsController::RunGarbageCollector(size_t numberOfDiscardedRenderers)
861 {
862   TraceCallStack::NamedParams namedParams;
863   namedParams["numberOfDiscardedRenderers"] << numberOfDiscardedRenderers;
864   mCallStack.PushCall("RunGarbageCollector", "", namedParams);
865 }
866
867 void TestGraphicsController::DiscardUnusedResources()
868 {
869   mCallStack.PushCall("DiscardUnusedResources", "");
870 }
871
872 bool TestGraphicsController::IsDiscardQueueEmpty()
873 {
874   mCallStack.PushCall("IsDiscardQueueEmpty", "");
875   return isDiscardQueueEmptyResult;
876 }
877
878 /**
879  * @brief Test if the graphics subsystem has resumed & should force a draw
880  *
881  * @return true if the graphics subsystem requires a re-draw
882  */
883 bool TestGraphicsController::IsDrawOnResumeRequired()
884 {
885   mCallStack.PushCall("IsDrawOnResumeRequired", "");
886   return isDrawOnResumeRequiredResult;
887 }
888
889 Graphics::UniquePtr<Graphics::Buffer> TestGraphicsController::CreateBuffer(const Graphics::BufferCreateInfo& createInfo, Graphics::UniquePtr<Graphics::Buffer>&& oldBuffer)
890 {
891   std::ostringstream oss;
892   oss << "bufferCreateInfo:" << createInfo;
893   mCallStack.PushCall("CreateBuffer", oss.str());
894   return Graphics::MakeUnique<TestGraphicsBuffer>(mCallStack, mGl, createInfo.size, createInfo.usage);
895 }
896
897 Graphics::UniquePtr<Graphics::CommandBuffer> TestGraphicsController::CreateCommandBuffer(const Graphics::CommandBufferCreateInfo& commandBufferCreateInfo, Graphics::UniquePtr<Graphics::CommandBuffer>&& oldCommandBuffer)
898 {
899   std::ostringstream oss;
900   oss << "commandBufferCreateInfo:" << commandBufferCreateInfo;
901   mCallStack.PushCall("CreateCommandBuffer", oss.str());
902   return Graphics::MakeUnique<TestGraphicsCommandBuffer>(mCommandBufferCallStack, mGl);
903 }
904
905 Graphics::UniquePtr<Graphics::RenderPass> TestGraphicsController::CreateRenderPass(const Graphics::RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<Graphics::RenderPass>&& oldRenderPass)
906 {
907   mCallStack.PushCall("CreateRenderPass", "");
908   return Graphics::MakeUnique<TestGraphicsRenderPass>(mGl, renderPassCreateInfo);
909 }
910
911 Graphics::UniquePtr<Graphics::Texture> TestGraphicsController::CreateTexture(const Graphics::TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Graphics::Texture>&& oldTexture)
912 {
913   TraceCallStack::NamedParams namedParams;
914   namedParams["textureCreateInfo"] << textureCreateInfo;
915   mCallStack.PushCall("CreateTexture", namedParams.str(), namedParams);
916
917   return Graphics::MakeUnique<TestGraphicsTexture>(mGl, textureCreateInfo);
918 }
919
920 Graphics::UniquePtr<Graphics::Framebuffer> TestGraphicsController::CreateFramebuffer(
921   const Graphics::FramebufferCreateInfo&       createInfo,
922   Graphics::UniquePtr<Graphics::Framebuffer>&& oldFramebuffer)
923 {
924   TraceCallStack::NamedParams namedParams;
925   namedParams["framebufferCreateInfo"] << createInfo;
926   mCallStack.PushCall("Controller::CreateFramebuffer", namedParams.str(), namedParams);
927
928   return Graphics::MakeUnique<TestGraphicsFramebuffer>(mFrameBufferCallStack, mGl, createInfo);
929 }
930
931 Graphics::UniquePtr<Graphics::Pipeline> TestGraphicsController::CreatePipeline(const Graphics::PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Graphics::Pipeline>&& oldPipeline)
932 {
933   mCallStack.PushCall("CreatePipeline", "");
934   return std::make_unique<TestGraphicsPipeline>(mGl, pipelineCreateInfo);
935 }
936
937 Graphics::UniquePtr<Graphics::Program> TestGraphicsController::CreateProgram(const Graphics::ProgramCreateInfo& programCreateInfo, Graphics::UniquePtr<Graphics::Program>&& oldProgram)
938 {
939   mCallStack.PushCall("CreateProgram", "");
940
941   for(auto cacheEntry : mProgramCache)
942   {
943     bool found = true;
944     for(auto& shader : *(programCreateInfo.shaderState))
945     {
946       auto                 graphicsShader = Uncast<TestGraphicsShader>(shader.shader);
947       std::vector<uint8_t> source;
948       source.resize(graphicsShader->mCreateInfo.sourceSize);
949       memcpy(&source[0], graphicsShader->mCreateInfo.sourceData, graphicsShader->mCreateInfo.sourceSize);
950
951       if(!std::equal(source.begin(), source.end(), cacheEntry.shaders[shader.pipelineStage].begin()))
952       {
953         found = false;
954         break;
955       }
956     }
957     if(found)
958     {
959       return Graphics::MakeUnique<TestGraphicsProgram>(cacheEntry.programImpl);
960     }
961   }
962
963   mProgramCache.emplace_back();
964   mProgramCache.back().programImpl = new TestGraphicsProgramImpl(mGl, programCreateInfo, mVertexFormats, mCustomUniforms);
965   for(auto& shader : *(programCreateInfo.shaderState))
966   {
967     auto graphicsShader = Uncast<TestGraphicsShader>(shader.shader);
968     mProgramCache.back().shaders[shader.pipelineStage].resize(graphicsShader->mCreateInfo.sourceSize);
969     memcpy(&mProgramCache.back().shaders[shader.pipelineStage][0], graphicsShader->mCreateInfo.sourceData, graphicsShader->mCreateInfo.sourceSize);
970   }
971   return Graphics::MakeUnique<TestGraphicsProgram>(mProgramCache.back().programImpl);
972 }
973
974 Graphics::UniquePtr<Graphics::Shader> TestGraphicsController::CreateShader(const Graphics::ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Graphics::Shader>&& oldShader)
975 {
976   mCallStack.PushCall("CreateShader", "");
977   return Graphics::MakeUnique<TestGraphicsShader>(mGl, shaderCreateInfo);
978 }
979
980 Graphics::UniquePtr<Graphics::Sampler> TestGraphicsController::CreateSampler(const Graphics::SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Graphics::Sampler>&& oldSampler)
981 {
982   TraceCallStack::NamedParams namedParams;
983   namedParams["samplerCreateInfo"] << samplerCreateInfo;
984   mCallStack.PushCall("CreateSampler", namedParams.str(), namedParams);
985
986   return Graphics::MakeUnique<TestGraphicsSampler>(mGl, samplerCreateInfo);
987 }
988
989 Graphics::UniquePtr<Graphics::RenderTarget> TestGraphicsController::CreateRenderTarget(const Graphics::RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<Graphics::RenderTarget>&& oldRenderTarget)
990 {
991   mCallStack.PushCall("CreateRenderTarget", "");
992   return Graphics::MakeUnique<TestGraphicsRenderTarget>(mGl, renderTargetCreateInfo);
993 }
994
995 Graphics::UniquePtr<Graphics::Memory> TestGraphicsController::MapBufferRange(const Graphics::MapBufferInfo& mapInfo)
996 {
997   mCallStack.PushCall("MapBufferRange", "");
998
999   auto buffer = static_cast<TestGraphicsBuffer*>(mapInfo.buffer);
1000   buffer->memory.resize(mapInfo.offset + mapInfo.size); // For initial testing, allow writes past capacity
1001
1002   return std::make_unique<TestGraphicsMemory>(mCallStack, *buffer, mapInfo.offset, mapInfo.size);
1003 }
1004
1005 Graphics::UniquePtr<Graphics::Memory> TestGraphicsController::MapTextureRange(const Graphics::MapTextureInfo& mapInfo)
1006 {
1007   mCallStack.PushCall("MapTextureRange", "");
1008   return nullptr;
1009 }
1010
1011 void TestGraphicsController::UnmapMemory(Graphics::UniquePtr<Graphics::Memory> memory)
1012 {
1013   mCallStack.PushCall("UnmapMemory", "");
1014 }
1015
1016 Graphics::MemoryRequirements TestGraphicsController::GetTextureMemoryRequirements(Graphics::Texture& texture) const
1017 {
1018   mCallStack.PushCall("GetTextureMemoryRequirements", "");
1019   return Graphics::MemoryRequirements{};
1020 }
1021
1022 Graphics::MemoryRequirements TestGraphicsController::GetBufferMemoryRequirements(Graphics::Buffer& buffer) const
1023 {
1024   mCallStack.PushCall("GetBufferMemoryRequirements", "");
1025   return Graphics::MemoryRequirements{};
1026 }
1027
1028 const Graphics::TextureProperties& TestGraphicsController::GetTextureProperties(const Graphics::Texture& texture)
1029 {
1030   static Graphics::TextureProperties textureProperties{};
1031   mCallStack.PushCall("GetTextureProperties", "");
1032
1033   return textureProperties;
1034 }
1035
1036 const Graphics::Reflection& TestGraphicsController::GetProgramReflection(const Graphics::Program& program)
1037 {
1038   mCallStack.PushCall("GetProgramReflection", "");
1039
1040   return static_cast<const TestGraphicsProgram*>(&program)->GetReflection();
1041 }
1042
1043 bool TestGraphicsController::PipelineEquals(const Graphics::Pipeline& pipeline0, const Graphics::Pipeline& pipeline1) const
1044 {
1045   mCallStack.PushCall("PipelineEquals", "");
1046   return false;
1047 }
1048
1049 bool TestGraphicsController::GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData)
1050 {
1051   mCallStack.PushCall("GetProgramParameter", "");
1052   auto graphicsProgram = Uncast<TestGraphicsProgram>(&program);
1053   return graphicsProgram->GetParameter(parameterId, outData);
1054 }
1055
1056 } // namespace Dali