Use a global ref-counted singleton for long name map.

This makes sure the same varying/uniform variables maps to the unique name in vertex/fragment shader.

BUG=
TEST=webgl conformance tests
Review URL: https://codereview.appspot.com/5556065

git-svn-id: https://angleproject.googlecode.com/svn/trunk@950 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/common/version.h b/src/common/version.h
index 7c30a0b..effc283 100644
--- a/src/common/version.h
+++ b/src/common/version.h
@@ -1,7 +1,7 @@
 #define MAJOR_VERSION 1
 #define MINOR_VERSION 0
 #define BUILD_VERSION 0
-#define BUILD_REVISION 949
+#define BUILD_REVISION 950
 
 #define STRINGIFY(x) #x
 #define MACRO_STRINGIFY(x) STRINGIFY(x)
diff --git a/src/compiler/Compiler.cpp b/src/compiler/Compiler.cpp
index 22764bd..f27cb75 100644
--- a/src/compiler/Compiler.cpp
+++ b/src/compiler/Compiler.cpp
@@ -1,5 +1,5 @@
 //
-// Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved.
+// Copyright (c) 2002-2012 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.
 //
@@ -8,10 +8,10 @@
 #include "compiler/DetectRecursion.h"
 #include "compiler/ForLoopUnroll.h"
 #include "compiler/Initialize.h"
+#include "compiler/MapLongVariableNames.h"
 #include "compiler/ParseHelper.h"
 #include "compiler/ShHandle.h"
 #include "compiler/ValidateLimitations.h"
