Skip to content
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

ndk/media/image_reader: Don't assume ownership of NativeWindow #418

Merged
merged 1 commit into from
Aug 15, 2023

Conversation

MarijnS95
Copy link
Member

@MarijnS95 MarijnS95 commented Aug 11, 2023

When implementing proper ownership acquire() and release() semantics for NativeWindow in #207, I thought I had checked all existing calls to NativeWindow::from_ptr() to make sure that they return a pointer that we get ownership over, and have to clean up ourselves. This turns out to not be the case for AImageReader_getWindow():

The ANativeWindow is managed by this image reader. Do NOT call ANativeWindow_release on it. Instead, use AImageReader_delete.

And can be trivially reproduced by creating an ImageReader and calling .get_window(). Dropping the NativeWindow is fine but subsequently dropping the ImageReader results in a NULL-pointer SEGFAULT.

For now calling clone_from_ptr() is enough to first acquire an extra reference on the pointer so that ownership remains balanced, but in the future we'd like to investigate a new non-owned NativeWindow type similar to HardwareBuffer.

As of writing the following calls to from_ptr() remain, that are all confirmed to transfer ownership and require cleanup via _release():

  • ASurfaceTexture_acquireANativeWindow();
  • AMediaCodec_createInputSurface();
  • AMediaCodec_createPersistentInputSurface();
  • ANativeWindow_fromSurface().

When implementing proper ownership `acquire()` and `release()` semantics
for `NativeWindow` in #207, I
thought I had checked all existing calls to `NativeWindow::from_ptr()`
to make sure that they return a pointer that we get ownership over, and
have to clean up ourselves.  This turns out to [not be the case for
`AImageReader_getWindow()`]:

    The ANativeWindow is managed by this image reader. Do NOT call ANativeWindow_release on it. Instead, use AImageReader_delete.

And can be trivially reproduced by creating an `ImageReader` and calling
`.get_window()`.  Dropping the `NativeWindow` is fine but subsequently
dropping the `ImageReader` results in a NULL-pointer SEGFAULT.

For now calling `clone_from_ptr()` is enough to first acquire an extra
reference on the pointer so that ownership remains balanced, but in the
future we'd like to investigate a [new non-owned `NativeWindow` type
similar to `HardwareBuffer`].

As of writing the following calls to `from_ptr()` remain, that are all
confirmed to transfer ownership and require cleanup via `_release()`:
- `ASurfaceTexture_acquireANativeWindow()`;
- `AMediaCodec_createInputSurface()`;
- `AMediaCodec_createPersistentInputSurface()`;
- `ANativeWindow_fromSurface()`.

[not be the case for `AImageReader_getWindow()`]: https://developer.android.com/ndk/reference/group/media#aimagereader_getwindow
[new non-owned `NativeWindow` type similar to `HardwareBuffer`]: #309
@MarijnS95 MarijnS95 requested a review from rib August 15, 2023 13:59
Copy link
Contributor

@rib rib left a comment

Choose a reason for hiding this comment

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

Was initially unsure how it considers the possibility of AImageReader_getWindow failing and not setting a window pointer, but looks like construct_never_null handles that ok.

looks good to me.

@MarijnS95
Copy link
Member Author

Yup, should be enough asserts there.

@MarijnS95 MarijnS95 merged commit e3f9225 into master Aug 15, 2023
36 checks passed
@MarijnS95 MarijnS95 deleted the image-reader-no-native-window-ownership branch August 15, 2023 23:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants