Skip to content

Commit 7fbdc4c

Browse files
fix: Fix compose coordinate offset. (#331)
- Fix compose coordinate offset. - Fix crash if unsuccessful kind of view was sent to draw into canvas <img width="725" height="585" alt="image" src="https://github.com/user-attachments/assets/070420ed-69b9-4c6a-b76b-4c4b69489f8e" /> <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Fixes Compose mask polygon coordinates and avoids crashes by safely handling Canvas draw failures during capture. > > - **Masking (Compose)**: > - Correct coordinate calculation for polygons by using `localToScreen(...)` points directly (remove root offset adjustment) in `ComposeMaskTarget.points`. > - **Capture**: > - Make `canvasDraw` return `Bitmap?` and wrap `view.draw` in try/catch to prevent crashes; recycle bitmap on failure. > - Log window capture/draw failures with `warn` level in `CaptureSource`. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit a7cbe59. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent ed2b645 commit 7fbdc4c

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

sdk/@launchdarkly/observability-android/lib/src/main/kotlin/com/launchdarkly/observability/replay/capture/CaptureSource.kt

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class CaptureSource(
200200
view: View,
201201
rect: Rect,
202202
): Bitmap? {
203-
val bitmap = createBitmap(view.width, view.height)
203+
val bitmap = createBitmapForView(view) ?: return null
204204

205205
return suspendCancellableCoroutine { continuation ->
206206
val handler = Handler(Looper.getMainLooper())
@@ -220,22 +220,38 @@ class CaptureSource(
220220
)
221221
} catch (exp: Exception) {
222222
// It could normally happen when view is being closed during screenshot
223-
logger.info("Failed to capture window", exp)
223+
logger.warn("Failed to capture window", exp)
224224
continuation.resume(null)
225225
}
226226
}
227227
}
228228

229229
private fun canvasDraw(
230230
view: View
231-
): Bitmap {
232-
val bitmap = createBitmap(view.width, view.height)
231+
): Bitmap? {
232+
val bitmap = createBitmapForView(view) ?: return null
233233

234234
val canvas = Canvas(bitmap)
235-
view.draw(canvas)
235+
try {
236+
view.draw(canvas)
237+
} catch (t: Throwable) {
238+
logger.warn("Failed to draw Canvas. This view might be better processed by PixelCopy", t)
239+
bitmap.recycle()
240+
return null
241+
}
236242
return bitmap
237243
}
238244

245+
private fun createBitmapForView(view: View): Bitmap? {
246+
val width = view.width
247+
val height = view.height
248+
if (width <= 0 || height <= 0) {
249+
logger.warn("Cannot draw view with zero dimensions: ${view.width}x${view.height}")
250+
return null
251+
}
252+
return createBitmap(width, height)
253+
}
254+
239255
private fun createCaptureEvent(
240256
postMask: Bitmap,
241257
rect: Rect,

sdk/@launchdarkly/observability-android/lib/src/main/kotlin/com/launchdarkly/observability/replay/masking/ComposeMaskTarget.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,6 @@ data class ComposeMaskTarget(
135135
t4.x, t4.y
136136
)
137137

138-
for (i in pts.indices step 2) {
139-
pts[i] -= context.rootX
140-
pts[i + 1] -= context.rootY
141-
}
142-
143138
return pts
144139
}
145140
}

0 commit comments

Comments
 (0)