Home | History | Annotate | Download | only in utils
      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 
     19 #include "Debug.h"
     20 
     21 #include <log/log.h>
     22 
     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 
     29 namespace android {
     30 namespace uirenderer {
     31 
     32 #if DEBUG_OPENGL
     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)
     40 #else
     41 #define GL_CHECKPOINT(LEVEL)
     42 #endif
     43 
     44 class GLUtils {
     45 public:
     46     /**
     47      * Print out any GL errors with ALOGE, returns true if any errors were found.
     48      * You probably want to use GL_CHECKPOINT(LEVEL) instead of calling this directly
     49      */
     50     static bool dumpGLErrors();
     51 
     52     static const char* getGLFramebufferError();
     53 };  // class GLUtils
     54 
     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 
     99 } /* namespace uirenderer */
    100 } /* namespace android */
    101 
    102 #endif /* GLUTILS_H */
    103