1 /* 2 * Copyright (C) 2011 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 TRANSLATOR_IFACES_H 17 #define TRANSLATOR_IFACES_H 18 19 #include "base/SmallVector.h" 20 #include "GLcommon/GLutils.h" 21 #include "GLcommon/ShareGroup.h" 22 23 #include <EGL/egl.h> 24 #include <GLES/gl.h> 25 #include <GLES2/gl2.h> 26 #include <GLES3/gl3.h> 27 #include <string.h> 28 29 #include <memory> 30 #include <unordered_map> 31 #include <functional> 32 33 extern "C" { 34 35 /* This is a generic function pointer type, whose name indicates it must 36 * be cast to the proper type *and calling convention* before use. 37 */ 38 typedef void (*__translatorMustCastToProperFunctionPointerType)(void); 39 40 typedef struct { 41 const char* name; 42 __translatorMustCastToProperFunctionPointerType address; 43 } ExtensionDescriptor; 44 45 class SaveableTexture; 46 typedef std::shared_ptr<SaveableTexture> SaveableTexturePtr; 47 48 struct EglImage 49 { ~EglImageEglImage50 ~EglImage(){}; 51 unsigned int imageId; 52 NamedObjectPtr globalTexObj; 53 unsigned int width; 54 unsigned int height; 55 unsigned int internalFormat; 56 unsigned int border; 57 unsigned int format; 58 unsigned int type; 59 unsigned int texStorageLevels; 60 SaveableTexturePtr saveableTexture; 61 bool needRestore; 62 GLsync sync; 63 }; 64 65 typedef std::shared_ptr<EglImage> ImagePtr; 66 typedef std::unordered_map<unsigned int, ImagePtr> ImagesHndlMap; 67 typedef std::unordered_map<unsigned int, SaveableTexturePtr> SaveableTextureMap; 68 69 class GLEScontext; 70 class SaveableTexture; 71 72 namespace android_studio { 73 class EmulatorGLESUsages; 74 } 75 76 typedef struct { 77 void (*initGLESx)(bool isGles2Gles); 78 GLEScontext* (*createGLESContext)(int majorVersion, int minorVersion, GlobalNameSpace* globalNameSpace, android::base::Stream* stream); 79 void (*initContext)(GLEScontext*,ShareGroupPtr); 80 void (*setMaxGlesVersion)(GLESVersion); 81 void (*deleteGLESContext)(GLEScontext*); 82 void (*flush)(); 83 void (*finish)(); 84 int (*getError)(); 85 void (*setShareGroup)(GLEScontext*,ShareGroupPtr); 86 __translatorMustCastToProperFunctionPointerType (*getProcAddress)(const char*); 87 GLsync (*fenceSync)(GLenum, GLbitfield); 88 GLenum (*clientWaitSync)(GLsync, GLbitfield, GLuint64); 89 void (*waitSync)(GLsync, GLbitfield, GLuint64); 90 void (*deleteSync)(GLsync); 91 void (*preSaveTexture)(); 92 void (*postSaveTexture)(); 93 void (*saveTexture)(SaveableTexture*, android::base::Stream*, android::base::SmallVector<unsigned char>* buffer); 94 SaveableTexture* (*createTexture)(GlobalNameSpace*, 95 std::function<void(SaveableTexture*)>&&); 96 void (*restoreTexture)(SaveableTexture*); 97 void (*deleteRbo)(GLuint); 98 void (*blitFromCurrentReadBufferANDROID)(EGLImage); 99 bool (*vulkanInteropSupported)(); 100 void (*getSynciv)(GLsync, GLenum pname, GLsizei bufSize, GLsizei *length, GLint *values); 101 } GLESiface; 102 103 class GlLibrary; 104 105 // A structure containing function pointers implemented by the EGL 106 // translator library, and called from the GLES 1.x and 2.0 translator 107 // libraries. 108 struct EGLiface { 109 // Get the current GLESContext instance for the current thread. 110 GLEScontext* (*getGLESContext)(); 111 112 // Retrieve a shared pointer to a global EglImage object named |imageId| 113 ImagePtr (*getEGLImage)(unsigned int imageId); 114 115 // Return instance of GlLibrary class to use for dispatch. 116 // at runtime. This is implemented in the EGL library because on Windows 117 // all functions returned by wglGetProcAddress() are context-dependent! 118 GlLibrary* (*eglGetGlLibrary)(); 119 120 // Context creation functions for auxiliary functions (e.g., 121 // background loading). Instead of going through the standard 122 // eglChooseConfig/eglCreatePbufferSurface/eglCreateContext/eglMakeCurrent, 123 // these functions set up a minimal context with pre-set configuration 124 // that allows one to easily run GL calls in separate threads. 125 // createAndBindAuxiliaryContext(): Creates a minimal context (1x1 pbuffer, with first config) 126 // and makes it the current context and writes the resulting context and surface to 127 // |context_out| and |surface_out|. Returns false if any step of context creation failed, 128 // true otherwise. 129 bool (*createAndBindAuxiliaryContext)(EGLContext* context_out, EGLSurface* surface_out); 130 // unbindAndDestroyAuxiliaryContext(): Cleans up the resulting |context| and |surface| from a 131 // createAndBindAuxiliaryContext() call; binds the null context and surface, then 132 // destroys the surface and context. Returns false if any step of cleanup failed, 133 // true otherwise. 134 bool (*unbindAndDestroyAuxiliaryContext)(EGLContext context, EGLSurface surface); 135 // Makes |context| and |surface| current, assuming they have been created already. 136 bool (*bindAuxiliaryContext)(EGLContext context, EGLSurface surface); 137 // Makes EGL_NO_SURFACE and EGL_NO_CONTEXT current, unbinding whatever is the current context. 138 bool (*unbindAuxiliaryContext)(); 139 140 // When we need to get function pointers via eglGetProcAddress. 141 std::function<void*(const char*)> getProcAddress; 142 }; 143 144 typedef GLESiface* (*__translator_getGLESIfaceFunc)(const EGLiface*); 145 146 } 147 #endif 148