Multi-level context caching
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-context-state-cache.h
1 #ifndef DALI_GRAPHICS_GLES_CONTEXT_STATE_CACHE_H
2 #define DALI_GRAPHICS_GLES_CONTEXT_STATE_CACHE_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
21 // EXTERNAL INCLUDES
22 #include <dali/graphics-api/graphics-types.h>
23 #include <dali/integration-api/gl-abstraction.h>
24 #include <dali/integration-api/gl-defines.h>
25
26 // INTERNAL INCLUDES
27 #include "gles-framebuffer-state-cache.h"
28
29 namespace Dali::Graphics
30 {
31 namespace GLES
32 {
33 namespace
34 {
35 static constexpr unsigned int MAX_TEXTURE_UNITS        = 32; // As what is defined in gl-defines.h, which is more than DALi uses anyways
36 static constexpr unsigned int MAX_TEXTURE_TARGET       = 4;  // We only support GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP and GL_TEXTURE_EXTERNAL_OES
37 static constexpr unsigned int MAX_ATTRIBUTE_CACHE_SIZE = 8;  // Size of the VertexAttributeArray enables
38 } // namespace
39
40 /**
41  * @brief Cache of GL State per context
42  */
43 struct GLStateCache
44 {
45   /**
46    * Reset the cached texture ids.
47    */
48   void ResetTextureCache()
49   {
50     // reset the cached texture id's in case the driver re-uses them
51     // when creating new textures
52     for(unsigned int i = 0; i < MAX_TEXTURE_UNITS; ++i)
53     {
54       for(unsigned int j = 0; j < MAX_TEXTURE_TARGET; ++j)
55       {
56         mBoundTextureId[i][j] = 0;
57       }
58     }
59   }
60
61   /**
62    * Reset the cached buffer ids.
63    */
64   void ResetBufferCache()
65   {
66     // reset the cached buffer id's
67     // fixes problem where some drivers will a generate a buffer with the
68     // same id, as the last deleted buffer id.
69     mBoundArrayBufferId        = 0;
70     mBoundElementArrayBufferId = 0;
71   }
72
73   /**
74    * @return true if next draw operation will write to depth buffer
75    */
76   bool DepthBufferWriteEnabled() const
77   {
78     return mDepthBufferEnabled && mDepthMaskEnabled;
79   }
80
81   /**
82    * @return true if next draw operation will write to stencil buffer
83    */
84   bool StencilBufferWriteEnabled() const
85   {
86     return mStencilBufferEnabled && (mStencilMask > 0);
87   }
88
89   // glEnable/glDisable states
90   bool   mColorMask{true};
91   GLuint mStencilMask{0xFF};
92   bool   mBlendEnabled{false};
93   bool   mDepthBufferEnabled{false};
94   bool   mDepthMaskEnabled{false};
95   bool   mScissorTestEnabled{false};
96   bool   mStencilBufferEnabled{false};
97   bool   mClearColorSet{false};
98
99   // glBindBuffer() state
100   GLuint mBoundArrayBufferId{0};        ///< The ID passed to glBindBuffer(GL_ARRAY_BUFFER)
101   GLuint mBoundElementArrayBufferId{0}; ///< The ID passed to glBindBuffer(GL_ELEMENT_ARRAY_BUFFER)
102
103   // glBindTexture() state
104   GLenum mActiveTextureUnit{MAX_TEXTURE_UNITS};
105   GLuint mBoundTextureId[MAX_TEXTURE_UNITS][MAX_TEXTURE_TARGET]; ///< The ID passed to glBindTexture()
106
107   // glBlendFuncSeparate() state
108   BlendFactor mBlendFuncSeparateSrcRGB{BlendFactor::ONE};    ///< The srcRGB parameter passed to glBlendFuncSeparate()
109   BlendFactor mBlendFuncSeparateDstRGB{BlendFactor::ZERO};   ///< The dstRGB parameter passed to glBlendFuncSeparate()
110   BlendFactor mBlendFuncSeparateSrcAlpha{BlendFactor::ONE};  ///< The srcAlpha parameter passed to glBlendFuncSeparate()
111   BlendFactor mBlendFuncSeparateDstAlpha{BlendFactor::ZERO}; ///< The dstAlpha parameter passed to glBlendFuncSeparate()
112
113   // glBlendEquationSeparate state
114   BlendOp mBlendEquationSeparateModeRGB{BlendOp::ADD};   ///< Controls RGB blend mode
115   BlendOp mBlendEquationSeparateModeAlpha{BlendOp::ADD}; ///< Controls Alpha blend mode
116
117   // glStencilFunc() and glStencilOp() state.
118   CompareOp mStencilFunc{CompareOp::ALWAYS};
119   GLuint    mStencilFuncRef{0};
120   GLuint    mStencilFuncMask{0xFFFFFFFF};
121   StencilOp mStencilOpFail{StencilOp::KEEP};
122   StencilOp mStencilOpDepthFail{StencilOp::KEEP};
123   StencilOp mStencilOpDepthPass{StencilOp::KEEP};
124
125   CompareOp mDepthFunction{CompareOp::LESS}; ///The depth function
126
127   Vector4 mClearColor{Color::WHITE}; ///< clear color. never used until it's been set by the user
128
129   CullMode mCullFaceMode{CullMode::NONE}; ///< Face culling mode
130
131   // Vertex Attribute Buffer enable caching
132   bool mVertexAttributeCachedState[MAX_ATTRIBUTE_CACHE_SIZE];  ///< Value cache for Enable Vertex Attribute
133   bool mVertexAttributeCurrentState[MAX_ATTRIBUTE_CACHE_SIZE]; ///< Current state on the driver for Enable Vertex Attribute
134
135   FrameBufferStateCache mFrameBufferStateCache{}; ///< frame buffer state cache
136 };
137
138 } // namespace GLES
139
140 } // namespace Dali::Graphics
141
142 #endif // DALI_GRAPHICS_GLES_CONTEXT_STATE_CACHE_H