Files
kryon/tests/android_image_services_test.zi
T

258 lines
16 KiB
Ziran

Services :: #import "pixmap_assets_android";
#import "android_avif"
#import "android_webp"
Pixels :: #import "image_pixels";
Decode :: #import "android_image_decode";
Raster :: #import "pixmap";
Jni :: #import "android_jni";
Cleanup :: #import "android_host_cleanup";
#import "geometry"
#import "android_native_test_host"
#import "image_integration_fixture"
#import "webp_fixture_checks"
#import "std/byte_text_linux"
functions: Jni.JniFunctions;
vm_functions: Jni.JniVmFunctions;
env: Jni.JniEnv;
vm: Jni.JniVm;
activity: Jni.NativeActivity;
app: Jni.NativeAndroidApp;
large_surface: [32768]u32;
frames: s32;
references: s32;
bitmap_calls: s32;
bad_bitmap: bool;
bridge_missing: bool;
bitmap: [4]u32 = .[2, 1, 0x80341256, 0xff090807];
GetEnv :: (input: *Jni.JniVm, output: **Jni.JniEnv, version: s32) -> s32 {
unused input; unused version; output.* = *env; return 0
}
Push :: (input: *Jni.JniEnv, capacity: s32) -> s32 { unused input; unused capacity; frames += 1; return 0 }
Pop :: (input: *Jni.JniEnv, result: *void) -> *void { unused input; frames -= 1; return result }
Exception :: (input: *Jni.JniEnv) -> u8 { unused input; return 0 }
Clear :: (input: *Jni.JniEnv) { unused input }
ObjectClass :: (input: *Jni.JniEnv, object: *void) -> *void {
unused input; unused object; references += 1; return cast(*void)cast(usize)1
}
Method :: (input: *Jni.JniEnv, klass: *void, name: *u8, signature: *u8) -> *void {
unused input; unused klass
if bridge_missing || TextFromCString(name) != "decodeBundleImage" || TextFromCString(signature) != "([BI)[I" { return null }
return cast(*void)cast(usize)2
}
Delete :: (input: *Jni.JniEnv, object: *void) { unused input; unused object; references -= 1 }
NewBytes :: (input: *Jni.JniEnv, count: s32) -> *void {
unused input; unused count; references += 1; return cast(*void)cast(usize)3
}
SetBytes :: (input: *Jni.JniEnv, array: *void, start: s32, count: s32, bytes: *u8) {
unused input; unused array; unused start; unused count; unused bytes
}
Call :: (input: *Jni.JniEnv, object: *void, method: *void, arguments: *Jni.JniValue) -> *void {
unused input; unused object; unused method; unused arguments
bitmap_calls += 1; references += 1; return cast(*void)cast(usize)4
}
Length :: (input: *Jni.JniEnv, array: *void) -> s32 { unused input; unused array; return 4 }
ReadInts :: (input: *Jni.JniEnv, array: *void, start: s32, count: s32, output: *s32) {
unused input; unused array
for at: cast(s32)0..count - 1 { output[at] = cast(s32)bitmap[start + at] }
if bad_bitmap && start == 0 { output[0] = 1; output[1] = 2 }
}
MockActivity :: () {
vm_functions.GetEnv = GetEnv
functions.PushLocalFrame = Push; functions.PopLocalFrame = Pop
functions.ExceptionCheck = Exception; functions.ExceptionClear = Clear
functions.ExceptionDescribe = Clear; functions.GetObjectClass = ObjectClass
functions.GetMethodID = Method; functions.DeleteLocalRef = Delete
functions.NewByteArray = NewBytes; functions.SetByteArrayRegion = SetBytes
functions.CallObjectMethodA = Call; functions.GetArrayLength = Length
functions.GetIntArrayRegion = ReadInts
env.functions = *functions; vm.functions = *vm_functions
activity.vm = *vm; activity.activity = cast(*void)cast(usize)5
app.activity = *activity; Jni.jni_android_app = *app
}
// APK assets: only pictures/shipped.* is packaged, holding asset_pointer.
asset_manager: u8;
asset_pointer: *u8;
asset_count: s32;
not_a_picture: [8]u8;
asset_opens: s32;
asset_closes: s32;
#program_export
AAssetManager_open :: (manager: *void, path: *u8, mode: s32) -> *void {
unused mode
asset_opens += 1
name := TextFromCString(path)
if manager != cast(*void)*asset_manager || name.count < 17 || name[0:17] != "pictures/shipped." { return null }
return cast(*void)asset_pointer
}
#program_export
AAsset_getLength :: (asset: *void) -> s32 { unused asset; return asset_count }
#program_export
AAsset_read :: (asset: *void, output: *void, count: s32) -> s32 {
unused asset
bytes := cast(*u8)output
for at: cast(s32)0..count - 1 { bytes[at] = asset_pointer[at] }
return count
}
#program_export
AAsset_getBuffer :: (asset: *void) -> *u8 { unused asset; return asset_pointer }
#program_export
AAsset_close :: (asset: *void) { unused asset; asset_closes += 1 }
CheckPreview :: (data: string, width: s32, height: s32, expected: []u8) -> bool {
w: s32; h: s32; channels: s32; upright: bool
pixels := Services.ImagePreviewDecode(data, ".wrong", *w, *h, *channels, *upright)
defer Services.ImagePreviewRelease(pixels)
if pixels == null || w != width || h != height || channels != 4 || !upright { return false }
for at: 0..expected.count - 1 { if pixels[at] != expected[at] { return false } }
return true
}
#program_export
main :: () -> s32 {
png := Fixture("build/scratch/image-pixels", "rgba", ".png"); defer VecFree(png)
png_data := TextFromBytes(png[:])
expected: [8]u8 = .[173, 61, 219, 97, 19, 203, 41, 255]
if !CheckPreview(png_data, 2, 1, expected[:]) { return 1 }
webp := Fixture("tests/fixtures/webp", "lossless", ".webp"); defer VecFree(webp)
decoded := Decode.AndroidImageDecodeEncoded(TextFromBytes(webp[:]), ".png")
if decoded.pixels == null || WebPFixtureCheck("lossless", decoded.pixels, decoded.width, decoded.height, 0).count != 0 { return 2 }
Services.ImagePreviewRelease(decoded.pixels)
avif := Fixture("tests/fixtures/avif", "transform31", ".avif"); defer VecFree(avif)
golden := Fixture("tests/fixtures/avif", "transform31", ".rgba"); defer VecFree(golden)
if golden.count != 64 || !CheckPreview(TextFromBytes(avif[:]), 4, 4, golden[:]) { return 3 }
// The VM is a deterministic OS boundary here. Real PNG, WebP and AVIF
// decoding above uses native package codecs; this checks bitmap JNI
// ownership, dimension checks and Exif transformation independently.
MockActivity()
jpeg := Fixture("build/scratch/android-image-services", "orientation6", ".jpg"); defer VecFree(jpeg)
jpeg_expected: [8]u8 = .[52, 18, 86, 128, 9, 8, 7, 255]
if !CheckPreview(TextFromBytes(jpeg[:]), 1, 2, jpeg_expected[:]) || bitmap_calls != 1 || frames != 0 || references != 0 { return 4 }
bad_bitmap = true
if Decode.AndroidImageDecodeEncoded(TextFromBytes(jpeg[:]), "").pixels != null || frames != 0 || references != 0 { return 5 }
bad_bitmap = false
huge := Fixture("build/scratch/android-image-services", "huge", ".png"); defer VecFree(huge)
w: s32 = 99; h: s32 = 99; channels: s32 = 99; upright: bool
if Services.ImagePreviewDecode(TextFromBytes(huge[:]), ".png", *w, *h, *channels, *upright) != null ||
w != 0 || h != 0 || channels != 0 || bitmap_calls != 2 { return 6 }
if Decode.AndroidImageDecodeEncoded("invalid", "").pixels != null || Decode.AndroidImageDecodeEncoded(png_data[:20], "").pixels != null { return 7 }
if !Services.PixmapLoadImageBytes("pictures/a.png", png_data, "") || Services.android_picture_bytes != 8 { return 8 }
slot := Raster.PixmapImageIndex("pictures/a.png")
source: Rectangle = .{0.0, 0.0, 2.0, 1.0}
if slot < 0 || !Raster.PixmapPrepareImage(slot, *source, 2.0, 1.0) { return 36 }
if Raster.pixmap_image_pixels[slot].value[0] != cast(u32)0x80341256 { return 9 }
previous := *Raster.pixmap_image_pixels[slot].value[0]
if Services.PixmapLoadImageBytes("pictures/a.png", "invalid", "") || *Raster.pixmap_image_pixels[slot].value[0] != previous { return 10 }
if !Services.PixmapLoadImageBytes("pictures/a.png", TextFromBytes(webp[:]), "") || Services.android_picture_bytes != 24 { return 11 }
if !Services.PixmapLoadImageBytes("pictures/b.png", png_data, "") || !Services.PixmapLoadImageBytes("pictures-extra/c.png", png_data, "") { return 12 }
if Services.RasterInvalidateImagesIn("pictures/") != 2 || Services.android_picture_bytes != 8 || Raster.PixmapImageIndex("pictures-extra/c.png") < 0 { return 13 }
slot = Raster.PixmapImageIndex("pictures-extra/c.png")
// An app can replace an owned picture with borrowed pixels. Releasing
// the previous allocation must not claim ownership of the new slice.
borrowed: [1]u32 = .[cast(u32)0xff112233]
if !Raster.PixmapSetImage(slot, "borrowed", 1, 1, borrowed[:]) || Services.android_picture_bytes != 0 { return 14 }
if !Services.RasterInvalidateImage("borrowed") || borrowed[0] != cast(u32)0xff112233 || Services.RasterInvalidateImage("borrowed") { return 15 }
if !Services.PixmapLoadImageBytes("/picture", png_data, "") || !Services.PixmapLoadImageBytes("relative", png_data, "") { return 16 }
if Services.RasterInvalidateImagesIn("/") != 1 || Services.android_picture_bytes != 8 { return 17 }
Cleanup.AndroidHostResourcesClose()
if Services.android_picture_bytes != 0 || Raster.PixmapImageIndex("relative") >= 0 { return 18 }
if Services.RasterInvalidateImages() != 0 || Services.PixmapLoadImageBytes("", png_data, "") { return 19 }
// A shipped picture probes dimensions without decoded storage; a missing one is asked for once until a picture is cleared.
activity.asset_manager = cast(*void)*asset_manager
asset_pointer = *png[0]; asset_count = cast(s32)png.count
if Raster.ImageWidth("pictures/shipped.png") != 2 || Raster.ImageHeight("pictures/shipped.png") != 1 ||
asset_opens != 1 || asset_closes != 1 || Services.android_picture_bytes != 0 { return 20 }
slot = Raster.PixmapImageIndex("pictures/shipped.png")
source = .{0.0, 0.0, 2.0, 1.0}
if slot < 0 || !Raster.PixmapPrepareImage(slot, *source, 2.0, 1.0) { return 36 }
if Raster.pixmap_image_pixels[slot].value[0] != cast(u32)0x80341256 ||
Raster.PixmapPaintImageSlot("pictures/shipped.png", 0) != slot || asset_opens != 2 { return 21 }
if Raster.ImageWidth("pictures/missing.png") != 0 || Raster.ImageHeight("pictures/missing.png") != 0 ||
Raster.PixmapPaintImageSlot("pictures/missing.png", 0) >= 0 || asset_opens != 3 { return 22 }
if !Services.RasterInvalidateImage("pictures/shipped.png") || Services.android_picture_bytes != 0 ||
Raster.PixmapPaintImageSlot("pictures/shipped.png", 0) < 0 || asset_opens != 4 ||
Raster.ImageWidth("pictures/missing.png") != 0 || asset_opens != 5 { return 23 }
asset_pointer = *not_a_picture[0]; asset_count = 8; unused Services.RasterInvalidateImages()
if Raster.ImageWidth("pictures/shipped.png") != 0 || asset_opens != 6 || asset_closes != 4 { return 24 }
asset_pointer = *png[0]; asset_count = cast(s32)png.count
Cleanup.AndroidHostResourcesClose()
if Raster.ImageWidth("pictures/shipped.png") != 2 || asset_opens != 7 { return 25 }
Cleanup.AndroidHostResourcesClose()
if Services.android_picture_bytes != 0 || Raster.PixmapImageIndex("pictures/shipped.png") >= 0 { return 26 }
// Scene textures and CPU images decode PNG, WebP and AVIF in process,
// without the activity's Bitmap bridge.
bitmap_calls = 0
asset_pointer = *avif[0]; asset_count = cast(s32)avif.count
avif_texture := Pixels.LoadImagePixels("pictures/shipped.avif"); defer VecFree(avif_texture.pixels)
if avif_texture.width != 4 || avif_texture.height != 4 || avif_texture.pixels.count != 64 || bitmap_calls != 0 { return 27 }
for at: 0..63 { if avif_texture.pixels[at] != golden[at] { return 28 } }
asset_pointer = *webp[0]; asset_count = cast(s32)webp.count
webp_texture := Pixels.LoadImagePixels("pictures/shipped.webp"); defer VecFree(webp_texture.pixels)
if webp_texture.pixels.count == 0 || bitmap_calls != 0 ||
WebPFixtureCheck("lossless", *webp_texture.pixels[0], webp_texture.width, webp_texture.height, 0).count != 0 { return 29 }
asset_pointer = *png[0]; asset_count = cast(s32)png.count
png_texture := Pixels.LoadImagePixels("pictures/shipped.png"); defer VecFree(png_texture.pixels)
if png_texture.width != 2 || png_texture.height != 1 || png_texture.pixels.count != 8 || bitmap_calls != 0 { return 30 }
for at: 0..7 { if png_texture.pixels[at] != expected[at] { return 31 } }
// A texture can still go through the activity's Bitmap bridge.
asset_pointer = *jpeg[0]; asset_count = cast(s32)jpeg.count
jpeg_texture := Pixels.LoadImagePixels("pictures/shipped.jpg"); defer VecFree(jpeg_texture.pixels)
if jpeg_texture.width != 1 || jpeg_texture.height != 2 || jpeg_texture.pixels.count != 8 ||
bitmap_calls != 1 || frames != 0 || references != 0 { return 32 }
for at: 0..7 { if jpeg_texture.pixels[at] != jpeg_expected[at] { return 33 } }
// Without the bridge, repeated failures return an empty image and log once.
bridge_missing = true
logs := native_test_log_lines
missing_bridge := Pixels.LoadImagePixels("pictures/shipped.jpg"); defer VecFree(missing_bridge.pixels)
if missing_bridge.pixels.count != 0 || native_test_log_lines != logs + 1 || frames != 0 || references != 0 { return 34 }
again := Pixels.LoadImagePixels("pictures/shipped.jpg"); defer VecFree(again.pixels)
if again.pixels.count != 0 || native_test_log_lines != logs + 1 || bitmap_calls != 1 { return 35 }
bridge_missing = false
// Large shipped WebP: metadata allocates no RGBA, first draw retains a
// 64x smaller high-quality copy, and repeated/smaller draws reuse it.
Cleanup.AndroidHostResourcesClose()
large := Fixture("build/scratch/android-image-services", "large", ".webp"); defer VecFree(large)
small := Fixture("build/scratch/android-image-services", "large", ".rgba"); defer VecFree(small)
asset_pointer = *large[0]; asset_count = cast(s32)large.count
if Raster.ImageWidth("pictures/shipped.webp") != 2048 || Raster.ImageHeight("pictures/shipped.webp") != 1024 ||
Services.android_picture_bytes != 0 { return 37 }
slot = Raster.PixmapImageIndex("pictures/shipped.webp")
opens := asset_opens
source = .{0.0, 0.0, 2048.0, 1024.0}
if !Raster.PixmapBeginInto(0, 256, 128, large_surface[:]) { return 45 }
Raster.RasterImage("pictures/shipped.webp", 0, source, .{0.0, 0.0, 256.0, 128.0},
.{0.0, 0.0, 256.0, 128.0}, .{}, 0.0, 0.0, .{255, 255, 255, 255})
Raster.PixmapEndInto()
if Raster.pixmap_image_widths[slot] != 256 || Raster.pixmap_image_heights[slot] != 128 ||
Services.android_picture_bytes != 131072 || asset_opens != opens + 1 || small.count != 131072 { return 38 }
for at: cast(s64)0..small.count / 4 - 1 {
pixel := Raster.pixmap_image_pixels[slot].value[at]
expected_pixel := (cast(u32)small[at * 4 + 3] << 24) | (cast(u32)small[at * 4] << 16) |
(cast(u32)small[at * 4 + 1] << 8) | cast(u32)small[at * 4 + 2]
if pixel != expected_pixel { print("Reduction differs from Pillow at %: % != %\n", at, pixel, expected_pixel); return 39 }
}
revision := Raster.PixmapImageRevision()
for attempt: 0..9 {
source = .{512.0, 256.0, -1024.0, 512.0}
if !Raster.PixmapPrepareImage(slot, *source, 128.0, 64.0) || source.x != 64.0 || source.y != 32.0 ||
source.width != -128.0 || source.height != 64.0 { return 40 }
}
if asset_opens != opens + 1 || Raster.PixmapImageRevision() != revision { return 41 }
source = .{0.0, 0.0, 2048.0, 1024.0}
if !Raster.PixmapPrepareImage(slot, *source, 512.0, 256.0) || Services.android_picture_bytes != 524288 ||
asset_opens != opens + 2 || Raster.ImageWidth("pictures/shipped.webp") != 2048 { return 42 }
source = .{0.0, 0.0, 2048.0, 1024.0}
if !Raster.PixmapPrepareImage(slot, *source, 256.0, 128.0) || asset_opens != opens + 2 { return 43 }
Cleanup.AndroidHostResourcesClose()
if Services.android_picture_bytes != 0 || Raster.PixmapImageIndex("pictures/shipped.webp") >= 0 { return 44 }
Services.ImagePreviewRelease(null)
print("Android image services: native RGBA/orientation, bounded bitmap JNI, owned cache replacement, folder invalidation, lazy APK pictures, in-process textures, missing bridge and cleanup passed\n")
return 0
}