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