blob: ca8810bde0707c4e039527912e7f434b533ebbec [file] [log] [blame]
Chris Craike4aa95e2014-05-08 13:57:05 -07001/*
2 * Copyright (C) 2014 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#ifndef GLUTILS_H
17#define GLUTILS_H
18
John Reck2de77712016-01-20 11:09:53 -080019#include "Debug.h"
20
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070021#include <log/log.h>
John Reck9372ac32016-01-19 11:46:52 -080022
Stan Iliev11606ff2018-09-17 14:01:16 -040023#include <EGL/egl.h>
24#include <EGL/eglext.h>
25#include <GLES2/gl2.h>
26#include <GLES2/gl2ext.h>
27#include <GLES3/gl3.h>
28
Chris Craike4aa95e2014-05-08 13:57:05 -070029namespace android {
30namespace uirenderer {
31
John Reck2de77712016-01-20 11:09:53 -080032#if DEBUG_OPENGL
John Reck1bcacfd2017-11-03 10:12:19 -070033#define GL_CHECKPOINT(LEVEL) \
34 do { \
35 if (DEBUG_OPENGL >= DEBUG_LEVEL_##LEVEL) { \
36 LOG_ALWAYS_FATAL_IF(android::uirenderer::GLUtils::dumpGLErrors(), "GL errors! %s:%d", \
37 __FILE__, __LINE__); \
38 } \
39 } while (0)
John Reck9372ac32016-01-19 11:46:52 -080040#else
John Reck975591a2016-01-22 16:28:07 -080041#define GL_CHECKPOINT(LEVEL)
John Reck9372ac32016-01-19 11:46:52 -080042#endif
43
Chris Craike4aa95e2014-05-08 13:57:05 -070044class GLUtils {
Chris Craike4aa95e2014-05-08 13:57:05 -070045public:
46 /**
Chris Craik5686bae2015-06-23 10:33:46 -070047 * Print out any GL errors with ALOGE, returns true if any errors were found.
John Reck975591a2016-01-22 16:28:07 -080048 * You probably want to use GL_CHECKPOINT(LEVEL) instead of calling this directly
Chris Craike4aa95e2014-05-08 13:57:05 -070049 */
Chris Craik5686bae2015-06-23 10:33:46 -070050 static bool dumpGLErrors();
Chris Craike4aa95e2014-05-08 13:57:05 -070051
Stan Iliev11606ff2018-09-17 14:01:16 -040052 static const char* getGLFramebufferError();
John Reck1bcacfd2017-11-03 10:12:19 -070053}; // class GLUtils
Chris Craike4aa95e2014-05-08 13:57:05 -070054
Stan Iliev11606ff2018-09-17 14:01:16 -040055class AutoEglImage {
56public:
57 AutoEglImage(EGLDisplay display, EGLClientBuffer clientBuffer) : mDisplay(display) {
58 EGLint imageAttrs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE};
59 image = eglCreateImageKHR(display, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, clientBuffer,
60 imageAttrs);
61 }
62
63 ~AutoEglImage() {
64 if (image != EGL_NO_IMAGE_KHR) {
65 eglDestroyImageKHR(mDisplay, image);
66 }
67 }
68
69 EGLImageKHR image = EGL_NO_IMAGE_KHR;
70
71private:
72 EGLDisplay mDisplay = EGL_NO_DISPLAY;
73};
74
75class AutoSkiaGlTexture {
76public:
77 AutoSkiaGlTexture() {
78 glGenTextures(1, &mTexture);
79 glBindTexture(GL_TEXTURE_2D, mTexture);
80 }
81
82 ~AutoSkiaGlTexture() { glDeleteTextures(1, &mTexture); }
83
84 GLuint mTexture = 0;
85};
86
87class AutoGLFramebuffer {
88public:
89 AutoGLFramebuffer() {
90 glGenFramebuffers(1, &mFb);
91 glBindFramebuffer(GL_FRAMEBUFFER, mFb);
92 }
93
94 ~AutoGLFramebuffer() { glDeleteFramebuffers(1, &mFb); }
95
96 GLuint mFb;
97};
98
Chris Craike4aa95e2014-05-08 13:57:05 -070099} /* namespace uirenderer */
100} /* namespace android */
101
102#endif /* GLUTILS_H */