Merge "Revert "Rework CtsGpuProfilingDataTest"" into rvc-dev
diff --git a/hostsidetests/graphics/gpuprofiling/AndroidTest.xml b/hostsidetests/graphics/gpuprofiling/AndroidTest.xml
index bfaa33e..6dd7410 100644
--- a/hostsidetests/graphics/gpuprofiling/AndroidTest.xml
+++ b/hostsidetests/graphics/gpuprofiling/AndroidTest.xml
@@ -20,13 +20,9 @@
     <option name="config-descriptor:metadata" key="parameter" value="not_instant_app" />
     <option name="config-descriptor:metadata" key="parameter" value="multi_abi" />
     <option name="config-descriptor:metadata" key="parameter" value="secondary_user" />
-    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
-        <option name="cleanup-apks" value="true" />
-        <option name="test-file-name" value="CtsGraphicsProfilingDataApp.apk" />
-    </target_preparer>
     <target_preparer class="com.android.compatibility.common.tradefed.targetprep.FilePusher">
         <option name="cleanup" value="true" />
-        <option name="push" value="ctsgraphicsgpucountersinit->/data/local/tmp/ctsgraphicsgpucountersinit" />
+        <option name="push" value="ctsgraphicsgpuprofilinginit->/data/local/tmp/ctsgraphicsgpuprofilinginit" />
         <option name="append-bitness" value="true" />
     </target_preparer>
     <test class="com.android.compatibility.common.tradefed.testtype.JarHostTest" >
diff --git a/hostsidetests/graphics/gpuprofiling/app/Android.bp b/hostsidetests/graphics/gpuprofiling/app/Android.bp
index 896cffe..6c05ece 100644
--- a/hostsidetests/graphics/gpuprofiling/app/Android.bp
+++ b/hostsidetests/graphics/gpuprofiling/app/Android.bp
@@ -13,9 +13,9 @@
 // limitations under the License.
 
 cc_test {
-    name: "ctsgraphicsgpucountersinit",
+    name: "ctsgraphicsgpuprofilinginit",
     srcs: [
-        "android_graphics_cts_GpuCounters.cpp",
+        "android_graphics_cts_GpuProfilingData.cpp",
     ],
     test_suites: ["cts"],
     compile_multilib: "both",
@@ -34,43 +34,9 @@
     shared_libs: [
         "libdl",
         "libandroid",
+        "libvulkan",
         "liblog",
     ],
     stl: "c++_static",
     sdk_version: "current",
 }
