Support dump gfxinfo reset for process' without a Window

Bug: 199791928
Test: manual
Change-Id: I995d236c2c9d60194f72275a3da1c82340f77898
diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java
index f1fd8b7..6353977 100644
--- a/core/java/android/app/ActivityThread.java
+++ b/core/java/android/app/ActivityThread.java
@@ -4578,8 +4578,7 @@
 
     private void handleDumpGfxInfo(DumpComponentInfo info) {
         try {
-            nDumpGraphicsInfo(info.fd.getFileDescriptor());
-            WindowManagerGlobal.getInstance().dumpGfxInfo(info.fd.getFileDescriptor(), info.args);
+            ThreadedRenderer.handleDumpGfxInfo(info.fd.getFileDescriptor(), info.args);
         } catch (Exception e) {
             Log.w(TAG, "Caught exception from dumpGfxInfo()", e);
         } finally {
@@ -7905,6 +7904,5 @@
 
     // ------------------ Regular JNI ------------------------
     private native void nPurgePendingResources();
-    private native void nDumpGraphicsInfo(FileDescriptor fd);
     private native void nInitZygoteChildHeapProfiling();
 }
diff --git a/core/java/android/view/ThreadedRenderer.java b/core/java/android/view/ThreadedRenderer.java
index bee0709..7af77ca 100644
--- a/core/java/android/view/ThreadedRenderer.java
+++ b/core/java/android/view/ThreadedRenderer.java
@@ -609,11 +609,7 @@
         return mHeight;
     }
 
-    /**
-     * Outputs extra debugging information in the specified file descriptor.
-     */
-    void dumpGfxInfo(PrintWriter pw, FileDescriptor fd, String[] args) {
-        pw.flush();
+    private static int dumpArgsToFlags(String[] args) {
         // If there's no arguments, eg 'dumpsys gfxinfo', then dump everything.
         // If there's a targetted package, eg 'dumpsys gfxinfo com.android.systemui', then only
         // dump the summary information
@@ -631,7 +627,21 @@
                     break;
             }
         }
-        dumpProfileInfo(fd, flags);
+        return flags;
+    }
+
+    /** @hide */
+    public static void handleDumpGfxInfo(FileDescriptor fd, String[] args) {
+        dumpGlobalProfileInfo(fd, dumpArgsToFlags(args));
+        WindowManagerGlobal.getInstance().dumpGfxInfo(fd, args);
+    }
+
+    /**
+     * Outputs extra debugging information in the specified file descriptor.
+     */
+    void dumpGfxInfo(PrintWriter pw, FileDescriptor fd, String[] args) {
+        pw.flush();
+        dumpProfileInfo(fd, dumpArgsToFlags(args));
     }
 
     Picture captureRenderingCommands() {
diff --git a/core/jni/android_app_ActivityThread.cpp b/core/jni/android_app_ActivityThread.cpp
index e9d9a20..e25ba76 100644
--- a/core/jni/android_app_ActivityThread.cpp
+++ b/core/jni/android_app_ActivityThread.cpp
@@ -21,7 +21,6 @@
 #include <unistd.h>
 
 #include <bionic/malloc.h>
-#include <android/graphics/renderthread.h>
 
 namespace android {
 
@@ -30,12 +29,6 @@
     mallopt(M_PURGE, 0);
 }
 
-static void
-android_app_ActivityThread_dumpGraphics(JNIEnv* env, jobject clazz, jobject javaFileDescriptor) {
-    int fd = jniGetFDFromFileDescriptor(env, javaFileDescriptor);
-    ARenderThread_dumpGraphicsMemory(fd);
-}
-
 static void android_app_ActivityThread_initZygoteChildHeapProfiling(JNIEnv* env, jobject clazz) {
     android_mallopt(M_INIT_ZYGOTE_CHILD_PROFILING, nullptr, 0);
 }
@@ -44,8 +37,6 @@
     // ------------ Regular JNI ------------------
     { "nPurgePendingResources",        "()V",
       (void*) android_app_ActivityThread_purgePendingResources },
-    { "nDumpGraphicsInfo",        "(Ljava/io/FileDescriptor;)V",
-      (void*) android_app_ActivityThread_dumpGraphics },
     { "nInitZygoteChildHeapProfiling",        "()V",
       (void*) android_app_ActivityThread_initZygoteChildHeapProfiling }
 };
diff --git a/graphics/java/android/graphics/HardwareRenderer.java b/graphics/java/android/graphics/HardwareRenderer.java
index abdf1a2..2b18350 100644
--- a/graphics/java/android/graphics/HardwareRenderer.java
+++ b/graphics/java/android/graphics/HardwareRenderer.java
@@ -823,6 +823,13 @@
     /**
      * @hide
      */
+    public static void dumpGlobalProfileInfo(FileDescriptor fd, @DumpFlags int dumpFlags) {
+        nDumpGlobalProfileInfo(fd, dumpFlags);
+    }
+
+    /**
+     * @hide
+     */
     public void dumpProfileInfo(FileDescriptor fd, @DumpFlags int dumpFlags) {
         nDumpProfileInfo(mNativeProxy, fd, dumpFlags);
     }
@@ -1390,6 +1397,8 @@
     private static native void nDumpProfileInfo(long nativeProxy, FileDescriptor fd,
             @DumpFlags int dumpFlags);
 
+    private static native void nDumpGlobalProfileInfo(FileDescriptor fd, @DumpFlags int dumpFlags);
+
     private static native void nAddRenderNode(long nativeProxy, long rootRenderNode,
             boolean placeFront);
 
diff --git a/libs/hwui/Android.bp b/libs/hwui/Android.bp
index b931d03..fea1e15 100644
--- a/libs/hwui/Android.bp
+++ b/libs/hwui/Android.bp
@@ -272,7 +272,6 @@
                 "apex/android_bitmap.cpp",
                 "apex/android_canvas.cpp",
                 "apex/jni_runtime.cpp",
-                "apex/renderthread.cpp",
             ],
         },
         host: {
diff --git a/libs/hwui/apex/include/android/graphics/renderthread.h b/libs/hwui/apex/include/android/graphics/renderthread.h
deleted file mode 100644
index 50280a6..0000000
--- a/libs/hwui/apex/include/android/graphics/renderthread.h
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright (C) 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.
- */
-#ifndef ANDROID_GRAPHICS_RENDERTHREAD_H
-#define ANDROID_GRAPHICS_RENDERTHREAD_H
-
-#include <cutils/compiler.h>
-#include <sys/cdefs.h>
-
-__BEGIN_DECLS
-
-/**
- * Dumps a textual representation of the graphics stats for this process.
- * @param fd The file descriptor that the available graphics stats will be appended to.  The
- *           function requires a valid fd, but does not persist or assume ownership of the fd
- *           outside the scope of this function.
- */
-ANDROID_API void ARenderThread_dumpGraphicsMemory(int fd);
-
-__END_DECLS
-
-#endif // ANDROID_GRAPHICS_RENDERTHREAD_H
diff --git a/libs/hwui/apex/renderthread.cpp b/libs/hwui/apex/renderthread.cpp
deleted file mode 100644
index 5d26afe..0000000
--- a/libs/hwui/apex/renderthread.cpp
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Copyright (C) 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 "android/graphics/renderthread.h"
-
-#include <renderthread/RenderProxy.h>
-
-using namespace android;
-
-void ARenderThread_dumpGraphicsMemory(int fd) {
-    uirenderer::renderthread::RenderProxy::dumpGraphicsMemory(fd);
-}
diff --git a/libs/hwui/jni/android_graphics_HardwareRenderer.cpp b/libs/hwui/jni/android_graphics_HardwareRenderer.cpp
index e536950..bd93a4f 100644
--- a/libs/hwui/jni/android_graphics_HardwareRenderer.cpp
+++ b/libs/hwui/jni/android_graphics_HardwareRenderer.cpp
@@ -379,6 +379,13 @@
     proxy->dumpProfileInfo(fd, dumpFlags);
 }
 
