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 "gles-context.h"
26 #include "gles-graphics-buffer.h"
27 #include "gles-graphics-command-buffer.h"
28 #include "gles-graphics-pipeline-cache.h"
29 #include "gles-graphics-pipeline.h"
30 #include "gles-graphics-reflection.h"
31 #include "gles-graphics-sampler.h"
32 #include "gles-graphics-shader.h"
33 #include "gles-graphics-texture.h"
34 #include "gles-graphics-types.h"
35 #include "gles2-graphics-memory.h"
42 class GlSyncAbstraction;
43 class GlContextHelperAbstraction;
44 } // namespace Integration
55 * EGL Implementation of the graphics controller.
57 * Temporarily holds the old GL abstractions whilst dali-core is migrated to the new API.
59 DALI_IMPORT_API class EglGraphicsController : public Graphics::Controller
63 * @brief Deault constructor
65 EglGraphicsController() = default;
70 ~EglGraphicsController() override;
73 * Initialize the GLES abstraction. This can be called from the main thread.
75 void InitializeGLES(Integration::GlAbstraction& glAbstraction);
78 * Initialize with a reference to the GL abstractions.
80 * Note, this is now executed in the render thread, after core initialization
82 void Initialize(Integration::GlSyncAbstraction& glSyncAbstraction,
83 Integration::GlContextHelperAbstraction& glContextHelperAbstraction);
85 Integration::GlAbstraction& GetGlAbstraction() override;
86 Integration::GlSyncAbstraction& GetGlSyncAbstraction() override;
87 Integration::GlContextHelperAbstraction& GetGlContextHelperAbstraction() override;
90 * @copydoc Dali::Graphics::SubmitCommandBuffers()
92 void SubmitCommandBuffers(const SubmitInfo& submitInfo) override;
95 * @copydoc Dali::Graphics::PresentRenderTarget()
97 void PresentRenderTarget(RenderTarget* renderTarget) override
102 * @copydoc Dali::Graphics::WaitIdle()
104 void WaitIdle() override
109 * @copydoc Dali::Graphics::Pause()
111 void Pause() override
116 * @copydoc Dali::Graphics::Resume()
118 void Resume() override
123 * @copydoc Dali::Graphics::Shutdown()
125 void Shutdown() override
127 mIsShuttingDown = true;
131 * @copydoc Dali::Graphics::Destroy()
133 void Destroy() override
140 * @copydoc Dali::Graphics::UpdateTextures()
142 void UpdateTextures(const std::vector<TextureUpdateInfo>& updateInfoList,
143 const std::vector<TextureUpdateSourceInfo>& sourceList) override;
146 * @copydoc Dali::Graphics::EnableDepthStencilBuffer()
148 bool EnableDepthStencilBuffer(bool enableDepth, bool enableStencil) override
154 * @copydoc Dali::Graphics::RunGarbageCollector()
156 void RunGarbageCollector(size_t numberOfDiscardedRenderers) override
161 * @copydoc Dali::Graphics::DiscardUnusedResources()
163 void DiscardUnusedResources() override
168 * @copydoc Dali::Graphics::IsDiscardQueueEmpty()
170 bool IsDiscardQueueEmpty() override
176 * @copydoc Dali::Graphics::IsDrawOnResumeRequired()
178 bool IsDrawOnResumeRequired() override
184 * @copydoc Dali::Graphics::CreateBuffer()
186 Graphics::UniquePtr<Buffer> CreateBuffer(const BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Buffer>&& oldBuffer) override;
189 * @copydoc Dali::Graphics::CreateCommandBuffer()
191 Graphics::UniquePtr<CommandBuffer> CreateCommandBuffer(const CommandBufferCreateInfo& commandBufferCreateInfo, Graphics::UniquePtr<CommandBuffer>&& oldCommandBuffer) override;
194 * @copydoc Dali::Graphics::CreateRenderPass()
196 Graphics::UniquePtr<RenderPass> CreateRenderPass(const RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<RenderPass>&& oldRenderPass) override
202 * @copydoc Dali::Graphics::CreateTexture()
204 Graphics::UniquePtr<Texture> CreateTexture(const TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Texture>&& oldTexture) override;
207 * @copydoc Dali::Graphics::CreateFramebuffer()
209 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
243 * @copydoc Dali::Graphics::MapBufferRange()
245 Graphics::UniquePtr<Memory> MapBufferRange(const MapBufferInfo& mapInfo) override;
248 * @copydoc Dali::Graphics::MapTextureRange()
250 Graphics::UniquePtr<Memory> MapTextureRange(const MapTextureInfo& mapInfo) override
256 * @copydoc Dali::Graphics::UnmapMemory()
258 void UnmapMemory(Graphics::UniquePtr<Memory> memory) override
262 * @copydoc Dali::Graphics::GetTextureMemoryRequirements()
264 MemoryRequirements GetTextureMemoryRequirements(Texture& texture) const override
270 * @copydoc Dali::Graphics::GetBufferMemoryRequirements()
272 MemoryRequirements GetBufferMemoryRequirements(Buffer& buffer) const override
278 * @copydoc Dali::Graphics::GetTextureProperties()
280 const TextureProperties& GetTextureProperties(const Texture& texture) override
282 // for compiler not to moan
283 static TextureProperties dummy{};
288 * @copydoc Dali::Graphics::Controller::GetPipelineReflection()
291 [[nodiscard]] const Reflection& GetProgramReflection(const Graphics::Program& program) override;
294 * @copydoc Dali::Graphics::PipelineEquals()
296 [[nodiscard]] bool PipelineEquals(const Pipeline& pipeline0, const Pipeline& pipeline1) const override
301 [[nodiscard]] Integration::GlAbstraction* GetGL() const
307 return mGlAbstraction;
311 void AddTexture(GLES::Texture& texture);
314 * @brief Adds buffer to the creation queue
317 void AddBuffer(GLES::Buffer& buffer);
320 * @brief Pushes Bufer to the discard queue
322 * Function is called from the UniquePtr custom deleter.
324 * @param[in] texture Pointer to the texture
326 void DiscardResource(GLES::Texture* texture)
328 mDiscardTextureQueue.push(texture);
332 * @brief Pushes Buffer to the discard queue
334 * Function is called from the UniquePtr custom deleter.
336 * @param[in] texture Pointer to the texture
338 void DiscardResource(GLES::Buffer* buffer)
340 mDiscardBufferQueue.push(buffer);
344 * @brief Pushes Program to the discard queue
346 * Function is called from the UniquePtr custom deleter.
348 * @param[in] program Pointer to the program
350 void DiscardResource(GLES::Program* program)
352 mDiscardProgramQueue.push(program);
356 * @brief Pushes Shader to the discard queue
358 * Function is called from the UniquePtr custom deleter.
360 * @param[in] program Pointer to the Shader
362 void DiscardResource(GLES::Shader* shader)
364 mDiscardShaderQueue.push(shader);
368 * @brief Pushes CommandBuffer to the discard queue
370 * Function is called from the UniquePtr custom deleter.
372 * @param[in] program Pointer to the CommandBuffer
374 void DiscardResource(GLES::CommandBuffer* commandBuffer)
376 mDiscardCommandBufferQueue.push(commandBuffer);
380 * @brief Pushes Sampler to the discard queue
382 * Function is called from the UniquePtr custom deleter.
384 * @param[in] program Pointer to the Sampler
386 void DiscardResource(GLES::Sampler* sampler)
388 mDiscardSamplerQueue.push(sampler);
392 * @brief Pushes Pipeline to the discard queue
394 * Function is called from the UniquePtr custom deleter.
396 * @param[in] program Pointer to the pipeline
398 void DiscardResource(GLES::Pipeline* pipeline)
400 mDiscardPipelineQueue.push(pipeline);
404 * @brief Flushes all pending updates
406 * Function flushes all pending resource constructions,
407 * executes command buffers and empties discard queues.
412 ProcessCreateQueues();
415 ProcessTextureUpdateQueue();
417 // Process main command queue
418 ProcessCommandQueues();
421 ProcessDiscardQueues();
423 // Flush pipeline cache to remove unused pipelines
426 mPipelineCache->FlushCache();
430 // Test update to tick controller, usually it will run own thread
431 void ProcessDiscardQueues();
434 * @brief Processes a create queue for type specified
436 * @param[in,out] queue Reference to the create queue
439 void ProcessCreateQueue(T& queue)
441 while(!queue.empty())
443 auto* object = queue.front();
446 // Initialize texture
447 if(!object->InitializeResource())
449 // TODO: handle error
455 * @brief Processes a create queue for type specified
457 * @param[in,out] queue Reference to the create queue
459 template<class U, class T>
460 void ProcessDiscardQueue(T& queue)
462 while(!queue.empty())
464 auto* object = queue.front();
467 object->DestroyResource();
470 auto* clbk = object->GetCreateInfo().allocationCallbacks;
477 clbk->freeCallback(object, clbk->userData);
488 * @brief Processes all resource create queues
490 void ProcessCreateQueues();
493 * @brief Process command queues and buffers
495 void ProcessCommandQueues();
498 * @brief Executes all pending texture updates
500 void ProcessTextureUpdateQueue();
503 * @brief Returns program custom parameter
505 * This function can be used as a backdoor in order to retrieve
506 * certain data out of implementation
508 * @param[in] program Valid Program object
509 * @param parameterId Integer id of parameter
510 * @param outData Output data
511 * @return True if parameter retrieved
513 bool GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData) override;
516 * @brief Returns pipeline cache object
518 * @return Valid pipeline cache object
520 [[nodiscard]] GLES::PipelineCache& GetPipelineCache() const;
523 * @brief Returns runtime supported GLES version
525 * @return GLES version enum
527 GLES::GLESVersion GetGLESVersion() const
529 // TODO: return proper version but for now we can
531 return GLES::GLESVersion::GLES_20;
534 bool IsShuttingDown() const
536 return mIsShuttingDown;
540 Integration::GlAbstraction* mGlAbstraction{nullptr};
541 Integration::GlSyncAbstraction* mGlSyncAbstraction{nullptr};
542 Integration::GlContextHelperAbstraction* mGlContextHelperAbstraction{nullptr};
544 std::queue<GLES::Texture*> mCreateTextureQueue; ///< Create queue for texture resource
545 std::queue<GLES::Texture*> mDiscardTextureQueue; ///< Discard queue for texture resource
547 std::queue<GLES::Buffer*> mCreateBufferQueue; ///< Create queue for buffer resource
548 std::queue<GLES::Buffer*> mDiscardBufferQueue; ///< Discard queue for buffer resource
550 std::queue<GLES::Program*> mDiscardProgramQueue; ///< Discard queue for program resource
551 std::queue<GLES::Pipeline*> mDiscardPipelineQueue; ///< Discard queue of pipelines
552 std::queue<GLES::Shader*> mDiscardShaderQueue; ///< Discard queue of shaders
553 std::queue<GLES::Sampler*> mDiscardSamplerQueue; ///< Discard queue of samplers
554 std::queue<GLES::CommandBuffer*> mDiscardCommandBufferQueue; ///< Discard queue of command buffers
556 std::queue<GLES::CommandBuffer*> mCommandQueue; ///< we may have more in the future
558 using TextureUpdateRequest = std::pair<TextureUpdateInfo, TextureUpdateSourceInfo>;
559 std::queue<TextureUpdateRequest> mTextureUpdateRequests;
561 std::unique_ptr<GLES::Context> mContext{nullptr}; ///< Context object handling command buffers execution
563 std::unique_ptr<GLES::PipelineCache> mPipelineCache{nullptr}; ///< Internal pipeline cache
565 bool mIsShuttingDown{false}; ///< Indicates whether the controller is shutting down
568 } // namespace Graphics
572 #endif //DALI_EGL_GRAPHICS_CONTROLLER_H