-
-cc_test_library {
-    name: "libctsgraphicsgpuprofiling_jni",
-    gtest: false,
-    srcs: [
-        "jni/CtsGraphicsProfilingDataJniOnLoad.cpp",
-        "jni/android_graphics_cts_GpuRenderStages.cpp",
-    ],
-    cflags: [
-        "-Wall",
-        "-Werror",
-    ],
-    shared_libs: [
-        "libandroid",
-        "libvulkan",
-        "liblog",
-    ],
-    stl: "c++_shared",
-    sdk_version: "current",
-}
-
-android_test_helper_app {
-    name: "CtsGraphicsProfilingDataApp",
-    defaults: ["cts_support_defaults"],
-    srcs: ["src/**/*.java"],
-    sdk_version: "current",
-    // tag this module as a cts test artifact
-    test_suites: ["cts"],
-    compile_multilib: "both",
-    jni_libs: [
-        "libctsgraphicsgpuprofiling_jni",
-    ],
-    use_embedded_native_libs: false,
-    stl: "c++_shared",
-}
\ No newline at end of file
diff --git a/hostsidetests/graphics/gpuprofiling/app/AndroidManifest.xml b/hostsidetests/graphics/gpuprofiling/app/AndroidManifest.xml
deleted file mode 100644
index cc1a4db..0000000
--- a/hostsidetests/graphics/gpuprofiling/app/AndroidManifest.xml
+++ /dev/null
@@ -1,28 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-# Copyright (C) 2020 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.
- -->
-<manifest xmlns:android="http://schemas.android.com/apk/res/android"
-    package="android.graphics.gpuprofiling.app">
-    <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
-    <application android:extractNativeLibs="true" android:debuggable="true">
-        <activity android:name=".GpuRenderStagesDeviceActivity" android:exported="true">
-            <intent-filter>
-                <action android:name="android.intent.action.MAIN" />
-                <category android:name="android.intent.category.LAUNCHER" />
-            </intent-filter>
-        </activity>
-    </application>
-</manifest>
diff --git a/hostsidetests/graphics/gpuprofiling/app/android_graphics_cts_GpuCounters.cpp b/hostsidetests/graphics/gpuprofiling/app/android_graphics_cts_GpuCounters.cpp
deleted file mode 100644
index c710d4f..0000000
--- a/hostsidetests/graphics/gpuprofiling/app/android_graphics_cts_GpuCounters.cpp
+++ /dev/null
@@ -1,105 +0,0 @@
-/*
- * Copyright 2020 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.
- *
- */
-
-#define LOG_TAG "GpuProfilingData"
-
-#include <chrono>
-#include <csignal>
-#include <string>
-#include <thread>
-#include <unistd.h>
-#include <vector>
-
-#include <android/log.h>
-#include <dlfcn.h>
-
-#define ALOGI(msg, ...)                                                        \
-  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, (msg), __VA_ARGS__)
-#define ALOGE(msg, ...)                                                        \
-  __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, (msg), __VA_ARGS__)
-#define ALOGD(msg, ...)                                                        \
-  __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, (msg), __VA_ARGS__)
-#define REQUIRE_SUCCESS(fn, name)                                              \
-  do {                                                                         \
-    if (VK_SUCCESS != fn) {                                                    \
-      ALOGE("Vulkan Error in %s", name);                                       \
-      return -1;                                                               \
-    }                                                                          \
-  } while (0)
-
-namespace {
-
-typedef void (*FN_PTR)(void);
-
-/**
- * Load the vendor provided counter producer library.
- * startCounterProducer is a thin rewrite of the same producer loading logic in
- * github.com/google/agi
- */
-
-int startCounterProducer() {
-  ALOGI("%s", "Loading producer library");
-  char *error;
-  std::string libDir = sizeof(void *) == 8 ? "lib64" : "lib";
-  std::string producerPath = "/vendor/" + libDir + "/libgpudataproducer.so";
-
-  ALOGI("Trying %s", producerPath.c_str());
-  void *handle = dlopen(producerPath.c_str(), RTLD_GLOBAL);
-  if ((error = dlerror()) != nullptr || handle == nullptr) {
-    ALOGE("Error loading lib: %s", error);
-    return -1;
-  }
-
-  FN_PTR startFunc = (FN_PTR)dlsym(handle, "start");
-  if ((error = dlerror()) != nullptr) {
-    ALOGE("Error looking for start symbol: %s", error);
-    dlclose(handle);
-    return -1;
-  }
-
-  if (startFunc == nullptr) {
-    ALOGE("Did not find the producer library %s", producerPath.c_str());
-    ALOGE("LD_LIBRARY_PATH=%s", getenv("LD_LIBRARY_PATH"));
-    return -1;
-  }
-
-  ALOGI("Calling start at %p", startFunc);
-  (*startFunc)();
-  ALOGI("Producer %s has exited.", producerPath.c_str());
-  dlclose(handle);
-  return 0;
-}
-
-volatile std::sig_atomic_t done = 0;
-
-} // anonymous namespace
-
-int main() {
-  std::signal(SIGTERM, [](int /*signal*/) {
-    ALOGI("%s", "SIGTERM received");
-    done = 1;
-  });
-  std::thread dummy([&]() {
-    int result = startCounterProducer();
-    ALOGI("%s %d", "startCounterProducer returned", result);
-  });
-  ALOGI("%s", "Waiting for host");
-  while (!done) {
-    std::this_thread::sleep_for(std::chrono::milliseconds(100));
-  }
-  return 0;
-}
diff --git a/hostsidetests/graphics/gpuprofiling/app/jni/android_graphics_cts_GpuRenderStages.cpp b/hostsidetests/graphics/gpuprofiling/app/android_graphics_cts_GpuProfilingData.cpp
similarity index 70%
rename from hostsidetests/graphics/gpuprofiling/app/jni/android_graphics_cts_GpuRenderStages.cpp
rename to hostsidetests/graphics/gpuprofiling/app/android_graphics_cts_GpuProfilingData.cpp
index e0d49c0..19439c0 100644
--- a/hostsidetests/graphics/gpuprofiling/app/jni/android_graphics_cts_GpuRenderStages.cpp
+++ b/hostsidetests/graphics/gpuprofiling/app/android_graphics_cts_GpuProfilingData.cpp
@@ -17,12 +17,15 @@
 
 #define LOG_TAG "GpuProfilingData"
 
