Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6dac0d4b authored by Diego Perez's avatar Diego Perez
Browse files

Reduce the amount of allocations on Paint_Delegate

When the text properties of the Paint object are updated, or when .set()
is called, updateFontObject will be called. This method is currently at the
top of the methods for number of allocations.
This CL reduces the number of calls to updateFontObject by a 90%,
reducing the number of Font allocations.

Change-Id: I3e232f9e6e230da8ecdd19921faadf2d3f4ff763
parent 2bc3ff3c
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -178,7 +178,9 @@ public class FontFamily_Delegate {
        desiredStyle.mIsItalic = isItalic;
        FontInfo bestFont = null;
        int bestMatch = Integer.MAX_VALUE;
        for (FontInfo font : mFonts) {
        //noinspection ForLoopReplaceableByForEach (avoid iterator instantiation)
        for (int i = 0, n = mFonts.size(); i < n; i++) {
            FontInfo font = mFonts.get(i);
            int match = computeMatch(font, desiredStyle);
            if (match < bestMatch) {
                bestMatch = match;
@@ -415,7 +417,9 @@ public class FontFamily_Delegate {
        boolean isItalic = fontInfo.mIsItalic;
        // The list is usually just two fonts big. So iterating over all isn't as bad as it looks.
        // It's biggest for roboto where the size is 12.
        for (FontInfo font : mFonts) {
        //noinspection ForLoopReplaceableByForEach (avoid iterator instantiation)
        for (int i = 0, n = mFonts.size(); i < n; i++) {
            FontInfo font = mFonts.get(i);
            if (font.mWeight == weight && font.mIsItalic == isItalic) {
                return false;
            }
+55 −17
Original line number Diff line number Diff line
@@ -480,9 +480,11 @@ public class Paint_Delegate {
            return;
        }

        if (delegate.mTextSize != textSize) {
            delegate.mTextSize = textSize;
            delegate.updateFontObject();
        }
    }

    @LayoutlibDelegate
    /*package*/ static float getTextScaleX(Paint thisPaint) {
@@ -503,9 +505,11 @@ public class Paint_Delegate {
            return;
        }

        if (delegate.mTextScaleX != scaleX) {
            delegate.mTextScaleX = scaleX;
            delegate.updateFontObject();
        }
    }

    @LayoutlibDelegate
    /*package*/ static float getTextSkewX(Paint thisPaint) {
@@ -526,9 +530,11 @@ public class Paint_Delegate {
            return;
        }

        if (delegate.mTextSkewX != skewX) {
            delegate.mTextSkewX = skewX;
            delegate.updateFontObject();
        }
    }

    @LayoutlibDelegate
    /*package*/ static float ascent(Paint thisPaint) {
@@ -897,9 +903,12 @@ public class Paint_Delegate {
            return 0;
        }

        Typeface_Delegate typefaceDelegate = Typeface_Delegate.getDelegate(typeface);
        if (delegate.mTypeface != typefaceDelegate || delegate.mNativeTypeface != typeface) {
            delegate.mTypeface = Typeface_Delegate.getDelegate(typeface);
            delegate.mNativeTypeface = typeface;
            delegate.updateFontObject();
        }
        return typeface;
    }

@@ -1214,13 +1223,31 @@ public class Paint_Delegate {
        mCap = paint.mCap;
        mJoin = paint.mJoin;
        mTextAlign = paint.mTextAlign;

        boolean needsFontUpdate = false;
        if (mTypeface != paint.mTypeface || mNativeTypeface != paint.mNativeTypeface) {
            mTypeface = paint.mTypeface;
            mNativeTypeface = paint.mNativeTypeface;
        mStrokeWidth = paint.mStrokeWidth;
        mStrokeMiter = paint.mStrokeMiter;
            needsFontUpdate = true;
        }

        if (mTextSize != paint.mTextSize) {
            mTextSize = paint.mTextSize;
            needsFontUpdate = true;
        }

        if (mTextScaleX != paint.mTextScaleX) {
            mTextScaleX = paint.mTextScaleX;
            needsFontUpdate = true;
        }

        if (mTextSkewX != paint.mTextSkewX) {
            mTextSkewX = paint.mTextSkewX;
            needsFontUpdate = true;
        }

        mStrokeWidth = paint.mStrokeWidth;
        mStrokeMiter = paint.mStrokeMiter;
        mXfermode = paint.mXfermode;
        mColorFilter = paint.mColorFilter;
        mShader = paint.mShader;
@@ -1228,8 +1255,11 @@ public class Paint_Delegate {
        mMaskFilter = paint.mMaskFilter;
        mRasterizer = paint.mRasterizer;
        mHintingMode = paint.mHintingMode;

        if (needsFontUpdate) {
            updateFontObject();
        }
    }

    private void reset() {
        mFlags = Paint.HIDDEN_DEFAULT_PAINT_FLAGS;
@@ -1264,10 +1294,18 @@ public class Paint_Delegate {
            // Get the fonts from the TypeFace object.
            List<Font> fonts = mTypeface.getFonts(mFontVariant);

            if (fonts.isEmpty()) {
                mFonts = Collections.emptyList();
                return;
            }

            // create new font objects as well as FontMetrics, based on the current text size
            // and skew info.
            ArrayList<FontInfo> infoList = new ArrayList<FontInfo>(fonts.size());
            for (Font font : fonts) {
            int nFonts = fonts.size();
            ArrayList<FontInfo> infoList = new ArrayList<FontInfo>(nFonts);
            //noinspection ForLoopReplaceableByForEach (avoid iterator instantiation)
            for (int i = 0; i < nFonts; i++) {
                Font font = fonts.get(i);
                if (font == null) {
                    // If the font is null, add null to infoList. When rendering the text, if this
                    // null is reached, a warning will be logged.