-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Reuse Bitmap, Paint & Canvas instances in Android LocalGlyphRasterizer #12421
Comments
Hi @themics, that sounds like a good suggestion! You're right, Also I'm curious, did the glyph rasterization cost show up as a significant expense in part of your profiling? A 1.5x-3x speed-up is great, although I had the assumption that even on maps with lots of CJK characters, the rasterization cost would be a pretty small part of overall map loading time. |
@ChrisLoer I'll try if you want, but it will takes time because I should rework. My test was kind of proof of concept, so I just stored them in the static field. public class LocalGlyphRasterizer {
private static Bitmap bitmap;
private static Paint paint;
private static Canvas canvas;
static {
bitmap = Bitmap.createBitmap(35, 35, Bitmap.Config.ARGB_8888);
paint = new Paint();
paint.setAntiAlias(true);
paint.setTextSize(24);
canvas = new Canvas();
canvas.setBitmap(bitmap);
}
@WorkerThread
public static Bitmap drawGlyphBitmap(String fontFamily, boolean bold, char glyphID) {
paint.setTypeface(Typeface.create(fontFamily, bold ? Typeface.BOLD : Typeface.NORMAL));
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.drawText(String.valueOf(glyphID), 0, 20, paint);
return bitmap;
}
} As you can see it's not usable when 2 or more About overall loading time, you're right. It's not much significant but still effective especially on the large screen and/or maximum tilt. I measured time between new CameraPosition.Builder().target(new LatLng(37.56738, 126.99655))
.zoom(13).tilt(60).bearing(90)
.build() The result is:
|
Ah I see, yes, it probably most makes sense for the re-used items to be members of Sounds like almost 9% improvement in time to render, or >100ms -- that's not huge, but it definitely sounds worth the effort. I'm really glad to hear you're getting use out of the local glyph generation feature! If you got really ambitious and wanted to further optimize Android glyph generation, I suspect a good approach would be to batch up the drawing request so that all of the glyphs in a batch were drawn into a single bitmap, in order to minimize the JNI overhead. |
@ChrisLoer Yup turns out it was simple job, native |
Mapbox SDK versions: 6.3.0-beta1
Since
LocalGlyphRasterizer.drawGlyphBitmap()
only called in a GL thread, couldn't we store these instances(per GL thread, perhapsThreadLocal
?) and reuse?In this case of course we should clear the canvas by
drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)
before drawing, but even with that, reusing is much more fast.I tested a bit by rasterizing 1,000 korean characters with my Pixel 2, here's the result:
The text was updated successfully, but these errors were encountered: