From f462c2491bae67815ae9e4a2ff7ce77db97b49c6 Mon Sep 17 00:00:00 2001 From: Ben Wagner Date: Mon, 12 Jan 2015 14:26:14 -0500 Subject: [PATCH] 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 --- core/jni/android/graphics/FontFamily.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/core/jni/android/graphics/FontFamily.cpp b/core/jni/android/graphics/FontFamily.cpp index 8a85d625e0513..3e6327ba4de9c 100644 --- a/core/jni/android/graphics/FontFamily.cpp +++ b/core/jni/android/graphics/FontFamily.cpp @@ -19,6 +19,8 @@ #include "JNIHelp.h" #include +#include "SkData.h" +#include "SkRefCnt.h" #include "SkTypeface.h" #include "GraphicsJNI.h" #include @@ -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(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 data(SkData::NewWithProc(buf, asset->getLength(), releaseAsset, asset)); + SkAutoTUnref 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;