Skip to content

Commit f9ba7e5

Browse files
committed
perf: improve MarkerComposable rendering performance
1 parent 8994e12 commit f9ba7e5

File tree

2 files changed

+75
-62
lines changed

2 files changed

+75
-62
lines changed

maps-compose/src/main/java/com/google/maps/android/compose/Marker.kt

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -341,27 +341,28 @@ public fun MarkerComposable(
341341
content: @UiComposable @Composable () -> Unit,
342342
) {
343343
val icon = rememberComposeBitmapDescriptor(*keys) { content() }
344-
345-
MarkerImpl(
346-
state = state,
347-
contentDescription = contentDescription,
348-
alpha = alpha,
349-
anchor = anchor,
350-
draggable = draggable,
351-
flat = flat,
352-
icon = icon,
353-
infoWindowAnchor = infoWindowAnchor,
354-
rotation = rotation,
355-
snippet = snippet,
356-
tag = tag,
357-
title = title,
358-
visible = visible,
359-
zIndex = zIndex,
360-
onClick = onClick,
361-
onInfoWindowClick = onInfoWindowClick,
362-
onInfoWindowClose = onInfoWindowClose,
363-
onInfoWindowLongClick = onInfoWindowLongClick,
364-
)
344+
if (icon != null) {
345+
MarkerImpl(
346+
state = state,
347+
contentDescription = contentDescription,
348+
alpha = alpha,
349+
anchor = anchor,
350+
draggable = draggable,
351+
flat = flat,
352+
icon = icon,
353+
infoWindowAnchor = infoWindowAnchor,
354+
rotation = rotation,
355+
snippet = snippet,
356+
tag = tag,
357+
title = title,
358+
visible = visible,
359+
zIndex = zIndex,
360+
onClick = onClick,
361+
onInfoWindowClick = onInfoWindowClick,
362+
onInfoWindowClose = onInfoWindowClose,
363+
onInfoWindowLongClick = onInfoWindowLongClick,
364+
)
365+
}
365366
}
366367

367368
/**
@@ -489,27 +490,28 @@ public fun MarkerInfoWindowComposable(
489490
content: @UiComposable @Composable () -> Unit,
490491
) {
491492
val icon = rememberComposeBitmapDescriptor(*keys) { content() }
492-
493-
MarkerImpl(
494-
state = state,
495-
alpha = alpha,
496-
anchor = anchor,
497-
draggable = draggable,
498-
flat = flat,
499-
icon = icon,
500-
infoWindowAnchor = infoWindowAnchor,
501-
rotation = rotation,
502-
snippet = snippet,
503-
tag = tag,
504-
title = title,
505-
visible = visible,
506-
zIndex = zIndex,
507-
onClick = onClick,
508-
onInfoWindowClick = onInfoWindowClick,
509-
onInfoWindowClose = onInfoWindowClose,
510-
onInfoWindowLongClick = onInfoWindowLongClick,
511-
infoWindow = infoContent,
512-
)
493+
if (icon != null) {
494+
MarkerImpl(
495+
state = state,
496+
alpha = alpha,
497+
anchor = anchor,
498+
draggable = draggable,
499+
flat = flat,
500+
icon = icon,
501+
infoWindowAnchor = infoWindowAnchor,
502+
rotation = rotation,
503+
snippet = snippet,
504+
tag = tag,
505+
title = title,
506+
visible = visible,
507+
zIndex = zIndex,
508+
onClick = onClick,
509+
onInfoWindowClick = onInfoWindowClick,
510+
onInfoWindowClose = onInfoWindowClose,
511+
onInfoWindowLongClick = onInfoWindowLongClick,
512+
infoWindow = infoContent,
513+
)
514+
}
513515
}
514516

515517
/**

maps-compose/src/main/java/com/google/maps/android/compose/RememberComposeBitmapDescriptor.kt

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,52 +19,65 @@ package com.google.maps.android.compose
1919
import android.view.View
2020
import android.view.ViewGroup
2121
import androidx.compose.runtime.Composable
22-
import androidx.compose.runtime.CompositionContext
22+
import androidx.compose.runtime.DisposableEffect
23+
import androidx.compose.runtime.LaunchedEffect
2324
import androidx.compose.runtime.getValue
25+
import androidx.compose.runtime.mutableStateOf
2426
import androidx.compose.runtime.remember
2527
import androidx.compose.runtime.rememberCompositionContext
2628
import androidx.compose.runtime.rememberUpdatedState
29+
import androidx.compose.runtime.setValue
2730
import androidx.compose.ui.platform.ComposeView
2831
import androidx.compose.ui.platform.LocalView
2932
import androidx.core.graphics.applyCanvas
33+
import androidx.core.graphics.createBitmap
3034
import com.google.android.gms.maps.model.BitmapDescriptor
3135
import com.google.android.gms.maps.model.BitmapDescriptorFactory
32-
import androidx.core.graphics.createBitmap
36+
import kotlinx.coroutines.Dispatchers
37+
import kotlinx.coroutines.withContext
3338

3439
@MapsComposeExperimentalApi
3540
@Composable
3641
public fun rememberComposeBitmapDescriptor(
3742
vararg keys: Any,
3843
content: @Composable () -> Unit,
39-
): BitmapDescriptor {
44+
): BitmapDescriptor? {
45+
var bitmapDescriptor by remember { mutableStateOf<BitmapDescriptor?>(null) }
4046
val parent = LocalView.current as ViewGroup
4147
val compositionContext = rememberCompositionContext()
4248
val currentContent by rememberUpdatedState(content)
49+
val composeView = remember { ComposeView(parent.context) }
4350

44-
return remember(parent, compositionContext, currentContent, *keys) {
45-
renderComposableToBitmapDescriptor(parent, compositionContext, currentContent)
46-
}
47-
}
48-
49-
private val measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
50-
51-
private fun renderComposableToBitmapDescriptor(
52-
parent: ViewGroup,
53-
compositionContext: CompositionContext,
54-
content: @Composable () -> Unit,
55-
): BitmapDescriptor {
56-
val composeView =
57-
ComposeView(parent.context)
51+
DisposableEffect(Unit) {
52+
composeView
5853
.apply {
5954
layoutParams = ViewGroup.LayoutParams(
6055
ViewGroup.LayoutParams.WRAP_CONTENT,
6156
ViewGroup.LayoutParams.WRAP_CONTENT,
6257
)
6358
setParentCompositionContext(compositionContext)
64-
setContent(content)
59+
setContent(currentContent)
6560
}
6661
.also(parent::addView)
62+
onDispose {
63+
parent.removeView(composeView)
64+
}
65+
}
66+
67+
LaunchedEffect(*keys) {
68+
bitmapDescriptor = withContext(Dispatchers.IO) {
69+
renderComposableToBitmapDescriptor(composeView)
70+
}
71+
}
72+
73+
return bitmapDescriptor
74+
}
6775

76+
private val measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
77+
78+
private fun renderComposableToBitmapDescriptor(
79+
composeView: ComposeView,
80+
): BitmapDescriptor {
6881
composeView.measure(measureSpec, measureSpec)
6982

7083
if (composeView.measuredWidth == 0 || composeView.measuredHeight == 0) {
@@ -79,7 +92,5 @@ private fun renderComposableToBitmapDescriptor(
7992

8093
bitmap.applyCanvas { composeView.draw(this) }
8194

82-
parent.removeView(composeView)
83-
8495
return BitmapDescriptorFactory.fromBitmap(bitmap)
8596
}

0 commit comments

Comments
 (0)