1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // ResourceManager.cpp: Implements the ResourceManager class, which tracks and 16 // retrieves objects which may be shared by multiple Contexts. 17 18 #include "ResourceManager.h" 19 20 #include "Buffer.h" 21 #include "Renderbuffer.h" 22 #include "Texture.h" 23 24 namespace es1 25 { 26 ResourceManager::ResourceManager() 27 { 28 mRefCount = 1; 29 } 30 31 ResourceManager::~ResourceManager() 32 { 33 while(!mBufferNameSpace.empty()) 34 { 35 deleteBuffer(mBufferNameSpace.firstName()); 36 } 37 38 while(!mRenderbufferNameSpace.empty()) 39 { 40 deleteRenderbuffer(mRenderbufferNameSpace.firstName()); 41 } 42 43 while(!mTextureNameSpace.empty()) 44 { 45 deleteTexture(mTextureNameSpace.firstName()); 46 } 47 } 48 49 void ResourceManager::addRef() 50 { 51 mRefCount++; 52 } 53 54 void ResourceManager::release() 55 { 56 if(--mRefCount == 0) 57 { 58 delete this; 59 } 60 } 61 62 // Returns an unused buffer name 63 GLuint ResourceManager::createBuffer() 64 { 65 return mBufferNameSpace.allocate(); 66 } 67 68 // Returns an unused texture name 69 GLuint ResourceManager::createTexture() 70 { 71 return mTextureNameSpace.allocate(); 72 } 73 74 // Returns an unused renderbuffer name 75 GLuint ResourceManager::createRenderbuffer() 76 { 77 return mRenderbufferNameSpace.allocate(); 78 } 79 80 void ResourceManager::deleteBuffer(GLuint buffer) 81 { 82 Buffer *bufferObject = mBufferNameSpace.remove(buffer); 83 84 if(bufferObject) 85 { 86 bufferObject->release(); 87 } 88 } 89 90 void ResourceManager::deleteTexture(GLuint texture) 91 { 92 Texture *textureObject = mTextureNameSpace.remove(texture); 93 94 if(textureObject) 95 { 96 textureObject->release(); 97 } 98 } 99 100 void ResourceManager::deleteRenderbuffer(GLuint renderbuffer) 101 { 102 Renderbuffer *renderbufferObject = mRenderbufferNameSpace.remove(renderbuffer); 103 104 if(renderbufferObject) 105 { 106 renderbufferObject->release(); 107 } 108 } 109 110 Buffer *ResourceManager::getBuffer(unsigned int handle) 111 { 112 return mBufferNameSpace.find(handle); 113 } 114 115 Texture *ResourceManager::getTexture(unsigned int handle) 116 { 117 return mTextureNameSpace.find(handle); 118 } 119 120 Renderbuffer *ResourceManager::getRenderbuffer(unsigned int handle) 121 { 122 return mRenderbufferNameSpace.find(handle); 123 } 124 125 void ResourceManager::checkBufferAllocation(unsigned int buffer) 126 { 127 if(buffer != 0 && !getBuffer(buffer)) 128 { 129 Buffer *bufferObject = new Buffer(buffer); 130 bufferObject->addRef(); 131 132 mBufferNameSpace.insert(buffer, bufferObject); 133 } 134 } 135 136 void ResourceManager::checkTextureAllocation(GLuint texture, TextureType type) 137 { 138 if(!getTexture(texture) && texture != 0) 139 { 140 Texture *textureObject; 141 142 if(type == TEXTURE_2D) 143 { 144 textureObject = new Texture2D(texture); 145 } 146 else if(type == TEXTURE_EXTERNAL) 147 { 148 textureObject = new TextureExternal(texture); 149 } 150 else 151 { 152 UNREACHABLE(type); 153 return; 154 } 155 156 textureObject->addRef(); 157 158 mTextureNameSpace.insert(texture, textureObject); 159 } 160 } 161 162 void ResourceManager::checkRenderbufferAllocation(GLuint handle) 163 { 164 if(handle != 0 && !getRenderbuffer(handle)) 165 { 166 Renderbuffer *renderbufferObject = new Renderbuffer(handle, new Colorbuffer(0, 0, GL_RGBA4_OES, 0)); 167 renderbufferObject->addRef(); 168 169 mRenderbufferNameSpace.insert(handle, renderbufferObject); 170 } 171 } 172 173 } 174