blob: 5eeca85204ec3a09875b2eed1426c3b2de5fdf3b [file] [log] [blame]
Zach Reizner09807052015-08-13 14:53:41 -07001/*
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
34namespace 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
48AUTO_GL_TYPE(AutoGLFramebuffer, GLuint, 0, glDeleteFramebuffers(1, &p))
49AUTO_GL_TYPE(AutoGLBuffer, GLuint, 0, glDeleteBuffers(1, &p))
50AUTO_GL_TYPE(AutoGLTexture, GLuint, 0, glDeleteTextures(1, &p))
51AUTO_GL_TYPE(AutoGLShader, GLint, 0, glDeleteShader(p))
52AUTO_GL_TYPE(AutoGLProgram, GLint, 0, glDeleteProgram(p))
53
54struct AutoEGLDisplayImage {
55 AutoEGLDisplayImage() : display_(EGL_NO_DISPLAY), image_(EGL_NO_IMAGE_KHR) {
56 }
57
58 AutoEGLDisplayImage(EGLDisplay display, EGLImageKHR image)
59 : display_(display), image_(image) {
60 }
61
62 AutoEGLDisplayImage(const AutoEGLDisplayImage& rhs) = delete;
63 AutoEGLDisplayImage(AutoEGLDisplayImage&& rhs) {
Season Lie823f642015-10-07 17:47:55 -070064 display_ = rhs.display_;
65 image_ = rhs.image_;
66 rhs.display_ = EGL_NO_DISPLAY;
67 rhs.image_ = EGL_NO_IMAGE_KHR;
Zach Reizner09807052015-08-13 14:53:41 -070068 }
69
70 ~AutoEGLDisplayImage() {
71 clear();
72 }
73
74 AutoEGLDisplayImage& operator=(const AutoEGLDisplayImage& rhs) = delete;
75 AutoEGLDisplayImage& operator=(AutoEGLDisplayImage&& rhs) {
76 clear();
77 std::swap(display_, rhs.display_);
78 std::swap(image_, rhs.image_);
79 return *this;
80 }
81
82 void reset(EGLDisplay display, EGLImageKHR image) {
83 clear();
84 display_ = display;
85 image_ = image;
86 }
87
88 void clear() {
89 if (image_ != EGL_NO_IMAGE_KHR) {
90 eglDestroyImageKHR(display_, image_);
91 display_ = EGL_NO_DISPLAY;
92 image_ = EGL_NO_IMAGE_KHR;
93 }
94 }
95
96 EGLImageKHR image() const {
97 return image_;
98 }
99
100 private:
101 EGLDisplay display_;
102 EGLImageKHR image_;
103};
104
105struct AutoEGLImageAndGLTexture {
106 AutoEGLDisplayImage image;
107 AutoGLTexture texture;
108};
109}
110
111#endif