Read system properties to disable Swappy during initialization. am: 3a3a38c5c0 am: 8ce87f0a7c
am: 52f28e6cda

Change-Id: I60494ba2f577127f1ae27ae94acb8b7ef04c86fb
diff --git a/samples/bouncyball/app/build.gradle b/samples/bouncyball/app/build.gradle
index b059b87..b4c5193 100644
--- a/samples/bouncyball/app/build.gradle
+++ b/samples/bouncyball/app/build.gradle
@@ -16,9 +16,17 @@
         }
     }
     buildTypes {
+        debug {
+            ndk {
+                abiFilters "arm64-v8a", "armeabi-v7a"
+            }
+        }
         release {
             minifyEnabled false
             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
+            ndk {
+                abiFilters "arm64-v8a", "armeabi-v7a"
+            }
         }
     }
     externalNativeBuild {
diff --git a/src/swappy/CMakeLists.txt b/src/swappy/CMakeLists.txt
index 4d58cab..54a5ab6 100644
--- a/src/swappy/CMakeLists.txt
+++ b/src/swappy/CMakeLists.txt
@@ -24,6 +24,7 @@
              ${SOURCE_LOCATION}/EGL.cpp
              ${SOURCE_LOCATION}/FrameStatistics.cpp
              ${SOURCE_LOCATION}/CpuInfo.cpp
+             ${SOURCE_LOCATION}/SystemProperties.cpp
              ${SOURCE_LOCATION}/Swappy.cpp
              ${SOURCE_LOCATION}/Settings.cpp
              ${SOURCE_LOCATION}/Thread.cpp
diff --git a/src/swappy/Swappy.cpp b/src/swappy/Swappy.cpp
index 204deee..f24c0a5 100644
--- a/src/swappy/Swappy.cpp
+++ b/src/swappy/Swappy.cpp
@@ -28,6 +28,7 @@
 #include "ChoreographerThread.h"
 #include "EGL.h"
 #include "FrameStatistics.h"
+#include "SystemProperties.h"
 
 // uncomment below line to enable ALOGV messages
 //#define SWAPPY_DEBUG
@@ -143,7 +144,11 @@
         return EGL_FALSE;
     }
 
-    return swappy->swapInternal(display, surface);
+    if (swappy->enabled()) {
+        return swappy->swapInternal(display, surface);
+    } else {
+        return eglSwapBuffers(display, surface) == EGL_TRUE;
+    }
 }
 
 bool Swappy::swapInternal(EGLDisplay display, EGLSurface surface) {
@@ -246,6 +251,10 @@
             return;
     }
 
+    if (!swappy->enabled()) {
+        return;
+    }
+
     if (!swappy->getEgl()->statsSupported()) {
         ALOGI("stats are not suppored on this platform");
         return;
@@ -367,22 +376,30 @@
     : mRefreshPeriod(refreshPeriod),
       mFrameStatistics(nullptr),
       mSfOffset(sfOffset),
-      mChoreographerFilter(std::make_unique<ChoreographerFilter>(refreshPeriod,
-                                                                 sfOffset - appOffset,
-                                                                 [this]() { return wakeClient(); })),
-      mChoreographerThread(ChoreographerThread::createChoreographerThread(
-              ChoreographerThread::Type::Swappy,
-              vm,
-              [this]{ handleChoreographer(); })),
       mSwapDuration(std::chrono::nanoseconds(0)),
       mSwapInterval(1),
       mAutoSwapInterval(1)
 {
+    mDisableSwappy = getSystemPropViaGetAsBool("swappy.disable", false);
+    if (!enabled()) {
+        ALOGI("Swappy is disabled");
+        return;
+    }
+
+    mChoreographerFilter = std::make_unique<ChoreographerFilter>(refreshPeriod,
+                                                               sfOffset - appOffset,
+                                                               [this]() { return wakeClient(); });
+
+    mChoreographerThread = ChoreographerThread::createChoreographerThread(
+                    ChoreographerThread::Type::Swappy,
+                    vm,
+                    [this]{ handleChoreographer(); });
 
     Settings::getInstance()->addListener([this]() { onSettingsChanged(); });
 
     ALOGI("Initialized Swappy with refreshPeriod=%lld, appOffset=%lld, sfOffset=%lld",
           refreshPeriod.count(), appOffset.count(), sfOffset.count());
+
     std::lock_guard<std::mutex> lock(mEglMutex);
     mEgl = EGL::create(refreshPeriod);
     if (!mEgl) {
diff --git a/src/swappy/Swappy.h b/src/swappy/Swappy.h
index a902822..eedea51 100644
--- a/src/swappy/Swappy.h
+++ b/src/swappy/Swappy.h
@@ -134,6 +134,8 @@
 
     static Swappy *getInstance();
 
+    bool enabled() const { return !mDisableSwappy; }
+
     EGL *getEgl();
 
     bool swapInternal(EGLDisplay display, EGLSurface surface);
@@ -184,6 +186,8 @@
                       const std::chrono::nanoseconds& lowerBound,
                       const int32_t& newSwapInterval) REQUIRES(mFrameDurationsMutex);
 
+    bool mDisableSwappy = false;
+
     int32_t nanoToSwapInterval(std::chrono::nanoseconds);
 
     std::atomic<std::chrono::nanoseconds> mSwapDuration;
diff --git a/src/swappy/SystemProperties.cpp b/src/swappy/SystemProperties.cpp
new file mode 100644
index 0000000..95aa7d3
--- /dev/null
+++ b/src/swappy/SystemProperties.cpp
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string>
+
+#include "Log.h"
+
+#include <sys/system_properties.h>
+
+#define LOG_TAG "SysProp"
+
+std::string getSystemPropViaGet(const char* key, std::string default_value = "") {
+    char buffer[PROP_VALUE_MAX + 1];
+    int bufferLen = __system_property_get(key, buffer);
+    if (bufferLen > PROP_VALUE_MAX || bufferLen == 0) {
+        return default_value;
+    }
+    return std::string(buffer, bufferLen);
+}
+
+int getSystemPropViaGetAsInt(const char* key, int default_value = 0) {
+    char buffer[PROP_VALUE_MAX + 1];
+    int bufferLen = __system_property_get(key, buffer);
+    if (bufferLen > PROP_VALUE_MAX || bufferLen == 0) {
+        return default_value;
+    }
+    return atoi(buffer);
+}
+
+int getSystemPropViaGetAsBool(const char* key, bool default_value = false) {
+    return getSystemPropViaGetAsInt(key, default_value) != 0;
+}
diff --git a/src/swappy/SystemProperties.h b/src/swappy/SystemProperties.h
new file mode 100644
index 0000000..2a9a61e
--- /dev/null
+++ b/src/swappy/SystemProperties.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <string>
+
+//
+// To set system properties:
+// adb shell setprop swappy.property value
+//
+
+std::string getSystemPropViaGet(const char* key, std::string default_value = "");
+
+int getSystemPropViaGetAsInt(const char* key, int default_value = 0);
+
+int getSystemPropViaGetAsBool(const char* key, bool default_value = false);
+