2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_AUTO_GL_H_
18 #define ANDROID_AUTO_GL_H_
21 #define EGL_EGLEXT_PROTOTYPES
22 #define GL_GLEXT_PROTOTYPES
25 #include <EGL/eglext.h>
26 #include <GLES2/gl2.h>
27 #include <GLES2/gl2ext.h>
29 // TODO(zachr): use hwc_drm_bo to turn buffer handles into textures
30 #ifndef EGL_NATIVE_HANDLE_ANDROID_NVX
31 #define EGL_NATIVE_HANDLE_ANDROID_NVX 0x322A
36 #define AUTO_GL_TYPE(name, type, zero, deleter) \
37 struct name##Deleter { \
38 typedef type pointer; \
40 void operator()(pointer p) const { \
46 typedef std::unique_ptr<type, name##Deleter> name;
48 AUTO_GL_TYPE(AutoGLFramebuffer, GLuint, 0, glDeleteFramebuffers(1, &p))
49 AUTO_GL_TYPE(AutoGLBuffer, GLuint, 0, glDeleteBuffers(1, &p))
50 AUTO_GL_TYPE(AutoGLTexture, GLuint, 0, glDeleteTextures(1, &p))
51 AUTO_GL_TYPE(AutoGLShader, GLint, 0, glDeleteShader(p))
52 AUTO_GL_TYPE(AutoGLProgram, GLint, 0, glDeleteProgram(p))
54 struct AutoEGLDisplayImage {
55 AutoEGLDisplayImage() = default;
57 AutoEGLDisplayImage(EGLDisplay display, EGLImageKHR image)
58 : display_(display), image_(image) {
61 AutoEGLDisplayImage(const AutoEGLDisplayImage& rhs) = delete;
62 AutoEGLDisplayImage(AutoEGLDisplayImage&& rhs) {
63 display_ = rhs.display_;
65 rhs.display_ = EGL_NO_DISPLAY;
66 rhs.image_ = EGL_NO_IMAGE_KHR;
69 ~AutoEGLDisplayImage() {
73 AutoEGLDisplayImage& operator=(const AutoEGLDisplayImage& rhs) = delete;
74 AutoEGLDisplayImage& operator=(AutoEGLDisplayImage&& rhs) {
76 std::swap(display_, rhs.display_);
77 std::swap(image_, rhs.image_);
81 void reset(EGLDisplay display, EGLImageKHR image) {
88 if (image_ != EGL_NO_IMAGE_KHR) {
89 eglDestroyImageKHR(display_, image_);
90 display_ = EGL_NO_DISPLAY;
91 image_ = EGL_NO_IMAGE_KHR;
95 EGLImageKHR image() const {
100 EGLDisplay display_ = EGL_NO_DISPLAY;
101 EGLImageKHR image_ = EGL_NO_IMAGE_KHR;
104 struct AutoEGLImageAndGLTexture {
105 AutoEGLDisplayImage image;
106 AutoGLTexture texture;