gl2_copyTexImage.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <time.h>
  19. #include <sched.h>
  20. #include <sys/resource.h>
  21. #include <EGL/egl.h>
  22. #include <GLES2/gl2.h>
  23. #include <GLES2/gl2ext.h>
  24. #include <utils/Timers.h>
  25. #include <WindowSurface.h>
  26. #include <EGLUtils.h>
  27. using namespace android;
  28. static void printGLString(const char *name, GLenum s) {
  29. // fprintf(stderr, "printGLString %s, %d\n", name, s);
  30. const char *v = (const char *) glGetString(s);
  31. // int error = glGetError();
  32. // fprintf(stderr, "glGetError() = %d, result of glGetString = %x\n", error,
  33. // (unsigned int) v);
  34. // if ((v < (const char*) 0) || (v > (const char*) 0x10000))
  35. // fprintf(stderr, "GL %s = %s\n", name, v);
  36. // else
  37. // fprintf(stderr, "GL %s = (null) 0x%08x\n", name, (unsigned int) v);
  38. fprintf(stderr, "GL %s = %s\n", name, v);
  39. }
  40. static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
  41. if (returnVal != EGL_TRUE) {
  42. fprintf(stderr, "%s() returned %d\n", op, returnVal);
  43. }
  44. for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
  45. = eglGetError()) {
  46. fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
  47. error);
  48. }
  49. }
  50. static void checkGlError(const char* op) {
  51. for (GLint error = glGetError(); error; error
  52. = glGetError()) {
  53. fprintf(stderr, "after %s() glError (0x%x)\n", op, error);
  54. }
  55. }
  56. static const char gVertexShader[] = "attribute vec4 vPosition;\n"
  57. "void main() {\n"
  58. " gl_Position = vPosition;\n"
  59. "}\n";
  60. static const char gFragmentShader[] = "precision mediump float;\n"
  61. "void main() {\n"
  62. " gl_FragColor = vec4(0.0, 1.0, 0.0, 0.5);\n"
  63. "}\n";
  64. GLuint loadShader(GLenum shaderType, const char* pSource) {
  65. GLuint shader = glCreateShader(shaderType);
  66. if (shader) {
  67. glShaderSource(shader, 1, &pSource, NULL);
  68. glCompileShader(shader);
  69. GLint compiled = 0;
  70. glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
  71. if (!compiled) {
  72. GLint infoLen = 0;
  73. glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
  74. if (infoLen) {
  75. char* buf = (char*) malloc(infoLen);
  76. if (buf) {
  77. glGetShaderInfoLog(shader, infoLen, NULL, buf);
  78. fprintf(stderr, "Could not compile shader %d:\n%s\n",
  79. shaderType, buf);
  80. free(buf);
  81. }
  82. glDeleteShader(shader);
  83. shader = 0;
  84. }
  85. }
  86. }
  87. return shader;
  88. }
  89. GLuint createProgram(const char* pVertexSource, const char* pFragmentSource) {
  90. GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource);
  91. if (!vertexShader) {
  92. return 0;
  93. }
  94. GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource);
  95. if (!pixelShader) {
  96. return 0;
  97. }
  98. GLuint program = glCreateProgram();
  99. if (program) {
  100. glAttachShader(program, vertexShader);
  101. checkGlError("glAttachShader");
  102. glAttachShader(program, pixelShader);
  103. checkGlError("glAttachShader");
  104. glLinkProgram(program);
  105. GLint linkStatus = GL_FALSE;
  106. glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
  107. if (linkStatus != GL_TRUE) {
  108. GLint bufLength = 0;
  109. glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
  110. if (bufLength) {
  111. char* buf = (char*) malloc(bufLength);
  112. if (buf) {
  113. glGetProgramInfoLog(program, bufLength, NULL, buf);
  114. fprintf(stderr, "Could not link program:\n%s\n", buf);
  115. free(buf);
  116. }
  117. }
  118. glDeleteProgram(program);
  119. program = 0;
  120. }
  121. }
  122. return program;
  123. }
  124. GLuint gProgram;
  125. GLuint gTextureProgram;
  126. GLuint gvPositionHandle;
  127. GLuint gvTexturePositionHandle;
  128. GLuint gvTextureTexCoordsHandle;
  129. GLuint gvTextureSamplerHandle;
  130. GLuint gFbo;
  131. GLuint gTexture;
  132. GLuint gBufferTexture;
  133. static const char gSimpleVS[] =
  134. "attribute vec4 position;\n"
  135. "attribute vec2 texCoords;\n"
  136. "varying vec2 outTexCoords;\n"
  137. "\nvoid main(void) {\n"
  138. " outTexCoords = texCoords;\n"
  139. " gl_Position = position;\n"
  140. "}\n\n";
  141. static const char gSimpleFS[] =
  142. "precision mediump float;\n\n"
  143. "varying vec2 outTexCoords;\n"
  144. "uniform sampler2D texture;\n"
  145. "\nvoid main(void) {\n"
  146. " gl_FragColor = texture2D(texture, outTexCoords);\n"
  147. "}\n\n";
  148. bool setupGraphics(int w, int h) {
  149. gProgram = createProgram(gVertexShader, gFragmentShader);
  150. if (!gProgram) {
  151. return false;
  152. }
  153. gvPositionHandle = glGetAttribLocation(gProgram, "vPosition");
  154. checkGlError("glGetAttribLocation");
  155. fprintf(stderr, "glGetAttribLocation(\"vPosition\") = %d\n", gvPositionHandle);
  156. gTextureProgram = createProgram(gSimpleVS, gSimpleFS);
  157. if (!gTextureProgram) {
  158. return false;
  159. }
  160. gvTexturePositionHandle = glGetAttribLocation(gTextureProgram, "position");
  161. checkGlError("glGetAttribLocation");
  162. gvTextureTexCoordsHandle = glGetAttribLocation(gTextureProgram, "texCoords");
  163. checkGlError("glGetAttribLocation");
  164. gvTextureSamplerHandle = glGetUniformLocation(gTextureProgram, "texture");
  165. checkGlError("glGetAttribLocation");
  166. glActiveTexture(GL_TEXTURE0);
  167. glGenTextures(1, &gTexture);
  168. glBindTexture(GL_TEXTURE_2D, gTexture);
  169. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  170. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  171. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  172. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  173. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  174. glGenTextures(1, &gBufferTexture);
  175. glBindTexture(GL_TEXTURE_2D, gBufferTexture);
  176. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  177. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  178. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  179. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  180. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  181. glGenFramebuffers(1, &gFbo);
  182. glBindFramebuffer(GL_FRAMEBUFFER, gFbo);
  183. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gTexture, 0);
  184. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  185. glViewport(0, 0, w, h);
  186. checkGlError("glViewport");
  187. return true;
  188. }
  189. const GLfloat gTriangleVertices[] = { 0.0f, 0.5f, -0.5f, -0.5f,
  190. 0.5f, -0.5f };
  191. const GLint FLOAT_SIZE_BYTES = 4;
  192. const GLint TRIANGLE_VERTICES_DATA_STRIDE_BYTES = 5 * FLOAT_SIZE_BYTES;
  193. const GLfloat gTriangleVerticesData[] = {
  194. // X, Y, Z, U, V
  195. -1.0f, -1.0f, 0, 0.f, 0.f,
  196. 1.0f, -1.0f, 0, 1.f, 0.f,
  197. -1.0f, 1.0f, 0, 0.f, 1.f,
  198. 1.0f, 1.0f, 0, 1.f, 1.f,
  199. };
  200. void renderFrame(GLint w, GLint h) {
  201. glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
  202. checkGlError("glClearColor");
  203. glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  204. checkGlError("glClear");
  205. // Bind FBO and draw into it
  206. glBindFramebuffer(GL_FRAMEBUFFER, gFbo);
  207. checkGlError("glBindFramebuffer");
  208. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  209. checkGlError("glClearColor");
  210. glClear(GL_COLOR_BUFFER_BIT);
  211. checkGlError("glClear");
  212. glUseProgram(gProgram);
  213. checkGlError("glUseProgram");
  214. glVertexAttribPointer(gvPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, gTriangleVertices);
  215. checkGlError("glVertexAttribPointer");
  216. glEnableVertexAttribArray(gvPositionHandle);
  217. checkGlError("glEnableVertexAttribArray");
  218. glDrawArrays(GL_TRIANGLES, 0, 3);
  219. checkGlError("glDrawArrays");
  220. // Copy content of FBO into a texture
  221. glBindTexture(GL_TEXTURE_2D, gBufferTexture);
  222. glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, w / 2, h / 2);
  223. checkGlError("glCopyTexSubImage2D");
  224. // Back to the display
  225. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  226. checkGlError("glBindFramebuffer");
  227. // Draw copied content on the screen
  228. glUseProgram(gTextureProgram);
  229. checkGlError("glUseProgram");
  230. glEnable(GL_BLEND);
  231. glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  232. glVertexAttribPointer(gvTexturePositionHandle, 3, GL_FLOAT, GL_FALSE,
  233. TRIANGLE_VERTICES_DATA_STRIDE_BYTES, gTriangleVerticesData);
  234. checkGlError("glVertexAttribPointer");
  235. glVertexAttribPointer(gvTextureTexCoordsHandle, 2, GL_FLOAT, GL_FALSE,
  236. TRIANGLE_VERTICES_DATA_STRIDE_BYTES, &gTriangleVerticesData[3]);
  237. checkGlError("glVertexAttribPointer");
  238. glEnableVertexAttribArray(gvTexturePositionHandle);
  239. glEnableVertexAttribArray(gvTextureTexCoordsHandle);
  240. checkGlError("glEnableVertexAttribArray");
  241. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  242. checkGlError("glDrawArrays");
  243. }
  244. void printEGLConfiguration(EGLDisplay dpy, EGLConfig config) {
  245. #define X(VAL) {VAL, #VAL}
  246. struct {EGLint attribute; const char* name;} names[] = {
  247. X(EGL_BUFFER_SIZE),
  248. X(EGL_ALPHA_SIZE),
  249. X(EGL_BLUE_SIZE),
  250. X(EGL_GREEN_SIZE),
  251. X(EGL_RED_SIZE),
  252. X(EGL_DEPTH_SIZE),
  253. X(EGL_STENCIL_SIZE),
  254. X(EGL_CONFIG_CAVEAT),
  255. X(EGL_CONFIG_ID),
  256. X(EGL_LEVEL),
  257. X(EGL_MAX_PBUFFER_HEIGHT),
  258. X(EGL_MAX_PBUFFER_PIXELS),
  259. X(EGL_MAX_PBUFFER_WIDTH),
  260. X(EGL_NATIVE_RENDERABLE),
  261. X(EGL_NATIVE_VISUAL_ID),
  262. X(EGL_NATIVE_VISUAL_TYPE),
  263. X(EGL_SAMPLES),
  264. X(EGL_SAMPLE_BUFFERS),
  265. X(EGL_SURFACE_TYPE),
  266. X(EGL_TRANSPARENT_TYPE),
  267. X(EGL_TRANSPARENT_RED_VALUE),
  268. X(EGL_TRANSPARENT_GREEN_VALUE),
  269. X(EGL_TRANSPARENT_BLUE_VALUE),
  270. X(EGL_BIND_TO_TEXTURE_RGB),
  271. X(EGL_BIND_TO_TEXTURE_RGBA),
  272. X(EGL_MIN_SWAP_INTERVAL),
  273. X(EGL_MAX_SWAP_INTERVAL),
  274. X(EGL_LUMINANCE_SIZE),
  275. X(EGL_ALPHA_MASK_SIZE),
  276. X(EGL_COLOR_BUFFER_TYPE),
  277. X(EGL_RENDERABLE_TYPE),
  278. X(EGL_CONFORMANT),
  279. };
  280. #undef X
  281. for (size_t j = 0; j < sizeof(names) / sizeof(names[0]); j++) {
  282. EGLint value = -1;
  283. EGLint returnVal = eglGetConfigAttrib(dpy, config, names[j].attribute, &value);
  284. EGLint error = eglGetError();
  285. if (returnVal && error == EGL_SUCCESS) {
  286. printf(" %s: ", names[j].name);
  287. printf("%d (0x%x)", value, value);
  288. }
  289. }
  290. printf("\n");
  291. }
  292. int printEGLConfigurations(EGLDisplay dpy) {
  293. EGLint numConfig = 0;
  294. EGLint returnVal = eglGetConfigs(dpy, NULL, 0, &numConfig);
  295. checkEglError("eglGetConfigs", returnVal);
  296. if (!returnVal) {
  297. return false;
  298. }
  299. printf("Number of EGL configuration: %d\n", numConfig);
  300. EGLConfig* configs = (EGLConfig*) malloc(sizeof(EGLConfig) * numConfig);
  301. if (! configs) {
  302. printf("Could not allocate configs.\n");
  303. return false;
  304. }
  305. returnVal = eglGetConfigs(dpy, configs, numConfig, &numConfig);
  306. checkEglError("eglGetConfigs", returnVal);
  307. if (!returnVal) {
  308. free(configs);
  309. return false;
  310. }
  311. for(int i = 0; i < numConfig; i++) {
  312. printf("Configuration %d\n", i);
  313. printEGLConfiguration(dpy, configs[i]);
  314. }
  315. free(configs);
  316. return true;
  317. }
  318. int main(int /*argc*/, char** /*argv*/) {
  319. EGLBoolean returnValue;
  320. EGLConfig myConfig = {0};
  321. EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
  322. EGLint s_configAttribs[] = {
  323. EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  324. EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
  325. EGL_RED_SIZE, 8,
  326. EGL_GREEN_SIZE, 8,
  327. EGL_BLUE_SIZE, 8,
  328. EGL_ALPHA_SIZE, 8,
  329. EGL_NONE };
  330. EGLint majorVersion;
  331. EGLint minorVersion;
  332. EGLContext context;
  333. EGLSurface surface;
  334. EGLint w, h;
  335. EGLDisplay dpy;
  336. checkEglError("<init>");
  337. dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  338. checkEglError("eglGetDisplay");
  339. if (dpy == EGL_NO_DISPLAY) {
  340. printf("eglGetDisplay returned EGL_NO_DISPLAY.\n");
  341. return 0;
  342. }
  343. returnValue = eglInitialize(dpy, &majorVersion, &minorVersion);
  344. checkEglError("eglInitialize", returnValue);
  345. fprintf(stderr, "EGL version %d.%d\n", majorVersion, minorVersion);
  346. if (returnValue != EGL_TRUE) {
  347. printf("eglInitialize failed\n");
  348. return 0;
  349. }
  350. if (!printEGLConfigurations(dpy)) {
  351. printf("printEGLConfigurations failed\n");
  352. return 0;
  353. }
  354. checkEglError("printEGLConfigurations");
  355. WindowSurface windowSurface;
  356. EGLNativeWindowType window = windowSurface.getSurface();
  357. EGLint numConfigs = -1, n = 0;
  358. eglChooseConfig(dpy, s_configAttribs, 0, 0, &numConfigs);
  359. if (numConfigs) {
  360. EGLConfig* const configs = new EGLConfig[numConfigs];
  361. eglChooseConfig(dpy, s_configAttribs, configs, numConfigs, &n);
  362. myConfig = configs[0];
  363. delete[] configs;
  364. }
  365. checkEglError("EGLUtils::selectConfigForNativeWindow");
  366. printf("Chose this configuration:\n");
  367. printEGLConfiguration(dpy, myConfig);
  368. surface = eglCreateWindowSurface(dpy, myConfig, window, NULL);
  369. checkEglError("eglCreateWindowSurface");
  370. if (surface == EGL_NO_SURFACE) {
  371. printf("gelCreateWindowSurface failed.\n");
  372. return 0;
  373. }
  374. context = eglCreateContext(dpy, myConfig, EGL_NO_CONTEXT, context_attribs);
  375. checkEglError("eglCreateContext");
  376. if (context == EGL_NO_CONTEXT) {
  377. printf("eglCreateContext failed\n");
  378. return 0;
  379. }
  380. returnValue = eglMakeCurrent(dpy, surface, surface, context);
  381. checkEglError("eglMakeCurrent", returnValue);
  382. if (returnValue != EGL_TRUE) {
  383. return 0;
  384. }
  385. eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
  386. checkEglError("eglQuerySurface");
  387. eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
  388. checkEglError("eglQuerySurface");
  389. fprintf(stderr, "Window dimensions: %d x %d\n", w, h);
  390. printGLString("Version", GL_VERSION);
  391. printGLString("Vendor", GL_VENDOR);
  392. printGLString("Renderer", GL_RENDERER);
  393. printGLString("Extensions", GL_EXTENSIONS);
  394. if(!setupGraphics(w, h)) {
  395. fprintf(stderr, "Could not set up graphics.\n");
  396. return 0;
  397. }
  398. for (;;) {
  399. renderFrame(w, h);
  400. eglSwapBuffers(dpy, surface);
  401. checkEglError("eglSwapBuffers");
  402. }
  403. return 0;
  404. }