feat(libcairo): write image surfaces to PNG streams #18382

Merged
bipinkrish merged 1 commit from upstream-pr/18382 into main 2026-11-12 12:04:38 +00:00
bipinkrish commented 2026-11-11 13:03:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by bipinkrish on 2026-11-11T13:03:43Z.


What

Implement cairo_surface_write_to_png_stream (A3FLVZ4FHbY) for Cairo image surfaces and subsurfaces. The writer handles the eight image formats, restores straight alpha, preserves 16-bit output for floating-point images, and stops at the first error returned by the guest System V callback. Subsurfaces use the pixel rectangle computed at creation and retain the underlying image.

Extend the shared PNG encoder with optional 16-bit samples and a white background chunk. Cairo uses these options to match native image metadata. The encoder buffers the stream before calling the writer, so compression and callback chunk sizes can differ from native Cairo. PS5 behavior remains unmeasured.

Tested

  • Ubuntu 24.04.4 x86-64, Release. All 12 Cairo and PNG CTests passed with ctest --test-dir build -R '^(guest_cairo_.*|decoder_png|guest_png_(enc|dec))$' --output-on-failure.

  • Compared 587 synthetic cases against Cairo 1.18.0. PNG dimensions, color types, bit depths, white background payloads and every reconstructed sample matched for the eight image formats. Coverage included padded and negative strides, scaled and clipped subsurfaces, and nested crops. CRCs validated, callback failures propagated at the requested call with the same status, and the surface status stayed successful.

  • Applied the production NID patcher to the built library and repeated the 587 cases through the resolved A3FLVZ4FHbY export.

  • Public header audit passed. Conventions passed against upstream main. The tested branch was based on 2ecc68bda26 and merged cleanly in a merge-tree check against 1e8dfd69d55 and the sibling Cairo geometry branch.

  • Ran tools/pr_overlap.py against 1,982 open PR heads. Manually reviewed #14212, #15208, #16895 and #16954. The shared lines are the Cairo debt summary, neighboring debt rows and the adjacent pattern declaration. Those PRs leave this PNG writer stub in place.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5259971062 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18382). Originally posted by [bipinkrish](https://github.com/bipinkrish) on 2026-11-11T13:03:43Z. --- ### What Implement `cairo_surface_write_to_png_stream` (`A3FLVZ4FHbY`) for Cairo image surfaces and subsurfaces. The writer handles the eight image formats, restores straight alpha, preserves 16-bit output for floating-point images, and stops at the first error returned by the guest System V callback. Subsurfaces use the pixel rectangle computed at creation and retain the underlying image. Extend the shared PNG encoder with optional 16-bit samples and a white background chunk. Cairo uses these options to match native image metadata. The encoder buffers the stream before calling the writer, so compression and callback chunk sizes can differ from native Cairo. PS5 behavior remains unmeasured. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> - Ubuntu 24.04.4 x86-64, Release. All 12 Cairo and PNG CTests passed with `ctest --test-dir build -R '^(guest_cairo_.*|decoder_png|guest_png_(enc|dec))$' --output-on-failure`. - Compared 587 synthetic cases against Cairo 1.18.0. PNG dimensions, color types, bit depths, white background payloads and every reconstructed sample matched for the eight image formats. Coverage included padded and negative strides, scaled and clipped subsurfaces, and nested crops. CRCs validated, callback failures propagated at the requested call with the same status, and the surface status stayed successful. - Applied the production NID patcher to the built library and repeated the 587 cases through the resolved `A3FLVZ4FHbY` export. - Public header audit passed. Conventions passed against upstream `main`. The tested branch was based on `2ecc68bda26` and merged cleanly in a merge-tree check against `1e8dfd69d55` and the sibling Cairo geometry branch. - Ran `tools/pr_overlap.py` against 1,982 open PR heads. Manually reviewed #14212, #15208, #16895 and #16954. The shared lines are the Cairo debt summary, neighboring debt rows and the adjacent pattern declaration. Those PRs leave this PNG writer stub in place. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
