diff --git a/platform/darwin/bazel/files.bzl b/platform/darwin/bazel/files.bzl index 2866f4a25..872b0352b 100644 --- a/platform/darwin/bazel/files.bzl +++ b/platform/darwin/bazel/files.bzl @@ -63,6 +63,7 @@ MLN_DARWIN_OBJC_HEADERS = [ "src/MLNCustomStyleLayer.h", "src/MLNCustomDrawableStyleLayer.h", "src/MLNDefaultStyle.h", + "src/MLNDisplayUtils.h", "src/MLNDistanceFormatter.h", "src/MLNFeature.h", "src/MLNForegroundStyleLayer.h", @@ -175,6 +176,7 @@ MLN_DARWIN_PUBLIC_OBJCPP_SOURCE = [ "src/MLNComputedShapeSource.mm", "src/MLNCustomStyleLayer.mm", "src/MLNDefaultStyle.mm", + "src/MLNDisplayUtils.mm", "src/MLNFeature.mm", "src/MLNForegroundStyleLayer.mm", "src/MLNFoundation.mm", diff --git a/platform/darwin/src/MLNDisplayUtils.h b/platform/darwin/src/MLNDisplayUtils.h new file mode 100644 index 000000000..2b22518d9 --- /dev/null +++ b/platform/darwin/src/MLNDisplayUtils.h @@ -0,0 +1,10 @@ +#import +#import + +#if TARGET_OS_IPHONE || TARGET_OS_TV +#import +#else +#import +#endif + +FOUNDATION_EXTERN CGFloat MLNEffectiveScaleFactorForView(id viewOrNil); diff --git a/platform/darwin/src/MLNDisplayUtils.mm b/platform/darwin/src/MLNDisplayUtils.mm new file mode 100644 index 000000000..fb4a50310 --- /dev/null +++ b/platform/darwin/src/MLNDisplayUtils.mm @@ -0,0 +1,27 @@ +#import "MLNDisplayUtils.h" + +CGFloat MLNEffectiveScaleFactorForView(id viewOrNil) { +#if TARGET_OS_IPHONE || TARGET_OS_TV + + UIView *view = (UIView *)viewOrNil; + UIScreen *screen = view.window.screen ?: [UIScreen mainScreen]; + + CGFloat logicalToPixelRatio = screen.nativeBounds.size.width / screen.bounds.size.width; + if (logicalToPixelRatio > 0.0) { + return logicalToPixelRatio; + } + + return [screen respondsToSelector:@selector(nativeScale)] ? screen.nativeScale : screen.scale; + +#elif TARGET_OS_OSX + + NSView *view = (NSView *)viewOrNil; + NSScreen *screen = view ? (view.window.screen ?: [NSScreen mainScreen]) : [NSScreen mainScreen]; + return screen ? screen.backingScaleFactor : 1.0; + +#else + + return 1.0; + +#endif +} diff --git a/platform/ios/src/MLNMapView+Metal.mm b/platform/ios/src/MLNMapView+Metal.mm index 976b54717..d17bd09be 100644 --- a/platform/ios/src/MLNMapView+Metal.mm +++ b/platform/ios/src/MLNMapView+Metal.mm @@ -1,3 +1,4 @@ +#import "MLNDisplayUtils.h" #import "MLNFoundation_Private.h" #import "MLNLoggingConfiguration_Private.h" #import "MLNMapView+Metal.h" @@ -32,14 +33,6 @@ @end -namespace { -CGFloat contentScaleFactor() { - return [UIScreen instancesRespondToSelector:@selector(nativeScale)] - ? [[UIScreen mainScreen] nativeScale] - : [[UIScreen mainScreen] scale]; -} -} // namespace - class MLNMapViewMetalRenderableResource final : public mbgl::mtl::RenderableResource { public: MLNMapViewMetalRenderableResource(MLNMapViewMetalImpl& backend_) @@ -164,11 +157,12 @@ void MLNMapViewMetalImpl::createView() { } id device = (__bridge id)resource.getBackend().getDevice().get(); + const auto scaleFactor = MLNEffectiveScaleFactorForView(resource.mtlView); resource.mtlView = [[MTKView alloc] initWithFrame:mapView.bounds device:device]; resource.mtlView.delegate = resource.delegate; resource.mtlView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; - resource.mtlView.contentScaleFactor = contentScaleFactor(); + resource.mtlView.contentScaleFactor = scaleFactor; resource.mtlView.contentMode = UIViewContentModeCenter; resource.mtlView.colorPixelFormat = MTLPixelFormatBGRA8Unorm; resource.mtlView.depthStencilPixelFormat = MTLPixelFormatDepth32Float_Stencil8; @@ -222,9 +216,10 @@ UIImage* MLNMapViewMetalImpl::snapshot() { } void MLNMapViewMetalImpl::layoutChanged() { - const auto scaleFactor = contentScaleFactor(); - size = { static_cast(mapView.bounds.size.width * scaleFactor), - static_cast(mapView.bounds.size.height * scaleFactor) }; + const auto scaleFactor = MLNEffectiveScaleFactorForView(mapView); + const auto screenSize = mapView.window.screen.nativeBounds.size; + size = { static_cast(screenSize.width * scaleFactor), + static_cast(screenSize.height * scaleFactor) }; } MLNBackendResource* MLNMapViewMetalImpl::getObject() {