-
Notifications
You must be signed in to change notification settings - Fork 69
android: Make the backend zero-copy #331
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
MarijnS95
wants to merge
2
commits into
master
Choose a base branch
from
android-zerocopy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As long as android is using a swap chain of two or three buffers and we can get the buffer age, it probably makes the most sense for softbuffer to zero-initialize the buffers, if Android doesn't already do that.
It shouldn't really add any meaningful cost to zero each buffer once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can somehow infer the amount of buffers with
ANativeWindow_getBuffersDataSpace?But I'd also be fine with zero-initializing in
buffer_mutfor now, and coming back to this later, this PR will be an improvement anyhow.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at https://developer.android.com/ndk/reference/group/a-native-window#anativewindow_lock, I guess it would return the whole bounds of the buffer in
inOutDirtyBoundsif a buffer is freshly allocated?Though the API seems a bit backwards from how we have
softbufferwork. We don't have a damage region to pass until we present.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's discussed in a code comment too:
softbuffer/src/backends/android.rs
Lines 151 to 158 in afa87bb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems the implementation of this is in
Surface::lockdefined in https://android.googlesource.com/platform/frameworks/native/+/main/libs/gui/Surface.cpp.Which also has:
And then copies the front buffer to the back buffer if it can.
Or otherwise:
Maybe a bit of an abuse of the API, but if we pass an empty
inOutDirtyBoundsregion, maybe we could then see if we get an empty region in return. In which case we can treat it as age1. Otherwise treat it as a new buffer with age0, which we can also zero-initialize to if we think that's necessary.Sounds like that would work? And should generally return an age of
1, keeping things very simple for any damage-tracked renderer usingsoftbuffer, since Android is apparently copying the front buffer to the back buffer already.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That function queries the color space and related info: https://developer.android.com/ndk/reference/group/a-data-space
Okay, we could do that...
It seems that I get the same pixel buffer back on every 3rd lock, so a typical triple-buffered swapchain.
But always tricky to infer if you don't know about internal copies that may change the context relative to what you thought it might have been given a non-zero age.
That all goes away with custom
AHardwareBufferpresented throughASurfaceControl/Transaction.EDIT: And if I remember correctly, I once asked the Android developers to create an
AImageWritersynonymous to the existingAImageReaderto serve this exact purpose of (de)queueing buffers from aSurfacein a more controllable manner (since the underlying APIs exist in the privateANativeWindowstructure).I'm a bit skeptical with those sources since they may change across Android versions. Despite that the documentation is never comprehensive/complete and may "change" to "clarify" scenarios though.
In that case, isn't it expected or allowed that nothing of the buffer is presented, because nothing was communicated or expected to have changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, me too. So I wouldn't expose it in
age, at least not unless we can find some definitive docs for things (including the tripple-buffered statement you made above).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also unclear what would happen on a resize (which also changes the format). If the "view" size changes but resize is not called, does it reuse the same buffers with implied upscaling?
(On
wgpu/glutinI saw that this format change implicitly limitedEGLto only return configs for that format, so it was definitely confusing)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking the input dirty bounds is only used to calculate the output dirty bounds, but looking again the dirty region is also passed to
backBuffer->lockAsync. So I guess we do need to just passNULL(equivalent to the whole region).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay so the only remaining thing here is to zero-initialize it (always) to get rid of the
MaybeUninit"safely", and leave theage()at0.