Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return compilation to native support #964

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
62874e3
Changes needed to be able to compile native-plasma with wasm_ndk
dimitry- Oct 24, 2023
9637dd9
Make a copy of native-plasma for wasm build
dimitry- Oct 24, 2023
8a84963
Add wasm2c generated files (and copy wasm-rt headers)
dimitry- Oct 24, 2023
7f9567d
Modify CMakeList.txt to use wasm2c generated files
dimitry- Oct 24, 2023
5e5ce1c
Merge pull request #2 from AndroidWasm/wasm-plasma
matthias-blume Oct 24, 2023
1e12923
Show the aggregated frame statistics as an overlay.
Oct 24, 2023
018a482
Merge pull request #3 from negge/main
matthias-blume Oct 25, 2023
15d8665
Add FONT_SCALE to overlay rendering for HiDPI displays.
negge Oct 25, 2023
59bb3b4
Add STATS_OFFX and STATS_OFFY to adjust overlay position.
negge Oct 25, 2023
551ba2e
Add STATS_OFFX and STATS_OFFY to adjust overlay position.
negge Oct 25, 2023
290fba1
Various fixups.
matthias-blume Oct 25, 2023
aefe98b
Missing files.
matthias-blume Oct 25, 2023
f2687c6
Merge pull request #4 from negge/main
matthias-blume Oct 25, 2023
7bf7d02
Merge branch 'main' into scale
matthias-blume Oct 25, 2023
7c5fbbb
Revert some unneeded changes.
matthias-blume Oct 25, 2023
89f93e4
Merge pull request #5 from AndroidWasm/stats-and-opts
matthias-blume Oct 25, 2023
317c4fa
Implement on-screen stats.
matthias-blume Nov 2, 2023
215ef73
Merge pull request #6 from AndroidWasm/with-stats
matthias-blume Nov 2, 2023
8aad675
Support compile to native again
dimitry- Nov 7, 2023
09e09e8
Use cloned android_native_app_glue.c for wasm
dimitry- Nov 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions native-plasma/app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,37 @@

cmake_minimum_required(VERSION 3.4.1)


if (ANDROID_WASM)

add_compile_options(-O3)
add_compile_options(-fno-unroll-loops)

# For wasm-ld only
# Export ANativeActivity_onCreate(),

add_link_options(-Wl,--export=ANativeActivity_onCreate)
add_link_options(-Wl,--strip-debug)

# build native_app_glue as a static lib
# Note that this is a cloned version with logging disabled.
add_library(native_app_glue STATIC android_native_app_glue.c)

# now build app's shared lib
add_executable(native-plasma
plasma.c
${WASM_LIBRARY_PATH}/standalone-printf.c)


target_include_directories(native-plasma PRIVATE
${ANDROID_NDK}/sources/android/native_app_glue)

# add lib dependencies
target_link_libraries(native-plasma
native_app_glue
)

else()

