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

Commit

Permalink
[ios, osx] Avoid recomputing tile cache size on every redraw
Browse files Browse the repository at this point in the history
The tile cache size is only recomputed when an input changes.

Fixes #4026.
  • Loading branch information
1ec5 committed May 6, 2016
1 parent 8a7087a commit d28b6f3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 21 deletions.
40 changes: 28 additions & 12 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ - (void)commonInit
// setup mbgl map
mbgl::DefaultFileSource *mbglFileSource = [MGLOfflineStorage sharedOfflineStorage].mbglFileSource;
_mbglMap = new mbgl::Map(*_mbglView, *mbglFileSource, mbgl::MapMode::Continuous, mbgl::GLContextMode::Unique, mbgl::ConstrainMode::None);
[self validateTileCacheSize];

// start paused if in IB
if (_isTargetingInterfaceBuilder || background) {
Expand Down Expand Up @@ -646,15 +647,37 @@ - (void)didReceiveMemoryWarning
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];

[self setNeedsLayout];
if ( ! CGRectEqualToRect(frame, self.frame))
{
[self validateTileCacheSize];
}
}

- (void)setBounds:(CGRect)bounds
{
[super setBounds:bounds];
if ( ! CGRectEqualToRect(bounds, self.bounds))
{
[self validateTileCacheSize];
}
}

[self setNeedsLayout];
- (void)validateTileCacheSize
{
if ( ! _mbglMap)
{
return;
}

CGFloat zoomFactor = self.maximumZoomLevel - self.minimumZoomLevel + 1;
CGFloat cpuFactor = [NSProcessInfo processInfo].processorCount;
CGFloat memoryFactor = (CGFloat)[NSProcessInfo processInfo].physicalMemory / 1000 / 1000 / 1000;
CGFloat sizeFactor = (CGRectGetWidth(self.bounds) / mbgl::util::tileSize) *
(CGRectGetHeight(self.bounds) / mbgl::util::tileSize);

NSUInteger cacheSize = zoomFactor * cpuFactor * memoryFactor * sizeFactor * 0.5;

_mbglMap->setSourceTileCacheSize(cacheSize);
}

+ (BOOL)requiresConstraintBasedLayout
Expand Down Expand Up @@ -830,15 +853,6 @@ - (void)glkView:(__unused GLKView *)view drawInRect:(__unused CGRect)rect
{
if ( ! self.dormant)
{
CGFloat zoomFactor = _mbglMap->getMaxZoom() - _mbglMap->getMinZoom() + 1;
CGFloat cpuFactor = (CGFloat)[[NSProcessInfo processInfo] processorCount];
CGFloat memoryFactor = (CGFloat)[[NSProcessInfo processInfo] physicalMemory] / 1000 / 1000 / 1000;
CGFloat sizeFactor = ((CGFloat)_mbglMap->getWidth() / mbgl::util::tileSize) *
((CGFloat)_mbglMap->getHeight() / mbgl::util::tileSize);

NSUInteger cacheSize = zoomFactor * cpuFactor * memoryFactor * sizeFactor * 0.5;

_mbglMap->setSourceTileCacheSize(cacheSize);
_mbglMap->render();

[self updateUserLocationAnnotationView];
Expand Down Expand Up @@ -2175,6 +2189,7 @@ - (void)setZoomLevel:(double)zoomLevel animated:(BOOL)animated
- (void)setMinimumZoomLevel:(double)minimumZoomLevel
{
_mbglMap->setMinZoom(minimumZoomLevel);
[self validateTileCacheSize];
}

- (double)minimumZoomLevel
Expand All @@ -2185,6 +2200,7 @@ - (double)minimumZoomLevel
- (void)setMaximumZoomLevel:(double)maximumZoomLevel
{
_mbglMap->setMaxZoom(maximumZoomLevel);
[self validateTileCacheSize];
}

- (double)maximumZoomLevel
Expand Down
30 changes: 21 additions & 9 deletions platform/osx/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ - (void)commonInit {

mbgl::DefaultFileSource *mbglFileSource = [MGLOfflineStorage sharedOfflineStorage].mbglFileSource;
_mbglMap = new mbgl::Map(*_mbglView, *mbglFileSource, mbgl::MapMode::Continuous, mbgl::GLContextMode::Unique, mbgl::ConstrainMode::None);
[self validateTileCacheSize];

// Install the OpenGL layer. Interface Builder’s synchronous drawing means
// we can’t display a map, so don’t even bother to have a map layer.
Expand Down Expand Up @@ -607,6 +608,9 @@ - (BOOL)wantsBestResolutionOpenGLSurface {

- (void)setFrame:(NSRect)frame {
super.frame = frame;
if (!NSEqualRects(frame, self.frame)) {
[self validateTileCacheSize];
}
if (!_isTargetingInterfaceBuilder) {
_mbglMap->update(mbgl::Update::Dimensions);
}
Expand Down Expand Up @@ -705,15 +709,6 @@ - (void)updateConstraints {

- (void)renderSync {
if (!self.dormant) {
CGFloat zoomFactor = _mbglMap->getMaxZoom() - _mbglMap->getMinZoom() + 1;
CGFloat cpuFactor = (CGFloat)[NSProcessInfo processInfo].processorCount;
CGFloat memoryFactor = (CGFloat)[NSProcessInfo processInfo].physicalMemory / 1000 / 1000 / 1000;
CGFloat sizeFactor = ((CGFloat)_mbglMap->getWidth() / mbgl::util::tileSize) * ((CGFloat)_mbglMap->getHeight() / mbgl::util::tileSize);

NSUInteger cacheSize = zoomFactor * cpuFactor * memoryFactor * sizeFactor * 0.5;

_mbglMap->setSourceTileCacheSize(cacheSize);

// Enable vertex buffer objects.
mbgl::gl::InitializeExtensions([](const char *name) {
static CFBundleRef framework = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl"));
Expand Down Expand Up @@ -742,6 +737,21 @@ - (void)renderSync {
}
}

- (void)validateTileCacheSize {
if (!_mbglMap) {
return;
}

CGFloat zoomFactor = self.maximumZoomLevel - self.minimumZoomLevel + 1;
CGFloat cpuFactor = [NSProcessInfo processInfo].processorCount;
CGFloat memoryFactor = (CGFloat)[NSProcessInfo processInfo].physicalMemory / 1000 / 1000 / 1000;
CGFloat sizeFactor = (NSWidth(self.bounds) / mbgl::util::tileSize) * (NSHeight(self.bounds) / mbgl::util::tileSize);

NSUInteger cacheSize = zoomFactor * cpuFactor * memoryFactor * sizeFactor * 0.5;

_mbglMap->setSourceTileCacheSize(cacheSize);
}

- (void)invalidate {
MGLAssertIsMainThread();

Expand Down Expand Up @@ -947,11 +957,13 @@ - (void)scaleBy:(double)scaleFactor atPoint:(NSPoint)point animated:(BOOL)animat
- (void)setMinimumZoomLevel:(double)minimumZoomLevel
{
_mbglMap->setMinZoom(minimumZoomLevel);
[self validateTileCacheSize];
}

- (void)setMaximumZoomLevel:(double)maximumZoomLevel
{
_mbglMap->setMaxZoom(maximumZoomLevel);
[self validateTileCacheSize];
}

- (double)maximumZoomLevel {
Expand Down

0 comments on commit d28b6f3

Please sign in to comment.