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 <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/gles2-graphics-memory.h>
45 class GlContextHelperAbstraction;
46 } // namespace Integration
57 * EGL Implementation of the graphics controller.
59 * Temporarily holds the old GL abstractions whilst dali-core is migrated to the new API.
61 class EglGraphicsController : public Graphics::Controller
65 * @brief Deault constructor
67 EglGraphicsController() = default;
72 ~EglGraphicsController() override;
75 * Initialize the GLES abstraction. This can be called from the main thread.
77 void InitializeGLES(Integration::GlAbstraction& glAbstraction);
80 * Initialize with a reference to the GL abstractions.
82 * Note, this is now executed in the render thread, after core initialization
84 void Initialize(Integration::GraphicsSyncAbstraction& syncImplementation,
85 Integration::GlContextHelperAbstraction& glContextHelperAbstraction,
86 Internal::Adaptor::GraphicsInterface& graphicsInterface);
88 Integration::GlAbstraction& GetGlAbstraction() override;
89 Integration::GlContextHelperAbstraction& GetGlContextHelperAbstraction() override;
90 Internal::Adaptor::EglSyncImplementation& GetEglSyncImplementation();
93 * @copydoc Dali::Graphics::SubmitCommandBuffers()
95 void SubmitCommandBuffers(const SubmitInfo& submitInfo) override;
98 * @copydoc Dali::Graphics::PresentRenderTarget()
100 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 mContext->GlContextDestroyed();
144 for(auto&& context : mSurfaceContexts)
148 context.second->GlContextDestroyed();
154 * @copydoc Dali::Graphics::UpdateTextures()
156 void UpdateTextures(const std::vector<TextureUpdateInfo>& updateInfoList,
157 const std::vector<TextureUpdateSourceInfo>& sourceList) override;
160 * @copydoc Dali::Graphics::GenerateTextureMipmaps()
162 void GenerateTextureMipmaps(const Texture& texture) override;
165 * @copydoc Dali::Graphics::EnableDepthStencilBuffer()
167 bool EnableDepthStencilBuffer(bool enableDepth, bool enableStencil) override
173 * @copydoc Dali::Graphics::RunGarbageCollector()
175 void RunGarbageCollector(size_t numberOfDiscardedRenderers) override
180 * @copydoc Dali::Graphics::DiscardUnusedResources()
182 void DiscardUnusedResources() override
187 * @copydoc Dali::Graphics::IsDiscardQueueEmpty()
189 bool IsDiscardQueueEmpty() override
195 * @copydoc Dali::Graphics::IsDrawOnResumeRequired()
197 bool IsDrawOnResumeRequired() override
203 * @copydoc Dali::Graphics::CreateBuffer()
205 Graphics::UniquePtr<Buffer> CreateBuffer(const BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Buffer>&& oldBuffer) override;
208 * @copydoc Dali::Graphics::CreateCommandBuffer()
210 Graphics::UniquePtr<CommandBuffer> CreateCommandBuffer(const CommandBufferCreateInfo& commandBufferCreateInfo, Graphics::UniquePtr<CommandBuffer>&& oldCommandBuffer) override;
213 * @copydoc Dali::Graphics::CreateRenderPass()
215 Graphics::UniquePtr<RenderPass> CreateRenderPass(const RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<RenderPass>&& oldRenderPass) override;
218 * @copydoc Dali::Graphics::CreateTexture()
220 Graphics::UniquePtr<Texture> CreateTexture(const TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Texture>&& oldTexture) override;
223 * @copydoc Dali::Graphics::CreateFramebuffer()
225 Graphics::UniquePtr<Framebuffer> CreateFramebuffer(const FramebufferCreateInfo& framebufferCreateInfo, Graphics::UniquePtr<Framebuffer>&& oldFramebuffer) override;
228 * @copydoc Dali::Graphics::CreatePipeline()
230 Graphics::UniquePtr<Pipeline> CreatePipeline(const PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Pipeline>&& oldPipeline) override;
233 * @copydoc Dali::Graphics::CreateProgram()
235 Graphics::UniquePtr<Program> CreateProgram(const ProgramCreateInfo& programCreateInfo, UniquePtr<Program>&& oldProgram) override;
238 * @copydoc Dali::Graphics::CreateShader()
240 Graphics::UniquePtr<Shader> CreateShader(const ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Shader>&& oldShader) override;
243 * @copydoc Dali::Graphics::CreateSampler()
245 Graphics::UniquePtr<Sampler> CreateSampler(const SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Sampler>&& oldSampler) override;
248 * @copydoc Dali::Graphics::CreateRenderTarget()
250 Graphics::UniquePtr<RenderTarget> CreateRenderTarget(const RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<RenderTarget>&& oldRenderTarget) override;
253 * @copydoc Dali::Graphics::CreateSyncObject()
255 Graphics::UniquePtr<SyncObject> CreateSyncObject(const SyncObjectCreateInfo& syncObjectCreateInfo,
256 Graphics::UniquePtr<SyncObject>&& oldSyncObject) override;
259 * @copydoc Dali::Graphics::MapBufferRange()
261 Graphics::UniquePtr<Memory> MapBufferRange(const MapBufferInfo& mapInfo) override;
264 * @copydoc Dali::Graphics::MapTextureRange()
266 Graphics::UniquePtr<Memory> MapTextureRange(const MapTextureInfo& mapInfo) override
272 * @copydoc Dali::Graphics::UnmapMemory()
274 void UnmapMemory(Graphics::UniquePtr<Memory> memory) override
278 * @copydoc Dali::Graphics::GetTextureMemoryRequirements()
280 MemoryRequirements GetTextureMemoryRequirements(Texture& texture) const override
286 * @copydoc Dali::Graphics::GetBufferMemoryRequirements()
288 MemoryRequirements GetBufferMemoryRequirements(Buffer& buffer) const override
294 * @copydoc Dali::Graphics::GetTextureProperties()
296 const TextureProperties& GetTextureProperties(const Texture& texture) override
298 // for compiler not to moan
299 static TextureProperties dummy{};
304 * @copydoc Dali::Graphics::Controller::GetPipelineReflection()
307 [[nodiscard]] const Reflection& GetProgramReflection(const Graphics::Program& program) override;
310 * @copydoc Dali::Graphics::PipelineEquals()
312 [[nodiscard]] bool PipelineEquals(const Pipeline& pipeline0, const Pipeline& pipeline1) const override
317 [[nodiscard]] Integration::GlAbstraction* GetGL() const
323 return mGlAbstraction;
326 [[nodiscard]] Internal::Adaptor::GraphicsInterface* GetGraphicsInterface() const
332 void AddTexture(GLES::Texture& texture);
335 * @brief Adds buffer to the creation queue
338 void AddBuffer(GLES::Buffer& buffer);
341 * @brief Adds framebuffer to the creation queue
344 void AddFramebuffer(GLES::Framebuffer& framebuffer);
347 * @brief Pushes Bufer to the discard queue
349 * Function is called from the UniquePtr custom deleter.
351 * @param[in] texture Pointer to the texture
353 void DiscardResource(GLES::Texture* texture)
355 mDiscardTextureQueue.push(texture);
359 * @brief Pushes Buffer to the discard queue
361 * Function is called from the UniquePtr custom deleter.
363 * @param[in] buffer Pointer to the buffer object
365 void DiscardResource(GLES::Buffer* buffer)
367 mDiscardBufferQueue.push(buffer);
371 * @brief Pushes framebuffer to the discard queue
373 * Function is called from the UniquePtr custom deleter.
375 * @param[in] framebuffer Pointer to the framebuffer object
377 void DiscardResource(GLES::Framebuffer* framebuffer)
379 mDiscardFramebufferQueue.push(framebuffer);
383 * @brief Pushes Program to the discard queue
385 * Function is called from the UniquePtr custom deleter.
387 * @param[in] program Pointer to the program
389 void DiscardResource(GLES::Program* program)
391 mDiscardProgramQueue.push(program);
395 * @brief Pushes Shader to the discard queue
397 * Function is called from the UniquePtr custom deleter.
399 * @param[in] program Pointer to the Shader
401 void DiscardResource(GLES::Shader* shader)
403 mDiscardShaderQueue.push(shader);
407 * @brief Pushes CommandBuffer to the discard queue
409 * Function is called from the UniquePtr custom deleter.
411 * @param[in] program Pointer to the CommandBuffer
413 void DiscardResource(GLES::CommandBuffer* commandBuffer)
415 mDiscardCommandBufferQueue.push(commandBuffer);
419 * @brief Pushes Sampler to the discard queue
421 * Function is called from the UniquePtr custom deleter.
423 * @param[in] program Pointer to the Sampler
425 void DiscardResource(GLES::Sampler* sampler)
427 mDiscardSamplerQueue.push(sampler);
431 * @brief Pushes Pipeline to the discard queue
433 * Function is called from the UniquePtr custom deleter.
435 * @param[in] program Pointer to the pipeline
437 void DiscardResource(GLES::Pipeline* pipeline)
439 mDiscardPipelineQueue.push(pipeline);
443 * @brief Flushes all pending updates
445 * Function flushes all pending resource constructions,
446 * executes command buffers and empties discard queues.
450 if(!mCreateTextureQueue.empty() ||
451 !mCreateBufferQueue.empty() ||
452 !mCreateFramebufferQueue.empty() ||
453 !mTextureUpdateRequests.empty() ||
454 !mTextureMipmapGenerationRequests.empty())
456 mGraphics->ActivateResourceContext();
460 ProcessCreateQueues();
463 ProcessTextureUpdateQueue();
465 // Process texture mipmap generation requests
466 ProcessTextureMipmapGenerationQueue();
468 // Process main command queue
469 ProcessCommandQueues();
471 // Reset texture cache in the contexts while destroying textures
474 // Reset buffer cache in the contexts while destroying buffers
478 ProcessDiscardQueues();
480 // Flush pipeline cache to remove unused pipelines
483 mPipelineCache->FlushCache();
487 // Test update to tick controller, usually it will run own thread
488 void ProcessDiscardQueues();
491 * @brief Processes a create queue for type specified
493 * @param[in,out] queue Reference to the create queue
496 void ProcessCreateQueue(T& queue)
498 while(!queue.empty())
500 auto* object = queue.front();
503 // Initialize texture
504 if(!object->InitializeResource())
506 // TODO: handle error
512 * @brief Processes a discard queue for type specified
514 * @param[in,out] queue Reference to the discard queue
516 template<class U, class T>
517 void ProcessDiscardQueue(T& queue)
519 while(!queue.empty())
521 auto* object = const_cast<U*>(queue.front());
524 object->DestroyResource();
527 auto* clbk = object->GetCreateInfo().allocationCallbacks;
534 clbk->freeCallback(object, clbk->userData);
545 * @brief Processes a discard queue for pipeline
547 * @param[in,out] queue Reference to the create queue
549 void ProcessDiscardQueue(std::queue<GLES::Pipeline*>& queue)
551 while(!queue.empty())
553 auto* object = const_cast<GLES::Pipeline*>(queue.front());
555 // Inform the contexts to invalidate the pipeline if cached
558 mContext->InvalidateCachedPipeline(object);
561 for(auto&& context : mSurfaceContexts)
565 context.second->InvalidateCachedPipeline(object);
570 object->DestroyResource();
573 auto* clbk = object->GetCreateInfo().allocationCallbacks;
577 using GLESPipeline = GLES::Pipeline;
578 object->~GLESPipeline();
581 clbk->freeCallback(object, clbk->userData);
592 * @brief Processes all resource create queues
594 void ProcessCreateQueues();
597 * @brief Process command queues and buffers
599 void ProcessCommandQueues();
602 * @brief Executes all pending texture updates
604 void ProcessTextureUpdateQueue();
607 * @brief Executes all pending texture mipmap generation
609 void ProcessTextureMipmapGenerationQueue();
612 * @brief Returns program custom parameter
614 * This function can be used as a backdoor in order to retrieve
615 * certain data out of implementation
617 * @param[in] program Valid Program object
618 * @param parameterId Integer id of parameter
619 * @param outData Output data
620 * @return True if parameter retrieved
622 bool GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData) override;
625 * @brief Returns pipeline cache object
627 * @return Valid pipeline cache object
629 [[nodiscard]] GLES::PipelineCache& GetPipelineCache() const;
632 * @brief Returns runtime supported GLES version
634 * @return GLES version enum
636 GLES::GLESVersion GetGLESVersion() const
642 * @brief Sets runtime supported GLES version
644 * @param[in] glesVersion The runtime supported GLES version
646 void SetGLESVersion(GLES::GLESVersion glesVersion)
648 mGLESVersion = glesVersion;
651 bool IsShuttingDown() const
653 return mIsShuttingDown;
657 * @brief Reset texture cache in the contexts
659 void ResetTextureCache()
663 mContext->GetGLStateCache().ResetTextureCache();
666 for(auto& context : mSurfaceContexts)
670 context.second->GetGLStateCache().ResetTextureCache();
676 * @brief Reset buffer cache in the contexts
678 void ResetBufferCache()
682 mContext->GetGLStateCache().ResetBufferCache();
685 for(auto& context : mSurfaceContexts)
689 context.second->GetGLStateCache().ResetBufferCache();
694 void ProcessCommandBuffer(const GLES::CommandBuffer& commandBuffer);
696 // Resolves presentation
697 void ResolvePresentRenderTarget(GLES::RenderTarget* renderTarget);
700 * Creates a GLES context for the given render surface
702 * @param[in] surface The surface whose GLES context to be created.
704 void CreateSurfaceContext(Dali::RenderSurfaceInterface* surface);
707 * Deletes a GLES context
709 * @param[in] surface The surface whose GLES context to be deleted.
711 void DeleteSurfaceContext(Dali::RenderSurfaceInterface* surface);
714 * Activate the resource context (shared surfaceless context)
716 void ActivateResourceContext();
719 * Activate the surface context
721 * @param[in] surface The surface whose context to be switched to.
723 void ActivateSurfaceContext(Dali::RenderSurfaceInterface* surface);
726 * @brief Returns the current context
728 * @return the current context
730 GLES::Context* GetCurrentContext() const
732 return mCurrentContext;
736 Integration::GlAbstraction* mGlAbstraction{nullptr};
737 Integration::GlContextHelperAbstraction* mGlContextHelperAbstraction{nullptr};
739 Internal::Adaptor::EglSyncImplementation* mEglSyncImplementation{nullptr};
740 Internal::Adaptor::GraphicsInterface* mGraphics{nullptr}; // Pointer to owning structure via interface.
742 std::queue<GLES::Texture*> mCreateTextureQueue; ///< Create queue for texture resource
743 std::queue<GLES::Texture*> mDiscardTextureQueue; ///< Discard queue for texture resource
745 std::queue<GLES::Buffer*> mCreateBufferQueue; ///< Create queue for buffer resource
746 std::queue<GLES::Buffer*> mDiscardBufferQueue; ///< Discard queue for buffer resource
748 std::queue<GLES::Program*> mDiscardProgramQueue; ///< Discard queue for program resource
749 std::queue<GLES::Pipeline*> mDiscardPipelineQueue; ///< Discard queue of pipelines
750 std::queue<GLES::Shader*> mDiscardShaderQueue; ///< Discard queue of shaders
751 std::queue<GLES::Sampler*> mDiscardSamplerQueue; ///< Discard queue of samplers
752 std::queue<const GLES::CommandBuffer*> mDiscardCommandBufferQueue; ///< Discard queue of command buffers
753 std::queue<GLES::Framebuffer*> mCreateFramebufferQueue; ///< Create queue for framebuffer resource
754 std::queue<GLES::Framebuffer*> mDiscardFramebufferQueue; ///< Discard queue for framebuffer resource
756 std::queue<GLES::CommandBuffer*> mCommandQueue; ///< we may have more in the future
758 using TextureUpdateRequest = std::pair<TextureUpdateInfo, TextureUpdateSourceInfo>;
759 std::queue<TextureUpdateRequest> mTextureUpdateRequests;
761 std::queue<const GLES::Texture*> mTextureMipmapGenerationRequests; ///< Queue for texture mipmap generation requests
763 GLES::Context* mCurrentContext{nullptr}; ///< The current context
764 std::unique_ptr<GLES::Context> mContext{nullptr}; ///< Context object handling command buffers execution
765 using SurfaceContextPair = std::pair<Dali::RenderSurfaceInterface*, std::unique_ptr<GLES::Context>>;
766 std::vector<SurfaceContextPair> mSurfaceContexts; ///< Vector of surface context objects handling command buffers execution
768 std::unique_ptr<GLES::PipelineCache> mPipelineCache{nullptr}; ///< Internal pipeline cache
770 GLES::GLESVersion mGLESVersion{GLES::GLESVersion::GLES_20}; ///< Runtime supported GLES version
771 uint32_t mTextureUploadTotalCPUMemoryUsed{0u};
773 bool mIsShuttingDown{false}; ///< Indicates whether the controller is shutting down
775 std::queue<const GLES::CommandBuffer*> mPresentationCommandBuffers{}; ///< Queue of reusable command buffers used by presentation engine
778 } // namespace Graphics
782 #endif //DALI_EGL_GRAPHICS_CONTROLLER_H