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
5#include <EGL/egl.h>
6#include <EGL/eglext.h>
7
8extern "C" {
9#if defined(GLES2_CONFORM_SUPPORT_ONLY)
10#include "gpu/gles2_conform_support/gtf/gtf_stubs.h"
11#else
12#include "third_party/gles2_conform/GTF_ES/glsl/GTF/Source/eglNative.h"
13#endif
14
15EGLImageKHR GTFCreateEGLImage(int width, int height,
16                              GLenum format, GLenum type) {
17  PFNEGLCREATEIMAGEKHRPROC egl_create_image_khr_;
18  egl_create_image_khr_ = reinterpret_cast<PFNEGLCREATEIMAGEKHRPROC>
19                            (eglGetProcAddress("eglCreateImageKHR"));
20
21  static const EGLint attrib[] = {
22    EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
23    EGL_GL_TEXTURE_LEVEL_KHR, 0,
24    EGL_NONE
25  };
26
27  if (format != GL_RGBA && format != GL_RGB)
28    return static_cast<EGLImageKHR>(NULL);
29
30  if (type != GL_UNSIGNED_BYTE)
31    return static_cast<EGLImageKHR>(NULL);
32
33  GLuint texture;
34  glGenTextures(1, &texture);
35  glBindTexture(GL_TEXTURE_2D, texture);
36  glTexImage2D(GL_TEXTURE_2D,
37               0,
38               format,
39               width,
40               height,
41               0,
42               format,
43               type,
44               NULL);
45
46  // Disable mip-maps because we do not require it.
47  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
48  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
49
50  if(glGetError() != GL_NO_ERROR)
51    return static_cast<EGLImageKHR>(NULL);
52
53  EGLImageKHR egl_image =
54      egl_create_image_khr_(eglGetCurrentDisplay(),
55                            eglGetCurrentContext(),
56                            EGL_GL_TEXTURE_2D_KHR,
57                            reinterpret_cast<EGLClientBuffer>(texture),
58                            attrib);
59
60  if (eglGetError() == EGL_SUCCESS)
61    return egl_image;
62  else
63    return static_cast<EGLImageKHR>(NULL);
64}
65
66void GTFDestroyEGLImage(EGLImageKHR image) {
67  PFNEGLDESTROYIMAGEKHRPROC egl_destroy_image_khr_;
68  egl_destroy_image_khr_ = reinterpret_cast<PFNEGLDESTROYIMAGEKHRPROC>
69                             (eglGetProcAddress("eglDestroyImageKHR"));
70
71  egl_destroy_image_khr_(eglGetCurrentDisplay(), image);
72}
73
74}  // extern "C"
75