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-memory.h"
28 #include "gles-graphics-pipeline-cache.h"
29 #include "gles-graphics-pipeline.h"
30 #include "gles-graphics-reflection.h"
31 #include "gles-graphics-texture.h"
38 class GlSyncAbstraction;
39 class GlContextHelperAbstraction;
40 } // namespace Integration
51 * EGL Implementation of the graphics controller.
53 * Temporarily holds the old GL abstractions whilst dali-core is migrated to the new API.
55 DALI_IMPORT_API class EglGraphicsController : public Graphics::Controller
59 * @brief Deault constructor
61 EglGraphicsController() = default;
66 ~EglGraphicsController() override = default;
69 * Initialize the GLES abstraction. This can be called from the main thread.
71 void InitializeGLES(Integration::GlAbstraction& glAbstraction);
74 * Initialize with a reference to the GL abstractions.
76 * Note, this is now executed in the render thread, after core initialization
78 void Initialize(Integration::GlSyncAbstraction& glSyncAbstraction,
79 Integration::GlContextHelperAbstraction& glContextHelperAbstraction);
81 Integration::GlAbstraction& GetGlAbstraction() override;
82 Integration::GlSyncAbstraction& GetGlSyncAbstraction() override;
83 Integration::GlContextHelperAbstraction& GetGlContextHelperAbstraction() override;
86 * @copydoc Dali::Graphics::SubmitCommandBuffers()
88 void SubmitCommandBuffers(const SubmitInfo& submitInfo) override;
91 * @copydoc Dali::Graphics::PresentRenderTarget()
93 void PresentRenderTarget(RenderTarget* renderTarget) override
98 * @copydoc Dali::Graphics::WaitIdle()
100 void WaitIdle() override
105 * @copydoc Dali::Graphics::Pause()
107 void Pause() override
112 * @copydoc Dali::Graphics::Resume()
114 void Resume() override
119 * @copydoc Dali::Graphics::UpdateTextures()
121 void UpdateTextures(const std::vector<TextureUpdateInfo>& updateInfoList,
122 const std::vector<TextureUpdateSourceInfo>& sourceList) override;
125 * @copydoc Dali::Graphics::EnableDepthStencilBuffer()
127 bool EnableDepthStencilBuffer(bool enableDepth, bool enableStencil) override
133 * @copydoc Dali::Graphics::RunGarbageCollector()
135 void RunGarbageCollector(size_t numberOfDiscardedRenderers) override
140 * @copydoc Dali::Graphics::DiscardUnusedResources()
142 void DiscardUnusedResources() override
147 * @copydoc Dali::Graphics::IsDiscardQueueEmpty()
149 bool IsDiscardQueueEmpty() override
155 * @copydoc Dali::Graphics::IsDrawOnResumeRequired()
157 bool IsDrawOnResumeRequired() override
163 * @copydoc Dali::Graphics::CreateBuffer()
165 Graphics::UniquePtr<Buffer> CreateBuffer(const BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Buffer>&& oldBuffer) override;
168 * @copydoc Dali::Graphics::CreateCommandBuffer()
170 Graphics::UniquePtr<CommandBuffer> CreateCommandBuffer(const CommandBufferCreateInfo& commandBufferCreateInfo, Graphics::UniquePtr<CommandBuffer>&& oldCommandBuffer) override;
173 * @copydoc Dali::Graphics::CreateRenderPass()
175 Graphics::UniquePtr<RenderPass> CreateRenderPass(const RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<RenderPass>&& oldRenderPass) override
181 * @copydoc Dali::Graphics::CreateTexture()
183 Graphics::UniquePtr<Texture> CreateTexture(const TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Texture>&& oldTexture) override;
186 * @copydoc Dali::Graphics::CreateFramebuffer()
188 Graphics::UniquePtr<Framebuffer> CreateFramebuffer(const FramebufferCreateInfo& framebufferCreateInfo, Graphics::UniquePtr<Framebuffer>&& oldFramebuffer) override
194 * @copydoc Dali::Graphics::CreatePipeline()
196 Graphics::UniquePtr<Pipeline> CreatePipeline(const PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Pipeline>&& oldPipeline) override;
199 * @copydoc Dali::Graphics::CreateProgram()
201 Graphics::UniquePtr<Program> CreateProgram(const ProgramCreateInfo& programCreateInfo, UniquePtr<Program>&& oldProgram) override;
204 * @copydoc Dali::Graphics::CreateShader()
206 Graphics::UniquePtr<Shader> CreateShader(const ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Shader>&& oldShader) override;
209 * @copydoc Dali::Graphics::CreateSampler()
211 Graphics::UniquePtr<Sampler> CreateSampler(const SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Sampler>&& oldSampler) override
217 * @copydoc Dali::Graphics::CreateRenderTarget()
219 Graphics::UniquePtr<RenderTarget> CreateRenderTarget(const RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<RenderTarget>&& oldRenderTarget) override
225 * @copydoc Dali::Graphics::MapBufferRange()
227 Graphics::UniquePtr<Memory> MapBufferRange(const MapBufferInfo& mapInfo) override;
230 * @copydoc Dali::Graphics::MapTextureRange()
232 Graphics::UniquePtr<Memory> MapTextureRange(const MapTextureInfo& mapInfo) override
238 * @copydoc Dali::Graphics::UnmapMemory()
240 void UnmapMemory(Graphics::UniquePtr<Memory> memory) override
244 * @copydoc Dali::Graphics::GetTextureMemoryRequirements()
246 MemoryRequirements GetTextureMemoryRequirements(Texture& texture) const override
252 * @copydoc Dali::Graphics::GetBufferMemoryRequirements()
254 MemoryRequirements GetBufferMemoryRequirements(Buffer& buffer) const override
260 * @copydoc Dali::Graphics::GetTextureProperties()
262 const TextureProperties& GetTextureProperties(const Texture& texture) override
264 // for compiler not to moan
265 static TextureProperties dummy{};
270 * @copydoc Dali::Graphics::Controller::GetPipelineReflection()
273 [[nodiscard]] const Reflection& GetProgramReflection(const Graphics::Program& program) override;
276 * @copydoc Dali::Graphics::PipelineEquals()
278 [[nodiscard]] bool PipelineEquals(const Pipeline& pipeline0, const Pipeline& pipeline1) const override
283 [[nodiscard]] Integration::GlAbstraction* GetGL() const
285 return mGlAbstraction;
289 void AddTexture(GLES::Texture& texture);
292 * @brief Adds buffer to the creation queue
295 void AddBuffer(GLES::Buffer& buffer);
298 * @brief Pushes Bufer to the discard queue
300 * Function is called from the UniquePtr custom deleter.
302 * @param[in] texture Pointer to the texture
304 void DiscardResource(GLES::Texture* texture)
306 mDiscardTextureQueue.push(texture);
310 * @brief Pushes Buffer to the discard queue
312 * Function is called from the UniquePtr custom deleter.
314 * @param[in] texture Pointer to the texture
316 void DiscardResource(GLES::Buffer* texture)
318 mDiscardBufferQueue.push(texture);
322 * @brief Pushes Program to the discard queue
324 * Function is called from the UniquePtr custom deleter.
326 * @param[in] program Pointer to the program
328 void DiscardResource(GLES::Program* program)
330 mDiscardProgramQueue.push(program);
334 * @brief Pushes Pipeline to the discard queue
336 * Function is called from the UniquePtr custom deleter.
338 * @param[in] program Pointer to the pipeline
340 void DiscardResource(GLES::Pipeline* pipeline)
342 mDiscardPipelineQueue.push(pipeline);
346 * @brief Flushes all pending updates
348 * Function flushes all pending resource constructions,
349 * executes command buffers and empties discard queues.
354 ProcessCreateQueues();
357 ProcessTextureUpdateQueue();
359 // Process main command queue
360 ProcessCommandQueues();
363 ProcessDiscardQueues();
365 // Flush pipeline cache to remove unused pipelines
366 GetPipelineCache().FlushCache();
369 // Test update to tick controller, usually it will run own thread
370 void ProcessDiscardQueues();
373 * @brief Processes a create queue for type specified
375 * @param[in,out] queue Reference to the create queue
378 void ProcessCreateQueue(T& queue)
380 while(!queue.empty())
382 auto* object = queue.front();
385 // Initialize texture
386 if(!object->InitializeResource())
388 // TODO: handle error
394 * @brief Processes a create queue for type specified
396 * @param[in,out] queue Reference to the create queue
398 template<class U, class T>
399 void ProcessDiscardQueue(T& queue)
401 while(!queue.empty())
403 auto* object = queue.front();
406 object->DestroyResource();
409 auto* clbk = object->GetCreateInfo().allocationCallbacks;
416 clbk->freeCallback(object, clbk->userData);
427 * @brief Processes all resource create queues
429 void ProcessCreateQueues();
432 * @brief Process command queues and buffers
434 void ProcessCommandQueues();
437 * @brief Executes all pending texture updates
439 void ProcessTextureUpdateQueue();
442 * @brief Returns program custom parameter
444 * This function can be used as a backdoor in order to retrieve
445 * certain data out of implementation
447 * @param[in] program Valid Program object
448 * @param parameterId Integer id of parameter
449 * @param outData Output data
450 * @return True if parameter retrieved
452 bool GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData) override;
455 * @brief Returns pipeline cache object
457 * @return Valid pipeline cache object
459 [[nodiscard]] GLES::PipelineCache& GetPipelineCache() const;
462 Integration::GlAbstraction* mGlAbstraction{nullptr};
463 Integration::GlSyncAbstraction* mGlSyncAbstraction{nullptr};
464 Integration::GlContextHelperAbstraction* mGlContextHelperAbstraction{nullptr};
466 std::queue<GLES::Texture*> mCreateTextureQueue; ///< Create queue for texture resource
467 std::queue<GLES::Texture*> mDiscardTextureQueue; ///< Discard queue for texture resource
469 std::queue<GLES::Buffer*> mCreateBufferQueue; ///< Create queue for buffer resource
470 std::queue<GLES::Buffer*> mDiscardBufferQueue; ///< Discard queue for buffer resource
472 std::queue<GLES::Program*> mDiscardProgramQueue; ///< Discard queue for program resource
474 std::queue<GLES::Pipeline*> mDiscardPipelineQueue; ///< Discard queue of pipelines
476 std::queue<GLES::CommandBuffer*> mCommandQueue; ///< we may have more in the future
478 using TextureUpdateRequest = std::pair<TextureUpdateInfo, TextureUpdateSourceInfo>;
479 std::queue<TextureUpdateRequest> mTextureUpdateRequests;
481 std::unique_ptr<GLES::Context> mContext{nullptr}; ///< Context object handling command buffers execution
483 std::unique_ptr<GLES::PipelineCache> mPipelineCache{nullptr}; ///< Internal pipeline cache
486 } // namespace Graphics
490 #endif //DALI_EGL_GRAPHICS_CONTROLLER_H