Add PerfHUD ES profiling capabilities

The eglGetSystemTimeNV extension can be used to enable profiling
in PerfHUD ES. When the delta of two calls to eglGetSystemTimeNV
equals 0, we now cancels display lists updates. This allows the
tool to redraw the same frame several times in a row to run its
analysis.

For better results profiling should only be attempted after
setting viewroot.profile_rendering to true using adb shell
setprop.

Change-Id: I02e3c237418004cff8d6cb0b9a37126efae44c90
diff --git a/core/java/android/view/HardwareRenderer.java b/core/java/android/view/HardwareRenderer.java
index e8c1653..8dada90 100644
--- a/core/java/android/view/HardwareRenderer.java
+++ b/core/java/android/view/HardwareRenderer.java
@@ -406,6 +406,17 @@
     private static native void nBeginFrame(int[] size);
 
     /**
+     * Returns the current system time according to the renderer.
+     * This method is used for debugging only and should not be used
+     * as a clock.
+     */
+    static long getSystemTime() {
+        return nGetSystemTime();
+    }
+
+    private static native long nGetSystemTime();
+
+    /**
      * Preserves the back buffer of the current surface after a buffer swap.
      * Calling this method sets the EGL_SWAP_BEHAVIOR attribute of the current
      * surface to EGL_BUFFER_PRESERVED. Calling this method requires an EGL
@@ -852,6 +863,8 @@
         private final int[] mSurfaceSize = new int[2];
         private final FunctorsRunnable mFunctorsRunnable = new FunctorsRunnable();
 
+        private long mDrawDelta = Long.MAX_VALUE;
+
         GlRenderer(int glVersion, boolean translucent) {
             mGlVersion = glVersion;
             mTranslucent = translucent;
@@ -1413,6 +1426,7 @@
                     int saveCount = 0;
                     int status = DisplayList.STATUS_DONE;
 
+                    long start = getSystemTime();
                     try {
                         status = prepareFrame(dirty);
 
@@ -1432,12 +1446,15 @@
                         canvas.restoreToCount(saveCount);
                         view.mRecreateDisplayList = false;
 
-                        debugOverdraw(attachInfo, dirty, canvas, displayList);
+                        mDrawDelta = getSystemTime() - start;
 
-                        mFrameCount++;
+                        if (mDrawDelta > 0) {
+                            mFrameCount++;
 
-                        debugDirtyRegions(dirty, canvas);
-                        drawProfileData(attachInfo);
+                            debugOverdraw(attachInfo, dirty, canvas, displayList);
+                            debugDirtyRegions(dirty, canvas);
+                            drawProfileData(attachInfo);
+                        }
                     }
 
                     onPostDraw();
@@ -1509,6 +1526,10 @@
         }
 
         private DisplayList buildDisplayList(View view, HardwareCanvas canvas) {
+            if (mDrawDelta <= 0) {
+                return view.mDisplayList;
+            }
+
             view.mRecreateDisplayList = (view.mPrivateFlags & View.PFLAG_INVALIDATED)
                     == View.PFLAG_INVALIDATED;
             view.mPrivateFlags &= ~View.PFLAG_INVALIDATED;
diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java
index fc005d1..8462c80 100644
--- a/core/java/android/view/ViewRootImpl.java
+++ b/core/java/android/view/ViewRootImpl.java
@@ -111,7 +111,7 @@
      * Set this system property to true to force the view hierarchy to render
      * at 60 Hz. This can be used to measure the potential framerate.
      */
