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

Commit f462c249 authored by Ben Wagner's avatar Ben Wagner
Browse files

Use at least SkStreamAsset for SkTypefaces.

The existing AssetStreamAdapter is only SkStreamRewindable, and does not
support 'duplicate'. This is needed in order for SkTypeface to be used
properly. Also, SkTypeface is moving to require SkStreamAsset for
creating typefaces for performance and code reasons.

In the previous code, Asset::getBuffer is called on creation of the
typeface, so do so explicitly and manage the memory directly. This
also prevents additional copies being made of the asset data.

BUG: 18867034
Change-Id: I458a8ec024efefb761138178b87b88b48cb4a773
parent 91a9bfc3
Loading
Loading
Loading
Loading
+14 −5
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@
#include "JNIHelp.h"
#include <core_jni_helpers.h>

#include "SkData.h"
#include "SkRefCnt.h"
#include "SkTypeface.h"
#include "GraphicsJNI.h"
#include <ScopedPrimitiveArray.h>
@@ -82,6 +84,10 @@ static jboolean FontFamily_addFontWeightStyle(JNIEnv* env, jobject clazz, jlong
    return true;
}

static void releaseAsset(const void* ptr, size_t length, void* context) {
    delete static_cast<Asset*>(context);
}

static jboolean FontFamily_addFontFromAsset(JNIEnv* env, jobject, jlong familyPtr,
        jobject jassetMgr, jstring jpath) {
    NPE_CHECK_RETURN_ZERO(env, jassetMgr);
@@ -98,12 +104,15 @@ static jboolean FontFamily_addFontFromAsset(JNIEnv* env, jobject, jlong familyPt
        return false;
    }

    SkStream* stream = new AssetStreamAdaptor(asset,
                                              AssetStreamAdaptor::kYes_OwnAsset,
                                              AssetStreamAdaptor::kYes_HasMemoryBase);
    const void* buf = asset->getBuffer(false);
    if (NULL == buf) {
        delete asset;
        return false;
    }

    SkAutoTUnref<SkData> data(SkData::NewWithProc(buf, asset->getLength(), releaseAsset, asset));
    SkAutoTUnref<SkMemoryStream> stream(new SkMemoryStream(data));
    SkTypeface* face = SkTypeface::CreateFromStream(stream);
    // Note: SkTypeface::CreateFromStream holds its own reference to the stream
    stream->unref();
    if (face == NULL) {
        ALOGE("addFontFromAsset failed to create font %s", str.c_str());
        return false;