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

custom raster tile renderer (work around iOS 8 bug, support 512px v4 tiles) - #136

Closed
incanus wants to merge 29 commits into
masterfrom
132-renderer
Closed

incanus wants to merge 29 commits into
masterfrom
132-renderer

Conversation

@incanus

@incanus incanus commented Dec 10, 2014

Copy link
Copy Markdown
Contributor

No description provided.

@incanus incanus changed the title customer raster tile renderer (work around iOS 8 bug, support 512px v4 tiles) custom raster tile renderer (work around iOS 8 bug, support 512px v4 tiles) Dec 11, 2014
@robmaceachern

Copy link
Copy Markdown

I'm seeing a pretty consistent crash when running the sample project on the 132-renderer branch. It's failing in MBXRasterTileRenderer canDrawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale

Here's the crash report. I was seeing it on iPhone 6 iOS 8.1.1.

@incanus

incanus commented Jan 14, 2015

Copy link
Copy Markdown
Contributor Author

Thanks for that, @robmaceachern. I'll check it out.

@incanus

incanus commented Jan 14, 2015

Copy link
Copy Markdown
Contributor Author

@robmaceachern Can you give the latest iteration of the branch (with 3f5cfa7) a try? The crash logs seems to say that this is related to use of -[UIImage imageWithData:] in background threads, which seems to be unadvisable if you take the strictly conservative approach based on:

http://stackoverflow.com/questions/12766501/is-creating-uiimage-thread-safe

That is, UIKit clearly shouldn't draw or update UI from background threads, but it's open to interpretation whether image instantiation is ok.

Also, note that at least in my device testing, using a Release build is noticeably faster, so do check that out if performance is a concern.

@incanus

incanus commented Jan 15, 2015

Copy link
Copy Markdown
Contributor Author

Trying to push a bit more of the image work out to CGImageRef and friends instead of dispatching to the main thread. Just about there.

@robmaceachern

Copy link
Copy Markdown

I was hoping it was going to be that easy too, but there's something else going on. 😢

screen shot 2015-01-14 at 4 58 12 pm

It seems like the tileData is (intermittently) bogus.

@incanus

incanus commented Jan 15, 2015

Copy link
Copy Markdown
Contributor Author

@robmaceachern The thing I don't understand about that is how that point in the control flow is reached then, since just three lines above, there is an if (tileData) {. That's what leads me to think that it's threading related with UIKit.

@tkrajacic

Copy link
Copy Markdown
Contributor

Self can go away between those lines or not?
Wasn't there an issue last year where we tried to figure something like this out? I can't find it though.
Back then it was about the tiles being still around when the map instance was already deallocated.

@incanus

incanus commented Jan 15, 2015

Copy link
Copy Markdown
Contributor Author

self could, yes, since it's the MKMapView in question inside of that loadTileAtPath:result: callback, but it doesn't — the map view stays put. So I'm thinking this is related to creation of UIImage objects happening in non-main threads; something about the way graphics memory is allocated or used, maybe?

@robmaceachern

Copy link
Copy Markdown

@incanus tileData is not nil at that point which is why it passes the conditional test. I've also experimented with core graphics and experienced the same issue, so we can rule out UIKit issues.

I just noticed your commit. I'll try to reproduce the problem again.

@incanus

incanus commented Jan 15, 2015

Copy link
Copy Markdown
Contributor Author

Yeah, please see if you can reproduce. I cut out all use of UIImage initialization since we're never on the main thread by default.

Now I'm working on performance — I don't like it, and ~40% of the time is spent in PNG encoding after slicing 512px tiles into 256px tiles in the -canDrawMapRect:zoomScale:. Thinking I'll just move to raw pixels instead of PNG data since we're only talking about in-memory storage anyway (I thought it best to never cache on disk the 256px sliced tiles since we never request them directly in the API).

@robmaceachern

Copy link
Copy Markdown

Yeah it's still an issue. It's failing at the same CG function as before _CGImageSourceBindToPlugin ()

