Skip to content

Commit

Permalink
Remove trace
Browse files Browse the repository at this point in the history
  • Loading branch information
rbro112 committed Dec 19, 2024
1 parent 4c1d64c commit 3429432
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ internal object SnapshotSaver {
snapshotsDir: File,
keyName: String,
bitmap: Bitmap,
) = Profiler.trace("saveImage") {
) {
Profiler.startSpan("saveImage")
saveFile(snapshotsDir, "$keyName$PNG_EXTENSION") {
bitmap.compress(Bitmap.CompressFormat.PNG, DEFAULT_PNG_QUALITY, this)
}
Profiler.endSpan()
}

private fun saveMetadata(
Expand All @@ -103,7 +105,8 @@ internal object SnapshotSaver {
displayName: String?,
fqn: String,
composePreviewSnapshotConfig: ComposePreviewSnapshotConfig,
) = Profiler.trace("saveMetadata") {
) {
Profiler.startSpan("saveMetadata")
val metadata: SnapshotMetadata = SnapshotMetadata.SuccessMetadata(
name = keyName,
displayName = displayName,
Expand All @@ -118,6 +121,7 @@ internal object SnapshotSaver {
saveFile(snapshotsDir, "$keyName$JSON_EXTENSION") {
write(jsonString.toByteArray(Charset.defaultCharset()))
}
Profiler.endSpan()
}

private fun saveErrorMetadata(
Expand All @@ -126,7 +130,8 @@ internal object SnapshotSaver {
fqn: String,
errorType: SnapshotErrorType,
composePreviewSnapshotConfig: ComposePreviewSnapshotConfig,
) = Profiler.trace("saveErrorMetadata") {
) {
Profiler.startSpan("saveErrorMetadata")
val keyName = composePreviewSnapshotConfig.keyName()
val metadata: SnapshotMetadata = SnapshotMetadata.ErrorMetadata(
name = composePreviewSnapshotConfig.keyName(),
Expand All @@ -142,22 +147,25 @@ internal object SnapshotSaver {
saveFile(snapshotsDir, "$keyName$JSON_EXTENSION") {
write(jsonString.toByteArray(Charset.defaultCharset()))
}
Profiler.endSpan()
}

private fun saveFile(
dir: File,
filenameWithExtension: String,
writer: FileOutputStream.() -> Unit,
) = Profiler.trace("saveFile") {
) {
Profiler.startSpan("saveFile")
val outputFile = File(dir, filenameWithExtension)

if (outputFile.exists()) {
Log.e(TAG, "File with name $filenameWithExtension already exists, skipping save.")
return@trace
return
}

Log.d(TAG, "Saving file to ${outputFile.path}")
outputFile.outputStream().use { writer(it) }
Profiler.endSpan()
}

private const val PNG_EXTENSION = ".png"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ object ComposableInvoker {
methodName: String,
composer: Composer,
vararg args: Any?
) = Profiler.trace("invokeComposable") {
) {
Profiler.startSpan("invokeComposable")
try {
val composableClass = Class.forName(className)
val method = composableClass.findComposableMethod(methodName, *args)
Expand All @@ -36,6 +37,8 @@ object ComposableInvoker {
} catch (e: Exception) {
Log.w(TAG, "Failed to invoke Composable Method '$className.$methodName'")
throw e
} finally {
Profiler.endSpan()
}
}

Expand Down Expand Up @@ -98,10 +101,11 @@ object ComposableInvoker {
private fun Class<*>.findComposableMethod(
methodName: String,
vararg previewParamArgs: Any?
): Method? = Profiler.trace("findComposableMethod") {
): Method? {
Profiler.startSpan("findComposableMethod")
val argsArray: Array<Class<out Any>> =
previewParamArgs.mapNotNull { it?.javaClass }.toTypedArray()
return@trace try {
return try {
val changedParamsCount = changedParamCount(argsArray.size, 0)
val changedParams = Int::class.java.dup(changedParamsCount)
declaredMethods.findCompatibleComposeMethod(
Expand All @@ -122,6 +126,8 @@ object ComposableInvoker {
Log.w(TAG, "Method $methodName not found in class ${this.simpleName}")
null
}
} finally {
Profiler.endSpan()
}
}

Expand All @@ -134,7 +140,8 @@ object ComposableInvoker {
instance: Any?,
composer: Composer,
vararg args: Any?
): Any? = Profiler.trace("Method.invokeComposableMethod") {
): Any? {
Profiler.startSpan("Method.invokeComposableMethod")
val composerIndex = parameterTypes.indexOfLast { it == Composer::class.java }
val realParams = composerIndex
val thisParams = if (instance != null) 1 else 0
Expand Down Expand Up @@ -176,7 +183,10 @@ object ComposableInvoker {
else -> error("Unexpected index")
}
}
return@trace invoke(instance, *arguments)

val result = invoke(instance, *arguments)
Profiler.endSpan()
return result
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fun snapshotComposable(
snapshotRule: EmergeSnapshots,
activity: PreviewActivity,
previewConfig: ComposePreviewSnapshotConfig,
) = Profiler.trace("snapshotComposable") {
) {
try {
snapshot(
activity = activity,
Expand Down Expand Up @@ -57,73 +57,74 @@ private fun snapshot(
val deviceSpec = configToDeviceSpec(previewConfig)

for (index in previewParameters.indices) {
Profiler.trace("previewParam_$index") {
val prevParam = previewParameters[index]
Log.d(
EmergeComposeSnapshotReflectiveParameterizedInvoker.TAG,
"Invoking composable method with preview parameter: $prevParam"
)
Profiler.startSpan("previewParam_$index")
val prevParam = previewParameters[index]
Log.d(
EmergeComposeSnapshotReflectiveParameterizedInvoker.TAG,
"Invoking composable method with preview parameter: $prevParam"
)

Profiler.startSpan("setupComposeView")
Profiler.startSpan("setupComposeView")

val composeView = ComposeView(activity)
composeView.layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
val args = if (prevParam != null) arrayOf(prevParam) else emptyArray()
val composeView = ComposeView(activity)
composeView.layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
val args = if (prevParam != null) arrayOf(prevParam) else emptyArray()

val saveablePreviewConfig = previewConfig.copy(
previewParameter = previewConfig.previewParameter?.copy(index = index)
)
val saveablePreviewConfig = previewConfig.copy(
previewParameter = previewConfig.previewParameter?.copy(index = index)
)

Profiler.endSpan()
Profiler.endSpan()

// Update activity window size if device is specified
if (deviceSpec != null) {
updateActivityBounds(activity, deviceSpec)
}
// Update activity window size if device is specified
if (deviceSpec != null) {
updateActivityBounds(activity, deviceSpec)
}

composeView.setContent {
SnapshotVariantProvider(previewConfig, deviceSpec?.scalingFactor) {
ComposableInvoker.invokeComposable(
className = previewConfig.fullyQualifiedClassName,
methodName = previewConfig.originalFqn.substringAfterLast("."),
composer = currentComposer,
args = args,
)
}
composeView.setContent {
SnapshotVariantProvider(previewConfig, deviceSpec?.scalingFactor) {
ComposableInvoker.invokeComposable(
className = previewConfig.fullyQualifiedClassName,
methodName = previewConfig.originalFqn.substringAfterLast("."),
composer = currentComposer,
args = args,
)
}
}

// Add the ComposeView to the activity
Profiler.trace("addContentView") {
activity.addContentView(
composeView,
LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
// Add the ComposeView to the activity
Profiler.startSpan("addContentView")
activity.addContentView(
composeView,
LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
)
Profiler.endSpan()

composeView.post {
val size = measureViewSize(composeView, previewConfig)
val bitmap = captureBitmap(composeView, size.width, size.height)

bitmap?.let {
snapshotRule.take(it, saveablePreviewConfig)
} ?: run {
snapshotRule.saveError(
errorType = SnapshotErrorType.EMPTY_SNAPSHOT,
composePreviewSnapshotConfig = saveablePreviewConfig
)
}

composeView.post {
val size = measureViewSize(composeView, previewConfig)
val bitmap = captureBitmap(composeView, size.width, size.height)

bitmap?.let {
snapshotRule.take(it, saveablePreviewConfig)
} ?: run {
snapshotRule.saveError(
errorType = SnapshotErrorType.EMPTY_SNAPSHOT,
composePreviewSnapshotConfig = saveablePreviewConfig
)
}

// Reset activity content view
(composeView.parent as? ViewGroup)?.removeView(composeView)
}
// Reset activity content view
(composeView.parent as? ViewGroup)?.removeView(composeView)
}
Profiler.endSpan()
}
}

private fun measureViewSize(
view: View,
previewConfig: ComposePreviewSnapshotConfig
): IntSize = Profiler.trace("measureViewSize") {
): IntSize {
Profiler.startSpan("measureViewSize")
val deviceSpec = configToDeviceSpec(previewConfig)

// Use exact measurements when we have them
Expand Down Expand Up @@ -160,19 +161,21 @@ private fun measureViewSize(
}

view.measure(widthMeasureSpec, heightMeasureSpec)
return@trace IntSize(view.measuredWidth, view.measuredHeight)
Profiler.endSpan()
return IntSize(view.measuredWidth, view.measuredHeight)
}

private fun updateActivityBounds(activity: Activity, deviceSpec: DeviceSpec) =
Profiler.trace("updateActivityBounds") {
// Apply the device spec dimensions to the activity window
val width = deviceSpec.widthPixels
val height = deviceSpec.heightPixels
private fun updateActivityBounds(activity: Activity, deviceSpec: DeviceSpec) {
Profiler.startSpan("updateActivityBounds")
// Apply the device spec dimensions to the activity window
val width = deviceSpec.widthPixels
val height = deviceSpec.heightPixels

if (width > 0 && height > 0) {
activity.window.setLayout(width, height)
}
if (width > 0 && height > 0) {
activity.window.setLayout(width, height)
}
Profiler.endSpan()
}

private fun dpToPx(dp: Int, scalingFactor: Float): Int {
return (dp * scalingFactor).toInt()
Expand All @@ -182,19 +185,22 @@ fun captureBitmap(
view: View,
width: Int,
height: Int,
): Bitmap? = Profiler.trace("captureBitmap") {
): Bitmap? {
Profiler.startSpan("captureBitmap")
try {
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
view.layout(0, 0, width, height)
view.draw(canvas)
return@trace bitmap
return bitmap
} catch (e: IllegalArgumentException) {
Log.e(
EmergeComposeSnapshotReflectiveParameterizedInvoker.TAG,
"Error capturing bitmap",
e,
)
return@trace null
return null
} finally {
Profiler.endSpan()
}
}
Loading

0 comments on commit 3429432

Please sign in to comment.