formats

command
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Example: ANativeWindow pixel format constants and API overview.

Enumerates every pixel format constant exposed by the window package and prints its numeric value alongside the human-readable String() name.

Creating an ANativeWindow requires a running Android Activity (the window handle is obtained from the Activity's native surface), so this example focuses on the type system and documents the full rendering lifecycle that a real application would follow.

ANativeWindow rendering lifecycle:

  1. Acquire -- Obtain a *Window from the Activity's native surface. The NDK delivers this via ANativeActivity callbacks (onNativeWindowCreated). The Window wraps an opaque ANativeWindow pointer managed by the system compositor.

  2. Query -- Inspect the window with Width(), Height(), and Format(). Width and Height return the surface dimensions in pixels. Format returns the pixel format as an int32 matching one of the Format constants (Rgba8888, Rgbx8888, Rgb565).

  3. Configure -- Optionally call SetBuffersGeometry(width, height, format) to request a different buffer size or pixel format. The compositor will scale the buffer to the window size if the requested dimensions differ. Pass 0 for width or height to keep the current dimension. Returns an error if the geometry change is rejected.

  4. Lock -- Lock the next back-buffer for CPU access. This yields an ANativeWindow_Buffer describing the pixel memory layout: stride, dimensions, format, and a raw pointer to pixel data. While locked the compositor cannot read the buffer, so the lock duration should be kept short.

  5. Draw -- Write pixels directly into the locked buffer. The pixel layout depends on the format: Rgba8888 uses 4 bytes per pixel (R, G, B, A), Rgbx8888 uses 4 bytes with the alpha channel ignored, and Rgb565 packs each pixel into 2 bytes (5 red, 6 green, 5 blue bits).

  6. Unlock -- Call UnlockAndPost() to release the buffer and submit it to the compositor for display. This atomically unlocks the buffer and posts it; there is no separate post step. Returns an error if the surface is in an invalid state.

  7. Release -- When the Activity's window is destroyed (onNativeWindowDestroyed), stop using the Window. The system reclaims the underlying native surface.

This program must run on an Android device.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL