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