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>
25 #include <dali/public-api/common/constants.h>
26 #include <dali/public-api/common/compile-time-assert.h>
27 #include <dali/integration-api/platform-abstraction.h>
28 #include <dali/integration-api/debug.h>
29 #include <dali/internal/render/common/render-manager.h>
30 #include <dali/devel-api/rendering/material.h>
38 namespace // unnamed namespace
41 DALI_COMPILE_TIME_ASSERT( TEXTURE_UNIT_LAST <= Context::MAX_TEXTURE_UNITS );
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" }
60 } // unnamed namespace
63 Debug::Filter* gContextLogFilter = Debug::Filter::New(Debug::Concise, false, "LOG_CONTEXT_STATE");
66 Context::Context(Integration::GlAbstraction& glAbstraction)
67 : mGlAbstraction(glAbstraction),
68 mGlContextCreated(false),
72 mDepthBufferEnabled(false),
73 mDepthMaskEnabled(false),
74 mDitherEnabled(true), // This the only GL capability which defaults to true
75 mPolygonOffsetFillEnabled(false),
76 mSampleAlphaToCoverageEnabled(false),
77 mSampleCoverageEnabled(false),
78 mScissorTestEnabled(false),
79 mStencilBufferEnabled(false),
80 mClearColorSet(false),
81 mUsingDefaultBlendColor(true),
82 mBoundArrayBufferId(0),
83 mBoundElementArrayBufferId(0),
84 mBoundTransformFeedbackBufferId(0),
85 mActiveTextureUnit( TEXTURE_UNIT_LAST ),
86 mBlendColor(Color::TRANSPARENT),
87 mBlendFuncSeparateSrcRGB(GL_ONE),
88 mBlendFuncSeparateDstRGB(GL_ZERO),
89 mBlendFuncSeparateSrcAlpha(GL_ONE),
90 mBlendFuncSeparateDstAlpha(GL_ZERO),
91 mBlendEquationSeparateModeRGB( GL_FUNC_ADD ),
92 mBlendEquationSeparateModeAlpha( GL_FUNC_ADD ),
94 mClearColor(Color::WHITE), // initial color, never used until it's been set by the user
95 mCullFaceMode( Dali::Material::NONE ),
96 mViewPort( 0, 0, 0, 0 ),
107 void Context::GlContextCreated()
109 DALI_LOG_INFO(gContextLogFilter, Debug::Verbose, "Context::GlContextCreated()\n");
111 DALI_ASSERT_DEBUG(!mGlContextCreated);
113 mGlContextCreated = true;
115 // Set the initial GL state, and check it.
123 void Context::GlContextDestroyed()
125 DALI_LOG_INFO(gContextLogFilter, Debug::Verbose, "Context::GlContextDestroyed()\n");
126 mGlContextCreated = false;
129 const char* Context::ErrorToString( GLenum errorCode )
131 for( unsigned int i = 0; i < sizeof(errors) / sizeof(errors[0]); ++i)
133 if (errorCode == errors[i].errorCode)
135 return errors[i].errorString;
138 return "Unknown Open GLES error";
141 const Rect< int >& Context::GetViewport()
146 void Context::FlushVertexAttributeLocations()
148 for( unsigned int i = 0; i < MAX_ATTRIBUTE_CACHE_SIZE; ++i )
150 // see if our cached state is different to the actual state
151 if (mVertexAttributeCurrentState[ i ] != mVertexAttributeCachedState[ i ] )
153 // it's different so make the change to the driver
154 // and update the cached state
155 mVertexAttributeCurrentState[ i ] = mVertexAttributeCachedState[ i ];
157 if (mVertexAttributeCurrentState[ i ] )
159 LOG_GL("EnableVertexAttribArray %d\n", i);
160 CHECK_GL( mGlAbstraction, mGlAbstraction.EnableVertexAttribArray( i ) );
164 LOG_GL("DisableVertexAttribArray %d\n", i);
165 CHECK_GL( mGlAbstraction, mGlAbstraction.DisableVertexAttribArray( i ) );
172 void Context::SetVertexAttributeLocation(unsigned int location, bool state)
175 if( location >= MAX_ATTRIBUTE_CACHE_SIZE )
177 // not cached, make the gl call through context
180 LOG_GL("EnableVertexAttribArray %d\n", location);
181 CHECK_GL( mGlAbstraction, mGlAbstraction.EnableVertexAttribArray( location ) );
185 LOG_GL("DisableVertexAttribArray %d\n", location);
186 CHECK_GL( mGlAbstraction, mGlAbstraction.DisableVertexAttribArray( location ) );
191 // set the cached state, it will be set at the next draw call
192 // if it's different from the current driver state
193 mVertexAttributeCachedState[ location ] = state;
197 void Context::InitializeGlState()
199 DALI_LOG_INFO(gContextLogFilter, Debug::Verbose, "Context::InitializeGlState()\n");
200 DALI_ASSERT_DEBUG(mGlContextCreated);
202 mClearColorSet = false;
205 mBlendEnabled = false;
206 mDepthBufferEnabled = false;
207 mDepthMaskEnabled = false;
208 mPolygonOffsetFillEnabled = false;
209 mSampleAlphaToCoverageEnabled = false;
210 mSampleCoverageEnabled = false;
211 mScissorTestEnabled = false;
212 mStencilBufferEnabled = false;
213 mDitherEnabled = false; // This and GL_MULTISAMPLE are the only GL capability which defaults to true
214 mGlAbstraction.Disable(GL_DITHER);
216 mBoundArrayBufferId = 0;
217 mBoundElementArrayBufferId = 0;
218 mBoundTransformFeedbackBufferId = 0;
219 mActiveTextureUnit = TEXTURE_UNIT_IMAGE;
221 mUsingDefaultBlendColor = true; //Default blend color is (0,0,0,0)
223 mBlendFuncSeparateSrcRGB = GL_ONE;
224 mBlendFuncSeparateDstRGB = GL_ZERO;
225 mBlendFuncSeparateSrcAlpha = GL_ONE;
226 mBlendFuncSeparateDstAlpha = GL_ZERO;
228 // initial state is GL_FUNC_ADD for both RGB and Alpha blend modes
229 mBlendEquationSeparateModeRGB = GL_FUNC_ADD;
230 mBlendEquationSeparateModeAlpha = GL_FUNC_ADD;
232 mCullFaceMode = Dali::Material::NONE; //By default cullface is disabled, front face is set to CCW and cull face is set to back
234 // get maximum texture size
235 mGlAbstraction.GetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
237 // reset viewport, this will be set to something useful when rendering
238 mViewPort.x = mViewPort.y = mViewPort.width = mViewPort.height = 0;
240 mFrameBufferStateCache.Reset();
245 void Context::PrintCurrentState()
247 const char* cullFaceModes[] = { "CullNone", "CullFront", "CullBack", "CullFrontAndBack" };
248 DALI_LOG_INFO( gContextLogFilter, Debug::General,
249 "\n----------------- Context State BEGIN -----------------\n"
255 "Polygon Offset Fill = %s\n"
256 "Sample Alpha To Coverage = %s\n"
257 "Sample Coverage = %s\n"
258 "Scissor Test = %s\n"
259 "Stencil Test = %s\n"
260 "----------------- Context State END -----------------\n",
261 mBlendEnabled ? "Enabled" : "Disabled",
262 cullFaceModes[ mCullFaceMode ],
263 mDepthBufferEnabled ? "Enabled" : "Disabled",
264 mDepthMaskEnabled ? "Enabled" : "Disabled",
265 mDitherEnabled ? "Enabled" : "Disabled",
266 mPolygonOffsetFillEnabled ? "Enabled" : "Disabled",
267 mSampleAlphaToCoverageEnabled ? "Enabled" : "Disabled",
268 mSampleCoverageEnabled ? "Enabled" : "Disabled",
269 mScissorTestEnabled ? "Enabled" : "Disabled",
270 mStencilBufferEnabled ? "Enabled" : "Disabled");
273 #endif // DALI_CONTEXT_LOGGING
275 } // namespace Internal