1 #ifndef DALI_EGL_GRAPHICS_CONTROLLER_H
2 #define DALI_EGL_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.
21 #include <dali/graphics-api/graphics-controller.h>
25 #include <dali/integration-api/graphics-sync-abstraction.h>
26 #include <dali/internal/graphics/common/graphics-interface.h>
27 #include <dali/internal/graphics/gles-impl/gles-context.h>
28 #include <dali/internal/graphics/gles-impl/gles-graphics-buffer.h>
29 #include <dali/internal/graphics/gles-impl/gles-graphics-command-buffer.h>
30 #include <dali/internal/graphics/gles-impl/gles-graphics-framebuffer.h>
31 #include <dali/internal/graphics/gles-impl/gles-graphics-pipeline-cache.h>
32 #include <dali/internal/graphics/gles-impl/gles-graphics-pipeline.h>
33 #include <dali/internal/graphics/gles-impl/gles-graphics-reflection.h>
34 #include <dali/internal/graphics/gles-impl/gles-graphics-sampler.h>
35 #include <dali/internal/graphics/gles-impl/gles-graphics-shader.h>
36 #include <dali/internal/graphics/gles-impl/gles-graphics-texture.h>
37 #include <dali/internal/graphics/gles-impl/gles-graphics-types.h>
38 #include <dali/internal/graphics/gles-impl/gles-sync-pool.h>
39 #include <dali/internal/graphics/gles-impl/gles-texture-dependency-checker.h>
40 #include <dali/internal/graphics/gles-impl/gles2-graphics-memory.h>
47 class GlContextHelperAbstraction;
48 } // namespace Integration
57 class TextureDependencyChecker;
61 * EGL Implementation of the graphics controller.
63 * Temporarily holds the old GL abstractions whilst dali-core is migrated to the new API.
65 class EglGraphicsController : public Graphics::Controller
71 EglGraphicsController();
76 ~EglGraphicsController() override;
79 * Initialize the GLES abstraction. This can be called from the main thread.
81 void InitializeGLES(Integration::GlAbstraction& glAbstraction);
84 * Initialize with a reference to the GL abstractions.
86 * Note, this is now executed in the render thread, after core initialization
88 void Initialize(Integration::GraphicsSyncAbstraction& syncImplementation,
89 Integration::GlContextHelperAbstraction& glContextHelperAbstraction,
90 Internal::Adaptor::GraphicsInterface& graphicsInterface);
92 Integration::GlAbstraction& GetGlAbstraction() override;
93 Integration::GlContextHelperAbstraction& GetGlContextHelperAbstraction() override;
94 Internal::Adaptor::EglSyncImplementation& GetEglSyncImplementation();
97 * Mark the start of the frame.
99 * Note, this is used for logging & debugging, so is not part of the main Graphics API.
104 * @copydoc Dali::Graphics::SubmitCommandBuffers()
106 void SubmitCommandBuffers(const SubmitInfo& submitInfo) override;
109 * @copydoc Dali::Graphics::PresentRenderTarget()
111 void PresentRenderTarget(RenderTarget* renderTarget) override;
114 * @copydoc Dali::Graphics::WaitIdle()
116 void WaitIdle() override;
119 * @copydoc Dali::Graphics::Pause()
121 void Pause() override
126 * @copydoc Dali::Graphics::Resume()
128 void Resume() override
133 * @copydoc Dali::Graphics::Shutdown()
135 void Shutdown() override
137 mIsShuttingDown = true;
144 mContext->GlContextDestroyed();
147 for(auto&& context : mSurfaceContexts)
151 context.second->GlContextDestroyed();
157 * @copydoc Dali::Graphics::Destroy()
159 void Destroy() override
164 * @copydoc Dali::Graphics::UpdateTextures()
166 void UpdateTextures(const std::vector<TextureUpdateInfo>& updateInfoList,
167 const std::vector<TextureUpdateSourceInfo>& sourceList) override;
170 * @copydoc Dali::Graphics::GenerateTextureMipmaps()
172 void GenerateTextureMipmaps(const Texture& texture) override;
175 * @copydoc Dali::Graphics::EnableDepthStencilBuffer()
177 bool EnableDepthStencilBuffer(bool enableDepth, bool enableStencil) override
183 * @copydoc Dali::Graphics::RunGarbageCollector()
185 void RunGarbageCollector(size_t numberOfDiscardedRenderers) override
190 * @copydoc Dali::Graphics::DiscardUnusedResources()
192 void DiscardUnusedResources() override
197 * @copydoc Dali::Graphics::IsDiscardQueueEmpty()
199 bool IsDiscardQueueEmpty() override
205 * @copydoc Dali::Graphics::IsDrawOnResumeRequired()
207 bool IsDrawOnResumeRequired() override
213 * @copydoc Dali::Graphics::CreateBuffer()
215 Graphics::UniquePtr<Buffer> CreateBuffer(const BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Buffer>&& oldBuffer) override;
218 * @copydoc Dali::Graphics::CreateCommandBuffer()
220 Graphics::UniquePtr<CommandBuffer> CreateCommandBuffer(const CommandBufferCreateInfo& commandBufferCreateInfo, Graphics::UniquePtr<CommandBuffer>&& oldCommandBuffer) override;
223 * @copydoc Dali::Graphics::CreateRenderPass()
225 Graphics::UniquePtr<RenderPass> CreateRenderPass(const RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<RenderPass>&& oldRenderPass) override;
228 * @copydoc Dali::Graphics::CreateTexture()
230 Graphics::UniquePtr<Texture> CreateTexture(const TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Texture>&& oldTexture) override;
233 * @copydoc Dali::Graphics::CreateFramebuffer()
235 Graphics::UniquePtr<Framebuffer> CreateFramebuffer(const FramebufferCreateInfo& framebufferCreateInfo, Graphics::UniquePtr<Framebuffer>&& oldFramebuffer) override;
238 * @copydoc Dali::Graphics::CreatePipeline()
240 Graphics::UniquePtr<Pipeline> CreatePipeline(const PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Pipeline>&& oldPipeline) override;
243 * @copydoc Dali::Graphics::CreateProgram()
245 Graphics::UniquePtr<Program> CreateProgram(const ProgramCreateInfo& programCreateInfo, UniquePtr<Program>&& oldProgram) override;
248 * @copydoc Dali::Graphics::CreateShader()
250 Graphics::UniquePtr<Shader> CreateShader(const ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Shader>&& oldShader) override;
253 * @copydoc Dali::Graphics::CreateSampler()
255 Graphics::UniquePtr<Sampler> CreateSampler(const SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Sampler>&& oldSampler) override;
258 * @copydoc Dali::Graphics::CreateRenderTarget()
260 Graphics::UniquePtr<RenderTarget> CreateRenderTarget(const RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<RenderTarget>&& oldRenderTarget) override;
263 * @copydoc Dali::Graphics::CreateSyncObject()
265 Graphics::UniquePtr<SyncObject> CreateSyncObject(const SyncObjectCreateInfo& syncObjectCreateInfo,
266 Graphics::UniquePtr<SyncObject>&& oldSyncObject) override;
269 * @copydoc Dali::Graphics::MapBufferRange()
271 Graphics::UniquePtr<Memory> MapBufferRange(const MapBufferInfo& mapInfo) override;
274 * @copydoc Dali::Graphics::MapTextureRange()
276 Graphics::UniquePtr<Memory> MapTextureRange(const MapTextureInfo& mapInfo) override
282 * @copydoc Dali::Graphics::UnmapMemory()
284 void UnmapMemory(Graphics::UniquePtr<Memory> memory) override
288 * @copydoc Dali::Graphics::GetTextureMemoryRequirements()
290 MemoryRequirements GetTextureMemoryRequirements(Texture& texture) const override
296 * @copydoc Dali::Graphics::GetBufferMemoryRequirements()
298 MemoryRequirements GetBufferMemoryRequirements(Buffer& buffer) const override
304 * @copydoc Dali::Graphics::GetTextureProperties()
306 TextureProperties GetTextureProperties(const Texture& texture) override;
309 * @copydoc Dali::Graphics::Controller::GetPipelineReflection()
311 [[nodiscard]] const Reflection& GetProgramReflection(const Graphics::Program& program) override;
314 * @copydoc Dali::Graphics::PipelineEquals()
316 [[nodiscard]] bool PipelineEquals(const Pipeline& pipeline0, const Pipeline& pipeline1) const override
321 [[nodiscard]] Integration::GlAbstraction* GetGL() const
327 return mGlAbstraction;
330 [[nodiscard]] Internal::Adaptor::GraphicsInterface* GetGraphicsInterface() const
336 void AddTexture(GLES::Texture& texture);
339 * @brief Adds buffer to the creation queue
342 void AddBuffer(GLES::Buffer& buffer);
345 * @brief Adds framebuffer to the creation queue
348 void AddFramebuffer(GLES::Framebuffer& framebuffer);
351 * @brief Pushes Bufer to the discard queue
353 * Function is called from the UniquePtr custom deleter.
355 * @param[in] texture Pointer to the texture
357 void DiscardResource(GLES::Texture* texture)
359 mDiscardTextureQueue.push(texture);
363 * @brief Pushes Buffer to the discard queue
365 * Function is called from the UniquePtr custom deleter.
367 * @param[in] buffer Pointer to the buffer object
369 void DiscardResource(GLES::Buffer* buffer)
371 mDiscardBufferQueue.push(buffer);
375 * @brief Pushes framebuffer to the discard queue
377 * Function is called from the UniquePtr custom deleter.
379 * @param[in] framebuffer Pointer to the framebuffer object
381 void DiscardResource(GLES::Framebuffer* framebuffer)
383 mDiscardFramebufferQueue.push(framebuffer);
387 * @brief Pushes Program to the discard queue
389 * Function is called from the UniquePtr custom deleter.
391 * @param[in] program Pointer to the program
393 void DiscardResource(GLES::Program* program)
395 mDiscardProgramQueue.push(program);
399 * @brief Pushes RenderPass to the discard queue
401 * Function is called from the UniquePtr custom deleter.
403 * @param[in] program Pointer to the RenderPass
405 void DiscardResource(GLES::RenderPass* renderPass)
407 mDiscardRenderPassQueue.push(renderPass);
411 * @brief Pushes RenderTarget to the discard queue
413 * Function is called from the UniquePtr custom deleter.
415 * @param[in] program Pointer to the RenderTarget
417 void DiscardResource(GLES::RenderTarget* renderTarget)
419 mDiscardRenderTargetQueue.push(renderTarget);
423 * @brief Pushes Shader to the discard queue
425 * Function is called from the UniquePtr custom deleter.
427 * @param[in] program Pointer to the Shader
429 void DiscardResource(GLES::Shader* shader)
431 mDiscardShaderQueue.push(shader);
435 * @brief Pushes CommandBuffer to the discard queue
437 * Function is called from the UniquePtr custom deleter.
439 * @param[in] program Pointer to the CommandBuffer
441 void DiscardResource(GLES::CommandBuffer* commandBuffer)
443 mDiscardCommandBufferQueue.push(commandBuffer);
447 * @brief Pushes Sampler to the discard queue
449 * Function is called from the UniquePtr custom deleter.
451 * @param[in] program Pointer to the Sampler
453 void DiscardResource(GLES::Sampler* sampler)
455 mDiscardSamplerQueue.push(sampler);
459 * @brief Pushes Pipeline to the discard queue
461 * Function is called from the UniquePtr custom deleter.
463 * @param[in] program Pointer to the pipeline
465 void DiscardResource(GLES::Pipeline* pipeline)
467 mDiscardPipelineQueue.push(pipeline);
471 * @brief Flushes all pending updates
473 * Function flushes all pending resource constructions,
474 * executes command buffers and empties discard queues.
478 if(!mCreateTextureQueue.empty() ||
479 !mCreateBufferQueue.empty() ||
480 !mCreateFramebufferQueue.empty() ||
481 !mTextureUpdateRequests.empty() ||
482 !mTextureMipmapGenerationRequests.empty())
484 mGraphics->ActivateResourceContext();
488 ProcessCreateQueues();
491 ProcessTextureUpdateQueue();
493 // Process texture mipmap generation requests
494 ProcessTextureMipmapGenerationQueue();
496 // Process main command queue
497 ProcessCommandQueues();
499 // Reset texture cache in the contexts while destroying textures
502 // Reset buffer cache in the contexts while destroying buffers
506 ProcessDiscardQueues();
508 // Flush pipeline cache to remove unused pipelines
511 mPipelineCache->FlushCache();
515 // Test update to tick controller, usually it will run own thread
516 void ProcessDiscardQueues();
519 * @brief Processes a create queue for type specified
521 * @param[in,out] queue Reference to the create queue
524 void ProcessCreateQueue(T& queue)
526 while(!queue.empty())
528 auto* object = queue.front();
531 // Initialize texture
532 if(!object->InitializeResource())
534 // TODO: handle error
540 * @brief Processes a discard queue for type specified
542 * @param[in,out] queue Reference to the discard queue
544 template<class U, class T>
545 void ProcessDiscardQueue(T& queue)
547 while(!queue.empty())
549 auto* object = const_cast<U*>(queue.front());
552 object->DestroyResource();
555 auto* clbk = object->GetCreateInfo().allocationCallbacks;
562 clbk->freeCallback(object, clbk->userData);
573 * @brief Processes a discard queue for pipeline
575 * @param[in,out] queue Reference to the create queue
577 void ProcessDiscardQueue(std::queue<GLES::Pipeline*>& queue)
579 while(!queue.empty())
581 auto* object = const_cast<GLES::Pipeline*>(queue.front());
583 // Inform the contexts to invalidate the pipeline if cached
586 mContext->InvalidateCachedPipeline(object);
589 for(auto&& context : mSurfaceContexts)
593 context.second->InvalidateCachedPipeline(object);
598 object->DestroyResource();
601 auto* clbk = object->GetCreateInfo().allocationCallbacks;
605 using GLESPipeline = GLES::Pipeline;
606 object->~GLESPipeline();
609 clbk->freeCallback(object, clbk->userData);
620 * @brief Processes all resource create queues
622 void ProcessCreateQueues();
625 * @brief Process command queues and buffers
627 void ProcessCommandQueues();
630 * @brief Executes all pending texture updates
632 void ProcessTextureUpdateQueue();
635 * @brief Executes all pending texture mipmap generation
637 void ProcessTextureMipmapGenerationQueue();
640 * @brief Returns program custom parameter
642 * This function can be used as a backdoor in order to retrieve
643 * certain data out of implementation
645 * @param[in] program Valid Program object
646 * @param parameterId Integer id of parameter
647 * @param outData Output data
648 * @return True if parameter retrieved
650 bool GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData) override;
653 * @brief Returns pipeline cache object
655 * @return Valid pipeline cache object
657 [[nodiscard]] GLES::PipelineCache& GetPipelineCache() const;
660 * @brief Returns runtime supported GLES version
662 * @return GLES version enum
664 GLES::GLESVersion GetGLESVersion() const
670 * @brief Sets runtime supported GLES version
672 * @param[in] glesVersion The runtime supported GLES version
674 void SetGLESVersion(GLES::GLESVersion glesVersion)
676 mGLESVersion = glesVersion;
679 bool IsShuttingDown() const
681 return mIsShuttingDown;
685 * @brief Reset texture cache in the contexts
687 void ResetTextureCache()
691 mContext->GetGLStateCache().ResetTextureCache();
694 for(auto& context : mSurfaceContexts)
698 context.second->GetGLStateCache().ResetTextureCache();
704 * @brief Reset buffer cache in the contexts
706 void ResetBufferCache()
710 mContext->GetGLStateCache().ResetBufferCache();
713 for(auto& context : mSurfaceContexts)
717 context.second->GetGLStateCache().ResetBufferCache();
722 void ProcessCommandBuffer(const GLES::CommandBuffer& commandBuffer);
724 // Resolves presentation
725 void ResolvePresentRenderTarget(GLES::RenderTarget* renderTarget);
728 * Invoked after all rendering has finished. Used to clean up sync resources
733 * Creates a GLES context for the given render surface
735 * @param[in] surface The surface whose GLES context to be created.
737 void CreateSurfaceContext(Dali::RenderSurfaceInterface* surface);
740 * Deletes a GLES context
742 * @param[in] surface The surface whose GLES context to be deleted.
744 void DeleteSurfaceContext(Dali::RenderSurfaceInterface* surface);
747 * Activate the resource context (shared surfaceless context)
749 void ActivateResourceContext();
752 * Activate the surface context
754 * @param[in] surface The surface whose context to be switched to.
756 void ActivateSurfaceContext(Dali::RenderSurfaceInterface* surface);
759 * @brief Returns the current context
761 * @return the current context
763 GLES::Context* GetCurrentContext() const
765 return mCurrentContext;
769 * @brief Returns EGL shared context
771 * @return valid EGL shared context
773 void* GetSharedContext() const
775 return mSharedContext;
778 GLES::SyncPool& GetSyncPool()
783 std::size_t GetCapacity() const
789 Integration::GlAbstraction* mGlAbstraction{nullptr};
790 Integration::GlContextHelperAbstraction* mGlContextHelperAbstraction{nullptr};
792 Internal::Adaptor::EglSyncImplementation* mEglSyncImplementation{nullptr};
793 Internal::Adaptor::GraphicsInterface* mGraphics{nullptr}; // Pointer to owning structure via interface.
795 std::queue<GLES::Texture*> mCreateTextureQueue; ///< Create queue for texture resource
796 std::queue<GLES::Texture*> mDiscardTextureQueue; ///< Discard queue for texture resource
798 std::queue<GLES::Buffer*> mCreateBufferQueue; ///< Create queue for buffer resource
799 std::queue<GLES::Buffer*> mDiscardBufferQueue; ///< Discard queue for buffer resource
801 std::queue<GLES::Program*> mDiscardProgramQueue; ///< Discard queue for program resource
802 std::queue<GLES::Pipeline*> mDiscardPipelineQueue; ///< Discard queue of pipelines
803 std::queue<GLES::RenderPass*> mDiscardRenderPassQueue; ///< Discard queue for renderpass resource
804 std::queue<GLES::RenderTarget*> mDiscardRenderTargetQueue; ///< Discard queue for rendertarget resource
805 std::queue<GLES::Shader*> mDiscardShaderQueue; ///< Discard queue of shaders
806 std::queue<GLES::Sampler*> mDiscardSamplerQueue; ///< Discard queue of samplers
807 std::queue<const GLES::CommandBuffer*> mDiscardCommandBufferQueue; ///< Discard queue of command buffers
808 std::queue<GLES::Framebuffer*> mCreateFramebufferQueue; ///< Create queue for framebuffer resource
809 std::queue<GLES::Framebuffer*> mDiscardFramebufferQueue; ///< Discard queue for framebuffer resource
811 std::queue<GLES::CommandBuffer*> mCommandQueue; ///< we may have more in the future
813 using TextureUpdateRequest = std::pair<TextureUpdateInfo, TextureUpdateSourceInfo>;
814 std::queue<TextureUpdateRequest> mTextureUpdateRequests;
816 std::queue<const GLES::Texture*> mTextureMipmapGenerationRequests; ///< Queue for texture mipmap generation requests
818 GLES::Context* mCurrentContext{nullptr}; ///< The current context
819 std::unique_ptr<GLES::Context> mContext{nullptr}; ///< Context object handling command buffers execution
820 using SurfaceContextPair = std::pair<Dali::RenderSurfaceInterface*, std::unique_ptr<GLES::Context>>;
821 std::vector<SurfaceContextPair> mSurfaceContexts; ///< Vector of surface context objects handling command buffers execution
823 std::unique_ptr<GLES::PipelineCache> mPipelineCache{nullptr}; ///< Internal pipeline cache
825 GLES::GLESVersion mGLESVersion{GLES::GLESVersion::GLES_20}; ///< Runtime supported GLES version
826 uint32_t mTextureUploadTotalCPUMemoryUsed{0u};
828 bool mIsShuttingDown{false}; ///< Indicates whether the controller is shutting down
830 std::queue<const GLES::CommandBuffer*> mPresentationCommandBuffers{}; ///< Queue of reusable command buffers used by presentation engine
832 void* mSharedContext{nullptr}; ///< Shared EGL context
834 GLES::TextureDependencyChecker mTextureDependencyChecker; // Checks if FBO textures need syncing
835 GLES::SyncPool mSyncPool;
836 std::size_t mCapacity{0u}; ///< Memory Usage (of command buffers)
839 } // namespace Graphics
843 #endif // DALI_EGL_GRAPHICS_CONTROLLER_H