+#include <chrono>
+#include <csignal>
+#include <string>
+#include <thread>
+#include <unistd.h>
 #include <vector>
 
 #include <android/log.h>
 #include <dlfcn.h>
-#include <jni.h>
-#include <string>
 #include <vulkan/vulkan.h>
 
 #define ALOGI(msg, ...)                                                        \
@@ -43,7 +46,48 @@
 
 typedef void (*FN_PTR)(void);
 
-int initVulkan() {
+/**
+ * Load the vendor provided counter producer library.
+ * startCounterProducer is a thin rewrite of the same producer loading logic in
+ * github.com/google/agi
+ */
+
+int startCounterProducer() {
+  ALOGI("%s", "Loading producer library");
+  char *error;
+  std::string libDir = sizeof(void *) == 8 ? "lib64" : "lib";
+  std::string producerPath = "/vendor/" + libDir + "/libgpudataproducer.so";
+
+  ALOGI("Trying %s", producerPath.c_str());
+  void *handle = dlopen(producerPath.c_str(), RTLD_GLOBAL);
+  if ((error = dlerror()) != nullptr || handle == nullptr) {
+    ALOGE("Error loading lib: %s", error);
+    return -1;
+  }
+
+  FN_PTR startFunc = (FN_PTR)dlsym(handle, "start");
+  if ((error = dlerror()) != nullptr) {
+    ALOGE("Error looking for start symbol: %s", error);
+    dlclose(handle);
+    return -1;
+  }
+
+  if (startFunc == nullptr) {
+    ALOGE("Did not find the producer library %s", producerPath.c_str());
+    ALOGE("LD_LIBRARY_PATH=%s", getenv("LD_LIBRARY_PATH"));
+    return -1;
+  }
+
+  ALOGI("Calling start at %p", startFunc);
+  (*startFunc)();
+  ALOGI("Producer %s has exited.", producerPath.c_str());
+  dlclose(handle);
+  return 0;
+}
+
+int initVulkan(VkDevice &device) {
+  std::string result = "";
+
   const VkApplicationInfo appInfo = {
       VK_STRUCTURE_TYPE_APPLICATION_INFO,
       nullptr,            // pNext
@@ -127,27 +171,34 @@
       nullptr,
   };
 
-  VkDevice device;
-
   REQUIRE_SUCCESS(
       vkCreateDevice(physicalDevice, &deviceCreateInfo, nullptr, &device),
       "vkCreateDevice");
+
   return 0;
 }
 
-jint android_graphics_cts_GpuProfilingData_nativeInitVulkan(JNIEnv * /*env*/,
-                                                            jclass /*clazz*/) {
-  return (jint)initVulkan();
-}
+volatile std::sig_atomic_t done = 0;
 
-static JNINativeMethod gMethods[] = {
-    {"nativeInitVulkan", "()I",
-     (void *)android_graphics_cts_GpuProfilingData_nativeInitVulkan}};
 } // anonymous namespace
 
