aboutsummaryrefslogtreecommitdiffstats
path: root/emulator/opengl/host/libs/Translator/GLcommon/TextureUtils.cpp
blob: 81152f6a3d35b97593c552a269a5e426c7cfeff3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <GLcommon/TextureUtils.h>
#include <GLcommon/GLESmacros.h>
#include <GLcommon/GLDispatch.h>
#include <GLcommon/GLESvalidate.h>
#include <stdio.h>
#include <cmath>

int getCompressedFormats(int* formats){
    if(formats){
        //Palette
        formats[0] = GL_PALETTE4_RGBA8_OES;
        formats[1] = GL_PALETTE4_RGBA4_OES;
        formats[2] = GL_PALETTE8_RGBA8_OES;
        formats[3] = GL_PALETTE8_RGBA4_OES;
        formats[4] = GL_PALETTE4_RGB8_OES;
        formats[5] = GL_PALETTE8_RGB8_OES;
        formats[6] = GL_PALETTE4_RGB5_A1_OES;
        formats[7] = GL_PALETTE8_RGB5_A1_OES;
        formats[8] = GL_PALETTE4_R5_G6_B5_OES;
        formats[9] = GL_PALETTE8_R5_G6_B5_OES;
        //ETC
        formats[MAX_SUPPORTED_PALETTE] = GL_ETC1_RGB8_OES;
    }
    return MAX_SUPPORTED_PALETTE + MAX_ETC_SUPPORTED;
}

void  doCompressedTexImage2D(GLEScontext * ctx, GLenum target, GLint level, 
                                          GLenum internalformat, GLsizei width, 
                                          GLsizei height, GLint border, 
                                          GLsizei imageSize, const GLvoid* data, void * funcPtr)
{
    /* XXX: This is just a hack to fix the resolve of glTexImage2D problem
       It will be removed when we'll no longer link against ligGL */
    typedef void (GLAPIENTRY *glTexImage2DPtr_t ) (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels);
    glTexImage2DPtr_t glTexImage2DPtr;
    glTexImage2DPtr =  (glTexImage2DPtr_t)funcPtr; 

    switch (internalformat) {
        case GL_ETC1_RGB8_OES:
            {
                GLint format = GL_RGB;
                GLint type = GL_UNSIGNED_BYTE;

                GLsizei compressedSize = etc1_get_encoded_data_size(width, height);
                SET_ERROR_IF((compressedSize > imageSize), GL_INVALID_VALUE);

                const int32_t align = ctx->getUnpackAlignment()-1;
                const int32_t bpr = ((width * 3) + align) & ~align;
                const size_t size = bpr * height;

                etc1_byte* pOut = new etc1_byte[size];
                int res = etc1_decode_image((const etc1_byte*)data, pOut, width, height, 3, bpr);
                SET_ERROR_IF(res!=0, GL_INVALID_VALUE);
                glTexImage2DPtr(target,level,format,width,height,border,format,type,pOut);
                delete [] pOut;
            }
            break;
            
        case GL_PALETTE4_RGB8_OES:
        case GL_PALETTE4_RGBA8_OES:
        case GL_PALETTE4_R5_G6_B5_OES:
        case GL_PALETTE4_RGBA4_OES:
        case GL_PALETTE4_RGB5_A1_OES:
        case GL_PALETTE8_RGB8_OES:
        case GL_PALETTE8_RGBA8_OES:
        case GL_PALETTE8_R5_G6_B5_OES:
        case GL_PALETTE8_RGBA4_OES:
        case GL_PALETTE8_RGB5_A1_OES:
            {
                SET_ERROR_IF(level > log2(ctx->getMaxTexSize()) || 
                             border !=0 || level > 0 || 
                             !GLESvalidate::texImgDim(width,height,ctx->getMaxTexSize()+2),GL_INVALID_VALUE)

                int nMipmaps = -level + 1;
                GLsizei tmpWidth  = width;
                GLsizei tmpHeight = height;

                for(int i = 0; i < nMipmaps ; i++)
                {
                   GLenum uncompressedFrmt;
                   unsigned char* uncompressed = uncompressTexture(internalformat,uncompressedFrmt,width,height,imageSize,data,i);
                   glTexImage2DPtr(target,i,uncompressedFrmt,tmpWidth,tmpHeight,border,uncompressedFrmt,GL_UNSIGNED_BYTE,uncompressed);
                   tmpWidth/=2;
                   tmpHeight/=2;
                   delete[] uncompressed;
                }
            }
            break;

        default:
            SET_ERROR_IF(1, GL_INVALID_ENUM);
            break;
    }
}