Skip to content

Add masking debug overlay #4357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.sentry.android.replay

import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.content.Context
import android.graphics.Bitmap
Expand All @@ -21,6 +22,7 @@ import io.sentry.SentryLevel.INFO
import io.sentry.SentryLevel.WARNING
import io.sentry.SentryOptions
import io.sentry.SentryReplayOptions
import io.sentry.android.replay.util.DebugOverlayDrawable
import io.sentry.android.replay.util.MainLooperHandler
import io.sentry.android.replay.util.addOnDrawListenerSafe
import io.sentry.android.replay.util.getVisibleRects
Expand All @@ -37,6 +39,7 @@ import java.util.concurrent.atomic.AtomicBoolean
import kotlin.LazyThreadSafetyMode.NONE
import kotlin.math.roundToInt

@SuppressLint("UseKtx")
@TargetApi(26)
internal class ScreenshotRecorder(
val config: ScreenshotRecorderConfig,
Expand All @@ -46,6 +49,10 @@ internal class ScreenshotRecorder(
private val screenshotRecorderCallback: ScreenshotRecorderCallback?
) : ViewTreeObserver.OnDrawListener {

private companion object {
private const val DEBUG_MODE = false
}

private var rootView: WeakReference<View>? = null
private val maskingPaint by lazy(NONE) { Paint() }
private val singlePixelBitmap: Bitmap by lazy(NONE) {
Expand All @@ -70,6 +77,8 @@ internal class ScreenshotRecorder(
private val isCapturing = AtomicBoolean(true)
private val lastCaptureSuccessful = AtomicBoolean(false)

private val debugOverlayDrawable = DebugOverlayDrawable()

fun capture() {
if (!isCapturing.get()) {
if (options.sessionReplay.isDebug) {
Expand Down Expand Up @@ -121,6 +130,8 @@ internal class ScreenshotRecorder(
root.traverse(viewHierarchy, options)

recorder.submitSafely(options, "screenshot_recorder.mask") {
val debugMasks = mutableListOf<Rect>()

val canvas = Canvas(screenshot)
canvas.setMatrix(prescaledMatrix)
viewHierarchy.traverse { node ->
Expand Down Expand Up @@ -158,10 +169,16 @@ internal class ScreenshotRecorder(
visibleRects.forEach { rect ->
canvas.drawRoundRect(RectF(rect), 10f, 10f, maskingPaint)
}
if (DEBUG_MODE) {
debugMasks.addAll(visibleRects)
}
}
return@traverse true
}

if (DEBUG_MODE) {
mainLooperHandler.post { debugOverlayDrawable.update(debugMasks) }
}
screenshotRecorderCallback?.onScreenshotRecorded(screenshot)
lastCaptureSuccessful.set(true)
contentChanged.set(false)
Expand All @@ -184,6 +201,9 @@ internal class ScreenshotRecorder(
}

contentChanged.set(true)
if (DEBUG_MODE) {
debugOverlayDrawable.invalidateSelf()
}
}

fun bind(root: View) {
Expand All @@ -194,11 +214,18 @@ internal class ScreenshotRecorder(
// next bind the new root
rootView = WeakReference(root)
root.addOnDrawListenerSafe(this)
if (DEBUG_MODE) {
root.overlay.add(debugOverlayDrawable)
}

// invalidate the flag to capture the first frame after new window is attached
contentChanged.set(true)
}

fun unbind(root: View?) {
if (DEBUG_MODE) {
root?.overlay?.remove(debugOverlayDrawable)
}
root?.removeOnDrawListenerSafe(this)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.sentry.android.replay.util

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.ColorFilter
import android.graphics.Paint
import android.graphics.PixelFormat
import android.graphics.Rect
import android.graphics.drawable.Drawable

internal class DebugOverlayDrawable : Drawable() {

private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
private val padding = 6f
private val tmpRect = Rect()
private var masks: List<Rect> = emptyList()

override fun draw(canvas: Canvas) {
paint.textSize = 32f
paint.setColor(Color.BLACK)

paint.strokeWidth = 4f

for (mask in masks) {
paint.setColor(Color.argb(128, 255, 20, 20))
paint.style = Paint.Style.STROKE
canvas.drawRect(mask, paint)

paint.style = Paint.Style.FILL
val label = "${mask.left} ${mask.top}"
Copy link
Member

@romtsn romtsn Apr 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally I'd want to have bottom and right too, shall we maybe use a smaller textSize but include all the bounds? e.g. in this format 0, 0 - 1, 1

paint.getTextBounds(label, 0, label.length, tmpRect)

paint.setColor(Color.argb(128, 255, 255, 255))
canvas.drawRect(
mask.left.toFloat() + paint.strokeWidth / 2,
mask.top.toFloat() + paint.strokeWidth / 2,
mask.left.toFloat() + tmpRect.width() + padding + padding,
mask.top.toFloat() + tmpRect.height() + padding + padding,
paint
)
paint.setColor(Color.BLACK)
canvas.drawText(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we actually draw this label outside the mask rect? I think it's nicer visually and wouldn't potentially overlap the content inside the mask (which may be useful to see in some cases)

label,
mask.left.toFloat() + padding + paint.strokeWidth / 2,
mask.top.toFloat() + tmpRect.height() - tmpRect.bottom + padding + paint.strokeWidth / 2,
paint
)
}
}

override fun setAlpha(alpha: Int) {
// no-op
}

override fun setColorFilter(colorFilter: ColorFilter?) {
// no-op
}

@Deprecated("Deprecated in Java")
override fun getOpacity(): Int = PixelFormat.TRANSLUCENT

fun update(masks: List<Rect>) {
this.masks = masks
invalidateSelf()
}
}
Loading