-int register_android_gputools_cts_GpuProfilingData(JNIEnv *env) {
-  jclass clazz = env->FindClass(
-      "android/graphics/gpuprofiling/app/GpuRenderStagesDeviceActivity");
-  return env->RegisterNatives(clazz, gMethods,
-                              sizeof(gMethods) / sizeof(JNINativeMethod));
-}
\ No newline at end of file
+int main() {
+  ALOGI("%s", "Creating Vulkan device");
+  VkDevice device;
+  std::signal(SIGTERM, [](int /*signal*/) {
+    ALOGI("%s", "SIGTERM received");
+    done = 1;
+  });
+  int result = initVulkan(device);
+  ALOGI("%s %d", "initVulkan returned", result);
+  std::thread dummy([&]() {
+    result = startCounterProducer();
+    ALOGI("%s %d", "startCounterProducer returned", result);
+  });
+  ALOGI("%s", "Waiting for host");
+  while (!done) {
+    std::this_thread::sleep_for(std::chrono::milliseconds(100));
+  }
+  vkDestroyDevice(device, nullptr);
+  return 0;
+}
diff --git a/hostsidetests/graphics/gpuprofiling/app/jni/CtsGraphicsProfilingDataJniOnLoad.cpp b/hostsidetests/graphics/gpuprofiling/app/jni/CtsGraphicsProfilingDataJniOnLoad.cpp
deleted file mode 100644
index 5948ff8..0000000
--- a/hostsidetests/graphics/gpuprofiling/app/jni/CtsGraphicsProfilingDataJniOnLoad.cpp
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright 2020 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 <jni.h>
-#include <stdio.h>
-
-extern int register_android_gputools_cts_GpuProfilingData(JNIEnv *);
-
-jint JNI_OnLoad(JavaVM *vm, void * /*reserved*/) {
-  JNIEnv *env = nullptr;
-  if (vm->GetEnv((void **)&env, JNI_VERSION_1_4) != JNI_OK)
-    return JNI_ERR;
-  if (register_android_gputools_cts_GpuProfilingData(env))
-    return JNI_ERR;
-  return JNI_VERSION_1_4;
-}
\ No newline at end of file
diff --git a/hostsidetests/graphics/gpuprofiling/app/src/android/gpuprofiling/GpuRenderStagesDeviceActivity.java b/hostsidetests/graphics/gpuprofiling/app/src/android/gpuprofiling/GpuRenderStagesDeviceActivity.java
deleted file mode 100644
index e6bd660..0000000
--- a/hostsidetests/graphics/gpuprofiling/app/src/android/gpuprofiling/GpuRenderStagesDeviceActivity.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Copyright (C) 2020 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.
- */
-
-package android.graphics.gpuprofiling.app;
-
-import android.app.Activity;
-import android.os.Bundle;
-import android.util.Log;
-
-import java.lang.Override;
-
-public class GpuRenderStagesDeviceActivity extends Activity {
-
-    static {
-        System.loadLibrary("ctsgraphicsgpuprofiling_jni");
-    }
-
-    private static final String TAG = GpuRenderStagesDeviceActivity.class.getSimpleName();
-
-    @Override
-    public void onCreate(Bundle icicle) {
-        super.onCreate(icicle);
-        int result = nativeInitVulkan();
-        Log.i(TAG, "nativeInitVulkan returned: " + result);
-        Log.i(TAG, "GpuProfilingData activity complete");
-    }
-
-    private static native int nativeInitVulkan();
-}
diff --git a/hostsidetests/graphics/gpuprofiling/src/android/graphics/gpuprofiling/cts/CtsGpuProfilingDataTest.java b/hostsidetests/graphics/gpuprofiling/src/android/graphics/gpuprofiling/cts/CtsGpuProfilingDataTest.java
index f5654cd..ff33375 100644
--- a/hostsidetests/graphics/gpuprofiling/src/android/graphics/gpuprofiling/cts/CtsGpuProfilingDataTest.java
+++ b/hostsidetests/graphics/gpuprofiling/src/android/graphics/gpuprofiling/cts/CtsGpuProfilingDataTest.java
@@ -17,7 +17,6 @@
 package android.graphics.gpuprofiling.cts;
 
 import com.android.tradefed.device.ITestDevice;
