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" |
Yunchao He | a336b90 | 2017-08-02 16:05:21 +0800 | [diff] [blame] | 16 | #include "libANGLE/ProgramPipeline.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 17 | #include "libANGLE/Renderbuffer.h" |
Jamie Madill | 53ea9cc | 2016-05-17 10:12:52 -0400 | [diff] [blame] | 18 | #include "libANGLE/Sampler.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 19 | #include "libANGLE/Shader.h" |
| 20 | #include "libANGLE/Texture.h" |
Jamie Madill | 53ea9cc | 2016-05-17 10:12:52 -0400 | [diff] [blame] | 21 | #include "libANGLE/renderer/GLImplFactory.h" |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 22 | |
| 23 | namespace gl |
| 24 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 25 | |
| 26 | namespace |
| 27 | { |
| 28 | |
| 29 | template <typename ResourceType> |
| 30 | GLuint AllocateEmptyObject(HandleAllocator *handleAllocator, ResourceMap<ResourceType> *objectMap) |
| 31 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 32 | GLuint handle = handleAllocator->allocate(); |
| 33 | objectMap->assign(handle, nullptr); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 34 | return handle; |
| 35 | } |
| 36 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 37 | } // anonymous namespace |
| 38 | |
| 39 | template <typename HandleAllocatorType> |
| 40 | ResourceManagerBase<HandleAllocatorType>::ResourceManagerBase() : mRefCount(1) |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 41 | { |
| 42 | } |
| 43 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 44 | template <typename HandleAllocatorType> |
| 45 | void ResourceManagerBase<HandleAllocatorType>::addRef() |
daniel@transgaming.com | 0d25b00 | 2010-07-28 19:21:07 +0000 | [diff] [blame] | 46 | { |
| 47 | mRefCount++; |
| 48 | } |
| 49 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 50 | template <typename HandleAllocatorType> |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 51 | void ResourceManagerBase<HandleAllocatorType>::release(const Context *context) |
daniel@transgaming.com | 0d25b00 | 2010-07-28 19:21:07 +0000 | [diff] [blame] | 52 | { |
| 53 | if (--mRefCount == 0) |
| 54 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 55 | reset(context); |
daniel@transgaming.com | 0d25b00 | 2010-07-28 19:21:07 +0000 | [diff] [blame] | 56 | delete this; |
| 57 | } |
| 58 | } |
| 59 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 60 | template <typename ResourceType, typename HandleAllocatorType, typename ImplT> |
| 61 | TypedResourceManager<ResourceType, HandleAllocatorType, ImplT>::~TypedResourceManager() |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 62 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 63 | ASSERT(mObjectMap.empty()); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 66 | template <typename ResourceType, typename HandleAllocatorType, typename ImplT> |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 67 | void TypedResourceManager<ResourceType, HandleAllocatorType, ImplT>::reset(const Context *context) |
| 68 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 69 | this->mHandleAllocator.reset(); |
| 70 | for (const auto &resource : mObjectMap) |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 71 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 72 | if (resource.second) |
| 73 | { |
| 74 | ImplT::DeleteObject(context, resource.second); |
| 75 | } |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 76 | } |
| 77 | mObjectMap.clear(); |
| 78 | } |
| 79 | |
| 80 | template <typename ResourceType, typename HandleAllocatorType, typename ImplT> |
| 81 | void TypedResourceManager<ResourceType, HandleAllocatorType, ImplT>::deleteObject( |
| 82 | const Context *context, |
| 83 | GLuint handle) |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 84 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 85 | ResourceType *resource = nullptr; |
| 86 | if (!mObjectMap.erase(handle, &resource)) |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 87 | { |
| 88 | return; |
| 89 | } |
| 90 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 91 | // Requires an explicit this-> because of C++ template rules. |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 92 | this->mHandleAllocator.release(handle); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 93 | |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 94 | if (resource) |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 95 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 96 | ImplT::DeleteObject(context, resource); |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 97 | } |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 100 | template class ResourceManagerBase<HandleAllocator>; |
| 101 | template class ResourceManagerBase<HandleRangeAllocator>; |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 102 | template class TypedResourceManager<Buffer, HandleAllocator, BufferManager>; |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 103 | template class TypedResourceManager<Texture, HandleAllocator, TextureManager>; |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 104 | template class TypedResourceManager<Renderbuffer, HandleAllocator, RenderbufferManager>; |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 105 | template class TypedResourceManager<Sampler, HandleAllocator, SamplerManager>; |
Jamie Madill | 70b5bb0 | 2017-08-28 13:32:37 -0400 | [diff] [blame] | 106 | template class TypedResourceManager<Sync, HandleAllocator, SyncManager>; |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 107 | template class TypedResourceManager<Framebuffer, HandleAllocator, FramebufferManager>; |
Yunchao He | a336b90 | 2017-08-02 16:05:21 +0800 | [diff] [blame] | 108 | template class TypedResourceManager<ProgramPipeline, HandleAllocator, ProgramPipelineManager>; |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 109 | |
| 110 | // BufferManager Implementation. |
| 111 | |
| 112 | // static |
| 113 | Buffer *BufferManager::AllocateNewObject(rx::GLImplFactory *factory, GLuint handle) |
| 114 | { |
| 115 | Buffer *buffer = new Buffer(factory, handle); |
| 116 | buffer->addRef(); |
| 117 | return buffer; |
| 118 | } |
| 119 | |
| 120 | // static |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 121 | void BufferManager::DeleteObject(const Context *context, Buffer *buffer) |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 122 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 123 | buffer->release(context); |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 124 | } |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 125 | |
| 126 | GLuint BufferManager::createBuffer() |
| 127 | { |
| 128 | return AllocateEmptyObject(&mHandleAllocator, &mObjectMap); |
| 129 | } |
| 130 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 131 | Buffer *BufferManager::getBuffer(GLuint handle) const |
| 132 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 133 | return mObjectMap.query(handle); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 134 | } |
| 135 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 136 | // ShaderProgramManager Implementation. |
| 137 | |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 138 | ShaderProgramManager::ShaderProgramManager() |
| 139 | { |
| 140 | } |
| 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, |
| 164 | GLenum type) |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 165 | { |
Jiawei Shao | 89be29a | 2017-11-06 14:36:45 +0800 | [diff] [blame] | 166 | ASSERT(type == GL_VERTEX_SHADER || type == GL_FRAGMENT_SHADER || type == GL_COMPUTE_SHADER || |
| 167 | type == GL_GEOMETRY_SHADER_EXT); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 168 | GLuint handle = mHandleAllocator.allocate(); |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 169 | mShaders.assign(handle, new Shader(this, factory, rendererLimitations, type, handle)); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 170 | return handle; |
| 171 | } |
| 172 | |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 173 | void ShaderProgramManager::deleteShader(const Context *context, GLuint shader) |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 174 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 175 | deleteObject(context, &mShaders, shader); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 178 | Shader *ShaderProgramManager::getShader(GLuint handle) const |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 179 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 180 | return mShaders.query(handle); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 183 | GLuint ShaderProgramManager::createProgram(rx::GLImplFactory *factory) |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 184 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 185 | GLuint handle = mHandleAllocator.allocate(); |
| 186 | mPrograms.assign(handle, new Program(factory, this, handle)); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 187 | return handle; |
| 188 | } |
| 189 | |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 190 | void ShaderProgramManager::deleteProgram(const gl::Context *context, GLuint program) |
Jamie Madill | dc35604 | 2013-07-19 16:36:57 -0400 | [diff] [blame] | 191 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 192 | deleteObject(context, &mPrograms, program); |
Jamie Madill | dc35604 | 2013-07-19 16:36:57 -0400 | [diff] [blame] | 193 | } |
| 194 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 195 | Program *ShaderProgramManager::getProgram(GLuint handle) const |
Jamie Madill | cd055f8 | 2013-07-26 11:55:15 -0400 | [diff] [blame] | 196 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 197 | return mPrograms.query(handle); |
Jamie Madill | cd055f8 | 2013-07-26 11:55:15 -0400 | [diff] [blame] | 198 | } |
| 199 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 200 | template <typename ObjectType> |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 201 | void ShaderProgramManager::deleteObject(const Context *context, |
| 202 | ResourceMap<ObjectType> *objectMap, |
| 203 | GLuint id) |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 204 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 205 | ObjectType *object = objectMap->query(id); |
| 206 | if (!object) |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 207 | { |
| 208 | return; |
| 209 | } |
| 210 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 211 | if (object->getRefCount() == 0) |
| 212 | { |
| 213 | mHandleAllocator.release(id); |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 214 | object->onDestroy(context); |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 215 | objectMap->erase(id, &object); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 216 | } |
| 217 | else |
| 218 | { |
| 219 | object->flagForDeletion(); |
| 220 | } |
| 221 | } |
| 222 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 223 | // TextureManager Implementation. |
| 224 | |
| 225 | // static |
| 226 | Texture *TextureManager::AllocateNewObject(rx::GLImplFactory *factory, GLuint handle, GLenum target) |
| 227 | { |
| 228 | Texture *texture = new Texture(factory, handle, target); |
| 229 | texture->addRef(); |
| 230 | return texture; |
| 231 | } |
| 232 | |
| 233 | // static |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 234 | void TextureManager::DeleteObject(const Context *context, Texture *texture) |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 235 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 236 | texture->release(context); |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 239 | GLuint TextureManager::createTexture() |
| 240 | { |
| 241 | return AllocateEmptyObject(&mHandleAllocator, &mObjectMap); |
| 242 | } |
| 243 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 244 | Texture *TextureManager::getTexture(GLuint handle) const |
| 245 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 246 | ASSERT(mObjectMap.query(0) == nullptr); |
| 247 | return mObjectMap.query(handle); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 248 | } |
| 249 | |
Jamie Madill | 81c2e25 | 2017-09-09 23:32:46 -0400 | [diff] [blame] | 250 | void TextureManager::signalAllTexturesDirty() const |
Geoff Lang | 9aded17 | 2017-04-05 11:07:56 -0400 | [diff] [blame] | 251 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 252 | for (const auto &texture : mObjectMap) |
Geoff Lang | 9aded17 | 2017-04-05 11:07:56 -0400 | [diff] [blame] | 253 | { |
| 254 | if (texture.second) |
| 255 | { |
Jamie Madill | 05b35b2 | 2017-10-03 09:01:44 -0400 | [diff] [blame] | 256 | // We don't know if the Texture needs init, but that's ok, since it will only force |
| 257 | // a re-check, and will not initialize the pixels if it's not needed. |
| 258 | texture.second->signalDirty(InitState::MayNeedInit); |
Geoff Lang | 9aded17 | 2017-04-05 11:07:56 -0400 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
Jamie Madill | fc3463d | 2018-01-03 13:46:21 -0500 | [diff] [blame^] | 263 | void TextureManager::enableHandleAllocatorLogging() |
| 264 | { |
| 265 | mHandleAllocator.enableLogging(true); |
| 266 | } |
| 267 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 268 | // RenderbufferManager Implementation. |
| 269 | |
| 270 | // static |
| 271 | Renderbuffer *RenderbufferManager::AllocateNewObject(rx::GLImplFactory *factory, GLuint handle) |
| 272 | { |
| 273 | Renderbuffer *renderbuffer = new Renderbuffer(factory->createRenderbuffer(), handle); |
| 274 | renderbuffer->addRef(); |
| 275 | return renderbuffer; |
| 276 | } |
| 277 | |
| 278 | // static |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 279 | void RenderbufferManager::DeleteObject(const Context *context, Renderbuffer *renderbuffer) |
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 | renderbuffer->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 RenderbufferManager::createRenderbuffer() |
| 285 | { |
| 286 | return AllocateEmptyObject(&mHandleAllocator, &mObjectMap); |
| 287 | } |
| 288 | |
Yunchao He | a438f4e | 2017-08-04 13:42:39 +0800 | [diff] [blame] | 289 | Renderbuffer *RenderbufferManager::getRenderbuffer(GLuint handle) const |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 290 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 291 | return mObjectMap.query(handle); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 292 | } |
| 293 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 294 | // SamplerManager Implementation. |
| 295 | |
| 296 | // static |
| 297 | Sampler *SamplerManager::AllocateNewObject(rx::GLImplFactory *factory, GLuint handle) |
| 298 | { |
| 299 | Sampler *sampler = new Sampler(factory, handle); |
| 300 | sampler->addRef(); |
| 301 | return sampler; |
| 302 | } |
| 303 | |
| 304 | // static |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 305 | void SamplerManager::DeleteObject(const Context *context, Sampler *sampler) |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 306 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 307 | sampler->release(context); |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 310 | GLuint SamplerManager::createSampler() |
| 311 | { |
| 312 | return AllocateEmptyObject(&mHandleAllocator, &mObjectMap); |
| 313 | } |
| 314 | |
Yunchao He | a438f4e | 2017-08-04 13:42:39 +0800 | [diff] [blame] | 315 | Sampler *SamplerManager::getSampler(GLuint handle) const |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 316 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 317 | return mObjectMap.query(handle); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 318 | } |
| 319 | |
Yunchao He | a438f4e | 2017-08-04 13:42:39 +0800 | [diff] [blame] | 320 | bool SamplerManager::isSampler(GLuint sampler) const |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 321 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 322 | return mObjectMap.contains(sampler); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 323 | } |
| 324 | |
Jamie Madill | 70b5bb0 | 2017-08-28 13:32:37 -0400 | [diff] [blame] | 325 | // SyncManager Implementation. |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 326 | |
| 327 | // static |
Jamie Madill | 70b5bb0 | 2017-08-28 13:32:37 -0400 | [diff] [blame] | 328 | void SyncManager::DeleteObject(const Context *context, Sync *sync) |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 329 | { |
Jamie Madill | 70b5bb0 | 2017-08-28 13:32:37 -0400 | [diff] [blame] | 330 | sync->release(context); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 331 | } |
| 332 | |
Jamie Madill | 70b5bb0 | 2017-08-28 13:32:37 -0400 | [diff] [blame] | 333 | GLuint SyncManager::createSync(rx::GLImplFactory *factory) |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 334 | { |
Jamie Madill | 70b5bb0 | 2017-08-28 13:32:37 -0400 | [diff] [blame] | 335 | GLuint handle = mHandleAllocator.allocate(); |
| 336 | Sync *sync = new Sync(factory->createSync(), handle); |
| 337 | sync->addRef(); |
| 338 | mObjectMap.assign(handle, sync); |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 339 | return handle; |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 340 | } |
| 341 | |
Jamie Madill | 70b5bb0 | 2017-08-28 13:32:37 -0400 | [diff] [blame] | 342 | Sync *SyncManager::getSync(GLuint handle) const |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 343 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 344 | return mObjectMap.query(handle); |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 345 | } |
| 346 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 347 | // PathManager Implementation. |
| 348 | |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 349 | PathManager::PathManager() |
| 350 | { |
| 351 | } |
| 352 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 353 | ErrorOrResult<GLuint> PathManager::createPaths(rx::GLImplFactory *factory, GLsizei range) |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 354 | { |
| 355 | // Allocate client side handles. |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 356 | const GLuint client = mHandleAllocator.allocateRange(static_cast<GLuint>(range)); |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 357 | if (client == HandleRangeAllocator::kInvalidHandle) |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 358 | return OutOfMemory() << "Failed to allocate path handle range."; |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 359 | |
| 360 | const auto &paths = factory->createPaths(range); |
| 361 | if (paths.empty()) |
| 362 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 363 | mHandleAllocator.releaseRange(client, range); |
Yuly Novikov | c4d18aa | 2017-03-09 18:45:02 -0500 | [diff] [blame] | 364 | return OutOfMemory() << "Failed to allocate path objects."; |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 365 | } |
| 366 | |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 367 | for (GLsizei i = 0; i < range; ++i) |
| 368 | { |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 369 | rx::PathImpl *impl = paths[static_cast<unsigned>(i)]; |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 370 | const auto id = client + i; |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 371 | mPaths.assign(id, new Path(impl)); |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 372 | } |
| 373 | return client; |
| 374 | } |
| 375 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 376 | void PathManager::deletePaths(GLuint first, GLsizei range) |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 377 | { |
| 378 | for (GLsizei i = 0; i < range; ++i) |
| 379 | { |
| 380 | const auto id = first + i; |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 381 | Path *p = nullptr; |
| 382 | if (!mPaths.erase(id, &p)) |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 383 | continue; |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 384 | delete p; |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 385 | } |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 386 | mHandleAllocator.releaseRange(first, static_cast<GLuint>(range)); |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 387 | } |
| 388 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 389 | Path *PathManager::getPath(GLuint handle) const |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 390 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 391 | return mPaths.query(handle); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 394 | bool PathManager::hasPath(GLuint handle) const |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 395 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 396 | return mHandleAllocator.isUsed(handle); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 399 | PathManager::~PathManager() |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 400 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 401 | ASSERT(mPaths.empty()); |
| 402 | } |
| 403 | |
| 404 | void PathManager::reset(const Context *context) |
| 405 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 406 | for (auto path : mPaths) |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 407 | { |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 408 | SafeDelete(path.second); |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 409 | } |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 410 | mPaths.clear(); |
Geoff Lang | f41a715 | 2016-09-19 15:11:17 -0400 | [diff] [blame] | 411 | } |
| 412 | |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 413 | // FramebufferManager Implementation. |
| 414 | |
| 415 | // static |
| 416 | Framebuffer *FramebufferManager::AllocateNewObject(rx::GLImplFactory *factory, |
| 417 | GLuint handle, |
| 418 | const Caps &caps) |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 419 | { |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 420 | return new Framebuffer(caps, factory, handle); |
| 421 | } |
| 422 | |
| 423 | // static |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 424 | void FramebufferManager::DeleteObject(const Context *context, Framebuffer *framebuffer) |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 425 | { |
| 426 | // Default framebuffer are owned by their respective Surface |
| 427 | if (framebuffer->id() != 0) |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 428 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 429 | framebuffer->onDestroy(context); |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 430 | delete framebuffer; |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
| 434 | GLuint FramebufferManager::createFramebuffer() |
| 435 | { |
Jamie Madill | 5f45e7c | 2017-02-10 15:23:28 -0800 | [diff] [blame] | 436 | return AllocateEmptyObject(&mHandleAllocator, &mObjectMap); |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | Framebuffer *FramebufferManager::getFramebuffer(GLuint handle) const |
| 440 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 441 | return mObjectMap.query(handle); |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | void FramebufferManager::setDefaultFramebuffer(Framebuffer *framebuffer) |
| 445 | { |
| 446 | ASSERT(framebuffer == nullptr || framebuffer->id() == 0); |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 447 | mObjectMap.assign(0, framebuffer); |
Geoff Lang | 3bf8e3a | 2016-12-01 17:28:52 -0500 | [diff] [blame] | 448 | } |
| 449 | |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 450 | void FramebufferManager::invalidateFramebufferComplenessCache() const |
Geoff Lang | 9aded17 | 2017-04-05 11:07:56 -0400 | [diff] [blame] | 451 | { |
Jamie Madill | 96a483b | 2017-06-27 16:49:21 -0400 | [diff] [blame] | 452 | for (const auto &framebuffer : mObjectMap) |
Geoff Lang | 9aded17 | 2017-04-05 11:07:56 -0400 | [diff] [blame] | 453 | { |
| 454 | if (framebuffer.second) |
| 455 | { |
| 456 | framebuffer.second->invalidateCompletenessCache(); |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
Yunchao He | a336b90 | 2017-08-02 16:05:21 +0800 | [diff] [blame] | 461 | // ProgramPipelineManager Implementation. |
| 462 | |
| 463 | // static |
| 464 | ProgramPipeline *ProgramPipelineManager::AllocateNewObject(rx::GLImplFactory *factory, |
| 465 | GLuint handle) |
| 466 | { |
| 467 | ProgramPipeline *pipeline = new ProgramPipeline(factory, handle); |
| 468 | pipeline->addRef(); |
| 469 | return pipeline; |
| 470 | } |
| 471 | |
| 472 | // static |
| 473 | void ProgramPipelineManager::DeleteObject(const Context *context, ProgramPipeline *pipeline) |
| 474 | { |
| 475 | pipeline->release(context); |
| 476 | } |
| 477 | |
| 478 | GLuint ProgramPipelineManager::createProgramPipeline() |
| 479 | { |
| 480 | return AllocateEmptyObject(&mHandleAllocator, &mObjectMap); |
| 481 | } |
| 482 | |
| 483 | ProgramPipeline *ProgramPipelineManager::getProgramPipeline(GLuint handle) const |
| 484 | { |
| 485 | return mObjectMap.query(handle); |
| 486 | } |
| 487 | |
Jamie Madill | 3f01e6c | 2016-03-08 13:53:02 -0500 | [diff] [blame] | 488 | } // namespace gl |