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/internal/graphics/common/graphics-interface.h>
26 #include "gles-context.h"
27 #include "gles-graphics-buffer.h"
28 #include "gles-graphics-command-buffer.h"
29 #include "gles-graphics-framebuffer.h"
30 #include "gles-graphics-pipeline-cache.h"
31 #include "gles-graphics-pipeline.h"
32 #include "gles-graphics-reflection.h"
33 #include "gles-graphics-sampler.h"
34 #include "gles-graphics-shader.h"
35 #include "gles-graphics-texture.h"
36 #include "gles-graphics-types.h"
37 #include "gles2-graphics-memory.h"
44 class GlSyncAbstraction;
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::GlSyncAbstraction& glSyncAbstraction,
85 Integration::GlContextHelperAbstraction& glContextHelperAbstraction,
86 Internal::Adaptor::GraphicsInterface& graphicsInterface);
88 Integration::GlAbstraction& GetGlAbstraction() override;
89 Integration::GlSyncAbstraction& GetGlSyncAbstraction() override;
90 Integration::GlContextHelperAbstraction& GetGlContextHelperAbstraction() override;
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 * @copydoc Dali::Graphics::UpdateTextures()
143 void UpdateTextures(const std::vector<TextureUpdateInfo>& updateInfoList,
144 const std::vector<TextureUpdateSourceInfo>& sourceList) override;
147 * @copydoc Dali::Graphics::GenerateTextureMipmaps()
149 void GenerateTextureMipmaps(const Texture& texture) override;
152 * @copydoc Dali::Graphics::EnableDepthStencilBuffer()
154 bool EnableDepthStencilBuffer(bool enableDepth, bool enableStencil) override
160 * @copydoc Dali::Graphics::RunGarbageCollector()
162 void RunGarbageCollector(size_t numberOfDiscardedRenderers) override
167 * @copydoc Dali::Graphics::DiscardUnusedResources()
169 void DiscardUnusedResources() override
174 * @copydoc Dali::Graphics::IsDiscardQueueEmpty()
176 bool IsDiscardQueueEmpty() override
182 * @copydoc Dali::Graphics::IsDrawOnResumeRequired()
184 bool IsDrawOnResumeRequired() override
190 * @copydoc Dali::Graphics::CreateBuffer()
192 Graphics::UniquePtr<Buffer> CreateBuffer(const BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Buffer>&& oldBuffer) override;
195 * @copydoc Dali::Graphics::CreateCommandBuffer()
197 Graphics::UniquePtr<CommandBuffer> CreateCommandBuffer(const CommandBufferCreateInfo& commandBufferCreateInfo, Graphics::UniquePtr<CommandBuffer>&& oldCommandBuffer) override;
200 * @copydoc Dali::Graphics::CreateRenderPass()
202 Graphics::UniquePtr<RenderPass> CreateRenderPass(const RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<RenderPass>&& oldRenderPass) override;
205 * @copydoc Dali::Graphics::CreateTexture()
207 Graphics::UniquePtr<Texture> CreateTexture(const TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Texture>&& oldTexture) override;
210 * @copydoc Dali::Graphics::CreateFramebuffer()
212 Graphics::UniquePtr<Framebuffer> CreateFramebuffer(const FramebufferCreateInfo& framebufferCreateInfo, Graphics::UniquePtr<Framebuffer>&& oldFramebuffer) override;
215 * @copydoc Dali::Graphics::CreatePipeline()
217 Graphics::UniquePtr<Pipeline> CreatePipeline(const PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Pipeline>&& oldPipeline) override;
220 * @copydoc Dali::Graphics::CreateProgram()
222 Graphics::UniquePtr<Program> CreateProgram(const ProgramCreateInfo& programCreateInfo, UniquePtr<Program>&& oldProgram) override;
225 * @copydoc Dali::Graphics::CreateShader()
227 Graphics::UniquePtr<Shader> CreateShader(const ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Shader>&& oldShader) override;
230 * @copydoc Dali::Graphics::CreateSampler()
232 Graphics::UniquePtr<Sampler> CreateSampler(const SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Sampler>&& oldSampler) override;
235 * @copydoc Dali::Graphics::CreateRenderTarget()
237 Graphics::UniquePtr<RenderTarget> CreateRenderTarget(const RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<RenderTarget>&& oldRenderTarget) override;
240 * @copydoc Dali::Graphics::MapBufferRange()
242 Graphics::UniquePtr<Memory> MapBufferRange(const MapBufferInfo& mapInfo) override;
245 * @copydoc Dali::Graphics::MapTextureRange()
247 Graphics::UniquePtr<Memory> MapTextureRange(const MapTextureInfo& mapInfo) override
253 * @copydoc Dali::Graphics::UnmapMemory()
255 void UnmapMemory(Graphics::UniquePtr<Memory> memory) override
259 * @copydoc Dali::Graphics::GetTextureMemoryRequirements()
261 MemoryRequirements GetTextureMemoryRequirements(Texture& texture) const override
267 * @copydoc Dali::Graphics::GetBufferMemoryRequirements()
269 MemoryRequirements GetBufferMemoryRequirements(Buffer& buffer) const override
275 * @copydoc Dali::Graphics::GetTextureProperties()
277 const TextureProperties& GetTextureProperties(const Texture& texture) override
279 // for compiler not to moan
280 static TextureProperties dummy{};
285 * @copydoc Dali::Graphics::Controller::GetPipelineReflection()
288 [[nodiscard]] const Reflection& GetProgramReflection(const Graphics::Program& program) override;
291 * @copydoc Dali::Graphics::PipelineEquals()
293 [[nodiscard]] bool PipelineEquals(const Pipeline& pipeline0, const Pipeline& pipeline1) const override
298 [[nodiscard]] Integration::GlAbstraction* GetGL() const
304 return mGlAbstraction;
307 [[nodiscard]] Internal::Adaptor::GraphicsInterface* GetGraphicsInterface() const
313 void AddTexture(GLES::Texture& texture);
316 * @brief Adds buffer to the creation queue
319 void AddBuffer(GLES::Buffer& buffer);
322 * @brief Adds framebuffer to the creation queue
325 void AddFramebuffer(GLES::Framebuffer& framebuffer);
328 * @brief Pushes Bufer to the discard queue
330 * Function is called from the UniquePtr custom deleter.
332 * @param[in] texture Pointer to the texture
334 void DiscardResource(GLES::Texture* texture)
336 mDiscardTextureQueue.push(texture);
340 * @brief Pushes Buffer to the discard queue
342 * Function is called from the UniquePtr custom deleter.
344 * @param[in] buffer Pointer to the buffer object
346 void DiscardResource(GLES::Buffer* buffer)
348 mDiscardBufferQueue.push(buffer);
352 * @brief Pushes framebuffer to the discard queue
354 * Function is called from the UniquePtr custom deleter.
356 * @param[in] framebuffer Pointer to the framebuffer object
358 void DiscardResource(GLES::Framebuffer* framebuffer)
360 mDiscardFramebufferQueue.push(framebuffer);
364 * @brief Pushes Program to the discard queue
366 * Function is called from the UniquePtr custom deleter.
368 * @param[in] program Pointer to the program
370 void DiscardResource(GLES::Program* program)
372 mDiscardProgramQueue.push(program);
376 * @brief Pushes Shader to the discard queue
378 * Function is called from the UniquePtr custom deleter.
380 * @param[in] program Pointer to the Shader
382 void DiscardResource(GLES::Shader* shader)
384 mDiscardShaderQueue.push(shader);
388 * @brief Pushes CommandBuffer to the discard queue
390 * Function is called from the UniquePtr custom deleter.
392 * @param[in] program Pointer to the CommandBuffer
394 void DiscardResource(GLES::CommandBuffer* commandBuffer)
396 mDiscardCommandBufferQueue.push(commandBuffer);
400 * @brief Pushes Sampler to the discard queue
402 * Function is called from the UniquePtr custom deleter.
404 * @param[in] program Pointer to the Sampler
406 void DiscardResource(GLES::Sampler* sampler)
408 mDiscardSamplerQueue.push(sampler);
412 * @brief Pushes Pipeline to the discard queue
414 * Function is called from the UniquePtr custom deleter.
416 * @param[in] program Pointer to the pipeline
418 void DiscardResource(GLES::Pipeline* pipeline)
420 mDiscardPipelineQueue.push(pipeline);
424 * @brief Flushes all pending updates
426 * Function flushes all pending resource constructions,
427 * executes command buffers and empties discard queues.
431 mGraphics->ActivateResourceContext();
434 ProcessCreateQueues();
437 ProcessTextureUpdateQueue();
439 // Process texture mipmap generation requests
440 ProcessTextureMipmapGenerationQueue();
442 // Process main command queue
443 ProcessCommandQueues();
446 ProcessDiscardQueues();
448 // Flush pipeline cache to remove unused pipelines
451 mPipelineCache->FlushCache();
455 // Test update to tick controller, usually it will run own thread
456 void ProcessDiscardQueues();
459 * @brief Processes a create queue for type specified
461 * @param[in,out] queue Reference to the create queue
464 void ProcessCreateQueue(T& queue)
466 while(!queue.empty())
468 auto* object = queue.front();
471 // Initialize texture
472 if(!object->InitializeResource())
474 // TODO: handle error
480 * @brief Processes a create queue for type specified
482 * @param[in,out] queue Reference to the create queue
484 template<class U, class T>
485 void ProcessDiscardQueue(T& queue)
487 while(!queue.empty())
489 auto* object = const_cast<U*>(queue.front());
492 object->DestroyResource();
495 auto* clbk = object->GetCreateInfo().allocationCallbacks;
502 clbk->freeCallback(object, clbk->userData);
513 * @brief Processes all resource create queues
515 void ProcessCreateQueues();
518 * @brief Process command queues and buffers
520 void ProcessCommandQueues();
523 * @brief Executes all pending texture updates
525 void ProcessTextureUpdateQueue();
528 * @brief Executes all pending texture mipmap generation
530 void ProcessTextureMipmapGenerationQueue();
533 * @brief Returns program custom parameter
535 * This function can be used as a backdoor in order to retrieve
536 * certain data out of implementation
538 * @param[in] program Valid Program object
539 * @param parameterId Integer id of parameter
540 * @param outData Output data
541 * @return True if parameter retrieved
543 bool GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData) override;
546 * @brief Returns pipeline cache object
548 * @return Valid pipeline cache object
550 [[nodiscard]] GLES::PipelineCache& GetPipelineCache() const;
553 * @brief Returns runtime supported GLES version
555 * @return GLES version enum
557 GLES::GLESVersion GetGLESVersion() const
563 * @brief Sets runtime supported GLES version
565 * @param[in] glesVersion The runtime supported GLES version
567 void SetGLESVersion(GLES::GLESVersion glesVersion)
569 mGLESVersion = glesVersion;
572 bool IsShuttingDown() const
574 return mIsShuttingDown;
577 void ProcessCommandBuffer(const GLES::CommandBuffer& commandBuffer);
579 // Resolves presentation
580 void ResolvePresentRenderTarget(GLES::RenderTarget* renderTarget);
583 * Creates a GLES context for the given render surface
585 * @param[in] surface The surface whose GLES context to be created.
587 void CreateSurfaceContext(Dali::RenderSurfaceInterface* surface);
590 * Deletes a GLES context
592 * @param[in] surface The surface whose GLES context to be deleted.
594 void DeleteSurfaceContext(Dali::RenderSurfaceInterface* surface);
597 * Activate the resource context (shared surfaceless context)
599 void ActivateResourceContext();
602 * Activate the surface context
604 * @param[in] surface The surface whose context to be switched to.
606 void ActivateSurfaceContext(Dali::RenderSurfaceInterface* surface);
609 Integration::GlAbstraction* mGlAbstraction{nullptr};
610 Integration::GlSyncAbstraction* mGlSyncAbstraction{nullptr};
611 Integration::GlContextHelperAbstraction* mGlContextHelperAbstraction{nullptr};
613 Internal::Adaptor::GraphicsInterface* mGraphics{nullptr};
615 std::queue<GLES::Texture*> mCreateTextureQueue; ///< Create queue for texture resource
616 std::queue<GLES::Texture*> mDiscardTextureQueue; ///< Discard queue for texture resource
618 std::queue<GLES::Buffer*> mCreateBufferQueue; ///< Create queue for buffer resource
619 std::queue<GLES::Buffer*> mDiscardBufferQueue; ///< Discard queue for buffer resource
621 std::queue<GLES::Program*> mDiscardProgramQueue; ///< Discard queue for program resource
622 std::queue<GLES::Pipeline*> mDiscardPipelineQueue; ///< Discard queue of pipelines
623 std::queue<GLES::Shader*> mDiscardShaderQueue; ///< Discard queue of shaders
624 std::queue<GLES::Sampler*> mDiscardSamplerQueue; ///< Discard queue of samplers
625 std::queue<const GLES::CommandBuffer*> mDiscardCommandBufferQueue; ///< Discard queue of command buffers
626 std::queue<GLES::Framebuffer*> mCreateFramebufferQueue; ///< Create queue for framebuffer resource
627 std::queue<GLES::Framebuffer*> mDiscardFramebufferQueue; ///< Discard queue for framebuffer resource
629 std::queue<GLES::CommandBuffer*> mCommandQueue; ///< we may have more in the future
631 using TextureUpdateRequest = std::pair<TextureUpdateInfo, TextureUpdateSourceInfo>;
632 std::queue<TextureUpdateRequest> mTextureUpdateRequests;
634 std::queue<const GLES::Texture*> mTextureMipmapGenerationRequests; ///< Queue for texture mipmap generation requests
636 GLES::Context* mCurrentContext{nullptr}; ///< The current context
637 std::unique_ptr<GLES::Context> mContext{nullptr}; ///< Context object handling command buffers execution
638 using SurfaceContextPair = std::pair<Dali::RenderSurfaceInterface*, std::unique_ptr<GLES::Context>>;
639 std::vector<SurfaceContextPair> mSurfaceContexts; ///< Vector of surface context objects handling command buffers execution
641 std::unique_ptr<GLES::PipelineCache> mPipelineCache{nullptr}; ///< Internal pipeline cache
643 GLES::GLESVersion mGLESVersion{GLES::GLESVersion::GLES_20}; ///< Runtime supported GLES version
644 uint32_t mTextureUploadTotalCPUMemoryUsed{0u};
646 bool mIsShuttingDown{false}; ///< Indicates whether the controller is shutting down
648 // todo: to be removed after renderpass
649 const Graphics::Framebuffer* currentFramebuffer{nullptr};
652 } // namespace Graphics
656 #endif //DALI_EGL_GRAPHICS_CONTROLLER_H