1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <time.h>
20 #include <sched.h>
21 #include <sys/resource.h>
22 
23 #include <EGL/egl.h>
24 #include <GLES/gl.h>
25 #include <GLES/glext.h>
26 
27 #include <utils/Timers.h>
28 
29 #include <WindowSurface.h>
30 #include <EGLUtils.h>
31 
32 using namespace android;
33 
main(int argc,char ** argv)34 int main(int argc, char** argv)
35 {
36     EGLint configAttribs[] = {
37          EGL_DEPTH_SIZE, 0,
38          EGL_NONE
39      };
40 
41      EGLint majorVersion;
42      EGLint minorVersion;
43      EGLContext context;
44      EGLConfig config;
45      EGLSurface surface;
46      EGLint w, h;
47      EGLDisplay dpy;
48 
49      WindowSurface windowSurface;
50      EGLNativeWindowType window = windowSurface.getSurface();
51 
52      dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
53      eglInitialize(dpy, &majorVersion, &minorVersion);
54 
55      status_t err = EGLUtils::selectConfigForNativeWindow(
56              dpy, configAttribs, window, &config);
57      if (err) {
58          fprintf(stderr, "couldn't find an EGLConfig matching the screen format\n");
59          return 0;
60      }
61 
62      surface = eglCreateWindowSurface(dpy, config, window, NULL);
63      context = eglCreateContext(dpy, config, NULL, NULL);
64      eglMakeCurrent(dpy, surface, surface, context);
65      eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
66      eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
67      GLint dim = w<h ? w : h;
68 
69      glBindTexture(GL_TEXTURE_2D, 0);
70      glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
71      glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
72      glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
73      glEnable(GL_TEXTURE_2D);
74      glColor4f(1,1,1,1);
75      glDisable(GL_DITHER);
76      glShadeModel(GL_FLAT);
77 
78      long long now, t;
79      int i;
80 
81      char* texels = (char*)malloc(512*512*2);
82      memset(texels,0xFF,512*512*2);
83 
84      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
85              512, 512, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, texels);
86 
87      char* dst = (char*)malloc(320*480*2);
88      memset(dst, 0, 320*480*2);
89      printf("307200 bytes memcpy\n");
90      for (i=0 ; i<4 ; i++) {
91          now = systemTime();
92          memcpy(dst, texels, 320*480*2);
93          t = systemTime();
94          printf("memcpy() time = %llu us\n", (t-now)/1000);
95          fflush(stdout);
96      }
97      free(dst);
98 
99      free(texels);
100 
101      setpriority(PRIO_PROCESS, 0, -20);
102 
103      printf("512x512 unmodified texture, 512x512 blit:\n");
104      glClear(GL_COLOR_BUFFER_BIT);
105      for (i=0 ; i<4 ; i++) {
106          GLint crop[4] = { 0, 512, 512, -512 };
107          glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
108          now = systemTime();
109          glDrawTexiOES(0, 0, 0, 512, 512);
110          glFinish();
111          t = systemTime();
112          printf("glFinish() time = %llu us\n", (t-now)/1000);
113          fflush(stdout);
114          eglSwapBuffers(dpy, surface);
115      }
116 
117      printf("512x512 unmodified texture, 1x1 blit:\n");
118      glClear(GL_COLOR_BUFFER_BIT);
119      for (i=0 ; i<4 ; i++) {
120          GLint crop[4] = { 0, 1, 1, -1 };
121          glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
122          now = systemTime();
123          glDrawTexiOES(0, 0, 0, 1, 1);
124          glFinish();
125          t = systemTime();
126          printf("glFinish() time = %llu us\n", (t-now)/1000);
127          fflush(stdout);
128          eglSwapBuffers(dpy, surface);
129      }
130 
131      printf("512x512 unmodified texture, 512x512 blit (x2):\n");
132      glClear(GL_COLOR_BUFFER_BIT);
133      for (i=0 ; i<4 ; i++) {
134          GLint crop[4] = { 0, 512, 512, -512 };
135          glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
136          now = systemTime();
137          glDrawTexiOES(0, 0, 0, 512, 512);
138          glDrawTexiOES(0, 0, 0, 512, 512);
139          glFinish();
140          t = systemTime();
141          printf("glFinish() time = %llu us\n", (t-now)/1000);
142          fflush(stdout);
143          eglSwapBuffers(dpy, surface);
144      }
145 
146      printf("512x512 unmodified texture, 1x1 blit (x2):\n");
147      glClear(GL_COLOR_BUFFER_BIT);
148      for (i=0 ; i<4 ; i++) {
149          GLint crop[4] = { 0, 1, 1, -1 };
150          glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
151          now = systemTime();
152          glDrawTexiOES(0, 0, 0, 1, 1);
153          glDrawTexiOES(0, 0, 0, 1, 1);
154          glFinish();
155          t = systemTime();
156          printf("glFinish() time = %llu us\n", (t-now)/1000);
157          fflush(stdout);
158          eglSwapBuffers(dpy, surface);
159      }
160 
161 
162      printf("512x512 (1x1 texel MODIFIED texture), 512x512 blit:\n");
163      glClear(GL_COLOR_BUFFER_BIT);
164      for (i=0 ; i<4 ; i++) {
165          uint16_t green = 0x7E0;
166          GLint crop[4] = { 0, 512, 512, -512 };
167          glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
168          glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, &green);
169          now = systemTime();
170          glDrawTexiOES(0, 0, 0, 512, 512);
171          glFinish();
172          t = systemTime();
173          printf("glFinish() time = %llu us\n", (t-now)/1000);
174          fflush(stdout);
175          eglSwapBuffers(dpy, surface);
176      }
177 
178 
179      int16_t texel = 0xF800;
180      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
181              1, 1, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, &texel);
182 
183      printf("1x1 unmodified texture, 1x1 blit:\n");
184      glClear(GL_COLOR_BUFFER_BIT);
185      for (i=0 ; i<4 ; i++) {
186          GLint crop[4] = { 0, 1, 1, -1 };
187          glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
188          now = systemTime();
189          glDrawTexiOES(0, 0, 0, 1, 1);
190          glFinish();
191          t = systemTime();
192          printf("glFinish() time = %llu us\n", (t-now)/1000);
193          eglSwapBuffers(dpy, surface);
194      }
195 
196      printf("1x1 unmodified texture, 512x512 blit:\n");
197      glClear(GL_COLOR_BUFFER_BIT);
198      for (i=0 ; i<4 ; i++) {
199          GLint crop[4] = { 0, 1, 1, -1 };
200          glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
201          now = systemTime();
202          glDrawTexiOES(0, 0, 0, 512, 512);
203          glFinish();
204          t = systemTime();
205          printf("glFinish() time = %llu us\n", (t-now)/1000);
206          fflush(stdout);
207          eglSwapBuffers(dpy, surface);
208      }
209 
210      printf("1x1 (1x1 texel MODIFIED texture), 512x512 blit:\n");
211      glClear(GL_COLOR_BUFFER_BIT);
212      for (i=0 ; i<4 ; i++) {
213          uint16_t green = 0x7E0;
214          GLint crop[4] = { 0, 1, 1, -1 };
215          glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
216          glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, &green);
217          now = systemTime();
218          glDrawTexiOES(0, 0, 0, 1, 1);
219          glFinish();
220          t = systemTime();
221          printf("glFinish() time = %llu us\n", (t-now)/1000);
222          fflush(stdout);
223          eglSwapBuffers(dpy, surface);
224      }
225 
226      return 0;
227 }
228