1 #ifndef TEST_GRAPHICS_CONTROLLER_H
2 #define TEST_GRAPHICS_CONTROLLER_H
5 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 #include <dali/graphics-api/graphics-controller.h>
21 #include <unordered_map>
22 #include "test-gl-abstraction.h"
23 #include "test-gl-context-helper-abstraction.h"
24 #include "test-graphics-command-buffer.h"
25 #include "test-graphics-program.h"
26 #include "test-graphics-reflection.h"
27 #include "test-graphics-sync-impl.h"
31 std::ostream& operator<<(std::ostream& o, const Graphics::BufferCreateInfo& bufferCreateInfo);
32 std::ostream& operator<<(std::ostream& o, const Graphics::CommandBufferCreateInfo& commandBufferCreateInfo);
33 std::ostream& operator<<(std::ostream& o, const Graphics::TextureType& textureType);
34 std::ostream& operator<<(std::ostream& o, const Graphics::Extent2D extent);
35 std::ostream& operator<<(std::ostream& o, const Graphics::TextureCreateInfo& createInfo);
36 std::ostream& operator<<(std::ostream& o, Graphics::SamplerAddressMode addressMode);
37 std::ostream& operator<<(std::ostream& o, Graphics::SamplerFilter filterMode);
38 std::ostream& operator<<(std::ostream& o, Graphics::SamplerMipmapMode mipmapMode);
39 std::ostream& operator<<(std::ostream& o, const Graphics::SamplerCreateInfo& createInfo);
42 T* Uncast(const Graphics::CommandBuffer* object)
44 return const_cast<T*>(static_cast<const T*>(object));
48 T* Uncast(const Graphics::Texture* object)
50 return const_cast<T*>(static_cast<const T*>(object));
54 T* Uncast(const Graphics::Sampler* object)
56 return const_cast<T*>(static_cast<const T*>(object));
60 T* Uncast(const Graphics::Buffer* object)
62 return const_cast<T*>(static_cast<const T*>(object));
66 T* Uncast(const Graphics::Shader* object)
68 return const_cast<T*>(static_cast<const T*>(object));
72 T* Uncast(const Graphics::Framebuffer* object)
74 return const_cast<T*>(static_cast<const T*>(object));
78 T* Uncast(const Graphics::Pipeline* object)
80 return const_cast<T*>(static_cast<const T*>(object));
84 T* Uncast(const Graphics::RenderTarget* object)
86 return const_cast<T*>(static_cast<const T*>(object));
90 T* Uncast(const Graphics::SyncObject* object)
92 return const_cast<T*>(static_cast<const T*>(object));
95 class TestGraphicsController : public Dali::Graphics::Controller
98 TestGraphicsController();
100 virtual ~TestGraphicsController() = default;
107 Integration::GlAbstraction& GetGlAbstraction() override
112 Integration::GlContextHelperAbstraction& GetGlContextHelperAbstraction() override
114 return mGlContextHelperAbstraction;
117 TestGraphicsSyncImplementation& GetGraphicsSyncImpl()
119 return mGraphicsSyncImpl;
122 void SubmitCommandBuffers(const Graphics::SubmitInfo& submitInfo) override;
125 * @brief Presents render target
126 * @param renderTarget render target to present
128 void PresentRenderTarget(Graphics::RenderTarget* renderTarget) override;
131 * @brief Waits until the GPU is idle
133 void WaitIdle() override;
136 * @brief Lifecycle pause event
138 void Pause() override;
141 * @brief Lifecycle resume event
143 void Resume() override;
146 * @brief Lifecycle shutdown event
148 void Shutdown() override;
151 * @brief Lifecycle destroy event
153 void Destroy() override;
156 * @brief Executes batch update of textures
158 * This function may perform full or partial update of many textures.
159 * The data source may come from:
160 * - CPU memory (client side)
161 * - GPU memory (another Texture or Buffer)
163 * UpdateTextures() is the only way to update unmappable Texture objects.
164 * It is recommended to batch updates as it may help with optimizing
165 * memory transfers based on dependencies.
168 void UpdateTextures(const std::vector<Graphics::TextureUpdateInfo>& updateInfoList,
169 const std::vector<Graphics::TextureUpdateSourceInfo>& sourceList) override;
172 * Auto generates mipmaps for the texture
173 * @param[in] texture The texture
175 void GenerateTextureMipmaps(const Graphics::Texture& texture) override;
178 * TBD: do we need those functions in the new implementation?
180 bool EnableDepthStencilBuffer(bool enableDepth, bool enableStencil) override;
182 void RunGarbageCollector(size_t numberOfDiscardedRenderers) override;
184 void DiscardUnusedResources() override;
186 bool IsDiscardQueueEmpty() override;
189 * @brief Test if the graphics subsystem has resumed & should force a draw
191 * @return true if the graphics subsystem requires a re-draw
193 bool IsDrawOnResumeRequired() override;
196 * @brief Creates new Buffer object
198 * The Buffer object is created with underlying memory. The Buffer
199 * specification is immutable. Based on the BufferCreateInfo::usage,
200 * the memory may be client-side mappable or not.
202 * The old buffer may be passed as BufferCreateInfo::oldbuffer, however,
203 * it's up to the implementation whether the object will be reused or
204 * discarded and replaced by the new one.
206 * @param[in] bufferCreateInfo The valid BufferCreateInfo structure
207 * @return pointer to the Buffer object
209 Graphics::UniquePtr<Graphics::Buffer> CreateBuffer(const Graphics::BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Graphics::Buffer>&& oldBuffer) override;
211 void DiscardBuffer(TestGraphicsBuffer* buffer);
214 * @brief Creates new CommandBuffer object
216 * @param[in] bufferCreateInfo The valid BufferCreateInfo structure
217 * @return pointer to the CommandBuffer object
219 Graphics::UniquePtr<Graphics::CommandBuffer> CreateCommandBuffer(const Graphics::CommandBufferCreateInfo& commandBufferCreateInfo, Graphics::UniquePtr<Graphics::CommandBuffer>&& oldCommandBuffer) override;
222 * @brief Creates new RenderPass object
224 * @param[in] renderPassCreateInfo The valid RenderPassCreateInfo structure
225 * @return pointer to the RenderPass object
227 Graphics::UniquePtr<Graphics::RenderPass> CreateRenderPass(const Graphics::RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<Graphics::RenderPass>&& oldRenderPass) override;
230 * @brief Creates new Texture object
232 * @param[in] textureCreateInfo The valid TextureCreateInfo structure
233 * @return pointer to the TextureCreateInfo object
235 Graphics::UniquePtr<Graphics::Texture> CreateTexture(const Graphics::TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Graphics::Texture>&& oldTexture) override;
238 * @brief Creates new Framebuffer object
240 * @param[in] framebufferCreateInfo The valid FramebufferCreateInfo structure
241 * @return pointer to the Framebuffer object
243 Graphics::UniquePtr<Graphics::Framebuffer> CreateFramebuffer(const Graphics::FramebufferCreateInfo& framebufferCreateInfo, Graphics::UniquePtr<Graphics::Framebuffer>&& oldFramebuffer) override;
246 * @brief Creates new Pipeline object
248 * @param[in] pipelineCreateInfo The valid PipelineCreateInfo structure
249 * @return pointer to the Pipeline object
251 Graphics::UniquePtr<Graphics::Pipeline> CreatePipeline(const Graphics::PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Graphics::Pipeline>&& oldPipeline) override;
254 * @brief Creates new Program object
256 * @param[in] programCreateInfo The valid ProgramCreateInfo structure
257 * @return pointer to the Program object
259 Graphics::UniquePtr<Graphics::Program> CreateProgram(const Graphics::ProgramCreateInfo& programCreateInfo, Graphics::UniquePtr<Graphics::Program>&& oldProgram) override;
262 * @brief Creates new Shader object
264 * @param[in] shaderCreateInfo The valid ShaderCreateInfo structure
265 * @return pointer to the Shader object
267 Graphics::UniquePtr<Graphics::Shader> CreateShader(const Graphics::ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Graphics::Shader>&& oldShader) override;
270 * @brief Creates new Sampler object
272 * @param[in] samplerCreateInfo The valid SamplerCreateInfo structure
273 * @return pointer to the Sampler object
275 Graphics::UniquePtr<Graphics::Sampler> CreateSampler(const Graphics::SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Graphics::Sampler>&& oldSampler) override;
278 * @brief Creates new RenderTarget object
280 * @param[in] renderTargetCreateInfo The valid RenderTargetCreateInfo structure
281 * @return pointer to the RenderTarget object
283 Graphics::UniquePtr<Graphics::RenderTarget> CreateRenderTarget(const Graphics::RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<Graphics::RenderTarget>&& oldRenderTarget) override;
286 * @brief Creates new sync object
287 * Could add timeout etc to createinfo... but nah.
289 * @return pointer to the SyncObject
291 Graphics::UniquePtr<Graphics::SyncObject> CreateSyncObject(const Graphics::SyncObjectCreateInfo& syncObjectCreateInfo,
292 Graphics::UniquePtr<Graphics::SyncObject>&& oldSyncObject) override;
295 * @brief Maps memory associated with Buffer object
297 * @param[in] mapInfo Filled details of mapped resource
299 * @return Returns pointer to Memory object or nullptr on error
301 Graphics::UniquePtr<Graphics::Memory> MapBufferRange(const Graphics::MapBufferInfo& mapInfo) override;
304 * @brief Maps memory associated with the texture.
306 * Only Texture objects that are backed with linear memory (staging memory) can be mapped.
308 * 1) GLES implementation may create PBO object as staging memory and couple it
309 * with the texture. Texture can be mapped and the memory can be read/write on demand.
311 * 2) Vulkan implementation may allocate DeviceMemory and use linear layout.
313 * @param[in] mapInfo Filled details of mapped resource
315 * @return Valid Memory object or nullptr on error
317 Graphics::UniquePtr<Graphics::Memory> MapTextureRange(const Graphics::MapTextureInfo& mapInfo) override;
320 * @brief Unmaps memory and discards Memory object
322 * This function automatically removes lock if Memory has been
325 * @param[in] memory Valid and previously mapped Memory object
327 void UnmapMemory(Graphics::UniquePtr<Graphics::Memory> memory) override;
330 * @brief Returns memory requirements of the Texture object.
332 * Call this function whenever it's necessary to know how much memory
333 * is needed to store all the texture data and what memory alignment
334 * the data should follow.
336 * @return Returns memory requirements of Texture
338 Graphics::MemoryRequirements GetTextureMemoryRequirements(Graphics::Texture& texture) const override;
341 * @brief Returns memory requirements of the Buffer object.
343 * Call this function whenever it's necessary to know how much memory
344 * is needed to store all the buffer data and what memory alignment
345 * the data should follow.
347 * @return Returns memory requirements of Buffer
349 Graphics::MemoryRequirements GetBufferMemoryRequirements(Graphics::Buffer& buffer) const override;
352 * @brief Returns specification of the Texture object
354 * Function obtains specification of the Texture object. It may retrieve
355 * implementation dependent details like ie. whether the texture is
356 * emulated (for example, RGB emulated on RGBA), compressed etc.
358 * @return Returns the TextureProperties object
360 Graphics::TextureProperties GetTextureProperties(const Graphics::Texture& texture) override;
363 * @brief Returns the reflection of the given program
365 * @param[in] program The program
366 * @return The reflection of the program
368 const Graphics::Reflection& GetProgramReflection(const Graphics::Program& program) override;
371 * @brief Tests whether two Pipelines are the same.
373 * On the higher level, this function may help wit creating pipeline cache.
375 * @return true if pipeline objects match
377 bool PipelineEquals(const Graphics::Pipeline& pipeline0, const Graphics::Pipeline& pipeline1) const override;
380 * @brief Retrieves program parameters
382 * This function can be used to retrieve data from internal implementation
384 * @param[in] program Valid program object
385 * @param[in] parameterId Integer parameter id
386 * @param[out] outData Pointer to output memory
387 * @return True on success
389 bool GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData) override;
391 public: // ResourceId relative API.
393 * @brief Create Graphics::Texture as resourceId.
394 * The ownership of Graphics::Texture will be hold on this controller.
395 * @note If some Graphics::Texture already created before, assert.
396 * @post DiscardTextureFromResourceId() or ReleaseTextureFromResourceId() should be called when we don't use resourceId texture anymore.
398 * @param[in] resourceId The unique id of resouces.
399 * @return Pointer of Graphics::Texture, or nullptr if we fail to create.
401 Graphics::Texture* CreateTextureByResourceId(uint32_t resourceId, const Graphics::TextureCreateInfo& createInfo) override;
404 * @brief Discard Graphics::Texture as resourceId.
406 * @param[in] resourceId The unique id of resouces.
408 void DiscardTextureFromResourceId(uint32_t resourceId) override;
411 * @brief Get the Graphics::Texture as resourceId.
413 * @param[in] resourceId The unique id of resouces.
414 * @return Pointer of Graphics::Texture, or nullptr if there is no valid objects.
416 Graphics::Texture* GetTextureFromResourceId(uint32_t resourceId) override;
419 * @brief Get the ownership of Graphics::Texture as resourceId.
421 * @param[in] resourceId The unique id of resouces.
422 * @return Pointer of Graphics::Texture.
424 Graphics::UniquePtr<Graphics::Texture> ReleaseTextureFromResourceId(uint32_t resourceId) override;
426 public: // Test Functions
427 void SetVertexFormats(Property::Array& vfs)
429 mVertexFormats = vfs;
432 void AddCustomUniforms(std::vector<UniformData>& customUniforms)
434 mCustomUniforms = customUniforms;
437 void AddCustomUniformBlock(const TestGraphicsReflection::TestUniformBlockInfo& blockInfo)
439 mCustomUniformBlocks.push_back(blockInfo);
442 void ClearSubmitStack()
444 mSubmitStack.clear();
447 void ProcessCommandBuffer(TestGraphicsCommandBuffer& commandBuffer);
449 void BindPipeline(TestGraphicsPipeline* pipeline);
452 mutable TraceCallStack mCallStack;
453 mutable TraceCallStack mCommandBufferCallStack;
454 mutable TraceCallStack mFrameBufferCallStack;
455 mutable std::vector<Graphics::SubmitInfo> mSubmitStack;
457 TestGlAbstraction mGl;
458 TestGraphicsSyncImplementation mGraphicsSyncImpl;
459 TestGlContextHelperAbstraction mGlContextHelperAbstraction;
461 bool isDiscardQueueEmptyResult{true};
462 bool isDrawOnResumeRequiredResult{true};
464 Property::Array mVertexFormats;
468 std::map<Graphics::PipelineStage, std::vector<uint8_t>> shaders;
469 TestGraphicsProgramImpl* programImpl;
471 std::vector<ProgramCache> mProgramCache;
473 std::vector<TestGraphicsBuffer*> mAllocatedBuffers;
475 std::unordered_map<uint32_t, Graphics::UniquePtr<Graphics::Texture>> mTextureUploadBindMapper;
481 std::vector<UniformData> mCustomUniforms;
482 std::vector<TestGraphicsReflection::TestUniformBlockInfo> mCustomUniformBlocks;
487 #endif //TEST_GRAPHICS_CONTROLLER_H