Overview
Understanding the end-to-end camera request flow is essential for debugging performance issues, latency, and synchronization problems in the Android camera stack. This flow traces the journey of a single frame from the moment an application requests it until the pixels are rendered on the screen or saved to disk. It highlights the intricate choreography between the Java API, native services, hardware abstractions, and kernel drivers.
1. Application Submits CaptureRequest
The journey begins in the application process. Once a CameraCaptureSession is configured with target Surface objects, the app builds a CaptureRequest.
- Building the Request: The app uses
CaptureRequest.Builderto define the metadata parameters (e.g., EV compensation, flash mode) and attaches the target Surfaces (e.g., aSurfaceViewfor preview). - Submission: The app calls
setRepeatingRequest()orcapture()on the session. - Binder IPC: The framework serializes the request and sends it via Binder IPC to the
CameraServicerunning in a separate system process.
// App side: Submitting the request
CaptureRequest request = requestBuilder.build();
mSession.setRepeatingRequest(request, mCaptureCallback, mBackgroundHandler);
2. CameraService Processing
The CameraService acts as the orchestrator.
- Request Validation: The
CameraDeviceClientreceives the request, validates the metadata, and ensures the target Streams are part of the active configuration. - Buffer Acquisition: For each target Surface in the request, the
CameraServicedequeues a freeGraphicBufferfrom the correspondingBufferQueue. This buffer represents physical memory allocated by gralloc. - Translation: The Java
CaptureRequestis translated into a nativecamera3_capture_request_tstructure. This structure includes the metadata and handles to the dequeued buffers. - Dispatch to HAL: The
CameraServicecallsprocessCaptureRequeston the HAL interface via HIDL or AIDL.
3. HAL Programs ISP and Sensor
The Camera HAL (Hardware Abstraction Layer) takes over execution.
- Queueing: The HAL places the request into an internal pipeline queue. Because hardware pipelines are deep, the HAL typically manages requests several frames ahead of actual sensor capture.
- Parameter Resolution: The HAL merges the requested metadata with its internal 3A states (Auto-Exposure, Auto-Focus, Auto-White Balance) and tuning data to calculate the exact hardware registers needed.
- Hardware Programming: The HAL interacts with kernel drivers (e.g., via V4L2) to program the sensor (exposure time, gain) and the Image Signal Processor (ISP) blocks (lens shading, noise reduction, scaling).
- Buffer Mapping: The HAL ensures the hardware DMA engines are configured to write the processed image data directly into the gralloc buffers provided in the request.
4. Hardware Capture and HAL Callback
The physical capture occurs.
- Exposure: The sensor exposes light and reads out raw data.
- ISP Processing: The raw data streams through the ISP pipeline, undergoing demosaicing, color correction, and formatting.
- Hardware Interrupt: Once the ISP completes writing to the destination buffer, it triggers a hardware interrupt. The kernel driver handles the interrupt and signals the HAL.
- processCaptureResult: The HAL constructs a
camera3_capture_result_tstructure containing the actual applied metadata (theCaptureResult) and the filled buffers. It sends this back to theCameraServicevia theprocessCaptureResultcallback.
// HAL side: Sending result back
camera3_capture_result_t result = {};
result.frame_number = request->frame_number;
result.result = final_metadata;
result.num_output_buffers = 1;
result.output_buffers = &filled_buffer;
callback->process_capture_result(callback, &result);
5. Buffer Flow and Application Consumption
The CameraService receives the result and routes data back to the consumer.
- Buffer Enqueueing: The
CameraServicetakes the filledGraphicBufferand enqueues it back into the targetBufferQueue. - Metadata Dispatch: The metadata is serialized and sent via Binder back to the application, triggering the
onCaptureCompletedcallback. - Consumer Notification: The
BufferQueuenotifies its consumer that a new buffer is available.- If the consumer is a
SurfaceView, SurfaceFlinger acquires the buffer and composites it onto the screen. - If the consumer is an
ImageReader, theonImageAvailablecallback is fired in the app. - If the consumer is
MediaCodec, the video encoder begins processing the frame.
- If the consumer is a
// App side: Receiving metadata
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session,
@NonNull CaptureRequest request,
@NonNull TotalCaptureResult result) {
// Process result metadata (e.g., read actual exposure time)
}
Analyzing the Pipeline with Systrace
This complex sequence is heavily dependent on precise timing. Developers use systrace (or Perfetto) to analyze the request flow.
When tracing a camera session, you can observe:
- App Thread: Submitting requests.
- CameraService Threads: Handling IPC and dequeuing buffers.
- HAL Threads: Processing requests and waiting on hardware interrupts.
- SurfaceFlinger / App Threads: Consuming the rendered buffers.
Bottlenecks at any stage (e.g., slow metadata processing in the app, ISP overload, or SurfaceFlinger composition delays) can result in dropped frames or visible stutter in the camera preview.