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 DALI_IMPORT_API 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::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;
200 * @copydoc Dali::Graphics::CreateTexture()
202 Graphics::UniquePtr<Texture> CreateTexture(const TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Texture>&& oldTexture) override;
205 * @copydoc Dali::Graphics::CreateFramebuffer()
207 Graphics::UniquePtr<Framebuffer> CreateFramebuffer(const FramebufferCreateInfo& framebufferCreateInfo, Graphics::UniquePtr<Framebuffer>&& oldFramebuffer) override;
210 * @copydoc Dali::Graphics::CreatePipeline()
212 Graphics::UniquePtr<Pipeline> CreatePipeline(const PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Pipeline>&& oldPipeline) override;
215 * @copydoc Dali::Graphics::CreateProgram()
217 Graphics::UniquePtr<Program> CreateProgram(const ProgramCreateInfo& programCreateInfo, UniquePtr<Program>&& oldProgram) override;
220 * @copydoc Dali::Graphics::CreateShader()
222 Graphics::UniquePtr<Shader> CreateShader(const ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Shader>&& oldShader) override;
225 * @copydoc Dali::Graphics::CreateSampler()
227 Graphics::UniquePtr<Sampler> CreateSampler(const SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Sampler>&& oldSampler) override;
230 * @copydoc Dali::Graphics::CreateRenderTarget()
232 Graphics::UniquePtr<RenderTarget> CreateRenderTarget(const RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<RenderTarget>&& oldRenderTarget) override;
235 * @copydoc Dali::Graphics::MapBufferRange()
237 Graphics::UniquePtr<Memory> MapBufferRange(const MapBufferInfo& mapInfo) override;
240 * @copydoc Dali::Graphics::MapTextureRange()
242 Graphics::UniquePtr<Memory> MapTextureRange(const MapTextureInfo& mapInfo) override
248 * @copydoc Dali::Graphics::UnmapMemory()
250 void UnmapMemory(Graphics::UniquePtr<Memory> memory) override
254 * @copydoc Dali::Graphics::GetTextureMemoryRequirements()
256 MemoryRequirements GetTextureMemoryRequirements(Texture& texture) const override
262 * @copydoc Dali::Graphics::GetBufferMemoryRequirements()
264 MemoryRequirements GetBufferMemoryRequirements(Buffer& buffer) const override
270 * @copydoc Dali::Graphics::GetTextureProperties()
272 const TextureProperties& GetTextureProperties(const Texture& texture) override
274 // for compiler not to moan
275 static TextureProperties dummy{};
280 * @copydoc Dali::Graphics::Controller::GetPipelineReflection()
283 [[nodiscard]] const Reflection& GetProgramReflection(const Graphics::Program& program) override;
286 * @copydoc Dali::Graphics::PipelineEquals()
288 [[nodiscard]] bool PipelineEquals(const Pipeline& pipeline0, const Pipeline& pipeline1) const override
293 [[nodiscard]] Integration::GlAbstraction* GetGL() const
299 return mGlAbstraction;
302 [[nodiscard]] Internal::Adaptor::GraphicsInterface* GetGraphicsInterface() const
308 void AddTexture(GLES::Texture& texture);
311 * @brief Adds buffer to the creation queue
314 void AddBuffer(GLES::Buffer& buffer);
317 * @brief Adds framebuffer to the creation queue
320 void AddFramebuffer(GLES::Framebuffer& framebuffer);
323 * @brief Pushes Bufer to the discard queue
325 * Function is called from the UniquePtr custom deleter.
327 * @param[in] texture Pointer to the texture
329 void DiscardResource(GLES::Texture* texture)
331 mDiscardTextureQueue.push(texture);
335 * @brief Pushes Buffer to the discard queue
337 * Function is called from the UniquePtr custom deleter.
339 * @param[in] buffer Pointer to the buffer object
341 void DiscardResource(GLES::Buffer* buffer)
343 mDiscardBufferQueue.push(buffer);
347 * @brief Pushes framebuffer to the discard queue
349 * Function is called from the UniquePtr custom deleter.
351 * @param[in] framebuffer Pointer to the framebuffer object
353 void DiscardResource(GLES::Framebuffer* framebuffer)
355 mDiscardFramebufferQueue.push(framebuffer);
359 * @brief Pushes Program to the discard queue
361 * Function is called from the UniquePtr custom deleter.
363 * @param[in] program Pointer to the program
365 void DiscardResource(GLES::Program* program)
367 mDiscardProgramQueue.push(program);
371 * @brief Pushes Shader to the discard queue
373 * Function is called from the UniquePtr custom deleter.
375 * @param[in] program Pointer to the Shader
377 void DiscardResource(GLES::Shader* shader)
379 mDiscardShaderQueue.push(shader);
383 * @brief Pushes CommandBuffer to the discard queue
385 * Function is called from the UniquePtr custom deleter.
387 * @param[in] program Pointer to the CommandBuffer
389 void DiscardResource(GLES::CommandBuffer* commandBuffer)
391 mDiscardCommandBufferQueue.push(commandBuffer);
395 * @brief Pushes Sampler to the discard queue
397 * Function is called from the UniquePtr custom deleter.
399 * @param[in] program Pointer to the Sampler
401 void DiscardResource(GLES::Sampler* sampler)
403 mDiscardSamplerQueue.push(sampler);
407 * @brief Pushes Pipeline to the discard queue
409 * Function is called from the UniquePtr custom deleter.
411 * @param[in] program Pointer to the pipeline
413 void DiscardResource(GLES::Pipeline* pipeline)
415 mDiscardPipelineQueue.push(pipeline);
419 * @brief Flushes all pending updates
421 * Function flushes all pending resource constructions,
422 * executes command buffers and empties discard queues.
426 mGraphics->ActivateResourceContext();
429 ProcessCreateQueues();
432 ProcessTextureUpdateQueue();
434 // Process main command queue
435 ProcessCommandQueues();
438 ProcessDiscardQueues();
440 // Flush pipeline cache to remove unused pipelines
443 mPipelineCache->FlushCache();
447 // Test update to tick controller, usually it will run own thread
448 void ProcessDiscardQueues();
451 * @brief Processes a create queue for type specified
453 * @param[in,out] queue Reference to the create queue
456 void ProcessCreateQueue(T& queue)
458 while(!queue.empty())
460 auto* object = queue.front();
463 // Initialize texture
464 if(!object->InitializeResource())
466 // TODO: handle error
472 * @brief Processes a create queue for type specified
474 * @param[in,out] queue Reference to the create queue
476 template<class U, class T>
477 void ProcessDiscardQueue(T& queue)
479 while(!queue.empty())
481 auto* object = queue.front();
484 object->DestroyResource();
487 auto* clbk = object->GetCreateInfo().allocationCallbacks;
494 clbk->freeCallback(object, clbk->userData);
505 * @brief Processes all resource create queues
507 void ProcessCreateQueues();
510 * @brief Process command queues and buffers
512 void ProcessCommandQueues();
515 * @brief Executes all pending texture updates
517 void ProcessTextureUpdateQueue();
520 * @brief Returns program custom parameter
522 * This function can be used as a backdoor in order to retrieve
523 * certain data out of implementation
525 * @param[in] program Valid Program object
526 * @param parameterId Integer id of parameter
527 * @param outData Output data
528 * @return True if parameter retrieved
530 bool GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData) override;
533 * @brief Returns pipeline cache object
535 * @return Valid pipeline cache object
537 [[nodiscard]] GLES::PipelineCache& GetPipelineCache() const;
540 * @brief Returns runtime supported GLES version
542 * @return GLES version enum
544 GLES::GLESVersion GetGLESVersion() const
546 // TODO: return proper version but for now we can
548 return GLES::GLESVersion::GLES_20;
551 bool IsShuttingDown() const
553 return mIsShuttingDown;
556 void ProcessCommandBuffer(GLES::CommandBuffer& commandBuffer);
558 // Resolves presentation
559 void ResolvePresentRenderTarget(GLES::RenderTarget* renderTarget);
562 * Creates a GLES context for the given render surface
564 * @param[in] surface The surface whose GLES context to be created.
566 void CreateSurfaceContext(Dali::RenderSurfaceInterface* surface);
569 * Deletes a GLES context
571 * @param[in] surface The surface whose GLES context to be deleted.
573 void DeleteSurfaceContext(Dali::RenderSurfaceInterface* surface);
576 * Activate the resource context (shared surfaceless context)
578 void ActivateResourceContext();
581 * Activate the surface context
583 * @param[in] surface The surface whose context to be switched to.
585 void ActivateSurfaceContext(Dali::RenderSurfaceInterface* surface);
588 Integration::GlAbstraction* mGlAbstraction{nullptr};
589 Integration::GlSyncAbstraction* mGlSyncAbstraction{nullptr};
590 Integration::GlContextHelperAbstraction* mGlContextHelperAbstraction{nullptr};
592 Internal::Adaptor::GraphicsInterface* mGraphics{nullptr};
594 std::queue<GLES::Texture*> mCreateTextureQueue; ///< Create queue for texture resource
595 std::queue<GLES::Texture*> mDiscardTextureQueue; ///< Discard queue for texture resource
597 std::queue<GLES::Buffer*> mCreateBufferQueue; ///< Create queue for buffer resource
598 std::queue<GLES::Buffer*> mDiscardBufferQueue; ///< Discard queue for buffer resource
600 std::queue<GLES::Program*> mDiscardProgramQueue; ///< Discard queue for program resource
601 std::queue<GLES::Pipeline*> mDiscardPipelineQueue; ///< Discard queue of pipelines
602 std::queue<GLES::Shader*> mDiscardShaderQueue; ///< Discard queue of shaders
603 std::queue<GLES::Sampler*> mDiscardSamplerQueue; ///< Discard queue of samplers
604 std::queue<GLES::CommandBuffer*> mDiscardCommandBufferQueue; ///< Discard queue of command buffers
605 std::queue<GLES::Framebuffer*> mCreateFramebufferQueue; ///< Create queue for framebuffer resource
606 std::queue<GLES::Framebuffer*> mDiscardFramebufferQueue; ///< Discard queue for framebuffer resource
608 std::queue<GLES::CommandBuffer*> mCommandQueue; ///< we may have more in the future
610 using TextureUpdateRequest = std::pair<TextureUpdateInfo, TextureUpdateSourceInfo>;
611 std::queue<TextureUpdateRequest> mTextureUpdateRequests;
613 GLES::Context* mCurrentContext{nullptr}; ///< The current context
614 std::unique_ptr<GLES::Context> mContext{nullptr}; ///< Context object handling command buffers execution
615 using SurfaceContextPair = std::pair<Dali::RenderSurfaceInterface*, std::unique_ptr<GLES::Context>>;
616 std::vector<SurfaceContextPair> mSurfaceContexts; ///< Vector of surface context objects handling command buffers execution
618 std::unique_ptr<GLES::PipelineCache> mPipelineCache{nullptr}; ///< Internal pipeline cache
620 bool mIsShuttingDown{false}; ///< Indicates whether the controller is shutting down
622 // todo: to be removed after renderpass
623 const Graphics::Framebuffer* currentFramebuffer{nullptr};
626 } // namespace Graphics
630 #endif //DALI_EGL_GRAPHICS_CONTROLLER_H