diff options
Diffstat (limited to 'platform/android/src')
39 files changed, 400 insertions, 144 deletions
diff --git a/platform/android/src/connectivity_listener.cpp b/platform/android/src/connectivity_listener.cpp index df5c60a485..cc2f0a4a81 100644 --- a/platform/android/src/connectivity_listener.cpp +++ b/platform/android/src/connectivity_listener.cpp @@ -1,7 +1,7 @@ #include "connectivity_listener.hpp" #include <mbgl/storage/network_status.hpp> -#include <mbgl/platform/log.hpp> +#include <mbgl/util/logging.hpp> #include <jni/jni.hpp> diff --git a/platform/android/src/example_custom_layer.cpp b/platform/android/src/example_custom_layer.cpp index 516a8f2cd5..c55c9c3527 100644 --- a/platform/android/src/example_custom_layer.cpp +++ b/platform/android/src/example_custom_layer.cpp @@ -1,14 +1,17 @@ #include <jni.h> #include <GLES2/gl2.h> +#include <mbgl/util/logging.hpp> + #include <mbgl/style/layers/custom_layer.hpp> static const GLchar * vertexShaderSource = "attribute vec2 a_pos; void main() { gl_Position = vec4(a_pos, 0, 1); }"; -static const GLchar * fragmentShaderSource = "void main() { gl_FragColor = vec4(0, 1, 0, 1); }"; +static const GLchar * fragmentShaderSource = "uniform vec4 fill_color; void main() { gl_FragColor = fill_color; }"; class ExampleCustomLayer { public: ~ExampleCustomLayer() { + mbgl::Log::Info(mbgl::Event::General, "~ExampleCustomLayer"); if (program) { glDeleteBuffers(1, &buffer); glDetachShader(program, vertexShader); @@ -20,6 +23,7 @@ public: } void initialize() { + mbgl::Log::Info(mbgl::Event::General, "Initialize"); program = glCreateProgram(); vertexShader = glCreateShader(GL_VERTEX_SHADER); fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); @@ -32,6 +36,7 @@ public: glAttachShader(program, fragmentShader); glLinkProgram(program); a_pos = glGetAttribLocation(program, "a_pos"); + fill_color = glGetUniformLocation(program, "fill_color"); GLfloat background[] = { -1,-1, 1,-1, -1,1, 1,1 }; glGenBuffers(1, &buffer); @@ -40,12 +45,15 @@ public: } void render() { + mbgl::Log::Info(mbgl::Event::General, "Render"); glUseProgram(program); glBindBuffer(GL_ARRAY_BUFFER, buffer); glEnableVertexAttribArray(a_pos); glVertexAttribPointer(a_pos, 2, GL_FLOAT, GL_FALSE, 0, NULL); glDisable(GL_STENCIL_TEST); glDisable(GL_DEPTH_TEST); + glUniform4fv(fill_color, 1, color); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); } @@ -54,35 +62,54 @@ public: GLuint fragmentShader = 0; GLuint buffer = 0; GLuint a_pos = 0; + GLuint fill_color = 0; + + static GLfloat color[]; }; +GLfloat ExampleCustomLayer::color[] = { 0.0f, 1.0f, 0.0f, 1.0f }; + jlong JNICALL nativeCreateContext(JNIEnv*, jobject) { - return reinterpret_cast<jlong>(new ExampleCustomLayer()); + mbgl::Log::Info(mbgl::Event::General, "nativeCreateContext"); + return reinterpret_cast<jlong>(new ExampleCustomLayer()); +} + +void JNICALL nativeSetColor(JNIEnv*, jobject, jfloat red, jfloat green, jfloat blue, jfloat alpha) { + mbgl::Log::Info(mbgl::Event::General, "nativeSetColor: %.2f, %.2f, %.2f, %.2f", red, green, blue, alpha); + ExampleCustomLayer::color[0] = red; + ExampleCustomLayer::color[1] = green; + ExampleCustomLayer::color[2] = blue; + ExampleCustomLayer::color[3] = alpha; } void nativeInitialize(void *context) { + mbgl::Log::Info(mbgl::Event::General, "nativeInitialize"); reinterpret_cast<ExampleCustomLayer*>(context)->initialize(); } void nativeRender(void *context, const mbgl::style::CustomLayerRenderParameters& /*parameters*/) { + mbgl::Log::Info(mbgl::Event::General, "nativeRender"); reinterpret_cast<ExampleCustomLayer*>(context)->render(); } void nativeDenitialize(void *context) { + mbgl::Log::Info(mbgl::Event::General, "nativeDeinitialize"); delete reinterpret_cast<ExampleCustomLayer*>(context); } extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) { + mbgl::Log::Info(mbgl::Event::General, "OnLoad"); JNIEnv *env = nullptr; vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6); jclass customLayerClass = env->FindClass("com/mapbox/mapboxsdk/testapp/model/customlayer/ExampleCustomLayer"); JNINativeMethod methods[] = { - {"createContext", "()J", reinterpret_cast<void *>(&nativeCreateContext)} + {"createContext", "()J", reinterpret_cast<void *>(&nativeCreateContext)}, + {"setColor", "(FFFF)V", reinterpret_cast<void *>(&nativeSetColor)} }; - if (env->RegisterNatives(customLayerClass, methods, 1) < 0) { + if (env->RegisterNatives(customLayerClass, methods, 2) < 0) { env->ExceptionDescribe(); return JNI_ERR; } diff --git a/platform/android/src/geometry/conversion/feature.hpp b/platform/android/src/geometry/conversion/feature.hpp index 9f75b9c8f1..f0c77c3389 100644 --- a/platform/android/src/geometry/conversion/feature.hpp +++ b/platform/android/src/geometry/conversion/feature.hpp @@ -16,7 +16,7 @@ #include <vector> #include <sstream> -#include <mbgl/platform/log.hpp> +#include <mbgl/util/logging.hpp> namespace mbgl { namespace android { diff --git a/platform/android/src/http_file_source.cpp b/platform/android/src/http_file_source.cpp index ed4d81391f..ee1429bd74 100644 --- a/platform/android/src/http_file_source.cpp +++ b/platform/android/src/http_file_source.cpp @@ -1,7 +1,7 @@ #include <mbgl/storage/http_file_source.hpp> #include <mbgl/storage/resource.hpp> #include <mbgl/storage/response.hpp> -#include <mbgl/platform/log.hpp> +#include <mbgl/util/logging.hpp> #include <mbgl/util/async_task.hpp> #include <mbgl/util/util.hpp> diff --git a/platform/android/src/jni.cpp b/platform/android/src/jni.cpp index 6dcd177cd6..5182e268f3 100755 --- a/platform/android/src/jni.cpp +++ b/platform/android/src/jni.cpp @@ -25,8 +25,8 @@ #include <mbgl/style/layer.hpp> #include <mbgl/style/source.hpp> #include <mbgl/sprite/sprite_image.hpp> -#include <mbgl/platform/event.hpp> -#include <mbgl/platform/log.hpp> +#include <mbgl/util/event.hpp> +#include <mbgl/util/logging.hpp> #include <mbgl/storage/network_status.hpp> #include <mbgl/util/exception.hpp> #include <mbgl/util/optional.hpp> @@ -431,6 +431,12 @@ void nativeSetStyleUrl(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, j nativeMapView->getMap().setStyleURL(std_string_from_jstring(env, url)); } +jni::jstring* nativeGetStyleUrl(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr){ + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + return std_string_to_jstring(env, nativeMapView->getMap().getStyleURL()); +} + void nativeSetStyleJson(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jstring* newStyleJson) { assert(nativeMapViewPtr != 0); NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); @@ -471,8 +477,7 @@ void nativeMoveBy(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jdoubl jlong duration) { assert(nativeMapViewPtr != 0); NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); - mbgl::ScreenCoordinate center(dx, dy); - nativeMapView->getMap().moveBy(center, mbgl::Milliseconds(duration)); + nativeMapView->getMap().moveBy({dx, dy}, mbgl::Milliseconds(duration)); } void nativeSetLatLng(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jdouble latitude, jdouble longitude, jlong duration) { @@ -498,7 +503,7 @@ jdoubleArray nativeGetCameraValues(JNIEnv *env, jni::jobject* obj, jlong nativeM jdouble buf[5]; buf[0] = latLng.latitude; buf[1] = latLng.longitude; - buf[2] = -(nativeMapView->getMap().getBearing()-360); + buf[2] = -nativeMapView->getMap().getBearing(); buf[3] = nativeMapView->getMap().getPitch(); buf[4] = nativeMapView->getMap().getZoom(); env->SetDoubleArrayRegion(output, start, leng, buf); @@ -833,9 +838,10 @@ void nativeAddAnnotationIcon(JNIEnv *env, jni::jobject* obj, jlong nativeMapView NullCheck(*env, jpixels); std::size_t size = jni::GetArrayLength(*env, *jpixels); - mbgl::PremultipliedImage premultipliedImage(width, height); + mbgl::PremultipliedImage premultipliedImage( + { static_cast<uint32_t>(width), static_cast<uint32_t>(height) }); - if (premultipliedImage.size() != uint32_t(size)) { + if (premultipliedImage.bytes() != uint32_t(size)) { throw mbgl::util::SpriteImageException("Sprite image pixel count mismatch"); } @@ -1007,7 +1013,7 @@ void nativeJumpTo(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jdoubl mbgl::CameraOptions options; if (angle != -1) { - options.angle = angle * M_PI / 180; + options.angle = (-angle * M_PI) / 180; } options.center = mbgl::LatLng(latitude, longitude); options.padding = nativeMapView->getInsets(); @@ -1027,7 +1033,7 @@ void nativeEaseTo(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jdoubl mbgl::CameraOptions cameraOptions; if (angle != -1) { - cameraOptions.angle = angle * M_PI / 180; + cameraOptions.angle = (-angle * M_PI) / 180; } cameraOptions.center = mbgl::LatLng(latitude, longitude); cameraOptions.padding = nativeMapView->getInsets(); @@ -1060,7 +1066,7 @@ void nativeFlyTo(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jdouble mbgl::CameraOptions cameraOptions; if (angle != -1) { - cameraOptions.angle = angle * M_PI / 180 ; + cameraOptions.angle = (-angle * M_PI) / 180 ; } cameraOptions.center = mbgl::LatLng(latitude, longitude); cameraOptions.padding = nativeMapView->getInsets(); @@ -1099,15 +1105,19 @@ void nativeAddLayer(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jlon assert(nativeLayerPtr != 0); NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); - Layer *layer = reinterpret_cast<Layer *>(nativeLayerPtr); - nativeMapView->getMap().addLayer( - layer->releaseCoreLayer(), - before ? mbgl::optional<std::string>(std_string_from_jstring(env, before)) : mbgl::optional<std::string>() - ); + Layer *layer = reinterpret_cast<Layer *>(nativeLayerPtr); + try { + layer->addToMap(nativeMapView->getMap(), before ? mbgl::optional<std::string>(std_string_from_jstring(env, before)) : mbgl::optional<std::string>()); + } catch (const std::runtime_error& error) { + jni::ThrowNew(*env, jni::FindClass(*env, "com/mapbox/mapboxsdk/style/layers/CannotAddLayerException"), error.what()); + } } -void nativeRemoveLayer(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jstring* id) { +/** + * Remove by layer id. Ownership is not transferred back + */ +void nativeRemoveLayerById(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jstring* id) { assert(nativeMapViewPtr != 0); NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); try { @@ -1117,6 +1127,22 @@ void nativeRemoveLayer(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, j } } +/** + * Remove with wrapper object id. Ownership is transferred back to the wrapper + */ +void nativeRemoveLayer(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jlong layerPtr) { + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + mbgl::android::Layer *layer = reinterpret_cast<mbgl::android::Layer *>(layerPtr); + try { + std::unique_ptr<mbgl::style::Layer> coreLayer = nativeMapView->getMap().removeLayer(layer->get().getID()); + layer->setLayer(std::move(coreLayer)); + } catch (const std::runtime_error& error) { + jni::ThrowNew(*env, jni::FindClass(*env, "com/mapbox/mapboxsdk/style/layers/NoSuchLayerException"), error.what()); + } +} + + jni::jobject* nativeGetSource(JNIEnv *env, jni::jobject* obj, jni::jlong nativeMapViewPtr, jni::jstring* sourceId) { assert(env); assert(nativeMapViewPtr != 0); @@ -1140,16 +1166,32 @@ void nativeAddSource(JNIEnv *env, jni::jobject* obj, jni::jlong nativeMapViewPtr assert(nativeSourcePtr != 0); NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + Source *source = reinterpret_cast<Source *>(nativeSourcePtr); + try { + source->addToMap(nativeMapView->getMap()); + } catch (const std::runtime_error& error) { + jni::ThrowNew(*env, jni::FindClass(*env, "com/mapbox/mapboxsdk/style/sources/CannotAddSourceException"), error.what()); + } +} - nativeMapView->getMap().addSource(source->releaseCoreSource()); +void nativeRemoveSourceById(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jstring* id) { + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + try { + nativeMapView->getMap().removeSource(std_string_from_jstring(env, id)); + } catch (const std::runtime_error& error) { + jni::ThrowNew(*env, jni::FindClass(*env, "com/mapbox/mapboxsdk/style/sources/NoSuchSourceException"), error.what()); + } } -void nativeRemoveSource(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jstring* id) { +void nativeRemoveSource(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jlong sourcePtr) { assert(nativeMapViewPtr != 0); NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + mbgl::android::Source *source = reinterpret_cast<mbgl::android::Source *>(sourcePtr); try { - nativeMapView->getMap().removeSource(std_string_from_jstring(env, id)); + std::unique_ptr<mbgl::style::Source> coreSource = nativeMapView->getMap().removeSource(source->get().getID()); + source->setSource(std::move(coreSource)); } catch (const std::runtime_error& error) { jni::ThrowNew(*env, jni::FindClass(*env, "com/mapbox/mapboxsdk/style/sources/NoSuchSourceException"), error.what()); } @@ -1162,9 +1204,10 @@ void nativeAddImage(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni: // Create Pre-multiplied image from byte[] NullCheck(*env, data); std::size_t size = jni::GetArrayLength(*env, *data); - mbgl::PremultipliedImage premultipliedImage(width, height); + mbgl::PremultipliedImage premultipliedImage( + { static_cast<uint32_t>(width), static_cast<uint32_t>(height) }); - if (premultipliedImage.size() != uint32_t(size)) { + if (premultipliedImage.bytes() != uint32_t(size)) { throw mbgl::util::SpriteImageException("Sprite image pixel count mismatch"); } @@ -1666,9 +1709,12 @@ void updateOfflineRegionMetadata(JNIEnv *env, jni::jobject* offlineRegion_, jni: // Offline calls end -} +} // anonymous -extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) { +namespace mbgl { +namespace android { + +void registerNatives(JavaVM *vm) { theJVM = vm; jni::JNIEnv& env = jni::GetEnv(*vm, jni::jni_version_1_6); @@ -1776,6 +1822,7 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) { MAKE_NATIVE_METHOD(nativeSetClasses, "(JLjava/util/List;)V"), MAKE_NATIVE_METHOD(nativeGetClasses, "(J)Ljava/util/List;"), MAKE_NATIVE_METHOD(nativeSetStyleUrl, "(JLjava/lang/String;)V"), + MAKE_NATIVE_METHOD(nativeGetStyleUrl, "(J)Ljava/lang/String;"), MAKE_NATIVE_METHOD(nativeSetStyleJson, "(JLjava/lang/String;)V"), MAKE_NATIVE_METHOD(nativeGetStyleJson, "(J)Ljava/lang/String;"), MAKE_NATIVE_METHOD(nativeSetAccessToken, "(JLjava/lang/String;)V"), @@ -1831,10 +1878,12 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) { MAKE_NATIVE_METHOD(nativeFlyTo, "(JDDDJDD)V"), MAKE_NATIVE_METHOD(nativeGetLayer, "(JLjava/lang/String;)Lcom/mapbox/mapboxsdk/style/layers/Layer;"), MAKE_NATIVE_METHOD(nativeAddLayer, "(JJLjava/lang/String;)V"), - MAKE_NATIVE_METHOD(nativeRemoveLayer, "(JLjava/lang/String;)V"), + MAKE_NATIVE_METHOD(nativeRemoveLayerById, "(JLjava/lang/String;)V"), + MAKE_NATIVE_METHOD(nativeRemoveLayer, "(JJ)V"), MAKE_NATIVE_METHOD(nativeGetSource, "(JLjava/lang/String;)Lcom/mapbox/mapboxsdk/style/sources/Source;"), MAKE_NATIVE_METHOD(nativeAddSource, "(JJ)V"), - MAKE_NATIVE_METHOD(nativeRemoveSource, "(JLjava/lang/String;)V"), + MAKE_NATIVE_METHOD(nativeRemoveSourceById, "(JLjava/lang/String;)V"), + MAKE_NATIVE_METHOD(nativeRemoveSource, "(JJ)V"), MAKE_NATIVE_METHOD(nativeAddImage, "(JLjava/lang/String;IIF[B)V"), MAKE_NATIVE_METHOD(nativeRemoveImage, "(JLjava/lang/String;)V"), MAKE_NATIVE_METHOD(nativeSetContentPadding, "(JDDDD)V"), @@ -1949,6 +1998,7 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) { char release[PROP_VALUE_MAX] = ""; __system_property_get("ro.build.version.release", release); androidRelease = std::string(release); - - return JNI_VERSION_1_6; } + +} // android +} // mbgl diff --git a/platform/android/src/jni.hpp b/platform/android/src/jni.hpp index 0810ee656d..90ec914cf2 100644 --- a/platform/android/src/jni.hpp +++ b/platform/android/src/jni.hpp @@ -23,6 +23,8 @@ extern jmethodID onSnapshotReadyId; extern bool attach_jni_thread(JavaVM* vm, JNIEnv** env, std::string threadName); extern void detach_jni_thread(JavaVM* vm, JNIEnv** env, bool detach); + +extern void registerNatives(JavaVM* vm); -} -} +} //android +} //mbgl diff --git a/platform/android/src/log_android.cpp b/platform/android/src/logging_android.cpp index e5c8cfd812..2e025c059f 100644 --- a/platform/android/src/log_android.cpp +++ b/platform/android/src/logging_android.cpp @@ -1,4 +1,4 @@ -#include <mbgl/platform/log.hpp> +#include <mbgl/util/logging.hpp> #include <android/log.h> diff --git a/platform/android/src/main.cpp b/platform/android/src/main.cpp new file mode 100644 index 0000000000..03a8288719 --- /dev/null +++ b/platform/android/src/main.cpp @@ -0,0 +1,9 @@ +#include "jni.hpp" +#include <jni/jni.hpp> + +extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *) { + assert(vm != nullptr); + mbgl::android::registerNatives(vm); + return JNI_VERSION_1_6; +} + diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp index 84daf82e8b..8234b74af2 100755 --- a/platform/android/src/native_map_view.cpp +++ b/platform/android/src/native_map_view.cpp @@ -10,13 +10,10 @@ #include <sys/system_properties.h> -#include <GLES2/gl2.h> - -#include <mbgl/platform/platform.hpp> -#include <mbgl/platform/event.hpp> -#include <mbgl/platform/log.hpp> +#include <mbgl/util/platform.hpp> +#include <mbgl/util/event.hpp> +#include <mbgl/util/logging.hpp> #include <mbgl/gl/extension.hpp> -#include <mbgl/gl/gl.hpp> #include <mbgl/gl/context.hpp> #include <mbgl/util/constants.hpp> #include <mbgl/util/image.hpp> @@ -40,23 +37,6 @@ void log_egl_string(EGLDisplay display, EGLint name, const char *label) { } } -void log_gl_string(GLenum name, const char *label) { - const GLubyte *str = glGetString(name); - if (str == nullptr) { - mbgl::Log::Error(mbgl::Event::OpenGL, "glGetString(%d) returned error %d", name, - glGetError()); - throw std::runtime_error("glGetString() failed"); - } else { - char buf[513]; - for (int len = std::strlen(reinterpret_cast<const char *>(str)), pos = 0; len > 0; - len -= 512, pos += 512) { - strncpy(buf, reinterpret_cast<const char *>(str) + pos, 512); - buf[512] = 0; - mbgl::Log::Info(mbgl::Event::OpenGL, "GL %s: %s", label, buf); - } - } -} - NativeMapView::NativeMapView(JNIEnv *env_, jobject obj_, float pixelRatio, int availableProcessors_, size_t totalMemory_) : env(env_), availableProcessors(availableProcessors_), @@ -83,15 +63,14 @@ NativeMapView::NativeMapView(JNIEnv *env_, jobject obj_, float pixelRatio, int a mbgl::android::apkPath); map = std::make_unique<mbgl::Map>( - *this, - std::array<uint16_t, 2>{{ static_cast<uint16_t>(width), static_cast<uint16_t>(height) }}, + *this, mbgl::Size{ static_cast<uint32_t>(width), static_cast<uint32_t>(height) }, pixelRatio, *fileSource, threadPool, MapMode::Continuous); float zoomFactor = map->getMaxZoom() - map->getMinZoom() + 1; float cpuFactor = availableProcessors; float memoryFactor = static_cast<float>(totalMemory) / 1000.0f / 1000.0f / 1000.0f; - float sizeFactor = (static_cast<float>(map->getWidth()) / mbgl::util::tileSize) * - (static_cast<float>(map->getHeight()) / mbgl::util::tileSize); + float sizeFactor = (static_cast<float>(map->getSize().width) / mbgl::util::tileSize) * + (static_cast<float>(map->getSize().height) / mbgl::util::tileSize); size_t cacheSize = zoomFactor * cpuFactor * memoryFactor * sizeFactor * 0.5f; @@ -117,14 +96,20 @@ NativeMapView::~NativeMapView() { vm = nullptr; } +mbgl::Size NativeMapView::getFramebufferSize() const { + return { static_cast<uint32_t>(fbWidth), static_cast<uint32_t>(fbHeight) }; +} + void NativeMapView::updateViewBinding() { getContext().bindFramebuffer.setCurrentValue(0); - getContext().viewport.setCurrentValue({ 0, 0, static_cast<uint16_t>(fbWidth), static_cast<uint16_t>(fbHeight) }); + assert(mbgl::gl::value::BindFramebuffer::Get() == getContext().bindFramebuffer.getCurrentValue()); + getContext().viewport.setCurrentValue({ 0, 0, getFramebufferSize() }); + assert(mbgl::gl::value::Viewport::Get() == getContext().viewport.getCurrentValue()); } void NativeMapView::bind() { getContext().bindFramebuffer = 0; - getContext().viewport = { 0, 0, static_cast<uint16_t>(fbWidth), static_cast<uint16_t>(fbHeight) }; + getContext().viewport = { 0, 0, getFramebufferSize() }; } void NativeMapView::activate() { @@ -192,6 +177,11 @@ void NativeMapView::invalidate() { void NativeMapView::render() { BackendScope guard(*this); + if (framebufferSizeChanged) { + getContext().viewport = { 0, 0, getFramebufferSize() }; + framebufferSizeChanged = false; + } + updateViewBinding(); map->render(*this); @@ -199,18 +189,7 @@ void NativeMapView::render() { snapshot = false; // take snapshot - const unsigned int w = fbWidth; - const unsigned int h = fbHeight; - mbgl::PremultipliedImage image { static_cast<uint16_t>(w), static_cast<uint16_t>(h) }; - MBGL_CHECK_ERROR(glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, image.data.get())); - const size_t stride = image.stride(); - auto tmp = std::make_unique<uint8_t[]>(stride); - uint8_t *rgba = image.data.get(); - for (int i = 0, j = h - 1; i < j; i++, j--) { - std::memcpy(tmp.get(), rgba + i * stride, stride); - std::memcpy(rgba + i * stride, rgba + j * stride, stride); - std::memcpy(rgba + j * stride, tmp.get(), stride); - } + auto image = getContext().readFramebuffer<mbgl::PremultipliedImage>(getFramebufferSize()); // encode and convert to jbytes std::string string = encodePNG(image); @@ -241,13 +220,6 @@ mbgl::Map &NativeMapView::getMap() { return *map; } mbgl::DefaultFileSource &NativeMapView::getFileSource() { return *fileSource; } -bool NativeMapView::inEmulator() { - // Detect if we are in emulator - char prop[PROP_VALUE_MAX]; - __system_property_get("ro.kernel.qemu", prop); - return strtol(prop, nullptr, 0) == 1; -} - void NativeMapView::initializeDisplay() { mbgl::Log::Debug(mbgl::Event::Android, "NativeMapView::initializeDisplay"); @@ -277,22 +249,39 @@ void NativeMapView::initializeDisplay() { log_egl_string(display, EGL_CLIENT_APIS, "Client APIs"); log_egl_string(display, EGL_EXTENSIONS, "Client Extensions"); - // Detect if we are in emulator - if (inEmulator()) { + // Detect if we are in emulator. + const bool inEmulator = []() { + char prop[PROP_VALUE_MAX]; + __system_property_get("ro.kernel.qemu", prop); + return strtol(prop, nullptr, 0) == 1; + }(); + + if (inEmulator) { + // XXX https://code.google.com/p/android/issues/detail?id=78977 mbgl::Log::Warning(mbgl::Event::Android, "In emulator! Enabling hacks :-("); } + if (!eglBindAPI(EGL_OPENGL_ES_API)) { + mbgl::Log::Error(mbgl::Event::OpenGL, "eglBindAPI(EGL_OPENGL_ES_API) returned error %d", eglGetError()); + throw std::runtime_error("eglBindAPI() failed"); + } + // Get all configs at least RGB 565 with 16 depth and 8 stencil EGLint configAttribs[] = { - EGL_CONFIG_CAVEAT, EGL_NONE, EGL_RENDERABLE_TYPE, - EGL_OPENGL_ES2_BIT, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, - EGL_BUFFER_SIZE, 16, EGL_RED_SIZE, - 5, EGL_GREEN_SIZE, 6, - EGL_BLUE_SIZE, 5, EGL_DEPTH_SIZE, - 16, EGL_STENCIL_SIZE, 8, - (inEmulator() ? EGL_NONE : EGL_CONFORMANT), EGL_OPENGL_ES2_BIT, // Ugly hack - (inEmulator() ? EGL_NONE : EGL_COLOR_BUFFER_TYPE), EGL_RGB_BUFFER, // Ugly hack - EGL_NONE}; + EGL_CONFIG_CAVEAT, EGL_NONE, + EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, + EGL_SURFACE_TYPE, EGL_WINDOW_BIT, + EGL_BUFFER_SIZE, 16, + EGL_RED_SIZE, 5, + EGL_GREEN_SIZE, 6, + EGL_BLUE_SIZE, 5, + EGL_DEPTH_SIZE, 16, + EGL_STENCIL_SIZE, 8, + (inEmulator ? EGL_NONE : EGL_CONFORMANT), EGL_OPENGL_ES2_BIT, + (inEmulator ? EGL_NONE : EGL_COLOR_BUFFER_TYPE), EGL_RGB_BUFFER, + EGL_NONE + }; + EGLint numConfigs; if (!eglChooseConfig(display, configAttribs, nullptr, 0, &numConfigs)) { mbgl::Log::Error(mbgl::Event::OpenGL, "eglChooseConfig(NULL) returned error %d", @@ -427,16 +416,6 @@ void NativeMapView::createSurface(ANativeWindow *window_) { throw std::runtime_error("eglMakeCurrent() failed"); } - log_gl_string(GL_VENDOR, "Vendor"); - log_gl_string(GL_RENDERER, "Renderer"); - log_gl_string(GL_VERSION, "Version"); - if (!inEmulator()) { - log_gl_string(GL_SHADING_LANGUAGE_VERSION, - "SL Version"); // In the emulator this returns NULL with error code 0? - // https://code.google.com/p/android/issues/detail?id=78977 - } - - log_gl_string(GL_EXTENSIONS, "Extensions"); mbgl::gl::InitializeExtensions([] (const char * name) { return reinterpret_cast<mbgl::gl::glProc>(eglGetProcAddress(name)); }); @@ -714,12 +693,13 @@ void NativeMapView::updateFps() { void NativeMapView::resizeView(int w, int h) { width = w; height = h; - map->setSize({{ static_cast<uint16_t>(width), static_cast<uint16_t>(height) }}); + map->setSize({ static_cast<uint32_t>(width), static_cast<uint32_t>(height) }); } void NativeMapView::resizeFramebuffer(int w, int h) { fbWidth = w; fbHeight = h; + framebufferSizeChanged = true; invalidate(); } diff --git a/platform/android/src/native_map_view.hpp b/platform/android/src/native_map_view.hpp index 00cb6e591e..e7379700a9 100755 --- a/platform/android/src/native_map_view.hpp +++ b/platform/android/src/native_map_view.hpp @@ -4,7 +4,7 @@ #include <mbgl/map/view.hpp> #include <mbgl/map/backend.hpp> #include <mbgl/util/noncopyable.hpp> -#include <mbgl/platform/default/thread_pool.hpp> +#include <mbgl/util/default_thread_pool.hpp> #include <mbgl/storage/default_file_source.hpp> #include <string> @@ -20,6 +20,7 @@ public: NativeMapView(JNIEnv *env, jobject obj, float pixelRatio, int availableProcessors, size_t totalMemory); virtual ~NativeMapView(); + mbgl::Size getFramebufferSize() const; void updateViewBinding(); void bind() override; @@ -58,8 +59,6 @@ protected: private: EGLConfig chooseConfig(const EGLConfig configs[], EGLint numConfigs); - bool inEmulator(); - private: JavaVM *vm = nullptr; JNIEnv *env = nullptr; @@ -91,6 +90,7 @@ private: int height = 0; int fbWidth = 0; int fbHeight = 0; + bool framebufferSizeChanged = true; int availableProcessors = 0; size_t totalMemory = 0; diff --git a/platform/android/src/run_loop.cpp b/platform/android/src/run_loop.cpp index 1e5fc9b4ba..170b05c23c 100644 --- a/platform/android/src/run_loop.cpp +++ b/platform/android/src/run_loop.cpp @@ -1,6 +1,6 @@ #include "run_loop_impl.hpp" -#include <mbgl/platform/platform.hpp> +#include <mbgl/util/platform.hpp> #include <mbgl/util/thread_context.hpp> #include <mbgl/util/thread_local.hpp> #include <mbgl/util/timer.hpp> diff --git a/platform/android/src/style/android_conversion.hpp b/platform/android/src/style/android_conversion.hpp index cea7ce6d2a..de0ac91502 100644 --- a/platform/android/src/style/android_conversion.hpp +++ b/platform/android/src/style/android_conversion.hpp @@ -2,7 +2,7 @@ #include "value.hpp" -#include <mbgl/platform/log.hpp> +#include <mbgl/util/logging.hpp> #include <mbgl/style/conversion.hpp> #include <mbgl/util/feature.hpp> #include <mbgl/util/optional.hpp> diff --git a/platform/android/src/style/conversion/geojson.hpp b/platform/android/src/style/conversion/geojson.hpp index 920c670fcb..6bc48b3700 100644 --- a/platform/android/src/style/conversion/geojson.hpp +++ b/platform/android/src/style/conversion/geojson.hpp @@ -6,7 +6,7 @@ #include <mbgl/style/conversion.hpp> #include <mbgl/style/conversion/geojson.hpp> #include <mbgl/util/rapidjson.hpp> -#include <mbgl/platform/log.hpp> +#include <mbgl/util/logging.hpp> #include <jni/jni.hpp> #include <sstream> diff --git a/platform/android/src/style/conversion/types.hpp b/platform/android/src/style/conversion/types.hpp index d3c12ff89a..1c433bb264 100644 --- a/platform/android/src/style/conversion/types.hpp +++ b/platform/android/src/style/conversion/types.hpp @@ -1,4 +1,4 @@ -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`. +// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. #pragma once #include "types_string_values.hpp" diff --git a/platform/android/src/style/conversion/types.hpp.ejs b/platform/android/src/style/conversion/types.hpp.ejs index de26d061f7..d248d42b72 100644 --- a/platform/android/src/style/conversion/types.hpp.ejs +++ b/platform/android/src/style/conversion/types.hpp.ejs @@ -1,7 +1,7 @@ <% const properties = locals.properties; -%> -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`. +// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. #pragma once #include "types_string_values.hpp" diff --git a/platform/android/src/style/conversion/types_string_values.hpp b/platform/android/src/style/conversion/types_string_values.hpp index b42ca52acd..9f21a2fed9 100644 --- a/platform/android/src/style/conversion/types_string_values.hpp +++ b/platform/android/src/style/conversion/types_string_values.hpp @@ -1,4 +1,4 @@ -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`. +// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. #pragma once #include <mbgl/style/types.hpp> diff --git a/platform/android/src/style/conversion/types_string_values.hpp.ejs b/platform/android/src/style/conversion/types_string_values.hpp.ejs index 39021d390d..c1646baa1a 100644 --- a/platform/android/src/style/conversion/types_string_values.hpp.ejs +++ b/platform/android/src/style/conversion/types_string_values.hpp.ejs @@ -1,7 +1,7 @@ <% const properties = locals.properties; -%> -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`. +// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. #pragma once #include <mbgl/style/types.hpp> diff --git a/platform/android/src/style/layers/background_layer.cpp b/platform/android/src/style/layers/background_layer.cpp index 25526a07fa..021ac947ad 100644 --- a/platform/android/src/style/layers/background_layer.cpp +++ b/platform/android/src/style/layers/background_layer.cpp @@ -1,4 +1,4 @@ -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`. +// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. #include "background_layer.hpp" diff --git a/platform/android/src/style/layers/background_layer.hpp b/platform/android/src/style/layers/background_layer.hpp index f201213e46..bd62c024f4 100644 --- a/platform/android/src/style/layers/background_layer.hpp +++ b/platform/android/src/style/layers/background_layer.hpp @@ -1,4 +1,4 @@ -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`. +// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. #pragma once diff --git a/platform/android/src/style/layers/circle_layer.cpp b/platform/android/src/style/layers/circle_layer.cpp index c2d6ff06d1..4a6ba95d31 100644 --- a/platform/android/src/style/layers/circle_layer.cpp +++ b/platform/android/src/style/layers/circle_layer.cpp @@ -1,4 +1,4 @@ -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`. +// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. #include "circle_layer.hpp" @@ -63,6 +63,24 @@ namespace android { return jni::Object<jni::ObjectTag>(*converted); } + jni::Object<jni::ObjectTag> CircleLayer::getCircleStrokeWidth(jni::JNIEnv& env) { + using namespace mbgl::android::conversion; + Result<jni::jobject*> converted = convert<jni::jobject*>(env, layer.as<mbgl::style::CircleLayer>()->CircleLayer::getCircleStrokeWidth()); + return jni::Object<jni::ObjectTag>(*converted); + } + + jni::Object<jni::ObjectTag> CircleLayer::getCircleStrokeColor(jni::JNIEnv& env) { + using namespace mbgl::android::conversion; + Result<jni::jobject*> converted = convert<jni::jobject*>(env, layer.as<mbgl::style::CircleLayer>()->CircleLayer::getCircleStrokeColor()); + return jni::Object<jni::ObjectTag>(*converted); + } + + jni::Object<jni::ObjectTag> CircleLayer::getCircleStrokeOpacity(jni::JNIEnv& env) { + using namespace mbgl::android::conversion; + Result<jni::jobject*> converted = convert<jni::jobject*>(env, layer.as<mbgl::style::CircleLayer>()->CircleLayer::getCircleStrokeOpacity()); + return jni::Object<jni::ObjectTag>(*converted); + } + jni::Class<CircleLayer> CircleLayer::javaClass; jni::jobject* CircleLayer::createJavaPeer(jni::JNIEnv& env) { @@ -88,7 +106,10 @@ namespace android { METHOD(&CircleLayer::getCircleOpacity, "nativeGetCircleOpacity"), METHOD(&CircleLayer::getCircleTranslate, "nativeGetCircleTranslate"), METHOD(&CircleLayer::getCircleTranslateAnchor, "nativeGetCircleTranslateAnchor"), - METHOD(&CircleLayer::getCirclePitchScale, "nativeGetCirclePitchScale")); + METHOD(&CircleLayer::getCirclePitchScale, "nativeGetCirclePitchScale"), + METHOD(&CircleLayer::getCircleStrokeWidth, "nativeGetCircleStrokeWidth"), + METHOD(&CircleLayer::getCircleStrokeColor, "nativeGetCircleStrokeColor"), + METHOD(&CircleLayer::getCircleStrokeOpacity, "nativeGetCircleStrokeOpacity")); } } // namespace android diff --git a/platform/android/src/style/layers/circle_layer.hpp b/platform/android/src/style/layers/circle_layer.hpp index 91c99c686e..d45984f23b 100644 --- a/platform/android/src/style/layers/circle_layer.hpp +++ b/platform/android/src/style/layers/circle_layer.hpp @@ -1,4 +1,4 @@ -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`. +// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. #pragma once @@ -39,6 +39,12 @@ public: jni::Object<jni::ObjectTag> getCirclePitchScale(jni::JNIEnv&); + jni::Object<jni::ObjectTag> getCircleStrokeWidth(jni::JNIEnv&); + + jni::Object<jni::ObjectTag> getCircleStrokeColor(jni::JNIEnv&); + + jni::Object<jni::ObjectTag> getCircleStrokeOpacity(jni::JNIEnv&); + jni::jobject* createJavaPeer(jni::JNIEnv&); }; // class CircleLayer diff --git a/platform/android/src/style/layers/custom_layer.cpp b/platform/android/src/style/layers/custom_layer.cpp index 6568455c67..d5d330a019 100644 --- a/platform/android/src/style/layers/custom_layer.cpp +++ b/platform/android/src/style/layers/custom_layer.cpp @@ -2,7 +2,7 @@ #include <string> -#include <mbgl/platform/log.hpp> +#include <mbgl/util/logging.hpp> namespace mbgl { namespace android { diff --git a/platform/android/src/style/layers/fill_layer.cpp b/platform/android/src/style/layers/fill_layer.cpp index 8cb96c9cd3..84d47b6afe 100644 --- a/platform/android/src/style/layers/fill_layer.cpp +++ b/platform/android/src/style/layers/fill_layer.cpp @@ -1,4 +1,4 @@ -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`. +// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. #include "fill_layer.hpp" diff --git a/platform/android/src/style/layers/fill_layer.hpp b/platform/android/src/style/layers/fill_layer.hpp index 5dbd7a3412..7609a5742f 100644 --- a/platform/android/src/style/layers/fill_layer.hpp +++ b/platform/android/src/style/layers/fill_layer.hpp @@ -1,4 +1,4 @@ -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`. +// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. #pragma once diff --git a/platform/android/src/style/layers/layer.cpp b/platform/android/src/style/layers/layer.cpp index 3c1fc0af62..c0c57c839d 100644 --- a/platform/android/src/style/layers/layer.cpp +++ b/platform/android/src/style/layers/layer.cpp @@ -3,7 +3,7 @@ #include <jni/jni.hpp> -#include <mbgl/platform/log.hpp> +#include <mbgl/util/logging.hpp> //Java -> C++ conversion #include <mbgl/style/conversion.hpp> @@ -32,8 +32,21 @@ namespace android { Layer::~Layer() { } - jni::String Layer::getId(jni::JNIEnv& env) { - return jni::Make<jni::String>(env, layer.getID()); + void Layer::addToMap(mbgl::Map& _map, mbgl::optional<std::string> before) { + //Check to see if we own the layer first + if (!ownedLayer) { + throw std::runtime_error("Cannot add layer twice"); + } + + //Add layer to map + _map.addLayer(releaseCoreLayer(), before); + + //Save pointer to the map + this->map = &_map; + } + + void Layer::setLayer(std::unique_ptr<mbgl::style::Layer> sourceLayer) { + this->ownedLayer = std::move(sourceLayer); } std::unique_ptr<mbgl::style::Layer> Layer::releaseCoreLayer() { @@ -41,6 +54,14 @@ namespace android { return std::move(ownedLayer); } + jni::String Layer::getId(jni::JNIEnv& env) { + return jni::Make<jni::String>(env, layer.getID()); + } + + style::Layer& Layer::get() { + return layer; + } + void Layer::setLayoutProperty(jni::JNIEnv& env, jni::String jname, jni::Object<> jvalue) { Value value(env, jvalue); diff --git a/platform/android/src/style/layers/layer.cpp.ejs b/platform/android/src/style/layers/layer.cpp.ejs index 68dd27b801..500c76ea7a 100644 --- a/platform/android/src/style/layers/layer.cpp.ejs +++ b/platform/android/src/style/layers/layer.cpp.ejs @@ -2,9 +2,9 @@ const type = locals.type; const properties = locals.properties; -%> -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`. +// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. -#include "<%- type %>_layer.hpp" +#include "<%- type.replace('-', '_') %>_layer.hpp" #include <string> diff --git a/platform/android/src/style/layers/layer.hpp b/platform/android/src/style/layers/layer.hpp index 024a3c38b7..f3cd073552 100644 --- a/platform/android/src/style/layers/layer.hpp +++ b/platform/android/src/style/layers/layer.hpp @@ -34,10 +34,16 @@ public: virtual jni::jobject* createJavaPeer(jni::JNIEnv&) = 0; + /** + * Set core layer (ie return ownership after remove) + */ + void setLayer(std::unique_ptr<mbgl::style::Layer>); + + void addToMap(mbgl::Map&, mbgl::optional<std::string>); + jni::String getId(jni::JNIEnv&); - //Release the owned view and return it - std::unique_ptr<mbgl::style::Layer> releaseCoreLayer(); + style::Layer& get(); void setLayoutProperty(jni::JNIEnv&, jni::String, jni::Object<> value); @@ -64,8 +70,16 @@ public: jni::Object<jni::ObjectTag> getVisibility(jni::JNIEnv&); protected: + //Release the owned view and return it + std::unique_ptr<mbgl::style::Layer> releaseCoreLayer(); + + //Owned layer is set when creating a new layer, before adding it to the map std::unique_ptr<mbgl::style::Layer> ownedLayer; + + //Raw reference to the layer mbgl::style::Layer& layer; + + //Map is set when the layer is retrieved or after adding to the map mbgl::Map* map; }; diff --git a/platform/android/src/style/layers/layer.hpp.ejs b/platform/android/src/style/layers/layer.hpp.ejs index 826e133fca..3d715746ff 100644 --- a/platform/android/src/style/layers/layer.hpp.ejs +++ b/platform/android/src/style/layers/layer.hpp.ejs @@ -2,12 +2,12 @@ const type = locals.type; const properties = locals.properties; -%> -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`. +// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. #pragma once #include "layer.hpp" -#include <mbgl/style/layers/<%- type %>_layer.hpp> +#include <mbgl/style/layers/<%- type.replace('-', '_') %>_layer.hpp> #include <jni/jni.hpp> namespace mbgl { diff --git a/platform/android/src/style/layers/line_layer.cpp b/platform/android/src/style/layers/line_layer.cpp index 91d3e4a1cd..2dce8b618a 100644 --- a/platform/android/src/style/layers/line_layer.cpp +++ b/platform/android/src/style/layers/line_layer.cpp @@ -1,4 +1,4 @@ -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`. +// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. #include "line_layer.hpp" diff --git a/platform/android/src/style/layers/line_layer.hpp b/platform/android/src/style/layers/line_layer.hpp index da9b564325..e2fc93329e 100644 --- a/platform/android/src/style/layers/line_layer.hpp +++ b/platform/android/src/style/layers/line_layer.hpp @@ -1,4 +1,4 @@ -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`. +// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. #pragma once diff --git a/platform/android/src/style/layers/raster_layer.cpp b/platform/android/src/style/layers/raster_layer.cpp index da78ccb8e3..25b26155ae 100644 --- a/platform/android/src/style/layers/raster_layer.cpp +++ b/platform/android/src/style/layers/raster_layer.cpp @@ -1,4 +1,4 @@ -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`. +// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. #include "raster_layer.hpp" diff --git a/platform/android/src/style/layers/raster_layer.hpp b/platform/android/src/style/layers/raster_layer.hpp index e59cd05f87..3cc2d96dde 100644 --- a/platform/android/src/style/layers/raster_layer.hpp +++ b/platform/android/src/style/layers/raster_layer.hpp @@ -1,4 +1,4 @@ -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`. +// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. #pragma once diff --git a/platform/android/src/style/layers/symbol_layer.cpp b/platform/android/src/style/layers/symbol_layer.cpp index fd69b6591f..9318d42d5b 100644 --- a/platform/android/src/style/layers/symbol_layer.cpp +++ b/platform/android/src/style/layers/symbol_layer.cpp @@ -1,4 +1,4 @@ -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`. +// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. #include "symbol_layer.hpp" diff --git a/platform/android/src/style/layers/symbol_layer.hpp b/platform/android/src/style/layers/symbol_layer.hpp index f5165327bf..1048b01b14 100644 --- a/platform/android/src/style/layers/symbol_layer.hpp +++ b/platform/android/src/style/layers/symbol_layer.hpp @@ -1,4 +1,4 @@ -// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make style-code-android`. +// This file is generated. Edit android/platform/scripts/generate-style-code.js, then run `make android-style-code`. #pragma once diff --git a/platform/android/src/style/sources/source.cpp b/platform/android/src/style/sources/source.cpp index f3daa777d1..aca7bd6a84 100644 --- a/platform/android/src/style/sources/source.cpp +++ b/platform/android/src/style/sources/source.cpp @@ -3,7 +3,7 @@ #include <jni/jni.hpp> -#include <mbgl/platform/log.hpp> +#include <mbgl/util/logging.hpp> //Java -> C++ conversion #include <mbgl/style/conversion.hpp> @@ -31,10 +31,31 @@ namespace android { Source::~Source() { } + style::Source& Source::get() { + return source; + } + + void Source::setSource(std::unique_ptr<style::Source> coreSource) { + this->ownedSource = std::move(coreSource); + } + jni::String Source::getId(jni::JNIEnv& env) { return jni::Make<jni::String>(env, source.getID()); } + void Source::addToMap(mbgl::Map& _map) { + //Check to see if we own the source first + if (!ownedSource) { + throw std::runtime_error("Cannot add source twice"); + } + + //Add source to map + _map.addSource(releaseCoreSource()); + + //Save pointer to the map + this->map = &_map; + } + std::unique_ptr<mbgl::style::Source> Source::releaseCoreSource() { assert(ownedSource != nullptr); return std::move(ownedSource); diff --git a/platform/android/src/style/sources/source.hpp b/platform/android/src/style/sources/source.hpp index 0785e4d4e0..0e5d354d93 100644 --- a/platform/android/src/style/sources/source.hpp +++ b/platform/android/src/style/sources/source.hpp @@ -32,16 +32,30 @@ public: virtual ~Source(); + /** + * Set core source (ie return ownership after remove) + */ + void setSource(std::unique_ptr<style::Source>); + + style::Source& get(); + + void addToMap(mbgl::Map&); + virtual jni::jobject* createJavaPeer(jni::JNIEnv&) = 0; jni::String getId(jni::JNIEnv&); +protected: //Release the owned view and return it std::unique_ptr<mbgl::style::Source> releaseCoreSource(); -protected: + //Set on newly created sources until added to the map std::unique_ptr<mbgl::style::Source> ownedSource; + + //Raw pointer that is valid until the source is removed from the map mbgl::style::Source& source; + + //Map pointer is valid for newly created sources only after adding to the map mbgl::Map* map; }; diff --git a/platform/android/src/test/Main.java b/platform/android/src/test/Main.java new file mode 100644 index 0000000000..2abcf2bfdb --- /dev/null +++ b/platform/android/src/test/Main.java @@ -0,0 +1,15 @@ + +public class Main { + public native void runAllTests(); + + public static void main(String[] args) throws Exception { + //Load the tests + System.loadLibrary("mbgl-test"); + + //Run the tests + new Main().runAllTests(); + + //Exit explicitly otherwise dalvikvm won't quit + System.exit(0); + } +} diff --git a/platform/android/src/test/main.jni.cpp b/platform/android/src/test/main.jni.cpp new file mode 100644 index 0000000000..d37b908202 --- /dev/null +++ b/platform/android/src/test/main.jni.cpp @@ -0,0 +1,76 @@ +#include "../jni.hpp" + +#include <android/log.h> +#include <jni/jni.hpp> + +#include <mbgl/util/logging.hpp> +#include <mbgl/test.hpp> + +#pragma clang diagnostic ignored "-Wunused-parameter" + +#define MAKE_NATIVE_METHOD(name, sig) jni::MakeNativeMethod<decltype(name), name>( #name, sig ) + +namespace { + +/** + * JNI Bound to Main#runAllTests() + */ +void runAllTests(JNIEnv *env, jni::jobject* obj) { + mbgl::Log::Info(mbgl::Event::JNI, "Starting tests"); + mbgl::runTests(0, nullptr); +} + +} + +// JNI Bindings to stub the android.util.Log implementation + +static jboolean isLoggable(JNIEnv* env, jni::jobject* clazz, jni::jstring* tag, jint level) { + return true; +} + +static jint println_native(JNIEnv* env, jni::jobject* clazz, jint bufID, jint priority, jni::jstring* jtag, jni::jstring* jmessage) { + if (jtag == nullptr || jmessage == nullptr) { + return false; + } + + std::string tag = jni::Make<std::string>(*env, jni::String(jtag)); + std::string message = jni::Make<std::string>(*env, jni::String(jmessage)); + + return __android_log_print(priority, tag.c_str(), "%s", message.c_str()); +} + +static jint logger_entry_max_payload_native(JNIEnv* env, jni::jobject* clazz) { + return static_cast<jint>(4068); +} + + +// Main entry point + +extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) { + //Load the main library jni bindings + mbgl::Log::Info(mbgl::Event::JNI, "Registering main JNI Methods"); + mbgl::android::registerNatives(vm); + + //Load the test library jni bindings + mbgl::Log::Info(mbgl::Event::JNI, "Registering test JNI Methods"); + + jni::JNIEnv& env = jni::GetEnv(*vm, jni::jni_version_1_6); + + //Main class (entry point for tests from dalvikvm) + struct Main { static constexpr auto Name() { return "Main"; } }; + jni::RegisterNatives(env, jni::Class<Main>::Find(env), MAKE_NATIVE_METHOD(runAllTests, "()V")); + + //Bindings for system classes + struct Log { static constexpr auto Name() { return "android/util/Log"; } }; + try { + jni::RegisterNatives(env, jni::Class<Log>::Find(env), + MAKE_NATIVE_METHOD(isLoggable, "(Ljava/lang/String;I)Z"), + MAKE_NATIVE_METHOD(logger_entry_max_payload_native, "()I"), + MAKE_NATIVE_METHOD(println_native, "(IILjava/lang/String;Ljava/lang/String;)I") + ); + } catch (jni::PendingJavaException ex) { + env.ThrowNew(jni::JavaErrorClass(env), "Could not register Log mocks"); + } + + return JNI_VERSION_1_6; +} diff --git a/platform/android/src/thread.cpp b/platform/android/src/thread.cpp index 77f9815866..c708dfdceb 100644 --- a/platform/android/src/thread.cpp +++ b/platform/android/src/thread.cpp @@ -1,5 +1,5 @@ -#include <mbgl/platform/log.hpp> -#include <mbgl/platform/platform.hpp> +#include <mbgl/util/logging.hpp> +#include <mbgl/util/platform.hpp> #include <sys/prctl.h> #include <sys/resource.h> |