This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Hook ImageReaderSurfaceProducer to the onTrimMemory listener interface #50792
Merged
johnmccutchan
merged 1 commit into
flutter:main
from
johnmccutchan:image_reader_trim_memory
Feb 21, 2024
Merged
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -189,6 +189,7 @@ public SurfaceProducer createSurfaceProducer() { | |
| if (!debugForceSurfaceProducerGlTextures && Build.VERSION.SDK_INT >= 29) { | ||
| final ImageReaderSurfaceProducer producer = new ImageReaderSurfaceProducer(id); | ||
| registerImageTexture(id, producer); | ||
| addOnTrimMemoryListener(producer); | ||
| Log.v(TAG, "New ImageReaderSurfaceProducer ID: " + id); | ||
| entry = producer; | ||
| } else { | ||
|
|
@@ -404,7 +405,9 @@ public void run() { | |
| @Keep | ||
| @TargetApi(29) | ||
| final class ImageReaderSurfaceProducer | ||
| implements TextureRegistry.SurfaceProducer, TextureRegistry.ImageConsumer { | ||
| implements TextureRegistry.SurfaceProducer, | ||
| TextureRegistry.ImageConsumer, | ||
| TextureRegistry.OnTrimMemoryListener { | ||
| private static final String TAG = "ImageReaderSurfaceProducer"; | ||
| private static final int MAX_IMAGES = 4; | ||
|
|
||
|
|
@@ -436,6 +439,7 @@ final class ImageReaderSurfaceProducer | |
| private final LinkedList<PerImageReader> imageReaderQueue = new LinkedList<PerImageReader>(); | ||
| private final HashMap<ImageReader, PerImageReader> perImageReaders = | ||
| new HashMap<ImageReader, PerImageReader>(); | ||
| private PerImage lastDequeuedImage = null; | ||
| private PerImageReader lastReaderDequeuedFrom = null; | ||
|
|
||
| /** Internal class: state held per Image produced by ImageReaders. */ | ||
|
|
@@ -607,7 +611,15 @@ PerImage dequeueImage() { | |
| lastDequeueTime = System.nanoTime(); | ||
| } | ||
| } | ||
| // We have dequeued the first image from reader. | ||
| if (lastDequeuedImage != null) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do we know that we're done presenting the image when we dequeueImage? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the contract between image_external_texture* and this class. Making this more explicit is probably a good cleanup to do. |
||
| // We must keep the last image dequeued open until we are done presenting it. | ||
| // We have just dequeued a new image (r). Close the previously dequeued image. | ||
| lastDequeuedImage.image.close(); | ||
| lastDequeuedImage = null; | ||
| } | ||
| // Remember the last image and reader dequeued from. We do this because we must | ||
| // keep both of these alive until we are done presenting the image. | ||
| lastDequeuedImage = r; | ||
| lastReaderDequeuedFrom = reader; | ||
| break; | ||
| } | ||
|
|
@@ -616,23 +628,40 @@ PerImage dequeueImage() { | |
| return r; | ||
| } | ||
|
|
||
| @Override | ||
| public void onTrimMemory(int level) { | ||
| cleanup(); | ||
| createNewReader = true; | ||
| } | ||
|
|
||
| private void releaseInternal() { | ||
| cleanup(); | ||
| released = true; | ||
| for (PerImageReader pir : perImageReaders.values()) { | ||
| pir.close(); | ||
| } | ||
|
|
||
| private void cleanup() { | ||
| synchronized (lock) { | ||
| for (PerImageReader pir : perImageReaders.values()) { | ||
| pir.close(); | ||
| } | ||
| perImageReaders.clear(); | ||
| if (lastDequeuedImage != null) { | ||
| lastDequeuedImage.image.close(); | ||
| lastDequeuedImage = null; | ||
| } | ||
| if (lastReaderDequeuedFrom != null) { | ||
| lastReaderDequeuedFrom.close(); | ||
| lastReaderDequeuedFrom = null; | ||
| } | ||
| imageReaderQueue.clear(); | ||
| } | ||
| perImageReaders.clear(); | ||
| imageReaderQueue.clear(); | ||
| } | ||
|
|
||
| @TargetApi(33) | ||
| private void waitOnFence(Image image) { | ||
| try { | ||
| SyncFence fence = image.getFence(); | ||
| boolean signaled = fence.awaitForever(); | ||
| if (!signaled) { | ||
| Log.e(TAG, "acquireLatestImage image's fence was never signalled."); | ||
| } | ||
| fence.awaitForever(); | ||
| } catch (IOException e) { | ||
| // Drop. | ||
| } | ||
|
|
@@ -874,10 +903,7 @@ public void pushImage(Image image) { | |
| private void waitOnFence(Image image) { | ||
| try { | ||
| SyncFence fence = image.getFence(); | ||
| boolean signaled = fence.awaitForever(); | ||
| if (!signaled) { | ||
| Log.e(TAG, "acquireLatestImage image's fence was never signalled."); | ||
| } | ||
| fence.awaitForever(); | ||
| } catch (IOException e) { | ||
| // Drop. | ||
| } | ||
|
|
||
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
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.