+static void android_view_ThreadedRenderer_dumpGlobalProfileInfo(JNIEnv* env, jobject clazz,
+                                                                jobject javaFileDescriptor,
+                                                                jint dumpFlags) {
+    int fd = jniGetFDFromFileDescriptor(env, javaFileDescriptor);
+    RenderProxy::dumpGraphicsMemory(fd, true, dumpFlags & DumpFlags::Reset);
+}
+
 static void android_view_ThreadedRenderer_addRenderNode(JNIEnv* env, jobject clazz,
         jlong proxyPtr, jlong renderNodePtr, jboolean placeFront) {
     RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
@@ -910,6 +917,8 @@
         {"nNotifyFramePending", "(J)V", (void*)android_view_ThreadedRenderer_notifyFramePending},
         {"nDumpProfileInfo", "(JLjava/io/FileDescriptor;I)V",
          (void*)android_view_ThreadedRenderer_dumpProfileInfo},
+        {"nDumpGlobalProfileInfo", "(Ljava/io/FileDescriptor;I)V",
+         (void*)android_view_ThreadedRenderer_dumpGlobalProfileInfo},
         {"setupShadersDiskCache", "(Ljava/lang/String;Ljava/lang/String;)V",
          (void*)android_view_ThreadedRenderer_setupShadersDiskCache},
         {"nAddRenderNode", "(JJZ)V", (void*)android_view_ThreadedRenderer_addRenderNode},
diff --git a/libs/hwui/libhwui.map.txt b/libs/hwui/libhwui.map.txt
index 77b8a44..087c006 100644
--- a/libs/hwui/libhwui.map.txt
+++ b/libs/hwui/libhwui.map.txt
@@ -1,4 +1,4 @@
-LIBHWUI {
+LIBHWUI { # platform-only /* HWUI isn't current a module, so all of these are still platform-only */
   global:
     /* listing of all C APIs to be exposed by libhwui to consumers outside of the module */
     ABitmap_getInfoFromJava;
@@ -39,7 +39,6 @@
     ARegionIterator_next;
     ARegionIterator_getRect;
     ARegionIterator_getTotalBounds;
-    ARenderThread_dumpGraphicsMemory;
   local:
     *;
 };
diff --git a/libs/hwui/renderthread/RenderProxy.cpp b/libs/hwui/renderthread/RenderProxy.cpp
index 72d4ac5..430c4d3 100644
--- a/libs/hwui/renderthread/RenderProxy.cpp
+++ b/libs/hwui/renderthread/RenderProxy.cpp
@@ -257,10 +257,15 @@
     });
 }
 
-void RenderProxy::dumpGraphicsMemory(int fd, bool includeProfileData) {
+void RenderProxy::dumpGraphicsMemory(int fd, bool includeProfileData, bool resetProfile) {
     if (RenderThread::hasInstance()) {
         auto& thread = RenderThread::getInstance();
-        thread.queue().runSync([&]() { thread.dumpGraphicsMemory(fd, includeProfileData); });
+        thread.queue().runSync([&]() {
+            thread.dumpGraphicsMemory(fd, includeProfileData);
+            if (resetProfile) {
+                thread.globalProfileData()->reset();
+            }
+        });
     }
 }
 
diff --git a/libs/hwui/renderthread/RenderProxy.h b/libs/hwui/renderthread/RenderProxy.h
index 6417b38..6d46be4 100644
--- a/libs/hwui/renderthread/RenderProxy.h
+++ b/libs/hwui/renderthread/RenderProxy.h
@@ -108,7 +108,8 @@
     // Not exported, only used for testing
     void resetProfileInfo();
     uint32_t frameTimePercentile(int p);
-    static void dumpGraphicsMemory(int fd, bool includeProfileData = true);
+    static void dumpGraphicsMemory(int fd, bool includeProfileData = true,
+                                   bool resetProfile = false);
     static void getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage);
 
     static void rotateProcessStatsBuffer();