Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

NativeMapView.addImage without copying bitmap into Java #11111

Merged
merged 2 commits into from
Feb 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -758,20 +758,8 @@ public void addImage(@NonNull String name, @NonNull Bitmap image) {
return;
}

// Check/correct config
if (image.getConfig() != Bitmap.Config.ARGB_8888) {
image = image.copy(Bitmap.Config.ARGB_8888, false);
}

// Get pixels
ByteBuffer buffer = ByteBuffer.allocate(image.getByteCount());
image.copyPixelsToBuffer(buffer);

// Determine pixel ratio
float density = image.getDensity() == Bitmap.DENSITY_NONE ? Bitmap.DENSITY_NONE : image.getDensity();
float pixelRatio = density / DisplayMetrics.DENSITY_DEFAULT;

nativeAddImage(name, image.getWidth(), image.getHeight(), pixelRatio, buffer.array());
nativeAddImage(name, image, image.getDensity() / DisplayMetrics.DENSITY_DEFAULT);
}

public void addImages(@NonNull HashMap<String, Bitmap> bitmapHashMap) {
Expand Down Expand Up @@ -1032,8 +1020,7 @@ private native void nativeFlyTo(double angle, double latitude, double longitude,

private native void nativeRemoveSource(Source source, long sourcePtr);

private native void nativeAddImage(String name, int width, int height, float pixelRatio,
byte[] array);
private native void nativeAddImage(String name, Bitmap bitmap, float pixelRatio);

private native void nativeAddImages(Image[] images);

Expand Down
10 changes: 8 additions & 2 deletions platform/android/src/bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ PremultipliedImage Bitmap::GetImage(jni::JNIEnv& env, jni::Object<Bitmap> bitmap
}

if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
// TODO: convert
throw std::runtime_error("bitmap decoding: bitmap format invalid");
bitmap = Bitmap::Copy(env, bitmap);
}

const PixelGuard guard(env, bitmap);
Expand All @@ -128,5 +127,12 @@ PremultipliedImage Bitmap::GetImage(jni::JNIEnv& env, jni::Object<Bitmap> bitmap
return { Size{ info.width, info.height }, std::move(pixels) };
}

jni::Object<Bitmap> Bitmap::Copy(jni::JNIEnv& env, jni::Object<Bitmap> bitmap) {
using Signature = jni::Object<Bitmap>(jni::Object<Config>, jni::jboolean);
auto static method = _class.GetMethod<Signature>(env, "copy");
auto config = Bitmap::Config::Create(env, Bitmap::Config::Value::ARGB_8888);
return bitmap.Call(env, method, config, (jni::jboolean) false);
}

} // namespace android
} // namespace mbgl
1 change: 1 addition & 0 deletions platform/android/src/bitmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Bitmap {

static PremultipliedImage GetImage(jni::JNIEnv&, jni::Object<Bitmap>);
static jni::Object<Bitmap> CreateBitmap(jni::JNIEnv&, const PremultipliedImage&);
static jni::Object<Bitmap> Copy(jni::JNIEnv&, jni::Object<Bitmap>);

private:
static jni::Class<Bitmap> _class;
Expand Down
13 changes: 3 additions & 10 deletions platform/android/src/native_map_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,16 +893,9 @@ void NativeMapView::removeSource(JNIEnv& env, jni::Object<Source> obj, jlong sou
source->removeFromMap(env, obj, *map);
}

void NativeMapView::addImage(JNIEnv& env, jni::String name, jni::jint w, jni::jint h, jni::jfloat scale, jni::Array<jbyte> pixels) {
jni::NullCheck(env, &pixels);
std::size_t size = pixels.Length(env);

mbgl::PremultipliedImage premultipliedImage({ static_cast<uint32_t>(w), static_cast<uint32_t>(h) });
if (premultipliedImage.bytes() != uint32_t(size)) {
throw mbgl::util::SpriteImageException("Sprite image pixel count mismatch");
}

jni::GetArrayRegion(env, *pixels, 0, size, reinterpret_cast<jbyte*>(premultipliedImage.data.get()));
void NativeMapView::addImage(JNIEnv& env, jni::String name, jni::Object<Bitmap> bitmap, jni::jfloat scale) {
jni::NullCheck(env, &bitmap);
mbgl::PremultipliedImage premultipliedImage = Bitmap::GetImage(env, bitmap);

map->getStyle().addImage(std::make_unique<mbgl::style::Image>(
jni::Make<std::string>(env, name),
Expand Down
2 changes: 1 addition & 1 deletion platform/android/src/native_map_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class NativeMapView : public MapObserver {

void removeSource(JNIEnv&, jni::Object<Source>, jlong nativePtr);

void addImage(JNIEnv&, jni::String, jni::jint, jni::jint, jni::jfloat, jni::Array<jbyte>);
void addImage(JNIEnv&, jni::String, jni::Object<Bitmap> bitmap, jni::jfloat);

void addImages(JNIEnv&, jni::Array<jni::Object<mbgl::android::Image>>);

Expand Down