Home | History | Annotate | Download | only in filter
      1 #include <stdlib.h>
      2 #include <stdio.h>
      3 
      4 #include <EGL/egl.h>
      5 #include <GLES/gl.h>
      6 #include <GLES/glext.h>
      7 
      8 #include <WindowSurface.h>
      9 #include <EGLUtils.h>
     10 
     11 using namespace android;
     12 
     13 #define USE_DRAW_TEXTURE 1
     14 
     15 int main(int argc, char** argv)
     16 {
     17     if (argc!=2 && argc!=3) {
     18         printf("usage: %s <0-6> [pbuffer]\n", argv[0]);
     19         return 0;
     20     }
     21 
     22     const int test = atoi(argv[1]);
     23     int usePbuffer = argc==3 && !strcmp(argv[2], "pbuffer");
     24     EGLint s_configAttribs[] = {
     25          EGL_SURFACE_TYPE, EGL_PBUFFER_BIT|EGL_WINDOW_BIT,
     26          EGL_RED_SIZE,       5,
     27          EGL_GREEN_SIZE,     6,
     28          EGL_BLUE_SIZE,      5,
     29          EGL_NONE
     30      };
     31 
     32      EGLint numConfigs = -1;
     33      EGLint majorVersion;
     34      EGLint minorVersion;
     35      EGLConfig config;
     36      EGLContext context;
     37      EGLSurface surface;
     38      EGLint w, h;
     39 
     40      EGLDisplay dpy;
     41 
     42      EGLNativeWindowType window = 0;
     43      WindowSurface* windowSurface = NULL;
     44      if (!usePbuffer) {
     45          windowSurface = new WindowSurface();
     46          window = windowSurface->getSurface();
     47      }
     48 
     49      dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
     50      eglInitialize(dpy, &majorVersion, &minorVersion);
     51      if (!usePbuffer) {
     52          EGLUtils::selectConfigForNativeWindow(
     53                  dpy, s_configAttribs, window, &config);
     54          surface = eglCreateWindowSurface(dpy, config, window, NULL);
     55      } else {
     56          printf("using pbuffer\n");
     57          eglChooseConfig(dpy, s_configAttribs, &config, 1, &numConfigs);
     58          EGLint attribs[] = { EGL_WIDTH, 320, EGL_HEIGHT, 480, EGL_NONE };
     59          surface = eglCreatePbufferSurface(dpy, config, attribs);
     60          if (surface == EGL_NO_SURFACE) {
     61              printf("eglCreatePbufferSurface error %x\n", eglGetError());
     62          }
     63      }
     64      context = eglCreateContext(dpy, config, NULL, NULL);
     65      eglMakeCurrent(dpy, surface, surface, context);
     66      eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
     67      eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
     68      GLint dim = w<h ? w : h;
     69 
     70      glViewport(0, 0, w, h);
     71      glMatrixMode(GL_PROJECTION);
     72      glLoadIdentity();
     73      glOrthof(0, w, 0, h, 0, 1);
     74 
     75      glClearColor(0,0,0,0);
     76      glClear(GL_COLOR_BUFFER_BIT);
     77 
     78      GLint crop[4] = { 0, 4, 4, -4 };
     79      glBindTexture(GL_TEXTURE_2D, 0);
     80      glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
     81      glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     82      glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     83      glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
     84      glEnable(GL_TEXTURE_2D);
     85      glColor4f(1,1,1,1);
     86 
     87      // packing is always 4
     88      uint8_t t8[]  = {
     89              0x00, 0x55, 0x00, 0x55,
     90              0xAA, 0xFF, 0xAA, 0xFF,
     91              0x00, 0x55, 0x00, 0x55,
     92              0xAA, 0xFF, 0xAA, 0xFF  };
     93 
     94      uint16_t t16[]  = {
     95              0x0000, 0x5555, 0x0000, 0x5555,
     96              0xAAAA, 0xFFFF, 0xAAAA, 0xFFFF,
     97              0x0000, 0x5555, 0x0000, 0x5555,
     98              0xAAAA, 0xFFFF, 0xAAAA, 0xFFFF  };
     99 
    100      uint16_t t5551[]  = {
    101              0x0000, 0xFFFF, 0x0000, 0xFFFF,
    102              0xFFFF, 0x0000, 0xFFFF, 0x0000,
    103              0x0000, 0xFFFF, 0x0000, 0xFFFF,
    104              0xFFFF, 0x0000, 0xFFFF, 0x0000  };
    105 
    106      uint32_t t32[]  = {
    107              0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFFFF0000,
    108              0xFF00FF00, 0xFFFF0000, 0xFF000000, 0xFF0000FF,
    109              0xFF00FFFF, 0xFF00FF00, 0x00FF00FF, 0xFFFFFF00,
    110              0xFF000000, 0xFFFF00FF, 0xFF00FFFF, 0xFFFFFFFF
    111      };
    112 
    113      switch(test)
    114      {
    115      case 1:
    116          glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE,
    117                  4, 4, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, t8);
    118          break;
    119      case 2:
    120          glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
    121                  4, 4, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, t16);
    122          break;
    123      case 3:
    124          glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
    125                  4, 4, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, t16);
    126          break;
    127      case 4:
    128          glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA,
    129                  4, 4, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, t16);
    130          break;
    131      case 5:
    132          glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
    133                  4, 4, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, t5551);
    134          break;
    135      case 6:
    136          glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
    137                  4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE, t32);
    138          break;
    139      }
    140 
    141      //glDrawTexiOES(0, 0, 0, dim, dim);
    142 
    143 #if !USE_DRAW_TEXTURE || !GL_OES_draw_texture
    144      const GLfloat fdim = dim;
    145      const GLfloat vertices[4][2] = {
    146              { 0,     0    },
    147              { 0,     fdim },
    148              { fdim,  fdim },
    149              { fdim,  0    }
    150      };
    151 
    152      const GLfloat texCoords[4][2] = {
    153              { 0,  0 },
    154              { 0,  1 },
    155              { 1,  1 },
    156              { 1,  0 }
    157      };
    158 #endif
    159 
    160      if (!usePbuffer) {
    161          eglSwapBuffers(dpy, surface);
    162      }
    163 
    164      glMatrixMode(GL_MODELVIEW);
    165      glScissor(0,dim,dim,h-dim);
    166      glDisable(GL_SCISSOR_TEST);
    167 
    168      for (int y=0 ; y<dim ; y++) {
    169          //glDisable(GL_SCISSOR_TEST);
    170          glClear(GL_COLOR_BUFFER_BIT);
    171 
    172          //glEnable(GL_SCISSOR_TEST);
    173 
    174 #if USE_DRAW_TEXTURE && GL_OES_draw_texture
    175          glDrawTexiOES(0, y, 1, dim, dim);
    176 #else
    177          glLoadIdentity();
    178          glTranslatef(0, y, 0);
    179          glEnableClientState(GL_VERTEX_ARRAY);
    180          glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    181          glVertexPointer(2, GL_FLOAT, 0, vertices);
    182          glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
    183          glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
    184 #endif
    185 
    186          if (!usePbuffer) {
    187              eglSwapBuffers(dpy, surface);
    188          } else {
    189              glFinish();
    190          }
    191      }
    192 
    193      eglTerminate(dpy);
    194      delete windowSurface;
    195      return 0;
    196 }
    197