Added RenderPass and RenderTarget support
[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-command-buffer.h"
28 #include "gles-graphics-framebuffer.h"
29 #include "gles-graphics-pipeline-cache.h"
30 #include "gles-graphics-pipeline.h"
31 #include "gles-graphics-reflection.h"
32 #include "gles-graphics-sampler.h"
33 #include "gles-graphics-shader.h"
34 #include "gles-graphics-texture.h"
35 #include "gles-graphics-types.h"
36 #include "gles2-graphics-memory.h"
37
38 namespace Dali
39 {
40 namespace Integration
41 {
42 class GlAbstraction;
43 class GlSyncAbstraction;
44 class GlContextHelperAbstraction;
45 } // namespace Integration
46
47 namespace Graphics
48 {
49 namespace GLES
50 {
51 class CommandBuffer;
52 class PipelineCache;
53 } // namespace GLES
54
55 /**
56  * EGL Implementation of the graphics controller.
57  *
58  * Temporarily holds the old GL abstractions whilst dali-core is migrated to the new API.
59  */
60 DALI_IMPORT_API class EglGraphicsController : public Graphics::Controller
61 {
62 public:
63   /**
64    * @brief Deault constructor
65    */
66   EglGraphicsController() = default;
67
68   /**
69    * @brief Destructor
70    */
71   ~EglGraphicsController() override;
72
73   /**
74    * Initialize the GLES abstraction. This can be called from the main thread.
75    */
76   void InitializeGLES(Integration::GlAbstraction& glAbstraction);
77
78   /**
79    * Initialize with a reference to the GL abstractions.
80    *
81    * Note, this is now executed in the render thread, after core initialization
82    */
83   void Initialize(Integration::GlSyncAbstraction&          glSyncAbstraction,
84                   Integration::GlContextHelperAbstraction& glContextHelperAbstraction);
85
86   Integration::GlAbstraction&              GetGlAbstraction() override;
87   Integration::GlSyncAbstraction&          GetGlSyncAbstraction() override;
88   Integration::GlContextHelperAbstraction& GetGlContextHelperAbstraction() override;
89
90   /**
91    * @copydoc Dali::Graphics::SubmitCommandBuffers()
92    */
93   void SubmitCommandBuffers(const SubmitInfo& submitInfo) override;
94
95   /**
96    * @copydoc Dali::Graphics::PresentRenderTarget()
97    */
98   void PresentRenderTarget(RenderTarget* renderTarget) override
99   {
100   }
101
102   /**
103    * @copydoc Dali::Graphics::WaitIdle()
104    */
105   void WaitIdle() override
106   {
107   }
108
109   /**
110    * @copydoc Dali::Graphics::Pause()
111    */
112   void Pause() override
113   {
114   }
115
116   /**
117    * @copydoc Dali::Graphics::Resume()
118    */
119   void Resume() override
120   {
121   }
122
123   /**
124    * @copydoc Dali::Graphics::Shutdown()
125    */
126   void Shutdown() override
127   {
128     mIsShuttingDown = true;
129   }
130
131   /**
132    * @copydoc Dali::Graphics::Destroy()
133    */
134   void Destroy() override
135   {
136     // Final flush
137     Flush();
138   }
139
140   /**
141    * @copydoc Dali::Graphics::UpdateTextures()
142    */
143   void UpdateTextures(const std::vector<TextureUpdateInfo>&       updateInfoList,
144                       const std::vector<TextureUpdateSourceInfo>& sourceList) override;
145
146   /**
147    * @copydoc Dali::Graphics::EnableDepthStencilBuffer()
148    */
149   bool EnableDepthStencilBuffer(bool enableDepth, bool enableStencil) override
150   {
151     return {};
152   }
153
154   /**
155    * @copydoc Dali::Graphics::RunGarbageCollector()
156    */
157   void RunGarbageCollector(size_t numberOfDiscardedRenderers) override
158   {
159   }
160
161   /**
162    * @copydoc Dali::Graphics::DiscardUnusedResources()
163    */
164   void DiscardUnusedResources() override
165   {
166   }
167
168   /**
169    * @copydoc Dali::Graphics::IsDiscardQueueEmpty()
170    */
171   bool IsDiscardQueueEmpty() override
172   {
173     return {};
174   }
175
176   /**
177    * @copydoc Dali::Graphics::IsDrawOnResumeRequired()
178    */
179   bool IsDrawOnResumeRequired() override
180   {
181     return {};
182   }
183
184   /**
185    * @copydoc Dali::Graphics::CreateBuffer()
186    */
187   Graphics::UniquePtr<Buffer> CreateBuffer(const BufferCreateInfo& bufferCreateInfo, Graphics::UniquePtr<Buffer>&& oldBuffer) override;
188
189   /**
190    * @copydoc Dali::Graphics::CreateCommandBuffer()
191    */
192   Graphics::UniquePtr<CommandBuffer> CreateCommandBuffer(const CommandBufferCreateInfo& commandBufferCreateInfo, Graphics::UniquePtr<CommandBuffer>&& oldCommandBuffer) override;
193
194   /**
195    * @copydoc Dali::Graphics::CreateRenderPass()
196    */
197   Graphics::UniquePtr<RenderPass> CreateRenderPass(const RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<RenderPass>&& oldRenderPass) override;
198
199   /**
200    * @copydoc Dali::Graphics::CreateTexture()
201    */
202   Graphics::UniquePtr<Texture> CreateTexture(const TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Texture>&& oldTexture) override;
203
204   /**
205    * @copydoc Dali::Graphics::CreateFramebuffer()
206    */
207   Graphics::UniquePtr<Framebuffer> CreateFramebuffer(const FramebufferCreateInfo& framebufferCreateInfo, Graphics::UniquePtr<Framebuffer>&& oldFramebuffer) override;
208
209   /**
210    * @copydoc Dali::Graphics::CreatePipeline()
211    */
212   Graphics::UniquePtr<Pipeline> CreatePipeline(const PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Pipeline>&& oldPipeline) override;
213
214   /**
215    * @copydoc Dali::Graphics::CreateProgram()
216    */
217   Graphics::UniquePtr<Program> CreateProgram(const ProgramCreateInfo& programCreateInfo, UniquePtr<Program>&& oldProgram) override;
218
219   /**
220    * @copydoc Dali::Graphics::CreateShader()
221    */
222   Graphics::UniquePtr<Shader> CreateShader(const ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Shader>&& oldShader) override;
223
224   /**
225    * @copydoc Dali::Graphics::CreateSampler()
226    */
227   Graphics::UniquePtr<Sampler> CreateSampler(const SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Sampler>&& oldSampler) override;
228
229   /**
230    * @copydoc Dali::Graphics::CreateRenderTarget()
231    */
232   Graphics::UniquePtr<RenderTarget> CreateRenderTarget(const RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<RenderTarget>&& oldRenderTarget) override;
233
234   /**
235    * @copydoc Dali::Graphics::MapBufferRange()
236    */
237   Graphics::UniquePtr<Memory> MapBufferRange(const MapBufferInfo& mapInfo) override;
238
239   /**
240    * @copydoc Dali::Graphics::MapTextureRange()
241    */
242   Graphics::UniquePtr<Memory> MapTextureRange(const MapTextureInfo& mapInfo) override
243   {
244     return nullptr;
245   }
246
247   /**
248    * @copydoc Dali::Graphics::UnmapMemory()
249    */
250   void UnmapMemory(Graphics::UniquePtr<Memory> memory) override
251   {
252   }
253   /**
254    * @copydoc Dali::Graphics::GetTextureMemoryRequirements()
255    */
256   MemoryRequirements GetTextureMemoryRequirements(Texture& texture) const override
257   {
258     return {};
259   }
260
261   /**
262    * @copydoc Dali::Graphics::GetBufferMemoryRequirements()
263    */
264   MemoryRequirements GetBufferMemoryRequirements(Buffer& buffer) const override
265   {
266     return {};
267   }
268
269   /**
270    * @copydoc Dali::Graphics::GetTextureProperties()
271    */
272   const TextureProperties& GetTextureProperties(const Texture& texture) override
273   {
274     // for compiler not to moan
275     static TextureProperties dummy{};
276     return dummy;
277   }
278
279   /**
280    * @copydoc Dali::Graphics::Controller::GetPipelineReflection()
281    */
282
283   [[nodiscard]] const Reflection& GetProgramReflection(const Graphics::Program& program) override;
284
285   /**
286    * @copydoc Dali::Graphics::PipelineEquals()
287    */
288   [[nodiscard]] bool PipelineEquals(const Pipeline& pipeline0, const Pipeline& pipeline1) const override
289   {
290     return {};
291   }
292
293   [[nodiscard]] Integration::GlAbstraction* GetGL() const
294   {
295     if(mIsShuttingDown)
296     {
297       return nullptr;
298     }
299     return mGlAbstraction;
300   }
301
302   // Internal
303   void AddTexture(GLES::Texture& texture);
304
305   /**
306    * @brief Adds buffer to the creation queue
307    * @param buffer
308    */
309   void AddBuffer(GLES::Buffer& buffer);
310
311   /**
312    * @brief Adds framebuffer to the creation queue
313    * @param buffer
314    */
315   void AddFramebuffer(GLES::Framebuffer& framebuffer);
316
317   /**
318    * @brief Pushes Bufer to the discard queue
319    *
320    * Function is called from the UniquePtr custom deleter.
321    *
322    * @param[in] texture Pointer to the texture
323    */
324   void DiscardResource(GLES::Texture* texture)
325   {
326     mDiscardTextureQueue.push(texture);
327   }
328
329   /**
330    * @brief Pushes Buffer to the discard queue
331    *
332    * Function is called from the UniquePtr custom deleter.
333    *
334    * @param[in] buffer Pointer to the buffer object
335    */
336   void DiscardResource(GLES::Buffer* buffer)
337   {
338     mDiscardBufferQueue.push(buffer);
339   }
340
341   /**
342    * @brief Pushes framebuffer to the discard queue
343    *
344    * Function is called from the UniquePtr custom deleter.
345    *
346    * @param[in] framebuffer Pointer to the framebuffer object
347    */
348   void DiscardResource(GLES::Framebuffer* framebuffer)
349   {
350     mDiscardFramebufferQueue.push(framebuffer);
351   }
352
353   /**
354    * @brief Pushes Program to the discard queue
355    *
356    * Function is called from the UniquePtr custom deleter.
357    *
358    * @param[in] program Pointer to the program
359    */
360   void DiscardResource(GLES::Program* program)
361   {
362     mDiscardProgramQueue.push(program);
363   }
364
365   /**
366    * @brief Pushes Shader to the discard queue
367    *
368    * Function is called from the UniquePtr custom deleter.
369    *
370    * @param[in] program Pointer to the Shader
371    */
372   void DiscardResource(GLES::Shader* shader)
373   {
374     mDiscardShaderQueue.push(shader);
375   }
376
377   /**
378    * @brief Pushes CommandBuffer to the discard queue
379    *
380    * Function is called from the UniquePtr custom deleter.
381    *
382    * @param[in] program Pointer to the CommandBuffer
383    */
384   void DiscardResource(GLES::CommandBuffer* commandBuffer)
385   {
386     mDiscardCommandBufferQueue.push(commandBuffer);
387   }
388
389   /**
390    * @brief Pushes Sampler to the discard queue
391    *
392    * Function is called from the UniquePtr custom deleter.
393    *
394    * @param[in] program Pointer to the Sampler
395    */
396   void DiscardResource(GLES::Sampler* sampler)
397   {
398     mDiscardSamplerQueue.push(sampler);
399   }
400
401   /**
402    * @brief Pushes Pipeline to the discard queue
403    *
404    * Function is called from the UniquePtr custom deleter.
405    *
406    * @param[in] program Pointer to the pipeline
407    */
408   void DiscardResource(GLES::Pipeline* pipeline)
409   {
410     mDiscardPipelineQueue.push(pipeline);
411   }
412
413   /**
414    * @brief Flushes all pending updates
415    *
416    * Function flushes all pending resource constructions,
417    * executes command buffers and empties discard queues.
418    */
419   void Flush()
420   {
421     // Process creations
422     ProcessCreateQueues();
423
424     // Process updates
425     ProcessTextureUpdateQueue();
426
427     // Process main command queue
428     ProcessCommandQueues();
429
430     // Process discards
431     ProcessDiscardQueues();
432
433     // Flush pipeline cache to remove unused pipelines
434     if(mPipelineCache)
435     {
436       mPipelineCache->FlushCache();
437     }
438   }
439
440   // Test update to tick controller, usually it will run own thread
441   void ProcessDiscardQueues();
442
443   /**
444    * @brief Processes a create queue for type specified
445    *
446    * @param[in,out] queue Reference to the create queue
447    */
448   template<class T>
449   void ProcessCreateQueue(T& queue)
450   {
451     while(!queue.empty())
452     {
453       auto* object = queue.front();
454       queue.pop();
455
456       // Initialize texture
457       if(!object->InitializeResource())
458       {
459         // TODO: handle error
460       }
461     }
462   }
463
464   /**
465    * @brief Processes a create queue for type specified
466    *
467    * @param[in,out] queue Reference to the create queue
468    */
469   template<class U, class T>
470   void ProcessDiscardQueue(T& queue)
471   {
472     while(!queue.empty())
473     {
474       auto* object = queue.front();
475
476       // Destroy
477       object->DestroyResource();
478
479       // Free
480       auto* clbk = object->GetCreateInfo().allocationCallbacks;
481       if(clbk)
482       {
483         // Call destructor
484         object->~U();
485
486         // Free memory
487         clbk->freeCallback(object, clbk->userData);
488       }
489       else
490       {
491         delete object;
492       }
493       queue.pop();
494     }
495   }
496
497   /**
498    * @brief Processes all resource create queues
499    */
500   void ProcessCreateQueues();
501
502   /**
503    * @brief Process command queues and buffers
504    */
505   void ProcessCommandQueues();
506
507   /**
508    * @brief Executes all pending texture updates
509    */
510   void ProcessTextureUpdateQueue();
511
512   /**
513    * @brief Returns program custom parameter
514    *
515    * This function can be used as a backdoor in order to retrieve
516    * certain data out of implementation
517    *
518    * @param[in] program Valid Program object
519    * @param parameterId Integer id of parameter
520    * @param outData Output data
521    * @return True if parameter retrieved
522    */
523   bool GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData) override;
524
525   /**
526    * @brief Returns pipeline cache object
527    *
528    * @return Valid pipeline cache object
529    */
530   [[nodiscard]] GLES::PipelineCache& GetPipelineCache() const;
531
532   /**
533    * @brief Returns runtime supported GLES version
534    *
535    * @return GLES version enum
536    */
537   GLES::GLESVersion GetGLESVersion() const
538   {
539     // TODO: return proper version but for now we can
540     // test fallbacks
541     return GLES::GLESVersion::GLES_20;
542   }
543
544   bool IsShuttingDown() const
545   {
546     return mIsShuttingDown;
547   }
548
549   void ProcessCommandBuffer(GLES::CommandBuffer& commandBuffer);
550
551 private:
552   Integration::GlAbstraction*              mGlAbstraction{nullptr};
553   Integration::GlSyncAbstraction*          mGlSyncAbstraction{nullptr};
554   Integration::GlContextHelperAbstraction* mGlContextHelperAbstraction{nullptr};
555
556   std::queue<GLES::Texture*> mCreateTextureQueue;  ///< Create queue for texture resource
557   std::queue<GLES::Texture*> mDiscardTextureQueue; ///< Discard queue for texture resource
558
559   std::queue<GLES::Buffer*> mCreateBufferQueue;  ///< Create queue for buffer resource
560   std::queue<GLES::Buffer*> mDiscardBufferQueue; ///< Discard queue for buffer resource
561
562   std::queue<GLES::Program*>       mDiscardProgramQueue;       ///< Discard queue for program resource
563   std::queue<GLES::Pipeline*>      mDiscardPipelineQueue;      ///< Discard queue of pipelines
564   std::queue<GLES::Shader*>        mDiscardShaderQueue;        ///< Discard queue of shaders
565   std::queue<GLES::Sampler*>       mDiscardSamplerQueue;       ///< Discard queue of samplers
566   std::queue<GLES::CommandBuffer*> mDiscardCommandBufferQueue; ///< Discard queue of command buffers
567   std::queue<GLES::Framebuffer*>   mCreateFramebufferQueue;    ///< Create queue for framebuffer resource
568   std::queue<GLES::Framebuffer*>   mDiscardFramebufferQueue;   ///< Discard queue for framebuffer resource
569
570   std::queue<GLES::CommandBuffer*> mCommandQueue; ///< we may have more in the future
571
572   using TextureUpdateRequest = std::pair<TextureUpdateInfo, TextureUpdateSourceInfo>;
573   std::queue<TextureUpdateRequest> mTextureUpdateRequests;
574
575   std::unique_ptr<GLES::Context> mContext{nullptr}; ///< Context object handling command buffers execution
576
577   std::unique_ptr<GLES::PipelineCache> mPipelineCache{nullptr}; ///< Internal pipeline cache
578
579   bool mIsShuttingDown{false}; ///< Indicates whether the controller is shutting down
580
581   // todo: to be removed after renderpass
582   const Graphics::Framebuffer* currentFramebuffer{nullptr};
583 };
584
585 } // namespace Graphics
586
587 } // namespace Dali
588
589 #endif //DALI_EGL_GRAPHICS_CONTROLLER_H