# build native_app_glue as a static lib
add_library(native_app_glue STATIC
${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)
Expand All @@ -38,3 +69,5 @@ target_link_libraries(native-plasma
native_app_glue
log
m)

endif()
135 changes: 135 additions & 0 deletions native-plasma/app/src/main/cpp/font.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#ifndef _FONT_H
#define _FONT_H

const unsigned char FONT_8x8[128][8] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7e, 0x81, 0xa5, 0x81, 0xbd, 0x99, 0x81, 0x7e,
0x7e, 0xff, 0xdb, 0xff, 0xc3, 0xe7, 0xff, 0x7e,
0x6c, 0xfe, 0xfe, 0xfe, 0x7c, 0x38, 0x10, 0x00,
0x10, 0x38, 0x7c, 0xfe, 0x7c, 0x38, 0x10, 0x00,
0x38, 0x7c, 0x38, 0xfe, 0xfe, 0x7c, 0x38, 0x7c,
0x10, 0x10, 0x38, 0x7c, 0xfe, 0x7c, 0x38, 0x7c,
0x00, 0x00, 0x18, 0x3c, 0x3c, 0x18, 0x00, 0x00,
0xff, 0xff, 0xe7, 0xc3, 0xc3, 0xe7, 0xff, 0xff,
0x00, 0x3c, 0x66, 0x42, 0x42, 0x66, 0x3c, 0x00,
0xff, 0xc3, 0x99, 0xbd, 0xbd, 0x99, 0xc3, 0xff,
0x0f, 0x07, 0x0f, 0x7d, 0xcc, 0xcc, 0xcc, 0x78,
0x3c, 0x66, 0x66, 0x66, 0x3c, 0x18, 0x7e, 0x18,
0x3f, 0x33, 0x3f, 0x30, 0x30, 0x70, 0xf0, 0xe0,
0x7f, 0x63, 0x7f, 0x63, 0x63, 0x67, 0xe6, 0xc0,
0x99, 0x5a, 0x3c, 0xe7, 0xe7, 0x3c, 0x5a, 0x99,
0x80, 0xe0, 0xf8, 0xfe, 0xf8, 0xe0, 0x80, 0x00,
0x02, 0x0e, 0x3e, 0xfe, 0x3e, 0x0e, 0x02, 0x00,
0x18, 0x3c, 0x7e, 0x18, 0x18, 0x7e, 0x3c, 0x18,
0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x66, 0x00,
0x7f, 0xdb, 0xdb, 0x7b, 0x1b, 0x1b, 0x1b, 0x00,
0x3e, 0x63, 0x38, 0x6c, 0x6c, 0x38, 0xcc, 0x78,
0x00, 0x00, 0x00, 0x00, 0x7e, 0x7e, 0x7e, 0x00,
0x18, 0x3c, 0x7e, 0x18, 0x7e, 0x3c, 0x18, 0xff,
0x18, 0x3c, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x00,
0x18, 0x18, 0x18, 0x18, 0x7e, 0x3c, 0x18, 0x00,
0x00, 0x18, 0x0c, 0xfe, 0x0c, 0x18, 0x00, 0x00,
0x00, 0x30, 0x60, 0xfe, 0x60, 0x30, 0x00, 0x00,
0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xfe, 0x00, 0x00,
0x00, 0x24, 0x66, 0xff, 0x66, 0x24, 0x00, 0x00,
0x00, 0x18, 0x3c, 0x7e, 0xff, 0xff, 0x00, 0x00,
0x00, 0xff, 0xff, 0x7e, 0x3c, 0x18, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x30, 0x78, 0x78, 0x30, 0x30, 0x00, 0x30, 0x00,
0x6c, 0x6c, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00,
0x6c, 0x6c, 0xfe, 0x6c, 0xfe, 0x6c, 0x6c, 0x00,
0x30, 0x7c, 0xc0, 0x78, 0x0c, 0xf8, 0x30, 0x00,
0x00, 0xc6, 0xcc, 0x18, 0x30, 0x66, 0xc6, 0x00,
0x38, 0x6c, 0x38, 0x76, 0xdc, 0xcc, 0x76, 0x00,
0x60, 0x60, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x18, 0x30, 0x60, 0x60, 0x60, 0x30, 0x18, 0x00,
0x60, 0x30, 0x18, 0x18, 0x18, 0x30, 0x60, 0x00,
0x00, 0x66, 0x3c, 0xff, 0x3c, 0x66, 0x00, 0x00,
0x00, 0x30, 0x30, 0xfc, 0x30, 0x30, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x60,
0x00, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00,
0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0, 0x80, 0x00,
0x7c, 0xc6, 0xce, 0xde, 0xf6, 0xe6, 0x7c, 0x00,
0x30, 0x70, 0x30, 0x30, 0x30, 0x30, 0xfc, 0x00,
0x78, 0xcc, 0x0c, 0x38, 0x60, 0xcc, 0xfc, 0x00,
0x78, 0xcc, 0x0c, 0x38, 0x0c, 0xcc, 0x78, 0x00,
0x1c, 0x3c, 0x6c, 0xcc, 0xfe, 0x0c, 0x1e, 0x00,
0xfc, 0xc0, 0xf8, 0x0c, 0x0c, 0xcc, 0x78, 0x00,
0x38, 0x60, 0xc0, 0xf8, 0xcc, 0xcc, 0x78, 0x00,
0xfc, 0xcc, 0x0c, 0x18, 0x30, 0x30, 0x30, 0x00,
0x78, 0xcc, 0xcc, 0x78, 0xcc, 0xcc, 0x78, 0x00,
0x78, 0xcc, 0xcc, 0x7c, 0x0c, 0x18, 0x70, 0x00,
0x00, 0x30, 0x30, 0x00, 0x00, 0x30, 0x30, 0x00,
0x00, 0x30, 0x30, 0x00, 0x00, 0x30, 0x30, 0x60,
0x18, 0x30, 0x60, 0xc0, 0x60, 0x30, 0x18, 0x00,
0x00, 0x00, 0xfc, 0x00, 0x00, 0xfc, 0x00, 0x00,
0x60, 0x30, 0x18, 0x0c, 0x18, 0x30, 0x60, 0x00,
0x78, 0xcc, 0x0c, 0x18, 0x30, 0x00, 0x30, 0x00,
0x7c, 0xc6, 0xde, 0xde, 0xde, 0xc0, 0x78, 0x00,
0x30, 0x78, 0xcc, 0xcc, 0xfc, 0xcc, 0xcc, 0x00,
0xfc, 0x66, 0x66, 0x7c, 0x66, 0x66, 0xfc, 0x00,
0x3c, 0x66, 0xc0, 0xc0, 0xc0, 0x66, 0x3c, 0x00,
0xf8, 0x6c, 0x66, 0x66, 0x66, 0x6c, 0xf8, 0x00,
0xfe, 0x62, 0x68, 0x78, 0x68, 0x62, 0xfe, 0x00,
0xfe, 0x62, 0x68, 0x78, 0x68, 0x60, 0xf0, 0x00,
0x3c, 0x66, 0xc0, 0xc0, 0xce, 0x66, 0x3e, 0x00,
0xcc, 0xcc, 0xcc, 0xfc, 0xcc, 0xcc, 0xcc, 0x00,
0x78, 0x30, 0x30, 0x30, 0x30, 0x30, 0x78, 0x00,
0x1e, 0x0c, 0x0c, 0x0c, 0xcc, 0xcc, 0x78, 0x00,
0xe6, 0x66, 0x6c, 0x78, 0x6c, 0x66, 0xe6, 0x00,
0xf0, 0x60, 0x60, 0x60, 0x62, 0x66, 0xfe, 0x00,
0xc6, 0xee, 0xfe, 0xfe, 0xd6, 0xc6, 0xc6, 0x00,
0xc6, 0xe6, 0xf6, 0xde, 0xce, 0xc6, 0xc6, 0x00,
0x38, 0x6c, 0xc6, 0xc6, 0xc6, 0x6c, 0x38, 0x00,
0xfc, 0x66, 0x66, 0x7c, 0x60, 0x60, 0xf0, 0x00,
0x78, 0xcc, 0xcc, 0xcc, 0xdc, 0x78, 0x1c, 0x00,
0xfc, 0x66, 0x66, 0x7c, 0x6c, 0x66, 0xe6, 0x00,
0x78, 0xcc, 0xe0, 0x70, 0x1c, 0xcc, 0x78, 0x00,
0xfc, 0xb4, 0x30, 0x30, 0x30, 0x30, 0x78, 0x00,
0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xfc, 0x00,
0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0x78, 0x30, 0x00,
0xc6, 0xc6, 0xc6, 0xd6, 0xfe, 0xee, 0xc6, 0x00,
0xc6, 0xc6, 0x6c, 0x38, 0x38, 0x6c, 0xc6, 0x00,
0xcc, 0xcc, 0xcc, 0x78, 0x30, 0x30, 0x78, 0x00,
0xfe, 0xc6, 0x8c, 0x18, 0x32, 0x66, 0xfe, 0x00,
0x78, 0x60, 0x60, 0x60, 0x60, 0x60, 0x78, 0x00,
0xc0, 0x60, 0x30, 0x18, 0x0c, 0x06, 0x02, 0x00,
0x78, 0x18, 0x18, 0x18, 0x18, 0x18, 0x78, 0x00,
0x10, 0x38, 0x6c, 0xc6, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
0x30, 0x30, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x78, 0x0c, 0x7c, 0xcc, 0x76, 0x00,
0xe0, 0x60, 0x60, 0x7c, 0x66, 0x66, 0xdc, 0x00,
0x00, 0x00, 0x78, 0xcc, 0xc0, 0xcc, 0x78, 0x00,
0x1c, 0x0c, 0x0c, 0x7c, 0xcc, 0xcc, 0x76, 0x00,
0x00, 0x00, 0x78, 0xcc, 0xfc, 0xc0, 0x78, 0x00,
0x38, 0x6c, 0x60, 0xf0, 0x60, 0x60, 0xf0, 0x00,
0x00, 0x00, 0x76, 0xcc, 0xcc, 0x7c, 0x0c, 0xf8,
0xe0, 0x60, 0x6c, 0x76, 0x66, 0x66, 0xe6, 0x00,
0x30, 0x00, 0x70, 0x30, 0x30, 0x30, 0x78, 0x00,
0x0c, 0x00, 0x0c, 0x0c, 0x0c, 0xcc, 0xcc, 0x78,
0xe0, 0x60, 0x66, 0x6c, 0x78, 0x6c, 0xe6, 0x00,
0x70, 0x30, 0x30, 0x30, 0x30, 0x30, 0x78, 0x00,
0x00, 0x00, 0xcc, 0xfe, 0xfe, 0xd6, 0xc6, 0x00,
0x00, 0x00, 0xf8, 0xcc, 0xcc, 0xcc, 0xcc, 0x00,
0x00, 0x00, 0x78, 0xcc, 0xcc, 0xcc, 0x78, 0x00,
0x00, 0x00, 0xdc, 0x66, 0x66, 0x7c, 0x60, 0xf0,
0x00, 0x00, 0x76, 0xcc, 0xcc, 0x7c, 0x0c, 0x1e,
0x00, 0x00, 0xdc, 0x76, 0x66, 0x60, 0xf0, 0x00,
0x00, 0x00, 0x7c, 0xc0, 0x78, 0x0c, 0xf8, 0x00,
0x10, 0x30, 0x7c, 0x30, 0x30, 0x34, 0x18, 0x00,
0x00, 0x00, 0xcc, 0xcc, 0xcc, 0xcc, 0x76, 0x00,
0x00, 0x00, 0xcc, 0xcc, 0xcc, 0x78, 0x30, 0x00,
0x00, 0x00, 0xc6, 0xd6, 0xfe, 0xfe, 0x6c, 0x00,
0x00, 0x00, 0xc6, 0x6c, 0x38, 0x6c, 0xc6, 0x00,
0x00, 0x00, 0xcc, 0xcc, 0xcc, 0x7c, 0x0c, 0xf8,
0x00, 0x00, 0xfc, 0x98, 0x30, 0x64, 0xfc, 0x00,
0x1c, 0x30, 0x30, 0xe0, 0x30, 0x30, 0x1c, 0x00,
0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00,
0xe0, 0x30, 0x30, 0x1c, 0x30, 0x30, 0xe0, 0x00,
0x76, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x10, 0x38, 0x6c, 0xc6, 0xc6, 0xfe, 0x00
};

