Skip to content

Fix #4188: RGBImage scaled drawImage renders blank#4986

Merged
shai-almog merged 4 commits into
masterfrom
fix-4188-rgbimage-scaled-draw
May 20, 2026
Merged

Fix #4188: RGBImage scaled drawImage renders blank#4986
shai-almog merged 4 commits into
masterfrom
fix-4188-rgbimage-scaled-draw

Conversation

@shai-almog
Copy link
Copy Markdown
Collaborator

Summary

  • RGBImage.drawImage(Graphics, Object, int, int, int, int) previously fell through to Image's base implementation, which dispatches via the null native peer; the iOS Metal / OpenGL paths (and Android Skia / JavaSE Graphics2D) all silently rendered nothing because the bitmap pointer they received was null. The bug is platform-independent — the broken dispatch happens above the renderer.
  • Override the scaled-draw path on RGBImage: save the current graphics transform, compose a matrix-correct translate + scale via g.translateMatrix / g.scale, emit drawRGB at the image's native size so the platform pipeline performs the scaling in hardware, then restore the transform in a finally block. Using translateMatrix (not the integer translate accumulator) keeps the composition uniform with the scale step. The CPU scaleArray fallback is kept only for ports without matrix-correct translate (legacy JavaScript).
  • Extend the unit-test harness to make pixel-level rendering tests possible: TestCodenameOneImplementation now persists an affine transform on TestGraphics and drawRGB rasterises (inverse-mapped nearest-neighbour) through it. New overrides cover setTransform / getTransform / setTransformAffine / translateMatrix / scale / resetAffine. Existing tests were stubs that never exercised these paths, so behaviour is preserved.

Test plan

  • mvn test in maven/core-unittests — 2505/2505 pass.
  • New @FormTest cases in RGBImageTest:
    • testScaledDrawImageIntegerScale — 2×2 source → 4×4 canvas, asserts every pixel.
    • testScaledDrawImageAtOffset — 2×2 source drawn at (1, 1) into a 6×6 canvas, asserts the inner 4×4 plus the unaltered border ring.
    • testScaledDrawImageDownscale — 4×4 source → 2×2 canvas, asserts the representative pixel per quadrant.
    • testScaledDrawImagePreservesPriorTransform — asserts the graphics transform is identical before and after the scaled draw.
  • Manual smoke on iOS Metal simulator and Android emulator with a small RGBImage scaled inside a Label icon (visual confirmation that the previously-blank region now renders).

Fixes #4188.

🤖 Generated with Claude Code

RGBImage.drawImage(Graphics, Object, int, int, int, int) fell through to
Image's base implementation, which dispatches via the null native peer
and the iOS Metal / OpenGL pipelines (and equivalent on Android / JavaSE)
silently rendered nothing. The bug was platform-independent because the
broken dispatch happens before the renderer is reached.

Override the scaled-draw path on RGBImage: save the graphics transform,
compose a matrix-correct translate + scale via translateMatrix / scale,
emit drawRGB at native size so the platform pipeline performs the
scaling, then restore the transform. Using translateMatrix (not the
integer translate accumulator) keeps the composition uniform with the
scale step. Falls back to CPU scaleArray only on ports without matrix-
correct translate (legacy JavaScript).

Add a screenshot test path to the unit test harness:
- TestCodenameOneImplementation now persists an affine transform on
  TestGraphics and rasterises drawRGB (inverse-mapped nearest neighbour)
  through it, with overrides for setTransform / getTransform /
  setTransformAffine / translateMatrix / scale / resetAffine.
- RGBImageTest adds four @formtest cases for integer upscale, offset
  draws, downscale and transform restoration, asserting full-canvas
  pixel arrays.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 19, 2026

✅ Continuous Quality Report

Test & Coverage

Static Analysis

  • SpotBugs [Report archive]
    • ByteCodeTranslator: 0 findings (no issues)
    • android: 0 findings (no issues)
    • codenameone-maven-plugin: 0 findings (no issues)
    • core-unittests: 0 findings (no issues)
    • ios: 0 findings (no issues)
  • PMD: 0 findings (no issues) [Report archive]
  • Checkstyle: 0 findings (no issues) [Report archive]

Generated automatically by the PR CI workflow.

@shai-almog
Copy link
Copy Markdown
Collaborator Author

shai-almog commented May 19, 2026

Compared 110 screenshots: 110 matched.

Native Android coverage

  • 📊 Line coverage: 11.81% (6575/55655 lines covered) [HTML preview] (artifact android-coverage-report, jacocoAndroidReport/html/index.html)
    • Other counters: instruction 9.53% (33012/346569), branch 4.12% (1357/32925), complexity 5.18% (1634/31558), method 9.01% (1329/14749), class 15.07% (301/1998)
    • Lowest covered classes
      • kotlin.collections.kotlin.collections.ArraysKt___ArraysKt – 0.00% (0/6327 lines covered)
      • kotlin.collections.unsigned.kotlin.collections.unsigned.UArraysKt___UArraysKt – 0.00% (0/2384 lines covered)
      • org.jacoco.agent.rt.internal_b6258fc.asm.org.jacoco.agent.rt.internal_b6258fc.asm.ClassReader – 0.00% (0/1519 lines covered)
      • kotlin.collections.kotlin.collections.CollectionsKt___CollectionsKt – 0.00% (0/1148 lines covered)
      • org.jacoco.agent.rt.internal_b6258fc.asm.org.jacoco.agent.rt.internal_b6258fc.asm.MethodWriter – 0.00% (0/923 lines covered)
      • kotlin.sequences.kotlin.sequences.SequencesKt___SequencesKt – 0.00% (0/730 lines covered)
      • kotlin.text.kotlin.text.StringsKt___StringsKt – 0.00% (0/623 lines covered)
      • org.jacoco.agent.rt.internal_b6258fc.asm.org.jacoco.agent.rt.internal_b6258fc.asm.Frame – 0.00% (0/564 lines covered)
      • kotlin.collections.kotlin.collections.ArraysKt___ArraysJvmKt – 0.00% (0/495 lines covered)
      • kotlinx.coroutines.kotlinx.coroutines.JobSupport – 0.00% (0/423 lines covered)

✅ Native Android screenshot tests passed.

Native Android coverage

  • 📊 Line coverage: 11.81% (6575/55655 lines covered) [HTML preview] (artifact android-coverage-report, jacocoAndroidReport/html/index.html)
    • Other counters: instruction 9.53% (33012/346569), branch 4.12% (1357/32925), complexity 5.18% (1634/31558), method 9.01% (1329/14749), class 15.07% (301/1998)
    • Lowest covered classes
      • kotlin.collections.kotlin.collections.ArraysKt___ArraysKt – 0.00% (0/6327 lines covered)
      • kotlin.collections.unsigned.kotlin.collections.unsigned.UArraysKt___UArraysKt – 0.00% (0/2384 lines covered)
      • org.jacoco.agent.rt.internal_b6258fc.asm.org.jacoco.agent.rt.internal_b6258fc.asm.ClassReader – 0.00% (0/1519 lines covered)
      • kotlin.collections.kotlin.collections.CollectionsKt___CollectionsKt – 0.00% (0/1148 lines covered)
      • org.jacoco.agent.rt.internal_b6258fc.asm.org.jacoco.agent.rt.internal_b6258fc.asm.MethodWriter – 0.00% (0/923 lines covered)
      • kotlin.sequences.kotlin.sequences.SequencesKt___SequencesKt – 0.00% (0/730 lines covered)
      • kotlin.text.kotlin.text.StringsKt___StringsKt – 0.00% (0/623 lines covered)
      • org.jacoco.agent.rt.internal_b6258fc.asm.org.jacoco.agent.rt.internal_b6258fc.asm.Frame – 0.00% (0/564 lines covered)
      • kotlin.collections.kotlin.collections.ArraysKt___ArraysJvmKt – 0.00% (0/495 lines covered)
      • kotlinx.coroutines.kotlinx.coroutines.JobSupport – 0.00% (0/423 lines covered)

Benchmark Results

Detailed Performance Metrics

Metric Duration
Base64 payload size 8192 bytes
Base64 benchmark iterations 6000
Base64 native encode 1036.000 ms
Base64 CN1 encode 217.000 ms
Base64 encode ratio (CN1/native) 0.209x (79.1% faster)
Base64 native decode 971.000 ms
Base64 CN1 decode 295.000 ms
Base64 decode ratio (CN1/native) 0.304x (69.6% faster)
Image encode benchmark status skipped (SIMD unsupported)

