Merge branch 'devel/master' into devel/graphics
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / egl-graphics-controller.h
1 #ifndef DALI_EGL_GRAPHICS_CONTROLLER_H
2 #define DALI_EGL_GRAPHICS_CONTROLLER_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 // EXTERNAL INCLUDES
21 #include <dali/graphics-api/graphics-controller.h>
22 #include <queue>
23
24 // INTERNAL INCLUDES
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"
32
33 namespace Dali
34 {
35 namespace Integration
36 {
37 class GlAbstraction;
38 class GlSyncAbstraction;
39 class GlContextHelperAbstraction;
40 } // namespace Integration
41
42 namespace Graphics
43 {
44 namespace GLES
45 {
46 class CommandBuffer;
47 class PipelineCache;
48 } // namespace GLES
49
50 /**
51  * EGL Implementation of the graphics controller.
52  *
53  * Temporarily holds the old GL abstractions whilst dali-core is migrated to the new API.
54  */
55 DALI_IMPORT_API class EglGraphicsController : public Graphics::Controller
56 {
57 public:
58   /**
59    * @brief Deault constructor
60    */
61   EglGraphicsController() = default;
62
63   /**
64    * @brief Destructor
65    */
66   ~EglGraphicsController() override = default;
67
68   /**
69    * Initialize the GLES abstraction. This can be called from the main thread.
70    */
71   void InitializeGLES(Integration::GlAbstraction& glAbstraction);
72
73   /**
74    * Initialize with a reference to the GL abstractions.
75    *
76    * Note, this is now executed in the render thread, after core initialization
77    */
78   void Initialize(Integration::GlSyncAbstraction&          glSyncAbstraction,
79                   Integration::GlContextHelperAbstraction& glContextHelperAbstraction);
80
81   Integration::GlAbstraction&              GetGlAbstraction() override;
82   Integration::GlSyncAbstraction&          GetGlSyncAbstraction() override;
83   Integration::GlContextHelperAbstraction& GetGlContextHelperAbstraction() override;
84
85   /**
86    * @copydoc Dali::Graphics::SubmitCommandBuffers()
87    */
88   void SubmitCommandBuffers(const SubmitInfo& submitInfo) override;
89
90   /**
91    * @copydoc Dali::Graphics::PresentRenderTarget()
92    */
93   void PresentRenderTarget(RenderTarget* renderTarget) override
94   {
95   }
96
97   /**
98    * @copydoc Dali::Graphics::WaitIdle()
99    */
100   void WaitIdle() override
101   {
102   }
103
104   /**
105    * @copydoc Dali::Graphics::Pause()
106    */
107   void Pause() override
108   {
109   }
110
111   /**
112    * @copydoc Dali::Graphics::Resume()
113    */
114   void Resume() override
115   {
116   }
117
118   /**
119    * @copydoc Dali::Graphics::UpdateTextures()
120    */
121   void UpdateTextures(const std::vector<TextureUpdateInfo>&       updateInfoList,
122                       const std::vector<TextureUpdateSourceInfo>& sourceList) override;
123
124   /**
125    * @copydoc Dali::Graphics::EnableDepthStencilBuffer()
126    */
127   bool EnableDepthStencilBuffer(bool enableDepth, bool enableStencil) override
128   {
129     return {};
130   }
131
132   /**
133    * @copydoc Dali::Graphics::RunGarbageCollector()
134    */
135   void RunGarbageCollector(size_t numberOfDiscardedRenderers) override
136   {
137   }
138
139   /**
140    * @copydoc Dali::Graphics::DiscardUnusedResources()
141    */
142   void DiscardUnusedResources() override
143   {
144   }
145
146   /**
147    * @copydoc Dali::Graphics::IsDiscardQueueEmpty()
148    */
149   bool IsDiscardQueueEmpty() override
150   {
151     return {};
152   }
153
154   /**
155    * @copydoc Dali::Graphics::IsDrawOnResumeRequired()
156    */
157   bool IsDrawOnResumeRequired() override
158   {
159     return {};
160   }
161
162   /**
163    * @copydoc Dali::Graphics::CreateBuffer()
164    */
165   Graphics::UniquePtr<Buffer> CreateBuffer(const BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Buffer>&& oldBuffer) override;
166
167   /**
168    * @copydoc Dali::Graphics::CreateCommandBuffer()
169    */
170   Graphics::UniquePtr<CommandBuffer> CreateCommandBuffer(const CommandBufferCreateInfo& commandBufferCreateInfo, Graphics::UniquePtr<CommandBuffer>&& oldCommandBuffer) override;
171
172   /**
173    * @copydoc Dali::Graphics::CreateRenderPass()
174    */
175   Graphics::UniquePtr<RenderPass> CreateRenderPass(const RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<RenderPass>&& oldRenderPass) override
176   {
177     return nullptr;
178   }
179
180   /**
181    * @copydoc Dali::Graphics::CreateTexture()
182    */
183   Graphics::UniquePtr<Texture> CreateTexture(const TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Texture>&& oldTexture) override;
184
185   /**
186    * @copydoc Dali::Graphics::CreateFramebuffer()
187    */
188   Graphics::UniquePtr<Framebuffer> CreateFramebuffer(const FramebufferCreateInfo& framebufferCreateInfo, Graphics::UniquePtr<Framebuffer>&& oldFramebuffer) override
189   {
190     return nullptr;
191   }
192
193   /**
194    * @copydoc Dali::Graphics::CreatePipeline()
195    */
196   Graphics::UniquePtr<Pipeline> CreatePipeline(const PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Pipeline>&& oldPipeline) override;
197
198   /**
199    * @copydoc Dali::Graphics::CreateProgram()
200    */
201   Graphics::UniquePtr<Program> CreateProgram(const ProgramCreateInfo& programCreateInfo, UniquePtr<Program>&& oldProgram) override;
202
203   /**
204    * @copydoc Dali::Graphics::CreateShader()
205    */
206   Graphics::UniquePtr<Shader> CreateShader(const ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Shader>&& oldShader) override;
207
208   /**
209    * @copydoc Dali::Graphics::CreateSampler()
210    */
211   Graphics::UniquePtr<Sampler> CreateSampler(const SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Sampler>&& oldSampler) override
212   {
213     return nullptr;
214   }
215
216   /**
217    * @copydoc Dali::Graphics::CreateRenderTarget()
218    */
219   Graphics::UniquePtr<RenderTarget> CreateRenderTarget(const RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<RenderTarget>&& oldRenderTarget) override
220   {
221     return nullptr;
222   }
223
224   /**
225    * @copydoc Dali::Graphics::MapBufferRange()
226    */
227   Graphics::UniquePtr<Memory> MapBufferRange(const MapBufferInfo& mapInfo) override;
228
229   /**
230    * @copydoc Dali::Graphics::MapTextureRange()
231    */
232   Graphics::UniquePtr<Memory> MapTextureRange(const MapTextureInfo& mapInfo) override
233   {
234     return nullptr;
235   }
236
237   /**
238    * @copydoc Dali::Graphics::UnmapMemory()
239    */
240   void UnmapMemory(Graphics::UniquePtr<Memory> memory) override
241   {
242   }
243   /**
244    * @copydoc Dali::Graphics::GetTextureMemoryRequirements()
245    */
246   MemoryRequirements GetTextureMemoryRequirements(Texture& texture) const override
247   {
248     return {};
249   }
250
251   /**
252    * @copydoc Dali::Graphics::GetBufferMemoryRequirements()
253    */
254   MemoryRequirements GetBufferMemoryRequirements(Buffer& buffer) const override
255   {
256     return {};
257   }
258
259   /**
260    * @copydoc Dali::Graphics::GetTextureProperties()
261    */
262   const TextureProperties& GetTextureProperties(const Texture& texture) override
263   {
264     // for compiler not to moan
265     static TextureProperties dummy{};
266     return dummy;
267   }
268
269   /**
270    * @copydoc Dali::Graphics::Controller::GetPipelineReflection()
271    */
272
273   [[nodiscard]] const Reflection& GetProgramReflection(const Graphics::Program& program) override;
274
275   /**
276    * @copydoc Dali::Graphics::PipelineEquals()
277    */
278   [[nodiscard]] bool PipelineEquals(const Pipeline& pipeline0, const Pipeline& pipeline1) const override
279   {
280     return {};
281   }
282
283   [[nodiscard]] Integration::GlAbstraction* GetGL() const
284   {
285     return mGlAbstraction;
286   }
287
288   // Internal
289   void AddTexture(GLES::Texture& texture);
290
291   /**
292    * @brief Adds buffer to the creation queue
293    * @param buffer
294    */
295   void AddBuffer(GLES::Buffer& buffer);
296
297   /**
298    * @brief Pushes Bufer to the discard queue
299    *
300    * Function is called from the UniquePtr custom deleter.
301    *
302    * @param[in] texture Pointer to the texture
303    */
304   void DiscardResource(GLES::Texture* texture)
305   {
306     mDiscardTextureQueue.push(texture);
307   }
308
309   /**
310    * @brief Pushes Buffer to the discard queue
311    *
312    * Function is called from the UniquePtr custom deleter.
313    *
314    * @param[in] texture Pointer to the texture
315    */
316   void DiscardResource(GLES::Buffer* texture)
317   {
318     mDiscardBufferQueue.push(texture);
319   }
320
321   /**
322    * @brief Pushes Program to the discard queue
323    *
324    * Function is called from the UniquePtr custom deleter.
325    *
326    * @param[in] program Pointer to the program
327    */
328   void DiscardResource(GLES::Program* program)
329   {
330     mDiscardProgramQueue.push(program);
331   }
332
333   /**
334    * @brief Pushes Pipeline to the discard queue
335    *
336    * Function is called from the UniquePtr custom deleter.
337    *
338    * @param[in] program Pointer to the pipeline
339    */
340   void DiscardResource(GLES::Pipeline* pipeline)
341   {
342     mDiscardPipelineQueue.push(pipeline);
343   }
344
345   /**
346    * @brief Flushes all pending updates
347    *
348    * Function flushes all pending resource constructions,
349    * executes command buffers and empties discard queues.
350    */
351   void Flush()
352   {
353     // Process creations
354     ProcessCreateQueues();
355
356     // Process updates
357     ProcessTextureUpdateQueue();
358
359     // Process main command queue
360     ProcessCommandQueues();
361
362     // Process discards
363     ProcessDiscardQueues();
364
365     // Flush pipeline cache to remove unused pipelines
366     GetPipelineCache().FlushCache();
367   }
368
369   // Test update to tick controller, usually it will run own thread
370   void ProcessDiscardQueues();
371
372   /**
373    * @brief Processes a create queue for type specified
374    *
375    * @param[in,out] queue Reference to the create queue
376    */
377   template<class T>
378   void ProcessCreateQueue(T& queue)
379   {
380     while(!queue.empty())
381     {
382       auto* object = queue.front();
383       queue.pop();
384
385       // Initialize texture
386       if(!object->InitializeResource())
387       {
388         // TODO: handle error
389       }
390     }
391   }
392
393   /**
394    * @brief Processes a create queue for type specified
395    *
396    * @param[in,out] queue Reference to the create queue
397    */
398   template<class U, class T>
399   void ProcessDiscardQueue(T& queue)
400   {
401     while(!queue.empty())
402     {
403       auto* object = queue.front();
404
405       // Destroy
406       object->DestroyResource();
407
408       // Free
409       auto* clbk = object->GetCreateInfo().allocationCallbacks;
410       if(clbk)
411       {
412         // Call destructor
413         object->~U();
414
415         // Free memory
416         clbk->freeCallback(object, clbk->userData);
417       }
418       else
419       {
420         delete object;
421       }
422       queue.pop();
423     }
424   }
425
426   /**
427    * @brief Processes all resource create queues
428    */
429   void ProcessCreateQueues();
430
431   /**
432    * @brief Process command queues and buffers
433    */
434   void ProcessCommandQueues();
435
436   /**
437    * @brief Executes all pending texture updates
438    */
439   void ProcessTextureUpdateQueue();
440
441   /**
442    * @brief Returns program custom parameter
443    *
444    * This function can be used as a backdoor in order to retrieve
445    * certain data out of implementation
446    *
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
451    */
452   bool GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData) override;
453
454   /**
455    * @brief Returns pipeline cache object
456    *
457    * @return Valid pipeline cache object
458    */
459   [[nodiscard]] GLES::PipelineCache& GetPipelineCache() const;
460
461 private:
462   Integration::GlAbstraction*              mGlAbstraction{nullptr};
463   Integration::GlSyncAbstraction*          mGlSyncAbstraction{nullptr};
464   Integration::GlContextHelperAbstraction* mGlContextHelperAbstraction{nullptr};
465
466   std::queue<GLES::Texture*> mCreateTextureQueue;  ///< Create queue for texture resource
467   std::queue<GLES::Texture*> mDiscardTextureQueue; ///< Discard queue for texture resource
468
469   std::queue<GLES::Buffer*> mCreateBufferQueue;  ///< Create queue for buffer resource
470   std::queue<GLES::Buffer*> mDiscardBufferQueue; ///< Discard queue for buffer resource
471
472   std::queue<GLES::Program*> mDiscardProgramQueue; ///< Discard queue for program resource
473
474   std::queue<GLES::Pipeline*> mDiscardPipelineQueue; ///< Discard queue of pipelines
475
476   std::queue<GLES::CommandBuffer*> mCommandQueue; ///< we may have more in the future
477
478   using TextureUpdateRequest = std::pair<TextureUpdateInfo, TextureUpdateSourceInfo>;
479   std::queue<TextureUpdateRequest> mTextureUpdateRequests;
480
481   std::unique_ptr<GLES::Context> mContext{nullptr}; ///< Context object handling command buffers execution
482
483   std::unique_ptr<GLES::PipelineCache> mPipelineCache{nullptr}; ///< Internal pipeline cache
484 };
485
486 } // namespace Graphics
487
488 } // namespace Dali
489
490 #endif //DALI_EGL_GRAPHICS_CONTROLLER_H