2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/render/gl-resources/context.h>
26 #include <dali/public-api/common/constants.h>
27 #include <dali/internal/render/shaders/program.h>
28 #include <dali/integration-api/platform-abstraction.h>
29 #include <dali/internal/render/common/render-manager.h>
30 #include <dali/integration-api/debug.h>
40 namespace // unnamed namespace
48 const GLenum errorCode;
49 const char* errorString;
51 errorStrings errors[] =
53 { GL_NO_ERROR, "GL_NO_ERROR" },
54 { GL_INVALID_ENUM, "GL_INVALID_ENUM" },
55 { GL_INVALID_VALUE, "GL_INVALID_VALUE" },
56 { GL_INVALID_OPERATION, "GL_INVALID_OPERATION" },
57 { GL_OUT_OF_MEMORY, "GL_OUT_OF_MEMORY" }
61 * Called by std::for_each from ~Context
63 void deletePrograms(std::pair< std::size_t, Program* > hashProgram)
65 DALI_ASSERT_DEBUG( hashProgram.second );
66 delete hashProgram.second;
69 const unsigned int UNINITIALIZED_TEXTURE_UNIT = std::numeric_limits<unsigned int>::max();// GL_MAX_TEXTURE_UNITS can't be used because it's depreciated in gles2
71 } // unnamed namespace
74 Debug::Filter* Context::gGlLogFilter = Debug::Filter::New(Debug::Concise, false, "LOG_CONTEXT");
77 Context::Context(Integration::GlAbstraction& glAbstraction)
78 : mGlAbstraction(glAbstraction),
79 mGlContextCreated(false),
83 mDepthTestEnabled(false),
84 mDepthMaskEnabled(false),
85 mDitherEnabled(true), // This the only GL capability which defaults to true
86 mPolygonOffsetFillEnabled(false),
87 mSampleAlphaToCoverageEnabled(false),
88 mSampleCoverageEnabled(false),
89 mScissorTestEnabled(false),
90 mStencilTestEnabled(false),
91 mClearColorSet(false),
92 mBoundArrayBufferId(0),
93 mBoundElementArrayBufferId(0),
94 mBoundTransformFeedbackBufferId(0),
95 mActiveTextureUnit( UNINITIALIZED_TEXTURE_UNIT ),
96 mUsingDefaultBlendColor(true),
97 mBlendFuncSeparateSrcRGB(GL_ONE),
98 mBlendFuncSeparateDstRGB(GL_ZERO),
99 mBlendFuncSeparateSrcAlpha(GL_ONE),
100 mBlendFuncSeparateDstAlpha(GL_ZERO),
101 mBlendEquationSeparateModeRGB( GL_FUNC_ADD ),
102 mBlendEquationSeparateModeAlpha( GL_FUNC_ADD ),
105 mClearColor(Color::WHITE), // initial color, never used until it's been set by the user
106 mCullFaceMode(CullNone),
107 mViewPort( 0, 0, 0, 0 ),
108 mCurrentProgram( NULL )
114 // release the cached programs
115 std::for_each(mProgramCache.begin(), mProgramCache.end(), deletePrograms);
116 mProgramCache.clear();
119 void Context::GlContextCreated()
121 DALI_ASSERT_DEBUG(!mGlContextCreated);
123 mGlContextCreated = true;
125 // Set the initial GL state, and check it.
128 const ProgramContainer::iterator endp = mProgramCache.end();
129 for ( ProgramContainer::iterator itp = mProgramCache.begin(); itp != endp; ++itp )
131 (*itp).second->GlContextCreated();
135 void Context::GlContextDestroyed()
137 const ProgramContainer::iterator endp = mProgramCache.end();
138 for ( ProgramContainer::iterator itp = mProgramCache.begin(); itp != endp; ++itp )
140 (*itp).second->GlContextDestroyed();
143 mGlContextCreated = false;
146 const char* Context::ErrorToString( GLenum errorCode )
148 for( unsigned int i = 0; i < sizeof(errors) / sizeof(errors[0]); ++i)
150 if (errorCode == errors[i].errorCode)
152 return errors[i].errorString;
155 return "Unknown Open GLES error";
158 Program* Context::GetCachedProgram( std::size_t hash ) const
160 std::map< std::size_t, Program* >::const_iterator iter = mProgramCache.find(hash);
162 if (iter != mProgramCache.end())
169 void Context::CacheProgram( std::size_t hash, Program* pointer )
171 mProgramCache[ hash ] = pointer;
174 const Rect< int >& Context::GetViewport()
179 void Context::FlushVertexAttributeLocations()
181 for( unsigned int i = 0; i < MAX_ATTRIBUTE_CACHE_SIZE; ++i )
183 // see if our cached state is different to the actual state
184 if (mVertexAttributeCurrentState[ i ] != mVertexAttributeCachedState[ i ] )
186 // it's different so make the change to the driver
187 // and update the cached state
188 mVertexAttributeCurrentState[ i ] = mVertexAttributeCachedState[ i ];
190 if (mVertexAttributeCurrentState[ i ] )
192 LOG_GL("EnableVertexAttribArray %d\n", i);
193 CHECK_GL( *this, mGlAbstraction.EnableVertexAttribArray( i ) );
197 LOG_GL("DisableVertexAttribArray %d\n", i);
198 CHECK_GL( *this, mGlAbstraction.DisableVertexAttribArray( i ) );
205 void Context::SetVertexAttributeLocation(unsigned int location, bool state)
208 if( location >= MAX_ATTRIBUTE_CACHE_SIZE )
210 // not cached, make the gl call through context
213 LOG_GL("EnableVertexAttribArray %d\n", location);
214 CHECK_GL( *this, mGlAbstraction.EnableVertexAttribArray( location ) );
218 LOG_GL("DisableVertexAttribArray %d\n", location);
219 CHECK_GL( *this, mGlAbstraction.DisableVertexAttribArray( location ) );
224 // set the cached state, it will be set at the next draw call
225 // if it's different from the current driver state
226 mVertexAttributeCachedState[ location ] = state;
230 void Context::ResetVertexAttributeState()
232 // reset attribute cache
233 for( unsigned int i=0; i < MAX_ATTRIBUTE_CACHE_SIZE; ++i )
235 mVertexAttributeCachedState[ i ] = false;
236 mVertexAttributeCurrentState[ i ] = false;
238 LOG_GL("DisableVertexAttribArray %d\n", i);
239 CHECK_GL( *this, mGlAbstraction.DisableVertexAttribArray( i ) );
243 void Context::ResetGlState()
245 DALI_ASSERT_DEBUG(mGlContextCreated);
247 mClearColorSet = false;
248 // Render manager will call clear in next render
250 // Reset internal state and Synchronize it with real OpenGL context.
251 // This may seem like overkill, but the GL context is not owned by dali-core,
252 // and no assumptions should be made about the initial state.
254 mGlAbstraction.ColorMask( true, true, true, true );
257 mGlAbstraction.StencilMask( 0xFF );
259 mBlendEnabled = false;
260 mGlAbstraction.Disable(GL_BLEND);
262 mDepthTestEnabled = false;
263 mGlAbstraction.Disable(GL_DEPTH_TEST);
265 mDepthMaskEnabled = false;
266 mGlAbstraction.DepthMask(GL_FALSE);
268 mDitherEnabled = false; // This the only GL capability which defaults to true
269 mGlAbstraction.Disable(GL_DITHER);
271 mPolygonOffsetFillEnabled = false;
272 mGlAbstraction.Disable(GL_POLYGON_OFFSET_FILL);
274 mSampleAlphaToCoverageEnabled = false;
275 mGlAbstraction.Disable(GL_SAMPLE_ALPHA_TO_COVERAGE);
277 mSampleCoverageEnabled = false;
278 mGlAbstraction.Disable(GL_SAMPLE_COVERAGE);
280 mScissorTestEnabled = false;
281 mGlAbstraction.Disable(GL_SCISSOR_TEST);
283 mStencilTestEnabled = false;
284 mGlAbstraction.Disable(GL_STENCIL_TEST);
286 mBoundArrayBufferId = 0;
287 LOG_GL("BindBuffer GL_ARRAY_BUFFER 0\n");
288 mGlAbstraction.BindBuffer(GL_ARRAY_BUFFER, mBoundArrayBufferId);
290 mBoundElementArrayBufferId = 0;
291 LOG_GL("BindBuffer GL_ELEMENT_ARRAY_BUFFER 0\n");
292 mGlAbstraction.BindBuffer(GL_ELEMENT_ARRAY_BUFFER, mBoundElementArrayBufferId);
294 #ifndef EMSCRIPTEN // not in WebGL
295 mBoundTransformFeedbackBufferId = 0;
296 LOG_GL("BindBuffer GL_TRANSFORM_FEEDBACK_BUFFER 0\n");
297 mGlAbstraction.BindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, mBoundTransformFeedbackBufferId);
300 mActiveTextureUnit = UNINITIALIZED_TEXTURE_UNIT;
302 mUsingDefaultBlendColor = true;
303 mGlAbstraction.BlendColor( 0.0f, 0.0f, 0.0f, 0.0f );
305 mBlendFuncSeparateSrcRGB = GL_ONE;
306 mBlendFuncSeparateDstRGB = GL_ZERO;
307 mBlendFuncSeparateSrcAlpha = GL_ONE;
308 mBlendFuncSeparateDstAlpha = GL_ZERO;
309 mGlAbstraction.BlendFuncSeparate( mBlendFuncSeparateSrcRGB, mBlendFuncSeparateDstRGB,
310 mBlendFuncSeparateSrcAlpha, mBlendFuncSeparateDstAlpha );
312 // initial state is GL_FUNC_ADD for both RGB and Alpha blend modes
313 mBlendEquationSeparateModeRGB = GL_FUNC_ADD;
314 mBlendEquationSeparateModeAlpha = GL_FUNC_ADD;
315 mGlAbstraction.BlendEquationSeparate( mBlendEquationSeparateModeRGB, mBlendEquationSeparateModeAlpha);
317 mCullFaceMode = CullNone;
318 mGlAbstraction.Disable(GL_CULL_FACE);
319 mGlAbstraction.FrontFace(GL_CCW);
320 mGlAbstraction.CullFace(GL_BACK);
322 // get max texture units
323 mGlAbstraction.GetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mMaxTextureUnits);
324 DALI_ASSERT_DEBUG(mMaxTextureUnits > 7); // according to GLES 2.0 specification
325 mBound2dTextureId.reserve(mMaxTextureUnits);
326 // rebind texture units
327 for( int i=0; i < mMaxTextureUnits; ++i )
329 mBound2dTextureId[ i ] = 0;
330 // set active texture
331 mGlAbstraction.ActiveTexture( GL_TEXTURE0 + i );
332 // bind the previous texture
333 mGlAbstraction.BindTexture(GL_TEXTURE_2D, mBound2dTextureId[ i ] );
336 // get maximum texture size
337 mGlAbstraction.GetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
339 GLint numProgramBinaryFormats;
340 mGlAbstraction.GetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS_OES, &numProgramBinaryFormats);
341 if( GL_NO_ERROR == mGlAbstraction.GetError() && 0 != numProgramBinaryFormats )
343 mProgramBinaryFormats.resize(numProgramBinaryFormats);
344 mGlAbstraction.GetIntegerv(GL_PROGRAM_BINARY_FORMATS_OES, &mProgramBinaryFormats[0]);
347 // reset viewport, this will be set to something useful when rendering
348 mViewPort.x = mViewPort.y = mViewPort.width = mViewPort.height = 0;
350 ResetVertexAttributeState();
353 #ifdef DALI_CONTEXT_LOGGING
355 void Context::PrintCurrentState()
357 DALI_LOG_INFO(SceneGraph::Context::gGlLogFilter, Debug::General,
358 "----------------- Context State BEGIN -----------------\n"
364 "Polygon Offset Fill = %s\n"
365 "Sample Alpha To Coverage = %s\n"
366 "Sample Coverage = %s\n"
367 "Scissor Test = %s\n"
368 "Stencil Test = %s\n"
369 "----------------- Context State END -----------------\n",
370 mBlendEnabled ? "Enabled" : "Disabled",
371 mDepthTestEnabled ? "Enabled" : "Disabled",
372 mDepthMaskEnabled ? "Enabled" : "Disabled",
373 mDitherEnabled ? "Enabled" : "Disabled",
374 mPolygonOffsetFillEnabled ? "Enabled" : "Disabled",
375 mSampleAlphaToCoverageEnabled ? "Enabled" : "Disabled",
376 mSampleCoverageEnabled ? "Enabled" : "Disabled",
377 mScissorTestEnabled ? "Enabled" : "Disabled",
378 mStencilTestEnabled ? "Enabled" : "Disabled");
381 #endif // DALI_CONTEXT_LOGGING
383 } // namespace Internal