Repository navigation
fix(replay): Do not recycle the canvas screenshot during a pixel copy #6342
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
...ry-android-replay/src/test/java/io/sentry/android/replay/screenshot/CanvasStrategyTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| package io.sentry.android.replay.screenshot | ||
|
|
||
| import android.graphics.Bitmap | ||
| import android.os.Handler | ||
| import android.os.Looper | ||
| import android.view.PixelCopy | ||
| import android.view.Surface | ||
| import android.view.View | ||
| import androidx.test.core.app.ApplicationProvider | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import com.google.common.truth.Truth.assertThat | ||
| import io.sentry.SentryOptions | ||
| import io.sentry.android.replay.ExecutorProvider | ||
| import io.sentry.android.replay.ScreenshotRecorderConfig | ||
| import io.sentry.android.replay.util.MainLooperHandler | ||
| import java.util.concurrent.ScheduledExecutorService | ||
| import kotlin.test.BeforeTest | ||
| import kotlin.test.Test | ||
| import org.junit.runner.RunWith | ||
| import org.mockito.kotlin.mock | ||
| import org.robolectric.Shadows.shadowOf | ||
| import org.robolectric.annotation.Config | ||
| import org.robolectric.annotation.Implementation | ||
| import org.robolectric.annotation.Implements | ||
|
|
||
| @RunWith(AndroidJUnit4::class) | ||
| @Config(sdk = [30], shadows = [DeferredSurfacePixelCopyShadow::class]) | ||
| class CanvasStrategyTest { | ||
|
|
||
| private class Fixture { | ||
| val options = SentryOptions() | ||
| val config = ScreenshotRecorderConfig(100, 100, 1f, 1f, 1, 1000) | ||
|
|
||
| fun getSut(): CanvasStrategy = | ||
| CanvasStrategy( | ||
| object : ExecutorProvider { | ||
| override fun getExecutor(): ScheduledExecutorService = mock() | ||
|
|
||
| override fun getMainLooperHandler(): MainLooperHandler = MainLooperHandler() | ||
|
|
||
| // runs the render task, the PixelCopy callback and the cleanup, like production | ||
| override fun getBackgroundHandler(): Handler = Handler(Looper.getMainLooper()) | ||
| }, | ||
| mock(), | ||
| options, | ||
| config, | ||
| ) | ||
| } | ||
|
|
||
| private val fixture = Fixture() | ||
|
|
||
| @BeforeTest | ||
| fun setup() { | ||
| DeferredSurfacePixelCopyShadow.reset() | ||
| } | ||
|
|
||
| @Test | ||
| fun `close keeps the screenshot alive until the in-flight pixel copy finishes`() { | ||
| val strategy = fixture.getSut() | ||
| strategy.capture(View(ApplicationProvider.getApplicationContext())) | ||
| shadowOf(Looper.getMainLooper()).idle() | ||
| val screenshot = DeferredSurfacePixelCopyShadow.dest!! | ||
|
|
||
| strategy.close() | ||
| shadowOf(Looper.getMainLooper()).idle() | ||
|
|
||
| // the copy is still writing into the screenshot, recycling it now would abort the RenderThread | ||
| assertThat(screenshot.isRecycled).isFalse() | ||
|
|
||
| DeferredSurfacePixelCopyShadow.flush() | ||
| shadowOf(Looper.getMainLooper()).idle() | ||
|
|
||
| assertThat(screenshot.isRecycled).isTrue() | ||
| } | ||
|
|
||
| @Test | ||
| fun `close recycles the screenshot right away when no pixel copy is in flight`() { | ||
| val strategy = fixture.getSut() | ||
| strategy.capture(View(ApplicationProvider.getApplicationContext())) | ||
| shadowOf(Looper.getMainLooper()).idle() | ||
| val screenshot = DeferredSurfacePixelCopyShadow.dest!! | ||
| DeferredSurfacePixelCopyShadow.flush() | ||
| shadowOf(Looper.getMainLooper()).idle() | ||
|
|
||
| strategy.close() | ||
| shadowOf(Looper.getMainLooper()).idle() | ||
|
|
||
| assertThat(screenshot.isRecycled).isTrue() | ||
| } | ||
| } | ||
|
|
||
| /** Holds back the result of a [PixelCopy] from a [Surface] until [flush] is called. */ | ||
| @Implements(PixelCopy::class) | ||
| class DeferredSurfacePixelCopyShadow { | ||
| companion object { | ||
| private val pendingCallbacks = mutableListOf<() -> Unit>() | ||
| var dest: Bitmap? = null | ||
|
|
||
| fun reset() { | ||
| pendingCallbacks.clear() | ||
| dest = null | ||
| } | ||
|
|
||
| fun flush() { | ||
| val callbacks = pendingCallbacks.toList() | ||
| pendingCallbacks.clear() | ||
| callbacks.forEach { it.invoke() } | ||
| } | ||
|
|
||
| @JvmStatic | ||
| @Implementation | ||
| @Suppress("UNUSED_PARAMETER") | ||
| fun request( | ||
| source: Surface, | ||
| dest: Bitmap, | ||
| listener: PixelCopy.OnPixelCopyFinishedListener, | ||
| listenerThread: Handler, | ||
| ) { | ||
| this.dest = dest | ||
| pendingCallbacks.add { | ||
| listenerThread.post { listener.onPixelCopyFinished(PixelCopy.SUCCESS) } | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.