gfx/angle/checkout/src/libANGLE/IndexRangeCache.cpp
author Jeff Gilbert <jgilbert@mozilla.com>
Fri, 15 Mar 2019 22:55:50 -0700
changeset 470378 167ee7c46b84bc9f0988896d74adc810ec2e495a
parent 406429 8ee92682ad1f3b67c110742bc910cf1af03ac48b
permissions -rw-r--r--
Bug 1520948 - Update ANGLE to chromium/3729..moz/firefox-68. Differential Revision: https://phabricator.services.mozilla.com/D23772

//
// Copyright (c) 2013 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.
//

// IndexRangeCache.cpp: Defines the gl::IndexRangeCache class which stores information about
// ranges of indices.

#include "libANGLE/IndexRangeCache.h"

#include "common/debug.h"
#include "libANGLE/formatutils.h"

namespace gl
{

IndexRangeCache::IndexRangeCache() {}

IndexRangeCache::~IndexRangeCache() {}

void IndexRangeCache::addRange(DrawElementsType type,
                               size_t offset,
                               size_t count,
                               bool primitiveRestartEnabled,
                               const IndexRange &range)
{
    mIndexRangeCache[IndexRangeKey(type, offset, count, primitiveRestartEnabled)] = range;
}

bool IndexRangeCache::findRange(DrawElementsType type,
                                size_t offset,
                                size_t count,
                                bool primitiveRestartEnabled,
                                IndexRange *outRange) const
{
    auto i = mIndexRangeCache.find(IndexRangeKey(type, offset, count, primitiveRestartEnabled));
    if (i != mIndexRangeCache.end())
    {
        if (outRange)
        {
            *outRange = i->second;
        }
        return true;
    }
    else
    {
        if (outRange)
        {
            *outRange = IndexRange();
        }
        return false;
    }
}

void IndexRangeCache::invalidateRange(size_t offset, size_t size)
{
    size_t invalidateStart = offset;
    size_t invalidateEnd   = offset + size;

    auto i = mIndexRangeCache.begin();
    while (i != mIndexRangeCache.end())
    {
        size_t rangeStart = i->first.offset;
        size_t rangeEnd =
            i->first.offset + (GetDrawElementsTypeSize(i->first.type) * i->first.count);

        if (invalidateEnd < rangeStart || invalidateStart > rangeEnd)
        {
            ++i;
        }
        else
        {
            mIndexRangeCache.erase(i++);
        }
    }
}

void IndexRangeCache::clear()
{
    mIndexRangeCache.clear();
}

IndexRangeCache::IndexRangeKey::IndexRangeKey()
    : IndexRangeCache::IndexRangeKey(DrawElementsType::InvalidEnum, 0, 0, false)
{}

IndexRangeCache::IndexRangeKey::IndexRangeKey(DrawElementsType type_,
                                              size_t offset_,
                                              size_t count_,
                                              bool primitiveRestartEnabled_)
    : type(type_), offset(offset_), count(count_), primitiveRestartEnabled(primitiveRestartEnabled_)
{}

bool IndexRangeCache::IndexRangeKey::operator<(const IndexRangeKey &rhs) const
{
    if (type != rhs.type)
    {
        return type < rhs.type;
    }
    if (offset != rhs.offset)
    {
        return offset < rhs.offset;
    }
    if (count != rhs.count)
    {
        return count < rhs.count;
    }
    if (primitiveRestartEnabled != rhs.primitiveRestartEnabled)
    {
        return primitiveRestartEnabled;
    }
    return false;
}

}  // namespace gl