1/*
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include <stdlib.h>
19#include <stdio.h>
20
21#include <EGL/egl.h>
22#include <GLES/gl.h>
23#include <GLES/glext.h>
24
25#include <ui/FramebufferNativeWindow.h>
26#include "EGLUtils.h"
27
28using namespace android;
29
30int main(int argc, char** argv)
31{
32    EGLint configAttribs[] = {
33         EGL_DEPTH_SIZE, 0,
34         EGL_NONE
35     };
36
37     EGLint majorVersion;
38     EGLint minorVersion;
39     EGLContext context;
40     EGLConfig config;
41     EGLSurface surface;
42     EGLint w, h;
43     EGLDisplay dpy;
44
45     EGLNativeWindowType window = android_createDisplaySurface();
46
47     dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
48     eglInitialize(dpy, &majorVersion, &minorVersion);
49
50     status_t err = EGLUtils::selectConfigForNativeWindow(
51             dpy, configAttribs, window, &config);
52     if (err) {
53         fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n");
54         return 0;
55     }
56
57     surface = eglCreateWindowSurface(dpy, config, window, NULL);
58     context = eglCreateContext(dpy, config, NULL, NULL);
59     eglMakeCurrent(dpy, surface, surface, context);
60     eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
61     eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
62     GLint dim = w<h ? w : h;
63
64
65     GLint crop[4] = { 0, 4, 4, -4 };
66     glBindTexture(GL_TEXTURE_2D, 0);
67     glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
68     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
69     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
70     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
71     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
72     glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
73     glEnable(GL_TEXTURE_2D);
74     glColor4f(1,1,1,1);
75
76     // packing is always 4
77     uint8_t t8[]  = {
78             0x00, 0x55, 0x00, 0x55,
79             0xAA, 0xFF, 0xAA, 0xFF,
80             0x00, 0x55, 0x00, 0x55,
81             0xAA, 0xFF, 0xAA, 0xFF  };
82
83     uint16_t t16[]  = {
84             0x0000, 0x5555, 0x0000, 0x5555,
85             0xAAAA, 0xFFFF, 0xAAAA, 0xFFFF,
86             0x0000, 0x5555, 0x0000, 0x5555,
87             0xAAAA, 0xFFFF, 0xAAAA, 0xFFFF  };
88
89     uint16_t t5551[]  = {
90             0x0000, 0xFFFF, 0x0000, 0xFFFF,
91             0xFFFF, 0x0000, 0xFFFF, 0x0000,
92             0x0000, 0xFFFF, 0x0000, 0xFFFF,
93             0xFFFF, 0x0000, 0xFFFF, 0x0000  };
94
95     uint32_t t32[]  = {
96             0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFFFF0000,
97             0xFF00FF00, 0xFFFF0000, 0xFF000000, 0xFF0000FF,
98             0xFF00FFFF, 0xFF00FF00, 0x00FF00FF, 0xFFFFFF00,
99             0xFF000000, 0xFFFF00FF, 0xFF00FFFF, 0xFFFFFFFF
100     };
101
102
103     glClear(GL_COLOR_BUFFER_BIT);
104     glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 4, 4, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, t8);
105     glDrawTexiOES(0, 0, 0, dim/2, dim/2);
106
107     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, t16);
108     glDrawTexiOES(dim/2, 0, 0, dim/2, dim/2);
109
110     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, t16);
111     glDrawTexiOES(0, dim/2, 0, dim/2, dim/2);
112
113     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE, t32);
114     glDrawTexiOES(dim/2, dim/2, 0, dim/2, dim/2);
115
116     eglSwapBuffers(dpy, surface);
117     return 0;
118}
119