1 #ifndef DALI_EGL_GRAPHICS_CONTROLLER_H
2 #define DALI_EGL_GRAPHICS_CONTROLLER_H
5 * Copyright (c) 2021 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.
21 #include <dali/graphics-api/graphics-controller.h>
25 #include "gles-context.h"
26 #include "gles-graphics-buffer.h"
27 #include "gles-graphics-command-buffer.h"
28 #include "gles-graphics-framebuffer.h"
29 #include "gles-graphics-pipeline-cache.h"
30 #include "gles-graphics-pipeline.h"
31 #include "gles-graphics-reflection.h"
32 #include "gles-graphics-sampler.h"
33 #include "gles-graphics-shader.h"
34 #include "gles-graphics-texture.h"
35 #include "gles-graphics-types.h"
36 #include "gles2-graphics-memory.h"
43 class GlSyncAbstraction;
44 class GlContextHelperAbstraction;
45 } // namespace Integration
56 * EGL Implementation of the graphics controller.
58 * Temporarily holds the old GL abstractions whilst dali-core is migrated to the new API.
60 DALI_IMPORT_API class EglGraphicsController : public Graphics::Controller
64 * @brief Deault constructor
66 EglGraphicsController() = default;
71 ~EglGraphicsController() override;
74 * Initialize the GLES abstraction. This can be called from the main thread.
76 void InitializeGLES(Integration::GlAbstraction& glAbstraction);
79 * Initialize with a reference to the GL abstractions.
81 * Note, this is now executed in the render thread, after core initialization
83 void Initialize(Integration::GlSyncAbstraction& glSyncAbstraction,
84 Integration::GlContextHelperAbstraction& glContextHelperAbstraction);
86 Integration::GlAbstraction& GetGlAbstraction() override;
87 Integration::GlSyncAbstraction& GetGlSyncAbstraction() override;
88 Integration::GlContextHelperAbstraction& GetGlContextHelperAbstraction() override;
91 * @copydoc Dali::Graphics::SubmitCommandBuffers()
93 void SubmitCommandBuffers(const SubmitInfo& submitInfo) override;
96 * @copydoc Dali::Graphics::PresentRenderTarget()
98 void PresentRenderTarget(RenderTarget* renderTarget) override
103 * @copydoc Dali::Graphics::WaitIdle()
105 void WaitIdle() override
110 * @copydoc Dali::Graphics::Pause()
112 void Pause() override
117 * @copydoc Dali::Graphics::Resume()
119 void Resume() override
124 * @copydoc Dali::Graphics::Shutdown()
126 void Shutdown() override
128 mIsShuttingDown = true;
132 * @copydoc Dali::Graphics::Destroy()
134 void Destroy() override
141 * @copydoc Dali::Graphics::UpdateTextures()
143 void UpdateTextures(const std::vector<TextureUpdateInfo>& updateInfoList,
144 const std::vector<TextureUpdateSourceInfo>& sourceList) override;
147 * @copydoc Dali::Graphics::EnableDepthStencilBuffer()
149 bool EnableDepthStencilBuffer(bool enableDepth, bool enableStencil) override
155 * @copydoc Dali::Graphics::RunGarbageCollector()
157 void RunGarbageCollector(size_t numberOfDiscardedRenderers) override
162 * @copydoc Dali::Graphics::DiscardUnusedResources()
164 void DiscardUnusedResources() override
169 * @copydoc Dali::Graphics::IsDiscardQueueEmpty()
171 bool IsDiscardQueueEmpty() override
177 * @copydoc Dali::Graphics::IsDrawOnResumeRequired()
179 bool IsDrawOnResumeRequired() override
185 * @copydoc Dali::Graphics::CreateBuffer()
187 Graphics::UniquePtr<Buffer> CreateBuffer(const BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Buffer>&& oldBuffer) override;
190 * @copydoc Dali::Graphics::CreateCommandBuffer()
192 Graphics::UniquePtr<CommandBuffer> CreateCommandBuffer(const CommandBufferCreateInfo& commandBufferCreateInfo, Graphics::UniquePtr<CommandBuffer>&& oldCommandBuffer) override;
195 * @copydoc Dali::Graphics::CreateRenderPass()
197 Graphics::UniquePtr<RenderPass> CreateRenderPass(const RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<RenderPass>&& oldRenderPass) override
203 * @copydoc Dali::Graphics::CreateTexture()
205 Graphics::UniquePtr<Texture> CreateTexture(const TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Texture>&& oldTexture) override;
208 * @copydoc Dali::Graphics::CreateFramebuffer()
210 Graphics::UniquePtr<Framebuffer> CreateFramebuffer(const FramebufferCreateInfo& framebufferCreateInfo, Graphics::UniquePtr<Framebuffer>&& oldFramebuffer) override;
213 * @copydoc Dali::Graphics::CreatePipeline()
215 Graphics::UniquePtr<Pipeline> CreatePipeline(const PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Pipeline>&& oldPipeline) override;
218 * @copydoc Dali::Graphics::CreateProgram()
220 Graphics::UniquePtr<Program> CreateProgram(const ProgramCreateInfo& programCreateInfo, UniquePtr<Program>&& oldProgram) override;
223 * @copydoc Dali::Graphics::CreateShader()
225 Graphics::UniquePtr<Shader> CreateShader(const ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Shader>&& oldShader) override;
228 * @copydoc Dali::Graphics::CreateSampler()
230 Graphics::UniquePtr<Sampler> CreateSampler(const SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Sampler>&& oldSampler) override;
233 * @copydoc Dali::Graphics::CreateRenderTarget()
235 Graphics::UniquePtr<RenderTarget> CreateRenderTarget(const RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<RenderTarget>&& oldRenderTarget) override
241 * @copydoc Dali::Graphics::MapBufferRange()
243 Graphics::UniquePtr<Memory> MapBufferRange(const MapBufferInfo& mapInfo) override;
246 * @copydoc Dali::Graphics::MapTextureRange()
248 Graphics::UniquePtr<Memory> MapTextureRange(const MapTextureInfo& mapInfo) override
254 * @copydoc Dali::Graphics::UnmapMemory()
256 void UnmapMemory(Graphics::UniquePtr<Memory> memory) override
260 * @copydoc Dali::Graphics::GetTextureMemoryRequirements()
262 MemoryRequirements GetTextureMemoryRequirements(Texture& texture) const override
268 * @copydoc Dali::Graphics::GetBufferMemoryRequirements()
270 MemoryRequirements GetBufferMemoryRequirements(Buffer& buffer) const override
276 * @copydoc Dali::Graphics::GetTextureProperties()
278 const TextureProperties& GetTextureProperties(const Texture& texture) override
280 // for compiler not to moan
281 static TextureProperties dummy{};
286 * @copydoc Dali::Graphics::Controller::GetPipelineReflection()
289 [[nodiscard]] const Reflection& GetProgramReflection(const Graphics::Program& program) override;
292 * @copydoc Dali::Graphics::PipelineEquals()
294 [[nodiscard]] bool PipelineEquals(const Pipeline& pipeline0, const Pipeline& pipeline1) const override
299 [[nodiscard]] Integration::GlAbstraction* GetGL() const
305 return mGlAbstraction;
309 void AddTexture(GLES::Texture& texture);
312 * @brief Adds buffer to the creation queue
315 void AddBuffer(GLES::Buffer& buffer);
318 * @brief Adds framebuffer to the creation queue
321 void AddFramebuffer(GLES::Framebuffer& framebuffer);
324 * @brief Pushes Bufer to the discard queue
326 * Function is called from the UniquePtr custom deleter.
328 * @param[in] texture Pointer to the texture
330 void DiscardResource(GLES::Texture* texture)
332 mDiscardTextureQueue.push(texture);
336 * @brief Pushes Buffer to the discard queue
338 * Function is called from the UniquePtr custom deleter.
340 * @param[in] buffer Pointer to the buffer object
342 void DiscardResource(GLES::Buffer* buffer)
344 mDiscardBufferQueue.push(buffer);
348 * @brief Pushes framebuffer to the discard queue
350 * Function is called from the UniquePtr custom deleter.
352 * @param[in] framebuffer Pointer to the framebuffer object
354 void DiscardResource(GLES::Framebuffer* framebuffer)
356 mDiscardFramebufferQueue.push(framebuffer);
360 * @brief Pushes Program to the discard queue
362 * Function is called from the UniquePtr custom deleter.
364 * @param[in] program Pointer to the program
366 void DiscardResource(GLES::Program* program)
368 mDiscardProgramQueue.push(program);
372 * @brief Pushes Shader to the discard queue
374 * Function is called from the UniquePtr custom deleter.
376 * @param[in] program Pointer to the Shader
378 void DiscardResource(GLES::Shader* shader)
380 mDiscardShaderQueue.push(shader);
384 * @brief Pushes CommandBuffer to the discard queue
386 * Function is called from the UniquePtr custom deleter.
388 * @param[in] program Pointer to the CommandBuffer
390 void DiscardResource(GLES::CommandBuffer* commandBuffer)
392 mDiscardCommandBufferQueue.push(commandBuffer);
396 * @brief Pushes Sampler to the discard queue
398 * Function is called from the UniquePtr custom deleter.
400 * @param[in] program Pointer to the Sampler
402 void DiscardResource(GLES::Sampler* sampler)
404 mDiscardSamplerQueue.push(sampler);
408 * @brief Pushes Pipeline to the discard queue
410 * Function is called from the UniquePtr custom deleter.
412 * @param[in] program Pointer to the pipeline
414 void DiscardResource(GLES::Pipeline* pipeline)
416 mDiscardPipelineQueue.push(pipeline);
420 * @brief Flushes all pending updates
422 * Function flushes all pending resource constructions,
423 * executes command buffers and empties discard queues.
428 ProcessCreateQueues();
431 ProcessTextureUpdateQueue();
433 // Process main command queue
434 ProcessCommandQueues();
437 ProcessDiscardQueues();
439 // Flush pipeline cache to remove unused pipelines
442 mPipelineCache->FlushCache();
446 // Test update to tick controller, usually it will run own thread
447 void ProcessDiscardQueues();
450 * @brief Processes a create queue for type specified
452 * @param[in,out] queue Reference to the create queue
455 void ProcessCreateQueue(T& queue)
457 while(!queue.empty())
459 auto* object = queue.front();
462 // Initialize texture
463 if(!object->InitializeResource())
465 // TODO: handle error
471 * @brief Processes a create queue for type specified
473 * @param[in,out] queue Reference to the create queue
475 template<class U, class T>
476 void ProcessDiscardQueue(T& queue)
478 while(!queue.empty())
480 auto* object = queue.front();
483 object->DestroyResource();
486 auto* clbk = object->GetCreateInfo().allocationCallbacks;
493 clbk->freeCallback(object, clbk->userData);
504 * @brief Processes all resource create queues
506 void ProcessCreateQueues();
509 * @brief Process command queues and buffers
511 void ProcessCommandQueues();
514 * @brief Executes all pending texture updates
516 void ProcessTextureUpdateQueue();
519 * @brief Returns program custom parameter
521 * This function can be used as a backdoor in order to retrieve
522 * certain data out of implementation
524 * @param[in] program Valid Program object
525 * @param parameterId Integer id of parameter
526 * @param outData Output data
527 * @return True if parameter retrieved
529 bool GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData) override;
532 * @brief Returns pipeline cache object
534 * @return Valid pipeline cache object
536 [[nodiscard]] GLES::PipelineCache& GetPipelineCache() const;
539 * @brief Returns runtime supported GLES version
541 * @return GLES version enum
543 GLES::GLESVersion GetGLESVersion() const
545 // TODO: return proper version but for now we can
547 return GLES::GLESVersion::GLES_20;
550 bool IsShuttingDown() const
552 return mIsShuttingDown;
556 Integration::GlAbstraction* mGlAbstraction{nullptr};
557 Integration::GlSyncAbstraction* mGlSyncAbstraction{nullptr};
558 Integration::GlContextHelperAbstraction* mGlContextHelperAbstraction{nullptr};
560 std::queue<GLES::Texture*> mCreateTextureQueue; ///< Create queue for texture resource
561 std::queue<GLES::Texture*> mDiscardTextureQueue; ///< Discard queue for texture resource
563 std::queue<GLES::Buffer*> mCreateBufferQueue; ///< Create queue for buffer resource
564 std::queue<GLES::Buffer*> mDiscardBufferQueue; ///< Discard queue for buffer resource
566 std::queue<GLES::Program*> mDiscardProgramQueue; ///< Discard queue for program resource
567 std::queue<GLES::Pipeline*> mDiscardPipelineQueue; ///< Discard queue of pipelines
568 std::queue<GLES::Shader*> mDiscardShaderQueue; ///< Discard queue of shaders
569 std::queue<GLES::Sampler*> mDiscardSamplerQueue; ///< Discard queue of samplers
570 std::queue<GLES::CommandBuffer*> mDiscardCommandBufferQueue; ///< Discard queue of command buffers
571 std::queue<GLES::Framebuffer*> mCreateFramebufferQueue; ///< Create queue for framebuffer resource
572 std::queue<GLES::Framebuffer*> mDiscardFramebufferQueue; ///< Discard queue for framebuffer resource
574 std::queue<GLES::CommandBuffer*> mCommandQueue; ///< we may have more in the future
576 using TextureUpdateRequest = std::pair<TextureUpdateInfo, TextureUpdateSourceInfo>;
577 std::queue<TextureUpdateRequest> mTextureUpdateRequests;
579 std::unique_ptr<GLES::Context> mContext{nullptr}; ///< Context object handling command buffers execution
581 std::unique_ptr<GLES::PipelineCache> mPipelineCache{nullptr}; ///< Internal pipeline cache
583 bool mIsShuttingDown{false}; ///< Indicates whether the controller is shutting down
586 } // namespace Graphics
590 #endif //DALI_EGL_GRAPHICS_CONTROLLER_H