screen shot 2015-01-15 at 11 02 31 am

@robmaceachern

Copy link
Copy Markdown

Just got another, different, EXC_BAD_ACCESS in some system networking code while running on the iPhone 6 simulator.

#0  0x000000010e85562a in getMIMETypeForRuleArray(__CFData const*, MIMETypeRule const*, unsigned char) ()
#1  0x000000010e7e0690 in URLResponse::guessMIMETypeCurrent(__CFData const*) ()
#2  0x000000010e849e08 in -[__NSCFURLSessionConnection _tick_sniffNow] ()
#3  0x000000010e849eb6 in -[__NSCFURLSessionConnection _tick_running] ()
#4  0x000000010e84d0d2 in SessionConnectionLoadable::_loaderClientEvent_DidReceiveData(__CFArray const*) ()
#5  0x000000010e84d216 in SessionConnectionLoadable::_loaderClientEvent_HandleCacheResponseIsValid(_CFCachedURLResponse const*) ()
#6  0x000000010e8b7e2f in ___ZN19URLConnectionLoader28protocolCacheResponseIsValidEPK20_CFCachedURLResponse_block_invoke ()
#7  0x000000010e84c943 in ___ZN25SessionConnectionLoadable21withLoaderClientAsyncEU13block_pointerFvP21LoaderClientInterfaceE_block_invoke ()
#8  0x000000010de37ba6 in _dispatch_call_block_and_release ()
#9  0x000000010de557f4 in _dispatch_client_callout ()
#10 0x000000010de3db22 in _dispatch_queue_drain ()
#11 0x000000010de3d432 in _dispatch_queue_invoke ()
#12 0x000000010de3ffc1 in _dispatch_root_queue_drain ()
#13 0x000000010de415d9 in _dispatch_worker_thread3 ()
#14 0x000000010e1d96cb in _pthread_wqthread ()
#15 0x000000010e1d74a1 in start_wqthread ()
Enqueued from __NSCFURLSessionConnection (Thread 24)Queue : __NSCFURLSessionConnection (serial)

@incanus

incanus commented Jan 15, 2015

Copy link
Copy Markdown
Contributor Author

Storing raw pixel data in the memory cache isn't much faster, and the bottleneck is copying the individual CGImageRef data there.

Trying the approach now of caching the CGImageRef pointers (after retaining their data) and seeing if that helps.

@incanus

incanus commented Jan 15, 2015

Copy link
Copy Markdown
Contributor Author

Just a summary, for my own reference (since I had to re-look it up to remind myself) on why we are splitting 512px tiles client-side:

  1. iOS 7 has a bug where MKTileOverlay cannot use tiles of sizes larger than 256px.
  2. iOS 8 has a bug where using MKTileOverlay logs lots of ImageIO warnings and occasionally crashes and/or doesn't draw tiles.
  3. Both of the above can be solved by creating a custom MKTileOverlayRenderer, but per mapkit tile overlay CGImageSourceCreateWithData error #132 (comment) it is not possible to draw in areas greater than 256px in a custom renderer.

@incanus

incanus commented Jan 17, 2015

Copy link
Copy Markdown
Contributor Author

Ok, a very promising lead now.

40f71de brings in a change that calls -loadTileAtPath:result: synchronously. There is nothing in the docs that says whether async is ok here, just that it should call from the main thread. In our own implementation of that method, like most sane implementations we kick off to a background thread anyway for the actual loading heavy lifting, but I think these chained async calls are, under high system load, possibly causing result()'s data to be released early. You'd think block scope would capture it properly, but I think you'd be wrong. There is no reason to call result() async here anyway, so it strikes me as possibly a Bad Idea and the root of our problems.

I also added c7a82c3, which responds to low system memory and jettisons the in-memory cache of tiles, which seems like a good idea given the amount of memory we're talking about. However, in lots of testing on a retina iPad, even with 300-400MB of app memory usage I wasn't able to trigger it very often.

@incanus

incanus commented Jan 17, 2015

