fillrate.cpp revision bb99ffb4db008441e6ac4236d8a48b6e2b0c01d9
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 <ui/FramebufferNativeWindow.h>
29
30using namespace android;
31
32int main(int argc, char** argv)
33{
34    EGLint configAttribs[] = {
35         EGL_DEPTH_SIZE, 0,
36         EGL_NONE
37     };
38
39     EGLint numConfigs = -1, n=0;
40     EGLint majorVersion;
41     EGLint minorVersion;
42     EGLConfig config;
43     EGLContext context;
44     EGLSurface surface;
45     EGLint w, h;
46
47     EGLDisplay dpy;
48
49     dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
50     eglInitialize(dpy, &majorVersion, &minorVersion);
51
52     // Get all the "potential match" configs...
53     eglGetConfigs(dpy, NULL, 0, &numConfigs);
54     EGLConfig* const configs = (EGLConfig*)malloc(sizeof(EGLConfig)*numConfigs);
55     eglChooseConfig(dpy, configAttribs, configs, numConfigs, &n);
56     config = configs[0];
57     if (n > 1) {
58         // if there is more than one candidate, go through the list
59         // and pick one that matches our framebuffer format
60         int fbSzA = 0; // should not hardcode
61         int fbSzR = 5; // should not hardcode
62         int fbSzG = 6; // should not hardcode
63         int fbSzB = 5; // should not hardcode
64         int i;
65         for (i=0 ; i<n ; i++) {
66             EGLint r,g,b,a;
67             eglGetConfigAttrib(dpy, configs[i], EGL_RED_SIZE,   &r);
68             eglGetConfigAttrib(dpy, configs[i], EGL_GREEN_SIZE, &g);
69             eglGetConfigAttrib(dpy, configs[i], EGL_BLUE_SIZE,  &b);
70             eglGetConfigAttrib(dpy, configs[i], EGL_ALPHA_SIZE, &a);
71             if (fbSzA == a && fbSzR == r && fbSzG == g && fbSzB  == b) {
72                 config = configs[i];
73                 break;
74             }
75         }
76     }
77     free(configs);
78
79     surface = eglCreateWindowSurface(dpy, config,
80             android_createDisplaySurface(), NULL);
81     context = eglCreateContext(dpy, config, NULL, NULL);
82     eglMakeCurrent(dpy, surface, surface, context);
83     eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
84     eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
85
86     printf("w=%d, h=%d\n", w, h);
87
88     glBindTexture(GL_TEXTURE_2D, 0);
89     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
90     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
91     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
92     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
93     glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
94     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
95     glDisable(GL_DITHER);
96     glEnable(GL_BLEND);
97     glEnable(GL_TEXTURE_2D);
98     glColor4f(1,1,1,1);
99
100     uint32_t* t32 = (uint32_t*)malloc(512*512*4);
101     for (int y=0 ; y<512 ; y++) {
102         for (int x=0 ; x<512 ; x++) {
103             int u = x-256;
104             int v = y-256;
105             if (u*u+v*v < 256*256) {
106                 t32[x+y*512] = 0x10FFFFFF;
107             } else {
108                 t32[x+y*512] = 0x20FF0000;
109             }
110         }
111     }
112
113     const GLfloat vertices[4][2] = {
114             { 0,  0 },
115             { 0,  h },
116             { w,  h },
117             { w,  0 }
118     };
119
120     const GLfloat texCoords[4][2] = {
121             { 0,  0 },
122             { 0,  1 },
123             { 1,  1 },
124             { 1,  0 }
125     };
126
127     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, t32);
128
129     glViewport(0, 0, w, h);
130     glMatrixMode(GL_PROJECTION);
131     glLoadIdentity();
132     glOrthof(0, w, 0, h, 0, 1);
133
134     glEnableClientState(GL_VERTEX_ARRAY);
135     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
136     glVertexPointer(2, GL_FLOAT, 0, vertices);
137     glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
138
139     eglSwapInterval(dpy, 1);
140
141     glClearColor(1,0,0,0);
142     glClear(GL_COLOR_BUFFER_BIT);
143     glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
144     eglSwapBuffers(dpy, surface);
145
146
147     nsecs_t times[32];
148
149     for (int c=1 ; c<32 ; c++) {
150         glClear(GL_COLOR_BUFFER_BIT);
151         for (int i=0 ; i<c ; i++) {
152             glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
153         }
154         eglSwapBuffers(dpy, surface);
155     }
156
157
158     //     for (int c=31 ; c>=1 ; c--) {
159     int j=0;
160     for (int c=1 ; c<32 ; c++) {
161         glClear(GL_COLOR_BUFFER_BIT);
162         nsecs_t now = systemTime();
163         for (int i=0 ; i<c ; i++) {
164             glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
165         }
166         eglSwapBuffers(dpy, surface);
167         nsecs_t t = systemTime() - now;
168         times[j++] = t;
169     }
170
171     for (int c=1, j=0 ; c<32 ; c++, j++) {
172         nsecs_t t = times[j];
173         printf("%lld\t%d\t%f\n", t, c, (double(t)/c)/1000000.0);
174     }
175
176
177
178     eglTerminate(dpy);
179
180     return 0;
181}
182