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 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 185 | // ShaderProgramManager Implementation. |
| 186 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 187 | ShaderProgramManager::~ShaderProgramManager() |
| 188 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 189 | ASSERT(mPrograms.empty()); |
| 190 | ASSERT(mShaders.empty()); |
| 191 | } |
| 192 | |
| 193 | void ShaderProgramManager::reset(const Context *context) |
| 194 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 195 | while (!mPrograms.empty()) |
| 196 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 197 | deleteProgram(context, mPrograms.begin()->first); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 198 | } |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 199 | mPrograms.clear(); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 200 | while (!mShaders.empty()) |
| 201 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 202 | deleteShader(context, mShaders.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 | mShaders.clear(); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | GLuint ShaderProgramManager::createShader(rx::GLImplFactory *factory, |
| 208 | const gl::Limitations &rendererLimitations, |
| 209 | GLenum type) |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 210 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 211 | 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] | 212 | GLuint handle = mHandleAllocator.allocate(); |
| 213 | mShaders[handle] = new Shader(this, factory, rendererLimitations, type, handle); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 214 | return handle; |
| 215 | } |
| 216 | |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 217 | void ShaderProgramManager::deleteShader(const Context *context, GLuint shader) |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 218 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 219 | deleteObject(context, &mShaders, shader); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 222 | Shader *ShaderProgramManager::getShader(GLuint handle) const |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 223 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 224 | return GetObject(mShaders, handle); |
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 | GLuint ShaderProgramManager::createProgram(rx::GLImplFactory *factory) |
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 | GLuint handle = mHandleAllocator.allocate(); |
| 230 | mPrograms[handle] = new Program(factory, this, handle); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 231 | return handle; |
| 232 | } |
| 233 | |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 234 | void ShaderProgramManager::deleteProgram(const gl::Context *context, GLuint program) |
Jamie Madill | dc35604 | 2013-07-19 16:36:57 -0400 | [diff] [blame] | 235 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 236 | deleteObject(context, &mPrograms, program); |
Jamie Madill | dc35604 | 2013-07-19 16:36:57 -0400 | [diff] [blame] | 237 | } |
| 238 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 239 | Program *ShaderProgramManager::getProgram(GLuint handle) const |
Jamie Madill | cd055f8 | 2013-07-26 11:55:15 -0400 | [diff] [blame] | 240 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 241 | return GetObject(mPrograms, handle); |
Jamie Madill | cd055f8 | 2013-07-26 11:55:15 -0400 | [diff] [blame] | 242 | } |
| 243 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 244 | template <typename ObjectType> |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 245 | void ShaderProgramManager::deleteObject(const Context *context, |
| 246 | ResourceMap<ObjectType> *objectMap, |
| 247 | GLuint id) |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 248 | { |
| 249 | auto iter = objectMap->find(id); |
| 250 | if (iter == objectMap->end()) |
| 251 | { |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | auto object = iter->second; |
| 256 | if (object->getRefCount() == 0) |
| 257 | { |
| 258 | mHandleAllocator.release(id); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 259 | object->onDestroy(context); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 260 | objectMap->erase(iter); |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | object->flagForDeletion(); |
| 265 | } |
| 266 | } |
| 267 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 268 | // TextureManager Implementation. |
| 269 | |
| 270 | // static |
| 271 | Texture *TextureManager::AllocateNewObject(rx::GLImplFactory *factory, GLuint handle, GLenum target) |
| 272 | { |
| 273 | Texture *texture = new Texture(factory, handle, target); |
| 274 | texture->addRef(); |
| 275 | return texture; |
| 276 | } |
| 277 | |
| 278 | // static |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 279 | void TextureManager::DeleteObject(const Context *context, Texture *texture) |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 280 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 281 | texture->release(context); |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 282 | } |
| 283 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 284 | GLuint TextureManager::createTexture() |
| 285 | { |
| 286 | return AllocateEmptyObject(&mHandleAllocator, &mObjectMap); |
| 287 | } |
| 288 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 289 | Texture *TextureManager::getTexture(GLuint handle) const |
| 290 | { |
| 291 | ASSERT(GetObject(mObjectMap, 0) == nullptr); |
| 292 | return GetObject(mObjectMap, handle); |
| 293 | } |
| 294 | |
Geoff Lang | 9aded17 | 2017-04-05 11:07:56 -0400 | [diff] [blame] | 295 | void TextureManager::invalidateTextureComplenessCache() |
| 296 | { |
| 297 | for (auto &texture : mObjectMap) |
| 298 | { |
| 299 | if (texture.second) |
| 300 | { |
| 301 | texture.second->invalidateCompletenessCache(); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 306 | // RenderbufferManager Implementation. |
| 307 | |
| 308 | // static |
| 309 | Renderbuffer *RenderbufferManager::AllocateNewObject(rx::GLImplFactory *factory, GLuint handle) |
| 310 | { |
| 311 | Renderbuffer *renderbuffer = new Renderbuffer(factory->createRenderbuffer(), handle); |
| 312 | renderbuffer->addRef(); |
| 313 | return renderbuffer; |
| 314 | } |
| 315 | |
| 316 | // static |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 317 | void RenderbufferManager::DeleteObject(const Context *context, Renderbuffer *renderbuffer) |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 318 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 319 | renderbuffer->release(context); |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 322 | GLuint RenderbufferManager::createRenderbuffer() |
| 323 | { |
| 324 | return AllocateEmptyObject(&mHandleAllocator, &mObjectMap); |
| 325 | } |
| 326 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 327 | Renderbuffer *RenderbufferManager::getRenderbuffer(GLuint handle) |
| 328 | { |
| 329 | return GetObject(mObjectMap, handle); |
| 330 | } |
| 331 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 332 | // SamplerManager Implementation. |
| 333 | |
| 334 | // static |
| 335 | Sampler *SamplerManager::AllocateNewObject(rx::GLImplFactory *factory, GLuint handle) |
| 336 | { |
| 337 | Sampler *sampler = new Sampler(factory, handle); |
| 338 | sampler->addRef(); |
| 339 | return sampler; |
| 340 | } |
| 341 | |
| 342 | // static |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 343 | void SamplerManager::DeleteObject(const Context *context, Sampler *sampler) |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 344 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 345 | sampler->release(context); |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 348 | GLuint SamplerManager::createSampler() |
| 349 | { |
| 350 | return AllocateEmptyObject(&mHandleAllocator, &mObjectMap); |
| 351 | } |
| 352 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 353 | Sampler *SamplerManager::getSampler(GLuint handle) |
| 354 | { |
| 355 | return GetObject(mObjectMap, handle); |
| 356 | } |
| 357 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 358 | bool SamplerManager::isSampler(GLuint sampler) |
| 359 | { |
| 360 | return mObjectMap.find(sampler) != mObjectMap.end(); |
| 361 | } |
| 362 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 363 | // FenceSyncManager Implementation. |
| 364 | |
| 365 | // static |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 366 | void FenceSyncManager::DeleteObject(const Context *context, FenceSync *fenceSync) |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 367 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 368 | fenceSync->release(context); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 369 | } |
| 370 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 371 | GLuint FenceSyncManager::createFenceSync(rx::GLImplFactory *factory) |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 372 | { |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 373 | GLuint handle = mHandleAllocator.allocate(); |
| 374 | FenceSync *fenceSync = new FenceSync(factory->createFenceSync(), handle); |
| 375 | fenceSync->addRef(); |
| 376 | mObjectMap[handle] = fenceSync; |
| 377 | return handle; |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | FenceSync *FenceSyncManager::getFenceSync(GLuint handle) |
| 381 | { |
| 382 | return GetObject(mObjectMap, handle); |
| 383 | } |
| 384 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 385 | // PathManager Implementation. |
| 386 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 387 | ErrorOrResult<GLuint> PathManager::createPaths(rx::GLImplFactory *factory, GLsizei range) |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 388 | { |
| 389 | // Allocate client side handles. |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 390 | const GLuint client = mHandleAllocator.allocateRange(static_cast<GLuint>(range)); |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 391 | if (client == HandleRangeAllocator::kInvalidHandle) |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 392 | return OutOfMemory() << "Failed to allocate path handle range."; |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 393 | |
| 394 | const auto &paths = factory->createPaths(range); |
| 395 | if (paths.empty()) |
| 396 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 397 | mHandleAllocator.releaseRange(client, range); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 398 | return OutOfMemory() << "Failed to allocate path objects."; |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 399 | } |
| 400 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 401 | auto hint = mPaths.begin(); |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 402 | |
| 403 | for (GLsizei i = 0; i < range; ++i) |
| 404 | { |
| 405 | const auto impl = paths[static_cast<unsigned>(i)]; |
| 406 | const auto id = client + i; |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 407 | 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] | 408 | } |
| 409 | return client; |
| 410 | } |
| 411 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 412 | void PathManager::deletePaths(GLuint first, GLsizei range) |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 413 | { |
| 414 | for (GLsizei i = 0; i < range; ++i) |
| 415 | { |
| 416 | const auto id = first + i; |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 417 | const auto it = mPaths.find(id); |
| 418 | if (it == mPaths.end()) |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 419 | continue; |
| 420 | Path *p = it->second; |
| 421 | delete p; |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 422 | mPaths.erase(it); |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 423 | } |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 424 | mHandleAllocator.releaseRange(first, static_cast<GLuint>(range)); |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 425 | } |
| 426 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 427 | Path *PathManager::getPath(GLuint handle) const |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 428 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 429 | auto iter = mPaths.find(handle); |
| 430 | return iter != mPaths.end() ? iter->second : nullptr; |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 433 | bool PathManager::hasPath(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 | return mHandleAllocator.isUsed(handle); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 438 | PathManager::~PathManager() |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 439 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 440 | ASSERT(mPaths.empty()); |
| 441 | } |
| 442 | |
| 443 | void PathManager::reset(const Context *context) |
| 444 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 445 | for (auto path : mPaths) |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 446 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 447 | SafeDelete(path.second); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 448 | } |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 449 | mPaths.clear(); |
Geoff Lang | f41a715 | 2016-09-19 15:11:17 -0400 | [diff] [blame] | 450 | } |
| 451 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 452 | // FramebufferManager Implementation. |
| 453 | |
| 454 | // static |
| 455 | Framebuffer *FramebufferManager::AllocateNewObject(rx::GLImplFactory *factory, |
| 456 | GLuint handle, |
| 457 | const Caps &caps) |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 458 | { |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 459 | return new Framebuffer(caps, factory, handle); |
| 460 | } |
| 461 | |
| 462 | // static |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 463 | void FramebufferManager::DeleteObject(const Context *context, Framebuffer *framebuffer) |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 464 | { |
| 465 | // Default framebuffer are owned by their respective Surface |
| 466 | if (framebuffer->id() != 0) |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 467 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 468 | framebuffer->onDestroy(context); |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 469 | delete framebuffer; |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
| 473 | GLuint FramebufferManager::createFramebuffer() |
| 474 | { |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 475 | return AllocateEmptyObject(&mHandleAllocator, &mObjectMap); |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | Framebuffer *FramebufferManager::getFramebuffer(GLuint handle) const |
| 479 | { |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 480 | return GetObject(mObjectMap, handle); |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | void FramebufferManager::setDefaultFramebuffer(Framebuffer *framebuffer) |
| 484 | { |
| 485 | ASSERT(framebuffer == nullptr || framebuffer->id() == 0); |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 486 | mObjectMap[0] = framebuffer; |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 487 | } |
| 488 | |
Geoff Lang | 9aded17 | 2017-04-05 11:07:56 -0400 | [diff] [blame] | 489 | void FramebufferManager::invalidateFramebufferComplenessCache() |
| 490 | { |
| 491 | for (auto &framebuffer : mObjectMap) |
| 492 | { |
| 493 | if (framebuffer.second) |
| 494 | { |
| 495 | framebuffer.second->invalidateCompletenessCache(); |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
Jamie Madill | 3f01e6c | 2016-03-08 13:53:02 -0500 | [diff] [blame] | 500 | } // namespace gl |