• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 
17 #ifndef ANDROID_AUTO_GL_H_
18 #define ANDROID_AUTO_GL_H_
19 
20 #include <memory>
21 #define EGL_EGLEXT_PROTOTYPES
22 #define GL_GLEXT_PROTOTYPES
23 
24 #include <EGL/egl.h>
25 #include <EGL/eglext.h>
26 #include <GLES2/gl2.h>
27 #include <GLES2/gl2ext.h>
28 
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
32 #endif
33 
34 namespace android {
35 
36 #define AUTO_GL_TYPE(name, type, zero, deleter) \
37   struct name##Deleter {                        \
38     typedef type pointer;                       \
39                                                 \
40     void operator()(pointer p) const {          \
41       if (p != zero) {                          \
42         deleter;                                \
43       }                                         \
44     }                                           \
45   };                                            \
46   typedef std::unique_ptr<type, name##Deleter> name;
47 
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))
53 
54 struct AutoEGLDisplayImage {
55   AutoEGLDisplayImage() = default;
56 
AutoEGLDisplayImageAutoEGLDisplayImage57   AutoEGLDisplayImage(EGLDisplay display, EGLImageKHR image)
58       : display_(display), image_(image) {
59   }
60 
61   AutoEGLDisplayImage(const AutoEGLDisplayImage& rhs) = delete;
AutoEGLDisplayImageAutoEGLDisplayImage62   AutoEGLDisplayImage(AutoEGLDisplayImage&& rhs) {
63     display_ = rhs.display_;
64     image_ = rhs.image_;
65     rhs.display_ = EGL_NO_DISPLAY;
66     rhs.image_ = EGL_NO_IMAGE_KHR;
67   }
68 
~AutoEGLDisplayImageAutoEGLDisplayImage69   ~AutoEGLDisplayImage() {
70     clear();
71   }
72 
73   AutoEGLDisplayImage& operator=(const AutoEGLDisplayImage& rhs) = delete;
74   AutoEGLDisplayImage& operator=(AutoEGLDisplayImage&& rhs) {
75     clear();
76     std::swap(display_, rhs.display_);
77     std::swap(image_, rhs.image_);
78     return *this;
79   }
80 
resetAutoEGLDisplayImage81   void reset(EGLDisplay display, EGLImageKHR image) {
82     clear();
83     display_ = display;
84     image_ = image;
85   }
86 
clearAutoEGLDisplayImage87   void clear() {
88     if (image_ != EGL_NO_IMAGE_KHR) {
89       eglDestroyImageKHR(display_, image_);
90       display_ = EGL_NO_DISPLAY;
91       image_ = EGL_NO_IMAGE_KHR;
92     }
93   }
94 
imageAutoEGLDisplayImage95   EGLImageKHR image() const {
96     return image_;
97   }
98 
99  private:
100   EGLDisplay display_ = EGL_NO_DISPLAY;
101   EGLImageKHR image_ = EGL_NO_IMAGE_KHR;
102 };
103 
104 struct AutoEGLImageAndGLTexture {
105   AutoEGLDisplayImage image;
106   AutoGLTexture texture;
107 };
108 }
109 
110 #endif
111