Skip to content

feat(image,stream): zero-copy image adopt-ctor + to_image bridge (Phase 2 PR1) - #516

Merged
transfix merged 2 commits into
masterfrom
feat/image-adopt-ctor
Oct 1, 2026
Merged

transfix merged 2 commits into
masterfrom
feat/image-adopt-ctor

Conversation

@transfix

@transfix transfix commented Oct 1, 2026

Copy link
Copy Markdown
Owner

Foundational H2 piece for the Phase-2 streaming sinks (roadmap STATE_BINARY_STREAMING.md). Lets a stream video frame become a cvc::image with no copy, so frame bytes can flow into a GL texture (PR2) zero-copy.

cvc::image

  • New adopt ctor image(w,h,f,dt,bytes,keepalive) — ALIASES foreign interleaved bytes (no alloc/memcpy). The shared_ptr<void> keepalive is bridged into the boost::shared_array<unsigned char> buffer via a no-op deleter that captures it, so foreign storage lives exactly as long as the image + any COW copy / storage() handle.
  • detach() now also forks an adopted buffer on first mutation (adopted buffers have use_count()==1 — the keepalive lives inside the deleter — so the plain use_count()>1 test alone would hand a writable pointer into immutable shared frame bytes). New _adopted flag; storage() documented read-only for adopted images.

cvc::stream

  • format_desc gains frame_origin (top_left default / bottom_left).
  • to_image(frame_ptr) aliases a video_raw frame as a cvc::image, co-owning the frame keepalive; throws on anything it can't alias losslessly: null/non-video, unknown or channel-swapped codec (matching-order only — never a silent reinterpret), padded stride (stride==0 = dense), bottom-left origin, or short buffer.

Tests: stream_to_image_test — alias-without-copy, mutation-forks-leaving-source-untouched, keepalive-released-exactly-when-last-alias-drops; to_image codec/stride/origin/size/kind gates + pixel round-trip. 12/12 pass, valgrind memcheck clean. Pure cvc::image + cvc::stream; no GL/Python/hardware.

First of the 6 Phase-2 PRs; folds in the planning-critique MED/LOW findings for to_image (origin-aware, stride==0-dense, strict codec map) and the mandatory adopted-buffer copy-on-write. Flags vs the roadmap: the const void* ctor copied (no adopt ctor existed); line-refs in the roadmap were stale.

…ge (Phase 2 PR1)

Foundational H2 piece for the Phase-2 streaming sinks (roadmap
STATE_BINARY_STREAMING.md). Lets a stream video frame become a cvc::image with
no copy, so a frame's bytes can flow into a GL texture (PR2) zero-copy.

cvc::image:
- New adopt constructor image(w,h,f,dt,bytes,keepalive) that ALIASES foreign
  interleaved bytes with no alloc/memcpy. The shared_ptr<void> keepalive is
  bridged into the boost::shared_array<unsigned char> buffer via a no-op deleter
  that captures it, so the foreign storage lives exactly as long as the image and
  any copy-on-write copy / storage() handle.
- detach() now also forks an adopted buffer on first mutation (an adopted buffer
  has use_count()==1 — its keepalive lives inside the deleter — so the plain
  use_count()>1 test alone would hand a writable pointer into immutable, shared
  frame bytes). New _adopted flag drives this. storage() documented read-only for
  adopted images.

cvc::stream:
- format_desc gains a frame_origin (top_left default / bottom_left) so the bridge
  can reject un-aliasable row order.
- to_image(frame_ptr) aliases a video_raw frame as a cvc::image, co-owning the
  frame keepalive. Rejects (throws) what it cannot alias losslessly: null/non-
  video frame, unknown or channel-swapped codec (only matching-order codecs map;
  never a silent reinterpret), padded row stride (stride==0 treated as dense),
  bottom-left origin, or a buffer smaller than w*h*bpp.

Tests: stream_to_image_test — adopt aliases without copy, mutation forks leaving
source untouched, keepalive released exactly when the last alias drops; to_image
codec/stride/origin/size/kind gates + pixel round-trip. 12/12 pass, valgrind
memcheck clean. Pure cvc::image + cvc::stream; no GL/Python/hardware.
…tests

Adversarial review of PR1 (verdict SHIP): fold in the one MED gap + the LOW
test-coverage notes.

- MED: to_image() now throws on a frame with null data (previously a malformed
  frame with data==null but a large claimed size passed every check and the adopt
  ctor's own `bytes` guard silently produced an EMPTY image, violating the
  "throws on anything it cannot alias losslessly" contract).
- Tests: RejectsNullData; AliasesNonRgbaMatchingCodecs (rgb8/gray8/graya8 map +
  alias); CopyDivergesOnMutationLeavingSibling (b=a; mutate a; assert b still
  aliases the original source bytes, then b forks on its own mutation).

15/15 pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant