Pass buffer base address to minikin::BufferReader.

Bug: 174672300
Test: atest CtsGraphicsTestCases:android.graphics.cts.TypefaceTest
Change-Id: Ie6bf1bb01ccc2803bc6206a0d2276ac59c6877a1
diff --git a/libs/hwui/jni/Typeface.cpp b/libs/hwui/jni/Typeface.cpp
index 19efc5f..6325226 100644
--- a/libs/hwui/jni/Typeface.cpp
+++ b/libs/hwui/jni/Typeface.cpp
@@ -305,7 +305,8 @@
     }
 }
 
-static jint Typeface_writeTypefaces(JNIEnv *env, jobject, jobject buffer, jlongArray faceHandles) {
+static jint Typeface_writeTypefaces(JNIEnv* env, jobject, jobject buffer, jint position,
+                                    jlongArray faceHandles) {
     MinikinFontSkiaFactory::init();
     ScopedLongArrayRO faces(env, faceHandles);
     std::vector<Typeface*> typefaces;
@@ -314,7 +315,12 @@
         typefaces.push_back(toTypeface(faces[i]));
     }
     void* addr = buffer == nullptr ? nullptr : env->GetDirectBufferAddress(buffer);
-    minikin::BufferWriter writer(addr);
+    if (addr != nullptr &&
+        reinterpret_cast<intptr_t>(addr) % minikin::BufferReader::kMaxAlignment != 0) {
+        ALOGE("addr (%p) must be aligned at kMaxAlignment, but it was not.", addr);
+        return 0;
+    }
+    minikin::BufferWriter writer(addr, position);
     std::vector<std::shared_ptr<minikin::FontCollection>> fontCollections;
     std::unordered_map<std::shared_ptr<minikin::FontCollection>, size_t> fcToIndex;
     for (Typeface* typeface : typefaces) {
@@ -334,11 +340,18 @@
     return static_cast<jint>(writer.size());
 }
 
-static jlongArray Typeface_readTypefaces(JNIEnv *env, jobject, jobject buffer) {
+static jlongArray Typeface_readTypefaces(JNIEnv* env, jobject, jobject buffer, jint position) {
     MinikinFontSkiaFactory::init();
     void* addr = buffer == nullptr ? nullptr : env->GetDirectBufferAddress(buffer);
-    if (addr == nullptr) return nullptr;
-    minikin::BufferReader reader(addr);
+    if (addr == nullptr) {
+        ALOGE("Passed a null buffer.");
+        return nullptr;
+    }
+    if (reinterpret_cast<intptr_t>(addr) % minikin::BufferReader::kMaxAlignment != 0) {
+        ALOGE("addr (%p) must be aligned at kMaxAlignment, but it was not.", addr);
+        return nullptr;
+    }
+    minikin::BufferReader reader(addr, position);
     std::vector<std::shared_ptr<minikin::FontCollection>> fontCollections =
             minikin::FontCollection::readVector(&reader);
     uint32_t typefaceCount = reader.read<uint32_t>();
@@ -357,7 +370,6 @@
     return result;
 }
 
-
 static void Typeface_forceSetStaticFinalField(JNIEnv *env, jclass cls, jstring fieldName,
         jobject typeface) {
     ScopedUtfChars fieldNameChars(env, fieldName);
@@ -417,8 +429,8 @@
         {"nativeGetSupportedAxes", "(J)[I", (void*)Typeface_getSupportedAxes},
         {"nativeRegisterGenericFamily", "(Ljava/lang/String;J)V",
          (void*)Typeface_registerGenericFamily},
-        {"nativeWriteTypefaces", "(Ljava/nio/ByteBuffer;[J)I", (void*)Typeface_writeTypefaces},
-        {"nativeReadTypefaces", "(Ljava/nio/ByteBuffer;)[J", (void*)Typeface_readTypefaces},
+        {"nativeWriteTypefaces", "(Ljava/nio/ByteBuffer;I[J)I", (void*)Typeface_writeTypefaces},
+        {"nativeReadTypefaces", "(Ljava/nio/ByteBuffer;I)[J", (void*)Typeface_readTypefaces},
         {"nativeForceSetStaticFinalField", "(Ljava/lang/String;Landroid/graphics/Typeface;)V",
          (void*)Typeface_forceSetStaticFinalField},
         {"nativeGetFamilySize", "(J)I", (void*)Typeface_getFamilySize},
diff --git a/libs/hwui/jni/fonts/Font.cpp b/libs/hwui/jni/fonts/Font.cpp
index 2c421f8..f17129c 100644
--- a/libs/hwui/jni/fonts/Font.cpp
+++ b/libs/hwui/jni/fonts/Font.cpp
@@ -228,7 +228,7 @@
 static jstring Font_getFontPath(JNIEnv* env, jobject, jlong fontPtr) {
     FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr);
     minikin::BufferReader reader = font->font->typefaceMetadataReader();
-    if (reader.data() != nullptr) {
+    if (reader.current() != nullptr) {
         std::string path = std::string(reader.readString());
         if (path.empty()) {
             return nullptr;
@@ -270,7 +270,7 @@
 static jint Font_getIndex(CRITICAL_JNI_PARAMS_COMMA jlong fontPtr) {
     FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr);
     minikin::BufferReader reader = font->font->typefaceMetadataReader();
-    if (reader.data() != nullptr) {
+    if (reader.current() != nullptr) {
         reader.skipString();  // fontPath
         return reader.read<int>();
     } else {
@@ -283,7 +283,7 @@
 static jint Font_getAxisCount(CRITICAL_JNI_PARAMS_COMMA jlong fontPtr) {
     FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr);
     minikin::BufferReader reader = font->font->typefaceMetadataReader();
-    if (reader.data() != nullptr) {
+    if (reader.current() != nullptr) {
         reader.skipString();  // fontPath
         reader.skip<int>();   // fontIndex
         return reader.readArray<minikin::FontVariation>().second;
@@ -298,7 +298,7 @@
     FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr);
     minikin::BufferReader reader = font->font->typefaceMetadataReader();
     minikin::FontVariation var;
-    if (reader.data() != nullptr) {
+    if (reader.current() != nullptr) {
         reader.skipString();  // fontPath
         reader.skip<int>();   // fontIndex
         var = reader.readArray<minikin::FontVariation>().first[index];