#endif
71 changes: 68 additions & 3 deletions native-plasma/app/src/main/cpp/plasma.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@
#include <time.h>

#define LOG_TAG "libplasma"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#define LOGI(...)
#define LOGW(...)
#define LOGE(...)

/* Set to 1 to enable debug log traces. */
#define DEBUG 0

/* Set to 1 to optimize memory stores when generating plasma. */
#define OPTIMIZE_WRITES 1

/* Set to 1 to display statistics over the rendered plasma */
#define SHOW_STATS (1)

/* Return current time in milliseconds */
static double now_ms(void) {
struct timeval tv;
Expand Down Expand Up @@ -176,6 +179,55 @@ static void init_tables(void) {
init_angles();
}

#if SHOW_STATS
#include "font.h"

typedef struct {
double minRender, maxRender, avgRender;
double minFrame, maxFrame, avgFrame;
} DisplayStats;

/* negge: apologies for the global static struct */
static DisplayStats ds = { 0 };

#define FONT_SCALE (2)

static void bitmap_print(uint16_t *pixels, int row, int stride, uint16_t color,
const unsigned char *str, const unsigned char font_8x8[][8]) {
pixels += 8*FONT_SCALE*row*stride;
while (*str) {
int i, j, u, v;
for (j = 0; j < 8; j++) {
for (v = 0; v < FONT_SCALE; v++, pixels += stride) {
for (i = 0; i < 8; i++) {
for (u = 0; u < FONT_SCALE; u++) {
if (font_8x8[*str][j] & (1 << (7 - i))) {
pixels[i*FONT_SCALE + u] = color;
}
}
}
}
}
pixels += 8*FONT_SCALE*(1 - stride);
str++;
}
}

#define STATS_OFFX (0)
#define STATS_OFFY (150)

static void show_stats(uint16_t *pixels, int stride) {
char buf[200];
pixels += STATS_OFFY*stride + STATS_OFFX;
sprintf(buf, "frame/s (avg,min,max) = (%.1f,%.1f,%.1f)",
ds.avgFrame, ds.minFrame, ds.maxFrame);
bitmap_print(pixels, 0, stride, 0xffff, buf, FONT_8x8);
sprintf(buf, "render time ms (avg,min,max) = (%.2f,%.2f,%.2f)",
ds.avgRender, ds.minRender, ds.maxRender);
bitmap_print(pixels, 1, stride, 0xffff, buf, FONT_8x8);
}
#endif

static void fill_plasma(ANativeWindow_Buffer* buffer, double t) {
Fixed yt1 = FIXED_FROM_FLOAT(t / 1230.);
Fixed yt2 = yt1;
Expand Down Expand Up @@ -257,6 +309,10 @@ static void fill_plasma(ANativeWindow_Buffer* buffer, double t) {
// go to next line
pixels = (uint16_t*)pixels + buffer->stride;
}

#if SHOW_STATS
show_stats((uint16_t *)buffer->bits, buffer->stride);
#endif
}

/* simple stats management */
Expand Down Expand Up @@ -322,6 +378,15 @@ static void stats_endFrame(Stats* s) {
"render time ms (avg,min,max) = (%.1f,%.1f,%.1f)\n",
1000. / avgFrame, 1000. / maxFrame, 1000. / minFrame, avgRender,
minRender, maxRender);

#if SHOW_STATS
ds.minRender = minRender;
ds.maxRender = maxRender;
ds.avgRender = avgRender;
ds.minFrame = 1000. / minFrame;
ds.maxFrame = 1000. / maxFrame;
ds.avgFrame = 1000. / avgFrame;
#endif
}
s->numFrames = 0;
s->firstFrame = 0;
Expand Down
7 changes: 7 additions & 0 deletions wasm-native-plasma/.google/packaging.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
status: PUBLISHED
technologies: [Android, NDK]
categories: [NDK]
languages: [C++]
solutions: [Mobile]
github: googlesamples/android-ndk
license: apache2
60 changes: 60 additions & 0 deletions wasm-native-plasma/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Native Plasma

