Skip to content

Commit 5eb1165

Browse files
authored
Fix a crash in MapView.snapshot (#2344)
1 parent 29aac72 commit 5eb1165

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
Mapbox welcomes participation and contributions from everyone.
44

5-
# 10.16.7
5+
# 10.17.0
66
## Bug fixes 🐞
77
* Fix map being pixelated on some devices when `ContextMode.SHARED` is used (e.g. in AndroidAuto extension).
88
* Fix incorrect widget position and scale when resizing the drawing surface.
9+
* Fix a crash in `MapView.snapshot` happening on specific devices.
910

1011
# 10.16.6 March 04, 2024
1112
## Bug fixes 🐞

sdk/src/main/java/com/mapbox/maps/renderer/MapboxRenderer.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,10 @@ internal abstract class MapboxRenderer : MapClient {
204204
pixelReader?.release()
205205
pixelReader = PixelReader(width, height)
206206
}
207+
val pixelReader = pixelReader!!
207208

208-
pixelReader?.let {
209-
val buffer = it.readPixels()
209+
try {
210+
val buffer = pixelReader.readPixels()
210211
buffer.rewind()
211212
val flipped = Bitmap.createBitmap(
212213
width,
@@ -228,6 +229,18 @@ internal abstract class MapboxRenderer : MapClient {
228229
} finally {
229230
flipped.recycle()
230231
}
232+
} catch (e: Throwable) {
233+
logW(TAG, "Exception ${e.localizedMessage} happened when reading pixels")
234+
if (pixelReader.supportsPbo) {
235+
logW(TAG, "Re-creating PixelReader with no PBO support and making snapshot again")
236+
pixelReader.release()
237+
this.pixelReader = PixelReader(
238+
width = pixelReader.width,
239+
height = pixelReader.height,
240+
supportsPbo = false
241+
)
242+
return performSnapshot()
243+
}
231244
}
232245
return null
233246
}

sdk/src/main/java/com/mapbox/maps/renderer/gl/PixelReader.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import java.nio.IntBuffer
2020
*/
2121
internal class PixelReader(
2222
val width: Int,
23-
val height: Int
23+
val height: Int,
24+
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.N)
25+
internal val supportsPbo: Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
2426
) {
2527
private val bufferSize = width * height * channelNum
2628
private var buffer = ByteBuffer
@@ -86,7 +88,5 @@ internal class PixelReader(
8688
companion object {
8789
// currently support just RGBA
8890
private const val channelNum = 4
89-
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.N)
90-
private val supportsPbo = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
9191
}
9292
}

0 commit comments

Comments
 (0)