hostsupport/hostopengles11/src/GLESContext.cpp
branchbug235_bringup_0
changeset 53 c2ef9095503a
child 68 8d4efe9fa1cf
equal deleted inserted replaced
52:39e5f73667ba 53:c2ef9095503a
       
     1 /* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     2  *
       
     3  * Permission is hereby granted, free of charge, to any person obtaining a
       
     4  * copy of this software and /or associated documentation files
       
     5  * (the "Materials "), to deal in the Materials without restriction,
       
     6  * including without limitation the rights to use, copy, modify, merge,
       
     7  * publish, distribute, sublicense, and/or sell copies of the Materials,
       
     8  * and to permit persons to whom the Materials are furnished to do so,
       
     9  * subject to the following conditions:
       
    10  *
       
    11  * The above copyright notice and this permission notice shall be included
       
    12  * in all copies or substantial portions of the Materials.
       
    13  *
       
    14  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
       
    15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
       
    16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
       
    17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
       
    18  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
       
    19  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR
       
    20  * THE USE OR OTHER DEALINGS IN THE MATERIALS.
       
    21  *
       
    22  * Initial Contributors:
       
    23  * Nokia Corporation - initial contribution.
       
    24  *
       
    25  * Contributors:
       
    26  *
       
    27  * Description:
       
    28  *
       
    29  */
       
    30 
       
    31 #include "GLESContext.h"
       
    32 #include "GLESTexture.h"
       
    33 
       
    34 GLESContext::GLESContext(void* nativeContext) :
       
    35 	m_nativeContext(nativeContext),
       
    36 	m_texCoordArray(NULL),
       
    37 	m_initialized(false)
       
    38 {
       
    39 }
       
    40 
       
    41 GLESContext::~GLESContext()
       
    42 {
       
    43 	delete[] m_texCoordArray;
       
    44 
       
    45 	{
       
    46 		BufferMap::iterator iter;
       
    47 		for(iter = m_buffers.begin(); iter != m_buffers.end(); ++iter)
       
    48 		{
       
    49 			delete iter->second;
       
    50 		}
       
    51 	}
       
    52 
       
    53 	{
       
    54 		TextureMap::iterator iter;
       
    55 		for(iter = m_textures.begin(); iter != m_textures.end(); ++iter)
       
    56 		{
       
    57 			delete iter->second;
       
    58 		}
       
    59 	}
       
    60 }
       
    61 
       
    62 bool GLESContext::Initialize()
       
    63 {
       
    64 	GLES_ASSERT(!m_initialized);
       
    65 	
       
    66 	if(!m_dgl.Load())
       
    67 	{
       
    68 		return false;
       
    69 	}
       
    70 
       
    71 	// Initialize state.
       
    72 	m_error = GL_NO_ERROR;
       
    73 	m_clientActiveTexture = 0;
       
    74 	m_arrayBufferBinding = 0;
       
    75 	m_elementArrayBufferBinding = 0;
       
    76 	m_enabledArrays = 0;
       
    77 
       
    78 	int maxTextureUnits;
       
    79 	int maxClipPlanes;
       
    80 	int maxLights;
       
    81 	m_dgl.glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxTextureUnits);
       
    82 	m_dgl.glGetIntegerv(GL_MAX_CLIP_PLANES, &maxClipPlanes);
       
    83 	m_dgl.glGetIntegerv(GL_MAX_LIGHTS, &maxLights);
       
    84 	// The maximum number of texture units supported by the wrapper depends on the number
       
    85 	// of bits in the array state variable (four bits are used by vertex, normal, color
       
    86 	// and point size arrays).
       
    87 	m_maxTextureUnits = GLES_MIN(maxTextureUnits, sizeof(m_enabledArrays) * 8 - 4);
       
    88 	m_maxClipPlanes = maxClipPlanes;
       
    89 	m_maxLights = maxLights;
       
    90 
       
    91 	int maxTextureSize;
       
    92 	m_dgl.glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
       
    93 	m_maxTextureLevel = glesLog2(maxTextureSize);
       
    94 
       
    95 	m_texCoordArray = GLES_NEW GLESArray[m_maxTextureUnits];
       
    96 	if(m_texCoordArray == NULL)
       
    97 	{
       
    98 		return false;
       
    99 	}
       
   100 
       
   101 	// Create texture named zero.
       
   102 	BindTexture(0);
       
   103 
       
   104 	m_initialized = true;
       
   105 
       
   106 	return true;
       
   107 }
       
   108 
       
   109 GLenum GLESContext::GetHostError()
       
   110 {
       
   111 	GLenum host_error = m_dgl.glGetError();
       
   112 	if(host_error != GL_NO_ERROR)
       
   113 	{
       
   114 		m_error = host_error;
       
   115 	}
       
   116 	return host_error;
       
   117 }
       
   118 
       
   119 GLESArray& GLESContext::TexCoordArray(unsigned int texture)
       
   120 {
       
   121 	GLES_ASSERT(texture >= 0 && texture < m_maxTextureUnits);
       
   122 	return m_texCoordArray[texture];
       
   123 }
       
   124 
       
   125 GLESArray& GLESContext::TexCoordArray()
       
   126 {
       
   127 	return m_texCoordArray[m_clientActiveTexture];
       
   128 }
       
   129 
       
   130 const GLESArray& GLESContext::TexCoordArray(unsigned int texture) const
       
   131 {
       
   132 	GLES_ASSERT(texture >= 0 && texture < m_maxTextureUnits);
       
   133 	return m_texCoordArray[texture];
       
   134 }
       
   135 
       
   136 const GLESArray& GLESContext::TexCoordArray() const
       
   137 {
       
   138 	return m_texCoordArray[m_clientActiveTexture];
       
   139 }
       
   140 
       
   141 void GLESContext::SetVertexArray(int size, GLenum type, int stride, const void *pointer)
       
   142 {
       
   143 	m_vertexArray = GLESArray(size, type, stride, const_cast<void*>(pointer), m_buffers[m_arrayBufferBinding]);
       
   144 }
       
   145 
       
   146 void GLESContext::SetNormalArray(GLenum type, int stride, const void *pointer)
       
   147 {
       
   148 	m_normalArray = GLESArray(3, type, stride, const_cast<void*>(pointer), m_buffers[m_arrayBufferBinding]);
       
   149 }
       
   150 
       
   151 void GLESContext::SetColorArray(int size, GLenum type, int stride, const void *pointer)
       
   152 {
       
   153 	m_colorArray = GLESArray(size, type, stride, const_cast<void*>(pointer), m_buffers[m_arrayBufferBinding]);
       
   154 }
       
   155 
       
   156 void GLESContext::SetPointerSizeArray(GLenum type, int stride, const void *pointer)
       
   157 {
       
   158 	m_pointSizeArray = GLESArray(1, type, stride, const_cast<void*>(pointer), m_buffers[m_arrayBufferBinding]);
       
   159 }
       
   160 
       
   161 void GLESContext::SetTexCoordArray(int size, GLenum type, int stride, const void *pointer)
       
   162 {
       
   163 	m_texCoordArray[m_clientActiveTexture] = GLESArray(size, type, stride,
       
   164 													   const_cast<void*>(pointer), m_buffers[m_arrayBufferBinding]);
       
   165 }
       
   166 
       
   167 const GLESBuffer* GLESContext::Buffer(unsigned int buffer) const
       
   168 {
       
   169 	BufferMap::const_iterator iter = m_buffers.find(buffer);
       
   170 	if(iter == m_buffers.end())
       
   171 	{
       
   172 		// Not found
       
   173 		return NULL;
       
   174 	}
       
   175 
       
   176 	return iter->second;
       
   177 }
       
   178 
       
   179 GLESBuffer* GLESContext::ArrayBuffer()
       
   180 {
       
   181 	if(m_arrayBufferBinding)
       
   182 	{
       
   183 		return m_buffers[m_arrayBufferBinding];
       
   184 	}
       
   185 	else
       
   186 	{
       
   187 		return NULL;
       
   188 	}
       
   189 }
       
   190 
       
   191 GLESBuffer* GLESContext::ElementArrayBuffer()
       
   192 {
       
   193 	if(m_elementArrayBufferBinding)
       
   194 	{
       
   195 		return m_buffers[m_elementArrayBufferBinding];
       
   196 	}
       
   197 	else
       
   198 	{
       
   199 		return NULL;
       
   200 	}
       
   201 }
       
   202 
       
   203 void GLESContext::ReserveBufferNames(int num, unsigned int* names)
       
   204 {
       
   205 	GLES_ASSERT(num >= 0);
       
   206 	unsigned int candidate = 1;
       
   207 	while(num && candidate > 0)
       
   208 	{
       
   209 		if(m_buffers.find(candidate) == m_buffers.end())
       
   210 		{
       
   211 			m_buffers[candidate] = NULL;
       
   212 			names[num-1] = candidate;
       
   213 			num--;
       
   214 		}
       
   215 		candidate++;
       
   216 	}
       
   217 }
       
   218 
       
   219 void GLESContext::DeleteBuffer(unsigned int buffer)
       
   220 {
       
   221 	if(m_vertexArray.BufferName() == buffer)
       
   222 	{
       
   223 		m_vertexArray.ReleaseBuffer();
       
   224 	}
       
   225 	
       
   226 	if(m_normalArray.BufferName() == buffer)
       
   227 	{
       
   228 		m_normalArray.ReleaseBuffer();
       
   229 	}
       
   230 
       
   231 	if(m_colorArray.BufferName() == buffer)
       
   232 	{
       
   233 		m_colorArray.ReleaseBuffer();
       
   234 	}
       
   235 
       
   236 	for(unsigned int i = 0; i < m_maxTextureUnits; i++)
       
   237 	{
       
   238 		if(m_texCoordArray[i].BufferName() == buffer)
       
   239 		{
       
   240 			m_texCoordArray[i].ReleaseBuffer();
       
   241 		}
       
   242 	}
       
   243 
       
   244 	if(m_arrayBufferBinding == buffer)
       
   245 	{
       
   246 		m_arrayBufferBinding = 0;
       
   247 	}
       
   248 
       
   249 	delete Buffer(buffer);
       
   250 	m_buffers.erase(buffer);
       
   251 }
       
   252 
       
   253 bool GLESContext::BindArrayBuffer(unsigned int buffer)
       
   254 {
       
   255 	if(buffer != 0 && Buffer(buffer) == NULL)
       
   256 	{
       
   257 		// A new buffer must be created
       
   258 		m_buffers[buffer] = GLES_NEW GLESBuffer(buffer);
       
   259 		if(m_buffers[buffer] == NULL)
       
   260 		{
       
   261 			return false;
       
   262 		}
       
   263 	}
       
   264 	m_arrayBufferBinding = buffer;
       
   265 
       
   266 	return true;
       
   267 }
       
   268 
       
   269 bool GLESContext::BindElementArrayBuffer(unsigned int buffer)
       
   270 {
       
   271 	if(buffer != 0 && Buffer(buffer) == NULL)
       
   272 	{
       
   273 		// A new buffer must be created
       
   274 		m_buffers[buffer] = GLES_NEW GLESBuffer(buffer);
       
   275 		if(m_buffers[buffer] == NULL)
       
   276 		{
       
   277 			return false;
       
   278 		}
       
   279 	}
       
   280 	m_elementArrayBufferBinding = buffer;
       
   281 
       
   282 	return true;
       
   283 }
       
   284 
       
   285 void GLESContext::DeleteTexture(unsigned int texture)
       
   286 {
       
   287 	if(texture == 0)
       
   288 	{
       
   289 		// The texture named zero cannot be destroyed.
       
   290 		return;
       
   291 	}
       
   292 
       
   293 	// Unbind texture.
       
   294 	if(m_textureBinding == texture)
       
   295 	{
       
   296 		m_textureBinding = 0;
       
   297 	}
       
   298 
       
   299 	delete Texture(texture);
       
   300 	m_textures.erase(texture);
       
   301 }
       
   302 
       
   303 bool GLESContext::BindTexture(unsigned int texture)
       
   304 {
       
   305 	if(Texture(texture) == NULL)
       
   306 	{
       
   307 		// A new texture must be created
       
   308 		m_textures[texture] = GLES_NEW GLESTexture(texture);
       
   309 		if(m_textures[texture] == NULL)
       
   310 		{
       
   311 			return false;
       
   312 		}
       
   313 
       
   314 		if(!m_textures[texture]->AllocateLevels(m_maxTextureLevel))
       
   315 		{
       
   316 			delete m_textures[texture];
       
   317 			m_textures[texture] = NULL;
       
   318 		}
       
   319 	}
       
   320 	m_textureBinding = texture;
       
   321 
       
   322 	return true;
       
   323 }
       
   324 
       
   325 GLESTexture* GLESContext::Texture(unsigned int texture)
       
   326 {
       
   327 	TextureMap::const_iterator iter = m_textures.find(texture);
       
   328 	if(iter == m_textures.end())
       
   329 	{
       
   330 		// Not found
       
   331 		return NULL;
       
   332 	}
       
   333 
       
   334 	return iter->second;
       
   335 }
       
   336 
       
   337 void* glesGetCurrentGLESContext()
       
   338 {
       
   339 	void* context = EGLtoGLESInterface::GetEGLInterface()->GetGLESContext();
       
   340 	return context;
       
   341 }