Android: Add hardware-backed Bitmap decode API#3109
Open
thebehera wants to merge 2 commits intoAOMediaCodec:mainfrom
Open
Android: Add hardware-backed Bitmap decode API#3109thebehera wants to merge 2 commits intoAOMediaCodec:mainfrom
thebehera wants to merge 2 commits intoAOMediaCodec:mainfrom
Conversation
a41d93b to
1107f45
Compare
Adds new methods to AvifDecoder that decode AVIF images directly into AHardwareBuffer and return hardware-backed Bitmaps (Bitmap.Config.HARDWARE). Hardware Bitmaps are GPU-resident, require no CPU→GPU upload, and are directly compatible with Canvas, ImageView, and Drawable. New API (requires API 26): AvifDecoder.decodeHardwareBitmap(ByteBuffer, int, int[, boolean]) AvifDecoder.nextFrameHardwareBitmap([boolean]) AvifDecoder.nthFrameHardwareBitmap(int[, boolean]) AvifDecoder.isHighBitDepthDisplaySupported(Display) The allowHdr parameter controls format selection: when true and the image has depth > 8, an R16G16B16A16_FLOAT AHardwareBuffer is tried first to preserve HDR precision (PQ/HLG color spaces tagged accordingly). Falls back to R8G8B8A8_UNORM if FP16 is unsupported or allowHdr is false. avifImageYUVToRGB decodes directly into the locked AHardwareBuffer memory with no intermediate copy. ApplyCrop is introduced as a shared helper used by both the software Bitmap and hardware Bitmap decode paths. Uses __ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__ (NDK 25) so AHardwareBuffer symbols are weak-linked and the library remains loadable on API < 26.
1107f45 to
0d21c76
Compare
After nthFrameHardwareBitmap(0), the decoder is positioned at frame 0, so nextFrameHardwareBitmap advances to frame 1 on the first call. Looping frameCount times overshoots by one, hitting AVIF_RESULT_NO_IMAGES_REMAINING on the last iteration. Switch the buffer-reuse loop to nthFrameHardwareBitmap(i, ...) which iterates frames 0 through frameCount-1 cleanly, and add a separate assertion to verify nextFrameHardwareBitmap with a dest buffer.
8557c18 to
e60e863
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds API to decode AVIF images directly into hardware-backed Bitmaps
(Config.HARDWARE), bypassing the software Bitmap intermediary.
New API
Still images
AvifDecoder.isHighBitDepthDisplaySupported(Display)
AvifDecoder.decodeHardwareBitmap(ByteBuffer, int)
AvifDecoder.decodeHardwareBitmap(ByteBuffer, int, int)
AvifDecoder.decodeHardwareBitmap(ByteBuffer, int, int, boolean allowHdr)
Animated images (instance methods)
decoder.createHardwareBuffer(boolean allowHdr)
decoder.nextFrameHardwareBitmap(boolean allowHdr)
decoder.nextFrameHardwareBitmap(boolean allowHdr, @nullable HardwareBuffer dest)
decoder.nthFrameHardwareBitmap(int n, boolean allowHdr)
decoder.nthFrameHardwareBitmap(int n, boolean allowHdr, @nullable HardwareBuffer dest)
Passing null for
destallocates a new HardwareBuffer on each call.Passing a pre-allocated buffer (from
createHardwareBuffer) enables zero-copy frame reuse: decode all frames into the same buffer, and a single Bitmap wrapping it reflects new content without re-allocation.Implementation
R8G8B8A8_UNORM, locks the buffer, decodes YUV→RGB directly into it.