Merge "Add systrace for VectorDrawable inflation and draw" into oc-mr1-dev

This commit is contained in:
Tenghui Zhu
2017-09-22 19:45:17 +00:00
committed by Android (Google) Code Review
2 changed files with 43 additions and 32 deletions

View File

@@ -31,6 +31,7 @@ import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffColorFilter; import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.Shader; import android.graphics.Shader;
import android.os.Trace;
import android.util.ArrayMap; import android.util.ArrayMap;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
@@ -605,38 +606,44 @@ public class VectorDrawable extends Drawable {
public void inflate(@NonNull Resources r, @NonNull XmlPullParser parser, public void inflate(@NonNull Resources r, @NonNull XmlPullParser parser,
@NonNull AttributeSet attrs, @Nullable Theme theme) @NonNull AttributeSet attrs, @Nullable Theme theme)
throws XmlPullParserException, IOException { throws XmlPullParserException, IOException {
if (mVectorState.mRootGroup != null || mVectorState.mNativeTree != null) { try {
// This VD has been used to display other VD resource content, clean up. Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "VectorDrawable#inflate");
if (mVectorState.mRootGroup != null) { if (mVectorState.mRootGroup != null || mVectorState.mNativeTree != null) {
// Subtract the native allocation for all the nodes. // This VD has been used to display other VD resource content, clean up.
VMRuntime.getRuntime().registerNativeFree(mVectorState.mRootGroup.getNativeSize()); if (mVectorState.mRootGroup != null) {
// Remove child nodes' reference to tree // Subtract the native allocation for all the nodes.
mVectorState.mRootGroup.setTree(null); VMRuntime.getRuntime().registerNativeFree(
mVectorState.mRootGroup.getNativeSize());
// Remove child nodes' reference to tree
mVectorState.mRootGroup.setTree(null);
}
mVectorState.mRootGroup = new VGroup();
if (mVectorState.mNativeTree != null) {
// Subtract the native allocation for the tree wrapper, which contains root node
// as well as rendering related data.
VMRuntime.getRuntime().registerNativeFree(mVectorState.NATIVE_ALLOCATION_SIZE);
mVectorState.mNativeTree.release();
}
mVectorState.createNativeTree(mVectorState.mRootGroup);
} }
mVectorState.mRootGroup = new VGroup(); final VectorDrawableState state = mVectorState;
if (mVectorState.mNativeTree != null) { state.setDensity(Drawable.resolveDensity(r, 0));
// Subtract the native allocation for the tree wrapper, which contains root node
// as well as rendering related data. final TypedArray a = obtainAttributes(r, theme, attrs, R.styleable.VectorDrawable);
VMRuntime.getRuntime().registerNativeFree(mVectorState.NATIVE_ALLOCATION_SIZE); updateStateFromTypedArray(a);
mVectorState.mNativeTree.release(); a.recycle();
}
mVectorState.createNativeTree(mVectorState.mRootGroup); mDpiScaledDirty = true;
state.mCacheDirty = true;
inflateChildElements(r, parser, attrs, theme);
state.onTreeConstructionFinished();
// Update local properties.
updateLocalState(r);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
} }
final VectorDrawableState state = mVectorState;
state.setDensity(Drawable.resolveDensity(r, 0));
final TypedArray a = obtainAttributes(r, theme, attrs, R.styleable.VectorDrawable);
updateStateFromTypedArray(a);
a.recycle();
mDpiScaledDirty = true;
state.mCacheDirty = true;
inflateChildElements(r, parser, attrs, theme);
state.onTreeConstructionFinished();
// Update local properties.
updateLocalState(r);
} }
private void updateStateFromTypedArray(TypedArray a) throws XmlPullParserException { private void updateStateFromTypedArray(TypedArray a) throws XmlPullParserException {

View File

@@ -22,6 +22,7 @@
#include "SkShader.h" #include "SkShader.h"
#include <utils/Log.h> #include <utils/Log.h>
#include "utils/Macros.h" #include "utils/Macros.h"
#include "utils/TraceUtils.h"
#include "utils/VectorDrawableUtils.h" #include "utils/VectorDrawableUtils.h"
#include <math.h> #include <math.h>
@@ -593,14 +594,17 @@ void Tree::draw(SkCanvas* canvas) {
void Tree::updateBitmapCache(Bitmap& bitmap, bool useStagingData) { void Tree::updateBitmapCache(Bitmap& bitmap, bool useStagingData) {
SkBitmap outCache; SkBitmap outCache;
bitmap.getSkBitmap(&outCache); bitmap.getSkBitmap(&outCache);
int cacheWidth = outCache.width();
int cacheHeight = outCache.height();
ATRACE_FORMAT("VectorDrawable repaint %dx%d", cacheWidth, cacheHeight);
outCache.eraseColor(SK_ColorTRANSPARENT); outCache.eraseColor(SK_ColorTRANSPARENT);
SkCanvas outCanvas(outCache); SkCanvas outCanvas(outCache);
float viewportWidth = useStagingData ? float viewportWidth = useStagingData ?
mStagingProperties.getViewportWidth() : mProperties.getViewportWidth(); mStagingProperties.getViewportWidth() : mProperties.getViewportWidth();
float viewportHeight = useStagingData ? float viewportHeight = useStagingData ?
mStagingProperties.getViewportHeight() : mProperties.getViewportHeight(); mStagingProperties.getViewportHeight() : mProperties.getViewportHeight();
float scaleX = outCache.width() / viewportWidth; float scaleX = cacheWidth / viewportWidth;
float scaleY = outCache.height() / viewportHeight; float scaleY = cacheHeight / viewportHeight;
outCanvas.scale(scaleX, scaleY); outCanvas.scale(scaleX, scaleY);
mRootNode->draw(&outCanvas, useStagingData); mRootNode->draw(&outCanvas, useStagingData);
} }