expo-android-glass-view 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/README.md +24 -14
- package/android/src/main/java/expo/modules/androidglassview/GlassHostView.kt +11 -17
- package/android/src/main/java/expo/modules/androidglassview/ViewBackdrop.kt +7 -0
- package/android/src/main/java/expo/modules/androidglassview/backdrop/highlight/HighlightModifier.kt +46 -24
- package/android/src/main/java/expo/modules/androidglassview/backdrop/shadow/InnerShadowModifier.kt +33 -12
- package/android/src/main/java/expo/modules/androidglassview/backdrop/shadow/ShadowModifier.kt +42 -16
- package/android/src/main/java/expo/modules/androidglassview/capture/ViewBackdropCapture.kt +167 -6
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## Unreleased
|
|
4
|
+
|
|
5
|
+
- Performance: a glass view's capture leaves out everything that cannot reach it. Before, an
|
|
6
|
+
animation anywhere on screen made every glass view re-render its blur and refraction on every
|
|
7
|
+
frame; now only the glass views it can be seen through do. On a screen with 17 glass views over
|
|
8
|
+
an animated backdrop (Helio G99, release build) this took the frame rate from about 20 to about
|
|
9
|
+
70 fps. A left-out view that moves, resizes or scrolls triggers a new capture.
|
|
10
|
+
|
|
11
|
+
- Performance: scrolling no longer re-captures every glass view in the window. A glass view
|
|
12
|
+
captures again only when a container whose scroll offset its capture baked in has scrolled;
|
|
13
|
+
lists it merely references (e.g. a screen's list under a tab bar) scroll live for free.
|
|
14
|
+
- Performance: the drop shadow, inner shadow and rim highlight are only re-recorded when their
|
|
15
|
+
shape or parameters change (not when they fade, and not on every redraw), and are skipped at
|
|
16
|
+
zero alpha.
|
|
17
|
+
|
|
3
18
|
## 0.1.3 — 2026-09-11
|
|
4
19
|
|
|
5
20
|
- Fix a crash when navigating to or from a screen that has a glass view (Expo Router and React
|
package/README.md
CHANGED
|
@@ -342,6 +342,9 @@ view tree of the window — your React Native screen — captured with an approa
|
|
|
342
342
|
when content behind the glass animates, the glass shows it without capturing again.
|
|
343
343
|
4. Other glass views are left out of the capture, except the glass's own ancestors: a toggle on
|
|
344
344
|
a glass card sees the card. This rules out cycles (glass A → glass B → glass A).
|
|
345
|
+
5. So is everything that cannot reach the glass (its bounds plus a small margin, and those of its
|
|
346
|
+
descendants): a live reference to it would make the glass re-render — capture, blur and
|
|
347
|
+
refraction — whenever it changes, even though none of it shows through the glass.
|
|
345
348
|
|
|
346
349
|
### The effects
|
|
347
350
|
|
|
@@ -356,7 +359,10 @@ The capture is cached in one RenderNode with its own GPU layer, and every glass
|
|
|
356
359
|
(the tab bar has three) samples that same texture. It is recorded again only when:
|
|
357
360
|
|
|
358
361
|
- the glass moves on screen (layout, transform, dragging),
|
|
359
|
-
- a
|
|
362
|
+
- a container it draws inline scrolls: one of its own ancestors, one of their direct children,
|
|
363
|
+
or a container that holds other glass views. A list that is simply behind the glass (deeper in
|
|
364
|
+
another branch, with no glass inside) is referenced, so it scrolls live without a new capture,
|
|
365
|
+
- a view it left out because it was too far away moves, resizes or scrolls,
|
|
360
366
|
- a glass view is added or removed.
|
|
361
367
|
|
|
362
368
|
A glass view that is off screen waits until it is visible again. Pressing or dragging a glass
|
|
@@ -376,25 +382,26 @@ screen.
|
|
|
376
382
|
|
|
377
383
|
## Performance
|
|
378
384
|
|
|
379
|
-
The glass costs nothing while nothing moves.
|
|
380
|
-
|
|
385
|
+
The glass costs nothing while nothing moves. It costs when what is behind a glass view changes:
|
|
386
|
+
that glass view then runs its blur and refraction again on the GPU. Changes elsewhere on the
|
|
387
|
+
screen don't count, because each glass view only captures what can reach it (see _Seeing the
|
|
388
|
+
React Native screen behind it_).
|
|
381
389
|
|
|
382
|
-
Measured with `adb shell dumpsys gfxinfo`
|
|
383
|
-
24117RN76E, MediaTek Helio G99, Android 16, 120 Hz.
|
|
384
|
-
That mostly slows the UI thread; the glass effects run on Android's RenderThread and GPU,
|
|
385
|
-
which a release build does not speed up much.
|
|
390
|
+
Measured with `adb shell dumpsys gfxinfo` and `atrace` on a release build, on a mid-range phone:
|
|
391
|
+
Xiaomi 24117RN76E, MediaTek Helio G99, Android 16, 120 Hz.
|
|
386
392
|
|
|
387
|
-
| Screen | Glass views
|
|
388
|
-
| -------------------------------------------------------------------------- |
|
|
389
|
-
| Static screen, nothing moving |
|
|
390
|
-
| Scrolling a list under a glass header, a glass button and the tab bar | 3
|
|
391
|
-
| 6 glass tiles over an animated backdrop, plus glass panels and controls |
|
|
393
|
+
| Screen | Glass views | Result |
|
|
394
|
+
| -------------------------------------------------------------------------- | ----------- | ----------------------------------------------------------------------- |
|
|
395
|
+
| Static screen, nothing moving | 3 | 0 frames rendered while idle |
|
|
396
|
+
| Scrolling a list under a glass header, a glass button and the tab bar | 3 | no missed frames; 90% of frames within 14 ms |
|
|
397
|
+
| 6 glass tiles over an animated backdrop, plus glass panels and controls | 17 | about 70 fps, no missed frames; the RenderThread needs ~14 ms per frame |
|
|
392
398
|
|
|
393
399
|
In practice:
|
|
394
400
|
|
|
395
401
|
- A glass header, a tab bar and a few glass controls on a scrolling screen stay smooth.
|
|
396
|
-
-
|
|
397
|
-
|
|
402
|
+
- What adds up is the number of glass views whose backdrop changes in the same frame — glass
|
|
403
|
+
over an animation, or glass that scrolls. On mid-range GPUs, keep those to a handful; glass
|
|
404
|
+
elsewhere on the screen doesn't add to it.
|
|
398
405
|
- Don't put a glass view inside every item of a long list: each visible item captures and
|
|
399
406
|
redraws on every scroll frame.
|
|
400
407
|
- Glass views scrolled out of view don't capture again until they are visible.
|
|
@@ -405,6 +412,9 @@ In practice:
|
|
|
405
412
|
- **Glass does not show other glass views** behind it, unless it sits inside them.
|
|
406
413
|
- Inside a container that holds a glass view, branches containing glass are drawn after their
|
|
407
414
|
siblings in the backdrop, so z-order can differ from the screen in that case.
|
|
415
|
+
- Content drawn well outside its view's bounds (more than about 8 dp, e.g. a large box shadow)
|
|
416
|
+
may be missing behind glass that the view itself is away from, and so may content that a
|
|
417
|
+
distant view animates towards the glass without moving itself.
|
|
408
418
|
- `SurfaceView`-based content (most video players, camera previews, some maps) is not part of the
|
|
409
419
|
view tree's drawing and shows up empty behind the glass. `TextureView` content works.
|
|
410
420
|
- A React Native `Modal` is a separate window, so glass inside it only sees the modal's content.
|
|
@@ -82,26 +82,26 @@ abstract class GlassHostView(context: Context, appContext: AppContext) : ExpoVie
|
|
|
82
82
|
private var downY = 0f
|
|
83
83
|
private var dragClaimed = false
|
|
84
84
|
|
|
85
|
-
// Re-sample when the glass moves on screen (layout, an ancestor scrolling, transforms)
|
|
85
|
+
// Re-sample when the glass moves on screen (layout, an ancestor scrolling, transforms), when a
|
|
86
|
+
// container the capture drew inline (not by reference) scrolled — its offset is baked into the
|
|
87
|
+
// capture — or when a view the capture left out (too far away to be seen through the glass)
|
|
88
|
+
// moved and may now be. A list that is only referenced scrolls live, so scrolling it costs no
|
|
89
|
+
// capture. There is deliberately no global-layout listener: React Native lays out on every
|
|
90
|
+
// state change (e.g. each slider tick updating a label), and re-sampling every glass view each
|
|
91
|
+
// time made interactions stutter. Everything else behind the glass is referenced live (see
|
|
92
|
+
// ViewBackdropCapture).
|
|
86
93
|
private val preDrawListener = ViewTreeObserver.OnPreDrawListener {
|
|
87
94
|
getLocationOnScreen(location)
|
|
88
95
|
if (location[0] != lastLocation[0] || location[1] != lastLocation[1]) {
|
|
89
96
|
lastLocation[0] = location[0]
|
|
90
97
|
lastLocation[1] = location[1]
|
|
91
98
|
resampleIfVisible()
|
|
92
|
-
} else if (resamplePending) {
|
|
99
|
+
} else if (resamplePending || backdrop.changedSinceCapture()) {
|
|
93
100
|
resampleIfVisible()
|
|
94
101
|
}
|
|
95
102
|
true
|
|
96
103
|
}
|
|
97
104
|
|
|
98
|
-
// Scrolling changes the containers that are drawn inline into the backdrop (the ones between
|
|
99
|
-
// the window root and a glass view), so re-sample. There is deliberately no global-layout
|
|
100
|
-
// listener: React Native lays out on every state change (e.g. each slider tick updating a
|
|
101
|
-
// label), and re-sampling every glass view each time made interactions stutter. Everything
|
|
102
|
-
// else behind the glass is referenced live (see ViewBackdropCapture).
|
|
103
|
-
private val scrollListener = ViewTreeObserver.OnScrollChangedListener { resampleIfVisible() }
|
|
104
|
-
|
|
105
105
|
/**
|
|
106
106
|
* Re-samples now if any part of this view is on screen. Sampling costs a capture plus the
|
|
107
107
|
* glass effects on the GPU, so glass scrolled out of view waits until it is visible again.
|
|
@@ -288,18 +288,12 @@ abstract class GlassHostView(context: Context, appContext: AppContext) : ExpoVie
|
|
|
288
288
|
override fun onAttachedToWindow() {
|
|
289
289
|
ensureViewTreeOwners()
|
|
290
290
|
super.onAttachedToWindow()
|
|
291
|
-
observer = viewTreeObserver.also {
|
|
292
|
-
it.addOnPreDrawListener(preDrawListener)
|
|
293
|
-
it.addOnScrollChangedListener(scrollListener)
|
|
294
|
-
}
|
|
291
|
+
observer = viewTreeObserver.also { it.addOnPreDrawListener(preDrawListener) }
|
|
295
292
|
GlassRegistry.add(this)
|
|
296
293
|
}
|
|
297
294
|
|
|
298
295
|
override fun onDetachedFromWindow() {
|
|
299
|
-
observer?.takeIf { it.isAlive }?.
|
|
300
|
-
it.removeOnPreDrawListener(preDrawListener)
|
|
301
|
-
it.removeOnScrollChangedListener(scrollListener)
|
|
302
|
-
}
|
|
296
|
+
observer?.takeIf { it.isAlive }?.removeOnPreDrawListener(preDrawListener)
|
|
303
297
|
observer = null
|
|
304
298
|
lastLocation[0] = Int.MIN_VALUE
|
|
305
299
|
lastLocation[1] = Int.MIN_VALUE
|
|
@@ -66,9 +66,16 @@ internal class ViewBackdrop(private val host: GlassHostView) : Backdrop {
|
|
|
66
66
|
Snapshot.sendApplyNotifications()
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
+
/**
|
|
70
|
+
* Whether the capture is out of date although the host didn't move: something it drew inline
|
|
71
|
+
* (not by reference) scrolled, or a view it left out moved (see ViewBackdropCapture).
|
|
72
|
+
*/
|
|
73
|
+
fun changedSinceCapture(): Boolean = capture?.changedSinceCapture() ?: false
|
|
74
|
+
|
|
69
75
|
/** Frees the cached capture; the next draw records a fresh one. */
|
|
70
76
|
fun release() {
|
|
71
77
|
captureNode?.discardDisplayList()
|
|
78
|
+
capture?.clear()
|
|
72
79
|
recordedGeneration = -1
|
|
73
80
|
}
|
|
74
81
|
|
package/android/src/main/java/expo/modules/androidglassview/backdrop/highlight/HighlightModifier.kt
CHANGED
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
*
|
|
5
5
|
* Vendored into expo-android-glass-view. Changes from upstream: package relocated
|
|
6
6
|
* from com.kyant.backdrop, Kotlin Multiplatform expect/actual merged into Android-only
|
|
7
|
-
* code, dependency on io.github.kyant0:shapes removed, Kotlin 2.1 compatible syntax
|
|
7
|
+
* code, dependency on io.github.kyant0:shapes removed, Kotlin 2.1 compatible syntax,
|
|
8
|
+
* the highlight layer is only re-recorded when its shape, size, width, blur or style change,
|
|
9
|
+
* and nothing is drawn at zero alpha.
|
|
8
10
|
*/
|
|
9
11
|
package expo.modules.androidglassview.backdrop.highlight
|
|
10
12
|
|
|
@@ -25,6 +27,7 @@ import androidx.compose.ui.node.invalidateDraw
|
|
|
25
27
|
import androidx.compose.ui.node.requireGraphicsContext
|
|
26
28
|
import androidx.compose.ui.platform.InspectorInfo
|
|
27
29
|
import androidx.compose.ui.unit.Density
|
|
30
|
+
import androidx.compose.ui.unit.Dp
|
|
28
31
|
import androidx.compose.ui.unit.IntSize
|
|
29
32
|
import androidx.compose.ui.util.fastCoerceAtMost
|
|
30
33
|
import expo.modules.androidglassview.backdrop.RuntimeShaderCacheImpl
|
|
@@ -92,9 +95,17 @@ internal class HighlightNode(
|
|
|
92
95
|
|
|
93
96
|
private var prevStyle: HighlightStyle? = null
|
|
94
97
|
|
|
98
|
+
// What the layer was last recorded with. Alpha and blend mode are layer properties, so
|
|
99
|
+
// animating them (e.g. a highlight fading in on press) doesn't re-record the rim, and neither
|
|
100
|
+
// does a redraw of the node for any other reason.
|
|
101
|
+
private var recordedOutline: Outline? = null
|
|
102
|
+
private var recordedWidth = Dp.Unspecified
|
|
103
|
+
private var recordedBlurRadius = Dp.Unspecified
|
|
104
|
+
private var recordedStyle: HighlightStyle? = null
|
|
105
|
+
|
|
95
106
|
override fun ContentDrawScope.draw() {
|
|
96
107
|
val highlight = highlight()
|
|
97
|
-
if (highlight == null || highlight.width.value <= 0f) {
|
|
108
|
+
if (highlight == null || highlight.width.value <= 0f || highlight.alpha <= 0f) {
|
|
98
109
|
return drawContent()
|
|
99
110
|
}
|
|
100
111
|
|
|
@@ -106,33 +117,43 @@ internal class HighlightNode(
|
|
|
106
117
|
val density: Density = this
|
|
107
118
|
val layoutDirection = layoutDirection
|
|
108
119
|
|
|
109
|
-
|
|
110
|
-
IntSize(
|
|
111
|
-
ceil(size.width).toInt() + 2,
|
|
112
|
-
ceil(size.height).toInt() + 2
|
|
113
|
-
)
|
|
114
|
-
|
|
120
|
+
// ShapeProvider hands out the same outline while shape, size and density stay.
|
|
115
121
|
val outline = shapeProvider.shape.createOutline(size, layoutDirection, density)
|
|
116
|
-
val clipPath =
|
|
117
|
-
if (outline is Outline.Rounded) {
|
|
118
|
-
clipPath ?: Path().also { clipPath = it }
|
|
119
|
-
} else {
|
|
120
|
-
null
|
|
121
|
-
}
|
|
122
122
|
|
|
123
|
-
|
|
123
|
+
if (outline !== recordedOutline || highlight.width != recordedWidth ||
|
|
124
|
+
highlight.blurRadius != recordedBlurRadius || highlight.style != recordedStyle
|
|
125
|
+
) {
|
|
126
|
+
val safeSize =
|
|
127
|
+
IntSize(
|
|
128
|
+
ceil(size.width).toInt() + 2,
|
|
129
|
+
ceil(size.height).toInt() + 2
|
|
130
|
+
)
|
|
131
|
+
val clipPath =
|
|
132
|
+
if (outline is Outline.Rounded) {
|
|
133
|
+
clipPath ?: Path().also { clipPath = it }
|
|
134
|
+
} else {
|
|
135
|
+
null
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
configurePaint(highlight)
|
|
139
|
+
|
|
140
|
+
highlightLayer.record(safeSize) {
|
|
141
|
+
translate(1f, 1f) {
|
|
142
|
+
val canvas = drawContext.canvas
|
|
143
|
+
canvas.save()
|
|
144
|
+
canvas.clipOutline(outline, clipPath)
|
|
145
|
+
canvas.drawOutline(outline, paint)
|
|
146
|
+
canvas.restore()
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
recordedOutline = outline
|
|
150
|
+
recordedWidth = highlight.width
|
|
151
|
+
recordedBlurRadius = highlight.blurRadius
|
|
152
|
+
recordedStyle = highlight.style
|
|
153
|
+
}
|
|
124
154
|
|
|
125
155
|
highlightLayer.alpha = highlight.alpha
|
|
126
156
|
highlightLayer.blendMode = highlight.style.blendMode
|
|
127
|
-
highlightLayer.record(safeSize) {
|
|
128
|
-
translate(1f, 1f) {
|
|
129
|
-
val canvas = drawContext.canvas
|
|
130
|
-
canvas.save()
|
|
131
|
-
canvas.clipOutline(outline, clipPath)
|
|
132
|
-
canvas.drawOutline(outline, paint)
|
|
133
|
-
canvas.restore()
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
157
|
|
|
137
158
|
translate(-1f, -1f) {
|
|
138
159
|
drawLayer(highlightLayer)
|
|
@@ -154,6 +175,7 @@ internal class HighlightNode(
|
|
|
154
175
|
clipPath = null
|
|
155
176
|
runtimeShaderCache.clear()
|
|
156
177
|
prevStyle = null
|
|
178
|
+
recordedOutline = null
|
|
157
179
|
}
|
|
158
180
|
|
|
159
181
|
private fun DrawScope.configurePaint(highlight: Highlight) {
|
package/android/src/main/java/expo/modules/androidglassview/backdrop/shadow/InnerShadowModifier.kt
CHANGED
|
@@ -4,13 +4,16 @@
|
|
|
4
4
|
*
|
|
5
5
|
* Vendored into expo-android-glass-view. Changes from upstream: package relocated
|
|
6
6
|
* from com.kyant.backdrop, Kotlin Multiplatform expect/actual merged into Android-only
|
|
7
|
-
* code, dependency on io.github.kyant0:shapes removed, Kotlin 2.1 compatible syntax
|
|
7
|
+
* code, dependency on io.github.kyant0:shapes removed, Kotlin 2.1 compatible syntax,
|
|
8
|
+
* the shadow layer is only re-recorded when its shape, size, offset or colour change, and
|
|
9
|
+
* nothing is drawn at zero alpha.
|
|
8
10
|
*/
|
|
9
11
|
package expo.modules.androidglassview.backdrop.shadow
|
|
10
12
|
|
|
11
13
|
import androidx.compose.ui.Modifier
|
|
12
14
|
import androidx.compose.ui.graphics.BlendMode
|
|
13
15
|
import androidx.compose.ui.graphics.BlurEffect
|
|
16
|
+
import androidx.compose.ui.graphics.Color
|
|
14
17
|
import androidx.compose.ui.graphics.Outline
|
|
15
18
|
import androidx.compose.ui.graphics.Paint
|
|
16
19
|
import androidx.compose.ui.graphics.Path
|
|
@@ -83,12 +86,20 @@ internal class InnerShadowNode(
|
|
|
83
86
|
|
|
84
87
|
private var prevRadius = Float.NaN
|
|
85
88
|
|
|
89
|
+
// What the layer was last recorded with. Radius (a render effect), alpha and blend mode are
|
|
90
|
+
// layer properties, so animating them doesn't re-record the shape.
|
|
91
|
+
private var recordedOutline: Outline? = null
|
|
92
|
+
private var recordedOffsetX = Float.NaN
|
|
93
|
+
private var recordedOffsetY = Float.NaN
|
|
94
|
+
private var recordedColor = Color.Unspecified
|
|
95
|
+
|
|
86
96
|
override fun ContentDrawScope.draw() {
|
|
87
97
|
drawContent()
|
|
88
98
|
|
|
89
99
|
if (!isRenderEffectSupported()) return
|
|
90
100
|
|
|
91
101
|
val shadow = shadow() ?: return
|
|
102
|
+
if (shadow.alpha <= 0f) return
|
|
92
103
|
|
|
93
104
|
val shadowLayer = shadowLayer
|
|
94
105
|
if (shadowLayer != null) {
|
|
@@ -108,8 +119,6 @@ internal class InnerShadowNode(
|
|
|
108
119
|
null
|
|
109
120
|
}
|
|
110
121
|
|
|
111
|
-
configurePaint(shadow)
|
|
112
|
-
|
|
113
122
|
shadowLayer.alpha = shadow.alpha
|
|
114
123
|
shadowLayer.blendMode = shadow.blendMode
|
|
115
124
|
if (prevRadius != radius) {
|
|
@@ -121,15 +130,25 @@ internal class InnerShadowNode(
|
|
|
121
130
|
}
|
|
122
131
|
prevRadius = radius
|
|
123
132
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
+
// ShapeProvider hands out the same outline while shape, size and density stay.
|
|
134
|
+
if (outline !== recordedOutline || offsetX != recordedOffsetX ||
|
|
135
|
+
offsetY != recordedOffsetY || shadow.color != recordedColor
|
|
136
|
+
) {
|
|
137
|
+
configurePaint(shadow)
|
|
138
|
+
shadowLayer.record {
|
|
139
|
+
val canvas = drawContext.canvas
|
|
140
|
+
canvas.save()
|
|
141
|
+
canvas.clipOutline(outline, clipPath)
|
|
142
|
+
canvas.drawOutline(outline, paint)
|
|
143
|
+
canvas.translate(offsetX, offsetY)
|
|
144
|
+
canvas.drawOutline(outline, ShadowMaskPaint)
|
|
145
|
+
canvas.translate(-offsetX, -offsetY)
|
|
146
|
+
canvas.restore()
|
|
147
|
+
}
|
|
148
|
+
recordedOutline = outline
|
|
149
|
+
recordedOffsetX = offsetX
|
|
150
|
+
recordedOffsetY = offsetY
|
|
151
|
+
recordedColor = shadow.color
|
|
133
152
|
}
|
|
134
153
|
|
|
135
154
|
val canvas = drawContext.canvas
|
|
@@ -154,6 +173,8 @@ internal class InnerShadowNode(
|
|
|
154
173
|
graphicsContext.releaseGraphicsLayer(layer)
|
|
155
174
|
shadowLayer = null
|
|
156
175
|
}
|
|
176
|
+
prevRadius = Float.NaN
|
|
177
|
+
recordedOutline = null
|
|
157
178
|
}
|
|
158
179
|
|
|
159
180
|
private fun DrawScope.configurePaint(shadow: InnerShadow) {
|
package/android/src/main/java/expo/modules/androidglassview/backdrop/shadow/ShadowModifier.kt
CHANGED
|
@@ -4,12 +4,16 @@
|
|
|
4
4
|
*
|
|
5
5
|
* Vendored into expo-android-glass-view. Changes from upstream: package relocated
|
|
6
6
|
* from com.kyant.backdrop, Kotlin Multiplatform expect/actual merged into Android-only
|
|
7
|
-
* code, dependency on io.github.kyant0:shapes removed, Kotlin 2.1 compatible syntax
|
|
7
|
+
* code, dependency on io.github.kyant0:shapes removed, Kotlin 2.1 compatible syntax,
|
|
8
|
+
* the shadow layer is only re-recorded when its shape, size, radius, offset or colour change,
|
|
9
|
+
* and nothing is drawn at zero alpha.
|
|
8
10
|
*/
|
|
9
11
|
package expo.modules.androidglassview.backdrop.shadow
|
|
10
12
|
|
|
11
13
|
import androidx.compose.ui.Modifier
|
|
12
14
|
import androidx.compose.ui.graphics.BlendMode
|
|
15
|
+
import androidx.compose.ui.graphics.Color
|
|
16
|
+
import androidx.compose.ui.graphics.Outline
|
|
13
17
|
import androidx.compose.ui.graphics.Paint
|
|
14
18
|
import androidx.compose.ui.graphics.drawOutline
|
|
15
19
|
import androidx.compose.ui.graphics.drawscope.ContentDrawScope
|
|
@@ -78,8 +82,18 @@ internal class ShadowNode(
|
|
|
78
82
|
|
|
79
83
|
private val paint = Paint()
|
|
80
84
|
|
|
85
|
+
// What the layer was last recorded with. Alpha and blend mode are layer properties, so
|
|
86
|
+
// animating them (e.g. a shadow fading in on press) doesn't re-record the blurred shape, and
|
|
87
|
+
// neither does a redraw of the node for any other reason.
|
|
88
|
+
private var recordedOutline: Outline? = null
|
|
89
|
+
private var recordedRadius = Float.NaN
|
|
90
|
+
private var recordedOffsetX = Float.NaN
|
|
91
|
+
private var recordedOffsetY = Float.NaN
|
|
92
|
+
private var recordedColor = Color.Unspecified
|
|
93
|
+
|
|
81
94
|
override fun ContentDrawScope.draw() {
|
|
82
|
-
val shadow = shadow()
|
|
95
|
+
val shadow = shadow()
|
|
96
|
+
if (shadow == null || shadow.alpha <= 0f) return drawContent()
|
|
83
97
|
|
|
84
98
|
val shadowLayer = shadowLayer
|
|
85
99
|
if (shadowLayer != null) {
|
|
@@ -90,25 +104,36 @@ internal class ShadowNode(
|
|
|
90
104
|
val radius = shadow.radius.toPx()
|
|
91
105
|
val offsetX = shadow.offset.x.toPx()
|
|
92
106
|
val offsetY = shadow.offset.y.toPx()
|
|
93
|
-
|
|
94
|
-
ceil(size.width + radius * 4f + offsetX).toInt(),
|
|
95
|
-
ceil(size.height + radius * 4f + offsetY).toInt()
|
|
96
|
-
)
|
|
107
|
+
// ShapeProvider hands out the same outline while shape, size and density stay.
|
|
97
108
|
val outline = shapeProvider.shape.createOutline(size, layoutDirection, density)
|
|
98
109
|
|
|
99
|
-
|
|
110
|
+
if (outline !== recordedOutline || radius != recordedRadius ||
|
|
111
|
+
offsetX != recordedOffsetX || offsetY != recordedOffsetY ||
|
|
112
|
+
shadow.color != recordedColor
|
|
113
|
+
) {
|
|
114
|
+
val shadowSize = IntSize(
|
|
115
|
+
ceil(size.width + radius * 4f + offsetX).toInt(),
|
|
116
|
+
ceil(size.height + radius * 4f + offsetY).toInt()
|
|
117
|
+
)
|
|
118
|
+
configurePaint(shadow)
|
|
119
|
+
shadowLayer.record(shadowSize) {
|
|
120
|
+
translate(radius * 2f + offsetX, radius * 2f + offsetY) {
|
|
121
|
+
val canvas = drawContext.canvas
|
|
122
|
+
canvas.drawOutline(outline, paint)
|
|
123
|
+
canvas.translate(-offsetX, -offsetY)
|
|
124
|
+
canvas.drawOutline(outline, ShadowMaskPaint)
|
|
125
|
+
canvas.translate(offsetX, offsetY)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
recordedOutline = outline
|
|
129
|
+
recordedRadius = radius
|
|
130
|
+
recordedOffsetX = offsetX
|
|
131
|
+
recordedOffsetY = offsetY
|
|
132
|
+
recordedColor = shadow.color
|
|
133
|
+
}
|
|
100
134
|
|
|
101
135
|
shadowLayer.alpha = shadow.alpha
|
|
102
136
|
shadowLayer.blendMode = shadow.blendMode
|
|
103
|
-
shadowLayer.record(shadowSize) {
|
|
104
|
-
translate(radius * 2f + offsetX, radius * 2f + offsetY) {
|
|
105
|
-
val canvas = drawContext.canvas
|
|
106
|
-
canvas.drawOutline(outline, paint)
|
|
107
|
-
canvas.translate(-offsetX, -offsetY)
|
|
108
|
-
canvas.drawOutline(outline, ShadowMaskPaint)
|
|
109
|
-
canvas.translate(offsetX, offsetY)
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
137
|
|
|
113
138
|
translate(-radius * 2f, -radius * 2f) {
|
|
114
139
|
drawLayer(shadowLayer)
|
|
@@ -132,6 +157,7 @@ internal class ShadowNode(
|
|
|
132
157
|
graphicsContext.releaseGraphicsLayer(layer)
|
|
133
158
|
shadowLayer = null
|
|
134
159
|
}
|
|
160
|
+
recordedOutline = null
|
|
135
161
|
}
|
|
136
162
|
|
|
137
163
|
private fun DrawScope.configurePaint(shadow: Shadow) {
|
|
@@ -6,16 +6,19 @@
|
|
|
6
6
|
* Changes from upstream: renamed, comments translated, generalised from "skip the one glass
|
|
7
7
|
* that is sampling" to "skip every other glass view", glass ancestors of the sampling view are
|
|
8
8
|
* drawn instead of skipped, expanded branches are positioned with the framework's own child
|
|
9
|
-
* transform (left/top, scroll, matrix, alpha) instead of screen deltas,
|
|
10
|
-
* sampling view are drawn child by child instead of through their own draw()
|
|
9
|
+
* transform (left/top, scroll, matrix, alpha) instead of screen deltas, the ancestors of the
|
|
10
|
+
* sampling view are drawn child by child instead of through their own draw(), and children that
|
|
11
|
+
* cannot reach the recorded area are left out (culled).
|
|
11
12
|
*/
|
|
12
13
|
package expo.modules.androidglassview.capture
|
|
13
14
|
|
|
14
15
|
import android.graphics.Canvas
|
|
16
|
+
import android.graphics.RectF
|
|
15
17
|
import android.os.Build
|
|
16
18
|
import android.view.View
|
|
17
19
|
import android.view.ViewGroup
|
|
18
20
|
import androidx.annotation.RequiresApi
|
|
21
|
+
import kotlin.math.max
|
|
19
22
|
|
|
20
23
|
/**
|
|
21
24
|
* Draws a source view's content into a canvas while leaving glass views out of it.
|
|
@@ -54,10 +57,22 @@ import androidx.annotation.RequiresApi
|
|
|
54
57
|
* Glass views that are *ancestors* of the sampling one are not skipped: glass on top of a glass
|
|
55
58
|
* card sees the card. That cannot form a cycle, because an ancestor never samples its own subtree.
|
|
56
59
|
*
|
|
60
|
+
* **Culling.** The capture only covers the area around the glass, but a reference to a subtree
|
|
61
|
+
* anywhere on screen makes the RenderThread re-render this glass — capture, blur and refraction —
|
|
62
|
+
* whenever that subtree changes, even when none of it can be seen through the glass. With a few
|
|
63
|
+
* glass views on a screen with an animation, that is every glass view on every frame. So a child
|
|
64
|
+
* is left out when nothing it draws can reach the recorded area ([reachesRecordedArea]): neither
|
|
65
|
+
* its bounds (plus a margin for shadows) nor, as long as it doesn't clip them, those of its
|
|
66
|
+
* descendants. A left-out child is not referenced, so it is not live: [changedSinceCapture]
|
|
67
|
+
* reports when one of them moved, resized or scrolled, and the glass then captures again.
|
|
68
|
+
*
|
|
57
69
|
* Known trade-offs: the children of ancestors are drawn without their view Animation and
|
|
58
70
|
* elevation shadow; inside an expanded container that is not an ancestor, glass-containing
|
|
59
71
|
* branches are drawn after their siblings, so a sibling that normally paints above such a branch
|
|
60
|
-
* ends up below it in the backdrop
|
|
72
|
+
* ends up below it in the backdrop; content drawn well outside its view's bounds (farther than
|
|
73
|
+
* the cull margin, e.g. a large box shadow) is missing from glass views that its view is away
|
|
74
|
+
* from, and so is content a left-out subtree animates into the glass area without its own root
|
|
75
|
+
* moving.
|
|
61
76
|
*/
|
|
62
77
|
@RequiresApi(Build.VERSION_CODES.Q) // View.setTransitionVisibility, ViewGroup.getChildDrawingOrder(int)
|
|
63
78
|
internal class ViewBackdropCapture {
|
|
@@ -77,7 +92,101 @@ internal class ViewBackdropCapture {
|
|
|
77
92
|
private val hidden = ArrayList<View>()
|
|
78
93
|
|
|
79
94
|
/**
|
|
80
|
-
*
|
|
95
|
+
* Views whose scroll offset the last capture applied itself, with the offsets it used (x, y
|
|
96
|
+
* pairs in [bakedScroll]). Every other scrolling view is referenced through its RenderNode, so
|
|
97
|
+
* the capture follows it without being recorded again.
|
|
98
|
+
*/
|
|
99
|
+
private val bakedViews = ArrayList<View>()
|
|
100
|
+
private var bakedScroll = IntArray(32)
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Children the last capture left out, with their geometry at the time ([GEOMETRY_FIELDS]
|
|
104
|
+
* values each in [culledGeometry]); [culledSet] holds the same views for lookups.
|
|
105
|
+
*/
|
|
106
|
+
private val culledViews = ArrayList<View>()
|
|
107
|
+
private val culledSet = HashSet<View>()
|
|
108
|
+
private var culledGeometry = FloatArray(GEOMETRY_FIELDS * 16)
|
|
109
|
+
|
|
110
|
+
/** Margin around a view's bounds that its drawing may still reach (shadows, borders), in px. */
|
|
111
|
+
private var cullPadding = 0f
|
|
112
|
+
private val cullRect = RectF()
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Whether the last capture is out of date although the glass itself didn't move: a view whose
|
|
116
|
+
* scroll offset it baked in has scrolled, or a view it left out has moved, resized or scrolled
|
|
117
|
+
* (and may now reach the glass).
|
|
118
|
+
*/
|
|
119
|
+
fun changedSinceCapture(): Boolean {
|
|
120
|
+
for (i in bakedViews.indices) {
|
|
121
|
+
val view = bakedViews[i]
|
|
122
|
+
if (view.scrollX != bakedScroll[i * 2] || view.scrollY != bakedScroll[i * 2 + 1]) return true
|
|
123
|
+
}
|
|
124
|
+
for (i in culledViews.indices) {
|
|
125
|
+
if (!hasGeometry(culledViews[i], i * GEOMETRY_FIELDS)) return true
|
|
126
|
+
}
|
|
127
|
+
return false
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Forgets the views of the last capture (they may belong to a window that is going away). */
|
|
131
|
+
fun clear() {
|
|
132
|
+
bakedViews.clear()
|
|
133
|
+
culledViews.clear()
|
|
134
|
+
culledSet.clear()
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
private fun bakeScroll(view: View) {
|
|
138
|
+
val index = bakedViews.size
|
|
139
|
+
if (bakedScroll.size < (index + 1) * 2) bakedScroll = bakedScroll.copyOf(bakedScroll.size * 2)
|
|
140
|
+
bakedViews.add(view)
|
|
141
|
+
bakedScroll[index * 2] = view.scrollX
|
|
142
|
+
bakedScroll[index * 2 + 1] = view.scrollY
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private fun cull(view: View) {
|
|
146
|
+
val offset = culledViews.size * GEOMETRY_FIELDS
|
|
147
|
+
if (culledGeometry.size < offset + GEOMETRY_FIELDS) {
|
|
148
|
+
culledGeometry = culledGeometry.copyOf(culledGeometry.size * 2)
|
|
149
|
+
}
|
|
150
|
+
culledViews.add(view)
|
|
151
|
+
culledSet.add(view)
|
|
152
|
+
val g = culledGeometry
|
|
153
|
+
g[offset] = view.left.toFloat()
|
|
154
|
+
g[offset + 1] = view.top.toFloat()
|
|
155
|
+
g[offset + 2] = view.right.toFloat()
|
|
156
|
+
g[offset + 3] = view.bottom.toFloat()
|
|
157
|
+
g[offset + 4] = view.translationX
|
|
158
|
+
g[offset + 5] = view.translationY
|
|
159
|
+
g[offset + 6] = view.scaleX
|
|
160
|
+
g[offset + 7] = view.scaleY
|
|
161
|
+
g[offset + 8] = view.rotation
|
|
162
|
+
g[offset + 9] = view.rotationX
|
|
163
|
+
g[offset + 10] = view.rotationY
|
|
164
|
+
g[offset + 11] = view.scrollX.toFloat()
|
|
165
|
+
g[offset + 12] = view.scrollY.toFloat()
|
|
166
|
+
g[offset + 13] = view.z
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
private fun hasGeometry(view: View, offset: Int): Boolean {
|
|
170
|
+
val g = culledGeometry
|
|
171
|
+
return g[offset] == view.left.toFloat() &&
|
|
172
|
+
g[offset + 1] == view.top.toFloat() &&
|
|
173
|
+
g[offset + 2] == view.right.toFloat() &&
|
|
174
|
+
g[offset + 3] == view.bottom.toFloat() &&
|
|
175
|
+
g[offset + 4] == view.translationX &&
|
|
176
|
+
g[offset + 5] == view.translationY &&
|
|
177
|
+
g[offset + 6] == view.scaleX &&
|
|
178
|
+
g[offset + 7] == view.scaleY &&
|
|
179
|
+
g[offset + 8] == view.rotation &&
|
|
180
|
+
g[offset + 9] == view.rotationX &&
|
|
181
|
+
g[offset + 10] == view.rotationY &&
|
|
182
|
+
g[offset + 11] == view.scrollX.toFloat() &&
|
|
183
|
+
g[offset + 12] == view.scrollY.toFloat() &&
|
|
184
|
+
g[offset + 13] == view.z
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* @param canvas a hardware canvas whose origin is aligned with [source]'s top-left corner, and
|
|
189
|
+
* whose clip is the area to record (children that cannot reach it are left out).
|
|
81
190
|
* @param self the glass view that is sampling.
|
|
82
191
|
* @param glassViews every glass view; the ones outside [source] are ignored.
|
|
83
192
|
*/
|
|
@@ -85,6 +194,10 @@ internal class ViewBackdropCapture {
|
|
|
85
194
|
expanded.clear()
|
|
86
195
|
excluded.clear()
|
|
87
196
|
selfPath.clear()
|
|
197
|
+
bakedViews.clear()
|
|
198
|
+
culledViews.clear()
|
|
199
|
+
culledSet.clear()
|
|
200
|
+
cullPadding = CULL_PADDING_DP * source.resources.displayMetrics.density
|
|
88
201
|
|
|
89
202
|
var ancestor = self.parent
|
|
90
203
|
while (ancestor is View) {
|
|
@@ -136,10 +249,14 @@ internal class ViewBackdropCapture {
|
|
|
136
249
|
if (group != null) {
|
|
137
250
|
for (i in 0 until group.childCount) {
|
|
138
251
|
val child = group.getChildAt(i)
|
|
139
|
-
if (child.visibility
|
|
252
|
+
if (child.visibility != View.VISIBLE) continue
|
|
253
|
+
val outside = child !in excluded && child.animation == null &&
|
|
254
|
+
!reachesRecordedArea(canvas, child, (child.left - host.scrollX).toFloat(), (child.top - host.scrollY).toFloat())
|
|
255
|
+
if (child in excluded || child in expanded || outside) {
|
|
140
256
|
// Only flips the visibility flag; does not invalidate anything.
|
|
141
257
|
child.setTransitionVisibility(View.INVISIBLE)
|
|
142
258
|
hidden.add(child)
|
|
259
|
+
if (outside) cull(child)
|
|
143
260
|
}
|
|
144
261
|
}
|
|
145
262
|
}
|
|
@@ -149,6 +266,7 @@ internal class ViewBackdropCapture {
|
|
|
149
266
|
try {
|
|
150
267
|
// The public View.draw() does not apply the view's own scroll offset (the framework does
|
|
151
268
|
// that in updateDisplayListIfDirty), so apply it here.
|
|
269
|
+
bakeScroll(host)
|
|
152
270
|
canvas.translate(-host.scrollX.toFloat(), -host.scrollY.toFloat())
|
|
153
271
|
host.draw(canvas)
|
|
154
272
|
} finally {
|
|
@@ -162,7 +280,7 @@ internal class ViewBackdropCapture {
|
|
|
162
280
|
val clipChildren = group.clipChildren
|
|
163
281
|
for (i in firstHidden until lastHidden) {
|
|
164
282
|
val child = hidden[i]
|
|
165
|
-
if (child in excluded) continue
|
|
283
|
+
if (child in excluded || child in culledSet) continue
|
|
166
284
|
val childSave = canvas.save()
|
|
167
285
|
// Same transform the parent applies when it draws the child normally.
|
|
168
286
|
canvas.translate((child.left - host.scrollX).toFloat(), (child.top - host.scrollY).toFloat())
|
|
@@ -186,18 +304,24 @@ internal class ViewBackdropCapture {
|
|
|
186
304
|
try {
|
|
187
305
|
// View.draw() paints the background at the view's origin, whatever its scroll offset.
|
|
188
306
|
host.background?.draw(canvas)
|
|
307
|
+
bakeScroll(host)
|
|
189
308
|
canvas.translate(-host.scrollX.toFloat(), -host.scrollY.toFloat())
|
|
190
309
|
val clipChildren = host.clipChildren
|
|
191
310
|
for (child in childrenInDrawingOrder(host)) {
|
|
192
311
|
if (child in excluded) continue
|
|
193
312
|
// Same visibility rule as ViewGroup.dispatchDraw.
|
|
194
313
|
if (child.visibility != View.VISIBLE && child.animation == null) continue
|
|
314
|
+
if (child.animation == null && !reachesRecordedArea(canvas, child, child.left.toFloat(), child.top.toFloat())) {
|
|
315
|
+
cull(child)
|
|
316
|
+
continue
|
|
317
|
+
}
|
|
195
318
|
val childSave = canvas.save()
|
|
196
319
|
canvas.translate(child.left.toFloat(), child.top.toFloat())
|
|
197
320
|
applyChildTransform(canvas, child, clipChildren)
|
|
198
321
|
if (child in expanded) {
|
|
199
322
|
drawLevel(canvas, child)
|
|
200
323
|
} else {
|
|
324
|
+
bakeScroll(child)
|
|
201
325
|
canvas.translate(-child.scrollX.toFloat(), -child.scrollY.toFloat())
|
|
202
326
|
child.draw(canvas)
|
|
203
327
|
}
|
|
@@ -208,6 +332,37 @@ internal class ViewBackdropCapture {
|
|
|
208
332
|
}
|
|
209
333
|
}
|
|
210
334
|
|
|
335
|
+
/**
|
|
336
|
+
* Whether anything [view] draws can land in the area being recorded (the canvas clip). [x], [y]
|
|
337
|
+
* is the view's position in the current canvas coordinates. A view that doesn't clip its
|
|
338
|
+
* children lets them draw outside its bounds, so they are checked too, recursively.
|
|
339
|
+
*/
|
|
340
|
+
private fun reachesRecordedArea(canvas: Canvas, view: View, x: Float, y: Float): Boolean {
|
|
341
|
+
val save = canvas.save()
|
|
342
|
+
try {
|
|
343
|
+
canvas.translate(x, y)
|
|
344
|
+
val matrix = view.matrix
|
|
345
|
+
if (!matrix.isIdentity) canvas.concat(matrix)
|
|
346
|
+
val padding = cullPadding + max(view.z, 0f) * 2f
|
|
347
|
+
cullRect.set(-padding, -padding, view.width + padding, view.height + padding)
|
|
348
|
+
@Suppress("DEPRECATION") // quickReject(RectF) needs API 30
|
|
349
|
+
if (!canvas.quickReject(cullRect, Canvas.EdgeType.BW)) return true
|
|
350
|
+
if (view is ViewGroup && !view.clipChildren) {
|
|
351
|
+
for (i in 0 until view.childCount) {
|
|
352
|
+
val child = view.getChildAt(i)
|
|
353
|
+
if (child.visibility != View.VISIBLE && child.animation == null) continue
|
|
354
|
+
if (child.animation != null) return true
|
|
355
|
+
val childX = (child.left - view.scrollX).toFloat()
|
|
356
|
+
val childY = (child.top - view.scrollY).toFloat()
|
|
357
|
+
if (reachesRecordedArea(canvas, child, childX, childY)) return true
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return false
|
|
361
|
+
} finally {
|
|
362
|
+
canvas.restoreToCount(save)
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
211
366
|
/** The child's matrix, clip and alpha, as its parent applies them; the canvas is at its left/top. */
|
|
212
367
|
private fun applyChildTransform(canvas: Canvas, child: View, clipChildren: Boolean) {
|
|
213
368
|
val matrix = child.matrix
|
|
@@ -237,6 +392,12 @@ internal class ViewBackdropCapture {
|
|
|
237
392
|
companion object {
|
|
238
393
|
private var depth = 0
|
|
239
394
|
|
|
395
|
+
/** Geometry values stored per culled view (bounds, transform, scroll, z). */
|
|
396
|
+
private const val GEOMETRY_FIELDS = 14
|
|
397
|
+
|
|
398
|
+
/** How far outside its bounds a view's drawing is assumed to reach (shadows, borders). */
|
|
399
|
+
private const val CULL_PADDING_DP = 8f
|
|
400
|
+
|
|
240
401
|
/** True while any glass view is sampling (main thread only). */
|
|
241
402
|
val isCapturing: Boolean
|
|
242
403
|
get() = depth > 0
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "expo-android-glass-view",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "Liquid Glass views, buttons, sliders and toggles for React Native on Android (Jetpack Compose, powered by Kyant0/AndroidLiquidGlass)",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|