-import com.android.tradefed.log.LogUtil.CLog;
 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
 import com.android.tradefed.util.CommandResult;
@@ -47,16 +46,11 @@
     // Positive tests
     // - Ensure the perfetto producers for render stages, counters, and ftrace gpu frequency are available
 
-    private static final String BIN_NAME = "ctsgraphicsgpucountersinit";
+    private static final String BIN_NAME = "ctsgraphicsgpuprofilinginit";
     private static final String DEVICE_BIN_PATH = "/data/local/tmp/" + BIN_NAME;
-    private static final String APP = "android.graphics.gpuprofiling.app";
-    private static final String APK = "CtsGraphicsProfilingDataApp.apk";
-    private static final String ACTIVITY = "GpuRenderStagesDeviceActivity";
     private static final String COUNTERS_SOURCE_NAME = "gpu.counters";
     private static final String STAGES_SOURCE_NAME = "gpu.renderstages";
-    private static final String PROFILING_PROPERTY = "graphics.gpu.profiler.support";
-    private static final String LAYER_PACKAGE_PROPERTY = "graphics.gpu.profiler.vulkan_layer_apk";
-    private static final String LAYER_NAME = "VkRenderStagesProducer";
+    private static final String PROFILING_PROPERTY = "ro.hardware.gpu.profiler.support";
     private static int MAX_RETRIES = 5;
 
     private class ShellThread extends Thread {
@@ -71,40 +65,28 @@
         @Override
         public void run() {
             try {
-                getDevice().executeShellV2Command(mCmd);
+                CommandResult activityStatus = getDevice().executeShellV2Command(mCmd);
             } catch (Exception e) {
-                CLog.e("Failed to start counters producer" + e.getMessage());
+                // TODO Do something here?
             }
         }
     }
 
     /**
-     * Kill the native process and remove the layer related settings after each test
+     * Kill the native process after each test
      */
     @After
     public void cleanup() throws Exception {
+        // TODO figure out how to unregister the producers
         getDevice().executeShellV2Command("killall " + BIN_NAME);
-        getDevice().executeShellV2Command("am force-stop " + APP);
-        getDevice().executeShellV2Command("settings delete global gpu_debug_layers");
-        getDevice().executeShellV2Command("settings delete global enable_gpu_debug_layers");
-        getDevice().executeShellV2Command("settings delete global gpu_debug_app");
-        getDevice().executeShellV2Command("settings delete global gpu_debug_layer_app");
     }
 
     /**
-     * Clean up before starting any tests. Apply the necessary layer settings if we need them
+     * Clean up before starting any tests.
      */
     @Before
     public void init() throws Exception {
         cleanup();
-        String layerApp = getDevice().getProperty(LAYER_PACKAGE_PROPERTY);
-        if (layerApp != null && !layerApp.isEmpty()) {
-            getDevice().executeShellV2Command("settings put global enable_gpu_debug_layers 1");
-            getDevice().executeShellV2Command("settings put global gpu_debug_app " + APP);
-            getDevice().executeShellV2Command("settings put global gpu_debug_layer_app " + layerApp);
-            getDevice().executeShellV2Command("settings put global gpu_debug_layers " + LAYER_NAME);
-        }
-        installPackage(APK);
     }
 
     /**
@@ -118,7 +100,6 @@
             // Spin up a new thread to avoid blocking the main thread while the native process waits to be killed.
             ShellThread shellThread = new ShellThread(DEVICE_BIN_PATH);
             shellThread.start();
-            CommandResult activityStatus = getDevice().executeShellV2Command("am start -n " + APP + "/." + ACTIVITY);
             boolean countersSourceFound = false;
             boolean stagesSourceFound = false;
             for(int i = 0; i < MAX_RETRIES; i++) {