Switch SkSL to std::string

On desktop, this saves just over 5% of the time in the SkSL compiler.

As written, the code will now build either way, so it's much easier to
switch back (or even have some platforms use SkString, if that's ever
required).

Bug: skia:
Change-Id: I634f26a4f6fcb404e59bda6a5c6a21a9c6d73c0b
Reviewed-on: https://skia-review.googlesource.com/34381
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
diff --git a/bench/GLBench.cpp b/bench/GLBench.cpp
index f043c95..316cca6 100644
--- a/bench/GLBench.cpp
+++ b/bench/GLBench.cpp
@@ -73,7 +73,7 @@
     std::unique_ptr<SkSL::Program> program = context->compiler()->convertProgram(
                                         type == GR_GL_VERTEX_SHADER ? SkSL::Program::kVertex_Kind
                                                                     : SkSL::Program::kFragment_Kind,
-                                        SkString(sksl),
+                                        SkSL::String(sksl),
                                         settings);
     if (!program || !context->compiler()->toGLSL(*program, &glsl)) {
         SkDebugf("SkSL compilation failed:\n%s\n%s\n", sksl,
diff --git a/fuzz/fuzz.cpp b/fuzz/fuzz.cpp
index f8897e6..bb2b1c8 100644
--- a/fuzz/fuzz.cpp
+++ b/fuzz/fuzz.cpp
@@ -543,8 +543,8 @@
     sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
     settings.fCaps = caps.get();
     std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
-                                                              SkString((const char*) bytes->data()),
-                                                              settings);
+                                                          SkSL::String((const char*) bytes->data()),
+                                                          settings);
     if (!program || !compiler.toGLSL(*program, &output)) {
         SkDebugf("[terminated] Couldn't compile input.\n");
         return;
diff --git a/src/effects/GrAlphaThresholdFragmentProcessor.cpp b/src/effects/GrAlphaThresholdFragmentProcessor.cpp
index 8c3afb7..bfd237d 100644
--- a/src/effects/GrAlphaThresholdFragmentProcessor.cpp
+++ b/src/effects/GrAlphaThresholdFragmentProcessor.cpp
@@ -39,9 +39,9 @@
                 kFragment_GrShaderFlag, kFloat_GrSLType, kDefault_GrSLPrecision, "innerThreshold");
         fOuterThresholdVar = args.fUniformHandler->addUniform(
                 kFragment_GrShaderFlag, kFloat_GrSLType, kDefault_GrSLPrecision, "outerThreshold");
-        SkSL::String sk_TransformedCoords2D_0 =
+        SkString sk_TransformedCoords2D_0 =
                 fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
