Enable SDF text when using a perspective matrix.

Also fixes some state issues in SampleApp.

Change-Id: I854754e8b547f7e62aa90914520aaaa20095f965
Reviewed-on: https://skia-review.googlesource.com/20975
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Jim Van Verth <jvanverth@google.com>
diff --git a/samplecode/SampleApp.cpp b/samplecode/SampleApp.cpp
index 16dbef2..dde939b 100644
--- a/samplecode/SampleApp.cpp
+++ b/samplecode/SampleApp.cpp
@@ -2026,23 +2026,19 @@
 }
 
 void SampleWindow::toggleDistanceFieldFonts() {
-    // reset backend
-    fDevManager->tearDownBackend(this);
-    fDevManager->setUpBackend(this, fBackendOptions);
-
     SkSurfaceProps props = this->getSurfaceProps();
     uint32_t flags = props.flags() ^ SkSurfaceProps::kUseDeviceIndependentFonts_Flag;
     this->setSurfaceProps(SkSurfaceProps(flags, props.pixelGeometry()));
 
+    // reset backend
+    fDevManager->tearDownBackend(this);
+    fDevManager->setUpBackend(this, fBackendOptions);
+
     this->updateTitle();
     this->inval(nullptr);
 }
 
 void SampleWindow::setPixelGeometry(int pixelGeometryIndex) {
-    // reset backend
-    fDevManager->tearDownBackend(this);
-    fDevManager->setUpBackend(this, fBackendOptions);
-
     const SkSurfaceProps& oldProps = this->getSurfaceProps();
     SkSurfaceProps newProps(oldProps.flags(), SkSurfaceProps::kLegacyFontHost_InitType);
     if (pixelGeometryIndex > 0) {
@@ -2051,6 +2047,10 @@
     }
     this->setSurfaceProps(newProps);
 
+    // reset backend
+    fDevManager->tearDownBackend(this);
+    fDevManager->setUpBackend(this, fBackendOptions);
+
     this->updateTitle();
     this->inval(nullptr);
 }
diff --git a/src/gpu/text/GrTextUtils.cpp b/src/gpu/text/GrTextUtils.cpp
index 20001a4..20d5aaf 100644
--- a/src/gpu/text/GrTextUtils.cpp
+++ b/src/gpu/text/GrTextUtils.cpp
@@ -197,27 +197,24 @@
 
 bool GrTextUtils::CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix,
                                           const SkSurfaceProps& props, const GrShaderCaps& caps) {
-    // TODO: support perspective (need getMaxScale replacement)
-    if (viewMatrix.hasPerspective()) {
-        return false;
-    }
+    if (!viewMatrix.hasPerspective()) {
+        SkScalar maxScale = viewMatrix.getMaxScale();
+        SkScalar scaledTextSize = maxScale * skPaint.getTextSize();
+        // Hinted text looks far better at small resolutions
+        // Scaling up beyond 2x yields undesireable artifacts
+        if (scaledTextSize < kMinDFFontSize ||
+            scaledTextSize > kLargeDFFontLimit) {
+            return false;
+        }
 
-    SkScalar maxScale = viewMatrix.getMaxScale();
-    SkScalar scaledTextSize = maxScale * skPaint.getTextSize();
-    // Hinted text looks far better at small resolutions
-    // Scaling up beyond 2x yields undesireable artifacts
-    if (scaledTextSize < kMinDFFontSize ||
-        scaledTextSize > kLargeDFFontLimit) {
-        return false;
-    }
-
-    bool useDFT = props.isUseDeviceIndependentFonts();
+        bool useDFT = props.isUseDeviceIndependentFonts();
 #if SK_FORCE_DISTANCE_FIELD_TEXT
-    useDFT = true;
+        useDFT = true;
 #endif
 
-    if (!useDFT && scaledTextSize < kLargeDFFontSize) {
-        return false;
+        if (!useDFT && scaledTextSize < kLargeDFFontSize) {
+            return false;
+        }
     }
 
     // rasterizers and mask filters modify alpha, which doesn't
@@ -238,16 +235,21 @@
                                          SkPaint* skPaint,
                                          SkScalar* textRatio,
                                          const SkMatrix& viewMatrix) {
-    // getMaxScale doesn't support perspective, so neither do we at the moment
-    SkASSERT(!viewMatrix.hasPerspective());
-    SkScalar maxScale = viewMatrix.getMaxScale();
     SkScalar textSize = skPaint->getTextSize();
     SkScalar scaledTextSize = textSize;
-    // if we have non-unity scale, we need to choose our base text size
-    // based on the SkPaint's text size multiplied by the max scale factor
-    // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
-    if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
-        scaledTextSize *= maxScale;
+
+    if (viewMatrix.hasPerspective()) {
+        // for perspective, we simply force to the medium size
+        // TODO: compute a size based on approximate screen area
+        scaledTextSize = kMediumDFFontLimit;
+    } else {
+        SkScalar maxScale = viewMatrix.getMaxScale();
+        // if we have non-unity scale, we need to choose our base text size
+        // based on the SkPaint's text size multiplied by the max scale factor
+        // TODO: do we need to do this if we're scaling down (i.e. maxScale < 1)?
+        if (maxScale > 0 && !SkScalarNearlyEqual(maxScale, SK_Scalar1)) {
+            scaledTextSize *= maxScale;
+        }
     }
 
     // We have three sizes of distance field text, and within each size 'bucket' there is a floor