Skip to content

Commit 0c70f94

Browse files
committed
Update na-mainloop
1 parent 744f872 commit 0c70f94

File tree

7 files changed

+40
-11
lines changed

7 files changed

+40
-11
lines changed

examples/na-mainloop/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ edition = "2021"
88
[dependencies]
99
log = "0.4"
1010
android_logger = "0.11.0"
11-
android-activity = { path="../../android-activity", features = [ "native-activity" ] }
11+
android-activity = { version = "0.4", features = [ "native-activity" ] }
12+
#android-activity = { path = "../../android-activity/android-activity", features = [ "native-activity" ] }
13+
ndk-sys = "0.4"
14+
ndk = "0.7"
1215

1316
[lib]
1417
#name="na_mainloop"

examples/na-mainloop/app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ plugins {
33
}
44

55
android {
6+
ndkVersion "25.2.9519653"
67
compileSdk 31
78

89
defaultConfig {
@@ -32,6 +33,7 @@ android {
3233
sourceCompatibility JavaVersion.VERSION_1_8
3334
targetCompatibility JavaVersion.VERSION_1_8
3435
}
36+
namespace 'co.realfit.namainloop'
3537
}
3638

3739
dependencies {

examples/na-mainloop/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="co.realfit.namainloop">
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
43

54
<application
65
android:allowBackup="true"

examples/na-mainloop/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22
plugins {
3-
id 'com.android.application' version '7.1.2' apply false
4-
id 'com.android.library' version '7.1.2' apply false
3+
id 'com.android.application' version '8.0.0' apply false
4+
id 'com.android.library' version '8.0.0' apply false
55
}
66

77
task clean(type: Delete) {

examples/na-mainloop/gradle.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ android.useAndroidX=true
1818
# Enables namespacing of each library's R class so that its R class includes only the
1919
# resources declared in the library itself and none from the library's dependencies,
2020
# thereby reducing the size of the R class for that library
21-
android.nonTransitiveRClass=true
21+
android.nonTransitiveRClass=true
22+
android.defaults.buildfeatures.buildconfig=true
23+
android.nonFinalResIds=false
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Mon May 02 15:39:12 BST 2022
22
distributionBase=GRADLE_USER_HOME
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
44
distributionPath=wrapper/dists
55
zipStorePath=wrapper/dists
66
zipStoreBase=GRADLE_USER_HOME

examples/na-mainloop/src/lib.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn android_main(app: AndroidApp) {
77

88
let mut quit = false;
99
let mut redraw_pending = true;
10-
let mut render_state: Option<()> = Default::default();
10+
let mut native_window: Option<ndk::native_window::NativeWindow> = None;
1111

1212
while !quit {
1313
app.poll_events(
@@ -37,11 +37,11 @@ fn android_main(app: AndroidApp) {
3737
}
3838
}
3939
MainEvent::InitWindow { .. } => {
40-
render_state = Some(());
40+
native_window = app.native_window();
4141
redraw_pending = true;
4242
}
4343
MainEvent::TerminateWindow { .. } => {
44-
render_state = None;
44+
native_window = None;
4545
}
4646
MainEvent::WindowResized { .. } => {
4747
redraw_pending = true;
@@ -65,7 +65,7 @@ fn android_main(app: AndroidApp) {
6565
}
6666

6767
if redraw_pending {
68-
if let Some(_rs) = render_state {
68+
if let Some(native_window) = &native_window {
6969
redraw_pending = false;
7070

7171
// Handle input
@@ -75,9 +75,32 @@ fn android_main(app: AndroidApp) {
7575
});
7676

7777
info!("Render...");
78+
dummy_render(native_window);
7879
}
7980
}
8081
},
8182
);
8283
}
8384
}
85+
86+
/// Post a NOP frame to the window
87+
///
88+
/// Since this is a bare minimum test app we don't depend
89+
/// on any GPU graphics APIs but we do need to at least
90+
/// convince Android that we're drawing something and are
91+
/// responsive, otherwise it will stop delivering input
92+
/// events to us.
93+
fn dummy_render(native_window: &ndk::native_window::NativeWindow) {
94+
unsafe {
95+
let mut buf: ndk_sys::ANativeWindow_Buffer = std::mem::zeroed();
96+
let mut rect: ndk_sys::ARect = std::mem::zeroed();
97+
ndk_sys::ANativeWindow_lock(
98+
native_window.ptr().as_ptr() as _,
99+
&mut buf as _,
100+
&mut rect as _,
101+
);
102+
// Note: we don't try and touch the buffer since that
103+
// also requires us to handle various buffer formats
104+
ndk_sys::ANativeWindow_unlockAndPost(native_window.ptr().as_ptr() as _);
105+
}
106+
}

0 commit comments

Comments
 (0)