1 #ifndef DALI_EGL_GRAPHICS_CONTROLLER_H
2 #define DALI_EGL_GRAPHICS_CONTROLLER_H
5 * Copyright (c) 2022 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 * @copydoc Dali::Graphics::SubmitCommandBuffers()
99 void SubmitCommandBuffers(const SubmitInfo& submitInfo) override;
102 * @copydoc Dali::Graphics::PresentRenderTarget()
104 void PresentRenderTarget(RenderTarget* renderTarget) override;
107 * @copydoc Dali::Graphics::WaitIdle()
109 void WaitIdle() override
114 * @copydoc Dali::Graphics::Pause()
116 void Pause() override
121 * @copydoc Dali::Graphics::Resume()
123 void Resume() override
128 * @copydoc Dali::Graphics::Shutdown()
130 void Shutdown() override
132 mIsShuttingDown = true;
139 mContext->GlContextDestroyed();
142 for(auto&& context : mSurfaceContexts)
146 context.second->GlContextDestroyed();
152 * @copydoc Dali::Graphics::Destroy()
154 void Destroy() override
159 * @copydoc Dali::Graphics::UpdateTextures()
161 void UpdateTextures(const std::vector<TextureUpdateInfo>& updateInfoList,
162 const std::vector<TextureUpdateSourceInfo>& sourceList) override;
165 * @copydoc Dali::Graphics::GenerateTextureMipmaps()
167 void GenerateTextureMipmaps(const Texture& texture) override;
170 * @copydoc Dali::Graphics::EnableDepthStencilBuffer()
172 bool EnableDepthStencilBuffer(bool enableDepth, bool enableStencil) override
178 * @copydoc Dali::Graphics::RunGarbageCollector()
180 void RunGarbageCollector(size_t numberOfDiscardedRenderers) override
185 * @copydoc Dali::Graphics::DiscardUnusedResources()
187 void DiscardUnusedResources() override
192 * @copydoc Dali::Graphics::IsDiscardQueueEmpty()
194 bool IsDiscardQueueEmpty() override
200 * @copydoc Dali::Graphics::IsDrawOnResumeRequired()
202 bool IsDrawOnResumeRequired() override
208 * @copydoc Dali::Graphics::CreateBuffer()
210 Graphics::UniquePtr<Buffer> CreateBuffer(const BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Buffer>&& oldBuffer) override;
213 * @copydoc Dali::Graphics::CreateCommandBuffer()
215 Graphics::UniquePtr<CommandBuffer> CreateCommandBuffer(const CommandBufferCreateInfo& commandBufferCreateInfo, Graphics::UniquePtr<CommandBuffer>&& oldCommandBuffer) override;
218 * @copydoc Dali::Graphics::CreateRenderPass()
220 Graphics::UniquePtr<RenderPass> CreateRenderPass(const RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<RenderPass>&& oldRenderPass) override;
223 * @copydoc Dali::Graphics::CreateTexture()
225 Graphics::UniquePtr<Texture> CreateTexture(const TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Texture>&& oldTexture) override;
228 * @copydoc Dali::Graphics::CreateFramebuffer()
230 Graphics::UniquePtr<Framebuffer> CreateFramebuffer(const FramebufferCreateInfo& framebufferCreateInfo, Graphics::UniquePtr<Framebuffer>&& oldFramebuffer) override;
233 * @copydoc Dali::Graphics::CreatePipeline()
235 Graphics::UniquePtr<Pipeline> CreatePipeline(const PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Pipeline>&& oldPipeline) override;
238 * @copydoc Dali::Graphics::CreateProgram()
240 Graphics::UniquePtr<Program> CreateProgram(const ProgramCreateInfo& programCreateInfo, UniquePtr<Program>&& oldProgram) override;
243 * @copydoc Dali::Graphics::CreateShader()
245 Graphics::UniquePtr<Shader> CreateShader(const ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Shader>&& oldShader) override;
248 * @copydoc Dali::Graphics::CreateSampler()
250 Graphics::UniquePtr<Sampler> CreateSampler(const SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Sampler>&& oldSampler) override;
253 * @copydoc Dali::Graphics::CreateRenderTarget()
255 Graphics::UniquePtr<RenderTarget> CreateRenderTarget(const RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<RenderTarget>&& oldRenderTarget) override;
258 * @copydoc Dali::Graphics::CreateSyncObject()
260 Graphics::UniquePtr<SyncObject> CreateSyncObject(const SyncObjectCreateInfo& syncObjectCreateInfo,
261 Graphics::UniquePtr<SyncObject>&& oldSyncObject) override;
264 * @copydoc Dali::Graphics::MapBufferRange()
266 Graphics::UniquePtr<Memory> MapBufferRange(const MapBufferInfo& mapInfo) override;
269 * @copydoc Dali::Graphics::MapTextureRange()
271 Graphics::UniquePtr<Memory> MapTextureRange(const MapTextureInfo& mapInfo) override
277 * @copydoc Dali::Graphics::UnmapMemory()
279 void UnmapMemory(Graphics::UniquePtr<Memory> memory) override
283 * @copydoc Dali::Graphics::GetTextureMemoryRequirements()
285 MemoryRequirements GetTextureMemoryRequirements(Texture& texture) const override
291 * @copydoc Dali::Graphics::GetBufferMemoryRequirements()
293 MemoryRequirements GetBufferMemoryRequirements(Buffer& buffer) const override
299 * @copydoc Dali::Graphics::GetTextureProperties()
301 TextureProperties GetTextureProperties(const Texture& texture) override;
304 * @copydoc Dali::Graphics::Controller::GetPipelineReflection()
306 [[nodiscard]] const Reflection& GetProgramReflection(const Graphics::Program& program) override;
309 * @copydoc Dali::Graphics::PipelineEquals()
311 [[nodiscard]] bool PipelineEquals(const Pipeline& pipeline0, const Pipeline& pipeline1) const override
316 [[nodiscard]] Integration::GlAbstraction* GetGL() const
322 return mGlAbstraction;
325 [[nodiscard]] Internal::Adaptor::GraphicsInterface* GetGraphicsInterface() const
331 void AddTexture(GLES::Texture& texture);
334 * @brief Adds buffer to the creation queue
337 void AddBuffer(GLES::Buffer& buffer);
340 * @brief Adds framebuffer to the creation queue
343 void AddFramebuffer(GLES::Framebuffer& framebuffer);
346 * @brief Pushes Bufer to the discard queue
348 * Function is called from the UniquePtr custom deleter.
350 * @param[in] texture Pointer to the texture
352 void DiscardResource(GLES::Texture* texture)
354 mDiscardTextureQueue.push(texture);
358 * @brief Pushes Buffer to the discard queue
360 * Function is called from the UniquePtr custom deleter.
362 * @param[in] buffer Pointer to the buffer object
364 void DiscardResource(GLES::Buffer* buffer)
366 mDiscardBufferQueue.push(buffer);
370 * @brief Pushes framebuffer to the discard queue
372 * Function is called from the UniquePtr custom deleter.
374 * @param[in] framebuffer Pointer to the framebuffer object
376 void DiscardResource(GLES::Framebuffer* framebuffer)
378 mDiscardFramebufferQueue.push(framebuffer);
382 * @brief Pushes Program to the discard queue
384 * Function is called from the UniquePtr custom deleter.
386 * @param[in] program Pointer to the program
388 void DiscardResource(GLES::Program* program)
390 mDiscardProgramQueue.push(program);
394 * @brief Pushes RenderPass to the discard queue
396 * Function is called from the UniquePtr custom deleter.
398 * @param[in] program Pointer to the RenderPass
400 void DiscardResource(GLES::RenderPass* renderPass)
402 mDiscardRenderPassQueue.push(renderPass);
406 * @brief Pushes RenderTarget to the discard queue
408 * Function is called from the UniquePtr custom deleter.
410 * @param[in] program Pointer to the RenderTarget
412 void DiscardResource(GLES::RenderTarget* renderTarget)
414 mDiscardRenderTargetQueue.push(renderTarget);
418 * @brief Pushes Shader to the discard queue
420 * Function is called from the UniquePtr custom deleter.
422 * @param[in] program Pointer to the Shader
424 void DiscardResource(GLES::Shader* shader)
426 mDiscardShaderQueue.push(shader);
430 * @brief Pushes CommandBuffer to the discard queue
432 * Function is called from the UniquePtr custom deleter.
434 * @param[in] program Pointer to the CommandBuffer
436 void DiscardResource(GLES::CommandBuffer* commandBuffer)
438 mDiscardCommandBufferQueue.push(commandBuffer);
442 * @brief Pushes Sampler to the discard queue
444 * Function is called from the UniquePtr custom deleter.
446 * @param[in] program Pointer to the Sampler
448 void DiscardResource(GLES::Sampler* sampler)
450 mDiscardSamplerQueue.push(sampler);
454 * @brief Pushes Pipeline to the discard queue
456 * Function is called from the UniquePtr custom deleter.
458 * @param[in] program Pointer to the pipeline
460 void DiscardResource(GLES::Pipeline* pipeline)
462 mDiscardPipelineQueue.push(pipeline);
466 * @brief Flushes all pending updates
468 * Function flushes all pending resource constructions,
469 * executes command buffers and empties discard queues.
473 if(!mCreateTextureQueue.empty() ||
474 !mCreateBufferQueue.empty() ||
475 !mCreateFramebufferQueue.empty() ||
476 !mTextureUpdateRequests.empty() ||
477 !mTextureMipmapGenerationRequests.empty())
479 mGraphics->ActivateResourceContext();
483 ProcessCreateQueues();
486 ProcessTextureUpdateQueue();
488 // Process texture mipmap generation requests
489 ProcessTextureMipmapGenerationQueue();
491 // Process main command queue
492 ProcessCommandQueues();
494 // Reset texture cache in the contexts while destroying textures
497 // Reset buffer cache in the contexts while destroying buffers
501 ProcessDiscardQueues();
503 // Flush pipeline cache to remove unused pipelines
506 mPipelineCache->FlushCache();
510 // Test update to tick controller, usually it will run own thread
511 void ProcessDiscardQueues();
514 * @brief Processes a create queue for type specified
516 * @param[in,out] queue Reference to the create queue
519 void ProcessCreateQueue(T& queue)
521 while(!queue.empty())
523 auto* object = queue.front();
526 // Initialize texture
527 if(!object->InitializeResource())
529 // TODO: handle error
535 * @brief Processes a discard queue for type specified
537 * @param[in,out] queue Reference to the discard queue
539 template<class U, class T>
540 void ProcessDiscardQueue(T& queue)
542 while(!queue.empty())
544 auto* object = const_cast<U*>(queue.front());
547 object->DestroyResource();
550 auto* clbk = object->GetCreateInfo().allocationCallbacks;
557 clbk->freeCallback(object, clbk->userData);
568 * @brief Processes a discard queue for pipeline
570 * @param[in,out] queue Reference to the create queue
572 void ProcessDiscardQueue(std::queue<GLES::Pipeline*>& queue)
574 while(!queue.empty())
576 auto* object = const_cast<GLES::Pipeline*>(queue.front());
578 // Inform the contexts to invalidate the pipeline if cached
581 mContext->InvalidateCachedPipeline(object);
584 for(auto&& context : mSurfaceContexts)
588 context.second->InvalidateCachedPipeline(object);
593 object->DestroyResource();
596 auto* clbk = object->GetCreateInfo().allocationCallbacks;
600 using GLESPipeline = GLES::Pipeline;
601 object->~GLESPipeline();
604 clbk->freeCallback(object, clbk->userData);
615 * @brief Processes all resource create queues
617 void ProcessCreateQueues();
620 * @brief Process command queues and buffers
622 void ProcessCommandQueues();
625 * @brief Executes all pending texture updates
627 void ProcessTextureUpdateQueue();
630 * @brief Executes all pending texture mipmap generation
632 void ProcessTextureMipmapGenerationQueue();
635 * @brief Returns program custom parameter
637 * This function can be used as a backdoor in order to retrieve
638 * certain data out of implementation
640 * @param[in] program Valid Program object
641 * @param parameterId Integer id of parameter
642 * @param outData Output data
643 * @return True if parameter retrieved
645 bool GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData) override;
648 * @brief Returns pipeline cache object
650 * @return Valid pipeline cache object
652 [[nodiscard]] GLES::PipelineCache& GetPipelineCache() const;
655 * @brief Returns runtime supported GLES version
657 * @return GLES version enum
659 GLES::GLESVersion GetGLESVersion() const
665 * @brief Sets runtime supported GLES version
667 * @param[in] glesVersion The runtime supported GLES version
669 void SetGLESVersion(GLES::GLESVersion glesVersion)
671 mGLESVersion = glesVersion;
674 bool IsShuttingDown() const
676 return mIsShuttingDown;
680 * @brief Reset texture cache in the contexts
682 void ResetTextureCache()
686 mContext->GetGLStateCache().ResetTextureCache();
689 for(auto& context : mSurfaceContexts)
693 context.second->GetGLStateCache().ResetTextureCache();
699 * @brief Reset buffer cache in the contexts
701 void ResetBufferCache()
705 mContext->GetGLStateCache().ResetBufferCache();
708 for(auto& context : mSurfaceContexts)
712 context.second->GetGLStateCache().ResetBufferCache();
717 void ProcessCommandBuffer(const GLES::CommandBuffer& commandBuffer);
719 // Resolves presentation
720 void ResolvePresentRenderTarget(GLES::RenderTarget* renderTarget);
723 * Invoked after all rendering has finished. Used to clean up sync resources
728 * Creates a GLES context for the given render surface
730 * @param[in] surface The surface whose GLES context to be created.
732 void CreateSurfaceContext(Dali::RenderSurfaceInterface* surface);
735 * Deletes a GLES context
737 * @param[in] surface The surface whose GLES context to be deleted.
739 void DeleteSurfaceContext(Dali::RenderSurfaceInterface* surface);
742 * Activate the resource context (shared surfaceless context)
744 void ActivateResourceContext();
747 * Activate the surface context
749 * @param[in] surface The surface whose context to be switched to.
751 void ActivateSurfaceContext(Dali::RenderSurfaceInterface* surface);
754 * @brief Returns the current context
756 * @return the current context
758 GLES::Context* GetCurrentContext() const
760 return mCurrentContext;
764 * @brief Returns EGL shared context
766 * @return valid EGL shared context
768 void* GetSharedContext() const
770 return mSharedContext;
773 GLES::SyncPool& GetSyncPool()
779 Integration::GlAbstraction* mGlAbstraction{nullptr};
780 Integration::GlContextHelperAbstraction* mGlContextHelperAbstraction{nullptr};
782 Internal::Adaptor::EglSyncImplementation* mEglSyncImplementation{nullptr};
783 Internal::Adaptor::GraphicsInterface* mGraphics{nullptr}; // Pointer to owning structure via interface.
785 std::queue<GLES::Texture*> mCreateTextureQueue; ///< Create queue for texture resource
786 std::queue<GLES::Texture*> mDiscardTextureQueue; ///< Discard queue for texture resource
788 std::queue<GLES::Buffer*> mCreateBufferQueue; ///< Create queue for buffer resource
789 std::queue<GLES::Buffer*> mDiscardBufferQueue; ///< Discard queue for buffer resource
791 std::queue<GLES::Program*> mDiscardProgramQueue; ///< Discard queue for program resource
792 std::queue<GLES::Pipeline*> mDiscardPipelineQueue; ///< Discard queue of pipelines
793 std::queue<GLES::RenderPass*> mDiscardRenderPassQueue; ///< Discard queue for renderpass resource
794 std::queue<GLES::RenderTarget*> mDiscardRenderTargetQueue; ///< Discard queue for rendertarget resource
795 std::queue<GLES::Shader*> mDiscardShaderQueue; ///< Discard queue of shaders
796 std::queue<GLES::Sampler*> mDiscardSamplerQueue; ///< Discard queue of samplers
797 std::queue<const GLES::CommandBuffer*> mDiscardCommandBufferQueue; ///< Discard queue of command buffers
798 std::queue<GLES::Framebuffer*> mCreateFramebufferQueue; ///< Create queue for framebuffer resource
799 std::queue<GLES::Framebuffer*> mDiscardFramebufferQueue; ///< Discard queue for framebuffer resource
801 std::queue<GLES::CommandBuffer*> mCommandQueue; ///< we may have more in the future
803 using TextureUpdateRequest = std::pair<TextureUpdateInfo, TextureUpdateSourceInfo>;
804 std::queue<TextureUpdateRequest> mTextureUpdateRequests;
806 std::queue<const GLES::Texture*> mTextureMipmapGenerationRequests; ///< Queue for texture mipmap generation requests
808 GLES::Context* mCurrentContext{nullptr}; ///< The current context
809 std::unique_ptr<GLES::Context> mContext{nullptr}; ///< Context object handling command buffers execution
810 using SurfaceContextPair = std::pair<Dali::RenderSurfaceInterface*, std::unique_ptr<GLES::Context>>;
811 std::vector<SurfaceContextPair> mSurfaceContexts; ///< Vector of surface context objects handling command buffers execution
813 std::unique_ptr<GLES::PipelineCache> mPipelineCache{nullptr}; ///< Internal pipeline cache
815 GLES::GLESVersion mGLESVersion{GLES::GLESVersion::GLES_20}; ///< Runtime supported GLES version
816 uint32_t mTextureUploadTotalCPUMemoryUsed{0u};
818 bool mIsShuttingDown{false}; ///< Indicates whether the controller is shutting down
820 std::queue<const GLES::CommandBuffer*> mPresentationCommandBuffers{}; ///< Queue of reusable command buffers used by presentation engine
822 void* mSharedContext{nullptr}; ///< Shared EGL context
824 GLES::TextureDependencyChecker mTextureDependencyChecker; // Checks if FBO textures need syncing
825 GLES::SyncPool mSyncPool;
828 } // namespace Graphics
832 #endif //DALI_EGL_GRAPHICS_CONTROLLER_H