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 | 526392d | 2018-11-16 09:35:14 -0500 | [diff] [blame] | 13 | #include "libANGLE/Context.h" |
Jamie Madill | 53ea9cc | 2016-05-17 10:12:52 -0400 | [diff] [blame] | 14 | #include "libANGLE/Fence.h" |
Michael Spang | fb201c5 | 2019-04-03 14:57:35 -0400 | [diff] [blame] | 15 | #include "libANGLE/MemoryObject.h" |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 16 | #include "libANGLE/Path.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 17 | #include "libANGLE/Program.h" |
Yunchao He | a336b90 | 2017-08-02 16:05:21 +0800 | [diff] [blame] | 18 | #include "libANGLE/ProgramPipeline.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 19 | #include "libANGLE/Renderbuffer.h" |
Jamie Madill | 53ea9cc | 2016-05-17 10:12:52 -0400 | [diff] [blame] | 20 | #include "libANGLE/Sampler.h" |
Michael Spang | 5093ba6 | 2019-05-14 17:36:36 -0400 | [diff] [blame] | 21 | #include "libANGLE/Semaphore.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 22 | #include "libANGLE/Shader.h" |
| 23 | #include "libANGLE/Texture.h" |
Jamie Madill | 526392d | 2018-11-16 09:35:14 -0500 | [diff] [blame] | 24 | #include "libANGLE/renderer/ContextImpl.h" |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 25 | |
| 26 | namespace gl |
| 27 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 28 | |
| 29 | namespace |
| 30 | { |
| 31 | |
| 32 | template <typename ResourceType> |
| 33 | GLuint AllocateEmptyObject(HandleAllocator *handleAllocator, ResourceMap<ResourceType> *objectMap) |
| 34 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 35 | GLuint handle = handleAllocator->allocate(); |
| 36 | objectMap->assign(handle, nullptr); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 37 | return handle; |
| 38 | } |
| 39 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 40 | } // anonymous namespace |
| 41 | |
| 42 | template <typename HandleAllocatorType> |
| 43 | ResourceManagerBase<HandleAllocatorType>::ResourceManagerBase() : mRefCount(1) |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 44 | {} |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 45 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 46 | template <typename HandleAllocatorType> |
| 47 | void ResourceManagerBase<HandleAllocatorType>::addRef() |
daniel@transgaming.com | 0d25b00 | 2010-07-28 19:21:07 +0000 | [diff] [blame] | 48 | { |
| 49 | mRefCount++; |
| 50 | } |
| 51 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 52 | template <typename HandleAllocatorType> |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 53 | void ResourceManagerBase<HandleAllocatorType>::release(const Context *context) |
daniel@transgaming.com | 0d25b00 | 2010-07-28 19:21:07 +0000 | [diff] [blame] | 54 | { |
| 55 | if (--mRefCount == 0) |
| 56 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 57 | reset(context); |
daniel@transgaming.com | 0d25b00 | 2010-07-28 19:21:07 +0000 | [diff] [blame] | 58 | delete this; |
| 59 | } |
| 60 | } |
| 61 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 62 | template <typename ResourceType, typename HandleAllocatorType, typename ImplT> |
| 63 | TypedResourceManager<ResourceType, HandleAllocatorType, ImplT>::~TypedResourceManager() |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 64 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 65 | ASSERT(mObjectMap.empty()); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 68 | template <typename ResourceType, typename HandleAllocatorType, typename ImplT> |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 69 | void TypedResourceManager<ResourceType, HandleAllocatorType, ImplT>::reset(const Context *context) |
| 70 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 71 | this->mHandleAllocator.reset(); |
| 72 | for (const auto &resource : mObjectMap) |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 73 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 74 | if (resource.second) |
| 75 | { |
| 76 | ImplT::DeleteObject(context, resource.second); |
| 77 | } |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 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 | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 87 | ResourceType *resource = nullptr; |
| 88 | if (!mObjectMap.erase(handle, &resource)) |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 89 | { |
| 90 | return; |
| 91 | } |
| 92 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 93 | // Requires an explicit this-> because of C++ template rules. |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 94 | this->mHandleAllocator.release(handle); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 95 | |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 96 | if (resource) |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 97 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 98 | ImplT::DeleteObject(context, resource); |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 99 | } |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 102 | template class ResourceManagerBase<HandleAllocator>; |
| 103 | template class ResourceManagerBase<HandleRangeAllocator>; |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 104 | template class TypedResourceManager<Buffer, HandleAllocator, BufferManager>; |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 105 | template class TypedResourceManager<Texture, HandleAllocator, TextureManager>; |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 106 | template class TypedResourceManager<Renderbuffer, HandleAllocator, RenderbufferManager>; |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 107 | template class TypedResourceManager<Sampler, HandleAllocator, SamplerManager>; |
Jamie Madill | 70b5bb0 | 2017-08-28 13:32:37 -0400 | [diff] [blame] | 108 | template class TypedResourceManager<Sync, HandleAllocator, SyncManager>; |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 109 | template class TypedResourceManager<Framebuffer, HandleAllocator, FramebufferManager>; |
Yunchao He | a336b90 | 2017-08-02 16:05:21 +0800 | [diff] [blame] | 110 | template class TypedResourceManager<ProgramPipeline, HandleAllocator, ProgramPipelineManager>; |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 111 | |
| 112 | // BufferManager Implementation. |
| 113 | |
| 114 | // static |
| 115 | Buffer *BufferManager::AllocateNewObject(rx::GLImplFactory *factory, GLuint handle) |
| 116 | { |
| 117 | Buffer *buffer = new Buffer(factory, handle); |
| 118 | buffer->addRef(); |
| 119 | return buffer; |
| 120 | } |
| 121 | |
| 122 | // static |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 123 | void BufferManager::DeleteObject(const Context *context, Buffer *buffer) |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 124 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 125 | buffer->release(context); |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 126 | } |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 127 | |
| 128 | GLuint BufferManager::createBuffer() |
| 129 | { |
| 130 | return AllocateEmptyObject(&mHandleAllocator, &mObjectMap); |
| 131 | } |
| 132 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 133 | Buffer *BufferManager::getBuffer(GLuint handle) const |
| 134 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 135 | return mObjectMap.query(handle); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 136 | } |
| 137 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 138 | // ShaderProgramManager Implementation. |
| 139 | |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 140 | ShaderProgramManager::ShaderProgramManager() {} |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 141 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 142 | ShaderProgramManager::~ShaderProgramManager() |
| 143 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 144 | ASSERT(mPrograms.empty()); |
| 145 | ASSERT(mShaders.empty()); |
| 146 | } |
| 147 | |
| 148 | void ShaderProgramManager::reset(const Context *context) |
| 149 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 150 | while (!mPrograms.empty()) |
| 151 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 152 | deleteProgram(context, mPrograms.begin()->first); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 153 | } |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 154 | mPrograms.clear(); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 155 | while (!mShaders.empty()) |
| 156 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 157 | deleteShader(context, mShaders.begin()->first); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 158 | } |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 159 | mShaders.clear(); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | GLuint ShaderProgramManager::createShader(rx::GLImplFactory *factory, |
| 163 | const gl::Limitations &rendererLimitations, |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 164 | ShaderType type) |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 165 | { |
Jiawei Shao | 385b3e0 | 2018-03-21 09:43:28 +0800 | [diff] [blame] | 166 | ASSERT(type != ShaderType::InvalidEnum); |
Jamie Madill | e4634a1 | 2018-11-14 09:54:35 -0500 | [diff] [blame] | 167 | GLuint handle = mHandleAllocator.allocate(); |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 168 | mShaders.assign(handle, new Shader(this, factory, rendererLimitations, type, handle)); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 169 | return handle; |
| 170 | } |
| 171 | |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 172 | void ShaderProgramManager::deleteShader(const Context *context, GLuint shader) |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 173 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 174 | deleteObject(context, &mShaders, shader); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 177 | Shader *ShaderProgramManager::getShader(GLuint handle) const |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 178 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 179 | return mShaders.query(handle); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 182 | GLuint ShaderProgramManager::createProgram(rx::GLImplFactory *factory) |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 183 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 184 | GLuint handle = mHandleAllocator.allocate(); |
| 185 | mPrograms.assign(handle, new Program(factory, this, handle)); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 186 | return handle; |
| 187 | } |
| 188 | |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 189 | void ShaderProgramManager::deleteProgram(const gl::Context *context, GLuint program) |
Jamie Madill | dc35604 | 2013-07-19 16:36:57 -0400 | [diff] [blame] | 190 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 191 | deleteObject(context, &mPrograms, program); |
Jamie Madill | dc35604 | 2013-07-19 16:36:57 -0400 | [diff] [blame] | 192 | } |
| 193 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 194 | template <typename ObjectType> |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 195 | void ShaderProgramManager::deleteObject(const Context *context, |
| 196 | ResourceMap<ObjectType> *objectMap, |
| 197 | GLuint id) |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 198 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 199 | ObjectType *object = objectMap->query(id); |
| 200 | if (!object) |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 201 | { |
| 202 | return; |
| 203 | } |
| 204 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 205 | if (object->getRefCount() == 0) |
| 206 | { |
| 207 | mHandleAllocator.release(id); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 208 | object->onDestroy(context); |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 209 | objectMap->erase(id, &object); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 210 | } |
| 211 | else |
| 212 | { |
| 213 | object->flagForDeletion(); |
| 214 | } |
| 215 | } |
| 216 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 217 | // TextureManager Implementation. |
| 218 | |
| 219 | // static |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 220 | Texture *TextureManager::AllocateNewObject(rx::GLImplFactory *factory, |
| 221 | GLuint handle, |
| 222 | TextureType type) |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 223 | { |
Corentin Wallez | 99d492c | 2018-02-27 15:17:10 -0500 | [diff] [blame] | 224 | Texture *texture = new Texture(factory, handle, type); |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 225 | texture->addRef(); |
| 226 | return texture; |
| 227 | } |
| 228 | |
| 229 | // static |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 230 | void TextureManager::DeleteObject(const Context *context, Texture *texture) |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 231 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 232 | texture->release(context); |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 235 | GLuint TextureManager::createTexture() |
| 236 | { |
| 237 | return AllocateEmptyObject(&mHandleAllocator, &mObjectMap); |
| 238 | } |
| 239 | |
Jamie Madill | 124f78c | 2019-06-18 11:48:24 -0400 | [diff] [blame] | 240 | void TextureManager::signalAllTexturesDirty() const |
Geoff Lang | 9aded17 | 2017-04-05 11:07:56 -0400 | [diff] [blame] | 241 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 242 | for (const auto &texture : mObjectMap) |
Geoff Lang | 9aded17 | 2017-04-05 11:07:56 -0400 | [diff] [blame] | 243 | { |
| 244 | if (texture.second) |
| 245 | { |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 246 | // We don't know if the Texture needs init, but that's ok, since it will only force |
| 247 | // a re-check, and will not initialize the pixels if it's not needed. |
Jamie Madill | 124f78c | 2019-06-18 11:48:24 -0400 | [diff] [blame] | 248 | texture.second->signalDirtyStorage(InitState::MayNeedInit); |
Geoff Lang | 9aded17 | 2017-04-05 11:07:56 -0400 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
Jamie Madill | fc3463d | 2018-01-03 13:46:21 -0500 | [diff] [blame] | 253 | void TextureManager::enableHandleAllocatorLogging() |
| 254 | { |
| 255 | mHandleAllocator.enableLogging(true); |
| 256 | } |
| 257 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 258 | // RenderbufferManager Implementation. |
| 259 | |
| 260 | // static |
| 261 | Renderbuffer *RenderbufferManager::AllocateNewObject(rx::GLImplFactory *factory, GLuint handle) |
| 262 | { |
Jamie Madill | e703c60 | 2018-02-20 10:21:48 -0500 | [diff] [blame] | 263 | Renderbuffer *renderbuffer = new Renderbuffer(factory, handle); |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 264 | renderbuffer->addRef(); |
| 265 | return renderbuffer; |
| 266 | } |
| 267 | |
| 268 | // static |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 269 | void RenderbufferManager::DeleteObject(const Context *context, Renderbuffer *renderbuffer) |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 270 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 271 | renderbuffer->release(context); |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 274 | GLuint RenderbufferManager::createRenderbuffer() |
| 275 | { |
| 276 | return AllocateEmptyObject(&mHandleAllocator, &mObjectMap); |
| 277 | } |
| 278 | |
Yunchao He | a438f4e | 2017-08-04 13:42:39 +0800 | [diff] [blame] | 279 | Renderbuffer *RenderbufferManager::getRenderbuffer(GLuint handle) const |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 280 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 281 | return mObjectMap.query(handle); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 282 | } |
| 283 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 284 | // SamplerManager Implementation. |
| 285 | |
| 286 | // static |
| 287 | Sampler *SamplerManager::AllocateNewObject(rx::GLImplFactory *factory, GLuint handle) |
| 288 | { |
| 289 | Sampler *sampler = new Sampler(factory, handle); |
| 290 | sampler->addRef(); |
| 291 | return sampler; |
| 292 | } |
| 293 | |
| 294 | // static |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 295 | void SamplerManager::DeleteObject(const Context *context, Sampler *sampler) |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 296 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 297 | sampler->release(context); |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 300 | GLuint SamplerManager::createSampler() |
| 301 | { |
| 302 | return AllocateEmptyObject(&mHandleAllocator, &mObjectMap); |
| 303 | } |
| 304 | |
Yunchao He | a438f4e | 2017-08-04 13:42:39 +0800 | [diff] [blame] | 305 | Sampler *SamplerManager::getSampler(GLuint handle) const |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 306 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 307 | return mObjectMap.query(handle); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 308 | } |
| 309 | |
Yunchao He | a438f4e | 2017-08-04 13:42:39 +0800 | [diff] [blame] | 310 | bool SamplerManager::isSampler(GLuint sampler) const |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 311 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 312 | return mObjectMap.contains(sampler); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 313 | } |
| 314 | |
Jamie Madill | 70b5bb0 | 2017-08-28 13:32:37 -0400 | [diff] [blame] | 315 | // SyncManager Implementation. |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 316 | |
| 317 | // static |
Jamie Madill | 70b5bb0 | 2017-08-28 13:32:37 -0400 | [diff] [blame] | 318 | void SyncManager::DeleteObject(const Context *context, Sync *sync) |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 319 | { |
Jamie Madill | 70b5bb0 | 2017-08-28 13:32:37 -0400 | [diff] [blame] | 320 | sync->release(context); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 321 | } |
| 322 | |
Jamie Madill | 70b5bb0 | 2017-08-28 13:32:37 -0400 | [diff] [blame] | 323 | GLuint SyncManager::createSync(rx::GLImplFactory *factory) |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 324 | { |
Jamie Madill | 70b5bb0 | 2017-08-28 13:32:37 -0400 | [diff] [blame] | 325 | GLuint handle = mHandleAllocator.allocate(); |
| 326 | Sync *sync = new Sync(factory->createSync(), handle); |
| 327 | sync->addRef(); |
| 328 | mObjectMap.assign(handle, sync); |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 329 | return handle; |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 330 | } |
| 331 | |
Jamie Madill | 70b5bb0 | 2017-08-28 13:32:37 -0400 | [diff] [blame] | 332 | Sync *SyncManager::getSync(GLuint handle) const |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 333 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 334 | return mObjectMap.query(handle); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 335 | } |
| 336 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 337 | // PathManager Implementation. |
| 338 | |
Jamie Madill | 526392d | 2018-11-16 09:35:14 -0500 | [diff] [blame] | 339 | PathManager::PathManager() = default; |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 340 | |
Jamie Madill | 526392d | 2018-11-16 09:35:14 -0500 | [diff] [blame] | 341 | angle::Result PathManager::createPaths(Context *context, GLsizei range, GLuint *createdOut) |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 342 | { |
Jamie Madill | 1395134 | 2018-09-30 15:24:28 -0400 | [diff] [blame] | 343 | *createdOut = 0; |
| 344 | |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 345 | // Allocate client side handles. |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 346 | const GLuint client = mHandleAllocator.allocateRange(static_cast<GLuint>(range)); |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 347 | if (client == HandleRangeAllocator::kInvalidHandle) |
Jamie Madill | 526392d | 2018-11-16 09:35:14 -0500 | [diff] [blame] | 348 | { |
| 349 | context->handleError(GL_OUT_OF_MEMORY, "Failed to allocate path handle range.", __FILE__, |
| 350 | ANGLE_FUNCTION, __LINE__); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 351 | return angle::Result::Stop; |
Jamie Madill | 526392d | 2018-11-16 09:35:14 -0500 | [diff] [blame] | 352 | } |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 353 | |
Jamie Madill | 526392d | 2018-11-16 09:35:14 -0500 | [diff] [blame] | 354 | const auto &paths = context->getImplementation()->createPaths(range); |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 355 | if (paths.empty()) |
| 356 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 357 | mHandleAllocator.releaseRange(client, range); |
Jamie Madill | 526392d | 2018-11-16 09:35:14 -0500 | [diff] [blame] | 358 | context->handleError(GL_OUT_OF_MEMORY, "Failed to allocate path objects.", __FILE__, |
| 359 | ANGLE_FUNCTION, __LINE__); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 360 | return angle::Result::Stop; |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 361 | } |
| 362 | |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 363 | for (GLsizei i = 0; i < range; ++i) |
| 364 | { |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 365 | rx::PathImpl *impl = paths[static_cast<unsigned>(i)]; |
Jamie Madill | e4634a1 | 2018-11-14 09:54:35 -0500 | [diff] [blame] | 366 | const auto id = client + i; |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 367 | mPaths.assign(id, new Path(impl)); |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 368 | } |
Jamie Madill | 1395134 | 2018-09-30 15:24:28 -0400 | [diff] [blame] | 369 | *createdOut = client; |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 370 | return angle::Result::Continue; |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 371 | } |
| 372 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 373 | void PathManager::deletePaths(GLuint first, GLsizei range) |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 374 | { |
| 375 | for (GLsizei i = 0; i < range; ++i) |
| 376 | { |
| 377 | const auto id = first + i; |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 378 | Path *p = nullptr; |
| 379 | if (!mPaths.erase(id, &p)) |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 380 | continue; |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 381 | delete p; |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 382 | } |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 383 | mHandleAllocator.releaseRange(first, static_cast<GLuint>(range)); |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 384 | } |
| 385 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 386 | Path *PathManager::getPath(GLuint handle) const |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 387 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 388 | return mPaths.query(handle); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 391 | bool PathManager::hasPath(GLuint handle) const |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 392 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 393 | return mHandleAllocator.isUsed(handle); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 396 | PathManager::~PathManager() |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 397 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 398 | ASSERT(mPaths.empty()); |
| 399 | } |
| 400 | |
| 401 | void PathManager::reset(const Context *context) |
| 402 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 403 | for (auto path : mPaths) |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 404 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 405 | SafeDelete(path.second); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 406 | } |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 407 | mPaths.clear(); |
Geoff Lang | f41a715 | 2016-09-19 15:11:17 -0400 | [diff] [blame] | 408 | } |
| 409 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 410 | // FramebufferManager Implementation. |
| 411 | |
| 412 | // static |
| 413 | Framebuffer *FramebufferManager::AllocateNewObject(rx::GLImplFactory *factory, |
| 414 | GLuint handle, |
| 415 | const Caps &caps) |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 416 | { |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 417 | return new Framebuffer(caps, factory, handle); |
| 418 | } |
| 419 | |
| 420 | // static |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 421 | void FramebufferManager::DeleteObject(const Context *context, Framebuffer *framebuffer) |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 422 | { |
Geoff Lang | bf7b95d | 2018-05-01 16:48:21 -0400 | [diff] [blame] | 423 | framebuffer->onDestroy(context); |
| 424 | delete framebuffer; |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | GLuint FramebufferManager::createFramebuffer() |
| 428 | { |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 429 | return AllocateEmptyObject(&mHandleAllocator, &mObjectMap); |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | Framebuffer *FramebufferManager::getFramebuffer(GLuint handle) const |
| 433 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 434 | return mObjectMap.query(handle); |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | void FramebufferManager::setDefaultFramebuffer(Framebuffer *framebuffer) |
| 438 | { |
| 439 | ASSERT(framebuffer == nullptr || framebuffer->id() == 0); |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 440 | mObjectMap.assign(0, framebuffer); |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 441 | } |
| 442 | |
Jamie Madill | 124f78c | 2019-06-18 11:48:24 -0400 | [diff] [blame] | 443 | void FramebufferManager::invalidateFramebufferComplenessCache() const |
Geoff Lang | 9aded17 | 2017-04-05 11:07:56 -0400 | [diff] [blame] | 444 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 445 | for (const auto &framebuffer : mObjectMap) |
Geoff Lang | 9aded17 | 2017-04-05 11:07:56 -0400 | [diff] [blame] | 446 | { |
| 447 | if (framebuffer.second) |
| 448 | { |
Jamie Madill | 124f78c | 2019-06-18 11:48:24 -0400 | [diff] [blame] | 449 | framebuffer.second->invalidateCompletenessCache(); |
Geoff Lang | 9aded17 | 2017-04-05 11:07:56 -0400 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
Yunchao He | a336b90 | 2017-08-02 16:05:21 +0800 | [diff] [blame] | 454 | // ProgramPipelineManager Implementation. |
| 455 | |
| 456 | // static |
| 457 | ProgramPipeline *ProgramPipelineManager::AllocateNewObject(rx::GLImplFactory *factory, |
| 458 | GLuint handle) |
| 459 | { |
| 460 | ProgramPipeline *pipeline = new ProgramPipeline(factory, handle); |
| 461 | pipeline->addRef(); |
| 462 | return pipeline; |
| 463 | } |
| 464 | |
| 465 | // static |
| 466 | void ProgramPipelineManager::DeleteObject(const Context *context, ProgramPipeline *pipeline) |
| 467 | { |
| 468 | pipeline->release(context); |
| 469 | } |
| 470 | |
| 471 | GLuint ProgramPipelineManager::createProgramPipeline() |
| 472 | { |
| 473 | return AllocateEmptyObject(&mHandleAllocator, &mObjectMap); |
| 474 | } |
| 475 | |
| 476 | ProgramPipeline *ProgramPipelineManager::getProgramPipeline(GLuint handle) const |
| 477 | { |
| 478 | return mObjectMap.query(handle); |
| 479 | } |
| 480 | |
Michael Spang | fb201c5 | 2019-04-03 14:57:35 -0400 | [diff] [blame] | 481 | // MemoryObjectManager Implementation. |
| 482 | |
| 483 | MemoryObjectManager::MemoryObjectManager() {} |
| 484 | |
| 485 | MemoryObjectManager::~MemoryObjectManager() |
| 486 | { |
| 487 | ASSERT(mMemoryObjects.empty()); |
| 488 | } |
| 489 | |
| 490 | void MemoryObjectManager::reset(const Context *context) |
| 491 | { |
| 492 | while (!mMemoryObjects.empty()) |
| 493 | { |
| 494 | deleteMemoryObject(context, mMemoryObjects.begin()->first); |
| 495 | } |
| 496 | mMemoryObjects.clear(); |
| 497 | } |
| 498 | |
| 499 | GLuint MemoryObjectManager::createMemoryObject(rx::GLImplFactory *factory) |
| 500 | { |
Jamie Madill | 124f78c | 2019-06-18 11:48:24 -0400 | [diff] [blame] | 501 | GLuint handle = mHandleAllocator.allocate(); |
Michael Spang | a0b00e9 | 2019-04-09 18:45:22 -0400 | [diff] [blame] | 502 | MemoryObject *memoryObject = new MemoryObject(factory, handle); |
| 503 | memoryObject->addRef(); |
| 504 | mMemoryObjects.assign(handle, memoryObject); |
Michael Spang | fb201c5 | 2019-04-03 14:57:35 -0400 | [diff] [blame] | 505 | return handle; |
| 506 | } |
| 507 | |
| 508 | void MemoryObjectManager::deleteMemoryObject(const Context *context, GLuint handle) |
| 509 | { |
| 510 | MemoryObject *memoryObject = nullptr; |
| 511 | if (!mMemoryObjects.erase(handle, &memoryObject)) |
| 512 | { |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | // Requires an explicit this-> because of C++ template rules. |
| 517 | this->mHandleAllocator.release(handle); |
| 518 | |
| 519 | if (memoryObject) |
| 520 | { |
| 521 | memoryObject->release(context); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | MemoryObject *MemoryObjectManager::getMemoryObject(GLuint handle) const |
| 526 | { |
| 527 | return mMemoryObjects.query(handle); |
| 528 | } |
| 529 | |
Michael Spang | 5093ba6 | 2019-05-14 17:36:36 -0400 | [diff] [blame] | 530 | // SemaphoreManager Implementation. |
| 531 | |
| 532 | SemaphoreManager::SemaphoreManager() {} |
| 533 | |
| 534 | SemaphoreManager::~SemaphoreManager() |
| 535 | { |
| 536 | ASSERT(mSemaphores.empty()); |
| 537 | } |
| 538 | |
| 539 | void SemaphoreManager::reset(const Context *context) |
| 540 | { |
| 541 | while (!mSemaphores.empty()) |
| 542 | { |
| 543 | deleteSemaphore(context, mSemaphores.begin()->first); |
| 544 | } |
| 545 | mSemaphores.clear(); |
| 546 | } |
| 547 | |
| 548 | GLuint SemaphoreManager::createSemaphore(rx::GLImplFactory *factory) |
| 549 | { |
| 550 | GLuint handle = mHandleAllocator.allocate(); |
| 551 | Semaphore *semaphore = new Semaphore(factory, handle); |
| 552 | semaphore->addRef(); |
| 553 | mSemaphores.assign(handle, semaphore); |
| 554 | return handle; |
| 555 | } |
| 556 | |
| 557 | void SemaphoreManager::deleteSemaphore(const Context *context, GLuint handle) |
| 558 | { |
| 559 | Semaphore *semaphore = nullptr; |
| 560 | if (!mSemaphores.erase(handle, &semaphore)) |
| 561 | { |
| 562 | return; |
| 563 | } |
| 564 | |
| 565 | // Requires an explicit this-> because of C++ template rules. |
| 566 | this->mHandleAllocator.release(handle); |
| 567 | |
| 568 | if (semaphore) |
| 569 | { |
| 570 | semaphore->release(context); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | Semaphore *SemaphoreManager::getSemaphore(GLuint handle) const |
| 575 | { |
| 576 | return mSemaphores.query(handle); |
| 577 | } |
| 578 | |
Jamie Madill | 3f01e6c | 2016-03-08 13:53:02 -0500 | [diff] [blame] | 579 | } // namespace gl |