Revert "[Tizen] Add codes for Dali Windows Backend"
[platform/core/uifw/dali-core.git] / dali / internal / render / gl-resources / frame-buffer-state-cache.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "frame-buffer-state-cache.h"
20
21 // INTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/gl-defines.h>
24
25 namespace Dali
26 {
27
28 namespace Internal
29 {
30
31 FrameBufferStateCache::FrameBufferStateCache()
32 :mCurrentFrameBufferId(0)
33 {
34 }
35
36 FrameBufferStateCache::~FrameBufferStateCache()
37 {
38 }
39
40 GLbitfield FrameBufferStateCache::GetClearMask( GLbitfield mask, bool forceClear, bool scissorTestEnabled )
41 {
42   if( scissorTestEnabled )
43   {
44     // don't do anything if scissor test is enabled, in the future we could
45     // potentially keep track of frame buffer size vs scissor test size to see if the entire
46     // buffer is cleared or not.
47     return mask;
48   }
49   FrameBufferState* state = GetFrameBufferState( mCurrentFrameBufferId );
50   if( !state )
51   {
52     DALI_LOG_ERROR("FrameBuffer not found %d \n", mCurrentFrameBufferId);
53     return mask;
54   }
55
56   // if we are forcing the clear operation, then just update the internal cached values
57   if( forceClear )
58   {
59     SetClearState( state, mask );
60     return mask;
61   }
62
63   // use the cached values
64   if( mask & GL_COLOR_BUFFER_BIT)
65   {
66     // check if color buffer is currently clean
67     if( state->mState & COLOR_BUFFER_CLEAN )
68     {
69       // remove clear color buffer flag from bitmask, no need to clear twice
70       mask&= ~GL_COLOR_BUFFER_BIT;
71     }
72   }
73   if( mask & GL_DEPTH_BUFFER_BIT)
74   {
75     // check if depth buffer is currently clean
76     if( state->mState & DEPTH_BUFFER_CLEAN )
77     {
78       // remove clear depth buffer flag from bitmask, no need to clear twice
79       mask&= ~GL_DEPTH_BUFFER_BIT;
80     }
81   }
82   if( mask & GL_STENCIL_BUFFER_BIT)
83   {
84     // check if stencil buffer is currently clean
85     if( state->mState & STENCIL_BUFFER_CLEAN )
86     {
87       // remove clear stencil buffer flag from bitmask, no need to clear twice
88
89       mask&= ~GL_STENCIL_BUFFER_BIT;
90     }
91   }
92
93   // set the clear state based, what's about to be cleared
94   SetClearState( state, mask );
95
96   return mask;
97 }
98
99 void FrameBufferStateCache::SetCurrentFrameBuffer( GLuint frameBufferId )
100 {
101   mCurrentFrameBufferId = frameBufferId;
102 }
103
104 void FrameBufferStateCache::FrameBuffersDeleted( GLsizei count, const GLuint* const frameBuffers )
105 {
106   for( GLsizei i = 0; i < count; ++i )
107   {
108     DeleteFrameBuffer( frameBuffers[i] );
109   }
110 }
111 void FrameBufferStateCache::FrameBuffersCreated( GLsizei count, const GLuint* const frameBuffers )
112 {
113   for( GLsizei i = 0; i < count; ++i )
114   {
115     // check the frame buffer doesn't exist already
116     GLuint id = frameBuffers[i];
117
118     FrameBufferState* state =  GetFrameBufferState( id );
119     if( state )
120     {
121       DALI_LOG_ERROR("FrameBuffer already exists%d \n", id );
122       // reset its state
123       state->mState = GetInitialFrameBufferState();
124       continue;
125     }
126
127     FrameBufferState newFrameBuffer( frameBuffers[i], GetInitialFrameBufferState() );
128     mFrameBufferStates.PushBack( newFrameBuffer );
129   }
130 }
131
132 void FrameBufferStateCache::DrawOperation( bool colorBuffer, bool depthBuffer, bool stencilBuffer )
133 {
134   FrameBufferState* state = GetFrameBufferState( mCurrentFrameBufferId );
135   if( !state )
136   {
137     // an error will have already been logged by the clear operation
138     return;
139   }
140
141   if( colorBuffer )
142   {
143     // un-set the clean bit
144     state->mState &= ~COLOR_BUFFER_CLEAN;
145   }
146   if( depthBuffer )
147   {
148     // un-set the clean bit
149     state->mState &= ~DEPTH_BUFFER_CLEAN;
150   }
151   if( stencilBuffer )
152   {
153     // un-set the clean bit
154     state->mState &= ~STENCIL_BUFFER_CLEAN;
155   }
156
157 }
158
159 void FrameBufferStateCache::Reset()
160 {
161   mFrameBufferStates.Clear();
162
163   // create the default frame buffer
164   GLuint id = 0; // 0 == default frame buffer id
165   FrameBuffersCreated( 1, &id );
166 }
167
168 void FrameBufferStateCache::SetClearState( FrameBufferState* state, GLbitfield mask )
169 {
170   if( mask & GL_COLOR_BUFFER_BIT)
171   {
172     // set the color buffer to clean
173     state->mState |= COLOR_BUFFER_CLEAN;
174   }
175   if( mask & GL_DEPTH_BUFFER_BIT)
176   {
177     // set the depth buffer to clean
178     state->mState |= DEPTH_BUFFER_CLEAN;
179   }
180   if( mask & GL_STENCIL_BUFFER_BIT)
181   {
182     // set the stencil buffer to clean
183     state->mState |= STENCIL_BUFFER_CLEAN;
184   }
185 }
186
187 FrameBufferStateCache::FrameBufferState* FrameBufferStateCache::GetFrameBufferState(  GLuint frameBufferId )
188 {
189   for( FrameBufferStateVector::SizeType i = 0; i < mFrameBufferStates.Count(); ++i )
190   {
191     FrameBufferState& state = mFrameBufferStates[i];
192     if( state.mId == frameBufferId )
193     {
194       return &state;
195     }
196   }
197   return NULL;
198 }
199
200 void FrameBufferStateCache::DeleteFrameBuffer( GLuint frameBufferId )
201 {
202   FrameBufferStateVector::Iterator iter = mFrameBufferStates.Begin();
203   FrameBufferStateVector::Iterator endIter = mFrameBufferStates.End();
204
205   for( ; iter != endIter ; ++iter )
206   {
207    if( (*iter).mId == frameBufferId )
208    {
209      mFrameBufferStates.Erase( iter);
210      return;
211    }
212  }
213  DALI_LOG_ERROR("FrameBuffer not found %d \n", frameBufferId);
214 }
215
216 unsigned int FrameBufferStateCache::GetInitialFrameBufferState()
217 {
218   return COLOR_BUFFER_CLEAN | DEPTH_BUFFER_CLEAN | STENCIL_BUFFER_CLEAN;
219 }
220
221
222 } // namespace Internal
223
224 } // namespace Dali