Files
kryon/tests/android_surface_behavior.zi
T
waozi 4eaf06b24a Draw Android apps inside their window when the surface is larger
Android gives a raised window, such as the floating picture-in-picture
window, a surface larger than its frame by the shadow on every side, and
keeps that margin after the window opens back to full screen. Kryon drew
from the surface's corner, so the app sat 18 pixels too high and too low
and lost 18 pixels at each side on a 1.75x phone. An activity now reports
its frame size; the viewport lies in the frame's part of the surface and
raylib's mouse offset moves touches there too.
2026-10-11 20:04:07 -03:00

206 lines
7.1 KiB
Ziran

#import, file "../src/backend/android_surface.zi";
#import, file "android_viewport_policy.zi";
c :: #system_library "c";
TestExit :: (status: s32) #foreign c "exit";
core: RaylibWindowState;
native_width: s32 = 1080;
native_height: s32 = 1920;
geometry_result: s32;
viewport_width: s32;
viewport_height: s32;
projection_width: float64;
projection_height: float64;
Require :: (condition: bool, code: s32) {
if !condition {
TestExit(code)
}
}
#program_export
GetScreenWidth :: () -> s32 {
return cast(s32)core.screen.width
}
#program_export
GetScreenHeight :: () -> s32 {
return cast(s32)core.screen.height
}
#program_export
GetWindowHandle :: () -> *void {
return cast(*void)*core
}
#program_export
dlsym :: (library: *void, symbol: *u8) -> *void {
unused library
Require(symbol[0] == #char "C" && symbol[1] == #char "O" &&
symbol[2] == #char "R" && symbol[3] == #char "E" && symbol[4] == 0, 10)
return cast(*void)*core
}
#program_export
ANativeWindow_getFormat :: (window: *void) -> s32 {
unused window
return 1
}
#program_export
ANativeWindow_setBuffersGeometry :: (window: *void, width: s32,
height: s32, format: s32) -> s32 {
unused window
Require(width == 0 && height == 0 && format == 1, 11)
return geometry_result
}
#program_export
ANativeWindow_getWidth :: (window: *void) -> s32 {
unused window
return native_width
}
#program_export
ANativeWindow_getHeight :: (window: *void) -> s32 {
unused window
return native_height
}
#program_export
rlViewport :: (x: s32, y: s32, width: s32, height: s32) {
Require(x == 0 && y == 0, 12)
viewport_width = width
viewport_height = height
}
#program_export
rlMatrixMode :: (mode: s32) {
Require(mode == 0x1701 || mode == 0x1700, 13)
}
#program_export
rlLoadIdentity :: () {
}
#program_export
rlOrtho :: (left: float64, right: float64, bottom: float64,
top: float64, near: float64, far: float64) {
Require(left == 0.0 && top == 0.0 && near == 0.0 && far == 1.0, 14)
projection_width = right
projection_height = bottom
}
#program_export
rlGetMatrixProjection :: () -> SurfaceMatrix {
matrix: SurfaceMatrix
matrix.values[0] = cast(float32)(2.0 / projection_width)
matrix.values[5] = cast(float32)(-2.0 / projection_height)
return matrix
}
mouse_offset_x: s32 = -1;
mouse_offset_y: s32 = -1;
#program_export
SetMouseOffset :: (x: s32, y: s32) {
mouse_offset_x = x
mouse_offset_y = y
}
clear_color: u32;
fill_count: s32;
fill_bounds: [4]s32;
fill_color: u32;
PackedColor :: (color: SurfaceColor) -> u32 {
return (cast(u32)color.r << 24) | (cast(u32)color.g << 16) |
(cast(u32)color.b << 8) | cast(u32)color.a
}
#program_export
ClearBackground :: (color: SurfaceColor) {
clear_color = PackedColor(color)
}
#program_export
DrawRectangle :: (x: s32, y: s32, width: s32, height: s32,
color: SurfaceColor) {
fill_count += 1
fill_bounds[0] = x
fill_bounds[1] = y
fill_bounds[2] = width
fill_bounds[3] = height
fill_color = PackedColor(color)
}
#program_export
main :: () -> s32 {
Require(PolicyCheck() == 42, 20)
core.screen.width = 320
core.screen.height = 560
core.render_offset.x = 120
core.render_offset.y = 240
core.screen_scale[0] = 0.5
core.screen_scale[5] = 0.5
core.screen_scale[10] = 1.0
core.screen_scale[15] = 1.0
width: s32
height: s32
Require(GetAndroidSurfaceSize(*width, *height) == 0 &&
width == 320 && height == 560, 21)
insets: AndroidWindowInsets
Require(GetAndroidWindowInsets(*insets) == 0, 22)
SetAndroidWindowInsets(-1, 53, 84, 24, 120, 4, 60, 80, 8)
Require(GetAndroidWindowInsets(*insets) == 1 && insets.system_left == 0, 23)
viewport: AndroidViewport
Require(SyncAndroidViewport(*viewport) == 1, 24)
Require(viewport.x == 4 && viewport.y == 60 && viewport.width == 992 &&
viewport.height == 1836, 25)
Require(core.screen.width == 1080 && core.screen.height == 1920 &&
core.render.width == 1080 && core.render.height == 1920 &&
core.current_fbo.width == 1080 && core.resized_last_frame, 26)
Require(viewport_width == 1080 && viewport_height == 1920 &&
projection_width == 1080.0 && projection_height == 1920.0, 27)
Require(GetAndroidSurfaceSize(*width, *height) == 1 && width == 1080 && height == 1920, 28)
Require(core.render_offset.x == 0 && core.render_offset.y == 0 &&
core.screen_scale[0] == 1.0 && core.screen_scale[5] == 1.0 &&
core.screen_scale[10] == 1.0 && core.screen_scale[15] == 1.0, 35)
transform: AndroidSurfaceTransform
Require(ReadAndroidSurfaceTransform(*transform) &&
transform.screen_width == 1080 && transform.screen_height == 1920 &&
transform.render_width == 1080 && transform.render_height == 1920 &&
transform.offset_x == 0 && transform.offset_y == 0 &&
transform.scale_x == 1.0 && transform.scale_y == 1.0 &&
transform.projection_x * 1080.0 > 1.99 &&
transform.projection_x * 1080.0 < 2.01 &&
transform.projection_y * 1920.0 > -2.01 &&
transform.projection_y * 1920.0 < -1.99, 36)
// The bar strips are black and the viewport takes the canvas color.
ClearAndroidFrame(viewport, cast(u32)0xE2EEFCFF)
Require(clear_color == cast(u32)0x000000FF && fill_count == 1 &&
fill_bounds[0] == 4 && fill_bounds[1] == 60 && fill_bounds[2] == 992 &&
fill_bounds[3] == 1836 && fill_color == cast(u32)0xE2EEFCFF, 33)
full := AndroidViewport.{0, 0, 1080, 1920, .{0, 0, 0, 0}, 1}
ClearAndroidFrame(full, cast(u32)0x182838FF)
Require(clear_color == cast(u32)0x182838FF && fill_count == 1, 34)
SyncAndroidViewport(*viewport)
Require(!core.resized_last_frame, 29)
core.using_fbo = true
core.current_fbo.width = 640
core.current_fbo.height = 480
native_width = 1920
native_height = 1080
SyncAndroidViewport(*viewport)
Require(core.resized_last_frame && core.screen.width == 1920 &&
core.current_fbo.width == 640 && core.current_fbo.height == 480, 30)
SetAndroidViewportPolicy(AndroidViewportPolicyFull())
SyncAndroidViewport(*viewport)
Require(viewport.width == 1920 && viewport.height == 1080 && viewport.x == 0, 31)
geometry_result = -1
native_width = 700
Require(SyncAndroidSurfaceSize(*width, *height) == 0 && width == 1920, 32)
// A window back from the floating window keeps an 18-pixel shadow around
// its frame: the frame is drawn and touched inside the larger surface.
geometry_result = 0
native_width = 1116
native_height = 1956
SetAndroidViewportPolicy(AndroidViewportPolicySafeArea())
SetAndroidWindowSize(1080, 1920)
Require(SyncAndroidViewport(*viewport) == 1, 37)
Require(core.screen.width == 1116 && core.screen.height == 1956 &&
viewport.x == 22 && viewport.y == 78 && viewport.width == 992 &&
viewport.height == 1836 && mouse_offset_x == 18 && mouse_offset_y == 18, 38)
SetAndroidWindowSize(1116, 1956)
SyncAndroidViewport(*viewport)
Require(viewport.x == 4 && viewport.y == 60 && mouse_offset_x == 0 &&
mouse_offset_y == 0, 39)
return 0
}