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
11using namespace android;
12
13#define USE_DRAW_TEXTURE 1
14
15int 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     const GLfloat vertices[4][2] = {
144             { 0,    0   },
145             { 0,    dim },
146             { dim,  dim },
147             { dim,  0   }
148     };
149
150     const GLfloat texCoords[4][2] = {
151             { 0,  0 },
152             { 0,  1 },
153             { 1,  1 },
154             { 1,  0 }
155     };
156
157     if (!usePbuffer) {
158         eglSwapBuffers(dpy, surface);
159     }
160
161     glMatrixMode(GL_MODELVIEW);
162     glScissor(0,dim,dim,h-dim);
163     glDisable(GL_SCISSOR_TEST);
164
165     for (int y=0 ; y<dim ; y++) {
166         //glDisable(GL_SCISSOR_TEST);
167         glClear(GL_COLOR_BUFFER_BIT);
168
169         //glEnable(GL_SCISSOR_TEST);
170
171#if USE_DRAW_TEXTURE && GL_OES_draw_texture
172         glDrawTexiOES(0, y, 1, dim, dim);
173#else
174         glLoadIdentity();
175         glTranslatef(0, y, 0);
176         glEnableClientState(GL_VERTEX_ARRAY);
177         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
178         glVertexPointer(2, GL_FLOAT, 0, vertices);
179         glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
180         glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
181#endif
182
183         if (!usePbuffer) {
184             eglSwapBuffers(dpy, surface);
185         } else {
186             glFinish();
187         }
188     }
189
190     eglTerminate(dpy);
191     delete windowSurface;
192     return 0;
193}
194