daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 1 | // |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 2 | // Copyright (c) 2002-2016 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 7 | // ResourceManager.cpp: Implements the the ResourceManager classes, which handle allocation and |
| 8 | // lifetime of GL objects. |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 9 | |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 10 | #include "libANGLE/ResourceManager.h" |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 11 | |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 12 | #include "libANGLE/Buffer.h" |
Jamie Madill | 53ea9cc | 2016-05-17 10:12:52 -0400 | [diff] [blame] | 13 | #include "libANGLE/Fence.h" |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 14 | #include "libANGLE/Path.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 15 | #include "libANGLE/Program.h" |
| 16 | #include "libANGLE/Renderbuffer.h" |
Jamie Madill | 53ea9cc | 2016-05-17 10:12:52 -0400 | [diff] [blame] | 17 | #include "libANGLE/Sampler.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 18 | #include "libANGLE/Shader.h" |
| 19 | #include "libANGLE/Texture.h" |
Jamie Madill | 53ea9cc | 2016-05-17 10:12:52 -0400 | [diff] [blame] | 20 | #include "libANGLE/renderer/GLImplFactory.h" |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 21 | |
| 22 | namespace gl |
| 23 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 24 | |
| 25 | namespace |
| 26 | { |
| 27 | |
| 28 | template <typename ResourceType> |
| 29 | GLuint AllocateEmptyObject(HandleAllocator *handleAllocator, ResourceMap<ResourceType> *objectMap) |
| 30 | { |
| 31 | GLuint handle = handleAllocator->allocate(); |
| 32 | (*objectMap)[handle] = nullptr; |
| 33 | return handle; |
| 34 | } |
| 35 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 36 | template <typename ResourceType> |
| 37 | ResourceType *GetObject(const ResourceMap<ResourceType> &objectMap, GLuint handle) |
| 38 | { |
| 39 | auto iter = objectMap.find(handle); |
| 40 | return iter != objectMap.end() ? iter->second : nullptr; |
| 41 | } |
| 42 | |
| 43 | } // anonymous namespace |
| 44 | |
| 45 | template <typename HandleAllocatorType> |
| 46 | ResourceManagerBase<HandleAllocatorType>::ResourceManagerBase() : mRefCount(1) |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 47 | { |
| 48 | } |
| 49 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 50 | template <typename HandleAllocatorType> |
| 51 | void ResourceManagerBase<HandleAllocatorType>::addRef() |
daniel@transgaming.com | 0d25b00 | 2010-07-28 19:21:07 +0000 | [diff] [blame] | 52 | { |
| 53 | mRefCount++; |
| 54 | } |
| 55 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 56 | template <typename HandleAllocatorType> |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 57 | void ResourceManagerBase<HandleAllocatorType>::release(const Context *context) |
daniel@transgaming.com | 0d25b00 | 2010-07-28 19:21:07 +0000 | [diff] [blame] | 58 | { |
| 59 | if (--mRefCount == 0) |
| 60 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 61 | reset(context); |
daniel@transgaming.com | 0d25b00 | 2010-07-28 19:21:07 +0000 | [diff] [blame] | 62 | delete this; |
| 63 | } |
| 64 | } |
| 65 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 66 | template <typename ResourceType, typename HandleAllocatorType, typename ImplT> |
| 67 | TypedResourceManager<ResourceType, HandleAllocatorType, ImplT>::~TypedResourceManager() |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 68 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 69 | ASSERT(mObjectMap.empty()); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 72 | template <typename ResourceType, typename HandleAllocatorType, typename ImplT> |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 73 | void TypedResourceManager<ResourceType, HandleAllocatorType, ImplT>::reset(const Context *context) |
| 74 | { |
| 75 | while (!mObjectMap.empty()) |
| 76 | { |
| 77 | deleteObject(context, mObjectMap.begin()->first); |
| 78 | } |
| 79 | mObjectMap.clear(); |
| 80 | } |
| 81 | |
| 82 | template <typename ResourceType, typename HandleAllocatorType, typename ImplT> |
| 83 | void TypedResourceManager<ResourceType, HandleAllocatorType, ImplT>::deleteObject( |
| 84 | const Context *context, |
| 85 | GLuint handle) |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 86 | { |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 87 | auto objectIter = mObjectMap.find(handle); |
| 88 | if (objectIter == mObjectMap.end()) |
| 89 | { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | if (objectIter->second != nullptr) |
| 94 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 95 | objectIter->second->destroy(context); |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 96 | ImplT::DeleteObject(objectIter->second); |
| 97 | } |
| 98 | |
| 99 | // Requires an explicit this-> because of C++ template rules. |
| 100 | this->mHandleAllocator.release(objectIter->first); |
| 101 | mObjectMap.erase(objectIter); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 102 | } |
| 103 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 104 | template <typename ResourceType, typename HandleAllocatorType, typename ImplT> |
| 105 | template <typename... ArgTypes> |
| 106 | ResourceType *TypedResourceManager<ResourceType, HandleAllocatorType, ImplT>::allocateObject( |
| 107 | typename ResourceMap<ResourceType>::iterator &objectMapIter, |
| 108 | rx::GLImplFactory *factory, |
| 109 | GLuint handle, |
| 110 | ArgTypes... args) |
| 111 | { |
| 112 | ResourceType *object = ImplT::AllocateNewObject(factory, handle, args...); |
| 113 | |
| 114 | if (objectMapIter != mObjectMap.end()) |
| 115 | { |
| 116 | objectMapIter->second = object; |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | this->mHandleAllocator.reserve(handle); |
| 121 | mObjectMap[handle] = object; |
| 122 | } |
| 123 | |
| 124 | return object; |
| 125 | } |
| 126 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 127 | template class ResourceManagerBase<HandleAllocator>; |
| 128 | template class ResourceManagerBase<HandleRangeAllocator>; |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 129 | template class TypedResourceManager<Buffer, HandleAllocator, BufferManager>; |
| 130 | template Buffer *TypedResourceManager<Buffer, HandleAllocator, BufferManager>::allocateObject( |
| 131 | ResourceMap<Buffer>::iterator &, |
| 132 | rx::GLImplFactory *, |
| 133 | GLuint); |
| 134 | template class TypedResourceManager<Texture, HandleAllocator, TextureManager>; |
| 135 | template Texture *TypedResourceManager<Texture, HandleAllocator, TextureManager>::allocateObject( |
| 136 | ResourceMap<Texture>::iterator &, |
| 137 | rx::GLImplFactory *, |
| 138 | GLuint, |
| 139 | GLenum); |
| 140 | template class TypedResourceManager<Renderbuffer, HandleAllocator, RenderbufferManager>; |
| 141 | template Renderbuffer * |
| 142 | TypedResourceManager<Renderbuffer, HandleAllocator, RenderbufferManager>::allocateObject( |
| 143 | ResourceMap<Renderbuffer>::iterator &, |
| 144 | rx::GLImplFactory *, |
| 145 | GLuint); |
| 146 | template class TypedResourceManager<Sampler, HandleAllocator, SamplerManager>; |
| 147 | template Sampler *TypedResourceManager<Sampler, HandleAllocator, SamplerManager>::allocateObject( |
| 148 | ResourceMap<Sampler>::iterator &, |
| 149 | rx::GLImplFactory *, |
| 150 | GLuint); |
| 151 | template class TypedResourceManager<FenceSync, HandleAllocator, FenceSyncManager>; |
| 152 | template class TypedResourceManager<Framebuffer, HandleAllocator, FramebufferManager>; |
| 153 | template Framebuffer * |
| 154 | TypedResourceManager<Framebuffer, HandleAllocator, FramebufferManager>::allocateObject( |
| 155 | ResourceMap<Framebuffer>::iterator &, |
| 156 | rx::GLImplFactory *, |
| 157 | GLuint, |
| 158 | const Caps &); |
| 159 | |
| 160 | // BufferManager Implementation. |
| 161 | |
| 162 | // static |
| 163 | Buffer *BufferManager::AllocateNewObject(rx::GLImplFactory *factory, GLuint handle) |
| 164 | { |
| 165 | Buffer *buffer = new Buffer(factory, handle); |
| 166 | buffer->addRef(); |
| 167 | return buffer; |
| 168 | } |
| 169 | |
| 170 | // static |
| 171 | void BufferManager::DeleteObject(Buffer *buffer) |
| 172 | { |
| 173 | buffer->release(); |
| 174 | } |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 175 | |
| 176 | GLuint BufferManager::createBuffer() |
| 177 | { |
| 178 | return AllocateEmptyObject(&mHandleAllocator, &mObjectMap); |
| 179 | } |
| 180 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 181 | Buffer *BufferManager::getBuffer(GLuint handle) const |
| 182 | { |
| 183 | return GetObject(mObjectMap, handle); |
| 184 | } |
| 185 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 186 | bool BufferManager::isBufferGenerated(GLuint buffer) const |
| 187 | { |
| 188 | return buffer == 0 || mObjectMap.find(buffer) != mObjectMap.end(); |
| 189 | } |
| 190 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 191 | // ShaderProgramManager Implementation. |
| 192 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 193 | ShaderProgramManager::~ShaderProgramManager() |
| 194 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 195 | ASSERT(mPrograms.empty()); |
| 196 | ASSERT(mShaders.empty()); |
| 197 | } |
| 198 | |
| 199 | void ShaderProgramManager::reset(const Context *context) |
| 200 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 201 | while (!mPrograms.empty()) |
| 202 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 203 | deleteProgram(context, mPrograms.begin()->first); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 204 | } |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 205 | mPrograms.clear(); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 206 | while (!mShaders.empty()) |
| 207 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 208 | deleteShader(context, mShaders.begin()->first); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 209 | } |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 210 | mShaders.clear(); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | GLuint ShaderProgramManager::createShader(rx::GLImplFactory *factory, |
| 214 | const gl::Limitations &rendererLimitations, |
| 215 | GLenum type) |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 216 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 217 | ASSERT(type == GL_VERTEX_SHADER || type == GL_FRAGMENT_SHADER || type == GL_COMPUTE_SHADER); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 218 | GLuint handle = mHandleAllocator.allocate(); |
| 219 | mShaders[handle] = new Shader(this, factory, rendererLimitations, type, handle); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 220 | return handle; |
| 221 | } |
| 222 | |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 223 | void ShaderProgramManager::deleteShader(const Context *context, GLuint shader) |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 224 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 225 | deleteObject(context, &mShaders, shader); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 228 | Shader *ShaderProgramManager::getShader(GLuint handle) const |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 229 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 230 | return GetObject(mShaders, handle); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 233 | GLuint ShaderProgramManager::createProgram(rx::GLImplFactory *factory) |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 234 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 235 | GLuint handle = mHandleAllocator.allocate(); |
| 236 | mPrograms[handle] = new Program(factory, this, handle); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 237 | return handle; |
| 238 | } |
| 239 | |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 240 | void ShaderProgramManager::deleteProgram(const gl::Context *context, GLuint program) |
Jamie Madill | dc35604 | 2013-07-19 16:36:57 -0400 | [diff] [blame] | 241 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 242 | deleteObject(context, &mPrograms, program); |
Jamie Madill | dc35604 | 2013-07-19 16:36:57 -0400 | [diff] [blame] | 243 | } |
| 244 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 245 | Program *ShaderProgramManager::getProgram(GLuint handle) const |
Jamie Madill | cd055f8 | 2013-07-26 11:55:15 -0400 | [diff] [blame] | 246 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 247 | return GetObject(mPrograms, handle); |
Jamie Madill | cd055f8 | 2013-07-26 11:55:15 -0400 | [diff] [blame] | 248 | } |
| 249 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 250 | template <typename ObjectType> |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 251 | void ShaderProgramManager::deleteObject(const Context *context, |
| 252 | ResourceMap<ObjectType> *objectMap, |
| 253 | GLuint id) |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 254 | { |
| 255 | auto iter = objectMap->find(id); |
| 256 | if (iter == objectMap->end()) |
| 257 | { |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | auto object = iter->second; |
| 262 | if (object->getRefCount() == 0) |
| 263 | { |
| 264 | mHandleAllocator.release(id); |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 265 | object->destroy(context); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 266 | SafeDelete(object); |
| 267 | objectMap->erase(iter); |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | object->flagForDeletion(); |
| 272 | } |
| 273 | } |
| 274 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 275 | // TextureManager Implementation. |
| 276 | |
| 277 | // static |
| 278 | Texture *TextureManager::AllocateNewObject(rx::GLImplFactory *factory, GLuint handle, GLenum target) |
| 279 | { |
| 280 | Texture *texture = new Texture(factory, handle, target); |
| 281 | texture->addRef(); |
| 282 | return texture; |
| 283 | } |
| 284 | |
| 285 | // static |
| 286 | void TextureManager::DeleteObject(Texture *texture) |
| 287 | { |
| 288 | texture->release(); |
| 289 | } |
| 290 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 291 | GLuint TextureManager::createTexture() |
| 292 | { |
| 293 | return AllocateEmptyObject(&mHandleAllocator, &mObjectMap); |
| 294 | } |
| 295 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 296 | Texture *TextureManager::getTexture(GLuint handle) const |
| 297 | { |
| 298 | ASSERT(GetObject(mObjectMap, 0) == nullptr); |
| 299 | return GetObject(mObjectMap, handle); |
| 300 | } |
| 301 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 302 | bool TextureManager::isTextureGenerated(GLuint texture) const |
| 303 | { |
| 304 | return texture == 0 || mObjectMap.find(texture) != mObjectMap.end(); |
| 305 | } |
| 306 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 307 | // RenderbufferManager Implementation. |
| 308 | |
| 309 | // static |
| 310 | Renderbuffer *RenderbufferManager::AllocateNewObject(rx::GLImplFactory *factory, GLuint handle) |
| 311 | { |
| 312 | Renderbuffer *renderbuffer = new Renderbuffer(factory->createRenderbuffer(), handle); |
| 313 | renderbuffer->addRef(); |
| 314 | return renderbuffer; |
| 315 | } |
| 316 | |
| 317 | // static |
| 318 | void RenderbufferManager::DeleteObject(Renderbuffer *renderbuffer) |
| 319 | { |
| 320 | renderbuffer->release(); |
| 321 | } |
| 322 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 323 | GLuint RenderbufferManager::createRenderbuffer() |
| 324 | { |
| 325 | return AllocateEmptyObject(&mHandleAllocator, &mObjectMap); |
| 326 | } |
| 327 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 328 | Renderbuffer *RenderbufferManager::getRenderbuffer(GLuint handle) |
| 329 | { |
| 330 | return GetObject(mObjectMap, handle); |
| 331 | } |
| 332 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 333 | bool RenderbufferManager::isRenderbufferGenerated(GLuint renderbuffer) const |
| 334 | { |
| 335 | return renderbuffer == 0 || mObjectMap.find(renderbuffer) != mObjectMap.end(); |
| 336 | } |
| 337 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 338 | // SamplerManager Implementation. |
| 339 | |
| 340 | // static |
| 341 | Sampler *SamplerManager::AllocateNewObject(rx::GLImplFactory *factory, GLuint handle) |
| 342 | { |
| 343 | Sampler *sampler = new Sampler(factory, handle); |
| 344 | sampler->addRef(); |
| 345 | return sampler; |
| 346 | } |
| 347 | |
| 348 | // static |
| 349 | void SamplerManager::DeleteObject(Sampler *sampler) |
| 350 | { |
| 351 | sampler->release(); |
| 352 | } |
| 353 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 354 | GLuint SamplerManager::createSampler() |
| 355 | { |
| 356 | return AllocateEmptyObject(&mHandleAllocator, &mObjectMap); |
| 357 | } |
| 358 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 359 | Sampler *SamplerManager::getSampler(GLuint handle) |
| 360 | { |
| 361 | return GetObject(mObjectMap, handle); |
| 362 | } |
| 363 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 364 | bool SamplerManager::isSampler(GLuint sampler) |
| 365 | { |
| 366 | return mObjectMap.find(sampler) != mObjectMap.end(); |
| 367 | } |
| 368 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 369 | // FenceSyncManager Implementation. |
| 370 | |
| 371 | // static |
| 372 | void FenceSyncManager::DeleteObject(FenceSync *fenceSync) |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 373 | { |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 374 | fenceSync->release(); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 375 | } |
| 376 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 377 | GLuint FenceSyncManager::createFenceSync(rx::GLImplFactory *factory) |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 378 | { |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 379 | GLuint handle = mHandleAllocator.allocate(); |
| 380 | FenceSync *fenceSync = new FenceSync(factory->createFenceSync(), handle); |
| 381 | fenceSync->addRef(); |
| 382 | mObjectMap[handle] = fenceSync; |
| 383 | return handle; |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | FenceSync *FenceSyncManager::getFenceSync(GLuint handle) |
| 387 | { |
| 388 | return GetObject(mObjectMap, handle); |
| 389 | } |
| 390 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 391 | // PathManager Implementation. |
| 392 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 393 | ErrorOrResult<GLuint> PathManager::createPaths(rx::GLImplFactory *factory, GLsizei range) |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 394 | { |
| 395 | // Allocate client side handles. |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 396 | const GLuint client = mHandleAllocator.allocateRange(static_cast<GLuint>(range)); |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 397 | if (client == HandleRangeAllocator::kInvalidHandle) |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 398 | return Error(GL_OUT_OF_MEMORY, "Failed to allocate path handle range."); |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 399 | |
| 400 | const auto &paths = factory->createPaths(range); |
| 401 | if (paths.empty()) |
| 402 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 403 | mHandleAllocator.releaseRange(client, range); |
| 404 | return Error(GL_OUT_OF_MEMORY, "Failed to allocate path objects."); |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 405 | } |
| 406 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 407 | auto hint = mPaths.begin(); |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 408 | |
| 409 | for (GLsizei i = 0; i < range; ++i) |
| 410 | { |
| 411 | const auto impl = paths[static_cast<unsigned>(i)]; |
| 412 | const auto id = client + i; |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 413 | hint = mPaths.insert(hint, std::make_pair(id, new Path(impl))); |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 414 | } |
| 415 | return client; |
| 416 | } |
| 417 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 418 | void PathManager::deletePaths(GLuint first, GLsizei range) |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 419 | { |
| 420 | for (GLsizei i = 0; i < range; ++i) |
| 421 | { |
| 422 | const auto id = first + i; |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 423 | const auto it = mPaths.find(id); |
| 424 | if (it == mPaths.end()) |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 425 | continue; |
| 426 | Path *p = it->second; |
| 427 | delete p; |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 428 | mPaths.erase(it); |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 429 | } |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 430 | mHandleAllocator.releaseRange(first, static_cast<GLuint>(range)); |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 431 | } |
| 432 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 433 | Path *PathManager::getPath(GLuint handle) const |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 434 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 435 | auto iter = mPaths.find(handle); |
| 436 | return iter != mPaths.end() ? iter->second : nullptr; |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 439 | bool PathManager::hasPath(GLuint handle) const |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 440 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 441 | return mHandleAllocator.isUsed(handle); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 444 | PathManager::~PathManager() |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 445 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 446 | ASSERT(mPaths.empty()); |
| 447 | } |
| 448 | |
| 449 | void PathManager::reset(const Context *context) |
| 450 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 451 | for (auto path : mPaths) |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 452 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 453 | SafeDelete(path.second); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 454 | } |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 455 | mPaths.clear(); |
Geoff Lang | f41a715 | 2016-09-19 15:11:17 -0400 | [diff] [blame] | 456 | } |
| 457 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 458 | // FramebufferManager Implementation. |
| 459 | |
| 460 | // static |
| 461 | Framebuffer *FramebufferManager::AllocateNewObject(rx::GLImplFactory *factory, |
| 462 | GLuint handle, |
| 463 | const Caps &caps) |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 464 | { |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 465 | return new Framebuffer(caps, factory, handle); |
| 466 | } |
| 467 | |
| 468 | // static |
| 469 | void FramebufferManager::DeleteObject(Framebuffer *framebuffer) |
| 470 | { |
| 471 | // Default framebuffer are owned by their respective Surface |
| 472 | if (framebuffer->id() != 0) |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 473 | { |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 474 | delete framebuffer; |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
| 478 | GLuint FramebufferManager::createFramebuffer() |
| 479 | { |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 480 | return AllocateEmptyObject(&mHandleAllocator, &mObjectMap); |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | Framebuffer *FramebufferManager::getFramebuffer(GLuint handle) const |
| 484 | { |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 485 | return GetObject(mObjectMap, handle); |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | void FramebufferManager::setDefaultFramebuffer(Framebuffer *framebuffer) |
| 489 | { |
| 490 | ASSERT(framebuffer == nullptr || framebuffer->id() == 0); |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 491 | mObjectMap[0] = framebuffer; |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | bool FramebufferManager::isFramebufferGenerated(GLuint framebuffer) |
| 495 | { |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 496 | ASSERT(mObjectMap.find(0) != mObjectMap.end()); |
| 497 | return mObjectMap.find(framebuffer) != mObjectMap.end(); |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 498 | } |
| 499 | |
Jamie Madill | 3f01e6c | 2016-03-08 13:53:02 -0500 | [diff] [blame] | 500 | } // namespace gl |