Home | History | Annotate | Download | only in GLcommon
      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 #include <GLcommon/TextureUtils.h>
     17 #include <GLcommon/GLESmacros.h>
     18 #include <GLcommon/GLDispatch.h>
     19 #include <GLcommon/GLESvalidate.h>
     20 #include <stdio.h>
     21 #include <cmath>
     22 
     23 int getCompressedFormats(int* formats){
     24     if(formats){
     25         //Palette
     26         formats[0] = GL_PALETTE4_RGBA8_OES;
     27         formats[1] = GL_PALETTE4_RGBA4_OES;
     28         formats[2] = GL_PALETTE8_RGBA8_OES;
     29         formats[3] = GL_PALETTE8_RGBA4_OES;
     30         formats[4] = GL_PALETTE4_RGB8_OES;
     31         formats[5] = GL_PALETTE8_RGB8_OES;
     32         formats[6] = GL_PALETTE4_RGB5_A1_OES;
     33         formats[7] = GL_PALETTE8_RGB5_A1_OES;
     34         formats[8] = GL_PALETTE4_R5_G6_B5_OES;
     35         formats[9] = GL_PALETTE8_R5_G6_B5_OES;
     36         //ETC
     37         formats[MAX_SUPPORTED_PALETTE] = GL_ETC1_RGB8_OES;
     38     }
     39     return MAX_SUPPORTED_PALETTE + MAX_ETC_SUPPORTED;
     40 }
     41 
     42 void  doCompressedTexImage2D(GLEScontext * ctx, GLenum target, GLint level,
     43                                           GLenum internalformat, GLsizei width,
     44                                           GLsizei height, GLint border,
     45                                           GLsizei imageSize, const GLvoid* data, void * funcPtr)
     46 {
     47     /* XXX: This is just a hack to fix the resolve of glTexImage2D problem
     48        It will be removed when we'll no longer link against ligGL */
     49     typedef void (GLAPIENTRY *glTexImage2DPtr_t ) (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels);
     50     glTexImage2DPtr_t glTexImage2DPtr;
     51     glTexImage2DPtr =  (glTexImage2DPtr_t)funcPtr;
     52 
     53     switch (internalformat) {
     54         case GL_ETC1_RGB8_OES:
     55             {
     56                 GLint format = GL_RGB;
     57                 GLint type = GL_UNSIGNED_BYTE;
     58 
     59                 GLsizei compressedSize = etc1_get_encoded_data_size(width, height);
     60                 SET_ERROR_IF((compressedSize > imageSize), GL_INVALID_VALUE);
     61 
     62                 const int32_t align = ctx->getUnpackAlignment()-1;
     63                 const int32_t bpr = ((width * 3) + align) & ~align;
     64                 const size_t size = bpr * height;
     65 
     66                 etc1_byte* pOut = new etc1_byte[size];
     67                 int res = etc1_decode_image((const etc1_byte*)data, pOut, width, height, 3, bpr);
     68                 SET_ERROR_IF(res!=0, GL_INVALID_VALUE);
     69                 glTexImage2DPtr(target,level,format,width,height,border,format,type,pOut);
     70                 delete [] pOut;
     71             }
     72             break;
     73 
     74         case GL_PALETTE4_RGB8_OES:
     75         case GL_PALETTE4_RGBA8_OES:
     76         case GL_PALETTE4_R5_G6_B5_OES:
     77         case GL_PALETTE4_RGBA4_OES:
     78         case GL_PALETTE4_RGB5_A1_OES:
     79         case GL_PALETTE8_RGB8_OES:
     80         case GL_PALETTE8_RGBA8_OES:
     81         case GL_PALETTE8_R5_G6_B5_OES:
     82         case GL_PALETTE8_RGBA4_OES:
     83         case GL_PALETTE8_RGB5_A1_OES:
     84             {
     85                 SET_ERROR_IF(level > log2(ctx->getMaxTexSize()) ||
     86                              border !=0 || level > 0 ||
     87                              !GLESvalidate::texImgDim(width,height,ctx->getMaxTexSize()+2),GL_INVALID_VALUE)
     88 
     89                 int nMipmaps = -level + 1;
     90                 GLsizei tmpWidth  = width;
     91                 GLsizei tmpHeight = height;
     92 
     93                 for(int i = 0; i < nMipmaps ; i++)
     94                 {
     95                    GLenum uncompressedFrmt;
     96                    unsigned char* uncompressed = uncompressTexture(internalformat,uncompressedFrmt,width,height,imageSize,data,i);
     97                    glTexImage2DPtr(target,i,uncompressedFrmt,tmpWidth,tmpHeight,border,uncompressedFrmt,GL_UNSIGNED_BYTE,uncompressed);
     98                    tmpWidth/=2;
     99                    tmpHeight/=2;
    100                    delete[] uncompressed;
    101                 }
    102             }
    103             break;
    104 
    105         default:
    106             SET_ERROR_IF(1, GL_INVALID_ENUM);
    107             break;
    108     }
    109 }
    110