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