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

Commit 5dd6dd55 authored by Seigo Nonaka's avatar Seigo Nonaka
Browse files

Keep source ID into native object

To avoid static field and synchronization for the mapping, keep source
identifier into native font instance.

Bug: 179113771
Test: minikin_tests
Test: hwui_unit_tests
Test: atest CtsTextTestCases CtsGraphicsTestCases
Change-Id: I238e7b8090ee89101937ec22cbe7c68aea97bcfd
parent 4c417f3b
Loading
Loading
Loading
Loading
+4 −22
Original line number Diff line number Diff line
@@ -27,7 +27,6 @@ import android.graphics.RectF;
import android.os.LocaleList;
import android.os.ParcelFileDescriptor;
import android.text.TextUtils;
import android.util.LongSparseLongArray;
import android.util.TypedValue;

import com.android.internal.annotations.GuardedBy;
@@ -67,11 +66,6 @@ public final class Font {
            NativeAllocationRegistry.createMalloced(Font.class.getClassLoader(),
                    nGetReleaseNativeFont());

    private static final Object SOURCE_ID_LOCK = new Object();
    @GuardedBy("SOURCE_ID_LOCK")
    private static final LongSparseLongArray FONT_SOURCE_ID_MAP =
            new LongSparseLongArray(300);  // System font has 200 fonts, so 300 should be enough.

    /**
     * A builder class for creating new Font.
     */
@@ -523,8 +517,6 @@ public final class Font {
    private @Nullable FontVariationAxis[] mAxes = null;
    @GuardedBy("mLock")
    private @NonNull LocaleList mLocaleList = null;
    @GuardedBy("mLock")
    private int mSourceIdentifier = -1;

    /**
     * Use Builder instead
@@ -758,20 +750,7 @@ public final class Font {
     * @return an unique identifier for the font source data.
     */
    public int getSourceIdentifier() {
        synchronized (mLock) {
            if (mSourceIdentifier == -1) {
                long bufferAddress = nGetBufferAddress(mNativePtr);
                synchronized (SOURCE_ID_LOCK) {
                    long id = FONT_SOURCE_ID_MAP.get(bufferAddress, -1);
                    if (id == -1) {
                        id = FONT_SOURCE_ID_MAP.size();
                        FONT_SOURCE_ID_MAP.append(bufferAddress, id);
                    }
                    mSourceIdentifier = (int) id;
                }
            }
            return mSourceIdentifier;
        }
        return nGetSourceId(mNativePtr);
    }

    /**
@@ -889,6 +868,9 @@ public final class Font {
    @CriticalNative
    private static native long nGetBufferAddress(long font);

    @CriticalNative
    private static native int nGetSourceId(long font);

    @CriticalNative
    private static native long nGetReleaseNativeFont();

+5 −4
Original line number Diff line number Diff line
@@ -30,10 +30,11 @@

namespace android {

MinikinFontSkia::MinikinFontSkia(sk_sp<SkTypeface> typeface, const void* fontData, size_t fontSize,
                                 std::string_view filePath, int ttcIndex,
MinikinFontSkia::MinikinFontSkia(sk_sp<SkTypeface> typeface, int sourceId, const void* fontData,
                                 size_t fontSize, std::string_view filePath, int ttcIndex,
                                 const std::vector<minikin::FontVariation>& axes)
        : mTypeface(std::move(typeface))
        , mSourceId(sourceId)
        , mFontData(fontData)
        , mFontSize(fontSize)
        , mTtcIndex(ttcIndex)
@@ -141,8 +142,8 @@ std::shared_ptr<minikin::MinikinFont> MinikinFontSkia::createFontWithVariation(
    sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
    sk_sp<SkTypeface> face(fm->makeFromStream(std::move(stream), args));

    return std::make_shared<MinikinFontSkia>(std::move(face), mFontData, mFontSize, mFilePath,
                                             ttcIndex, variations);
    return std::make_shared<MinikinFontSkia>(std::move(face), mSourceId, mFontData, mFontSize,
                                             mFilePath, ttcIndex, variations);
}

// hinting<<16 | edging<<8 | bools:5bits
+3 −1
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ namespace android {

class ANDROID_API MinikinFontSkia : public minikin::MinikinFont {
public:
    MinikinFontSkia(sk_sp<SkTypeface> typeface, const void* fontData, size_t fontSize,
    MinikinFontSkia(sk_sp<SkTypeface> typeface, int sourceId, const void* fontData, size_t fontSize,
                    std::string_view filePath, int ttcIndex,
                    const std::vector<minikin::FontVariation>& axes);

@@ -62,6 +62,7 @@ public:
    const std::vector<minikin::FontVariation>& GetAxes() const;
    std::shared_ptr<minikin::MinikinFont> createFontWithVariation(
            const std::vector<minikin::FontVariation>&) const;
    int GetSourceId() const override { return mSourceId; }

    static uint32_t packFontFlags(const SkFont&);
    static void unpackFontFlags(SkFont*, uint32_t fontFlags);
@@ -73,6 +74,7 @@ public:
private:
    sk_sp<SkTypeface> mTypeface;

    int mSourceId;
    // A raw pointer to the font data - it should be owned by some other object with
    // lifetime at least as long as this object.
    const void* mFontData;
+3 −3
Original line number Diff line number Diff line
@@ -185,9 +185,9 @@ void Typeface::setRobotoTypefaceForTest() {
    sk_sp<SkTypeface> typeface = SkTypeface::MakeFromStream(std::move(fontData));
    LOG_ALWAYS_FATAL_IF(typeface == nullptr, "Failed to make typeface from %s", kRobotoFont);

    std::shared_ptr<minikin::MinikinFont> font = std::make_shared<MinikinFontSkia>(
            std::move(typeface), data, st.st_size, kRobotoFont, 0,
            std::vector<minikin::FontVariation>());
    std::shared_ptr<minikin::MinikinFont> font =
            std::make_shared<MinikinFontSkia>(std::move(typeface), 0, data, st.st_size, kRobotoFont,
                                              0, std::vector<minikin::FontVariation>());
    std::vector<std::shared_ptr<minikin::Font>> fonts;
    fonts.push_back(minikin::Font::Builder(font).build());

+13 −6
Original line number Diff line number Diff line
@@ -17,15 +17,16 @@
#undef LOG_TAG
#define LOG_TAG "Minikin"

#include <nativehelper/ScopedPrimitiveArray.h>
#include <nativehelper/ScopedUtfChars.h>
#include "FontUtils.h"
#include "GraphicsJNI.h"
#include "SkData.h"
#include "SkFontMgr.h"
#include "SkRefCnt.h"
#include "SkTypeface.h"
#include "GraphicsJNI.h"
#include <nativehelper/ScopedPrimitiveArray.h>
#include <nativehelper/ScopedUtfChars.h>
#include "Utils.h"
#include "FontUtils.h"
#include "fonts/Font.h"

#include <hwui/MinikinSkia.h>
#include <hwui/Typeface.h>
@@ -35,6 +36,12 @@

#include <memory>

///////////////////////////////////////////////////////////////////////////////////////////////////
//
// The following JNI methods are kept only for compatibility reasons due to hidden API accesses.
//
///////////////////////////////////////////////////////////////////////////////////////////////////

namespace android {

struct NativeFamilyBuilder {
@@ -125,8 +132,8 @@ static bool addSkTypeface(NativeFamilyBuilder* builder, sk_sp<SkData>&& data, in
        return false;
    }
    std::shared_ptr<minikin::MinikinFont> minikinFont =
            std::make_shared<MinikinFontSkia>(std::move(face), fontPtr, fontSize, "", ttcIndex,
                    builder->axes);
            std::make_shared<MinikinFontSkia>(std::move(face), fonts::getNewSourceId(), fontPtr,
                                              fontSize, "", ttcIndex, builder->axes);
    minikin::Font::Builder fontBuilder(minikinFont);

    if (weight != RESOLVE_BY_FONT_TABLE) {
Loading