Turn off savelayer optimization for Android framework

This is a simpler version of https://skia-review.googlesource.com/c/8970/

Note that we couldn't do the optimization and add back the clip effect by
another clipRect or clipPath call because the final matrix is not available at
recording time.

BUG=skia:6334

Change-Id: I3ca091bdc1ee1d265a79fb88d0a6f9afe2b27e58
Reviewed-on: https://skia-review.googlesource.com/9311
Reviewed-by: Mike Klein <mtklein@chromium.org>
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Yuqian Li <liyuqian@google.com>
diff --git a/src/core/SkRecordOpts.cpp b/src/core/SkRecordOpts.cpp
index bd8364f..07ef19d 100644
--- a/src/core/SkRecordOpts.cpp
+++ b/src/core/SkRecordOpts.cpp
@@ -172,6 +172,7 @@
     while (apply(&onlyDraws, record) || apply(&noDraws, record));
 }
 
+#ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK
 static bool effectively_srcover(const SkPaint* paint) {
     if (!paint || paint->isSrcOver()) {
         return true;
@@ -229,7 +230,7 @@
     SaveLayerDrawRestoreNooper pass;
     apply(&pass, record);
 }
-
+#endif
 
 /* For SVG generated:
   SaveLayer (non-opaque, typically for CSS opacity)
@@ -297,7 +298,12 @@
     //     https://bugs.chromium.org/p/skia/issues/detail?id=5548
 //    SkRecordNoopSaveRestores(record);
 
+    // Turn off this optimization completely for Android framework
+    // because it makes the following Android CTS test fail:
+    // android.uirendering.cts.testclasses.LayerTests#testSaveLayerClippedWithAlpha
+#ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK
     SkRecordNoopSaveLayerDrawRestores(record);
+#endif
     SkRecordMergeSvgOpacityAndFilterLayers(record);
 
     record->defrag();
@@ -306,7 +312,10 @@
 void SkRecordOptimize2(SkRecord* record) {
     multiple_set_matrices(record);
     SkRecordNoopSaveRestores(record);
+    // See why we turn this off in SkRecordOptimize above.
+#ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK
     SkRecordNoopSaveLayerDrawRestores(record);
+#endif
     SkRecordMergeSvgOpacityAndFilterLayers(record);
 
     record->defrag();
diff --git a/src/core/SkRecordOpts.h b/src/core/SkRecordOpts.h
index d6531b5..f9db069 100644
--- a/src/core/SkRecordOpts.h
+++ b/src/core/SkRecordOpts.h
@@ -16,9 +16,11 @@
 // Turns logical no-op Save-[non-drawing command]*-Restore patterns into actual no-ops.
 void SkRecordNoopSaveRestores(SkRecord*);
 
+#ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK
 // For some SaveLayer-[drawing command]-Restore patterns, merge the SaveLayer's alpha into the
 // draw, and no-op the SaveLayer and Restore.
 void SkRecordNoopSaveLayerDrawRestores(SkRecord*);
+#endif
 
 // For SVG generated SaveLayer-Save-ClipRect-SaveLayer-3xRestore patterns, merge
 // the alpha of the first SaveLayer to the second SaveLayer.
diff --git a/tests/RecordOptsTest.cpp b/tests/RecordOptsTest.cpp
index f5f18f3..f0b4b83 100644
--- a/tests/RecordOptsTest.cpp
+++ b/tests/RecordOptsTest.cpp
@@ -8,6 +8,7 @@
 #include "Test.h"
 #include "RecordTestUtils.h"
 
+#include "SkBlurImageFilter.h"
 #include "SkColorFilter.h"
 #include "SkRecord.h"
 #include "SkRecordOpts.h"
@@ -100,6 +101,7 @@
     }
 }
 
+#ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK
 static void assert_savelayer_restore(skiatest::Reporter* r,
                                      SkRecord* record,
                                      int i,
@@ -128,7 +130,6 @@
     }
 }
 
-#include "SkBlurImageFilter.h"
 DEF_TEST(RecordOpts_NoopSaveLayerDrawRestore, r) {
     SkRecord record;
     SkRecorder recorder(&record, W, H);
@@ -194,6 +195,7 @@
     recorder.restore();
     assert_savelayer_draw_restore(r, &record, 18, false);
 }
+#endif
 
 static void assert_merge_svg_opacity_and_filter_layers(skiatest::Reporter* r,
                                                        SkRecord* record,