1 /* 2 * Copyright (C) 2011 The Android Open Source Project 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 #include "GLESv2Context.h" 18 19 20 21 void GLESv2Context::init() { 22 android::Mutex::Autolock mutex(s_lock); 23 if(!m_initialized) { 24 s_glDispatch.dispatchFuncs(GLES_2_0); 25 GLEScontext::init(); 26 for(int i=0; i < s_glSupport.maxVertexAttribs;i++){ 27 m_map[i] = new GLESpointer(); 28 } 29 setAttribute0value(0.0, 0.0, 0.0, 1.0); 30 31 const char* baseRenderer = (const char*)dispatcher().glGetString(GL_RENDERER); 32 size_t baseRendererLen = strlen(baseRenderer); 33 s_glRenderer.clear(); 34 s_glRenderer.reserve(16 + baseRendererLen); 35 s_glRenderer.append("OpenGL ES 2.0 (", 15); 36 s_glRenderer.append(baseRenderer, baseRendererLen); 37 s_glRenderer.append(")", 1); 38 } 39 m_initialized = true; 40 } 41 42 GLESv2Context::GLESv2Context():GLEScontext(), m_att0Array(NULL), m_att0ArrayLength(0), m_att0NeedsDisable(false){}; 43 44 GLESv2Context::~GLESv2Context() 45 { 46 delete[] m_att0Array; 47 } 48 49 void GLESv2Context::setAttribute0value(float x, float y, float z, float w) 50 { 51 m_attribute0value[0] = x; 52 m_attribute0value[1] = y; 53 m_attribute0value[2] = z; 54 m_attribute0value[3] = w; 55 } 56 57 void GLESv2Context::validateAtt0PreDraw(unsigned int count) 58 { 59 m_att0NeedsDisable = false; 60 61 if(count == 0) 62 return; 63 64 int enabled = 0; 65 s_glDispatch.glGetVertexAttribiv(0, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &enabled); 66 if(enabled) 67 return; 68 69 if(count > m_att0ArrayLength) 70 { 71 delete [] m_att0Array; 72 m_att0Array = new GLfloat[4*count]; 73 m_att0ArrayLength = count; 74 } 75 76 for(unsigned int i=0; i<count; i++) 77 memcpy(m_att0Array+i*4, m_attribute0value, 4*sizeof(GLfloat)); 78 79 s_glDispatch.glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, m_att0Array); 80 s_glDispatch.glEnableVertexAttribArray(0); 81 82 m_att0NeedsDisable = true; 83 } 84 85 void GLESv2Context::validateAtt0PostDraw(void) 86 { 87 if(m_att0NeedsDisable) 88 s_glDispatch.glDisableVertexAttribArray(0); 89 90 m_att0NeedsDisable = false; 91 } 92 93 void GLESv2Context::setupArraysPointers(GLESConversionArrays& cArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct) { 94 ArraysMap::iterator it; 95 96 //going over all clients arrays Pointers 97 for ( it=m_map.begin() ; it != m_map.end(); it++ ) { 98 GLenum array_id = (*it).first; 99 GLESpointer* p = (*it).second; 100 if(!isArrEnabled(array_id)) continue; 101 102 unsigned int size = p->getSize(); 103 104 if(needConvert(cArrs,first,count,type,indices,direct,p,array_id)){ 105 //conversion has occured 106 ArrayData currentArr = cArrs.getCurrentArray(); 107 setupArr(currentArr.data,array_id,currentArr.type,size,currentArr.stride, p->getNormalized()); 108 ++cArrs; 109 } else { 110 setupArr(p->getData(),array_id,p->getType(), 111 size,p->getStride(), p->getNormalized()); 112 } 113 } 114 } 115 116 //setting client side arr 117 void GLESv2Context::setupArr(const GLvoid* arr,GLenum arrayType,GLenum dataType,GLint size,GLsizei stride,GLboolean normalized, int index){ 118 if(arr == NULL) return; 119 s_glDispatch.glVertexAttribPointer(arrayType,size,dataType,normalized,stride,arr); 120 } 121 122 bool GLESv2Context::needConvert(GLESConversionArrays& cArrs,GLint first,GLsizei count,GLenum type,const GLvoid* indices,bool direct,GLESpointer* p,GLenum array_id) { 123 124 bool usingVBO = p->isVBO(); 125 GLenum arrType = p->getType(); 126 /* 127 conversion is not necessary in the following cases: 128 (*) array type is not fixed 129 */ 130 if(arrType != GL_FIXED) return false; 131 132 if(!usingVBO) { 133 if (direct) { 134 convertDirect(cArrs,first,count,array_id,p); 135 } else { 136 convertIndirect(cArrs,count,type,indices,array_id,p); 137 } 138 } else { 139 if (direct) { 140 convertDirectVBO(cArrs,first,count,array_id,p) ; 141 } else { 142 convertIndirectVBO(cArrs,count,type,indices,array_id,p); 143 } 144 } 145 return true; 146 } 147 148 void GLESv2Context::initExtensionString() { 149 *s_glExtensions = "GL_OES_EGL_image GL_OES_depth24 GL_OES_depth32 GL_OES_element_index_uint " 150 "GL_OES_texture_float GL_OES_texture_float_linear " 151 "GL_OES_compressed_paletted_texture GL_OES_compressed_ETC1_RGB8_texture GL_OES_depth_texture "; 152 if (s_glSupport.GL_ARB_HALF_FLOAT_PIXEL || s_glSupport.GL_NV_HALF_FLOAT) 153 *s_glExtensions+="GL_OES_texture_half_float GL_OES_texture_half_float_linear "; 154 if (s_glSupport.GL_EXT_PACKED_DEPTH_STENCIL) 155 *s_glExtensions+="GL_OES_packed_depth_stencil "; 156 if (s_glSupport.GL_ARB_HALF_FLOAT_VERTEX) 157 *s_glExtensions+="GL_OES_vertex_half_float "; 158 if (s_glSupport.GL_OES_STANDARD_DERIVATIVES) 159 *s_glExtensions+="GL_OES_standard_derivatives "; 160 } 161 162 int GLESv2Context::getMaxTexUnits() { 163 return getCaps()->maxTexImageUnits; 164 } 165