egl_native_x11.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <EGL/egl.h>
  5. #include <EGL/eglext.h>
  6. extern "C" {
  7. #if defined(GLES2_CONFORM_SUPPORT_ONLY)
  8. #include "gpu/gles2_conform_support/gtf/gtf_stubs.h"
  9. #else
  10. #include "third_party/gles2_conform/GTF_ES/glsl/GTF/Source/eglNative.h" // nogncheck
  11. #endif
  12. EGLImageKHR GTFCreateEGLImage(int width, int height,
  13. GLenum format, GLenum type) {
  14. PFNEGLCREATEIMAGEKHRPROC egl_create_image_khr_;
  15. egl_create_image_khr_ = reinterpret_cast<PFNEGLCREATEIMAGEKHRPROC>
  16. (eglGetProcAddress("eglCreateImageKHR"));
  17. static const EGLint attrib[] = {
  18. EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
  19. EGL_GL_TEXTURE_LEVEL_KHR, 0,
  20. EGL_NONE
  21. };
  22. if (format != GL_RGBA && format != GL_RGB)
  23. return static_cast<EGLImageKHR>(nullptr);
  24. if (type != GL_UNSIGNED_BYTE)
  25. return static_cast<EGLImageKHR>(nullptr);
  26. GLuint texture;
  27. glGenTextures(1, &texture);
  28. glBindTexture(GL_TEXTURE_2D, texture);
  29. glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type,
  30. nullptr);
  31. // Disable mip-maps because we do not require it.
  32. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  33. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  34. if(glGetError() != GL_NO_ERROR)
  35. return static_cast<EGLImageKHR>(nullptr);
  36. EGLImageKHR egl_image =
  37. egl_create_image_khr_(eglGetCurrentDisplay(),
  38. eglGetCurrentContext(),
  39. EGL_GL_TEXTURE_2D_KHR,
  40. reinterpret_cast<EGLClientBuffer>(texture),
  41. attrib);
  42. if (eglGetError() == EGL_SUCCESS)
  43. return egl_image;
  44. else
  45. return static_cast<EGLImageKHR>(nullptr);
  46. }
  47. void GTFDestroyEGLImage(EGLImageKHR image) {
  48. PFNEGLDESTROYIMAGEKHRPROC egl_destroy_image_khr_;
  49. egl_destroy_image_khr_ = reinterpret_cast<PFNEGLDESTROYIMAGEKHRPROC>
  50. (eglGetProcAddress("eglDestroyImageKHR"));
  51. egl_destroy_image_khr_(eglGetCurrentDisplay(), image);
  52. }
  53. } // extern "C"