Copy link
Copy Markdown
Contributor Author

Yet another very promising lead, the best one yet. Even with the above, I was able to reproduce the crash again. I investigated the NSData passed in the callback, and its underlying type was OS_dispatch_data, which makes a bit of sense.

However, this sounds rather transient and is probably going away out from under us. Trying either an -[NSData copy] or an +[NSData dataWithData:] to keep it around both yield identical types under the hood, like so:

2015-01-16 16:30:56.987 MBXMapKit[1242:317501] old: OS_dispatch_data
2015-01-16 16:30:56.987 MBXMapKit[1242:317501] new: OS_dispatch_data

No dice. However 226edce uses -[NSData initWithBytes:length:] per CocoaDev, which does yield a fresh copy & new type:

2015-01-16 16:30:30.669 MBXMapKit[1220:317060] old: OS_dispatch_data
2015-01-16 16:30:30.669 MBXMapKit[1220:317060] new: _NSInlineData

This copy happens in the main thread, then the copy is captured in the async block scope and dealt with (validated & cached) off the main thread, then ARC disposes of it, regardless of what has already happened with the original dispatched version's lifetime.

And now I've been unable to replicate the crash.

@robmaceachern

Copy link
Copy Markdown

No dice. tileData is still going bad.

screen shot 2015-01-16 at 6 03 59 pm

I think this may be an NSURLSession bug. I tried copying it in the data task handler and it still fails.

screen shot 2015-01-16 at 6 13 39 pm

@incanus

incanus commented Jan 19, 2015

Copy link
Copy Markdown
Contributor Author

I think this may be an NSURLSession bug. I tried copying it in the data task handler and it still fails.

Could be, but Occam's Razor says it's something we're doing. My guess could be maybe something around the passing of result inside of completionHandler and ARC not understanding proper lifetime scope. Will keep digging at this.

@incanus

incanus commented Jan 26, 2015

Copy link
Copy Markdown
Contributor Author

this may be an NSURLSession bug

Gonna try gutting this for NSURLConnection, I think, and see how that fares.

@incanus

incanus commented Jan 27, 2015

Copy link
Copy Markdown
Contributor Author

Ok, tried a number of approaches around reducing nesting of blocks, manually copying blocks, and otherwise being very explicit around blocks, with no luck.

Went the route of replacing NSURLSession with NSURLConnection in the overlay. The downside is we lose the ability to specify the number of concurrent HTTP connections per host and the ability to summarily cancel all pending fetches as in NSURLSession, but I think it's a fair tradeoff and I'm not seeing a performance consequence.

Curious how this works for you, @robmaceachern.

@incanus

incanus commented Jan 27, 2015

Copy link
Copy Markdown
Contributor Author

I've yet to replicate a crash here since moving to NSURLConnection. I need to benchmark a bit more between Debug and Release — I feel like this could be done faster. But then again, maybe not, as we are slicing 512px tiles on the client to accomplish this. /cc @willwhite

@incanus

incanus commented Jan 27, 2015

Copy link
Copy Markdown
Contributor Author

Yeah, there's plenty of duplicate URL fetching going on here in response to MapKit's impatience. Will work on this.

