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

Migrate samples off ALooper_pollAll. #1009

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
22 changes: 13 additions & 9 deletions hello-vulkan/app/src/main/cpp/vk_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,23 @@ void android_main(struct android_app *state) {
android_app_set_key_event_filter(state, VulkanKeyEventFilter);
android_app_set_motion_event_filter(state, VulkanMotionEventFilter);

while (true) {
int ident;
int events;
while (!state->destroyRequested) {
android_poll_source *source;
while ((ident = ALooper_pollAll(engine.canRender ? 0 : -1, nullptr, &events,
(void **)&source)) >= 0) {
if (source != nullptr) {
source->process(state, source);
}
auto result = ALooper_pollOnce(engine.canRender ? 0 : -1, nullptr, nullptr,
(void **)&source);
if (result == ALOOPER_POLL_ERROR) {
LOGE("ALooper_pollOnce returned an error");
std::abort();
}

if (source != nullptr) {
source->process(state, source);
}

HandleInputEvents(state);

engine.app_backend->render();
if (engine.canRender) {
engine.app_backend->render();
}
}
}
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();
}
Loading
Loading