@shai-almog
Copy link
Copy Markdown
Collaborator Author

shai-almog commented May 19, 2026

Compared 108 screenshots: 108 matched.
✅ Native iOS screenshot tests passed.

Benchmark Results

  • VM Translation Time: 0 seconds
  • Compilation Time: 223 seconds

Build and Run Timing

Metric Duration
Simulator Boot 76000 ms
Simulator Boot (Run) 1000 ms
App Install 27000 ms
App Launch 29000 ms
Test Execution 322000 ms

Detailed Performance Metrics

Metric Duration
Base64 payload size 8192 bytes
Base64 benchmark iterations 6000
Base64 native encode 768.000 ms
Base64 CN1 encode 2185.000 ms
Base64 encode ratio (CN1/native) 2.845x (184.5% slower)
Base64 native decode 615.000 ms
Base64 CN1 decode 1797.000 ms
Base64 decode ratio (CN1/native) 2.922x (192.2% slower)
Base64 SIMD encode 682.000 ms
Base64 encode ratio (SIMD/native) 0.888x (11.2% faster)
Base64 encode ratio (SIMD/CN1) 0.312x (68.8% faster)
Base64 SIMD decode 563.000 ms
Base64 decode ratio (SIMD/native) 0.915x (8.5% faster)
Base64 decode ratio (SIMD/CN1) 0.313x (68.7% faster)
Image encode benchmark iterations 100
Image createMask (SIMD off) 172.000 ms
Image createMask (SIMD on) 13.000 ms
Image createMask ratio (SIMD on/off) 0.076x (92.4% faster)
Image applyMask (SIMD off) 307.000 ms
Image applyMask (SIMD on) 112.000 ms
Image applyMask ratio (SIMD on/off) 0.365x (63.5% faster)
Image modifyAlpha (SIMD off) 291.000 ms
Image modifyAlpha (SIMD on) 300.000 ms
Image modifyAlpha ratio (SIMD on/off) 1.031x (3.1% slower)
Image modifyAlpha removeColor (SIMD off) 375.000 ms
Image modifyAlpha removeColor (SIMD on) 464.000 ms
Image modifyAlpha removeColor ratio (SIMD on/off) 1.237x (23.7% slower)
Image PNG encode (SIMD off) 1878.000 ms
Image PNG encode (SIMD on) 1239.000 ms
Image PNG encode ratio (SIMD on/off) 0.660x (34.0% faster)
Image JPEG encode 787.000 ms

@shai-almog
Copy link
Copy Markdown
Collaborator Author

shai-almog commented May 19, 2026

Compared 110 screenshots: 110 matched.
✅ Native iOS Metal screenshot tests passed.

Benchmark Results

  • VM Translation Time: 0 seconds
  • Compilation Time: 321 seconds

Build and Run Timing

Metric Duration
Simulator Boot 90000 ms
Simulator Boot (Run) 1000 ms
App Install 30000 ms
App Launch 16000 ms
Test Execution 338000 ms

Detailed Performance Metrics

Metric Duration
Base64 payload size 8192 bytes
Base64 benchmark iterations 6000
Base64 native encode 918.000 ms
Base64 CN1 encode 1989.000 ms
Base64 encode ratio (CN1/native) 2.167x (116.7% slower)
Base64 native decode 434.000 ms
Base64 CN1 decode 1450.000 ms
Base64 decode ratio (CN1/native) 3.341x (234.1% slower)
Base64 SIMD encode 681.000 ms
Base64 encode ratio (SIMD/native) 0.742x (25.8% faster)
Base64 encode ratio (SIMD/CN1) 0.342x (65.8% faster)
Base64 SIMD decode 560.000 ms
Base64 decode ratio (SIMD/native) 1.290x (29.0% slower)
Base64 decode ratio (SIMD/CN1) 0.386x (61.4% faster)
Image encode benchmark iterations 100
Image createMask (SIMD off) 109.000 ms
Image createMask (SIMD on) 17.000 ms
Image createMask ratio (SIMD on/off) 0.156x (84.4% faster)
Image applyMask (SIMD off) 317.000 ms
Image applyMask (SIMD on) 160.000 ms
Image applyMask ratio (SIMD on/off) 0.505x (49.5% faster)
Image modifyAlpha (SIMD off) 564.000 ms
Image modifyAlpha (SIMD on) 237.000 ms
Image modifyAlpha ratio (SIMD on/off) 0.420x (58.0% faster)
Image modifyAlpha removeColor (SIMD off) 332.000 ms
Image modifyAlpha removeColor (SIMD on) 227.000 ms
Image modifyAlpha removeColor ratio (SIMD on/off) 0.684x (31.6% faster)
Image PNG encode (SIMD off) 1916.000 ms
Image PNG encode (SIMD on) 1341.000 ms
Image PNG encode ratio (SIMD on/off) 0.700x (30.0% faster)
Image JPEG encode 782.000 ms

