Use std::unordered_map in ResourceManager.

The hashed query performs significantly faster than std::map.
This improves performance in applications which use Bind* often.

Also use the same scheme for the Context-managed resources.

BUG=angleproject:1333

Change-Id: I8a1f25d80e060c62dab1f86005e1bc4df1c06dba
Reviewed-on: https://chromium-review.googlesource.com/331387
Reviewed-by: Zhenyao Mo <zmo@chromium.org>
Commit-Queue: Jamie Madill <jmadill@chromium.org>
diff --git a/src/libANGLE/Context.cpp b/src/libANGLE/Context.cpp
index 031b98e..dba4aa9 100644
--- a/src/libANGLE/Context.cpp
+++ b/src/libANGLE/Context.cpp
@@ -567,7 +567,7 @@
 
 void Context::deleteFramebuffer(GLuint framebuffer)
 {
-    FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
+    auto framebufferObject = mFramebufferMap.find(framebuffer);
 
     if (framebufferObject != mFramebufferMap.end())
     {
@@ -581,7 +581,7 @@
 
 void Context::deleteFenceNV(GLuint fence)
 {
-    FenceNVMap::iterator fenceObject = mFenceNVMap.find(fence);
+    auto fenceObject = mFenceNVMap.find(fence);
 
     if (fenceObject != mFenceNVMap.end())
     {
@@ -593,7 +593,7 @@
 
 void Context::deleteQuery(GLuint query)
 {
-    QueryMap::iterator queryObject = mQueryMap.find(query);
+    auto queryObject = mQueryMap.find(query);
     if (queryObject != mQueryMap.end())
     {
         mQueryHandleAllocator.release(queryObject->first);
@@ -915,7 +915,7 @@
 
 FenceNV *Context::getFenceNV(unsigned int handle)
 {
-    FenceNVMap::iterator fence = mFenceNVMap.find(handle);
+    auto fence = mFenceNVMap.find(handle);
 
     if (fence == mFenceNVMap.end())
     {
@@ -929,7 +929,7 @@
 
 Query *Context::getQuery(unsigned int handle, bool create, GLenum type)
 {
-    QueryMap::iterator query = mQueryMap.find(handle);
+    auto query = mQueryMap.find(handle);
 
     if (query == mQueryMap.end())
     {
diff --git a/src/libANGLE/Context.h b/src/libANGLE/Context.h
index 968fb30..0a95b3f 100644
--- a/src/libANGLE/Context.h
+++ b/src/libANGLE/Context.h
@@ -10,6 +10,10 @@
 #ifndef LIBANGLE_CONTEXT_H_
 #define LIBANGLE_CONTEXT_H_
 
+#include <set>
+#include <string>
+
+#include "angle_gl.h"
 #include "common/angleutils.h"
 #include "libANGLE/RefCountObject.h"
 #include "libANGLE/Caps.h"
@@ -20,12 +24,6 @@
 #include "libANGLE/VertexAttribute.h"
 #include "libANGLE/angletypes.h"
 
-#include "angle_gl.h"
-
-#include <string>
-#include <set>
-#include <map>
-
 namespace rx
 {
 class Renderer;
@@ -439,24 +437,19 @@
 
     TextureMap mZeroTextures;
 
-    typedef std::map<GLuint, Framebuffer*> FramebufferMap;
-    FramebufferMap mFramebufferMap;
+    ResourceMap<Framebuffer> mFramebufferMap;
     HandleAllocator mFramebufferHandleAllocator;
 
-    typedef std::map<GLuint, FenceNV*> FenceNVMap;
-    FenceNVMap mFenceNVMap;
+    ResourceMap<FenceNV> mFenceNVMap;
     HandleAllocator mFenceNVHandleAllocator;
 
-    typedef std::map<GLuint, Query*> QueryMap;
-    QueryMap mQueryMap;
+    ResourceMap<Query> mQueryMap;
     HandleAllocator mQueryHandleAllocator;
 
-    typedef std::map<GLuint, VertexArray*> VertexArrayMap;
-    VertexArrayMap mVertexArrayMap;
+    ResourceMap<VertexArray> mVertexArrayMap;
     HandleAllocator mVertexArrayHandleAllocator;
 
-    typedef std::map<GLuint, TransformFeedback*> TransformFeedbackMap;
-    TransformFeedbackMap mTransformFeedbackMap;
+    ResourceMap<TransformFeedback> mTransformFeedbackMap;
     HandleAllocator mTransformFeedbackAllocator;
 
     std::string mRendererString;
diff --git a/src/libANGLE/ResourceManager.cpp b/src/libANGLE/ResourceManager.cpp
index db2c55f..3872d39 100644
--- a/src/libANGLE/ResourceManager.cpp
+++ b/src/libANGLE/ResourceManager.cpp
@@ -82,7 +82,7 @@
 {
     GLuint handle = mBufferHandleAllocator.allocate();
 
-    mBufferMap[handle] = NULL;
+    mBufferMap[handle] = nullptr;
 
     return handle;
 }
@@ -116,7 +116,7 @@
 {
     GLuint handle = mTextureHandleAllocator.allocate();
 
-    mTextureMap[handle] = NULL;
+    mTextureMap[handle] = nullptr;
 
     return handle;
 }
@@ -126,7 +126,7 @@
 {
     GLuint handle = mRenderbufferHandleAllocator.allocate();
 
-    mRenderbufferMap[handle] = NULL;
+    mRenderbufferMap[handle] = nullptr;
 
     return handle;
 }
@@ -136,7 +136,7 @@
 {
     GLuint handle = mSamplerHandleAllocator.allocate();
 
-    mSamplerMap[handle] = NULL;
+    mSamplerMap[handle] = nullptr;
 
     return handle;
 }
@@ -155,7 +155,7 @@
 
 void ResourceManager::deleteBuffer(GLuint buffer)
 {
-    BufferMap::iterator bufferObject = mBufferMap.find(buffer);
+    auto bufferObject = mBufferMap.find(buffer);
 
     if (bufferObject != mBufferMap.end())
     {
@@ -167,7 +167,7 @@
 
 void ResourceManager::deleteShader(GLuint shader)
 {
-    ShaderMap::iterator shaderObject = mShaderMap.find(shader);
+    auto shaderObject = mShaderMap.find(shader);
 
     if (shaderObject != mShaderMap.end())
     {
@@ -186,7 +186,7 @@
 
 void ResourceManager::deleteProgram(GLuint program)
 {
-    ProgramMap::iterator programObject = mProgramMap.find(program);
+    auto programObject = mProgramMap.find(program);
 
     if (programObject != mProgramMap.end())
     {
@@ -205,7 +205,7 @@
 
 void ResourceManager::deleteTexture(GLuint texture)
 {
-    TextureMap::iterator textureObject = mTextureMap.find(texture);
+    auto textureObject = mTextureMap.find(texture);
 
     if (textureObject != mTextureMap.end())
     {
@@ -217,7 +217,7 @@
 
 void ResourceManager::deleteRenderbuffer(GLuint renderbuffer)
 {
-    RenderbufferMap::iterator renderbufferObject = mRenderbufferMap.find(renderbuffer);
+    auto renderbufferObject = mRenderbufferMap.find(renderbuffer);
 
     if (renderbufferObject != mRenderbufferMap.end())
     {
@@ -253,11 +253,11 @@
 
 Buffer *ResourceManager::getBuffer(unsigned int handle)
 {
-    BufferMap::iterator buffer = mBufferMap.find(handle);
+    auto buffer = mBufferMap.find(handle);
 
     if (buffer == mBufferMap.end())
     {
-        return NULL;
+        return nullptr;
     }
     else
     {
@@ -267,11 +267,11 @@
 
 Shader *ResourceManager::getShader(unsigned int handle)
 {
-    ShaderMap::iterator shader = mShaderMap.find(handle);
+    auto shader = mShaderMap.find(handle);
 
     if (shader == mShaderMap.end())
     {
-        return NULL;
+        return nullptr;
     }
     else
     {
@@ -281,13 +281,14 @@
 
 Texture *ResourceManager::getTexture(unsigned int handle)
 {
-    if (handle == 0) return NULL;
+    if (handle == 0)
+        return nullptr;
 
-    TextureMap::iterator texture = mTextureMap.find(handle);
+    auto texture = mTextureMap.find(handle);
 
     if (texture == mTextureMap.end())
     {
-        return NULL;
+        return nullptr;
     }
     else
     {
@@ -297,11 +298,11 @@
 
 Program *ResourceManager::getProgram(unsigned int handle) const
 {
-    ProgramMap::const_iterator program = mProgramMap.find(handle);
+    auto program = mProgramMap.find(handle);
 
     if (program == mProgramMap.end())
     {
-        return NULL;
+        return nullptr;
     }
     else
     {
@@ -311,11 +312,11 @@
 
 Renderbuffer *ResourceManager::getRenderbuffer(unsigned int handle)
 {
-    RenderbufferMap::iterator renderbuffer = mRenderbufferMap.find(handle);
+    auto renderbuffer = mRenderbufferMap.find(handle);
 
     if (renderbuffer == mRenderbufferMap.end())
     {
-        return NULL;
+        return nullptr;
     }
     else
     {
@@ -329,7 +330,7 @@
 
     if (sampler == mSamplerMap.end())
     {
-        return NULL;
+        return nullptr;
     }
     else
     {
@@ -343,7 +344,7 @@
 
     if (fenceObjectIt == mFenceSyncMap.end())
     {
-        return NULL;
+        return nullptr;
     }
     else
     {
diff --git a/src/libANGLE/ResourceManager.h b/src/libANGLE/ResourceManager.h
index fad47f6..4845e39 100644
--- a/src/libANGLE/ResourceManager.h
+++ b/src/libANGLE/ResourceManager.h
@@ -15,8 +15,6 @@
 #include "libANGLE/angletypes.h"
 #include "libANGLE/HandleAllocator.h"
 
-#include <map>
-
 namespace rx
 {
 class ImplFactory;
@@ -82,31 +80,24 @@
     rx::ImplFactory *mFactory;
     std::size_t mRefCount;
 
-    typedef std::map<GLuint, Buffer*> BufferMap;
-    BufferMap mBufferMap;
+    ResourceMap<Buffer> mBufferMap;
     HandleAllocator mBufferHandleAllocator;
 
-    typedef std::map<GLuint, Shader*> ShaderMap;
-    ShaderMap mShaderMap;
+    ResourceMap<Shader> mShaderMap;
 
-    typedef std::map<GLuint, Program*> ProgramMap;
-    ProgramMap mProgramMap;
+    ResourceMap<Program> mProgramMap;
     HandleAllocator mProgramShaderHandleAllocator;
 
-    typedef std::map<GLuint, Texture*> TextureMap;
-    TextureMap mTextureMap;
+    ResourceMap<Texture> mTextureMap;
     HandleAllocator mTextureHandleAllocator;
 
-    typedef std::map<GLuint, Renderbuffer*> RenderbufferMap;
-    RenderbufferMap mRenderbufferMap;
+    ResourceMap<Renderbuffer> mRenderbufferMap;
     HandleAllocator mRenderbufferHandleAllocator;
 
-    typedef std::map<GLuint, Sampler*> SamplerMap;
-    SamplerMap mSamplerMap;
+    ResourceMap<Sampler> mSamplerMap;
     HandleAllocator mSamplerHandleAllocator;
 
-    typedef std::map<GLuint, FenceSync*> FenceMap;
-    FenceMap mFenceSyncMap;
+    ResourceMap<FenceSync> mFenceSyncMap;
     HandleAllocator mFenceSyncHandleAllocator;
 };
 
diff --git a/src/libANGLE/angletypes.h b/src/libANGLE/angletypes.h
index c29ad06..0842be3 100644
--- a/src/libANGLE/angletypes.h
+++ b/src/libANGLE/angletypes.h
@@ -15,6 +15,7 @@
 #include <stdint.h>
 
 #include <bitset>
+#include <unordered_map>
 
 namespace gl
 {
@@ -301,6 +302,11 @@
 
 // Use in Program
 typedef std::bitset<IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS> UniformBlockBindingMask;
+
+// A map of GL objects indexed by object ID. The specific map implementation may change.
+// Client code should treat it as a std::map.
+template <class ResourceT>
+using ResourceMap = std::unordered_map<GLuint, ResourceT *>;
 }
 
 namespace rx