<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mohammad Nouri on Medium]]></title>
        <description><![CDATA[Stories by Mohammad Nouri on Medium]]></description>
        <link>https://medium.com/@mohammadnori44?source=rss-8a2050d0d225------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*p5bVYwg06jErcDcXg_LtTQ.png</url>
            <title>Stories by Mohammad Nouri on Medium</title>
            <link>https://medium.com/@mohammadnori44?source=rss-8a2050d0d225------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 18:05:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mohammadnori44/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How I Built a Zero-Copy GPU Pipeline with NDK, AHardwareBuffer and OpenGL ES]]></title>
            <link>https://medium.com/@mohammadnori44/how-i-built-a-zero-copy-gpu-pipeline-with-ndk-ahardwarebuffer-and-opengl-es-0c8cb10f92ca?source=rss-8a2050d0d225------2</link>
            <guid isPermaLink="false">https://medium.com/p/0c8cb10f92ca</guid>
            <category><![CDATA[opengl]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[computer-graphics]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Mohammad Nouri]]></dc:creator>
            <pubDate>Mon, 14 Sep 2026 08:09:18 GMT</pubDate>
            <atom:updated>2026-09-14T08:09:18.222Z</atom:updated>
            <content:encoded><![CDATA[<h3>Camera Engine with NDK, AHardwareBuffer, EGLImageKHR, OpenGL ES, and ECS</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hXzVliwOVRnVvS2uUpmp4w.jpeg" /></figure><p><strong>A deep dive into </strong><strong>ZeroStallCam, a small Android graphics engine designed around one idea: keep camera frames out of the CPU&#39;s way.</strong></p><p>Modern Android applications make it easy to put a live camera preview on screen. But “easy” and “low-level efficient” are not the same thing.</p><p>A camera frame starts life inside the camera subsystem, moves through a buffer queue, reaches an application-owned consumer, gets mapped or copied, and finally becomes something the GPU can sample. Once you add overlays, transforms, timestamps, and high-frequency rendering, every extra ownership transition, CPU-side conversion, allocation, or synchronization point becomes interesting.</p><p>That is the problem I wanted to explore with <strong>ZeroStallCam</strong>.</p><p>The project is a native Android camera preview and rendering engine built with <strong>Android NDK, Camera2 NDK, OpenGL ES 3.0, </strong><strong>AHardwareBuffer, </strong><strong>EGLImageKHR, JNI, and a small Entity-Component-System (ECS) architecture</strong>.</p><p>The repository is here:</p><p><a href="https://github.com/MohammadNouri5700/zerostallcam"><strong>https://github.com/MohammadNouri5700/zerostallcam</strong></a></p><blockquote><strong><em>Important:</em></strong><em> this article describes the current implementation and its measured profiling results. The performance numbers are from the project’s own instrumentation, not from a controlled benchmark against CameraX or another production camera stack.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0zbg36i9Wf6fSoHjNAXskA.png" /></figure><h3>The idea in one sentence</h3><p><strong>Capture the camera frame in native hardware-backed memory, expose that memory to OpenGL without a CPU pixel copy, generate the render geometry procedurally in the vertex shader, and submit the camera + HUD in a single draw call.</strong></p><p>The intended path is:</p><pre>Camera2 NDK<br>    ↓<br>AImageReader<br>    ↓<br>AHardwareBuffer<br>    ↓<br>EGLImageKHR<br>    ↓<br>GL_TEXTURE_EXTERNAL_OES<br>    ↓<br>OpenGL ES<br>    ↓<br>Display</pre><p>The repository creates the AImageReader with GPU-sampling and camera-write usage flags, acquires the latest AHardwareBuffer, wraps it as an EGLImageKHR, and binds that image to an external OES texture.</p><h3>Why build something this low-level?</h3><p>A camera preview is an excellent stress test for a rendering architecture.</p><p>At 60 FPS, a frame budget is approximately <strong>16.67 ms</strong>.</p><p>That budget has to cover everything that happens between two display updates.</p><p>A rendering path that repeatedly allocates objects, copies pixels, blocks waiting for buffers, crosses JNI boundaries with large payloads, performs CPU-side geometry work, or forces GPU/CPU synchronization has fewer safe margins to work with.</p><p>The point of ZeroStallCam is not that Android’s higher-level APIs are “bad.”</p><p>The point is that when your problem is specifically:</p><blockquote><em>“I need to understand every important transition between camera memory and the GPU.”</em></blockquote><p>then a small native renderer makes those transitions visible.</p><h3>What is ZeroStallCam?</h3><p>ZeroStallCam is a native Android camera preview/rendering engine with:</p><ul><li><strong>Camera2 NDK</strong> for camera access</li><li><strong>AImageReader</strong> for native frame consumption</li><li><strong>AHardwareBuffer</strong> for hardware-backed frame memory</li><li><strong>EGLImageKHR</strong> for importing native buffers into EGL/OpenGL</li><li><strong>OpenGL ES 3.0</strong> for rendering</li><li><strong>GL_TEXTURE_EXTERNAL_OES</strong> for sampling the camera image</li><li><strong>JNI</strong> as the bridge between Kotlin and the native engine</li><li><strong>ECS-style components and systems</strong> to keep native state and logic separated</li><li><strong>procedural vertex generation</strong> through gl_VertexID</li><li><strong>uniform buffer objects (UBOs)</strong> for transform and timestamp data</li><li><strong>a bitmap font atlas</strong> for the on-screen timestamp</li><li><strong>Perfetto / ATrace instrumentation</strong> for profiling</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UnKZrqcE4JVGc8vY2P_N2A.png" /></figure><h3>The architecture</h3><p>At a high level:</p><pre>Kotlin / Android<br>       │<br>       │ JNI<br>       ▼<br>Native Engine<br>       │<br>       ├── Camera System<br>       ├── Buffer System<br>       ├── Render System<br>       ├── EGL Manager<br>       └── Shader Manager<br>       │<br>       ▼<br>Camera2 NDK + AHardwareBuffer + OpenGL ES</pre><p>The repository separates <strong>data</strong> from <strong>behavior</strong>.</p><p>The engine owns components such as:</p><ul><li>CameraComponent</li><li>BufferComponent</li><li>GraphicsComponent</li><li>TransformComponent</li><li>TimestampComponent</li></ul><p>while systems provide the behavior:</p><ul><li>CameraSystem</li><li>BufferSystem</li><li>RenderSystem</li><li>EglManager</li><li>ShaderManager</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G_QSv1HlpcM5I7DIv1gnjQ.png" /></figure><h3>Kotlin is deliberately thin</h3><p>The Kotlin side contains a SurfaceView called ZeroStallCamView.</p><p>It:</p><ol><li>loads the native library,</li><li>receives the Android Surface,</li><li>calls native initialization,</li><li>starts the camera,</li><li>schedules drawing from Choreographer,</li><li>and tears the native engine down when the surface disappears.</li></ol><p>Kotlin is <strong>not</strong> processing camera frames.</p><p>The JNI bridge is intentionally tiny:</p><pre>static std::unique_ptr&lt;ecs::Engine&gt; g_Engine;</pre><p>When the surface is created:</p><pre>g_Engine = std::make_unique&lt;ecs::Engine&gt;();<br>g_Engine-&gt;Init(window, am);</pre><p>Each display callback ultimately calls:</p><pre>g_Engine-&gt;DrawFrame();</pre><p>That is the philosophy of the project:</p><blockquote><em>Keep orchestration at the Java/Kotlin boundary; keep high-frequency work native.</em></blockquote><h3>Why Choreographer?</h3><p>The Kotlin view schedules drawing through Android’s Choreographer:</p><pre>private val frameCallback = object : Choreographer.FrameCallback {<br>    override fun doFrame(frameTimeNanos: Long) {<br>        if (isRendering) {<br>            nativeDrawFrame()<br>            Choreographer.getInstance().postFrameCallback(this)<br>        }<br>    }<br>}</pre><p>The relationship becomes:</p><pre>Display Vsync<br>     ↓<br>Choreographer<br>     ↓<br>JNI<br>     ↓<br>Engine::DrawFrame()<br>     ↓<br>OpenGL ES<br>     ↓<br>eglSwapBuffers()</pre><p>There is no timer loop trying to guess the display refresh rate.</p><h3>Camera2 NDK: capture directly into a native consumer</h3><p>The native camera setup:</p><ol><li>creates an ACameraManager,</li><li>enumerates camera IDs,</li><li>selects a rear-facing camera,</li><li>opens it,</li><li>builds a preview capture request,</li><li>targets the native ANativeWindow,</li><li>creates a capture session,</li><li>and starts a repeating request.</li></ol><p>For example:</p><pre>ACameraDevice_createCaptureRequest(<br>    camera.device,<br>    TEMPLATE_PREVIEW,<br>    &amp;camera.request<br>);</pre><pre>ACameraOutputTarget_create(<br>    window,<br>    &amp;camera.outputTarget<br>);</pre><pre>ACaptureRequest_addTarget(<br>    camera.request,<br>    camera.outputTarget<br>);</pre><p>Then:</p><pre>ACameraCaptureSession_setRepeatingRequest(<br>    camera.session,<br>    nullptr,<br>    1,<br>    &amp;camera.request,<br>    nullptr<br>);</pre><p>The camera’s output target is a native window owned by the rendering path.</p><h3>AImageReader and AHardwareBuffer</h3><p>BufferSystem::Init() creates the AImageReader using:</p><pre>AImageReader_newWithUsage(<br>    width,<br>    height,<br>    AIMAGE_FORMAT_YUV_420_888,<br>    AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |<br>    AHARDWAREBUFFER_USAGE_CAMERA_WRITE,<br>    3,<br>    &amp;buffer.reader<br>);</pre><p>The important part is that the buffer is camera-writable and GPU-sampleable.</p><p>The reader uses a small buffer count of <strong>3</strong>.</p><p>During rendering, the engine calls:</p><pre>AImageReader_acquireLatestImage(<br>    buffer.reader,<br>    &amp;img<br>);</pre><p>and extracts:</p><pre>AImage_getHardwareBuffer(<br>    img,<br>    &amp;hardwareBuffer<br>);</pre><p>The code acquires a reference to the hardware buffer before deleting the AImage.</p><h3>Why acquireLatestImage()?</h3><p>A live preview usually does not need to display every intermediate frame.</p><p>Imagine:</p><pre>Camera produces:<br>F1 → F2 → F3 → F4 → F5</pre><pre>Display is ready:<br>                └──────────────→ show F5</pre><p>For a preview, showing F5 is normally better than processing F1–F4 just because they arrived earlier.</p><p>A stale preview can feel worse than a dropped intermediate frame.</p><p>acquireLatestImage() is aligned with the project&#39;s goal:</p><blockquote><em>prioritize current visual state over processing every queued image.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*59V8YM07zoj1ewvLQ_J-Tw.png" /></figure><h3>Importing the hardware buffer into EGL</h3><p>Once the engine has an AHardwareBuffer, the next challenge is:</p><p><strong>How does OpenGL ES sample it?</strong></p><p>The renderer maintains:</p><pre>std::unordered_map&lt;<br>    AHardwareBuffer*,<br>    EGLImageKHR<br>&gt;</pre><p>The first time a buffer is encountered, the renderer creates an EGLImageKHR from its native client buffer.</p><p>Conceptually:</p><pre>AHardwareBuffer*<br>      ↓<br>eglGetNativeClientBufferANDROID()<br>      ↓<br>eglCreateImageKHR()<br>      ↓<br>EGLImageKHR<br>      ↓<br>glEGLImageTargetTexture2DOES()<br>      ↓<br>GL_TEXTURE_EXTERNAL_OES</pre><p>Subsequent uses can hit the cache instead of recreating the EGLImageKHR.</p><p>That cache is a key part of the low-overhead path.</p><h3>The external OES texture</h3><p>The camera texture is created as:</p><pre>glGenTextures(1, &amp;graphics.cameraTextureId);</pre><pre>glBindTexture(<br>    GL_TEXTURE_EXTERNAL_OES,<br>    graphics.cameraTextureId<br>);</pre><p>Then:</p><pre>glEGLImageTargetTexture2DOES(<br>    GL_TEXTURE_EXTERNAL_OES,<br>    graphics.eglImage<br>);</pre><p>The fragment shader samples it with:</p><pre>uniform samplerExternalOES sCamera;</pre><pre>fragColor = texture(sCamera, vTexCoord);</pre><p>The important idea is:</p><p><strong>OpenGL samples the camera-backed image directly instead of receiving a CPU-uploaded pixel array every frame.</strong></p><h3>“Zero-copy” needs a precise definition</h3><p>I use <strong>zero-copy</strong> carefully.</p><p>It does <strong>not</strong> mean:</p><blockquote><em>“Absolutely zero bytes are ever copied anywhere.”</em></blockquote><p>The font atlas, for example, is generated on the Kotlin side and uploaded once.</p><p>The useful claim is narrower:</p><blockquote><strong><em>The per-frame camera rendering path avoids CPU-side pixel copies and CPU-to-GPU frame uploads.</em></strong></blockquote><p>That distinction matters when presenting performance work accurately.</p><h3>The unusual part: no VBO for scene geometry</h3><p>A conventional path often looks like:</p><pre>CPU<br> ↓<br>Vertex array<br> ↓<br>VBO<br> ↓<br>GPU<br> ↓<br>vertex shader</pre><p>ZeroStallCam instead uses:</p><pre>int idx = gl_VertexID;</pre><p>The vertex shader derives positions and texture coordinates from the vertex ID.</p><p>The render call is simply:</p><pre>glDrawArrays(GL_TRIANGLES, 0, 54);</pre><p>One draw call.</p><h3>Why 54 vertices?</h3><p>The workload has two pieces.</p><h3>Camera background</h3><p>A fullscreen quad is two triangles:</p><p><strong>6 vertices</strong></p><h3>Timestamp overlay</h3><p>The timestamp is:</p><pre>HH:MM:SS</pre><p>Eight glyphs, each represented by a quad:</p><p><strong>8 × 6 = 48 vertices</strong></p><p>Total:</p><pre>6 + 48 = 54 vertices</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-qp2BA07JkNhyIl8mDgRRQ.png" /></figure><h3>The timestamp is generated on the GPU</h3><p>The CPU updates a tiny integer:</p><pre>mTimestamp.epochSeconds</pre><p>The shader computes:</p><pre>int h = (epoch / 3600) % 24;<br>int m = (epoch / 60) % 60;<br>int s = epoch % 60;</pre><p>and selects the correct character from the bitmap font atlas.</p><p>Instead of sending a text string every frame, the CPU sends a small numeric state value and the GPU derives the digits and their UV coordinates.</p><p>This is a simple example of a powerful graphics principle:</p><blockquote><em>When the data is tiny but the transformation is repeated every frame, push simple deterministic work to the GPU.</em></blockquote><h3>Uniform Buffer Objects</h3><p>The renderer keeps two UBOs:</p><pre>TransformBlock<br>TimestampBlock</pre><p>The transform state contains:</p><pre>screen width<br>screen height<br>camera width<br>camera height<br>rotation</pre><p>The timestamp state contains:</p><pre>epochSeconds</pre><p>Every frame, only those small pieces of state are updated.</p><p>We are not uploading a camera frame.</p><p>We are uploading instructions about how to interpret and draw it.</p><h3>Aspect ratio and rotation in the vertex shader</h3><p>The vertex shader compares screen and camera aspect ratios and calculates the crop coordinates.</p><p>It also handles 90° and 270° rotation cases.</p><p>That means camera-to-screen mapping is produced from a small amount of uniform state instead of rebuilding geometry on the CPU.</p><p>In other words:</p><blockquote><strong><em>send state, not geometry.</em></strong></blockquote><h3>A single draw call for camera + HUD</h3><p>RenderSystem::DrawFrame() binds:</p><ol><li>the external camera texture,</li><li>the font atlas,</li><li>the transform UBO,</li><li>the timestamp UBO,</li></ol><p>then submits:</p><pre>glDrawArrays(GL_TRIANGLES, 0, 54);</pre><p>The fragment shader uses the varying vType to decide whether a fragment comes from the camera texture or the font atlas.</p><p>The result is one render submission for both pieces of the scene.</p><p>A single draw call is not magically equivalent to “fast,” but reducing submission and state-transition overhead is a useful design goal.</p><h3>Profiling with ATrace and GPU timer queries</h3><p>The repository includes explicit measurement instrumentation.</p><p>CPU-side trace sections include:</p><pre>ZeroStall_UpdateTimestamp<br>ZeroStall_AcquireBuffer<br>ZeroStall_DrawFrame<br>ZeroStall_UboUpdate<br>ZeroStall_TextureBind</pre><p>GPU timing uses:</p><pre>GL_EXT_disjoint_timer_query</pre><p>and checks query availability before reading the result.</p><p>That avoids turning the measurement itself into a forced CPU/GPU synchronization point.</p><h3>The measured numbers</h3><p>The README reports this profiling run:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c31c2a040a8fb0af5a5b10d7f50d0f4/href">https://medium.com/media/5c31c2a040a8fb0af5a5b10d7f50d0f4/href</a></iframe><p>A 60 FPS frame budget is approximately:</p><pre>1000 / 60 = 16.67 ms</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*txLhARDxEW85utFcaidnOQ.png" /></figure><p>The average frame time is therefore inside a 60 FPS budget.</p><p>The maximum frame time is <strong>22.30 ms</strong>, so the honest interpretation is not “every frame is 60 FPS.”</p><p>It is:</p><blockquote><strong><em>The measured average stays within a 60 FPS budget, while occasional longer frames still occur.</em></strong></blockquote><p>That is the kind of wording I would use in a technical post because it distinguishes average performance from worst-case behavior.</p><h3>Why the 1.32 ms CPU number matters</h3><p>The reported average CPU blocking time is:</p><p><strong>1.32 ms</strong></p><p>against the project’s target threshold of 4 ms.</p><p>The useful observation is not that 1.32 ms is universally “fast.”</p><p>The useful observation is that the measured CPU-side renderer spends relatively little time waiting in the critical path compared with the full frame envelope.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Yx8xlhGqUQI9Ye8_Mh0Rjw.png" /></figure><p>For a streaming preview, this is exactly the kind of number worth profiling.</p><h3>The frame budget mental model</h3><p>Think of a 60 FPS frame as a 16.67 ms envelope:</p><pre>|----------------------- 16.67 ms -----------------------|<br>| camera | state | draw | swap | remaining headroom     |</pre><p>The architecture intentionally keeps the CPU side small:</p><pre>CPU:<br>  update tiny state<br>  acquire newest buffer<br>  bind resources<br>  issue draw</pre><pre>GPU:<br>  sample camera image<br>  generate geometry<br>  rasterize background<br>  rasterize timestamp</pre><p>The exact device-level timing is hardware- and driver-dependent, but the division of work is deliberate.</p><h3>Why ECS for a camera renderer?</h3><p>ECS can feel like overkill for one camera view.</p><p>But in native rendering code, separating state from behavior quickly becomes valuable as responsibilities grow.</p><p>For example:</p><pre>Engine<br> ├── CameraComponent<br> ├── BufferComponent<br> ├── GraphicsComponent<br> ├── TransformComponent<br> └── TimestampComponent</pre><p>while behavior lives in:</p><pre>CameraSystem<br>BufferSystem<br>RenderSystem<br>EglManager<br>ShaderManager</pre><p>That gives the project explicit ownership boundaries without turning the implementation into one monolithic native class.</p><h3>Resource lifecycle matters as much as rendering</h3><p>A performance renderer must also be a correct resource manager.</p><p>When the Android Surface is destroyed, the native engine is reset.</p><p>The engine then tears down:</p><pre>RenderSystem<br>EglManager<br>CameraSystem<br>BufferSystem</pre><p>The renderer destroys cached EGL images, clears the cache, deletes textures and UBOs, and releases other native graphics objects.</p><p>Native resource management is not secondary work here.</p><p>Leaking an EGL image, camera session, native window, or hardware buffer can destabilize the process itself.</p><h3>The complete frame lifecycle</h3><p>Putting everything together:</p><pre>Camera2 NDK<br>    │<br>    ▼<br>AImageReader<br>    │<br>    ▼<br>AHardwareBuffer<br>    │<br>    ▼<br>BufferSystem::AcquireLatestBuffer()<br>    │<br>    ▼<br>RenderSystem::UpdateCameraBuffer()<br>    │<br>    ├── cache lookup<br>    ├── eglCreateImageKHR() on miss<br>    └── glEGLImageTargetTexture2DOES()<br>    │<br>    ▼<br>Update tiny UBO state<br>    │<br>    ▼<br>glDrawArrays(GL_TRIANGLES, 0, 54)<br>    │<br>    ├── 6 camera vertices<br>    └── 48 HUD vertices<br>    │<br>    ▼<br>eglSwapBuffers()<br>    │<br>    ▼<br>AHardwareBuffer_release()</pre><p>That is the entire engine in one diagram.</p><h3>What makes it different from a normal CameraX demo?</h3><p>A typical application often optimizes for:</p><blockquote><em>“How quickly can I ship a reliable camera feature?”</em></blockquote><p>ZeroStallCam explores a different question:</p><blockquote><em>“What happens when I own the camera-to-GPU path?”</em></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1dd7989b1979e4f41a80a2db79d4491/href">https://medium.com/media/e1dd7989b1979e4f41a80a2db79d4491/href</a></iframe><p>Neither column is universally better.</p><p>They optimize for different constraints.</p><h3>Where this architecture becomes interesting</h3><p>The same design can be a foundation for:</p><h3>Computer-vision previews</h3><p>Keep the frame in native memory while another native component consumes it.</p><h3>AR-style overlays</h3><p>Treat the camera image as the background texture and compose overlays in the same graphics pipeline.</p><h3>Instrument and telemetry cameras</h3><p>Timestamp or metadata overlays become lightweight GPU-generated graphics.</p><h3>Embedded Android devices</h3><p>When hardware and software are tightly controlled, explicit native resource ownership becomes attractive.</p><h3>GPU image-processing experiments</h3><p>The external texture can become the starting point for additional GPU work.</p><h3>Limitations and honest caveats</h3><h3>Camera selection is simple</h3><p>The current implementation looks for a rear-facing camera and opens the first suitable result.</p><p>A production camera engine would likely expose camera selection and controls for resolution, frame rate, autofocus, exposure, and other metadata.</p><h3>The scene is intentionally tiny</h3><p>The current demo renders one camera background and one timestamp HUD.</p><p>This is a focused graphics experiment, not a general-purpose rendering engine.</p><h3>Driver and device compatibility matter</h3><p>Native graphics extensions are not abstract concepts; they depend on actual platform and driver support.</p><p>This architecture needs to be tested on the real device matrix that matters to you.</p><h3>The benchmark is not a universal performance claim</h3><p>The reported values are useful measurements from the project’s own instrumentation.</p><p>They are not a controlled A/B comparison against CameraX, SurfaceView, TextureView, or another renderer.</p><p>A serious benchmark should hold device, resolution, workload, warm-up period, thermal state, and measurement methodology constant.</p><h3>The principles behind ZeroStallCam</h3><p>The project is really a combination of a few principles.</p><h3>1. Don’t move large data unless necessary</h3><p>A camera frame is large.</p><p>Avoiding a per-frame CPU copy can matter much more than shaving a few instructions from a small calculation.</p><h3>2. Pass state, not geometry</h3><p>The CPU provides:</p><pre>screen size<br>camera size<br>rotation<br>timestamp</pre><p>The GPU generates the actual render geometry.</p><h3>3. Prefer the newest frame for preview workloads</h3><p>Freshness matters.</p><p>A live preview usually benefits more from current visual state than from processing every stale queued frame.</p><h3>4. Cache native graphics resources</h3><p>If the same hardware buffers recur, cache associated EGLImageKHR objects instead of recreating them every time.</p><h3>5. Measure the thing you actually built</h3><p>Performance work without instrumentation quickly becomes guesswork.</p><p>The repository includes both CPU trace points and GPU timing queries to reduce that uncertainty.</p><h3>A practical definition of “zero stall”</h3><p>I prefer to think of “zero stall” as an <strong>engineering goal</strong>, not a magical property.</p><p>The goal is to minimize unnecessary waiting between subsystems.</p><p>Conceptually:</p><pre><br>Less desirable:<br>CPU ───────── WAIT ─────────────────────→<br>                     ↑<br>                 synchronization<br><br>More desirable:<br>CPU ───── submit ────────────────────────→<br>GPU             ───────── render ────────→<br>CPU ───── prepare next frame ─────────────→</pre><p>The project’s architecture pushes toward this model through:</p><ul><li>latest-buffer acquisition,</li><li>cached EGL images,</li><li>no per-frame CPU vertex arrays,</li><li>a single procedural draw,</li><li>asynchronous-style GPU measurement,</li><li>and native ownership of the hot path.</li></ul><p>The device and driver still determine the final amount of waiting.</p><h3>What I would build next</h3><p>The next step would be turning the prototype into a reusable native camera graphics library.</p><p>A natural evolution could be:</p><pre>Camera abstraction<br>       ↓<br>Buffer abstraction<br>       ↓<br>Graphics abstraction<br>       ↓<br>Overlay / scene system<br>       ↓<br>Optional compute / CV pipeline</pre><p>That could enable:</p><ul><li>multiple overlays,</li><li>GPU effects,</li><li>color transforms,</li><li>camera switching,</li><li>frame timestamps,</li><li>native computer vision,</li><li>recording,</li><li>multi-pass rendering,</li><li>and possibly a Vulkan backend.</li></ul><p>The codebase is currently small enough that these changes remain approachable.</p><h3>Final thoughts</h3><p>ZeroStallCam started from a simple question:</p><blockquote><strong><em>Can an Android camera preview be designed so that the CPU does as little work as possible with the actual image data?</em></strong></blockquote><p>The implementation answers that question by combining:</p><p><strong>Camera2 NDK → AImageReader → AHardwareBuffer → EGLImageKHR → OpenGL ES external texture</strong></p><p>with:</p><p><strong>procedural GPU geometry + UBOs + a single draw call + native ECS orchestration.</strong></p><p>The current profiling run reports <strong>1,668 frames</strong>, <strong>15.28 ms average frame duration</strong>, <strong>22.30 ms maximum frame duration</strong>, and <strong>1.32 ms average CPU blocking time</strong>.</p><p>Those numbers do not prove that this renderer is faster than every other Android camera implementation.</p><p>What they demonstrate is more specific and, to me, more useful:</p><blockquote><strong><em>The architecture can keep the average frame near a 60 FPS budget while avoiding CPU-side copies of the camera frame in the per-frame render path.</em></strong></blockquote><p>The real lesson is not:</p><blockquote><em>“Use NDK because native code is faster.”</em></blockquote><p>It is:</p><blockquote><strong><em>Move ownership to the right layer, move large data as little as possible, push deterministic geometry work to the GPU, cache driver-facing resources, and measure every synchronization point you care about.</em></strong></blockquote><p>That mindset is useful well beyond camera previews — including AR, games, computer vision, embedded graphics, and real-time visualization.</p><p><a href="https://github.com/MohammadNouri5700/zerostallcam"><strong>https://github.com/MohammadNouri5700/zerostallcam</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0c8cb10f92ca" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>