-        SkSL::String sk_TransformedCoords2D_1 =
+        SkString sk_TransformedCoords2D_1 =
                 fragBuilder->ensureCoords2D(args.fTransformedCoords[1]);
         fragBuilder->codeAppendf(
                 "float4 _tmpVar1;float4 color = %stexture(%s, %s).%s%s;\nfloat4 mask_color = "
diff --git a/src/gpu/GrSKSLPrettyPrint.cpp b/src/gpu/GrSKSLPrettyPrint.cpp
index 65175c9..cb2da75 100644
--- a/src/gpu/GrSKSLPrettyPrint.cpp
+++ b/src/gpu/GrSKSLPrettyPrint.cpp
@@ -5,6 +5,7 @@
  * found in the LICENSE file.
  */
 #include "GrSKSLPrettyPrint.h"
+#include "SkSLString.h"
 
 namespace GrSKSLPrettyPrint {
 
@@ -12,7 +13,7 @@
 public:
     GLSLPrettyPrint() {}
 
-    SkString prettify(const char** strings, int* lengths, int count, bool countlines) {
+    SkSL::String prettify(const char** strings, int* lengths, int count, bool countlines) {
         fCountlines = countlines;
         fTabs = 0;
         fLinecount = 1;
@@ -184,7 +185,7 @@
     int fTabs, fLinecount;
     size_t fIndex, fLength;
     const char* fInput;
-    SkString fPretty;
+    SkSL::String fPretty;
 
     // Some helpers for parseUntil when we go over a string length
     bool fInParseUntilNewline;
@@ -192,7 +193,7 @@
     const char* fInParseUntilToken;
 };
 
-SkString PrettyPrint(const char** strings, int* lengths, int count, bool countlines) {
+SkSL::String PrettyPrint(const char** strings, int* lengths, int count, bool countlines) {
     GLSLPrettyPrint pp;
     return pp.prettify(strings, lengths, count, countlines);
 }
diff --git a/src/gpu/GrSKSLPrettyPrint.h b/src/gpu/GrSKSLPrettyPrint.h
index 8fa4c1e..530fcce 100644
--- a/src/gpu/GrSKSLPrettyPrint.h
+++ b/src/gpu/GrSKSLPrettyPrint.h
@@ -7,10 +7,10 @@
 #ifndef GrSKSLPrettyPrint_DEFINED
 #define GrSKSLPrettyPrint_DEFINED
 
-#include "SkString.h"
+#include "SkSLString.h"
 
 namespace GrSKSLPrettyPrint {
-SkString PrettyPrint(const char** strings, int* lengths, int count, bool countlines);
+SkSL::String PrettyPrint(const char** strings, int* lengths, int count, bool countlines);
 };
 
 #endif
diff --git a/src/gpu/effects/GrSimpleTextureEffect.cpp b/src/gpu/effects/GrSimpleTextureEffect.cpp
index 9a0017c..63f41a8 100644
--- a/src/gpu/effects/GrSimpleTextureEffect.cpp
+++ b/src/gpu/effects/GrSimpleTextureEffect.cpp
@@ -24,7 +24,7 @@
         const GrSimpleTextureEffect& _outer = args.fFp.cast<GrSimpleTextureEffect>();
         (void)_outer;
         fColorSpaceHelper.emitCode(args.fUniformHandler, _outer.colorXform().get());
-        SkSL::String sk_TransformedCoords2D_0 =
+        SkString sk_TransformedCoords2D_0 =
                 fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
         fragBuilder->codeAppendf(
                 "float4 _tmpVar1;%s = %s * %stexture(%s, %s).%s%s;\n", args.fOutputColor,
diff --git a/src/gpu/gl/builders/GrGLShaderStringBuilder.cpp b/src/gpu/gl/builders/GrGLShaderStringBuilder.cpp
index 3273218..f7eae42 100644
--- a/src/gpu/gl/builders/GrGLShaderStringBuilder.cpp
+++ b/src/gpu/gl/builders/GrGLShaderStringBuilder.cpp
@@ -37,10 +37,10 @@
                                      std::function<void(const char*)> println = [](const char* ln) {
                                          SkDebugf("%s\n", ln);
                                      }) {
-    SkString sksl = GrSKSLPrettyPrint::PrettyPrint(skslStrings, lengths, count, false);
+    SkSL::String sksl = GrSKSLPrettyPrint::PrettyPrint(skslStrings, lengths, count, false);
     println("SKSL:");
     print_source_lines_with_numbers(sksl.c_str(), println);
-    if (!glsl.isEmpty()) {
+    if (0 != glsl.size()) {
         println("GLSL:");
         print_source_lines_with_numbers(glsl.c_str(), println);
     }
@@ -50,7 +50,7 @@
                                                  const char** skslStrings, int* lengths, int count,
                                                  const SkSL::Program::Settings& settings,
                                                  SkSL::String* glsl) {
-    SkString sksl;
+    SkSL::String sksl;
 #ifdef SK_DEBUG
     sksl = GrSKSLPrettyPrint::PrettyPrint(skslStrings, lengths, count, false);
 #else
diff --git a/src/gpu/vk/GrVkUtil.cpp b/src/gpu/vk/GrVkUtil.cpp
index 84406c8..2ca701f 100644
--- a/src/gpu/vk/GrVkUtil.cpp
+++ b/src/gpu/vk/GrVkUtil.cpp
@@ -258,7 +258,7 @@
                              SkSL::Program::Inputs* outInputs) {
     std::unique_ptr<SkSL::Program> program = gpu->shaderCompiler()->convertProgram(
                                                               vk_shader_stage_to_skiasl_kind(stage),
-                                                              SkString(shaderString),
+                                                              SkSL::String(shaderString),
                                                               settings);
     if (!program) {
         SkDebugf("SkSL error:\n%s\n", gpu->shaderCompiler()->errorText().c_str());
diff --git a/src/sksl/SkSLString.cpp b/src/sksl/SkSLString.cpp
index fb8fd56..722d61f 100644
--- a/src/sksl/SkSLString.cpp
+++ b/src/sksl/SkSLString.cpp
@@ -24,7 +24,7 @@
     return result;
 }
 
-#ifdef SKSL_STANDALONE
+#ifdef SKSL_USE_STD_STRING
 void String::appendf(const char* fmt, ...) {
     va_list args;
     va_start(args, fmt);
diff --git a/src/sksl/SkSLString.h b/src/sksl/SkSLString.h
index 73ba746..2478c56 100644
--- a/src/sksl/SkSLString.h
+++ b/src/sksl/SkSLString.h
@@ -8,8 +8,9 @@
 #ifndef SKSL_STRING
 #define SKSL_STRING
 
+#define SKSL_USE_STD_STRING
 
-#ifdef SKSL_STANDALONE
+#ifdef SKSL_USE_STD_STRING
     #define SKSL_STRING_BASE std::string
     #include <string>
 #else
@@ -27,7 +28,7 @@
     String& operator=(const String&) = default;
     String& operator=(String&&) = default;
 
-#ifndef SKSL_STANDALONE
+#ifndef SKSL_USE_STD_STRING
     String(const SkString& s)
     : INHERITED(s) {}
 #endif
@@ -40,7 +41,7 @@
 
     static String printf(const char* fmt, ...);
 
-#ifdef SKSL_STANDALONE
+#ifdef SKSL_USE_STD_STRING
     void appendf(const char* fmt, ...);
 #endif
     void vappendf(const char* fmt, va_list va);
@@ -83,7 +84,7 @@
 
 } // namespace
 
-#ifdef SKSL_STANDALONE
+#ifdef SKSL_USE_STD_STRING
 namespace std {
     template<> struct hash<SkSL::String> {
         size_t operator()(const SkSL::String& s) const {
diff --git a/tests/GrSKSLPrettyPrintTest.cpp b/tests/GrSKSLPrettyPrintTest.cpp
index ec5dddb..c0de6c0 100644
--- a/tests/GrSKSLPrettyPrintTest.cpp
+++ b/tests/GrSKSLPrettyPrintTest.cpp
@@ -27,7 +27,7 @@
                      "an struct = { int a; int b; };"
                      "int[5] arr = int[5](1,2,3,4,5);} some code at the bottom; for(;;) {} }");
 
-const SkString output1(
+const SkSL::String output1(
         "   1\t#this is not a realshader\n"
         "   2\tvec4 some stuff;\n"
         "   3\toutside of a function;\n"
@@ -103,7 +103,7 @@
     testStr.push_back(input6.c_str());
     lengths.push_back((int)input6.size());
 
-    SkString test = GrSKSLPrettyPrint::PrettyPrint(testStr.begin(), lengths.begin(),
+    SkSL::String test = GrSKSLPrettyPrint::PrettyPrint(testStr.begin(), lengths.begin(),
                                                    testStr.count(), true);
     ASSERT(output1 == test);
 
diff --git a/tests/SkSLErrorTest.cpp b/tests/SkSLErrorTest.cpp
index df82987..b94cc82 100644
--- a/tests/SkSLErrorTest.cpp
+++ b/tests/SkSLErrorTest.cpp
@@ -16,7 +16,7 @@
     SkSL::Program::Settings settings;
     sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
     settings.fCaps = caps.get();
-    compiler.convertProgram(SkSL::Program::kFragment_Kind, SkString(src), settings);
+    compiler.convertProgram(SkSL::Program::kFragment_Kind, SkSL::String(src), settings);
     SkSL::String skError(error);
     if (compiler.errorText() != skError) {
         SkDebugf("SKSL ERROR:\n    source: %s\n    expected: %s    received: %s", src, error,
@@ -31,7 +31,7 @@
     sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
     settings.fCaps = caps.get();
     std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
-                                                                     SkString(src), settings);
+                                                                     SkSL::String(src), settings);
     REPORTER_ASSERT(r, program);
 }
 
diff --git a/tests/SkSLFPTest.cpp b/tests/SkSLFPTest.cpp
index 0c3da0b..1f983b7 100644
--- a/tests/SkSLFPTest.cpp
+++ b/tests/SkSLFPTest.cpp
@@ -19,7 +19,7 @@
     SkSL::StringStream output;
     std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
                                                              SkSL::Program::kFragmentProcessor_Kind,
-                                                             SkString(src),
+                                                             SkSL::String(src),
                                                              settings);
     if (!program) {
         SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str());
diff --git a/tests/SkSLGLSLTest.cpp b/tests/SkSLGLSLTest.cpp
index c1bf2bb..a7f6740 100644
--- a/tests/SkSLGLSLTest.cpp
+++ b/tests/SkSLGLSLTest.cpp
@@ -23,7 +23,8 @@
                  SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) {
     SkSL::Compiler compiler;
     SkSL::String output;
-    std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, SkString(src), settings);
+    std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, SkSL::String(src),
+                                                                     settings);
     if (!program) {
         SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str());
     }
diff --git a/tests/SkSLMemoryLayoutTest.cpp b/tests/SkSLMemoryLayoutTest.cpp
index 0a5d382..80188e6 100644
--- a/tests/SkSLMemoryLayoutTest.cpp
+++ b/tests/SkSLMemoryLayoutTest.cpp
@@ -54,40 +54,40 @@
 
     // struct 1
     std::vector<SkSL::Type::Field> fields1;
-    fields1.emplace_back(SkSL::Modifiers(), SkString("a"), context.fFloat3_Type.get());
-    SkSL::Type s1(SkSL::Position(), SkString("s1"), fields1);
+    fields1.emplace_back(SkSL::Modifiers(), SkSL::String("a"), context.fFloat3_Type.get());
+    SkSL::Type s1(SkSL::Position(), SkSL::String("s1"), fields1);
     REPORTER_ASSERT(r, 16 == layout.size(s1));
     REPORTER_ASSERT(r, 16 == layout.alignment(s1));
 
-    fields1.emplace_back(SkSL::Modifiers(), SkString("b"), context.fFloat_Type.get());
-    SkSL::Type s2(SkSL::Position(), SkString("s2"), fields1);
+    fields1.emplace_back(SkSL::Modifiers(), SkSL::String("b"), context.fFloat_Type.get());
+    SkSL::Type s2(SkSL::Position(), SkSL::String("s2"), fields1);
     REPORTER_ASSERT(r, 16 == layout.size(s2));
     REPORTER_ASSERT(r, 16 == layout.alignment(s2));
 
-    fields1.emplace_back(SkSL::Modifiers(), SkString("c"), context.fBool_Type.get());
-    SkSL::Type s3(SkSL::Position(), SkString("s3"), fields1);
+    fields1.emplace_back(SkSL::Modifiers(), SkSL::String("c"), context.fBool_Type.get());
+    SkSL::Type s3(SkSL::Position(), SkSL::String("s3"), fields1);
     REPORTER_ASSERT(r, 32 == layout.size(s3));
     REPORTER_ASSERT(r, 16 == layout.alignment(s3));
 
     // struct 2
     std::vector<SkSL::Type::Field> fields2;
-    fields2.emplace_back(SkSL::Modifiers(), SkString("a"), context.fInt_Type.get());
-    SkSL::Type s4(SkSL::Position(), SkString("s4"), fields2);
+    fields2.emplace_back(SkSL::Modifiers(), SkSL::String("a"), context.fInt_Type.get());
+    SkSL::Type s4(SkSL::Position(), SkSL::String("s4"), fields2);
     REPORTER_ASSERT(r, 16 == layout.size(s4));
     REPORTER_ASSERT(r, 16 == layout.alignment(s4));
 
-    fields2.emplace_back(SkSL::Modifiers(), SkString("b"), context.fFloat3_Type.get());
-    SkSL::Type s5(SkSL::Position(), SkString("s5"), fields2);
+    fields2.emplace_back(SkSL::Modifiers(), SkSL::String("b"), context.fFloat3_Type.get());
+    SkSL::Type s5(SkSL::Position(), SkSL::String("s5"), fields2);
     REPORTER_ASSERT(r, 32 == layout.size(s5));
     REPORTER_ASSERT(r, 16 == layout.alignment(s5));
 
     // arrays
-    SkSL::Type array1(SkString("float[4]"), SkSL::Type::kArray_Kind, *context.fFloat_Type, 4);
+    SkSL::Type array1(SkSL::String("float[4]"), SkSL::Type::kArray_Kind, *context.fFloat_Type, 4);
     REPORTER_ASSERT(r, 64 == layout.size(array1));
     REPORTER_ASSERT(r, 16 == layout.alignment(array1));
     REPORTER_ASSERT(r, 16 == layout.stride(array1));
 
-    SkSL::Type array2(SkString("float4[4]"), SkSL::Type::kArray_Kind, *context.fFloat4_Type, 4);
+    SkSL::Type array2(SkSL::String("float4[4]"), SkSL::Type::kArray_Kind, *context.fFloat4_Type, 4);
     REPORTER_ASSERT(r, 64 == layout.size(array2));
     REPORTER_ASSERT(r, 16 == layout.alignment(array2));
     REPORTER_ASSERT(r, 16 == layout.stride(array2));
@@ -135,40 +135,40 @@
 
     // struct 1
     std::vector<SkSL::Type::Field> fields1;
-    fields1.emplace_back(SkSL::Modifiers(), SkString("a"), context.fFloat3_Type.get());
-    SkSL::Type s1(SkSL::Position(), SkString("s1"), fields1);
+    fields1.emplace_back(SkSL::Modifiers(), SkSL::String("a"), context.fFloat3_Type.get());
+    SkSL::Type s1(SkSL::Position(), SkSL::String("s1"), fields1);
     REPORTER_ASSERT(r, 16 == layout.size(s1));
     REPORTER_ASSERT(r, 16 == layout.alignment(s1));
 
-    fields1.emplace_back(SkSL::Modifiers(), SkString("b"), context.fFloat_Type.get());
-    SkSL::Type s2(SkSL::Position(), SkString("s2"), fields1);
+    fields1.emplace_back(SkSL::Modifiers(), SkSL::String("b"), context.fFloat_Type.get());
+    SkSL::Type s2(SkSL::Position(), SkSL::String("s2"), fields1);
     REPORTER_ASSERT(r, 16 == layout.size(s2));
     REPORTER_ASSERT(r, 16 == layout.alignment(s2));
 
-    fields1.emplace_back(SkSL::Modifiers(), SkString("c"), context.fBool_Type.get());
-    SkSL::Type s3(SkSL::Position(), SkString("s3"), fields1);
+    fields1.emplace_back(SkSL::Modifiers(), SkSL::String("c"), context.fBool_Type.get());
+    SkSL::Type s3(SkSL::Position(), SkSL::String("s3"), fields1);
     REPORTER_ASSERT(r, 32 == layout.size(s3));
     REPORTER_ASSERT(r, 16 == layout.alignment(s3));
 
     // struct 2
     std::vector<SkSL::Type::Field> fields2;
-    fields2.emplace_back(SkSL::Modifiers(), SkString("a"), context.fInt_Type.get());
-    SkSL::Type s4(SkSL::Position(), SkString("s4"), fields2);
+    fields2.emplace_back(SkSL::Modifiers(), SkSL::String("a"), context.fInt_Type.get());
+    SkSL::Type s4(SkSL::Position(), SkSL::String("s4"), fields2);
     REPORTER_ASSERT(r, 4 == layout.size(s4));
     REPORTER_ASSERT(r, 4 == layout.alignment(s4));
 
-    fields2.emplace_back(SkSL::Modifiers(), SkString("b"), context.fFloat3_Type.get());
-    SkSL::Type s5(SkSL::Position(), SkString("s5"), fields2);
+    fields2.emplace_back(SkSL::Modifiers(), SkSL::String("b"), context.fFloat3_Type.get());
+    SkSL::Type s5(SkSL::Position(), SkSL::String("s5"), fields2);
     REPORTER_ASSERT(r, 32 == layout.size(s5));
     REPORTER_ASSERT(r, 16 == layout.alignment(s5));
 
     // arrays
-    SkSL::Type array1(SkString("float[4]"), SkSL::Type::kArray_Kind, *context.fFloat_Type, 4);
+    SkSL::Type array1(SkSL::String("float[4]"), SkSL::Type::kArray_Kind, *context.fFloat_Type, 4);
     REPORTER_ASSERT(r, 16 == layout.size(array1));
     REPORTER_ASSERT(r, 4 == layout.alignment(array1));
     REPORTER_ASSERT(r, 4 == layout.stride(array1));
 
-    SkSL::Type array2(SkString("float4[4]"), SkSL::Type::kArray_Kind, *context.fFloat4_Type, 4);
+    SkSL::Type array2(SkSL::String("float4[4]"), SkSL::Type::kArray_Kind, *context.fFloat4_Type, 4);
     REPORTER_ASSERT(r, 64 == layout.size(array2));
     REPORTER_ASSERT(r, 16 == layout.alignment(array2));
     REPORTER_ASSERT(r, 16 == layout.stride(array2));
diff --git a/tests/SkSLSPIRVTest.cpp b/tests/SkSLSPIRVTest.cpp
index 4d5457f..ba4697d 100644
--- a/tests/SkSLSPIRVTest.cpp
+++ b/tests/SkSLSPIRVTest.cpp
@@ -17,7 +17,7 @@
     sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
     settings.fCaps = caps.get();
     std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
-                                                                     SkString(src), settings);
+                                                                     SkSL::String(src), settings);
     if (program) {
         SkSL::String ignored;
         compiler.toSPIRV(*program, &ignored);