2015-01-27 15:28:49.692 MBXMapKit[263:13405] 16,10556,25512 start
2015-01-27 15:28:49.693 MBXMapKit[263:13409] 16,10556,25512 start
2015-01-27 15:28:49.694 MBXMapKit[263:13411] 16,10556,25512 start
2015-01-27 15:28:49.695 MBXMapKit[263:13408] 16,10555,25512 start
2015-01-27 15:28:49.695 MBXMapKit[263:13410] 16,10555,25512 start
2015-01-27 15:28:49.696 MBXMapKit[263:13413] 16,10558,25511 start
2015-01-27 15:28:49.696 MBXMapKit[263:13376] 16,10555,25512 start
2015-01-27 15:28:49.697 MBXMapKit[263:13412] 16,10557,25511 start
2015-01-27 15:28:49.698 MBXMapKit[263:13375] 16,10555,25512 start
2015-01-27 15:28:49.698 MBXMapKit[263:13414] 16,10557,25511 start
2015-01-27 15:28:49.699 MBXMapKit[263:13383] 16,10556,25511 start
2015-01-27 15:28:49.700 MBXMapKit[263:13416] 16,10556,25511 start
2015-01-27 15:28:49.700 MBXMapKit[263:13417] 16,10555,25511 start
2015-01-27 15:28:49.702 MBXMapKit[263:13418] 16,10555,25511 start
2015-01-27 15:28:50.095 MBXMapKit[263:13170] 16,10558,25515 end
2015-01-27 15:28:50.097 MBXMapKit[263:13170] 16,10558,25515 end
2015-01-27 15:28:50.148 MBXMapKit[263:13170] 16,10557,25515 end
2015-01-27 15:28:50.165 MBXMapKit[263:13170] 16,10557,25515 end
2015-01-27 15:28:50.191 MBXMapKit[263:13170] 16,10557,25515 end
2015-01-27 15:28:50.195 MBXMapKit[263:13170] 16,10557,25515 end
2015-01-27 15:28:50.573 MBXMapKit[263:13170] 16,10556,25515 end
2015-01-27 15:28:50.606 MBXMapKit[263:13170] 16,10556,25515 end
2015-01-27 15:28:50.608 MBXMapKit[263:13170] 16,10556,25515 end

@incanus

incanus commented Jan 28, 2015

Copy link
Copy Markdown
Contributor Author

Ok, the duplicates were somewhat expected given that requests made for to-be-split 512px tiles come in fours. However, we were possibly kicking off up to four duplicate HTTP requests at once, when one is enough to split and satisfy four sub-tile would-be requests. This works better now.

@robmaceachern

Copy link
Copy Markdown

No crashes for me 👍

Have you filed a radar for the NSURLSession issue?

@incanus

incanus commented Jan 28, 2015

Copy link
Copy Markdown
Contributor Author

Have you filed a radar for the NSURLSession issue?

No, nor for the three MapKit bugs I am seeing around this. Will hopefully make up use cases for these & submit them.

@mackuba

mackuba commented Jan 28, 2015

Copy link
Copy Markdown

This branch fixes the log warnings for me too. No crashes, but I've only scrolled it around for a short moment.

incanus added a commit that referenced this pull request Jan 28, 2015
 * brings back Mapbox v4 API & access token support, including to iOS 7
 * sidesteps iOS 7 >256px tile render bug
 * sidesteps iOS 8 CGImageSourceCreateWithData log spew & crash bug
 * sidesteps NSURLSession block passing bug
 * adds an access token & v4 API use in the sample app
 * cleans up block syntax
 * safer render state mutation for lifecycle callbacks
 * explicitly turns off 3D buildings in MapKit for the sample app
@incanus

incanus commented Jan 28, 2015

Copy link
Copy Markdown
Contributor Author

Thanks @robmaceachern @jsuder for your patience and testing! I am feeling good about this and it's landed in master.

@incanus incanus closed this Jan 28, 2015
@incanus incanus added this to the 0.6.0 milestone Jan 28, 2015
@incanus
incanus deleted the 132-renderer branch January 30, 2015 00:31
@mackuba

mackuba commented Feb 3, 2015

Copy link
Copy Markdown

Hmm, I started getting some random crashes that look like this, this looks like something related to maps...

screen shot 2015-02-03 at 17 25 53

@incanus

incanus commented Feb 3, 2015

Copy link
Copy Markdown
Contributor Author

@jsuder Hmm, that's deep in MapKit, and I haven't seen it before. Can you reliable replicate it? Device or sim? Particular UI interactions?

@mackuba

mackuba commented Feb 3, 2015

Copy link
Copy Markdown

I've only seen it twice today and I've run the app many times... It's on
the simulator and I'm not doing any interactions, just showing the map
centered on a specific point.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants