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