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-sampler.h"
32 #include "gles-graphics-texture.h"
39 class GlSyncAbstraction;
40 class GlContextHelperAbstraction;
41 } // namespace Integration
52 * EGL Implementation of the graphics controller.
54 * Temporarily holds the old GL abstractions whilst dali-core is migrated to the new API.
56 DALI_IMPORT_API class EglGraphicsController : public Graphics::Controller
60 * @brief Deault constructor
62 EglGraphicsController() = default;
67 ~EglGraphicsController() override = default;
70 * Initialize the GLES abstraction. This can be called from the main thread.
72 void InitializeGLES(Integration::GlAbstraction& glAbstraction);
75 * Initialize with a reference to the GL abstractions.
77 * Note, this is now executed in the render thread, after core initialization
79 void Initialize(Integration::GlSyncAbstraction& glSyncAbstraction,
80 Integration::GlContextHelperAbstraction& glContextHelperAbstraction);
82 Integration::GlAbstraction& GetGlAbstraction() override;
83 Integration::GlSyncAbstraction& GetGlSyncAbstraction() override;
84 Integration::GlContextHelperAbstraction& GetGlContextHelperAbstraction() override;
87 * @copydoc Dali::Graphics::SubmitCommandBuffers()
89 void SubmitCommandBuffers(const SubmitInfo& submitInfo) override;
92 * @copydoc Dali::Graphics::PresentRenderTarget()
94 void PresentRenderTarget(RenderTarget* renderTarget) override
99 * @copydoc Dali::Graphics::WaitIdle()
101 void WaitIdle() override
106 * @copydoc Dali::Graphics::Pause()
108 void Pause() override
113 * @copydoc Dali::Graphics::Resume()
115 void Resume() override
120 * @copydoc Dali::Graphics::UpdateTextures()
122 void UpdateTextures(const std::vector<TextureUpdateInfo>& updateInfoList,
123 const std::vector<TextureUpdateSourceInfo>& sourceList) override;
126 * @copydoc Dali::Graphics::EnableDepthStencilBuffer()
128 bool EnableDepthStencilBuffer(bool enableDepth, bool enableStencil) override
134 * @copydoc Dali::Graphics::RunGarbageCollector()
136 void RunGarbageCollector(size_t numberOfDiscardedRenderers) override
141 * @copydoc Dali::Graphics::DiscardUnusedResources()
143 void DiscardUnusedResources() override
148 * @copydoc Dali::Graphics::IsDiscardQueueEmpty()
150 bool IsDiscardQueueEmpty() override
156 * @copydoc Dali::Graphics::IsDrawOnResumeRequired()
158 bool IsDrawOnResumeRequired() override
164 * @copydoc Dali::Graphics::CreateBuffer()
166 Graphics::UniquePtr<Buffer> CreateBuffer(const BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Buffer>&& oldBuffer) override;
169 * @copydoc Dali::Graphics::CreateCommandBuffer()
171 Graphics::UniquePtr<CommandBuffer> CreateCommandBuffer(const CommandBufferCreateInfo& commandBufferCreateInfo, Graphics::UniquePtr<CommandBuffer>&& oldCommandBuffer) override;
174 * @copydoc Dali::Graphics::CreateRenderPass()
176 Graphics::UniquePtr<RenderPass> CreateRenderPass(const RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<RenderPass>&& oldRenderPass) override
182 * @copydoc Dali::Graphics::CreateTexture()
184 Graphics::UniquePtr<Texture> CreateTexture(const TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Texture>&& oldTexture) override;
187 * @copydoc Dali::Graphics::CreateFramebuffer()
189 Graphics::UniquePtr<Framebuffer> CreateFramebuffer(const FramebufferCreateInfo& framebufferCreateInfo, Graphics::UniquePtr<Framebuffer>&& oldFramebuffer) override
195 * @copydoc Dali::Graphics::CreatePipeline()
197 Graphics::UniquePtr<Pipeline> CreatePipeline(const PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Pipeline>&& oldPipeline) override;
200 * @copydoc Dali::Graphics::CreateProgram()
202 Graphics::UniquePtr<Program> CreateProgram(const ProgramCreateInfo& programCreateInfo, UniquePtr<Program>&& oldProgram) override;
205 * @copydoc Dali::Graphics::CreateShader()
207 Graphics::UniquePtr<Shader> CreateShader(const ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Shader>&& oldShader) override;
210 * @copydoc Dali::Graphics::CreateSampler()
212 Graphics::UniquePtr<Sampler> CreateSampler(const SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Sampler>&& oldSampler) override;
215 * @copydoc Dali::Graphics::CreateRenderTarget()
217 Graphics::UniquePtr<RenderTarget> CreateRenderTarget(const RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<RenderTarget>&& oldRenderTarget) override
223 * @copydoc Dali::Graphics::MapBufferRange()
225 Graphics::UniquePtr<Memory> MapBufferRange(const MapBufferInfo& mapInfo) override;
228 * @copydoc Dali::Graphics::MapTextureRange()
230 Graphics::UniquePtr<Memory> MapTextureRange(const MapTextureInfo& mapInfo) override
236 * @copydoc Dali::Graphics::UnmapMemory()
238 void UnmapMemory(Graphics::UniquePtr<Memory> memory) override
242 * @copydoc Dali::Graphics::GetTextureMemoryRequirements()
244 MemoryRequirements GetTextureMemoryRequirements(Texture& texture) const override
250 * @copydoc Dali::Graphics::GetBufferMemoryRequirements()
252 MemoryRequirements GetBufferMemoryRequirements(Buffer& buffer) const override
258 * @copydoc Dali::Graphics::GetTextureProperties()
260 const TextureProperties& GetTextureProperties(const Texture& texture) override
262 // for compiler not to moan
263 static TextureProperties dummy{};
268 * @copydoc Dali::Graphics::Controller::GetPipelineReflection()
271 [[nodiscard]] const Reflection& GetProgramReflection(const Graphics::Program& program) override;
274 * @copydoc Dali::Graphics::PipelineEquals()
276 [[nodiscard]] bool PipelineEquals(const Pipeline& pipeline0, const Pipeline& pipeline1) const override
281 [[nodiscard]] Integration::GlAbstraction* GetGL() const
283 return mGlAbstraction;
287 void AddTexture(GLES::Texture& texture);
290 * @brief Adds buffer to the creation queue
293 void AddBuffer(GLES::Buffer& buffer);
296 * @brief Pushes Bufer to the discard queue
298 * Function is called from the UniquePtr custom deleter.
300 * @param[in] texture Pointer to the texture
302 void DiscardResource(GLES::Texture* texture)
304 mDiscardTextureQueue.push(texture);
308 * @brief Pushes Buffer to the discard queue
310 * Function is called from the UniquePtr custom deleter.
312 * @param[in] texture Pointer to the texture
314 void DiscardResource(GLES::Buffer* texture)
316 mDiscardBufferQueue.push(texture);
320 * @brief Pushes Program to the discard queue
322 * Function is called from the UniquePtr custom deleter.
324 * @param[in] program Pointer to the program
326 void DiscardResource(GLES::Program* program)
328 mDiscardProgramQueue.push(program);
332 * @brief Pushes Pipeline to the discard queue
334 * Function is called from the UniquePtr custom deleter.
336 * @param[in] program Pointer to the pipeline
338 void DiscardResource(GLES::Pipeline* pipeline)
340 mDiscardPipelineQueue.push(pipeline);
344 * @brief Flushes all pending updates
346 * Function flushes all pending resource constructions,
347 * executes command buffers and empties discard queues.
352 ProcessCreateQueues();
355 ProcessTextureUpdateQueue();
357 // Process main command queue
358 ProcessCommandQueues();
361 ProcessDiscardQueues();
363 // Flush pipeline cache to remove unused pipelines
364 GetPipelineCache().FlushCache();
367 // Test update to tick controller, usually it will run own thread
368 void ProcessDiscardQueues();
371 * @brief Processes a create queue for type specified
373 * @param[in,out] queue Reference to the create queue
376 void ProcessCreateQueue(T& queue)
378 while(!queue.empty())
380 auto* object = queue.front();
383 // Initialize texture
384 if(!object->InitializeResource())
386 // TODO: handle error
392 * @brief Processes a create queue for type specified
394 * @param[in,out] queue Reference to the create queue
396 template<class U, class T>
397 void ProcessDiscardQueue(T& queue)
399 while(!queue.empty())
401 auto* object = queue.front();
404 object->DestroyResource();
407 auto* clbk = object->GetCreateInfo().allocationCallbacks;
414 clbk->freeCallback(object, clbk->userData);
425 * @brief Processes all resource create queues
427 void ProcessCreateQueues();
430 * @brief Process command queues and buffers
432 void ProcessCommandQueues();
435 * @brief Executes all pending texture updates
437 void ProcessTextureUpdateQueue();
440 * @brief Returns program custom parameter
442 * This function can be used as a backdoor in order to retrieve
443 * certain data out of implementation
445 * @param[in] program Valid Program object
446 * @param parameterId Integer id of parameter
447 * @param outData Output data
448 * @return True if parameter retrieved
450 bool GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData) override;
453 * @brief Returns pipeline cache object
455 * @return Valid pipeline cache object
457 [[nodiscard]] GLES::PipelineCache& GetPipelineCache() const;
460 Integration::GlAbstraction* mGlAbstraction{nullptr};
461 Integration::GlSyncAbstraction* mGlSyncAbstraction{nullptr};
462 Integration::GlContextHelperAbstraction* mGlContextHelperAbstraction{nullptr};
464 std::queue<GLES::Texture*> mCreateTextureQueue; ///< Create queue for texture resource
465 std::queue<GLES::Texture*> mDiscardTextureQueue; ///< Discard queue for texture resource
467 std::queue<GLES::Buffer*> mCreateBufferQueue; ///< Create queue for buffer resource
468 std::queue<GLES::Buffer*> mDiscardBufferQueue; ///< Discard queue for buffer resource
470 std::queue<GLES::Program*> mDiscardProgramQueue; ///< Discard queue for program resource
472 std::queue<GLES::Pipeline*> mDiscardPipelineQueue; ///< Discard queue of pipelines
474 std::queue<GLES::CommandBuffer*> mCommandQueue; ///< we may have more in the future
476 using TextureUpdateRequest = std::pair<TextureUpdateInfo, TextureUpdateSourceInfo>;
477 std::queue<TextureUpdateRequest> mTextureUpdateRequests;
479 std::unique_ptr<GLES::Context> mContext{nullptr}; ///< Context object handling command buffers execution
481 std::unique_ptr<GLES::PipelineCache> mPipelineCache{nullptr}; ///< Internal pipeline cache
484 } // namespace Graphics
488 #endif //DALI_EGL_GRAPHICS_CONTROLLER_H