From c6dc45700bf0c18708b0ad2f695ea85fadcbf131 Mon Sep 17 00:00:00 2001 From: Konstantin Lopyrev Date: Fri, 6 Aug 2010 15:01:52 -0700 Subject: [PATCH] Make sure profiling is done only for views that are actually measured, laid out and drawn. Change-Id: I88c66e882be2781d079c51b6580a19c4e359c5b1 --- core/java/android/view/View.java | 4 +- core/java/android/view/ViewDebug.java | 118 ++++++++++++++------------ 2 files changed, 68 insertions(+), 54 deletions(-) diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index c3f81a2b8c8df..8007710948d3d 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -1420,8 +1420,8 @@ public class View implements Drawable.Callback, KeyEvent.Callback, Accessibility static final int MEASURED_DIMENSION_SET = 0x00000800; /** {@hide} */ static final int FORCE_LAYOUT = 0x00001000; - - private static final int LAYOUT_REQUIRED = 0x00002000; + /** {@hide} */ + static final int LAYOUT_REQUIRED = 0x00002000; private static final int PRESSED = 0x00004000; diff --git a/core/java/android/view/ViewDebug.java b/core/java/android/view/ViewDebug.java index 5dd45f9df03c7..09939a6d7ffdc 100644 --- a/core/java/android/view/ViewDebug.java +++ b/core/java/android/view/ViewDebug.java @@ -934,65 +934,76 @@ public class ViewDebug { private static void profileViewAndChildren(final View view, BufferedWriter out) throws IOException { - final long durationMeasure = profileViewOperation(view, new ViewOperation() { - public Void[] pre() { - forceLayout(view); - return null; - } + profileViewAndChildren(view, out, true); + } - private void forceLayout(View view) { - view.forceLayout(); - if (view instanceof ViewGroup) { - ViewGroup group = (ViewGroup) view; - final int count = group.getChildCount(); - for (int i = 0; i < count; i++) { - forceLayout(group.getChildAt(i)); - } - } - } + private static void profileViewAndChildren(final View view, BufferedWriter out, boolean root) + throws IOException { - public void run(Void... data) { - view.measure(view.mOldWidthMeasureSpec, view.mOldHeightMeasureSpec); - } + long durationMeasure = + (root || (view.mPrivateFlags & View.MEASURED_DIMENSION_SET) != 0) ? profileViewOperation( + view, new ViewOperation() { + public Void[] pre() { + forceLayout(view); + return null; + } - public void post(Void... data) { - } - }); + private void forceLayout(View view) { + view.forceLayout(); + if (view instanceof ViewGroup) { + ViewGroup group = (ViewGroup) view; + final int count = group.getChildCount(); + for (int i = 0; i < count; i++) { + forceLayout(group.getChildAt(i)); + } + } + } - final long durationLayout = profileViewOperation(view, new ViewOperation() { - public Void[] pre() { - return null; - } + public void run(Void... data) { + view.measure(view.mOldWidthMeasureSpec, view.mOldHeightMeasureSpec); + } - public void run(Void... data) { - view.layout(view.mLeft, view.mTop, view.mRight, view.mBottom); - } + public void post(Void... data) { + } + }) + : 0; + long durationLayout = + (root || (view.mPrivateFlags & View.LAYOUT_REQUIRED) != 0) ? profileViewOperation( + view, new ViewOperation() { + public Void[] pre() { + return null; + } - public void post(Void... data) { - } - }); + public void run(Void... data) { + view.layout(view.mLeft, view.mTop, view.mRight, view.mBottom); + } - final long durationDraw = profileViewOperation(view, new ViewOperation() { - public Object[] pre() { - final DisplayMetrics metrics = view.getResources().getDisplayMetrics(); - final Bitmap bitmap = - Bitmap.createBitmap(metrics.widthPixels, metrics.heightPixels, - Bitmap.Config.RGB_565); - final Canvas canvas = new Canvas(bitmap); - return new Object[] { - bitmap, canvas - }; - } + public void post(Void... data) { + } + }) : 0; + long durationDraw = + (root || (view.mPrivateFlags & View.DRAWN) != 0) ? profileViewOperation(view, + new ViewOperation() { + public Object[] pre() { + final DisplayMetrics metrics = + view.getResources().getDisplayMetrics(); + final Bitmap bitmap = + Bitmap.createBitmap(metrics.widthPixels, + metrics.heightPixels, Bitmap.Config.RGB_565); + final Canvas canvas = new Canvas(bitmap); + return new Object[] { + bitmap, canvas + }; + } - public void run(Object... data) { - view.draw((Canvas) data[1]); - } - - public void post(Object... data) { - ((Bitmap) data[0]).recycle(); - } - }); + public void run(Object... data) { + view.draw((Canvas) data[1]); + } + public void post(Object... data) { + ((Bitmap) data[0]).recycle(); + } + }) : 0; out.write(String.valueOf(durationMeasure)); out.write(' '); out.write(String.valueOf(durationLayout)); @@ -1003,7 +1014,7 @@ public class ViewDebug { ViewGroup group = (ViewGroup) view; final int count = group.getChildCount(); for (int i = 0; i < count; i++) { - profileViewAndChildren(group.getChildAt(i), out); + profileViewAndChildren(group.getChildAt(i), out, false); } } } @@ -1033,7 +1044,10 @@ public class ViewDebug { }); try { - latch.await(CAPTURE_TIMEOUT, TimeUnit.MILLISECONDS); + if (!latch.await(CAPTURE_TIMEOUT, TimeUnit.MILLISECONDS)) { + Log.w("View", "Could not complete the profiling of the view " + view); + return -1; + } } catch (InterruptedException e) { Log.w("View", "Could not complete the profiling of the view " + view); Thread.currentThread().interrupt();