diff options
author | Kiyong Jung <autoexec.bat@navercorp.com> | 2018-07-19 12:08:40 +0900 |
---|---|---|
committer | Tobrun <tobrun@mapbox.com> | 2018-07-27 11:24:26 +0200 |
commit | 37bbed5b9c3f95dc1e596b885702808202e4193c (patch) | |
tree | 5d0527a5c6fbc3e99eabff8fd3998190ae187493 /platform | |
parent | 94ad8e2ff8928bec316dfcc9790db7b522673d3e (diff) | |
download | qtlocation-mapboxgl-37bbed5b9c3f95dc1e596b885702808202e4193c.tar.gz |
[android] Reuse Bitmap, Paint & Canvas instances in LocalGlyphRasterizer
- Make LocalGlyphRasterizer.drawGlyphBitmap() non-static
- Store and reuse Bitmap, Paint & Canvas instances
Diffstat (limited to 'platform')
3 files changed, 46 insertions, 24 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/text/LocalGlyphRasterizer.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/text/LocalGlyphRasterizer.java index 5904c0d69e..a4e98532d1 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/text/LocalGlyphRasterizer.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/text/LocalGlyphRasterizer.java @@ -1,8 +1,10 @@ package com.mapbox.mapboxsdk.text; import android.graphics.Canvas; +import android.graphics.Color; import android.graphics.Paint; import android.graphics.Bitmap; +import android.graphics.PorterDuff; import android.graphics.Typeface; import android.support.annotation.Keep; import android.support.annotation.WorkerThread; @@ -12,6 +14,25 @@ import android.support.annotation.WorkerThread; * by the portable local_glyph_rasterizer.hpp */ public class LocalGlyphRasterizer { + private final Bitmap bitmap; + private final Paint paint; + private final Canvas canvas; + + LocalGlyphRasterizer() { + /* + 35x35px dimensions are hardwired to match local_glyph_rasterizer.cpp + These dimensions are large enough to draw a 24 point character in the middle + of the bitmap (y: 20) with some buffer around the edge + */ + bitmap = Bitmap.createBitmap(35, 35, Bitmap.Config.ARGB_8888); + + paint = new Paint(); + paint.setAntiAlias(true); + paint.setTextSize(24); + + canvas = new Canvas(); + canvas.setBitmap(bitmap); + } /*** * Uses Android-native drawing code to rasterize a single glyph @@ -26,23 +47,10 @@ public class LocalGlyphRasterizer { */ @WorkerThread @Keep - protected static Bitmap drawGlyphBitmap(String fontFamily, boolean bold, char glyphID) { - /* - 35x35px dimensions are hardwired to match local_glyph_rasterizer.cpp - These dimensions are large enough to draw a 24 point character in the middle - of the bitmap (y: 20) with some buffer around the edge - */ - Bitmap bitmap = Bitmap.createBitmap(35, 35, Bitmap.Config.ARGB_8888); - - Paint paint = new Paint(); - paint.setAntiAlias(true); - paint.setTextSize(24); + protected Bitmap drawGlyphBitmap(String fontFamily, boolean bold, char glyphID) { paint.setTypeface(Typeface.create(fontFamily, bold ? Typeface.BOLD : Typeface.NORMAL)); - - Canvas canvas = new Canvas(); - canvas.setBitmap(bitmap); + canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); canvas.drawText(String.valueOf(glyphID), 0, 20, paint); - return bitmap; } } diff --git a/platform/android/src/text/local_glyph_rasterizer.cpp b/platform/android/src/text/local_glyph_rasterizer.cpp index d232058d15..3dc1eb275a 100644 --- a/platform/android/src/text/local_glyph_rasterizer.cpp +++ b/platform/android/src/text/local_glyph_rasterizer.cpp @@ -33,19 +33,27 @@ namespace mbgl { namespace android { +LocalGlyphRasterizer::LocalGlyphRasterizer() { + UniqueEnv env { AttachEnv() }; + + static auto constructor = javaClass.GetConstructor(*env); + + javaObject = javaClass.New(*env, constructor).NewGlobalRef(*env); +} + PremultipliedImage LocalGlyphRasterizer::drawGlyphBitmap(const std::string& fontFamily, const bool bold, const GlyphID glyphID) { UniqueEnv env { AttachEnv() }; using Signature = jni::Object<Bitmap>(jni::String, jni::jboolean, jni::jchar); - auto method = javaClass.GetStaticMethod<Signature>(*env, "drawGlyphBitmap"); + static auto method = javaClass.GetMethod<Signature>(*env, "drawGlyphBitmap"); jni::String jniFontFamily = jni::Make<jni::String>(*env, fontFamily); - auto javaBitmap = javaClass.Call(*env, - method, - jniFontFamily, - static_cast<jni::jboolean>(bold), - static_cast<jni::jchar>(glyphID)); + auto javaBitmap = javaObject->Call(*env, + method, + jniFontFamily, + static_cast<jni::jboolean>(bold), + static_cast<jni::jchar>(glyphID)); jni::DeleteLocalRef(*env, jniFontFamily); PremultipliedImage result = Bitmap::GetImage(*env, javaBitmap); @@ -77,13 +85,15 @@ public: std::string lowercaseFont = platform::lowercase(font); if (lowercaseFont.find("bold") != std::string::npos) { bold = true; + break; } } - return android::LocalGlyphRasterizer::drawGlyphBitmap(*fontFamily, bold, glyphID); + return androidLocalGlyphRasterizer.drawGlyphBitmap(*fontFamily, bold, glyphID); } private: optional<std::string> fontFamily; + android::LocalGlyphRasterizer androidLocalGlyphRasterizer; }; LocalGlyphRasterizer::LocalGlyphRasterizer(const optional<std::string> fontFamily) diff --git a/platform/android/src/text/local_glyph_rasterizer_jni.hpp b/platform/android/src/text/local_glyph_rasterizer_jni.hpp index 38d98d5368..1c83ea5f58 100644 --- a/platform/android/src/text/local_glyph_rasterizer_jni.hpp +++ b/platform/android/src/text/local_glyph_rasterizer_jni.hpp @@ -17,14 +17,18 @@ namespace android { class LocalGlyphRasterizer { public: - static PremultipliedImage drawGlyphBitmap(const std::string& fontFamily, const bool bold, const char16_t glyphID); - static constexpr auto Name() { return "com/mapbox/mapboxsdk/text/LocalGlyphRasterizer"; }; static jni::Class<LocalGlyphRasterizer> javaClass; static void registerNative(jni::JNIEnv&); + LocalGlyphRasterizer(); + + PremultipliedImage drawGlyphBitmap(const std::string& fontFamily, const bool bold, const char16_t glyphID); + +private: + jni::UniqueObject<LocalGlyphRasterizer> javaObject; }; } // namespace android |