gl2_yuvtex.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Copyright (C) 2010 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 <EGL/eglext.h>
  23. #include <GLES2/gl2.h>
  24. #include <GLES2/gl2ext.h>
  25. #include <utils/Timers.h>
  26. #include <WindowSurface.h>
  27. #include <ui/GraphicBuffer.h>
  28. #include <EGLUtils.h>
  29. using namespace android;
  30. static void printGLString(const char *name, GLenum s) {
  31. // fprintf(stderr, "printGLString %s, %d\n", name, s);
  32. const char *v = (const char *) glGetString(s);
  33. // int error = glGetError();
  34. // fprintf(stderr, "glGetError() = %d, result of glGetString = %x\n", error,
  35. // (unsigned int) v);
  36. // if ((v < (const char*) 0) || (v > (const char*) 0x10000))
  37. // fprintf(stderr, "GL %s = %s\n", name, v);
  38. // else
  39. // fprintf(stderr, "GL %s = (null) 0x%08x\n", name, (unsigned int) v);
  40. fprintf(stderr, "GL %s = %s\n", name, v);
  41. }
  42. static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
  43. if (returnVal != EGL_TRUE) {
  44. fprintf(stderr, "%s() returned %d\n", op, returnVal);
  45. }
  46. for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
  47. = eglGetError()) {
  48. fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
  49. error);
  50. }
  51. }
  52. static void checkGlError(const char* op) {
  53. for (GLint error = glGetError(); error; error
  54. = glGetError()) {
  55. fprintf(stderr, "after %s() glError (0x%x)\n", op, error);
  56. }
  57. }
  58. static const char gVertexShader[] = "attribute vec4 vPosition;\n"
  59. "varying vec2 yuvTexCoords;\n"
  60. "void main() {\n"
  61. " yuvTexCoords = vPosition.xy + vec2(0.5, 0.5);\n"
  62. " gl_Position = vPosition;\n"
  63. "}\n";
  64. static const char gFragmentShader[] = "#extension GL_OES_EGL_image_external : require\n"
  65. "precision mediump float;\n"
  66. "uniform samplerExternalOES yuvTexSampler;\n"
  67. "varying vec2 yuvTexCoords;\n"
  68. "void main() {\n"
  69. " gl_FragColor = texture2D(yuvTexSampler, yuvTexCoords);\n"
  70. "}\n";
  71. GLuint loadShader(GLenum shaderType, const char* pSource) {
  72. GLuint shader = glCreateShader(shaderType);
  73. if (shader) {
  74. glShaderSource(shader, 1, &pSource, NULL);
  75. glCompileShader(shader);
  76. GLint compiled = 0;
  77. glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
  78. if (!compiled) {
  79. GLint infoLen = 0;
  80. glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
  81. if (infoLen) {
  82. char* buf = (char*) malloc(infoLen);
  83. if (buf) {
  84. glGetShaderInfoLog(shader, infoLen, NULL, buf);
  85. fprintf(stderr, "Could not compile shader %d:\n%s\n",
  86. shaderType, buf);
  87. free(buf);
  88. }
  89. } else {
  90. fprintf(stderr, "Guessing at GL_INFO_LOG_LENGTH size\n");
  91. char* buf = (char*) malloc(0x1000);
  92. if (buf) {
  93. glGetShaderInfoLog(shader, 0x1000, NULL, buf);
  94. fprintf(stderr, "Could not compile shader %d:\n%s\n",
  95. shaderType, buf);
  96. free(buf);
  97. }
  98. }
  99. glDeleteShader(shader);
  100. shader = 0;
  101. }
  102. }
  103. return shader;
  104. }
  105. GLuint createProgram(const char* pVertexSource, const char* pFragmentSource) {
  106. GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource);
  107. if (!vertexShader) {
  108. return 0;
  109. }
  110. GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource);
  111. if (!pixelShader) {
  112. return 0;
  113. }
  114. GLuint program = glCreateProgram();
  115. if (program) {
  116. glAttachShader(program, vertexShader);
  117. checkGlError("glAttachShader");
  118. glAttachShader(program, pixelShader);
  119. checkGlError("glAttachShader");
  120. glLinkProgram(program);
  121. GLint linkStatus = GL_FALSE;
  122. glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
  123. if (linkStatus != GL_TRUE) {
  124. GLint bufLength = 0;
  125. glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
  126. if (bufLength) {
  127. char* buf = (char*) malloc(bufLength);
  128. if (buf) {
  129. glGetProgramInfoLog(program, bufLength, NULL, buf);
  130. fprintf(stderr, "Could not link program:\n%s\n", buf);
  131. free(buf);
  132. }
  133. }
  134. glDeleteProgram(program);
  135. program = 0;
  136. }
  137. }
  138. return program;
  139. }
  140. GLuint gProgram;
  141. GLint gvPositionHandle;
  142. GLint gYuvTexSamplerHandle;
  143. bool setupGraphics(int w, int h) {
  144. gProgram = createProgram(gVertexShader, gFragmentShader);
  145. if (!gProgram) {
  146. return false;
  147. }
  148. gvPositionHandle = glGetAttribLocation(gProgram, "vPosition");
  149. checkGlError("glGetAttribLocation");
  150. fprintf(stderr, "glGetAttribLocation(\"vPosition\") = %d\n",
  151. gvPositionHandle);
  152. gYuvTexSamplerHandle = glGetUniformLocation(gProgram, "yuvTexSampler");
  153. checkGlError("glGetUniformLocation");
  154. fprintf(stderr, "glGetUniformLocation(\"yuvTexSampler\") = %d\n",
  155. gYuvTexSamplerHandle);
  156. glViewport(0, 0, w, h);
  157. checkGlError("glViewport");
  158. return true;
  159. }
  160. int align(int x, int a) {
  161. return (x + (a-1)) & (~(a-1));
  162. }
  163. const int yuvTexWidth = 608;
  164. const int yuvTexHeight = 480;
  165. const int yuvTexUsage = GraphicBuffer::USAGE_HW_TEXTURE |
  166. GraphicBuffer::USAGE_SW_WRITE_RARELY;
  167. const int yuvTexFormat = HAL_PIXEL_FORMAT_YV12;
  168. const int yuvTexOffsetY = 0;
  169. const bool yuvTexSameUV = false;
  170. static sp<GraphicBuffer> yuvTexBuffer;
  171. static GLuint yuvTex;
  172. bool setupYuvTexSurface(EGLDisplay dpy, EGLContext context) {
  173. int blockWidth = yuvTexWidth > 16 ? yuvTexWidth / 16 : 1;
  174. int blockHeight = yuvTexHeight > 16 ? yuvTexHeight / 16 : 1;
  175. yuvTexBuffer = new GraphicBuffer(yuvTexWidth, yuvTexHeight, yuvTexFormat,
  176. yuvTexUsage);
  177. int yuvTexStrideY = yuvTexBuffer->getStride();
  178. int yuvTexOffsetV = yuvTexStrideY * yuvTexHeight;
  179. int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
  180. int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * yuvTexHeight/2;
  181. int yuvTexStrideU = yuvTexStrideV;
  182. char* buf = NULL;
  183. status_t err = yuvTexBuffer->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&buf));
  184. if (err != 0) {
  185. fprintf(stderr, "yuvTexBuffer->lock(...) failed: %d\n", err);
  186. return false;
  187. }
  188. for (int x = 0; x < yuvTexWidth; x++) {
  189. for (int y = 0; y < yuvTexHeight; y++) {
  190. int parityX = (x / blockWidth) & 1;
  191. int parityY = (y / blockHeight) & 1;
  192. unsigned char intensity = (parityX ^ parityY) ? 63 : 191;
  193. buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity;
  194. if (x < yuvTexWidth / 2 && y < yuvTexHeight / 2) {
  195. buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = intensity;
  196. if (yuvTexSameUV) {
  197. buf[yuvTexOffsetV + (y * yuvTexStrideV) + x] = intensity;
  198. } else if (x < yuvTexWidth / 4 && y < yuvTexHeight / 4) {
  199. buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 0] =
  200. buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 1] =
  201. buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 0] =
  202. buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 1] = intensity;
  203. }
  204. }
  205. }
  206. }
  207. err = yuvTexBuffer->unlock();
  208. if (err != 0) {
  209. fprintf(stderr, "yuvTexBuffer->unlock() failed: %d\n", err);
  210. return false;
  211. }
  212. EGLClientBuffer clientBuffer = (EGLClientBuffer)yuvTexBuffer->getNativeBuffer();
  213. EGLImageKHR img = eglCreateImageKHR(dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
  214. clientBuffer, 0);
  215. checkEglError("eglCreateImageKHR");
  216. if (img == EGL_NO_IMAGE_KHR) {
  217. return false;
  218. }
  219. glGenTextures(1, &yuvTex);
  220. checkGlError("glGenTextures");
  221. glBindTexture(GL_TEXTURE_EXTERNAL_OES, yuvTex);
  222. checkGlError("glBindTexture");
  223. glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)img);
  224. checkGlError("glEGLImageTargetTexture2DOES");
  225. return true;
  226. }
  227. const GLfloat gTriangleVertices[] = {
  228. -0.5f, 0.5f,
  229. -0.5f, -0.5f,
  230. 0.5f, -0.5f,
  231. 0.5f, 0.5f,
  232. };
  233. void renderFrame() {
  234. glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
  235. checkGlError("glClearColor");
  236. glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  237. checkGlError("glClear");
  238. glUseProgram(gProgram);
  239. checkGlError("glUseProgram");
  240. glVertexAttribPointer(gvPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, gTriangleVertices);
  241. checkGlError("glVertexAttribPointer");
  242. glEnableVertexAttribArray(gvPositionHandle);
  243. checkGlError("glEnableVertexAttribArray");
  244. glUniform1i(gYuvTexSamplerHandle, 0);
  245. checkGlError("glUniform1i");
  246. glBindTexture(GL_TEXTURE_EXTERNAL_OES, yuvTex);
  247. checkGlError("glBindTexture");
  248. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  249. checkGlError("glDrawArrays");
  250. }
  251. void printEGLConfiguration(EGLDisplay dpy, EGLConfig config) {
  252. #define X(VAL) {VAL, #VAL}
  253. struct {EGLint attribute; const char* name;} names[] = {
  254. X(EGL_BUFFER_SIZE),
  255. X(EGL_ALPHA_SIZE),
  256. X(EGL_BLUE_SIZE),
  257. X(EGL_GREEN_SIZE),
  258. X(EGL_RED_SIZE),
  259. X(EGL_DEPTH_SIZE),
  260. X(EGL_STENCIL_SIZE),
  261. X(EGL_CONFIG_CAVEAT),
  262. X(EGL_CONFIG_ID),
  263. X(EGL_LEVEL),
  264. X(EGL_MAX_PBUFFER_HEIGHT),
  265. X(EGL_MAX_PBUFFER_PIXELS),
  266. X(EGL_MAX_PBUFFER_WIDTH),
  267. X(EGL_NATIVE_RENDERABLE),
  268. X(EGL_NATIVE_VISUAL_ID),
  269. X(EGL_NATIVE_VISUAL_TYPE),
  270. X(EGL_SAMPLES),
  271. X(EGL_SAMPLE_BUFFERS),
  272. X(EGL_SURFACE_TYPE),
  273. X(EGL_TRANSPARENT_TYPE),
  274. X(EGL_TRANSPARENT_RED_VALUE),
  275. X(EGL_TRANSPARENT_GREEN_VALUE),
  276. X(EGL_TRANSPARENT_BLUE_VALUE),
  277. X(EGL_BIND_TO_TEXTURE_RGB),
  278. X(EGL_BIND_TO_TEXTURE_RGBA),
  279. X(EGL_MIN_SWAP_INTERVAL),
  280. X(EGL_MAX_SWAP_INTERVAL),
  281. X(EGL_LUMINANCE_SIZE),
  282. X(EGL_ALPHA_MASK_SIZE),
  283. X(EGL_COLOR_BUFFER_TYPE),
  284. X(EGL_RENDERABLE_TYPE),
  285. X(EGL_CONFORMANT),
  286. };
  287. #undef X
  288. for (size_t j = 0; j < sizeof(names) / sizeof(names[0]); j++) {
  289. EGLint value = -1;
  290. EGLint returnVal = eglGetConfigAttrib(dpy, config, names[j].attribute, &value);
  291. EGLint error = eglGetError();
  292. if (returnVal && error == EGL_SUCCESS) {
  293. printf(" %s: ", names[j].name);
  294. printf("%d (0x%x)", value, value);
  295. }
  296. }
  297. printf("\n");
  298. }
  299. int main(int argc, char** argv) {
  300. EGLBoolean returnValue;
  301. EGLConfig myConfig = {0};
  302. EGLint context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
  303. EGLint s_configAttribs[] = {
  304. EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  305. EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
  306. EGL_NONE };
  307. EGLint majorVersion;
  308. EGLint minorVersion;
  309. EGLContext context;
  310. EGLSurface surface;
  311. EGLint w, h;
  312. EGLDisplay dpy;
  313. checkEglError("<init>");
  314. dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  315. checkEglError("eglGetDisplay");
  316. if (dpy == EGL_NO_DISPLAY) {
  317. printf("eglGetDisplay returned EGL_NO_DISPLAY.\n");
  318. return 0;
  319. }
  320. returnValue = eglInitialize(dpy, &majorVersion, &minorVersion);
  321. checkEglError("eglInitialize", returnValue);
  322. fprintf(stderr, "EGL version %d.%d\n", majorVersion, minorVersion);
  323. if (returnValue != EGL_TRUE) {
  324. printf("eglInitialize failed\n");
  325. return 0;
  326. }
  327. WindowSurface windowSurface;
  328. EGLNativeWindowType window = windowSurface.getSurface();
  329. returnValue = EGLUtils::selectConfigForNativeWindow(dpy, s_configAttribs, window, &myConfig);
  330. if (returnValue) {
  331. printf("EGLUtils::selectConfigForNativeWindow() returned %d", returnValue);
  332. return 1;
  333. }
  334. checkEglError("EGLUtils::selectConfigForNativeWindow");
  335. printf("Chose this configuration:\n");
  336. printEGLConfiguration(dpy, myConfig);
  337. surface = eglCreateWindowSurface(dpy, myConfig, window, NULL);
  338. checkEglError("eglCreateWindowSurface");
  339. if (surface == EGL_NO_SURFACE) {
  340. printf("gelCreateWindowSurface failed.\n");
  341. return 1;
  342. }
  343. context = eglCreateContext(dpy, myConfig, EGL_NO_CONTEXT, context_attribs);
  344. checkEglError("eglCreateContext");
  345. if (context == EGL_NO_CONTEXT) {
  346. printf("eglCreateContext failed\n");
  347. return 1;
  348. }
  349. returnValue = eglMakeCurrent(dpy, surface, surface, context);
  350. checkEglError("eglMakeCurrent", returnValue);
  351. if (returnValue != EGL_TRUE) {
  352. return 1;
  353. }
  354. eglQuerySurface(dpy, surface, EGL_WIDTH, &w);
  355. checkEglError("eglQuerySurface");
  356. eglQuerySurface(dpy, surface, EGL_HEIGHT, &h);
  357. checkEglError("eglQuerySurface");
  358. GLint dim = w < h ? w : h;
  359. fprintf(stderr, "Window dimensions: %d x %d\n", w, h);
  360. printGLString("Version", GL_VERSION);
  361. printGLString("Vendor", GL_VENDOR);
  362. printGLString("Renderer", GL_RENDERER);
  363. printGLString("Extensions", GL_EXTENSIONS);
  364. if(!setupYuvTexSurface(dpy, context)) {
  365. fprintf(stderr, "Could not set up texture surface.\n");
  366. return 1;
  367. }
  368. if(!setupGraphics(w, h)) {
  369. fprintf(stderr, "Could not set up graphics.\n");
  370. return 1;
  371. }
  372. for (;;) {
  373. renderFrame();
  374. eglSwapBuffers(dpy, surface);
  375. checkEglError("eglSwapBuffers");
  376. }
  377. return 0;
  378. }