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#define LOG_TAG "fillrate"
19
20#include <stdlib.h>
21#include <stdio.h>
22
23#include <EGL/egl.h>
24#include <GLES/gl.h>
25#include <GLES/glext.h>
26
27#include <utils/StopWatch.h>
28#include <WindowSurface.h>
29#include <EGLUtils.h>
30
31using namespace android;
32
33int main(int argc, char** argv)
34{
35    EGLint configAttribs[] = {
36         EGL_DEPTH_SIZE, 0,
37         EGL_NONE
38     };
39
40     EGLint majorVersion;
41     EGLint minorVersion;
42     EGLContext context;
43     EGLConfig config;
44     EGLSurface surface;
45     EGLint w, h;
46     EGLDisplay dpy;
47
48     WindowSurface windowSurface;
49     EGLNativeWindowType window = windowSurface.getSurface();
50
51     dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
52     eglInitialize(dpy, &majorVersion, &minorVersion);
53
54     status_t err = EGLUtils::selectConfigForNativeWindow(
55             dpy, configAttribs, window, &config);
56     if (err) {
57         fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n");
58         return 0;
59     }
60
61     surface = eglCreateWindowSurface(dpy, config, window, NULL);
62     context = eglCreateContext(dpy, config, NULL, NULL);
63     eglMakeCurrent(dpy, surface, surface, context);
64     eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
65     eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
66
67     printf("w=%d, h=%d\n", w, h);
68
69     glBindTexture(GL_TEXTURE_2D, 0);
70     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
71     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
72     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
73     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
74     glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
75     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
76     glDisable(GL_DITHER);
77     glEnable(GL_BLEND);
78     glEnable(GL_TEXTURE_2D);
79     glColor4f(1,1,1,1);
80
81     uint32_t* t32 = (uint32_t*)malloc(512*512*4);
82     for (int y=0 ; y<512 ; y++) {
83         for (int x=0 ; x<512 ; x++) {
84             int u = x-256;
85             int v = y-256;
86             if (u*u+v*v < 256*256) {
87                 t32[x+y*512] = 0x10FFFFFF;
88             } else {
89                 t32[x+y*512] = 0x20FF0000;
90             }
91         }
92     }
93
94     const GLfloat vertices[4][2] = {
95             { 0,  0 },
96             { 0,  h },
97             { w,  h },
98             { w,  0 }
99     };
100
101     const GLfloat texCoords[4][2] = {
102             { 0,  0 },
103             { 0,  1 },
104             { 1,  1 },
105             { 1,  0 }
106     };
107
108     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, t32);
109
110     glViewport(0, 0, w, h);
111     glMatrixMode(GL_PROJECTION);
112     glLoadIdentity();
113     glOrthof(0, w, 0, h, 0, 1);
114
115     glEnableClientState(GL_VERTEX_ARRAY);
116     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
117     glVertexPointer(2, GL_FLOAT, 0, vertices);
118     glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
119
120     eglSwapInterval(dpy, 1);
121
122     glClearColor(1,0,0,0);
123     glClear(GL_COLOR_BUFFER_BIT);
124     glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
125     eglSwapBuffers(dpy, surface);
126
127
128     nsecs_t times[32];
129
130     for (int c=1 ; c<32 ; c++) {
131         glClear(GL_COLOR_BUFFER_BIT);
132         for (int i=0 ; i<c ; i++) {
133             glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
134         }
135         eglSwapBuffers(dpy, surface);
136     }
137
138
139     //     for (int c=31 ; c>=1 ; c--) {
140     int j=0;
141     for (int c=1 ; c<32 ; c++) {
142         glClear(GL_COLOR_BUFFER_BIT);
143         nsecs_t now = systemTime();
144         for (int i=0 ; i<c ; i++) {
145             glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
146         }
147         eglSwapBuffers(dpy, surface);
148         nsecs_t t = systemTime() - now;
149         times[j++] = t;
150     }
151
152     for (int c=1, j=0 ; c<32 ; c++, j++) {
153         nsecs_t t = times[j];
154         printf("%lld\t%d\t%f\n", t, c, (double(t)/c)/1000000.0);
155     }
156
157
158
159     eglTerminate(dpy);
160
161     return 0;
162}
163