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