Cleaned up some of the circular dependency between GLES::Context & GLES::GraphicsCont...
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-context.h
1 #ifndef DALI_GRAPHICS_GLES_CONTEXT_H
2 #define DALI_GRAPHICS_GLES_CONTEXT_H
3
4 /*
5  * Copyright (c) 2024 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
21 #include <dali/graphics-api/graphics-command-buffer.h>
22 #include "gles-context-state-cache.h"
23 #include "gles-graphics-types.h"
24
25 namespace Dali::Graphics
26 {
27 class EglGraphicsController;
28 namespace GLES
29 {
30 class Pipeline;
31 class RenderPass;
32 class RenderTarget;
33 class Texture;
34 class TextureDependencyChecker;
35
36 /**
37  * @brief Context represents single GLES context
38  */
39 class Context
40 {
41 public:
42   explicit Context(EglGraphicsController& controller, Integration::GlAbstraction* glAbstraction);
43
44   ~Context();
45
46   /**
47    * @brief Flushes the context
48    *
49    * Flushes the context by issuing GL calls to set the required
50    * state. Causes a glWaitSync if any drawn textures are dependent
51    * on another context.
52    *
53    * @param[in] reset If true then state is reset unconditionally
54    * @param[in] drawCall the draws that need flushing
55    * @param[in] dependencyChecker The texture dependecy checker
56    */
57   void Flush(bool reset, const GLES::DrawCallDescriptor& drawCall, GLES::TextureDependencyChecker& dependencyChecker);
58
59   /**
60    * @brief Returns context Id
61    * @return
62    */
63   uint32_t GetContextId() const
64   {
65     return 0;
66   }
67
68   /**
69    * @brief Binds textures to the context
70    *
71    * Bindings are merged, they are resolved at later time
72    * when the draw call is issued. Then the texture binding
73    * slots (GL active texture binding) are enabled/disabled accordingly
74    * to the uniform layout.
75    *
76    * @param[in] bindings List of bindings
77    *
78    */
79   void BindTextures(const Graphics::TextureBinding* bindings, uint32_t count);
80
81   /**
82    * @brief Vertex buffers to bind
83    *
84    * The bindings are taken from a command buffer being
85    * currently processed and copied into the local storage.
86    */
87
88   void BindVertexBuffers(const GLES::VertexBufferBindingDescriptor* bindings, uint32_t count);
89
90   /**
91    * @brief Binds index buffer
92    *
93    * @param indexBufferBinding
94    */
95   void BindIndexBuffer(const IndexBufferBindingDescriptor& indexBufferBinding);
96
97   /**
98    * @brief Binds pipeline to the context
99    * @param newPipeline
100    */
101   void BindPipeline(const GLES::Pipeline* newPipeline);
102
103   /**
104    * @brief Binds uniform buffers to the context
105    *
106    * @param[in] uboBindings real UBO binfins
107    * @param[in] standaloneBindings emulated (legacy) UBO object
108    */
109   void BindUniformBuffers(const UniformBufferBindingDescriptor* uboBindings, uint32_t uboCount, const UniformBufferBindingDescriptor& standaloneBindings);
110
111   /**
112    * @brief Resolves blend state on the currently attached pipeline
113    */
114   void ResolveBlendState();
115
116   /**
117    * @brief Resolves rasterization state on the currently attached pipeline
118    */
119   void ResolveRasterizationState();
120
121   /**
122    * @brief Resolves uniform buffers and binds data to the pipeline
123    */
124   void ResolveUniformBuffers();
125
126   /**
127    * @brief Special usecase for legacy shaders, called by ResolveUniformBuffers()
128    */
129   void ResolveStandaloneUniforms();
130
131   /**
132    * @brief Resolves GPU-based uniform buffers
133    */
134   void ResolveGpuUniformBuffers();
135
136   /**
137    * @brief Begins render pass for specified render target
138    *
139    * @param[in] renderPass render pass object to begin
140    */
141   void BeginRenderPass(const BeginRenderPassDescriptor& renderPassBegin);
142
143   /**
144    * @brief Ends render pass
145    *
146    * Ending render pass is necessary in order to ensure
147    * proper explicit synchronization is in place
148    */
149   void EndRenderPass(TextureDependencyChecker& checker);
150
151   /**
152    * @brief Returns the cache of GL state in the context
153    * @return the reference of GL state cache (which can be modified)
154    */
155   GLStateCache& GetGLStateCache();
156
157   /**
158    * @brief Called when the GL context has been created.
159    */
160   void GlContextCreated();
161
162   /**
163    * @brief Called when the GL context has been destroyed.
164    */
165   void GlContextDestroyed();
166
167   /**
168    * @brief Invalidates the cached pipeline object in the context if it matches
169    * This is called before the pipeline is deleted
170    *
171    * @param[in] pipeline The pipeline
172    */
173   void InvalidateCachedPipeline(GLES::Pipeline* pipeline);
174
175   /**
176    * @brief Sets up EGL context for native rendering
177    *
178    * - The native rendering uses dedicated context
179    * - There is one EGL native rendering context per GLES::Context object
180    * - Native rendering context is compatible with the window/surface context
181    * - Native rendering context dies with GLES::Context object
182    *
183    * When native rendering is about to be executed, the dedicated EGL context
184    * is acquired (created or reused) and made current. The Window/Surface context
185    * is cached to be restored afterwards.
186    */
187   void PrepareForNativeRendering();
188
189   /**
190    * @brief Restores window/surface context after native rendering.
191    */
192   void RestoreFromNativeRendering();
193
194   void ActiveTexture(uint32_t textureBindingIndex);
195   void BindTexture(GLenum target, BoundTextureType textureTypeId, uint32_t textureId);
196   void GenerateMipmap(GLenum target);
197
198   /**
199    * Binds the buffer.
200    * @return true if the buffer was actually bound, false if it's cached
201    */
202   bool BindBuffer(GLenum target, uint32_t bufferId);
203
204   void DrawBuffers(uint32_t count, const GLenum* buffers);
205   void BindFrameBuffer(GLenum target, uint32_t bufferId);
206   void GenFramebuffers(uint32_t count, uint32_t* framebuffers);
207   void DeleteFramebuffers(uint32_t count, uint32_t* framebuffers);
208   void ColorMask(bool enabled);
209   void ClearStencilBuffer();
210   void ClearDepthBuffer();
211   void ClearBuffer(uint32_t mask, bool forceClear);
212   void InvalidateDepthStencilBuffers();
213   void SetScissorTestEnabled(bool scissorEnabled);
214   void SetStencilTestEnable(bool stencilEnable);
215   void StencilMask(uint32_t writeMask);
216   void StencilFunc(Graphics::CompareOp compareOp,
217                    uint32_t            reference,
218                    uint32_t            compareMask);
219   void StencilOp(Graphics::StencilOp failOp,
220                  Graphics::StencilOp depthFailOp,
221                  Graphics::StencilOp passOp);
222   void SetDepthCompareOp(Graphics::CompareOp compareOp);
223   void SetDepthTestEnable(bool depthTestEnable);
224   void SetDepthWriteEnable(bool depthWriteEnable);
225
226 private:
227   /**
228    * @brief Clear current state
229    */
230   void ClearState();
231
232 private:
233   struct Impl;
234   std::unique_ptr<Impl> mImpl;
235 };
236 } // namespace GLES
237 } // namespace Dali::Graphics
238 #endif //DALI_GRAPHICS_GLES_CONTEXT_H