shai-almog and others added 3 commits May 20, 2026 04:02
…lback

The translateMatrix + scale approach worked when drawing to a mutable
image (xTranslate/yTranslate are 0) but rendered off-target on the
screen. On ports where impl.isTranslationSupported() is false (iOS),
g.translate(...) accumulates xTranslate/yTranslate on the Graphics
object and those are baked into draw coordinates BEFORE the impl matrix
is applied. A naked translateMatrix + scale composition therefore
multiplied the accumulator by the scale factor, shifting the image by
(sx-1)*xTranslate, (sy-1)*yTranslate.

Use Graphics.setTransform instead: it conjugates the matrix with
T(xTranslate, yTranslate) * M * T(-xTranslate, -yTranslate), cancelling
the accumulator so the image lands at (xT + x, yT + y) regardless of
the scale factor.

Also drop the isTransformSupported / isTranslateMatrixSupported gates
and the CPU scaleArray fallback. All shipped ports (iOS Metal/OpenGL,
Android, JavaSE) return true for both checks, so the gates always
passed in practice and the fallback was dead code on production
platforms while costing an extra int[] allocation on the legacy JS
port (whose matrix support is functionally adequate anyway).

Add a regression test that pre-translates the graphics before the
scaled draw and asserts the image lands at (xTranslate + x, yTranslate
+ y) -- this is the case the previous tests missed because they drew
into a fresh canvas where xTranslate/yTranslate were zero.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The DrawImage test in scripts/hellocodenameone exercises the
drawImage(RGBImage, x, y, w, h) overload twice -- previously broken
under #4188 -- so those regions in the prior reference were left as
the canvas background instead of the scaled gradient sphere. With the
fix, the scaled RGBImage now renders, so the stored reference no
longer matches and the Android instrumentation tests fail at
"graphics-draw-image-rect: Screenshot differs (320x640 px, bit depth
8)".

Promote the artifact produced by the failing CI run (which is the
correct rendering -- the previously-black regions now show the
gradient sphere) as the new Android golden.

iOS / iOS Metal / JavaScript goldens almost certainly also need a
refresh, but their screenshot-comparison workflows did not run on
this PR (the iOS workflow only builds the ScreenshotTest.m sources
without comparing) so those updates are deferred to whichever PR
re-triggers those pipelines.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The iOS UI workflow flagged the same diff Android did --
"Test 'graphics-draw-image-rect': Screenshot differs (1179x2556 px,
bit depth 8)" in both build-ios and build-ios-metal -- but
scripts-ios.yml does not set CN1SS_FAIL_ON_MISMATCH=1, so the job
reports the mismatch without failing. That hid the iOS goldens from
my first sweep. Visually the same as Android: the previously-blank
regions under the two drawImage(rgbImage, x, y, w, h) calls now show
the scaled gradient sphere.

Promote the artifacts produced by build-ios and build-ios-metal as
the new goldens.

scripts/javascript/screenshots/graphics-draw-image-rect.png also
needs refreshing for the same reason, but the JS workflow does not
run on this PR (its path filter excludes CodenameOne/src/**), so
deferring that until the JS pipeline produces a new artifact.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@shai-almog shai-almog merged commit 915e364 into master May 20, 2026
20 checks passed
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.

RGBImage can't be scaled dynamically

1 participant