-#include "compiler/MapLongVariableNames.h"
 
 namespace {
 bool InitializeSymbolTable(
@@ -91,10 +91,13 @@
       shaderSpec(spec),
       builtInFunctionEmulator(type)
 {
+    longNameMap = LongNameMap::GetInstance();
 }
 
 TCompiler::~TCompiler()
 {
+    ASSERT(longNameMap);
+    longNameMap->Release();
 }
 
 bool TCompiler::Init(const ShBuiltInResources& resources)
@@ -246,7 +249,8 @@
 
 void TCompiler::mapLongVariableNames(TIntermNode* root)
 {
-    MapLongVariableNames map(varyingLongNameMap);
+    ASSERT(longNameMap);
+    MapLongVariableNames map(longNameMap);
     root->traverse(&map);
 }
 
diff --git a/src/compiler/MapLongVariableNames.cpp b/src/compiler/MapLongVariableNames.cpp
index 3c5d356..0c7e1a9 100644
--- a/src/compiler/MapLongVariableNames.cpp
+++ b/src/compiler/MapLongVariableNames.cpp
@@ -8,26 +8,77 @@
 
 namespace {
 
-TString mapLongName(int id, const TString& name, bool isVarying)
+TString mapLongName(int id, const TString& name, bool isGlobal)
 {
     ASSERT(name.size() > MAX_SHORTENED_IDENTIFIER_SIZE);
     TStringStream stream;
     stream << "webgl_";
-    if (isVarying)
-        stream << "v";
+    if (isGlobal)
+        stream << "g";
     stream << id << "_";
     stream << name.substr(0, MAX_SHORTENED_IDENTIFIER_SIZE - stream.str().size());
     return stream.str();
 }
 
+LongNameMap* gLongNameMapInstance = NULL;
+
 }  // anonymous namespace
 
-MapLongVariableNames::MapLongVariableNames(
-    std::map<std::string, std::string>& varyingLongNameMap)
-    : mVaryingLongNameMap(varyingLongNameMap)
+LongNameMap::LongNameMap()
+    : refCount(0)
 {
 }
 
+LongNameMap::~LongNameMap()
+{
+}
+
+// static
+LongNameMap* LongNameMap::GetInstance()
+{
+    if (gLongNameMapInstance == NULL)
+        gLongNameMapInstance = new LongNameMap;
+    gLongNameMapInstance->refCount++;
+    return gLongNameMapInstance;
+}
+
+void LongNameMap::Release()
+{
+    ASSERT(gLongNameMapInstance == this);
+    ASSERT(refCount > 0);
+    refCount--;
+    if (refCount == 0) {
+        delete gLongNameMapInstance;
+        gLongNameMapInstance = NULL;
+    }
+}
+
+const char* LongNameMap::Find(const char* originalName) const
+{
+    std::map<std::string, std::string>::const_iterator it = mLongNameMap.find(
+        originalName);
+    if (it != mLongNameMap.end())
+        return (*it).second.c_str();
+    return NULL;
+}
+
+void LongNameMap::Insert(const char* originalName, const char* mappedName)
+{
+    mLongNameMap.insert(std::map<std::string, std::string>::value_type(
+        originalName, mappedName));
+}
+
+int LongNameMap::Size() const
+{
+    return mLongNameMap.size();
+}
+
+MapLongVariableNames::MapLongVariableNames(LongNameMap* globalMap)
+{
+    ASSERT(globalMap);
+    mGlobalMap = globalMap;
+}
+
 void MapLongVariableNames::visitSymbol(TIntermSymbol* symbol)
 {
     ASSERT(symbol != NULL);
@@ -39,7 +90,7 @@
           case EvqInvariantVaryingOut:
           case EvqUniform:
             symbol->setSymbol(
-                mapVaryingLongName(symbol->getSymbol()));
+                mapGlobalLongName(symbol->getSymbol()));
             break;
           default:
             symbol->setSymbol(
@@ -56,15 +107,14 @@
     return true;
 }
 
-TString MapLongVariableNames::mapVaryingLongName(const TString& name)
+TString MapLongVariableNames::mapGlobalLongName(const TString& name)
 {
-    std::map<std::string, std::string>::const_iterator it = mVaryingLongNameMap.find(name.c_str());
-    if (it != mVaryingLongNameMap.end())
-        return (*it).second.c_str();
-
-    int id = mVaryingLongNameMap.size();
-    TString mappedName = mapLongName(id, name, true);
-    mVaryingLongNameMap.insert(
-        std::map<std::string, std::string>::value_type(name.c_str(), mappedName.c_str()));
-    return mappedName;
+    ASSERT(mGlobalMap);
+    const char* mappedName = mGlobalMap->Find(name.c_str());
+    if (mappedName != NULL)
+        return mappedName;
+    int id = mGlobalMap->Size();
+    TString rt = mapLongName(id, name, true);
+    mGlobalMap->Insert(name.c_str(), rt.c_str());
+    return rt;
 }
diff --git a/src/compiler/MapLongVariableNames.h b/src/compiler/MapLongVariableNames.h
index 086f2b9..fb2c7e8 100644
--- a/src/compiler/MapLongVariableNames.h
+++ b/src/compiler/MapLongVariableNames.h
@@ -1,5 +1,5 @@
 //
-// Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved.
+// Copyright (c) 2002-2012 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.
 //
@@ -15,19 +15,45 @@
 // This size does not include '\0' in the end.
 #define MAX_SHORTENED_IDENTIFIER_SIZE 32
 
+// This is a ref-counted singleton. GetInstance() returns a pointer to the
+// singleton, and after use, call Release(). GetInstance() and Release() should
+// be paired.
+class LongNameMap {
+public:
+    static LongNameMap* GetInstance();
+    void Release();
+
+    // Return the mapped name if <originalName, mappedName> is in the map;
+    // otherwise, return NULL.
+    const char* Find(const char* originalName) const;
+
+    // Insert a pair into the map.
+    void Insert(const char* originalName, const char* mappedName);
+
+    // Return the number of entries in the map.
+    int Size() const;
+
+private:
+    LongNameMap();
+    ~LongNameMap();
+
+    size_t refCount;
+    std::map<std::string, std::string> mLongNameMap;
+};
+
 // Traverses intermediate tree to map attributes and uniforms names that are
 // longer than MAX_SHORTENED_IDENTIFIER_SIZE to MAX_SHORTENED_IDENTIFIER_SIZE.
 class MapLongVariableNames : public TIntermTraverser {
 public:
-    MapLongVariableNames(std::map<std::string, std::string>& varyingLongNameMap);
+    MapLongVariableNames(LongNameMap* globalMap);
 
     virtual void visitSymbol(TIntermSymbol*);
     virtual bool visitLoop(Visit, TIntermLoop*);
 
 private:
-    TString mapVaryingLongName(const TString& name);
+    TString mapGlobalLongName(const TString& name);
 
-    std::map<std::string, std::string>& mVaryingLongNameMap;
+    LongNameMap* mGlobalMap;
 };
 
 #endif  // COMPILER_MAP_LONG_VARIABLE_NAMES_H_
diff --git a/src/compiler/ShHandle.h b/src/compiler/ShHandle.h
index 8ddf0f1..91c47e7 100644
--- a/src/compiler/ShHandle.h
+++ b/src/compiler/ShHandle.h
@@ -1,5 +1,5 @@
 //
-// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
+// Copyright (c) 2002-2012 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.
 //
@@ -22,6 +22,7 @@
 #include "compiler/SymbolTable.h"
 #include "compiler/VariableInfo.h"
 
+class LongNameMap;
 class TCompiler;
 
 //
@@ -100,8 +101,8 @@
     TVariableInfoList attribs;  // Active attributes in the compiled shader.
     TVariableInfoList uniforms;  // Active uniforms in the compiled shader.
 
-    // Pair of long varying varibale name <originalName, mappedName>.
-    std::map<std::string, std::string> varyingLongNameMap;
+    // Cached copy of the ref-counted singleton.
+    LongNameMap* longNameMap;
 };
 
 //