PrivacyOnion commented 2026-11-11 16:07:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-11T16:07:15Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 65fa4e73c9:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_cairo_png_tests built cleanly.
  • CTest (guest_cairo_png):
    1/1 Test #4210: guest_cairo_png ..................   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/Decoder/Png/include/Decoder/Png.hpp, core/Decoder/Png/src/Png.cpp, core/libs/prx/libcairo/Export.cpp, core/libs/prx/libcairo/Surface.cpp, core/libs/prx/libcairo/include/InventoryExports.hpp, core/libs/prx/libcairo/include/Surface.hpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • Verified cairo_surface_write_to_png_stream validates surface nullity, surface error status propagation (e.g. kInvalidSize), and writer callback nullity, forwarding unverified edge cases to NotImplemented_nid_no_patch per docs/dev/debt/libcairo.md.
    • Audited EncodeImage and ReadPixel across all 8 Cairo formats (ARGB32, RGB24, A8, A1, RGB565, RGB30, RGB96F, RGBA128F) for correct channel packing, straight-alpha un-premultiplication with zero-alpha protection, safe float-to-integer conversion, and big-endian sample ordering.
    • Audited RowAddress offset calculations, stride arithmetic (including negative strides and inverted coordinate spaces), and GuestCopyIn_nid_no_patch buffer bounds to ensure absence of buffer over-reads or undefined behavior.
    • Inspected subsurface coordinate inheritance in cairo_surface_create_for_rectangle and CairoPngSource_nid_no_patch, verifying nested subsurfaces correctly retain the root image data pointer and accumulate pixel offsets without leaking parent surface references.
    • Checked Decoder::Png::Encode 16-bit depth support, bKGD white background chunk generation for grayscale and RGB/RGBA color types, and Paeth/Sub/Up filtering pixel size scaling.
    • Confirmed full compliance with AGENTS.md rules: strict absence of illegal code comments, correct removal of cairo_surface_write_to_png_stream from debt tables and inventory headers, and proper conventional commit metadata.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5861704613 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18382#pullrequestreview-5861704613). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-11T16:07:15Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `65fa4e73c9`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_cairo_png_tests` built cleanly. - **CTest** (`guest_cairo_png`): ``` 1/1 Test #4210: guest_cairo_png .................. Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/Decoder/Png/include/Decoder/Png.hpp`, `core/Decoder/Png/src/Png.cpp`, `core/libs/prx/libcairo/Export.cpp`, `core/libs/prx/libcairo/Surface.cpp`, `core/libs/prx/libcairo/include/InventoryExports.hpp`, `core/libs/prx/libcairo/include/Surface.hpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - Verified cairo_surface_write_to_png_stream validates surface nullity, surface error status propagation (e.g. kInvalidSize), and writer callback nullity, forwarding unverified edge cases to NotImplemented_nid_no_patch per docs/dev/debt/libcairo.md. - Audited EncodeImage and ReadPixel across all 8 Cairo formats (ARGB32, RGB24, A8, A1, RGB565, RGB30, RGB96F, RGBA128F) for correct channel packing, straight-alpha un-premultiplication with zero-alpha protection, safe float-to-integer conversion, and big-endian sample ordering. - Audited RowAddress offset calculations, stride arithmetic (including negative strides and inverted coordinate spaces), and GuestCopyIn_nid_no_patch buffer bounds to ensure absence of buffer over-reads or undefined behavior. - Inspected subsurface coordinate inheritance in cairo_surface_create_for_rectangle and CairoPngSource_nid_no_patch, verifying nested subsurfaces correctly retain the root image data pointer and accumulate pixel offsets without leaking parent surface references. - Checked Decoder::Png::Encode 16-bit depth support, bKGD white background chunk generation for grayscale and RGB/RGBA color types, and Paeth/Sub/Up filtering pixel size scaling. - Confirmed full compliance with AGENTS.md rules: strict absence of illegal code comments, correct removal of cairo_surface_write_to_png_stream from debt tables and inventory headers, and proper conventional commit metadata. - Note: the AI-assisted checkbox can stay. Looks Good To Me
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!18382
No description provided.