1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.compose.ui.node
18 
19 import androidx.compose.ui.geometry.MutableRect
20 import androidx.compose.ui.geometry.Offset
21 import androidx.compose.ui.graphics.Canvas
22 import androidx.compose.ui.graphics.Matrix
23 import androidx.compose.ui.graphics.ReusableGraphicsLayerScope
24 import androidx.compose.ui.graphics.layer.GraphicsLayer
25 import androidx.compose.ui.unit.IntOffset
26 import androidx.compose.ui.unit.IntSize
27 
28 /** A layer returned by [Owner.createLayer] to separate drawn content. */
29 internal interface OwnedLayer {
30 
31     /** Applies the new layer properties, causing this layer to be redrawn. */
updateLayerPropertiesnull32     fun updateLayerProperties(scope: ReusableGraphicsLayerScope)
33 
34     /**
35      * Returns `false` if [position] is outside the clipped region or `true` if clipping is disabled
36      * or it is within the clipped region.
37      */
38     fun isInLayer(position: Offset): Boolean
39 
40     /** Changes the position of the layer contents. */
41     fun move(position: IntOffset)
42 
43     /** Changes the size of the layer's drawn area. */
44     fun resize(size: IntSize)
45 
46     /** Causes the layer to be drawn into [canvas] */
47     fun drawLayer(canvas: Canvas, parentLayer: GraphicsLayer?)
48 
49     /** Updates the drawing on the current canvas. */
50     fun updateDisplayList()
51 
52     /** Asks to the layer to redraw itself without forcing all of its parents to redraw. */
53     fun invalidate()
54 
55     /** Indicates that the layer is no longer needed. */
56     fun destroy()
57 
58     /**
59      * Transforms [point] to this layer's bounds, returning an [Offset] with the transformed x and y
60      * values.
61      *
62      * @param point the [Offset] to transform to this layer's bounds
63      * @param inverse whether to invert this layer's transform [Matrix] first, such as when
64      *   converting an offset in a parent layer to be in this layer's coordinates.
65      */
66     fun mapOffset(point: Offset, inverse: Boolean): Offset
67 
68     /**
69      * Transforms the provided [rect] to this layer's bounds, then updates [rect] to match the new
70      * bounds after the transform.
71      *
72      * @param rect the bounds to transform to this layer's bounds, and then mutate with the
73      *   resulting value
74      * @param inverse whether to invert this layer's transform [Matrix] first, such as when
75      *   converting bounds in a parent layer to be in this layer's coordinates.
76      */
77     fun mapBounds(rect: MutableRect, inverse: Boolean)
78 
79     /**
80      * Reuse this layer after it was [destroy]ed, setting the new [drawBlock] and
81      * [invalidateParentLayer] values. The layer will be reinitialized as new after this call.
82      */
83     fun reuseLayer(
84         drawBlock: (canvas: Canvas, parentLayer: GraphicsLayer?) -> Unit,
85         invalidateParentLayer: () -> Unit
86     )
87 
88     /**
89      * Calculates the transform from the parent to the local coordinates and multiplies [matrix] by
90      * the transform.
91      */
92     fun transform(matrix: Matrix)
93 
94     /** The matrix associated with the affine transform of this layer */
95     val underlyingMatrix: Matrix
96 
97     /** The preferred frame rate that the content should be rendered at */
98     var frameRate: Float
99 
100     /** Whether the preferred frame rate comes from the parent layer */
101     var isFrameRateFromParent: Boolean
102 
103     /**
104      * Calculates the transform from the layer to the parent and multiplies [matrix] by the
105      * transform.
106      */
107     fun inverseTransform(matrix: Matrix)
108 }
109