1 /* 2 * Copyright (C) 2016 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 #pragma once 17 18 #include <assert.h> 19 #include <GLES/gl.h> 20 #include <memory> 21 22 enum class NamedObjectType : short { 23 NULLTYPE, 24 VERTEXBUFFER, 25 TEXTURE, 26 RENDERBUFFER, 27 FRAMEBUFFER, 28 SHADER_OR_PROGRAM, 29 SAMPLER, 30 QUERY, 31 VERTEX_ARRAY_OBJECT, 32 TRANSFORM_FEEDBACK, 33 NUM_OBJECT_TYPES // Must be last 34 }; 35 36 enum class ShaderProgramType : short { 37 PROGRAM, 38 VERTEX_SHADER, 39 FRAGMENT_SHADER, 40 COMPUTE_SHADER, 41 }; 42 43 // 44 // Class GenNameInfo - this class contains the type of an GL object to be 45 // generated. It also contains shader type when generating 46 // shader object. 47 // It is used by GlobalNameSpace::genName. 48 // When generating an object that is not shader or program, 49 // a NamedObjectType parameter should be passed to the 50 // constructor. When generating shader / program object, the 51 // ShaderProgramType should be passed to the constructor. 52 // Example: 53 // GlobalNameSpace globalNameSpace; 54 // GenNameInfo genTexture(NamedObjectType::TEXTURE); 55 // unsigned int texID = globalNameSpace.genName(genTexture); 56 // GenNameInfo genShader(ShaderProgramType::FRAGMENT_SHADER); 57 // unsigned int shaderID = globalNameSpace.genName(genShader); 58 59 struct GenNameInfo { 60 NamedObjectType m_type = (NamedObjectType)0; 61 // only used for NamedObjectType::SHADER_OR_PROGRAM 62 ShaderProgramType m_shaderProgramType = (ShaderProgramType)0; 63 // only used for NamedObjectType::SHADER_OR_PROGRAM, so far. 64 GLuint m_existingGlobal = 0; 65 66 GenNameInfo() = delete; 67 // constructor for generating non-shader object GenNameInfoGenNameInfo68 explicit GenNameInfo(NamedObjectType type) : m_type(type) { 69 assert(type != NamedObjectType::SHADER_OR_PROGRAM); 70 } 71 // constructor for generating shader object 72 explicit GenNameInfo(ShaderProgramType shaderProgramType, 73 GLuint existingGlobal = 0) : m_typeGenNameInfo74 m_type(NamedObjectType::SHADER_OR_PROGRAM), 75 m_shaderProgramType(shaderProgramType), 76 m_existingGlobal(existingGlobal) {} 77 }; 78 79 80 typedef unsigned long long ObjectLocalName; 81 82 class GlobalNameSpace; 83 84 // NamedObject is used to manage GPU names. 85 // It generates / releases GPU names. 86 // NamedObject is expected to be encapsulated in a shared pointer when used (see 87 // NamedObjectPtr). The main purpose to use smart pointers is to have reference 88 // counters for texture objects, which are frequently shared across contexts and 89 // share groups when used in EGLImages. The specification of EGLImage says: 90 // 91 // Once an EGLImage is created from an EGLImage source, the memory associated 92 // with the EGLImage source will remain allocated (and all EGLImage siblings 93 // in all client API contexts will be useable) as long as either of the 94 // following conditions is true: 95 // A) Any EGLImage siblings exist in any client API context 96 // B) The EGLImage object exists inside EGL 97 // Reference: 2.5.2 Lifetime and Usage of EGLImages in 98 // https://www.khronos.org/registry/egl/extensions/KHR/EGL_KHR_image_base.txt 99 // 100 // In our situation, "the memory associated with the EGLImage source" means the 101 // textures. Thus a shared pointer is needed. 102 103 class NamedObject { 104 public: 105 NamedObject(GenNameInfo genNameInfo, 106 GlobalNameSpace *globalNameSpace); 107 ~NamedObject(); getGlobalName()108 unsigned int getGlobalName() const {return m_globalName;} 109 private: 110 // m_globalName is the name generated by GPU 111 unsigned int m_globalName = 0; 112 NamedObjectType m_type; 113 GlobalNameSpace *m_globalNameSpace; 114 }; 115 116 typedef std::shared_ptr<NamedObject> NamedObjectPtr; 117