void TilesManager::deallocateTexturesVector(unsigned long long sparedDrawCount,
                                            WTF::Vector<BaseTileTexture*>& textures)
{
    const unsigned int max = textures.size();
    int dealloc = 0;
    for (unsigned int i = 0; i < max; i++) {
        TextureOwner* owner = textures[i]->owner();
        if (!owner || owner->drawCount() < sparedDrawCount) {
            textures[i]->discardGLTexture();
            dealloc++;
        }
    }
    XLOG("Deallocated %d gl textures (out of %d base tiles and %d layer tiles)",
         dealloc, max, maxLayer);
}
void TilesManager::deallocateTextures(bool allTextures)
{
    const unsigned int max = m_textures.size();

    unsigned long long sparedDrawCount = ~0; // by default, spare no textures
    if (!allTextures) {
        // if we're not deallocating all textures, spare those with max drawcount
        sparedDrawCount = 0;
        for (unsigned int i = 0; i < max; i++) {
            TextureOwner* owner = m_textures[i]->owner();
            if (owner)
                sparedDrawCount = std::max(sparedDrawCount, owner->drawCount());
        }
    }
    deallocateTexturesVector(sparedDrawCount, m_textures);
    deallocateTexturesVector(sparedDrawCount, m_tilesTextures);
}
void TilesManager::discardTexturesVector(unsigned long long sparedDrawCount,
                                         WTF::Vector<TileTexture*>& textures,
                                         bool deallocateGLTextures)
{
    const unsigned int max = textures.size();
    int dealloc = 0;
    WTF::Vector<int> discardedIndex;
    for (unsigned int i = 0; i < max; i++) {
        TextureOwner* owner = textures[i]->owner();
        if (!owner || owner->drawCount() < sparedDrawCount) {
            if (deallocateGLTextures) {
                // deallocate textures' gl memory
                textures[i]->discardGLTexture();
                discardedIndex.append(i);
            } else if (owner) {
                // simply detach textures from owner
                static_cast<Tile*>(owner)->discardTextures();
            }
            dealloc++;
        }
    }

    bool base = textures == m_textures;
    // Clean up the vector of TileTextures and reset the max texture count.
    if (discardedIndex.size()) {
        android::Mutex::Autolock lock(m_texturesLock);
        for (int i = discardedIndex.size() - 1; i >= 0; i--)
            textures.remove(discardedIndex[i]);

        int remainedTextureNumber = textures.size();
        int* countPtr = base ? &m_currentTextureCount : &m_currentLayerTextureCount;
        if (remainedTextureNumber < *countPtr) {
            ALOGV("reset currentTextureCount for %s tiles from %d to %d",
                  base ? "base" : "layer", *countPtr, remainedTextureNumber);
            *countPtr = remainedTextureNumber;
        }

    }

    ALOGV("Discarded %d %s textures (out of %d %s tiles)",
          dealloc, (deallocateGLTextures ? "gl" : ""),
          max, base ? "base" : "layer");
}