Merge pull request #1469 from mapbox/1435-android-gl-crash

Android GL Crash
This commit is contained in:
Brad Leege
2015-05-11 11:42:21 -05:00
11 changed files with 161 additions and 96 deletions

View File

@@ -175,27 +175,27 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
- (void)setStyleURL:(NSURL *)styleURL
{
if (_isTargetingInterfaceBuilder) return;
if ( ! styleURL)
{
styleURL = MGLURLForBundledStyleNamed([NSString stringWithFormat:@"%@-v%@",
MGLDefaultStyleName.lowercaseString,
MGLStyleVersion]);
}
if ( ! [styleURL scheme])
{
// Assume a relative path into the developer’s bundle.
styleURL = [[NSBundle mainBundle] URLForResource:styleURL.path withExtension:nil];
}
_mbglMap->setStyleURL([[styleURL absoluteString] UTF8String]);
}
- (BOOL)commonInit
{
_isTargetingInterfaceBuilder = NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent;
// create context
//
_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
@@ -210,7 +210,7 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
// setup accessibility
//
self.accessibilityLabel = @"Map";
// metrics: initial setup
NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
NSString *appVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
@@ -234,7 +234,7 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
[self insertSubview:_glView atIndex:0];
_glView.contentMode = UIViewContentModeCenter;
self.backgroundColor = [UIColor clearColor];
self.clipsToBounds = YES;
@@ -274,7 +274,10 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
_mbglFileSource = new mbgl::DefaultFileSource([MGLFileCache obtainSharedCacheWithObject:self]);
// Start paused on the IB canvas
_mbglMap = new mbgl::Map(*_mbglView, *_mbglFileSource, mbgl::MapMode::Continuous, _isTargetingInterfaceBuilder);
_mbglMap = new mbgl::Map(*_mbglView, *_mbglFileSource, mbgl::MapMode::Continuous);
if (_isTargetingInterfaceBuilder) {
_mbglMap->pause();
}
_mbglMap->resize(self.bounds.size.width, self.bounds.size.height, _glView.contentScaleFactor);
// Notify map object when network reachability status changes.
@@ -447,7 +450,7 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
- (void)setDelegate:(id<MGLMapViewDelegate>)delegate
{
if (_delegate == delegate) return;
_delegate = delegate;
}
@@ -653,7 +656,7 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
- (void)layoutSubviews
{
[super layoutSubviews];
if ( ! _isTargetingInterfaceBuilder)
{
_mbglMap->update();
@@ -778,7 +781,7 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
_mbglMap->moveBy(delta.x, delta.y);
self.centerPoint = CGPointMake(self.centerPoint.x + delta.x, self.centerPoint.y + delta.y);
[self notifyMapChange:@(mbgl::MapChangeRegionDidChangeAnimated)];
}
else if (pan.state == UIGestureRecognizerStateEnded || pan.state == UIGestureRecognizerStateCancelled)
@@ -789,7 +792,7 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
// Not enough velocity to overcome friction
velocity = CGPointZero;
}
CGFloat duration = UIScrollViewDecelerationRateNormal;
if ( ! CGPointEqualToPoint(velocity, CGPointZero))
{
@@ -869,7 +872,7 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
velocity = 0;
}
CGFloat duration = velocity > 0 ? 1 : 0.25;
CGFloat scale = self.scale * pinch.scale;
CGFloat newScale = scale;
if (velocity >= 0)
@@ -880,22 +883,22 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
{
newScale += scale / (velocity * duration) * 0.1;
}
if (newScale <= 0 || log2(newScale) < _mbglMap->getMinZoom())
{
velocity = 0;
}
if (velocity)
{
CGPoint pinchCenter = [pinch locationInView:pinch.view];
_mbglMap->setScale(newScale, pinchCenter.x, pinchCenter.y, secondsAsDuration(duration));
}
_mbglMap->setGestureInProgress(false);
[self unrotateIfNeededAnimated:YES];
if (velocity)
{
self.animatingGesture = YES;
@@ -951,37 +954,37 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
else if (rotate.state == UIGestureRecognizerStateEnded || rotate.state == UIGestureRecognizerStateCancelled)
{
CGFloat velocity = rotate.velocity;
if (fabs(velocity) > 3)
{
CGFloat radians = self.angle + rotate.rotation;
CGFloat duration = UIScrollViewDecelerationRateNormal;
CGFloat newRadians = radians + velocity * duration * 0.1;
CGFloat newDegrees = [MGLMapView radiansToDegrees:newRadians] * -1;
_mbglMap->setBearing(newDegrees, secondsAsDuration(duration));
_mbglMap->setGestureInProgress(false);
self.animatingGesture = YES;
__weak MGLMapView *weakSelf = self;
[self animateWithDelay:duration animations:^
{
weakSelf.animatingGesture = NO;
[weakSelf unrotateIfNeededAnimated:YES];
[weakSelf notifyMapChange:@(mbgl::MapChangeRegionDidChangeAnimated)];
}];
}
else
{
_mbglMap->setGestureInProgress(false);
[self unrotateIfNeededAnimated:YES];
[self notifyMapChange:@(mbgl::MapChangeRegionDidChange)];
}
}
@@ -1444,14 +1447,14 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
// NOTE: does not disrupt tracking mode
CLLocationCoordinate2D center = CLLocationCoordinate2DMake((northEastCoordinate.latitude + southWestCoordinate.latitude) / 2, (northEastCoordinate.longitude + southWestCoordinate.longitude) / 2);
CGFloat scale = _mbglMap->getScale();
CGFloat scaleX = _mbglMap->getWidth() / (northEastCoordinate.longitude - southWestCoordinate.longitude);
CGFloat scaleY = _mbglMap->getHeight() / (northEastCoordinate.latitude - southWestCoordinate.latitude);
CGFloat minZoom = _mbglMap->getMinZoom();
CGFloat maxZoom = _mbglMap->getMaxZoom();
CGFloat zoomLevel = MAX(MIN(log(scale * MIN(scaleX, scaleY)) / log(2), maxZoom), minZoom);
[self setCenterCoordinate:center zoomLevel:zoomLevel animated:animated];
}
@@ -1723,7 +1726,7 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng)
{
assert([annotation conformsToProtocol:@protocol(MGLAnnotation)]);
annotationIDsToRemove.push_back([[[self.annotationIDsByAnnotation objectForKey:annotation]
annotationIDsToRemove.push_back([[[self.annotationIDsByAnnotation objectForKey:annotation]
objectForKey:MGLAnnotationIDKey] unsignedIntValue]);
[self.annotationIDsByAnnotation removeObjectForKey:annotation];
@@ -1759,7 +1762,7 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng)
if ( ! annotation) return;
if ( ! [self viewportBounds].contains(coordinateToLatLng(annotation.coordinate))) return;
if (annotation == self.selectedAnnotation) return;
self.userTrackingMode = MGLUserTrackingModeNone;
@@ -1878,22 +1881,22 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng)
- (void)setShowsUserLocation:(BOOL)showsUserLocation
{
if (showsUserLocation == _showsUserLocation) return;
_showsUserLocation = showsUserLocation;
if (showsUserLocation)
{
if ([self.delegate respondsToSelector:@selector(mapViewWillStartLocatingUser:)])
{
[self.delegate mapViewWillStartLocatingUser:self];
}
self.userLocationAnnotationView = [[MGLUserLocationAnnotationView alloc] initInMapView:self];
self.userLocationAnnotationView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
self.locationManager = [CLLocationManager new];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
// enable iOS 8+ location authorization API
//
@@ -1906,7 +1909,7 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng)
[self.locationManager requestWhenInUseAuthorization];
}
#endif
self.locationManager.headingFilter = 5.0;
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
@@ -1924,7 +1927,7 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng)
}
[self setUserTrackingMode:MGLUserTrackingModeNone animated:YES];
[self.userLocationAnnotationView removeFromSuperview];
self.userLocationAnnotationView = nil;
}
@@ -2089,7 +2092,7 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng)
CLLocationCoordinate2D actualNorthEast = [self convertPoint:CGPointMake(userLocationPoint.x + pixelRadius,
userLocationPoint.y + pixelRadius)
toCoordinateFromView:self];
if (desiredNorthEast.latitude != actualNorthEast.latitude ||
desiredNorthEast.longitude != actualNorthEast.longitude ||
desiredSouthWest.latitude != actualSouthWest.latitude ||
@@ -2492,17 +2495,17 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng)
- (void)prepareForInterfaceBuilder
{
[super prepareForInterfaceBuilder];
self.layer.borderColor = [UIColor colorWithWhite:184/255. alpha:1].CGColor;
self.layer.borderWidth = 1;
if (self.accessToken)
{
self.layer.backgroundColor = [UIColor colorWithRed:59/255.
green:178/255.
blue:208/255.
alpha:0.8].CGColor;
UIImage *image = [[self class] resourceImageNamed:@"mapbox.png"];
UIImageView *previewView = [[UIImageView alloc] initWithImage:image];
previewView.translatesAutoresizingMaskIntoConstraints = NO;
@@ -2529,7 +2532,7 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng)
UIView *diagnosticView = [[UIView alloc] init];
diagnosticView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:diagnosticView];
// Headline
UILabel *headlineLabel = [[UILabel alloc] init];
headlineLabel.text = @"No Access Token";
@@ -2540,7 +2543,7 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng)
[headlineLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
forAxis:UILayoutConstraintAxisHorizontal];
[diagnosticView addSubview:headlineLabel];
// Explanation
UILabel *explanationLabel = [[UILabel alloc] init];
explanationLabel.text = @"To display a map here, you must provide a Mapbox access token. Get an access token from:";
@@ -2550,7 +2553,7 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng)
[explanationLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
forAxis:UILayoutConstraintAxisHorizontal];
[diagnosticView addSubview:explanationLabel];
// Link
UIButton *linkButton = [UIButton buttonWithType:UIButtonTypeSystem];
[linkButton setTitle:MGLMapboxAccessTokenManagerURLDisplayString forState:UIControlStateNormal];
@@ -2558,7 +2561,7 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng)
[linkButton setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
forAxis:UILayoutConstraintAxisHorizontal];
[diagnosticView addSubview:linkButton];
// More explanation
UILabel *explanationLabel2 = [[UILabel alloc] init];
explanationLabel2.text = @"and enter it into the Access Token field in the Attributes inspector.";
@@ -2568,7 +2571,7 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng)
[explanationLabel2 setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
forAxis:UILayoutConstraintAxisHorizontal];
[diagnosticView addSubview:explanationLabel2];
// Constraints
NSDictionary *views = @{
@"container": diagnosticView,