Skip to content

Commit 835d1aa

Browse files
committed
Remove extra background executor, as it's not needed anymore
1 parent 8ad5eb0 commit 835d1aa

File tree

4 files changed

+36
-48
lines changed

4 files changed

+36
-48
lines changed

sentry-android-replay/src/main/java/io/sentry/android/replay/WindowRecorder.kt

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -244,21 +244,6 @@ internal class WindowRecorder(
244244
override fun getExecutor(): ScheduledExecutorService = replayExecutor
245245

246246
override fun getMainLooperHandler(): MainLooperHandler = mainLooperHandler
247-
248-
override fun getBackgroundHandler(): Handler {
249-
// only start the background thread if it's actually needed, as it's only used by Canvas Capture
250-
// Strategy
251-
if (backgroundProcessingHandler == null) {
252-
backgroundProcessingHandlerLock.acquire().use {
253-
if (backgroundProcessingHandler == null) {
254-
backgroundProcessingHandlerThread = HandlerThread("SentryReplayBackgroundProcessing")
255-
backgroundProcessingHandlerThread?.start()
256-
backgroundProcessingHandler = Handler(backgroundProcessingHandlerThread!!.looper)
257-
}
258-
}
259-
}
260-
return backgroundProcessingHandler!!
261-
}
262247
}
263248

264249
internal interface ExecutorProvider {
@@ -267,7 +252,4 @@ internal interface ExecutorProvider {
267252

268253
/** Returns a handler associated with the main thread looper. */
269254
fun getMainLooperHandler(): MainLooperHandler
270-
271-
/** Returns a handler associated with a background thread looper. */
272-
fun getBackgroundHandler(): Handler
273255
}

sentry-android-replay/src/main/java/io/sentry/android/replay/screenshot/CanvasStrategy.kt

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import android.view.Surface
2929
import android.view.View
3030
import androidx.annotation.RequiresApi
3131
import io.sentry.SentryLevel
32+
import io.sentry.SentryLevel.DEBUG
3233
import io.sentry.SentryOptions
3334
import io.sentry.android.replay.ExecutorProvider
3435
import io.sentry.android.replay.ScreenshotRecorderCallback
@@ -71,15 +72,14 @@ internal class CanvasStrategy(
7172
@SuppressLint("NewApi")
7273
private val pictureRenderTask = Runnable {
7374
if (isClosed.get()) {
74-
options.logger.log(
75-
SentryLevel.DEBUG,
76-
"Canvas Strategy already closed, skipping picture render",
77-
)
75+
options.logger.log(DEBUG, "Canvas Strategy already closed, skipping picture render")
7876
return@Runnable
7977
}
8078
val picture = unprocessedPictureRef.getAndSet(null) ?: return@Runnable
8179

8280
try {
81+
// It's safe to access the surface because the render task,
82+
// as well as surface release are executed on the same single threaded executor
8383
// Draw picture to the Surface for PixelCopy
8484
val surfaceCanvas = surface.lockHardwareCanvas()
8585
try {
@@ -96,30 +96,45 @@ internal class CanvasStrategy(
9696
}
9797
}
9898
}
99-
100-
// Trigger PixelCopy capture
10199
PixelCopy.request(
102100
surface,
103101
screenshot!!,
104102
{ result ->
105-
if (result == PixelCopy.SUCCESS) {
106-
lastCaptureSuccessful.set(true)
107-
val bitmap = screenshot
108-
if (bitmap != null && !bitmap.isRecycled) {
109-
screenshotRecorderCallback?.onScreenshotRecorded(bitmap)
110-
}
111-
} else {
112-
options.logger.log(
113-
SentryLevel.ERROR,
114-
"Canvas Strategy: PixelCopy failed with code $result",
115-
)
116-
lastCaptureSuccessful.set(false)
103+
if (isClosed.get()) {
104+
options.logger.log(DEBUG, "CanvasStrategy is closed, ignoring capture result")
105+
return@request
117106
}
107+
executor
108+
.getExecutor()
109+
.submit(
110+
ReplayRunnable("screenshot_recorder.mask") {
111+
if (isClosed.get()) {
112+
options.logger.log(DEBUG, "CanvasStrategy is closed, ignoring capture result")
113+
return@ReplayRunnable
114+
}
115+
if (result == PixelCopy.SUCCESS) {
116+
lastCaptureSuccessful.set(true)
117+
val bitmap = screenshot
118+
if (bitmap != null) {
119+
synchronized(bitmap) {
120+
if (!bitmap.isRecycled)
121+
screenshotRecorderCallback?.onScreenshotRecorded(bitmap)
122+
}
123+
}
124+
} else {
125+
options.logger.log(
126+
SentryLevel.ERROR,
127+
"Canvas Strategy: PixelCopy failed with code $result",
128+
)
129+
lastCaptureSuccessful.set(false)
130+
}
131+
}
132+
)
118133
},
119-
executor.getBackgroundHandler(),
134+
executor.getMainLooperHandler().handler,
120135
)
121136
} catch (t: Throwable) {
122-
options.logger.log(SentryLevel.ERROR, "Canvas Strategy: picture render failed")
137+
options.logger.log(SentryLevel.ERROR, "Canvas Strategy: picture render failed", t)
123138
lastCaptureSuccessful.set(false)
124139
}
125140
}
@@ -139,10 +154,7 @@ internal class CanvasStrategy(
139154

140155
if (!isClosed.get()) {
141156
unprocessedPictureRef.set(picture)
142-
// use the same handler for PixelCopy and pictureRenderTask
143-
executor
144-
.getBackgroundHandler()
145-
.post(ReplayRunnable("screenshot_recorder.canvas", pictureRenderTask))
157+
executor.getExecutor().submit(ReplayRunnable("screenshot_recorder.canvas", pictureRenderTask))
146158
}
147159
}
148160

sentry-android-replay/src/test/java/io/sentry/android/replay/ScreenshotRecorderTest.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.sentry.android.replay
22

3-
import android.os.Handler
43
import androidx.test.ext.junit.runners.AndroidJUnit4
54
import io.sentry.ScreenshotStrategyType
65
import io.sentry.SentryOptions
@@ -30,8 +29,6 @@ class ScreenshotRecorderTest {
3029
override fun getExecutor(): ScheduledExecutorService = mock<ScheduledExecutorService>()
3130

3231
override fun getMainLooperHandler(): MainLooperHandler = mock<MainLooperHandler>()
33-
34-
override fun getBackgroundHandler(): Handler = mock<Handler>()
3532
},
3633
null,
3734
)

sentry-android-replay/src/test/java/io/sentry/android/replay/screenshot/PixelCopyStrategyTest.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package io.sentry.android.replay.screenshot
22

33
import android.app.Activity
44
import android.os.Bundle
5-
import android.os.Handler
65
import android.os.Looper
76
import android.widget.LinearLayout
87
import android.widget.LinearLayout.LayoutParams
@@ -45,8 +44,6 @@ class PixelCopyStrategyTest {
4544
override fun getExecutor(): ScheduledExecutorService = executor
4645

4746
override fun getMainLooperHandler(): MainLooperHandler = MainLooperHandler()
48-
49-
override fun getBackgroundHandler(): Handler = mock()
5047
},
5148
callback,
5249
options,

0 commit comments

Comments
 (0)