Multi-level context caching
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-framebuffer-state-cache.h
1 #ifndef DALI_GRAPHICS_GLES_CONTEXT_FRAMEBUFFER_STATE_CACHE_H
2 #define DALI_GRAPHICS_GLES_CONTEXT_FRAMEBUFFER_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 // INTERNAL INCLUDES
22 #include <dali/integration-api/gl-abstraction.h>
23 #include <dali/public-api/common/dali-vector.h>
24
25 namespace Dali::Graphics
26 {
27 namespace GLES
28 {
29 /**
30  * @brief Keeps track of color, depth and stencil buffer state within each frame buffer.
31  * Used to avoid redundant glClear calls.
32  *
33  */
34 class FrameBufferStateCache
35 {
36 public:
37   /**
38    * @brief Constructor
39    */
40   FrameBufferStateCache();
41
42   /**
43    * @brief  non-virtual destructor
44    */
45   ~FrameBufferStateCache();
46
47   /**
48    * @brief Get the bitmask to pass to glClear based on the mask requested
49    * and the current state of the frame buffer
50    * @param[in] mask glClear bit mask
51    * @param[in] forceClear whether to force the clear ( ignore cached state)
52    * @param[in] scissorTestEnabled whether scissor test is enabled
53    * @return new bitmask to pass to glClear
54    */
55   GLbitfield GetClearMask(GLbitfield mask, bool forceClear, bool scissorTestEnabled);
56
57   /**
58    * @brief Set the current bound frame buffer
59    * @param[in] frameBufferId frame buffer id
60    */
61   void SetCurrentFrameBuffer(GLuint frameBufferId);
62
63   /**
64    * @brief Called when frame buffers are deleted
65    * @param[in] count number of frame buffers
66    * @param[in] framebuffers array of frame buffer ids
67    */
68   void FrameBuffersDeleted(GLsizei count, const GLuint* const frameBuffers);
69
70   /**
71    * @brief Called when frame buffers are created
72    * @param[in] count number of frame buffers
73    * @param[in] framebuffers array of frame buffer ids
74    */
75   void FrameBuffersCreated(GLsizei count, const GLuint* const frameBuffers);
76
77   /**
78    * @brief Draw operation performed on the current frame buffer
79    * @param[in] colorBufferUsed whether the color buffer is being written to (glColorMask )
80    * @param[in] depthBufferUsed whether the depth buffer is being written to (glDepthMask )
81    * @param[in] stencilBufferUsed whether the stencil buffer is being written to (glStencilMask )
82    */
83   void DrawOperation(bool colorBufferUsed, bool depthBufferUsed, bool stencilBufferUsed);
84
85   /**
86    * Reset the cache
87    */
88   void Reset();
89
90 private:
91   /**
92    * Current status of the frame buffer
93    */
94   enum FrameBufferStatus
95   {
96     COLOR_BUFFER_CLEAN   = 1 << 0, ///< color buffer clean
97     DEPTH_BUFFER_CLEAN   = 1 << 1, ///< depth buffer clean
98     STENCIL_BUFFER_CLEAN = 1 << 2, ///< stencil buffer clean
99   };
100
101   /**
102    * POD to store the status of frame buffer regarding color,depth and stencil buffers
103    */
104   struct FrameBufferState
105   {
106     /**
107      * Constructor
108      */
109     FrameBufferState(GLuint id)
110     : mId(id),
111       mState(0)
112     {
113     }
114     GLuint       mId;    ///< Frame buffer id
115     unsigned int mState; ///< State, bitmask of FrameBufferStatus flags
116   };
117
118   using FrameBufferStateVector = Dali::Vector<FrameBufferState>;
119
120   /**
121    * @brief Set the clear state
122    * @param[in] pointer to frame buffer state object
123    * @param[in] mask clear mask
124    */
125   void SetClearState(FrameBufferState* state, GLbitfield mask);
126
127   /**
128    * @brief Helper
129    * @param[in] frameBufferId frame buffer id
130    * @return pointer to  frame buffer state object ( NULL if it doesn't exist)
131    */
132   FrameBufferState* GetFrameBufferState(GLuint frameBufferId);
133
134   /**
135    * @brief Helper to delete a frame buffer state object
136    * @param[in] frameBufferId frame buffer id
137    */
138   void DeleteFrameBuffer(GLuint frameBufferId);
139
140   FrameBufferStateCache(const FrameBufferStateCache&); ///< undefined copy constructor
141
142   FrameBufferStateCache& operator=(const FrameBufferStateCache&); ///< undefined assignment operator
143
144 private:                                        // data
145   FrameBufferStateVector mFrameBufferStates{};    ///< state of the frame buffers
146   GLuint                 mCurrentFrameBufferId{0}; ///< currently bound frame buffer
147 };
148
149 } // namespace GLES
150
151 } // namespace Dali::Graphics
152
153 #endif // DALI_GRAPHICS_GLES_CONTEXT_FRAMEBUFFER_STATE_CACHE_H