Native Plasma is an Android sample that renders a plasma effect in a Bitmap from
C code using
[Native Activity](http://developer.android.com/reference/android/app/NativeActivity.html).

This sample uses the new
[Android Studio CMake plugin](http://tools.android.com/tech-docs/external-c-builds)
with C++ support.

## Pre-requisites

- Android Studio 2.2+ with [NDK](https://developer.android.com/ndk/) bundle.

## Getting Started

1. [Download Android Studio](http://developer.android.com/sdk/index.html)
1. Launch Android Studio.
1. Open the sample directory.
1. Open *File/Project Structure...*

- Click *Download* or *Select NDK location*.

1. Click *Tools/Android/Sync Project with Gradle Files*.
1. Click *Run/Run 'app'*.

## Screenshots

![screenshot](screenshot.png)

## Support

If you've found an error in these samples, please
[file an issue](https://github.com/googlesamples/android-ndk/issues/new).

Patches are encouraged, and may be submitted by
[forking this project](https://github.com/googlesamples/android-ndk/fork) and
submitting a pull request through GitHub. Please see
[CONTRIBUTING.md](../CONTRIBUTING.md) for more details.

- [Stack Overflow](http://stackoverflow.com/questions/tagged/android-ndk)
- [Android Tools Feedbacks](http://tools.android.com/feedback)

## License

Copyright 2015 Google, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor
license agreements. See the NOTICE file distributed with this work for
additional information regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
27 changes: 27 additions & 0 deletions wasm-native-plasma/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
plugins {
id 'com.android.application'
}

android {
compileSdk 33
ndkVersion '25.1.8937393'

defaultConfig {
applicationId 'com.example.wasm_native_plasma'
minSdkVersion 14
targetSdkVersion 33
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path 'src/main/cpp/CMakeLists.txt'
}
}
namespace 'com.example.wasm_native_plasma'
}
Loading
Loading