Skip to content

Commit

Permalink
Migrate most samples off ALooper_pollAll.
Browse files Browse the repository at this point in the history
The only one left is the vulkan sample. That one crashed when I did the
trivial thing, so I'm leaving it for last.

There's a fair amount of absurd stuff left around the code I touched.
Some samples have a lot of code dedicated to managing sensors that then
do nothing with the sensors, some display a static image but have a
render "loop", all of these have a nearly identical main loop but don't
share any code, etc. I'll be back.
  • Loading branch information
DanAlbert committed May 3, 2024
1 parent ece52f2 commit 4073788
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 177 deletions.
28 changes: 10 additions & 18 deletions camera/basic/src/main/cpp/android_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,19 @@ extern "C" void android_main(struct android_app* state) {
state->onAppCmd = ProcessAndroidCmd;

// loop waiting for stuff to do.
while (1) {
// Read all pending events.
int events;
struct android_poll_source* source;

while (ALooper_pollAll(0, NULL, &events, (void**)&source) >= 0) {
// Process this event.
if (source != NULL) {
source->process(state, source);
}

// Check if we are exiting.
if (state->destroyRequested != 0) {
LOGI("CameraEngine thread destroy requested!");
engine.DeleteCamera();
pEngineObj = nullptr;
return;
}
while (!state->destroyRequested) {
struct android_poll_source* source = nullptr;
auto result = ALooper_pollOnce(0, NULL, nullptr, (void**)&source);
ASSERT(result != ALOOPER_POLL_ERROR, "ALooper_pollOnce returned an error");
if (source != NULL) {
source->process(state, source);
}
pEngineObj->DrawFrame();
}

LOGI("CameraEngine thread destroy requested!");
engine.DeleteCamera();
pEngineObj = nullptr;
}

/**
Expand Down
42 changes: 18 additions & 24 deletions display-p3/image-view/src/main/cpp/AndroidMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,39 +101,33 @@ void android_main(struct android_app* state) {
engine.sensorEventQueue = ASensorManager_createEventQueue(
engine.sensorManager, state->looper, LOOPER_ID_USER, NULL, NULL);

while (1) {
// Read all pending events.
int ident;
int events;
struct android_poll_source* source;

while (!state->destroyRequested) {
// If not animating_, we will block forever waiting for events.
// If animating_, we loop until all events are read, then continue
// to draw the next frame of animation.
while ((ident = ALooper_pollAll(engine.GetAnimationStatus() ? 0 : -1, NULL,
&events, (void**)&source)) >= 0) {
// Process this event.
if (source != NULL) {
source->process(state, source);
}
// If a sensor has data, process it now.
if (ident == LOOPER_ID_USER) {
if (engine.accelerometerSensor != NULL) {
ASensorEvent event;
while (ASensorEventQueue_getEvents(engine.sensorEventQueue, &event,
1) > 0) {
}
struct android_poll_source* source = nullptr;
int ident = ALooper_pollOnce(engine.GetAnimationStatus() ? 0 : -1, nullptr,
nullptr, (void**)&source);
ASSERT(ident != ALOOPER_POLL_ERROR, "ALooper_pollOnce returned and error");
// Process this event.
if (source != NULL) {
source->process(state, source);
}

// If a sensor has data, process it now.
if (ident == LOOPER_ID_USER) {
if (engine.accelerometerSensor != NULL) {
ASensorEvent event;
while (ASensorEventQueue_getEvents(engine.sensorEventQueue, &event,
1) > 0) {
}
}
// Check if we are exiting.
if (state->destroyRequested != 0) {
engine.TerminateDisplay();
return;
}
}

if (engine.GetAnimationStatus()) {
engine.DrawFrame();
}
}

engine.TerminateDisplay();
}
23 changes: 8 additions & 15 deletions endless-tunnel/app/src/main/cpp/native_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,15 @@ void NativeEngine::GameLoop() {
mApp->onAppCmd = _handle_cmd_proxy;
mApp->onInputEvent = _handle_input_proxy;

while (1) {
int events;
struct android_poll_source *source;

while (!mApp->destroyRequested) {
// If not animating, block until we get an event; if animating, don't block.
while ((ALooper_pollAll(IsAnimating() ? 0 : -1, NULL, &events,
(void **)&source)) >= 0) {
// process event
if (source != NULL) {
source->process(mApp, source);
}

// are we exiting?
if (mApp->destroyRequested) {
return;
}
struct android_poll_source *source = nullptr;
auto result = ALooper_pollOnce(IsAnimating() ? 0 : -1, NULL, nullptr,
(void **)&source);
MY_ASSERT(result != ALOOPER_POLL_ERROR);
// process event
if (source != NULL) {
source->process(mApp, source);
}

if (IsAnimating()) {
Expand Down
32 changes: 14 additions & 18 deletions native-plasma/app/src/main/cpp/plasma.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,32 +453,28 @@ void android_main(struct android_app* state) {

// loop waiting for stuff to do.

while (1) {
// Read all pending events.
int ident;
int events;
struct android_poll_source* source;

while (!state->destroyRequested) {
// If not animating, we will block forever waiting for events.
// If animating, we loop until all events are read, then continue
// to draw the next frame of animation.
while ((ident = ALooper_pollAll(engine.animating ? 0 : -1, NULL, &events,
(void**)&source)) >= 0) {
// Process this event.
if (source != NULL) {
source->process(state, source);
}
struct android_poll_source* source = NULL;
int result = ALooper_pollOnce(engine.animating ? 0 : -1, NULL, NULL,
(void**)&source);
if (result == ALOOPER_POLL_ERROR) {
LOGE("ALooper_pollOnce returned an error");
abort();
}

// Check if we are exiting.
if (state->destroyRequested != 0) {
LOGI("Engine thread destroy requested!");
engine_term_display(&engine);
return;
}
// Process this event.
if (source != NULL) {
source->process(state, source);
}

if (engine.animating) {
engine_draw_frame(&engine);
}
}

LOGI("Engine thread destroy requested!");
engine_term_display(&engine);
}
2 changes: 1 addition & 1 deletion sensor-graph/accelerometer/src/main/cpp/sensorgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class sensorgraph {
}

void update() {
ALooper_pollAll(0, NULL, NULL, NULL);
ALooper_pollOnce(0, NULL, NULL, NULL);
ASensorEvent event;
float a = SENSOR_FILTER_ALPHA;
while (ASensorEventQueue_getEvents(accelerometerEventQueue, &event, 1) >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -588,31 +588,27 @@ void android_main(android_app* state) {
state->onInputEvent = Engine::HandleInput;

// loop waiting for stuff to do.
while (1) {
// Read all pending events.
int id;
int events;
android_poll_source* source;

while (!state->destroyRequested) {
// If not animating, we will block forever waiting for events.
// If animating, we loop until all events are read, then continue
// to draw the next frame of animation.
while ((id = ALooper_pollAll(g_engine.IsReady() ? 0 : -1, NULL, &events,
(void**)&source)) >= 0) {
// Process this event.
if (source != NULL) source->process(state, source);

// Check if we are exiting.
if (state->destroyRequested != 0) {
g_engine.TermDisplay();
return;
}
android_poll_source* source = nullptr;
auto result = ALooper_pollOnce(g_engine.IsReady() ? 0 : -1, nullptr, nullptr,
(void**)&source);
if (result == ALOOPER_POLL_ERROR) {
LOGE("ALooper_pollOnce returned an error");
abort();
}

// Process this event.
if (source != NULL) source->process(state, source);

if (g_engine.IsReady()) {
// Drawing is throttled to the screen update rate, so there
// is no need to do timing here.
g_engine.DrawFrame();
}
}

g_engine.TermDisplay();
}
29 changes: 12 additions & 17 deletions teapots/classic-teapot/src/main/cpp/TeapotNativeActivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,33 +404,28 @@ void android_main(android_app* state) {
g_engine.InitSensors();

// loop waiting for stuff to do.
while (1) {
// Read all pending events.
int id;
int events;
android_poll_source* source;

while (!state->destroyRequested) {
// If not animating, we will block forever waiting for events.
// If animating, we loop until all events are read, then continue
// to draw the next frame of animation.
while ((id = ALooper_pollAll(g_engine.IsReady() ? 0 : -1, NULL, &events,
(void**)&source)) >= 0) {
// Process this event.
if (source != NULL) source->process(state, source);
android_poll_source* source = nullptr;
auto result = ALooper_pollOnce(g_engine.IsReady() ? 0 : -1, nullptr, nullptr,
(void**)&source);
if (result == ALOOPER_POLL_ERROR) {
LOGE("ALooper_pollOnce returned an error");
std::abort();
}

g_engine.ProcessSensors(id);
// Process this event.
if (source != NULL) source->process(state, source);

// Check if we are exiting.
if (state->destroyRequested != 0) {
g_engine.TermDisplay();
return;
}
}
g_engine.ProcessSensors(result);

if (g_engine.IsReady()) {
// Drawing is throttled to the screen update rate, so there
// is no need to do timing here.
g_engine.DrawFrame();
}
}
g_engine.TermDisplay();
}
31 changes: 14 additions & 17 deletions teapots/image-decoder/src/main/cpp/TeapotNativeActivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,33 +404,30 @@ void android_main(android_app* state) {
g_engine.InitSensors();

// loop waiting for stuff to do.
while (1) {
// Read all pending events.
int id;
int events;
android_poll_source* source;

while (!state->destroyRequested) {
// If not animating, we will block forever waiting for events.
// If animating, we loop until all events are read, then continue
// to draw the next frame of animation.
while ((id = ALooper_pollAll(g_engine.IsReady() ? 0 : -1, NULL, &events,
(void**)&source)) >= 0) {
// Process this event.
if (source != NULL) source->process(state, source);

g_engine.ProcessSensors(id);
android_poll_source* source = nullptr;
auto result = ALooper_pollOnce(g_engine.IsReady() ? 0 : -1, nullptr, nullptr,
(void**)&source);

// Check if we are exiting.
if (state->destroyRequested != 0) {
g_engine.TermDisplay();
return;
}
if (result == ALOOPER_POLL_ERROR) {
LOGE("ALooper_pollOnce returned an error");
std::abort();
}

// Process this event.
if (source != NULL) source->process(state, source);

g_engine.ProcessSensors(result);

if (g_engine.IsReady()) {
// Drawing is throttled to the screen update rate, so there
// is no need to do timing here.
g_engine.DrawFrame();
}
}

g_engine.TermDisplay();
}
29 changes: 12 additions & 17 deletions teapots/more-teapots/src/main/cpp/MoreTeapotsNativeActivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,33 +416,28 @@ void android_main(android_app* state) {
g_engine.InitSensors();

// loop waiting for stuff to do.
while (1) {
// Read all pending events.
int id;
int events;
android_poll_source* source;

while (!state->destroyRequested) {
// If not animating, we will block forever waiting for events.
// If animating, we loop until all events are read, then continue
// to draw the next frame of animation.
while ((id = ALooper_pollAll(g_engine.IsReady() ? 0 : -1, NULL, &events,
(void**)&source)) >= 0) {
// Process this event.
if (source != NULL) source->process(state, source);
android_poll_source* source = nullptr;
auto result = ALooper_pollOnce(g_engine.IsReady() ? 0 : -1, nullptr, nullptr,
(void**)&source);
if (result == ALOOPER_POLL_ERROR) {
LOGE("ALooper_pollOnce returned an error");
std::abort();
}

g_engine.ProcessSensors(id);
// Process this event.
if (source != NULL) source->process(state, source);

// Check if we are exiting.
if (state->destroyRequested != 0) {
g_engine.TermDisplay();
return;
}
}
g_engine.ProcessSensors(result);

if (g_engine.IsReady()) {
// Drawing is throttled to the screen update rate, so there
// is no need to do timing here.
g_engine.DrawFrame();
}
}
g_engine.TermDisplay();
}

0 comments on commit 4073788

Please sign in to comment.