Hash :
b8c64f3e
Author :
Date :
2024-05-21T15:34:26
Fix build on compilers that get confused with templates - part 2 Bug: angleproject:8667 Change-Id: I1db327643f0b16bea5dc59635ac51c2398fdedf6 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5554174 Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Auto-Submit: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Geoff Lang <geofflang@chromium.org> Reviewed-by: Geoff Lang <geofflang@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
//
// Copyright 2002 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// ResourceManager.cpp: Implements the the ResourceManager classes, which handle allocation and
// lifetime of GL objects.
#include "libANGLE/ResourceManager.h"
#include "libANGLE/Buffer.h"
#include "libANGLE/Context.h"
#include "libANGLE/Fence.h"
#include "libANGLE/MemoryObject.h"
#include "libANGLE/Program.h"
#include "libANGLE/ProgramPipeline.h"
#include "libANGLE/Query.h"
#include "libANGLE/Renderbuffer.h"
#include "libANGLE/Sampler.h"
#include "libANGLE/Semaphore.h"
#include "libANGLE/Shader.h"
#include "libANGLE/Texture.h"
#include "libANGLE/renderer/ContextImpl.h"
namespace gl
{
namespace
{
template <typename ResourceType, typename IDType>
IDType AllocateEmptyObject(HandleAllocator *handleAllocator,
ResourceMap<ResourceType, IDType> *objectMap)
{
IDType handle = PackParam<IDType>(handleAllocator->allocate());
objectMap->assign(handle, nullptr);
return handle;
}
} // anonymous namespace
ResourceManagerBase::ResourceManagerBase() : mRefCount(1) {}
ResourceManagerBase::~ResourceManagerBase() = default;
void ResourceManagerBase::addRef()
{
mRefCount++;
}
void ResourceManagerBase::release(const Context *context)
{
if (--mRefCount == 0)
{
reset(context);
delete this;
}
}
template <typename ResourceType, typename ImplT, typename IDType>
TypedResourceManager<ResourceType, ImplT, IDType>::~TypedResourceManager()
{
using UnsafeResourceMapIterTyped = UnsafeResourceMapIter<ResourceType, IDType>;
ASSERT(UnsafeResourceMapIterTyped(mObjectMap).empty());
}
template <typename ResourceType, typename ImplT, typename IDType>
void TypedResourceManager<ResourceType, ImplT, IDType>::reset(const Context *context)
{
// Note: this function is called when the last context in the share group is destroyed. Thus
// there are no thread safety concerns.
this->mHandleAllocator.reset();
for (const auto &resource : UnsafeResourceMapIter(mObjectMap))
{
if (resource.second)
{
ImplT::DeleteObject(context, resource.second);
}
}
mObjectMap.clear();
}
template <typename ResourceType, typename ImplT, typename IDType>
void TypedResourceManager<ResourceType, ImplT, IDType>::deleteObject(const Context *context,
IDType handle)
{
ResourceType *resource = nullptr;
if (!mObjectMap.erase(handle, &resource))
{
return;
}
// Requires an explicit this-> because of C++ template rules.
this->mHandleAllocator.release(GetIDValue(handle));
if (resource)
{
ImplT::DeleteObject(context, resource);
}
}
template class TypedResourceManager<Buffer, BufferManager, BufferID>;
template class TypedResourceManager<Texture, TextureManager, TextureID>;
template class TypedResourceManager<Renderbuffer, RenderbufferManager, RenderbufferID>;
template class TypedResourceManager<Sampler, SamplerManager, SamplerID>;
template class TypedResourceManager<Sync, SyncManager, SyncID>;
template class TypedResourceManager<Framebuffer, FramebufferManager, FramebufferID>;
template class TypedResourceManager<ProgramPipeline, ProgramPipelineManager, ProgramPipelineID>;
// BufferManager Implementation.
BufferManager::~BufferManager() = default;
// static
Buffer *BufferManager::AllocateNewObject(rx::GLImplFactory *factory, BufferID handle)
{
Buffer *buffer = new Buffer(factory, handle);
buffer->addRef();
return buffer;
}
// static
void BufferManager::DeleteObject(const Context *context, Buffer *buffer)
{
buffer->release(context);
}
BufferID BufferManager::createBuffer()
{
return AllocateEmptyObject(&mHandleAllocator, &mObjectMap);
}
Buffer *BufferManager::getBuffer(BufferID handle) const
{
return mObjectMap.query(handle);
}
// ShaderProgramManager Implementation.
ShaderProgramManager::ShaderProgramManager() {}
ShaderProgramManager::~ShaderProgramManager()
{
ASSERT(UnsafeResourceMapIter(mPrograms).empty());
ASSERT(UnsafeResourceMapIter(mShaders).empty());
}
void ShaderProgramManager::reset(const Context *context)
{
// Note: this function is called when the last context in the share group is destroyed. Thus
// there are no thread safety concerns.
mHandleAllocator.reset();
for (const auto &program : UnsafeResourceMapIter(mPrograms))
{
if (program.second)
{
program.second->onDestroy(context);
}
}
for (const auto &shader : UnsafeResourceMapIter(mShaders))
{
if (shader.second)
{
shader.second->onDestroy(context);
}
}
mPrograms.clear();
mShaders.clear();
}
ShaderProgramID ShaderProgramManager::createShader(rx::GLImplFactory *factory,
const gl::Limitations &rendererLimitations,
ShaderType type)
{
ASSERT(type != ShaderType::InvalidEnum);
ShaderProgramID handle = ShaderProgramID{mHandleAllocator.allocate()};
mShaders.assign(handle, new Shader(this, factory, rendererLimitations, type, handle));
return handle;
}
void ShaderProgramManager::deleteShader(const Context *context, ShaderProgramID shader)
{
deleteObject(context, &mShaders, shader);
}
Shader *ShaderProgramManager::getShader(ShaderProgramID handle) const
{
return mShaders.query(handle);
}
ShaderProgramID ShaderProgramManager::createProgram(rx::GLImplFactory *factory)
{
ShaderProgramID handle = ShaderProgramID{mHandleAllocator.allocate()};
mPrograms.assign(handle, new Program(factory, this, handle));
return handle;
}
void ShaderProgramManager::deleteProgram(const gl::Context *context, ShaderProgramID program)
{
deleteObject(context, &mPrograms, program);
}
template <typename ObjectType, typename IDType>
void ShaderProgramManager::deleteObject(const Context *context,
ResourceMap<ObjectType, IDType> *objectMap,
IDType id)
{
ObjectType *object = objectMap->query(id);
if (!object)
{
return;
}
if (object->getRefCount() == 0)
{
mHandleAllocator.release(id.value);
object->onDestroy(context);
objectMap->erase(id, &object);
}
else
{
object->flagForDeletion();
}
}
// TextureManager Implementation.
TextureManager::~TextureManager() = default;
// static
Texture *TextureManager::AllocateNewObject(rx::GLImplFactory *factory,
TextureID handle,
TextureType type)
{
Texture *texture = new Texture(factory, handle, type);
texture->addRef();
return texture;
}
// static
void TextureManager::DeleteObject(const Context *context, Texture *texture)
{
texture->release(context);
}
TextureID TextureManager::createTexture()
{
return AllocateEmptyObject(&mHandleAllocator, &mObjectMap);
}
void TextureManager::signalAllTexturesDirty() const
{
// Note: this function is called with glRequestExtensionANGLE and glDisableExtensionANGLE. The
// GL_ANGLE_request_extension explicitly requires the application to ensure thread safety.
for (const auto &texture : UnsafeResourceMapIter(mObjectMap))
{
if (texture.second)
{
// We don't know if the Texture needs init, but that's ok, since it will only force
// a re-check, and will not initialize the pixels if it's not needed.
texture.second->signalDirtyStorage(InitState::MayNeedInit);
}
}
}
void TextureManager::enableHandleAllocatorLogging()
{
mHandleAllocator.enableLogging(true);
}
// RenderbufferManager Implementation.
RenderbufferManager::~RenderbufferManager() = default;
// static
Renderbuffer *RenderbufferManager::AllocateNewObject(rx::GLImplFactory *factory,
RenderbufferID handle)
{
Renderbuffer *renderbuffer = new Renderbuffer(factory, handle);
renderbuffer->addRef();
return renderbuffer;
}
// static
void RenderbufferManager::DeleteObject(const Context *context, Renderbuffer *renderbuffer)
{
renderbuffer->release(context);
}
RenderbufferID RenderbufferManager::createRenderbuffer()
{
return {AllocateEmptyObject(&mHandleAllocator, &mObjectMap)};
}
Renderbuffer *RenderbufferManager::getRenderbuffer(RenderbufferID handle) const
{
return mObjectMap.query(handle);
}
// SamplerManager Implementation.
SamplerManager::~SamplerManager() = default;
// static
Sampler *SamplerManager::AllocateNewObject(rx::GLImplFactory *factory, SamplerID handle)
{
Sampler *sampler = new Sampler(factory, handle);
sampler->addRef();
return sampler;
}
// static
void SamplerManager::DeleteObject(const Context *context, Sampler *sampler)
{
sampler->release(context);
}
SamplerID SamplerManager::createSampler()
{
return AllocateEmptyObject(&mHandleAllocator, &mObjectMap);
}
Sampler *SamplerManager::getSampler(SamplerID handle) const
{
return mObjectMap.query(handle);
}
bool SamplerManager::isSampler(SamplerID sampler) const
{
return mObjectMap.contains(sampler);
}
// SyncManager Implementation.
SyncManager::~SyncManager() = default;
// static
void SyncManager::DeleteObject(const Context *context, Sync *sync)
{
sync->release(context);
}
SyncID SyncManager::createSync(rx::GLImplFactory *factory)
{
SyncID handle = {mHandleAllocator.allocate()};
Sync *sync = new Sync(factory, handle);
sync->addRef();
mObjectMap.assign(handle, sync);
return handle;
}
Sync *SyncManager::getSync(SyncID handle) const
{
return mObjectMap.query(handle);
}
// FramebufferManager Implementation.
FramebufferManager::~FramebufferManager() = default;
// static
Framebuffer *FramebufferManager::AllocateNewObject(rx::GLImplFactory *factory,
FramebufferID handle,
const Context *context)
{
// Make sure the caller isn't using a reserved handle.
ASSERT(handle != Framebuffer::kDefaultDrawFramebufferHandle);
return new Framebuffer(context, factory, handle);
}
// static
void FramebufferManager::DeleteObject(const Context *context, Framebuffer *framebuffer)
{
framebuffer->onDestroy(context);
delete framebuffer;
}
FramebufferID FramebufferManager::createFramebuffer()
{
return AllocateEmptyObject(&mHandleAllocator, &mObjectMap);
}
Framebuffer *FramebufferManager::getFramebuffer(FramebufferID handle) const
{
return mObjectMap.query(handle);
}
void FramebufferManager::setDefaultFramebuffer(Framebuffer *framebuffer)
{
ASSERT(framebuffer == nullptr || framebuffer->isDefault());
mObjectMap.assign(Framebuffer::kDefaultDrawFramebufferHandle, framebuffer);
}
Framebuffer *FramebufferManager::getDefaultFramebuffer() const
{
return getFramebuffer(Framebuffer::kDefaultDrawFramebufferHandle);
}
void FramebufferManager::invalidateFramebufferCompletenessCache() const
{
// Note: framebuffer objects are private to context and so the map doesn't need locking
for (const auto &framebuffer : UnsafeResourceMapIter(mObjectMap))
{
if (framebuffer.second)
{
framebuffer.second->invalidateCompletenessCache();
}
}
}
// ProgramPipelineManager Implementation.
ProgramPipelineManager::~ProgramPipelineManager() = default;
// static
ProgramPipeline *ProgramPipelineManager::AllocateNewObject(rx::GLImplFactory *factory,
ProgramPipelineID handle)
{
ProgramPipeline *pipeline = new ProgramPipeline(factory, handle);
pipeline->addRef();
return pipeline;
}
// static
void ProgramPipelineManager::DeleteObject(const Context *context, ProgramPipeline *pipeline)
{
pipeline->release(context);
}
ProgramPipelineID ProgramPipelineManager::createProgramPipeline()
{
return AllocateEmptyObject(&mHandleAllocator, &mObjectMap);
}
ProgramPipeline *ProgramPipelineManager::getProgramPipeline(ProgramPipelineID handle) const
{
return mObjectMap.query(handle);
}
// MemoryObjectManager Implementation.
MemoryObjectManager::MemoryObjectManager() {}
MemoryObjectManager::~MemoryObjectManager()
{
ASSERT(UnsafeResourceMapIter(mMemoryObjects).empty());
}
void MemoryObjectManager::reset(const Context *context)
{
// Note: this function is called when the last context in the share group is destroyed. Thus
// there are no thread safety concerns.
mHandleAllocator.reset();
for (const auto &memoryObject : UnsafeResourceMapIter(mMemoryObjects))
{
if (memoryObject.second)
{
memoryObject.second->release(context);
}
}
mMemoryObjects.clear();
}
MemoryObjectID MemoryObjectManager::createMemoryObject(rx::GLImplFactory *factory)
{
MemoryObjectID handle = MemoryObjectID{mHandleAllocator.allocate()};
MemoryObject *memoryObject = new MemoryObject(factory, handle);
memoryObject->addRef();
mMemoryObjects.assign(handle, memoryObject);
return handle;
}
void MemoryObjectManager::deleteMemoryObject(const Context *context, MemoryObjectID handle)
{
MemoryObject *memoryObject = nullptr;
if (!mMemoryObjects.erase(handle, &memoryObject))
{
return;
}
// Requires an explicit this-> because of C++ template rules.
this->mHandleAllocator.release(handle.value);
if (memoryObject)
{
memoryObject->release(context);
}
}
MemoryObject *MemoryObjectManager::getMemoryObject(MemoryObjectID handle) const
{
return mMemoryObjects.query(handle);
}
// SemaphoreManager Implementation.
SemaphoreManager::SemaphoreManager() {}
SemaphoreManager::~SemaphoreManager()
{
ASSERT(UnsafeResourceMapIter(mSemaphores).empty());
}
void SemaphoreManager::reset(const Context *context)
{
// Note: this function is called when the last context in the share group is destroyed. Thus
// there are no thread safety concerns.
mHandleAllocator.reset();
for (const auto &semaphore : UnsafeResourceMapIter(mSemaphores))
{
if (semaphore.second)
{
semaphore.second->release(context);
}
}
mSemaphores.clear();
}
SemaphoreID SemaphoreManager::createSemaphore(rx::GLImplFactory *factory)
{
SemaphoreID handle = SemaphoreID{mHandleAllocator.allocate()};
Semaphore *semaphore = new Semaphore(factory, handle);
semaphore->addRef();
mSemaphores.assign(handle, semaphore);
return handle;
}
void SemaphoreManager::deleteSemaphore(const Context *context, SemaphoreID handle)
{
Semaphore *semaphore = nullptr;
if (!mSemaphores.erase(handle, &semaphore))
{
return;
}
// Requires an explicit this-> because of C++ template rules.
this->mHandleAllocator.release(handle.value);
if (semaphore)
{
semaphore->release(context);
}
}
Semaphore *SemaphoreManager::getSemaphore(SemaphoreID handle) const
{
return mSemaphores.query(handle);
}
} // namespace gl