Home | History | Annotate | Download | only in GLES_CM
      1 /*
      2 * Copyright 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 <stdio.h>
     18 #include <string.h>
     19 #include <stdlib.h>
     20 #include <unistd.h>
     21 
     22 //#define GL_API
     23 //#define GL_APIENTRY
     24 
     25 #undef ANDROID
     26 #include <EGL/egl.h>
     27 #include <GLES/gl.h>
     28 
     29 #ifdef __APPLE__
     30 extern "C" void * createGLView(void *nsWindowPtr, int x, int y, int width, int height);
     31 #endif
     32 
     33 #undef HAVE_MALLOC_H
     34 #include <SDL.h>
     35 #include <SDL_syswm.h>
     36 
     37 
     38 #define WINDOW_WIDTH    500
     39 #define WINDOW_HEIGHT   500
     40 
     41 #define TEX_WIDTH 256
     42 #define TEX_HEIGHT 256
     43 
     44 
     45 #define F_to_X(d) ((d) > 32767.65535 ? 32767 * 65536 + 65535 :  \
     46                (d) < -32768.65535 ? -32768 * 65536 + 65535 : \
     47                ((GLfixed) ((d) * 65536)))
     48 #define X_to_F(x)  ((float)(x))/65536.0f
     49 
     50 static EGLint const attribute_list[] = {
     51     EGL_RED_SIZE, 1,
     52     EGL_GREEN_SIZE, 1,
     53     EGL_BLUE_SIZE, 1,
     54     EGL_NONE
     55 };
     56 
     57 unsigned char *genTexture(int width, int height, int comp)
     58 {
     59     unsigned char *img = new unsigned char[width * height * comp];
     60     unsigned char *ptr = img;
     61     for (int i = 0; i < height; i++) {
     62         for (int j = 0; j < width; j++) {
     63             unsigned char col = ((i / 8 + j / 8) % 2) * 255 ;
     64             if (j>(width/2)) col/=2;
     65             for (int c = 0; c < comp; c++) {
     66                 *ptr = col; ptr++;
     67             }
     68         }
     69     }
     70     return img;
     71 }
     72 
     73 unsigned char *genRedTexture(int width, int height, int comp)
     74 {
     75     unsigned char *img = new unsigned char[width * height * comp];
     76         memset(img,0,width*height*comp);
     77     unsigned char *ptr = img;
     78     for (int i = 0; i < height; i++) {
     79         for (int j = 0; j < width; j++) {
     80             unsigned char col = ((i / 8 + j / 8) % 2) * 255 ;
     81                         *ptr = col;
     82                          ptr+=comp;
     83         }
     84     }
     85     return img;
     86 }
     87 
     88 //mip 0;
     89 unsigned char *genPalette4_rgb8(int width, int height,int color)
     90 {
     91         int size = width*height/2 + 16*3/*palette size*/;
     92     unsigned char *img = new unsigned char[size];
     93 
     94         memset(img,0,size);
     95         img[0] = 255; img[1] = 0; img[2] = 0;     // red
     96         img[3] = 0; img[4] = 0; img[5] = 255;    //blue
     97         img[7] = 128; img[8] = 0; img[9] = 128;  //fucsia
     98         //rest of the palette is empty
     99 
    100     unsigned char *ptr = img+(16*3);
    101     for (int i = 0; i < (height*width/2); i++) {
    102              ptr[i] = (i%2)?0x0|color:0x11|color;
    103     }
    104     return img;
    105 }
    106 
    107 void usage(const char *progname)
    108 {
    109     fprintf(stderr, "usage: %s [-n <nframes> -i -h]\n", progname);
    110     fprintf(stderr, "\t-h: this message\n");
    111     fprintf(stderr, "\t-i: immidate mode\n");
    112     fprintf(stderr, "\t-n nframes: generate nframes\n");
    113     fprintf(stderr, "\t-e: use index arrays\n");
    114     fprintf(stderr, "\t-t: use texture\n");
    115     fprintf(stderr, "\t-f: use fixed points\n");
    116     fprintf(stderr, "\t-p: use point size OES extention\n");
    117 }
    118 
    119 #define SWITCH_SOURCE(add)\
    120             if(useConvertedType){                            \
    121                 if(useFixed){                                \
    122                       data = (GLvoid*)(fixedVertices+(add)); \
    123                 } else {                                     \
    124                       data = (GLvoid*)(byteVertices +(add)); \
    125                 }                                            \
    126             } else {                                         \
    127                       data = (GLvoid*)(afVertices+(add));    \
    128             }                                                \
    129 
    130 #ifdef _WIN32
    131 int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    132 #else
    133 int main(int argc, char **argv)
    134 #endif
    135 {
    136     GLuint  ui32Vbo = 0; // Vertex buffer object handle
    137     GLuint  ui32IndexVbo;
    138     GLuint  ui32Texture;
    139 
    140     int nframes = 100;
    141     bool immidateMode     = false;
    142     bool useIndices       = true;
    143     bool useTexture       = false;
    144     bool useCompTexture   = false;
    145     bool useConvertedType = true;
    146     bool useFixed         = false;
    147     bool usePoints        = false;
    148     bool useCopy          = false;
    149     bool useSubCopy       = false;
    150 
    151     int c;
    152     extern char *optarg;
    153 
    154     #ifdef _WIN32
    155         HWND   windowId = NULL;
    156     #elif __linux__
    157         Window windowId = NULL;
    158     #elif __APPLE__
    159         void* windowId = NULL;
    160     #endif
    161 
    162         //      // Inialize SDL window
    163         //
    164         if (SDL_Init(SDL_INIT_NOPARACHUTE | SDL_INIT_VIDEO)) {
    165             fprintf(stderr,"SDL init failed: %s\n", SDL_GetError());
    166             return -1;
    167         }
    168 
    169         SDL_Surface *surface = SDL_SetVideoMode(WINDOW_WIDTH,WINDOW_HEIGHT, 32, SDL_HWSURFACE);
    170         if (surface == NULL) {
    171             fprintf(stderr,"Failed to set video mode: %s\n", SDL_GetError());
    172             return -1;
    173         }
    174 
    175         SDL_SysWMinfo  wminfo;
    176         memset(&wminfo, 0, sizeof(wminfo));
    177         SDL_GetWMInfo(&wminfo);
    178     #ifdef _WIN32
    179         windowId = wminfo.window;
    180     #elif __linux__
    181         windowId = wminfo.info.x11.window;
    182     #elif __APPLE__
    183         windowId = createGLView(wminfo.nsWindowPtr,0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
    184 
    185     #endif
    186 
    187         int major,minor,num_config;
    188         EGLConfig configs[150];
    189         EGLSurface egl_surface;
    190         EGLContext ctx;
    191         EGLDisplay d = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    192         eglInitialize(d,&major,&minor);
    193         printf("DISPLAY == %p major =%d minor = %d\n",d,major,minor);
    194         eglChooseConfig(d, attribute_list, configs, 150, &num_config);
    195         printf("config returned %d\n",num_config);
    196         egl_surface = eglCreateWindowSurface(d,configs[0],windowId,NULL);
    197         printf("before creating context..\n");
    198         ctx = eglCreateContext(d,configs[0],EGL_NO_CONTEXT,NULL);
    199         printf("SURFACE == %p CONTEXT == %p\n",egl_surface,ctx);
    200         if(eglMakeCurrent(d,egl_surface,egl_surface,ctx)!= EGL_TRUE){
    201             printf("make current failed\n");
    202             return false;
    203         }
    204         printf("after make current\n");
    205 
    206         GLenum err = glGetError();
    207         if(err != GL_NO_ERROR) {
    208         printf("error before drawing ->>> %d  \n",err);
    209         } else {
    210         printf("no error before drawing\n");
    211         }
    212 
    213     if (useTexture) {
    214 
    215         glEnable(GL_TEXTURE_2D);
    216         ui32Texture = 1;
    217         glBindTexture(GL_TEXTURE_2D, ui32Texture);
    218                 GLenum err = glGetError();
    219 
    220         unsigned char *pixels = NULL;
    221                 if(useCompTexture) {
    222                         pixels = genPalette4_rgb8(TEX_WIDTH,TEX_HEIGHT,3);
    223                         glCompressedTexImage2D(GL_TEXTURE_2D,0,GL_PALETTE4_RGB8_OES,TEX_WIDTH,TEX_HEIGHT,0,3*16+TEX_WIDTH*TEX_HEIGHT/2,pixels);
    224 
    225                 } else {
    226                         pixels = genTexture(TEX_WIDTH, TEX_HEIGHT, 4);
    227             glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEX_WIDTH, TEX_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
    228                 }
    229 
    230         delete pixels;
    231 
    232                 err = glGetError();
    233                 if(err != GL_NO_ERROR)
    234             printf("error %d after image \n",err);
    235         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    236                 err = glGetError();
    237                 if(err != GL_NO_ERROR)
    238             printf("error after min filter \n");
    239         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    240                 err = glGetError();
    241                 if(err != GL_NO_ERROR)
    242             printf("error after mag filter \n");
    243         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    244                 err = glGetError();
    245                 if(err != GL_NO_ERROR)
    246             printf("error after env mode \n");
    247 
    248                 if(useCompTexture) {
    249                     pixels = genPalette4_rgb8(TEX_WIDTH,TEX_HEIGHT,1);
    250                     glCompressedTexSubImage2D(GL_TEXTURE_2D,0,TEX_WIDTH/4,TEX_HEIGHT/4,TEX_WIDTH/8,TEX_HEIGHT/8,GL_PALETTE4_RGB8_OES,3*16+(TEX_WIDTH*TEX_HEIGHT/128),pixels);
    251                 } else {
    252             pixels = genRedTexture(TEX_WIDTH/8, TEX_HEIGHT/8, 4);
    253                     glTexSubImage2D(GL_TEXTURE_2D,0,TEX_WIDTH/4,TEX_HEIGHT/4,TEX_WIDTH/8,TEX_HEIGHT/8,GL_RGBA,GL_UNSIGNED_BYTE,pixels);
    254                 }
    255                  err = glGetError();
    256                 if(err != GL_NO_ERROR)
    257             printf("error %d after subimage \n",err);
    258         delete pixels;
    259 
    260     }
    261 
    262     glClearColor(0.6f, 0.8f, 1.0f, 1.0f); // clear blue
    263 
    264     float afVertices[] = {  -0.4f,-0.4f,0.0f, // Position
    265                  1.0f,0.0f,0.0f,1.0f, // Color
    266                  0.0f, 0.0f, // texture
    267                  12.f, //point size
    268 
    269                  0.4f,-0.4f,0.0f,
    270                  0.0f,1.0f,0.0f,1.0f,
    271                  1.0f, 0.0f,
    272                  47.0f,
    273 
    274                  0.0f,0.4f,0.0f,
    275                  0.0f,0.0f,1.0f,1.0f,
    276                  0.5f, 1.0f,
    277                  14.0f
    278     };
    279 
    280 #define MAX_T 1
    281 #define MID_T 0
    282 #define MIN_T 0
    283 
    284     GLbyte byteVertices[] = { -1,-1,0, // Position
    285                              255,0,0,255, // Color
    286                              MIN_T, MIN_T, // texture
    287                              12, //point size
    288 
    289                              1,-1,0,
    290                              0,255,0,255,
    291                              MAX_T,MIN_T,
    292                              47,
    293 
    294                             0,1,0,
    295                             0,0,255,255,
    296                             MID_T, MAX_T,
    297                             14
    298     };
    299 
    300     GLfixed fixedVertices[] = { F_to_X(-0.4f),F_to_X(-0.4f),F_to_X(0.0f), // Position
    301                     F_to_X(1.0f),F_to_X(0.0f),F_to_X(0.0f),F_to_X(1.0f), // Color
    302                     F_to_X(0.0f),F_to_X(0.0f), // texture
    303                     F_to_X(12.0f),//points size
    304 
    305                     F_to_X(0.4f),F_to_X(-0.4f),F_to_X(0.0f),
    306                     F_to_X(0.0f),F_to_X(1.0f),F_to_X(0.0f),F_to_X(1.0f),
    307                     F_to_X(1.0f),F_to_X( 0.0f),
    308                     F_to_X(30.0f),
    309 
    310                     F_to_X(0.0f),F_to_X(0.4f),F_to_X(0.0f),
    311                     F_to_X(0.0f),F_to_X(0.0f),F_to_X(1.0f),F_to_X(1.0f),
    312                     F_to_X(0.5f), F_to_X(1.0f),
    313                     F_to_X(30.0)
    314     };
    315 
    316     unsigned short indices[] = { 2, 1, 0 };
    317 
    318     if (!immidateMode) {
    319         glGenBuffers(1, &ui32Vbo);
    320         ui32Vbo = 1;
    321          printf("ui32Vbo = %d\n", ui32Vbo);
    322 
    323         glBindBuffer(GL_ARRAY_BUFFER, ui32Vbo);
    324         void* data = (void*)afVertices;
    325         unsigned int uiSize = 3*(sizeof(float)*10);
    326         if(useConvertedType){
    327            if(useFixed){
    328                data = (void*)fixedVertices;
    329            } else {
    330                data   = (void*)byteVertices;
    331                uiSize = 3*(sizeof(GLbyte)*10);
    332            }
    333         }
    334         glBufferData(GL_ARRAY_BUFFER, uiSize,data, GL_STATIC_DRAW);
    335 
    336         ui32IndexVbo = 2;
    337         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ui32IndexVbo);
    338         glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
    339     }
    340 
    341     // Draws a triangle for 800 frames
    342     float angle = 0.0;
    343     glMatrixMode(GL_PROJECTION);
    344     glLoadIdentity();
    345     glMatrixMode(GL_MODELVIEW);
    346     glLoadIdentity();
    347 
    348     GLvoid* arr = NULL;
    349     GLenum  type;
    350     GLenum  drawType;
    351     GLenum  colorType;
    352     int     size_of;
    353 
    354     if(useConvertedType){
    355         if(useFixed)
    356         {
    357             arr = fixedVertices;
    358             colorType = type = GL_FIXED;
    359             size_of = sizeof(GLfixed);
    360         } else {
    361             arr = byteVertices;
    362             colorType = GL_UNSIGNED_BYTE;
    363             type = GL_BYTE;
    364             size_of = sizeof(GLbyte);
    365         }
    366     }else {
    367         arr = afVertices;
    368         colorType = type = GL_FLOAT;
    369         size_of = sizeof(float);
    370     }
    371 
    372     if(usePoints)
    373     {
    374         drawType = GL_POINTS;
    375     }
    376     else
    377         drawType = GL_TRIANGLES;
    378 
    379     GLvoid* data = NULL;
    380     for (int i = 0; i < 100; i++) {
    381 
    382         glClear(GL_COLOR_BUFFER_BIT);
    383         glPushMatrix();
    384         glRotatef(angle, 0.0, 0.0, 1.0);
    385         angle += 360.0 / nframes;
    386         // Enable vertex arrays
    387         glEnableClientState(GL_VERTEX_ARRAY);
    388         if (immidateMode) {
    389             glVertexPointer(3,type, size_of * 10, arr);
    390         } else {
    391             glVertexPointer(3,type, size_of * 10, 0);
    392         }
    393 
    394         // Set color data in the same way
    395         glEnableClientState(GL_COLOR_ARRAY);
    396         if (immidateMode) {
    397             SWITCH_SOURCE(3)
    398             glColorPointer(4, colorType, size_of * 10, data);
    399         } else {
    400             glColorPointer(4,colorType,size_of * 10, (GLvoid*) (size_of * 3) );
    401         }
    402         if (useTexture) {
    403             glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    404             if (immidateMode) {
    405                 SWITCH_SOURCE(7)
    406                 glTexCoordPointer(2, type, size_of * 10,data);
    407             } else {
    408                 glTexCoordPointer(2, type, size_of * 10, (GLvoid*)(size_of * 7));
    409             }
    410         }
    411         if(usePoints)
    412         {
    413             glEnableClientState(GL_POINT_SIZE_ARRAY_OES);
    414             if (immidateMode) {
    415                 SWITCH_SOURCE(9)
    416                 glPointSizePointerOES(type,size_of * 10,data);
    417                         } else {
    418                 glPointSizePointerOES(type,size_of * 10,(GLvoid*)(size_of * 9));
    419                         }
    420         }
    421 
    422         if (useIndices) {
    423             if (immidateMode) {
    424                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    425                 glDrawElements(drawType, 3, GL_UNSIGNED_SHORT, indices);
    426             } else {
    427                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ui32IndexVbo);
    428                 glDrawElements(drawType, 3, GL_UNSIGNED_SHORT, 0);
    429             }
    430         } else {
    431             glDrawArrays(drawType, 0, 3);
    432         }
    433 
    434                 GLenum err = glGetError();
    435                 if(err != GL_NO_ERROR)
    436             printf(" error %d has occured while drawing\n",err);
    437 
    438 
    439         glPopMatrix();
    440                 eglSwapBuffers(d,egl_surface);
    441 
    442                 if(useTexture && useCopy)
    443                     glCopyTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,0,0,256,256,0);
    444                 else if(useTexture && useSubCopy)
    445                     glCopyTexSubImage2D(GL_TEXTURE_2D,0,100,100,WINDOW_WIDTH/2,WINDOW_HEIGHT/2,50,50);
    446     }
    447                 err = glGetError();
    448                 if(err != GL_NO_ERROR)
    449             printf("error ->>> %d  \n",err);
    450         eglDestroySurface(d,egl_surface);
    451         eglDestroyContext(d,ctx);
    452 
    453 // Just wait until the window is closed
    454         SDL_Event ev;
    455         while( SDL_WaitEvent(&ev) ) {
    456             if (ev.type == SDL_QUIT) {
    457                 break;
    458             }
    459         }
    460     return 0;
    461 }
    462 
    463 
    464