Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 27960d71 authored by Jordan Demeulenaere's avatar Jordan Demeulenaere
Browse files

Share the Picture object of MovableElement

This CL makes sure that MovableElements share their Picture object.
Given that a MovableElement is always composed in a scene, there is no
need to have multiple Picture objects. This also ensures that the
picture contains the last drawing commands of the movable element,
independently of which scenes it is composed in.

Bug: 291053742
Test: Manual
Flag: N/A
Change-Id: I32bd35754ce701c01712612649b6c43dfdd8073f
parent 5787d4d5
Loading
Loading
Loading
Loading
+16 −6
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.compose.animation.scene

import android.graphics.Picture
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
@@ -66,12 +67,21 @@ internal class Element(val key: ElementKey) {
     * The movable content of this element, if this element is composed using
     * [SceneScope.MovableElement].
     */
    val movableContent by
        // This is only accessed from the composition (main) thread, so no need to use the default
        // lock of lazy {} to synchronize.
        lazy(mode = LazyThreadSafetyMode.NONE) {
            movableContentOf { content: @Composable () -> Unit -> content() }
        }
    private var _movableContent: (@Composable (@Composable () -> Unit) -> Unit)? = null
    val movableContent: @Composable (@Composable () -> Unit) -> Unit
        get() =
            _movableContent
                ?: movableContentOf { content: @Composable () -> Unit -> content() }
                    .also { _movableContent = it }

    /**
     * The [Picture] to which we save the last drawing commands of this element, if it is movable.
     * This is necessary because the content of this element might not be composed in the scene it
     * should currently be drawn.
     */
    private var _picture: Picture? = null
    val picture: Picture
        get() = _picture ?: Picture().also { _picture = it }

    override fun toString(): String {
        return "Element(key=$key)"
+1 −2
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.compose.animation.scene

import android.graphics.Picture
import android.util.Log
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Spacer
@@ -60,7 +59,7 @@ internal fun MovableElement(
        // The [Picture] to which we save the last drawing commands of this element. This is
        // necessary because the content of this element might not be composed in this scene, in
        // which case we still need to draw it.
        val picture = remember { Picture() }
        val picture = element.picture

        // Whether we should compose the movable element here. The scene picker logic to know in
        // which scene we should compose/draw a movable element might depend on the current