-    private static final String PROPERTY_PROFILE_RENDERING = "viewancestor.profile_rendering";
+    private static final String PROPERTY_PROFILE_RENDERING = "viewroot.profile_rendering";
     private static final String PROPERTY_MEDIA_DISABLED = "config.disable_media";
 
     /**
diff --git a/core/jni/android_view_HardwareRenderer.cpp b/core/jni/android_view_HardwareRenderer.cpp
index 948a966..479fbe2 100644
--- a/core/jni/android_view_HardwareRenderer.cpp
+++ b/core/jni/android_view_HardwareRenderer.cpp
@@ -20,9 +20,14 @@
 #include <nativehelper/JNIHelp.h>
 #include <android_runtime/AndroidRuntime.h>
 
+#include <EGL/egl.h>
+#include <EGL/eglext.h>
 #include <EGL/egl_cache.h>
 
+#include <utils/Timers.h>
+
 #include <Caches.h>
+#include <Extensions.h>
 
 #ifdef USE_OPENGL_RENDERER
     EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface);
@@ -119,6 +124,13 @@
     eglBeginFrame(display, surface);
 }
 
+static jlong android_view_HardwareRenderer_getSystemTime(JNIEnv* env, jobject clazz) {
+    if (uirenderer::Extensions::getInstance().hasNvSystemTime()) {
+        return eglGetSystemTimeNV();
+    }
+    return systemTime(SYSTEM_TIME_MONOTONIC);
+}
+
 #endif // USE_OPENGL_RENDERER
 
 // ----------------------------------------------------------------------------
@@ -146,6 +158,8 @@
     { "nLoadProperties",        "()Z",   (void*) android_view_HardwareRenderer_loadProperties },
 
     { "nBeginFrame",            "([I)V", (void*) android_view_HardwareRenderer_beginFrame },
+
+    { "nGetSystemTime",         "()J",   (void*) android_view_HardwareRenderer_getSystemTime },
 #endif
 
     { "nSetupShadersDiskCache", "(Ljava/lang/String;)V",
diff --git a/libs/hwui/Extensions.cpp b/libs/hwui/Extensions.cpp
index 51aec8d..eefdb84 100644
--- a/libs/hwui/Extensions.cpp
+++ b/libs/hwui/Extensions.cpp
@@ -14,6 +14,16 @@
  * limitations under the License.
  */
 
+#define LOG_TAG "OpenGLRenderer"
+
+#include <GLES2/gl2.h>
+#include <GLES2/gl2ext.h>
+
+#include <EGL/egl.h>
+#include <EGL/eglext.h>
+
+#include <utils/Log.h>
+
 #include "Debug.h"
 #include "Extensions.h"
 
