| Chris Craik | e4aa95e | 2014-05-08 13:57:05 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Reck | 2de7771 | 2016-01-20 11:09:53 -0800 | [diff] [blame] | 19 | #include "Debug.h" | 
|  | 20 |  | 
| Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 21 | #include <log/log.h> | 
| John Reck | 9372ac3 | 2016-01-19 11:46:52 -0800 | [diff] [blame] | 22 |  | 
| Stan Iliev | 11606ff | 2018-09-17 14:01:16 -0400 | [diff] [blame] | 23 | #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 Craik | e4aa95e | 2014-05-08 13:57:05 -0700 | [diff] [blame] | 29 | namespace android { | 
|  | 30 | namespace uirenderer { | 
|  | 31 |  | 
| John Reck | 2de7771 | 2016-01-20 11:09:53 -0800 | [diff] [blame] | 32 | #if DEBUG_OPENGL | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 33 | #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 Reck | 9372ac3 | 2016-01-19 11:46:52 -0800 | [diff] [blame] | 40 | #else | 
| John Reck | 975591a | 2016-01-22 16:28:07 -0800 | [diff] [blame] | 41 | #define GL_CHECKPOINT(LEVEL) | 
| John Reck | 9372ac3 | 2016-01-19 11:46:52 -0800 | [diff] [blame] | 42 | #endif | 
|  | 43 |  | 
| Chris Craik | e4aa95e | 2014-05-08 13:57:05 -0700 | [diff] [blame] | 44 | class GLUtils { | 
| Chris Craik | e4aa95e | 2014-05-08 13:57:05 -0700 | [diff] [blame] | 45 | public: | 
|  | 46 | /** | 
| Chris Craik | 5686bae | 2015-06-23 10:33:46 -0700 | [diff] [blame] | 47 | * Print out any GL errors with ALOGE, returns true if any errors were found. | 
| John Reck | 975591a | 2016-01-22 16:28:07 -0800 | [diff] [blame] | 48 | * You probably want to use GL_CHECKPOINT(LEVEL) instead of calling this directly | 
| Chris Craik | e4aa95e | 2014-05-08 13:57:05 -0700 | [diff] [blame] | 49 | */ | 
| Chris Craik | 5686bae | 2015-06-23 10:33:46 -0700 | [diff] [blame] | 50 | static bool dumpGLErrors(); | 
| Chris Craik | e4aa95e | 2014-05-08 13:57:05 -0700 | [diff] [blame] | 51 |  | 
| Stan Iliev | 11606ff | 2018-09-17 14:01:16 -0400 | [diff] [blame] | 52 | static const char* getGLFramebufferError(); | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 53 | };  // class GLUtils | 
| Chris Craik | e4aa95e | 2014-05-08 13:57:05 -0700 | [diff] [blame] | 54 |  | 
| Stan Iliev | 11606ff | 2018-09-17 14:01:16 -0400 | [diff] [blame] | 55 | class AutoEglImage { | 
|  | 56 | public: | 
|  | 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 |  | 
|  | 71 | private: | 
|  | 72 | EGLDisplay mDisplay = EGL_NO_DISPLAY; | 
|  | 73 | }; | 
|  | 74 |  | 
|  | 75 | class AutoSkiaGlTexture { | 
|  | 76 | public: | 
|  | 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 |  | 
|  | 87 | class AutoGLFramebuffer { | 
|  | 88 | public: | 
|  | 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 Craik | e4aa95e | 2014-05-08 13:57:05 -0700 | [diff] [blame] | 99 | } /* namespace uirenderer */ | 
|  | 100 | } /* namespace android */ | 
|  | 101 |  | 
|  | 102 | #endif /* GLUTILS_H */ |