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