@@ -40,33 +50,22 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 Extensions::Extensions(): Singleton<Extensions>() {
-    const char* buffer = (const char*) glGetString(GL_EXTENSIONS);
-    const char* current = buffer;
-    const char* head = current;
-    EXT_LOGD("Available GL extensions:");
-    do {
-        head = strchr(current, ' ');
-        String8 s(current, head ? head - current : strlen(current));
-        if (s.length()) {
-            mExtensionList.add(s);
-            EXT_LOGD("  %s", s.string());
-        }
-        current = head + 1;
-    } while (head);
+    // Query GL extensions
+    findExtensions((const char*) glGetString(GL_EXTENSIONS), mGlExtensionList);
+    mHasNPot = hasGlExtension("GL_OES_texture_npot");
+    mHasFramebufferFetch = hasGlExtension("GL_NV_shader_framebuffer_fetch");
+    mHasDiscardFramebuffer = hasGlExtension("GL_EXT_discard_framebuffer");
+    mHasDebugMarker = hasGlExtension("GL_EXT_debug_marker");
+    mHasDebugLabel = hasGlExtension("GL_EXT_debug_label");
+    mHasTiledRendering = hasGlExtension("GL_QCOM_tiled_rendering");
+    mHas1BitStencil = hasGlExtension("GL_OES_stencil1");
+    mHas4BitStencil = hasGlExtension("GL_OES_stencil4");
 
-    mHasNPot = hasExtension("GL_OES_texture_npot");
-    mHasFramebufferFetch = hasExtension("GL_NV_shader_framebuffer_fetch");
-    mHasDiscardFramebuffer = hasExtension("GL_EXT_discard_framebuffer");
-    mHasDebugMarker = hasExtension("GL_EXT_debug_marker");
-    mHasDebugLabel = hasExtension("GL_EXT_debug_label");
-    mHasTiledRendering = hasExtension("GL_QCOM_tiled_rendering");
-    mHas1BitStencil = hasExtension("GL_OES_stencil1");
-    mHas4BitStencil = hasExtension("GL_OES_stencil4");
-
-    mExtensions = strdup(buffer);
+    // Query EGL extensions
+    findExtensions(eglQueryString(eglGetCurrentDisplay(), EGL_EXTENSIONS), mEglExtensionList);
+    mHasNvSystemTime = hasEglExtension("EGL_NV_system_time");
 
     const char* version = (const char*) glGetString(GL_VERSION);
-    mVersion = strdup(version);
 
     // Section 6.1.5 of the OpenGL ES specification indicates the GL version
     // string strictly follows this format:
@@ -88,22 +87,41 @@
 }
 
 Extensions::~Extensions() {
-   free(mExtensions);
-   free(mVersion);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
 // Methods
 ///////////////////////////////////////////////////////////////////////////////
 
-bool Extensions::hasExtension(const char* extension) const {
+bool Extensions::hasGlExtension(const char* extension) const {
    const String8 s(extension);
-   return mExtensionList.indexOf(s) >= 0;
+   return mGlExtensionList.indexOf(s) >= 0;
+}
+
+bool Extensions::hasEglExtension(const char* extension) const {
+   const String8 s(extension);
+   return mEglExtensionList.indexOf(s) >= 0;
+}
+
+void Extensions::findExtensions(const char* extensions, SortedVector<String8>& list) const {
+    const char* current = extensions;
+    const char* head = current;
+    EXT_LOGD("Available extensions:");
+    do {
+        head = strchr(current, ' ');
+        String8 s(current, head ? head - current : strlen(current));
+        if (s.length()) {
+            list.add(s);
+            EXT_LOGD("  %s", s.string());
+        }
+        current = head + 1;
+    } while (head);
 }
 
 void Extensions::dump() const {
-   ALOGD("%s", mVersion);
-   ALOGD("Supported extensions:\n%s", mExtensions);
+   ALOGD("%s", (const char*) glGetString(GL_VERSION));
+   ALOGD("Supported GL extensions:\n%s", (const char*) glGetString(GL_EXTENSIONS));
+   ALOGD("Supported EGL extensions:\n%s", eglQueryString(eglGetCurrentDisplay(), EGL_EXTENSIONS));
 }
 
 }; // namespace uirenderer
diff --git a/libs/hwui/Extensions.h b/libs/hwui/Extensions.h
index a3f7c44..3112244 100644
--- a/libs/hwui/Extensions.h
+++ b/libs/hwui/Extensions.h
@@ -17,13 +17,12 @@
 #ifndef ANDROID_HWUI_EXTENSIONS_H
 #define ANDROID_HWUI_EXTENSIONS_H
 
+#include <cutils/compiler.h>
+
 #include <utils/Singleton.h>
 #include <utils/SortedVector.h>
 #include <utils/String8.h>
 
-#include <GLES2/gl2.h>
-#include <GLES2/gl2ext.h>
-
 namespace android {
 namespace uirenderer {
 
@@ -31,7 +30,7 @@
 // Classes
 ///////////////////////////////////////////////////////////////////////////////
 
-class Extensions: public Singleton<Extensions> {
+class ANDROID_API Extensions: public Singleton<Extensions> {
 public:
     inline bool hasNPot() const { return mHasNPot; }
     inline bool hasFramebufferFetch() const { return mHasFramebufferFetch; }
@@ -41,11 +40,13 @@
     inline bool hasTiledRendering() const { return mHasTiledRendering; }
     inline bool has1BitStencil() const { return mHas1BitStencil; }
     inline bool has4BitStencil() const { return mHas4BitStencil; }
+    inline bool hasNvSystemTime() const { return mHasNvSystemTime; }
 
     inline int getMajorGlVersion() const { return mVersionMajor; }
     inline int getMinorGlVersion() const { return mVersionMinor; }
 
-    bool hasExtension(const char* extension) const;
+    bool hasGlExtension(const char* extension) const;
+    bool hasEglExtension(const char* extension) const;
 
     void dump() const;
 
@@ -53,12 +54,12 @@
     Extensions();
     ~Extensions();
 
+    void findExtensions(const char* extensions, SortedVector<String8>& list) const;
+
     friend class Singleton<Extensions>;
 
-    SortedVector<String8> mExtensionList;
-
-    char* mExtensions;
-    char* mVersion;
+    SortedVector<String8> mGlExtensionList;
+    SortedVector<String8> mEglExtensionList;
 
     bool mHasNPot;
     bool mHasFramebufferFetch;
@@ -68,6 +69,7 @@
     bool mHasTiledRendering;
     bool mHas1BitStencil;
     bool mHas4BitStencil;
+    bool mHasNvSystemTime;
 
     int mVersionMajor;
     int mVersionMinor;