forked from androidx/androidx
-
Notifications
You must be signed in to change notification settings - Fork 62
/
SkiaLayer.skiko.kt
325 lines (291 loc) · 10.6 KB
/
SkiaLayer.skiko.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* Copyright 2020 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.
*/
package androidx.compose.ui.platform
import androidx.compose.ui.geometry.MutableRect
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.RoundRect
import androidx.compose.ui.geometry.toRect
import androidx.compose.ui.graphics.Canvas
import androidx.compose.ui.graphics.ClipOp
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.DefaultCameraDistance
import androidx.compose.ui.graphics.DefaultShadowColor
import androidx.compose.ui.graphics.Matrix
import androidx.compose.ui.graphics.Outline
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.RenderEffect
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.TransformOrigin
import androidx.compose.ui.graphics.asComposeCanvas
import androidx.compose.ui.graphics.asSkiaPath
import androidx.compose.ui.graphics.nativeCanvas
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.graphics.toSkiaRRect
import androidx.compose.ui.graphics.toSkiaRect
import androidx.compose.ui.node.OwnedLayer
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.toSize
import kotlin.math.max
import org.jetbrains.skia.ClipMode
import org.jetbrains.skia.Picture
import org.jetbrains.skia.PictureRecorder
import org.jetbrains.skia.Point3
import org.jetbrains.skia.ShadowUtils
internal class SkiaLayer(
private var density: Density,
private val invalidateParentLayer: () -> Unit,
private val drawBlock: (Canvas) -> Unit,
private val onDestroy: () -> Unit = {}
) : OwnedLayer {
private var size = IntSize.Zero
private var position = IntOffset.Zero
private var outlineCache =
OutlineCache(density, size, RectangleShape, LayoutDirection.Ltr)
// Internal for testing
internal val matrix = Matrix()
private val inverseMatrix: Matrix
get() = Matrix().apply {
setFrom(matrix)
invert()
}
private val pictureRecorder = PictureRecorder()
private var picture: Picture? = null
private var isDestroyed = false
private var transformOrigin: TransformOrigin = TransformOrigin.Center
private var translationX: Float = 0f
private var translationY: Float = 0f
private var rotationX: Float = 0f
private var rotationY: Float = 0f
private var rotationZ: Float = 0f
private var cameraDistance: Float = DefaultCameraDistance
private var scaleX: Float = 1f
private var scaleY: Float = 1f
private var alpha: Float = 1f
private var clip: Boolean = false
private var renderEffect: RenderEffect? = null
private var shadowElevation: Float = 0f
private var ambientShadowColor: Color = DefaultShadowColor
private var spotShadowColor: Color = DefaultShadowColor
override fun destroy() {
picture?.close()
pictureRecorder.close()
isDestroyed = true
onDestroy()
}
override fun reuseLayer(drawBlock: (Canvas) -> Unit, invalidateParentLayer: () -> Unit) {
// TODO: in destroy, call recycle, and reconfigure this layer to be ready to use here.
}
override fun resize(size: IntSize) {
if (size != this.size) {
this.size = size
outlineCache.size = size
updateMatrix()
invalidate()
}
}
override fun move(position: IntOffset) {
if (position != this.position) {
this.position = position
invalidateParentLayer()
}
}
override fun mapOffset(point: Offset, inverse: Boolean): Offset {
return if (inverse) {
inverseMatrix
} else {
matrix
}.map(point)
}
override fun mapBounds(rect: MutableRect, inverse: Boolean) {
if (inverse) {
inverseMatrix
} else {
matrix
}.map(rect)
}
override fun isInLayer(position: Offset): Boolean {
if (!clip) {
return true
}
val x = position.x
val y = position.y
if (outlineCache.shape === RectangleShape) {
return 0f <= x && x < size.width && 0f <= y && y < size.height
}
return isInOutline(outlineCache.outline, x, y)
}
override fun updateLayerProperties(
scaleX: Float,
scaleY: Float,
alpha: Float,
translationX: Float,
translationY: Float,
shadowElevation: Float,
rotationX: Float,
rotationY: Float,
rotationZ: Float,
cameraDistance: Float,
transformOrigin: TransformOrigin,
shape: Shape,
clip: Boolean,
renderEffect: RenderEffect?,
ambientShadowColor: Color,
spotShadowColor: Color,
layoutDirection: LayoutDirection,
density: Density
) {
this.transformOrigin = transformOrigin
this.translationX = translationX
this.translationY = translationY
this.rotationX = rotationX
this.rotationY = rotationY
this.rotationZ = rotationZ
this.cameraDistance = max(cameraDistance, 0.001f)
this.scaleX = scaleX
this.scaleY = scaleY
this.alpha = alpha
this.clip = clip
this.shadowElevation = shadowElevation
this.density = density
this.renderEffect = renderEffect
this.ambientShadowColor = ambientShadowColor
this.spotShadowColor = spotShadowColor
outlineCache.shape = shape
outlineCache.layoutDirection = layoutDirection
outlineCache.density = density
updateMatrix()
invalidate()
}
private fun updateMatrix() {
val pivotX = transformOrigin.pivotFractionX * size.width
val pivotY = transformOrigin.pivotFractionY * size.height
matrix.reset()
matrix.translate(x = -pivotX, y = -pivotY)
matrix *= Matrix().apply {
rotateZ(rotationZ)
rotateY(rotationY)
rotateX(rotationX)
scale(scaleX, scaleY)
}
matrix *= Matrix().apply {
// the camera location is passed in inches, set in pt
val depth = cameraDistance * 72f
set(row = 2, column = 3, v = -1f / depth)
set(row = 2, column = 2, v = 0f)
}
matrix *= Matrix().apply {
translate(x = pivotX + translationX, y = pivotY + translationY)
}
}
override fun invalidate() {
if (!isDestroyed && picture != null) {
picture?.close()
picture = null
invalidateParentLayer()
}
}
override fun drawLayer(canvas: Canvas) {
if (picture == null) {
val bounds = size.toSize().toRect()
val pictureCanvas = pictureRecorder.beginRecording(bounds.toSkiaRect())
performDrawLayer(pictureCanvas.asComposeCanvas(), bounds)
picture = pictureRecorder.finishRecordingAsPicture()
}
canvas.save()
canvas.concat(matrix)
canvas.translate(position.x.toFloat(), position.y.toFloat())
canvas.nativeCanvas.drawPicture(picture!!, null, null)
canvas.restore()
}
override fun transform(matrix: Matrix) {
matrix.timesAssign(this.matrix)
}
override fun inverseTransform(matrix: Matrix) {
matrix.timesAssign(inverseMatrix)
}
private fun performDrawLayer(canvas: Canvas, bounds: Rect) {
if (alpha > 0) {
if (shadowElevation > 0) {
drawShadow(canvas)
}
if (clip) {
canvas.save()
when (val outline = outlineCache.outline) {
is Outline.Rectangle -> canvas.clipRect(outline.rect)
is Outline.Rounded -> canvas.clipRoundRect(outline.roundRect)
is Outline.Generic -> canvas.clipPath(outline.path)
}
}
val currentRenderEffect = renderEffect
if (alpha < 1 || currentRenderEffect != null) {
canvas.saveLayer(
bounds,
Paint().apply {
alpha = this@SkiaLayer.alpha
asFrameworkPaint().imageFilter = currentRenderEffect?.asSkiaImageFilter()
}
)
} else {
canvas.save()
}
drawBlock(canvas)
canvas.restore()
if (clip) {
canvas.restore()
}
}
}
private fun Canvas.clipRoundRect(rect: RoundRect, clipOp: ClipOp = ClipOp.Intersect) {
val antiAlias = true
nativeCanvas.clipRRect(rect.toSkiaRRect(), clipOp.toSkia(), antiAlias)
}
private fun ClipOp.toSkia() = when (this) {
ClipOp.Difference -> ClipMode.DIFFERENCE
ClipOp.Intersect -> ClipMode.INTERSECT
else -> ClipMode.INTERSECT
}
override fun updateDisplayList() = Unit
fun drawShadow(canvas: Canvas) = with(density) {
val path = when (val outline = outlineCache.outline) {
is Outline.Rectangle -> Path().apply { addRect(outline.rect) }
is Outline.Rounded -> Path().apply { addRoundRect(outline.roundRect) }
is Outline.Generic -> outline.path
else -> return
}
// TODO: perspective?
val zParams = Point3(0f, 0f, shadowElevation)
// TODO: configurable?
val lightPos = Point3(0f, -300.dp.toPx(), 600.dp.toPx())
val lightRad = 800.dp.toPx()
val ambientAlpha = 0.039f * alpha
val spotAlpha = 0.19f * alpha
val ambientColor = ambientShadowColor.copy(alpha = ambientAlpha)
val spotColor = spotShadowColor.copy(alpha = spotAlpha)
ShadowUtils.drawShadow(
canvas.nativeCanvas, path.asSkiaPath(), zParams, lightPos,
lightRad,
ambientColor.toArgb(),
spotColor.toArgb(), alpha < 1f, false
)
}
}