/* * Copyright (C) 2025 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ @file:OptIn(ExperimentalKairosApi::class) package com.android.app.concurrent.benchmark.event import com.android.app.concurrent.benchmark.util.ThreadFactory import com.android.systemui.kairos.BuildScope import com.android.systemui.kairos.ExperimentalKairosApi import com.android.systemui.kairos.MutableState as KairosMutableState import com.android.systemui.kairos.State as KairosState import com.android.systemui.kairos.TransactionScope import com.android.systemui.kairos.combine as combineKairosState import com.android.systemui.kairos.launchKairosNetwork import com.android.systemui.kairos.map as mapKairosState import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch typealias KairosStateBoxIn = EventBox> typealias KairosStateBoxOut = EventBox> private class KairosStateBoxImpl>(event: E) : AbstractEventBox(event) private fun KairosStateBoxIn.unbox(): KairosState = (this as KairosStateBoxImpl).event class KairosObservationContext(val scope: BuildScope) : ReadContext> { override fun KairosStateBoxIn.observe(block: (T) -> Unit) { with(scope) { unbox().observeSync { block(it) } } } } class KairosWriteContext(val scope: TransactionScope) : WriteContext> { override fun KairosStateBoxIn.update(value: T) { val kairosState = (this as KairosStateBoxImpl).event as KairosMutableState with(scope) { kairosState.setValue(value) } } override fun KairosStateBoxIn.current(): T { TODO("Not yet implemented") } } class KairosWritableEventBuilder(val scope: CoroutineScope) : EventContext, WritableEventFactory>, EventContextProvider>, EventCombiner>, IntEventCombiner>, MapOperator> { val kairosNetwork = scope.launchKairosNetwork() override fun createWritableEvent(value: T): EventBox> { return KairosStateBoxImpl(KairosMutableState(kairosNetwork, value)) } override fun combineEvents( a: KairosStateBoxIn, b: KairosStateBoxIn, transform: (T1, T2) -> T3, ): KairosStateBoxIn { return KairosStateBoxImpl( combineKairosState(a.unbox(), b.unbox()) { aVal, bVal -> transform(aVal, bVal) } ) } override fun combineEvents( a: KairosStateBoxIn, b: KairosStateBoxIn, c: KairosStateBoxIn, transform: (T1, T2, T3) -> T4, ): KairosStateBoxIn { return KairosStateBoxImpl( combineKairosState(a.unbox(), b.unbox(), c.unbox()) { aVal, bVal, cVal -> transform(aVal, bVal, cVal) } ) } override fun combineIntEvents( events: Iterable>, transform: (Array) -> R, ): KairosStateBoxOut { val kairosStates = events.map { it.unbox() } return KairosStateBoxImpl( kairosStates.combineKairosState { input -> transform(input.toTypedArray()) } ) } override fun read(block: ReadContext>.() -> Unit): AutoCloseable { val job = scope.launch { kairosNetwork.activateSpec { val observationContext = KairosObservationContext(this) observationContext.block() } } return AutoCloseable { job.cancel() } } override fun write(block: WriteContext>.() -> Unit) { scope.launch { kairosNetwork.transact { val updateContext = KairosWriteContext(this) updateContext.block() } } } override fun KairosStateBoxIn.map(transform: (A) -> B): KairosStateBoxOut { this as KairosStateBoxImpl return KairosStateBoxImpl(event.mapKairosState { transform(it) }) } } abstract class BaseKairosEventBenchmark(threadParam: ThreadFactory) : BaseEventBenchmark( threadParam, { KairosWritableEventBuilder(it) }, )