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