diff options
Diffstat (limited to 'platform/android/src')
-rwxr-xr-x | platform/android/src/jni.cpp | 1 | ||||
-rw-r--r-- | platform/android/src/map/image.cpp | 44 | ||||
-rw-r--r-- | platform/android/src/map/image.hpp | 26 | ||||
-rwxr-xr-x | platform/android/src/native_map_view.cpp | 14 | ||||
-rwxr-xr-x | platform/android/src/native_map_view.hpp | 3 |
5 files changed, 88 insertions, 0 deletions
diff --git a/platform/android/src/jni.cpp b/platform/android/src/jni.cpp index 6acb6a3664..f6ddb8b6ee 100755 --- a/platform/android/src/jni.cpp +++ b/platform/android/src/jni.cpp @@ -171,6 +171,7 @@ void registerNatives(JavaVM *vm) { // Map CameraPosition::registerNative(env); + Image::registerNative(env); // Connectivity ConnectivityListener::registerNative(env); diff --git a/platform/android/src/map/image.cpp b/platform/android/src/map/image.cpp new file mode 100644 index 0000000000..5f5c90eddd --- /dev/null +++ b/platform/android/src/map/image.cpp @@ -0,0 +1,44 @@ +#include <mbgl/style/image.hpp> +#include <mbgl/util/exception.hpp> +#include "image.hpp" + +namespace mbgl { +namespace android { + +mbgl::style::Image Image::getImage(jni::JNIEnv& env, jni::Object<Image> image) { + static auto widthField = Image::javaClass.GetField<jni::jint>(env, "width"); + static auto heightField = Image::javaClass.GetField<jni::jint>(env, "height"); + static auto pixelRatioField = Image::javaClass.GetField<jni::jfloat>(env, "pixelRatio"); + static auto bufferField = Image::javaClass.GetField<jni::Array<jbyte>>(env, "buffer"); + static auto nameField = Image::javaClass.GetField<jni::String>(env, "name"); + + auto height = image.Get(env, heightField); + auto width = image.Get(env, widthField); + auto pixelRatio = image.Get(env, pixelRatioField); + auto pixels = image.Get(env, bufferField); + auto name = jni::Make<std::string>(env, image.Get(env, nameField)); + + jni::NullCheck(env, &pixels); + std::size_t size = pixels.Length(env); + + mbgl::PremultipliedImage premultipliedImage({ static_cast<uint32_t>(width), static_cast<uint32_t>(height) }); + 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())); + + return mbgl::style::Image {name, std::move(premultipliedImage), pixelRatio}; +} + +void Image::registerNative(jni::JNIEnv &env) { + // Lookup the class + Image::javaClass = *jni::Class<Image>::Find(env).NewGlobalRef(env).release(); +} + +jni::Class<Image> Image::javaClass; + + +} // namespace android +} // namespace mb + diff --git a/platform/android/src/map/image.hpp b/platform/android/src/map/image.hpp new file mode 100644 index 0000000000..1513e13ee7 --- /dev/null +++ b/platform/android/src/map/image.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include <mbgl/util/noncopyable.hpp> + +#include <jni/jni.hpp> +#include <mbgl/style/image.hpp> + +namespace mbgl { +namespace android { + +class Image : private mbgl::util::noncopyable { +public: + + static constexpr auto Name() { return "com/mapbox/mapboxsdk/maps/Image"; }; + + static mbgl::style::Image getImage(jni::JNIEnv&, jni::Object<Image>); + + static jni::Class<Image> javaClass; + + static void registerNative(jni::JNIEnv&); + +}; + + +} // namespace android +} // namespace mbgl
\ No newline at end of file diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp index 7372ecdf71..04cbb21927 100755 --- a/platform/android/src/native_map_view.cpp +++ b/platform/android/src/native_map_view.cpp @@ -49,6 +49,7 @@ #include "java/util.hpp" #include "geometry/lat_lng_bounds.hpp" #include "map/camera_position.hpp" +#include "map/image.hpp" #include "style/light.hpp" #include "bitmap_factory.hpp" @@ -924,6 +925,18 @@ void NativeMapView::addImage(JNIEnv& env, jni::String name, jni::jint w, jni::ji float(scale))); } +void NativeMapView::addImages(JNIEnv& env, jni::Array<jni::Object<mbgl::android::Image>> jimages) { + jni::NullCheck(env, &jimages); + std::size_t len = jimages.Length(env); + + for (std::size_t i = 0; i < len; i++) { + jni::Object<mbgl::android::Image> jimage = jimages.Get(env, i); + auto image = mbgl::android::Image::getImage(env, jimage); + map->getStyle().addImage(std::make_unique<mbgl::style::Image>(image)); + jni::DeleteLocalRef(env, jimage); + } +} + void NativeMapView::removeImage(JNIEnv& env, jni::String name) { map->getStyle().removeImage(jni::Make<std::string>(env, name)); } @@ -1038,6 +1051,7 @@ void NativeMapView::registerNative(jni::JNIEnv& env) { METHOD(&NativeMapView::removeSourceById, "nativeRemoveSourceById"), METHOD(&NativeMapView::removeSource, "nativeRemoveSource"), METHOD(&NativeMapView::addImage, "nativeAddImage"), + METHOD(&NativeMapView::addImages, "nativeAddImages"), METHOD(&NativeMapView::removeImage, "nativeRemoveImage"), METHOD(&NativeMapView::getImage, "nativeGetImage"), METHOD(&NativeMapView::setLatLngBounds, "nativeSetLatLngBounds"), diff --git a/platform/android/src/native_map_view.hpp b/platform/android/src/native_map_view.hpp index 11b12a9f6f..d7e3b17b99 100755 --- a/platform/android/src/native_map_view.hpp +++ b/platform/android/src/native_map_view.hpp @@ -22,6 +22,7 @@ #include "style/sources/sources.hpp" #include "geometry/lat_lng_bounds.hpp" #include "map/camera_position.hpp" +#include "map/image.hpp" #include "style/light.hpp" #include "bitmap.hpp" @@ -237,6 +238,8 @@ public: void addImage(JNIEnv&, jni::String, jni::jint, jni::jint, jni::jfloat, jni::Array<jbyte>); + void addImages(JNIEnv&, jni::Array<jni::Object<mbgl::android::Image>>); + void removeImage(JNIEnv&, jni::String); jni::Object<Bitmap> getImage(JNIEnv&, jni::String); |