Hash :
bdd419f9
Author :
Date :
2015-03-20T15:29:42
Fix ResourceManager create-on-bind reallocations. *re-land with build fix for Clang* We had a funny bug where the Handle Allocator would re-allocate reserved handles after the app layer creates one with Bind rather than using Gen. This affects Textures, Buffers and Renderbuffers. Fix this by using a different allocation scheme. It should still be fast on the "good" case (using Gen) and use tree lookups on the bind case. Also add some unit tests. BUG=angleproject:942 Change-Id: I63ce608fcd6a11f92e2b5421f090551934e729ed Reviewed-on: https://chromium-review.googlesource.com/261591 Tested-by: Jamie Madill <jmadill@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
//
// Copyright (c) 2002-2011 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.
//
// HandleAllocator.cpp: Implements the gl::HandleAllocator class, which is used
// to allocate GL handles.
#include "libANGLE/HandleAllocator.h"
#include <algorithm>
#include "common/debug.h"
namespace gl
{
struct HandleAllocator::HandleRangeComparator
{
bool operator()(const HandleRange &range, GLuint handle) const
{
return (handle < range.begin);
}
};
HandleAllocator::HandleAllocator() : mBaseValue(1), mNextValue(1)
{
mUnallocatedList.push_back(HandleRange(1, std::numeric_limits<GLuint>::max() - 1));
}
HandleAllocator::HandleAllocator(GLuint maximumHandleValue) : mBaseValue(1), mNextValue(1)
{
mUnallocatedList.push_back(HandleRange(1, maximumHandleValue));
}
HandleAllocator::~HandleAllocator()
{
}
void HandleAllocator::setBaseHandle(GLuint value)
{
ASSERT(mBaseValue == mNextValue);
mBaseValue = value;
mNextValue = value;
}
GLuint HandleAllocator::allocate()
{
ASSERT(!mUnallocatedList.empty() || !mReleasedList.empty());
// Allocate from released list, constant time.
if (!mReleasedList.empty())
{
GLuint reusedHandle = mReleasedList.back();
mReleasedList.pop_back();
return reusedHandle;
}
// Allocate from unallocated list, constant time.
auto listIt = mUnallocatedList.begin();
GLuint freeListHandle = listIt->begin;
ASSERT(freeListHandle > 0);
listIt->begin++;
if (listIt->begin == listIt->end)
{
mUnallocatedList.erase(listIt);
}
return freeListHandle;
}
void HandleAllocator::release(GLuint handle)
{
// Add to released list, constant time.
mReleasedList.push_back(handle);
}
void HandleAllocator::reserve(GLuint handle)
{
// Clear from released list -- might be a slow operation.
if (!mReleasedList.empty())
{
auto releasedIt = std::find(mReleasedList.begin(), mReleasedList.end(), handle);
if (releasedIt != mReleasedList.end())
{
mReleasedList.erase(releasedIt);
return;
}
}
// Not in released list, reserve in the unallocated list.
auto boundIt = std::lower_bound(mUnallocatedList.begin(), mUnallocatedList.end(), handle, HandleRangeComparator());
ASSERT(boundIt != mUnallocatedList.end());
GLuint begin = boundIt->begin;
GLuint end = boundIt->end;
if (handle == begin || handle == end)
{
if (begin + 1 == end)
{
mUnallocatedList.erase(boundIt);
}
else if (handle == begin)
{
boundIt->begin++;
}
else
{
ASSERT(handle == end);
boundIt->end--;
}
return;
}
// need to split the range
auto placementIt = mUnallocatedList.erase(boundIt);
if (begin != handle)
{
placementIt = mUnallocatedList.insert(placementIt, HandleRange(begin, handle));
}
if (handle + 1 != end)
{
mUnallocatedList.insert(placementIt, HandleRange(handle + 1, end));
}
}
}