Grafika

repository·master·Indexed 27 days ago

https://github.com/google/grafika

A collection of Android graphics and media examples and 'hacks' designed to demonstrate performance techniques and graphics features. Written in Java for API 18, the project focuses on thread safety and minimizing garbage collection pauses. It includes implementations for video playback via TextureView, circular buffer camera capture, side-by-side video decoding, OpenGL ES recording using Framebuffer Objects (FBO), and screen recording via MediaProjectionManager.

Tokens
1.6K
Snippets
0
Records
9
Agent score
41%

What's inside Grafika

  1. Overview of Grafika

    master

    Grafika is an open-source (Apache 2 license) collection of Android graphics and media hacks. It serves as a practical companion to the Android System-Level Graphics Architecture documentation.

    Key Characteristics:

    • Target API: Developed for API 18 (Android 4.3), with sporadic support for older versions.
    • Implementation: All code is written in Java; the NDK is not used.
    • Focus Areas: The project emphasizes thread safety (especially regarding GL/EGL and thread-local storage) and minimizing garbage collection (GC) pauses to prevent jank.
    • Stability: It is a work-in-progress, not a polished or stable library, and is not intended to demonstrate the 'proper' way to implement features.
  2. Record GL app using Framebuffer Objects (FBO)

    master

    The RecordFBOActivity simultaneously draws to the display and a video encoder using OpenGL ES and Framebuffer Objects (FBOs) to avoid redundant rendering.

    Technical Details:

    • Rendering Trigger: Uses Choreographer to update every vsync. If the system falls behind, frames are skipped (indicated by an on-screen drop counter and border flash).
    • Encoder Rate: The encoder is fed every-other frame, resulting in ~30fps output on typical devices.
    • Output Format: Produces a video-only MP4 file named fbo-gl-recording.mp4.
    • Aspect Ratio: Recording is letter- or pillar-boxed to match the display aspect ratio.
    • Implementation: Located in app/src/main/java/com/android/grafika/RecordFBOActivity.java.
  3. Continuous capture of camera video

    master

    The ContinuousCaptureActivity implements a circular buffer to store video, allowing you to save a segment when the 'capture' button is pressed.

    Technical Specifications:

    • Buffer Configuration: Hard-wired to capture approximately 7 seconds of video from the camera at 6MB/sec (targeting 15fps 720p). The buffer size is roughly 5MB.
    • Output Format: Produces a video-only MP4 file named constant-capture.mp4.
    • Resolution: Video is always 1280x720. If the camera input does not match this, the aspect ratio may be incorrect.
    • Implementation: Located in app/src/main/java/com/android/grafika/ContinuousCaptureActivity.java.
  4. Record screen via MediaProjectionManager

    master

    The ScreenRecordActivity records the device screen to a movie using the MediaProjectionManager API.

    Requirement: This requires API level 23 (Marshmallow) or greater. Implementation: Located in app/src/main/java/com/android/grafika/ScreenRecordActivity.java.

  5. Play video using TextureView

    master

    The PlayMovieActivity plays the video track from an MP4 file using a TextureView for output.

    Usage Details:

    • File Access: It only accesses files located in /data/data/com.android.grafika/files/. This directory also contains two automatically generated videos: gen-eight-rects.mp4 and gen-slides.mp4.
    • Playback Options: By default, video plays once at the recorded rate. You can enable looping or force playback at a fixed 60 FPS via checkboxes.
    • Implementation: Located in app/src/main/java/com/android/grafika/PlayMovieActivity.java.
  6. Scheduled buffer swapping with SurfaceFlinger

    master

    The ScheduledSwapActivity exercises a SurfaceFlinger feature that allows submitting buffers to be displayed at a specific time.

    Requirements and Usage:

    • API Requirement: Requires API 19 (Android 4.4 KitKat) for full functionality.
    • Configuration: Users can configure frame delivery timing (e.g., a 3-2 pattern for 24fps) and how far in advance frames are scheduled. Selecting "ASAP" disables scheduling.
    • Debugging: Use systrace with the following tags to observe effects: sched gfx view --app=com.android.grafika.
    • Implementation: Located in app/src/main/java/com/android/grafika/ScheduledSwapActivity.java.
  7. Double decode two video streams

    master

    The DoubleDecodeActivity decodes two video streams side-by-side, outputting to a pair of TextureViews.

    Key Behaviors:

    • Rotation Handling: When the screen rotates, the activity retains the SurfaceTexture and re-attaches it to the new TextureView to avoid expensive codec reconfiguration.
    • Resource Management: Decoders stop when the activity is exited to free hardware codec resources.
    • Layout: Provides distinct layouts for portrait and landscape modes, scaling videos to fit.
    • Implementation: Located in app/src/main/java/com/android/grafika/DoubleDecodeActivity.java.
  8. Show and capture camera preview

    master

    The CameraCaptureActivity attempts to record 720p video from the front-facing camera while simultaneously displaying the preview.

    Usage Details:

    • Recording: Use the record button to toggle recording. Note that if you exit and return to the activity, recording restarts, creating a gap in the timeline.
    • Filters: You can select filters to apply to the preview; however, these filters are not applied to the actual recording.
    • Output Format: Produces a video-only MP4 file named camera-test.mp4. The recorded video is scaled to 640x480, which may result in a squished aspect ratio.
    • Implementation: Located in app/src/main/java/com/android/grafika/CameraCaptureActivity.java.
  9. Reference of Grafika Activities

    master

    Grafika includes several specialized activities for testing graphics and media capabilities:

    • PlayMovieActivity: Plays MP4 via TextureView.
    • ContinuousCaptureActivity: Circular buffer camera capture.
    • DoubleDecodeActivity: Side-by-side video decoding.
    • HardwareScalerActivity: GL rendering with on-the-fly surface size changes.
    • LiveCameraActivity: Directs camera preview to a TextureView.
    • MultiSurfaceTest: Overlapping SurfaceViews (including secure surfaces).
    • PlayMovieSurfaceActivity: Plays MP4 via SurfaceView.
    • RecordFBOActivity: Records GL drawing to an encoder using FBOs.
    • ScreenRecordActivity: Screen recording via MediaProjectionManager (API 23+).
    • ScheduledSwapActivity: Exercises SurfaceFlinger buffer scheduling.
    • CameraCaptureActivity: Simultaneous camera preview and recording.
    • TextureViewCanvasActivity: Software rendering with Canvas to TextureView.
    • TextureViewGLActivity: Simple GLES in a TextureView.
    • TextureFromCameraActivity: Renders camera preview with a GLES texture.
    • ColorBarActivity: Displays RGB color bars.
    • GlesInfoActivity: Dumps OpenGL ES version and extensions.
    • TextureUploadActivity: glTexImage2D speed test.
    • ReadPixelsActivity: glReadPixels speed test.