Skip to content

Commit 2644ad7

Browse files
zeyapfacebook-github-bot
authored andcommitted
make sure view width and height are non 0 before calling createBitmap()
Summary: ## Changelog: [Android] [Fixed] - make sure view width and height are non 0 before calling createBitmap() Differential Revision: D102657602
1 parent 44a4b1b commit 2644ad7

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/ViewTransitionSnapshotManager.kt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,13 @@ internal class ViewTransitionSnapshotManager(
106106
val location = IntArray(2)
107107
view.getLocationInWindow(location)
108108

109+
// Capture dimensions now — the view may be resized or removed by the time
110+
// the async PixelCopy callback fires.
111+
val viewWidth = view.width
112+
val viewHeight = view.height
113+
109114
// The view's rect in window coordinates.
110-
val viewRect =
111-
Rect(location[0], location[1], location[0] + view.width, location[1] + view.height)
115+
val viewRect = Rect(location[0], location[1], location[0] + viewWidth, location[1] + viewHeight)
112116

113117
// Clamp to window bounds — PixelCopy only captures what's visible on the
114118
// window surface. Without clamping, off-screen portions are black/empty
@@ -144,23 +148,28 @@ internal class ViewTransitionSnapshotManager(
144148
if (copyResult == PixelCopy.SUCCESS) {
145149
// Compose the clamped capture into a full-size bitmap at the
146150
// correct offset so it aligns with the pseudo-element's bounds.
147-
val fullBitmap = createBitmap(view.width, view.height)
151+
val fullBitmap = createBitmap(viewWidth, viewHeight)
148152
Canvas(fullBitmap)
149153
.drawBitmap(clampedBitmap, offsetX.toFloat(), offsetY.toFloat(), null)
150154
clampedBitmap.recycle()
151155
onBitmapCaptured(reactTag, fullBitmap)
152156
} else {
157+
// Fall back to software rendering if the view is still valid.
153158
clampedBitmap.recycle()
154-
onBitmapCaptured(reactTag, captureSoftwareBitmap(view))
159+
if (view.width > 0 && view.height > 0) {
160+
onBitmapCaptured(reactTag, captureSoftwareBitmap(view))
161+
}
155162
}
156163
},
157164
mainHandler,
158165
)
159166
} catch (e: IllegalArgumentException) {
160167
// Window surface may have been destroyed (e.g., device idle/sleep).
161-
// Fall back to software rendering.
168+
// Fall back to software rendering if the view is still valid.
162169
clampedBitmap.recycle()
163-
onBitmapCaptured(reactTag, captureSoftwareBitmap(view))
170+
if (view.width > 0 && view.height > 0) {
171+
onBitmapCaptured(reactTag, captureSoftwareBitmap(view))
172+
}
164173
}
165174
}
166175

0 commit comments

Comments
 (0)