Skip to content

Commit 399cf20

Browse files
committed
stuff
1 parent 6da5f7a commit 399cf20

7 files changed

Lines changed: 86 additions & 72 deletions

File tree

src/ruisapp/glue/android/application.cxx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@ application_glue::application_glue(utki::version_duplet gl_version) :
77
gl_version(std::move(gl_version))
88
{}
99

10+
app_window& application_glue::make_window(ruisapp::window_parameters window_params){
11+
utki::assert(!this->window.has_value(), SL);
12+
13+
// TODO:
14+
15+
utki::assert(this->window.has_value(), SL);
16+
return this->window.value();
17+
}
18+
19+
void application_glue::render(){
20+
if(this->window.has_value()){
21+
this->window.value().render();
22+
}
23+
}
24+
1025
std::unique_ptr<papki::file> ruisapp::application::get_res_file(std::string_view path) const
1126
{
1227
utki::assert(globals_wrapper::native_activity, SL);
@@ -17,3 +32,8 @@ std::unique_ptr<papki::file> ruisapp::application::get_res_file(std::string_view
1732
path
1833
);
1934
}
35+
36+
ruisapp::window& ruisapp::application::make_window(ruisapp::window_parameters window_params){
37+
auto& glue = get_glue(*this);
38+
return glue.make_window(std::move(window_params));
39+
}

src/ruisapp/glue/android/application.hxx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ public:
2525
utki::shared_ref<ruis::updater> updater = utki::make_shared<ruis::updater>();
2626

2727
application_glue(utki::version_duplet gl_version);
28+
29+
app_window& make_window(ruisapp::window_parameters window_params);
30+
31+
void render();
32+
33+
void create_window_surface(ANativeWindow& android_window){
34+
if(this->window.has_value()){
35+
this->window.create_surface(android_window);
36+
}
37+
}
38+
39+
void destroy_window_surface(){
40+
if(this->window.has_value()){
41+
this->window.value().destroy_surface();
42+
}
43+
}
2844
};
2945
} // namespace
3046

src/ruisapp/glue/android/globals.cxx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ int on_update_timer_expired(
7979
}
8080
} // namespace
8181

82-
globals_wrapper()
82+
globals_wrapper() :
83+
app(std::move(ruisapp::application_factory::make_application(
84+
0, // argc
85+
nullptr // argv
86+
)))
8387
{
8488
// add timer descriptor to looper, this is needed for updatable to work
8589
if (ALooper_addFd(
@@ -106,6 +110,11 @@ globals_wrapper()
106110
{
107111
throw std::runtime_error("failed to add UI message queue descriptor to looper");
108112
}
113+
114+
// Set the fd_flag to call the update() for the first time if there
115+
// were any updateables started during creating application
116+
// object.
117+
this->fd_flag.set();
109118
}
110119

111120
~globals_wrapper()

src/ruisapp/glue/android/globals.hxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <android/window.h>
55
#include <nitki/queue.hpp>
66
#include <utki/debug.hpp>
7+
#include <utki/unique_ref.hpp>
78

89
#include "../../application.hpp"
910

@@ -56,11 +57,10 @@ struct globals_wrapper final {
5657
utki::make_unique<android_configuration_wrapper>(*android_configuration_wrapper::native_activity->assetManager);
5758

5859
AInputQueue* input_queue = nullptr;
59-
ANativeWindow* android_window = nullptr;
6060

6161
ruis::vector2 cur_window_dims(0, 0);
6262

63-
std::unique_ptr<ruisapp::application> app;
63+
utki::unique_ref<ruisapp::application> app;
6464
};
6565
} // namespace
6666

src/ruisapp/glue/android/glue.cxx

Lines changed: 31 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -481,11 +481,13 @@ void handle_input_events()
481481
auto& glob = get_glob();
482482
utki::assert(glob.input_queue, SL);
483483

484+
auto& glue = get_glue();
485+
486+
// TODO: remove
484487
auto& app = ruisapp::inst();
485488

486489
// read and handle input events
487-
AInputEvent* event;
488-
while (AInputQueue_getEvent(glob.input_queue, &event) >= 0) {
490+
while (AInputEvent* event; AInputQueue_getEvent(glob.input_queue, &event) >= 0) {
489491
utki::assert(event, SL);
490492

491493
// utki::log_debug([&](auto&o){o << "New input event: type = " <<
@@ -675,9 +677,9 @@ void handle_input_events()
675677
);
676678
}
677679

678-
get_impl(app).render(app);
680+
glue.render();
679681

680-
fd_flag.set();
682+
glob.fd_flag.set();
681683
}
682684
} // namespace
683685

@@ -735,17 +737,14 @@ void on_configuration_changed(ANativeActivity* activity)
735737

736738
auto& glob = get_glob();
737739

738-
// find out what exactly has changed in the configuration
739-
int32_t diff;
740-
{
741-
utki::assert(activity->assetManager, SL);
742-
auto config = utki::make_unique<android_configuration_wrapper>(*activity->assetManager);
740+
// find out what exactly has changed in the configuration
741+
utki::assert(activity->assetManager, SL);
742+
auto config = utki::make_unique<android_configuration_wrapper>(*activity->assetManager);
743743

744-
diff = glob.cur_android_configuration.diff(config);
744+
int32_t diff = glob.cur_android_configuration.diff(config);
745745

746-
// store new configuration
747-
glob.cur_android_configuration = std::move(config);
748-
}
746+
// store new configuration
747+
glob.cur_android_configuration = std::move(config);
749748

750749
// if orientation has changed
751750
if (diff & ACONFIGURATION_ORIENTATION) {
@@ -792,53 +791,22 @@ void on_native_window_created(
792791
utki::assert(globals_wrapper::native_activity, SL);
793792
utki::assert(activity == globals_wrapper::native_activity, SL);
794793

794+
utki::assert(window, SL);
795+
795796
utki::log_debug([](auto& o) {
796797
o << "on_native_window_created(): invoked" << std::endl;
797798
});
798799

799800
auto& glob = get_glob();
800801

801-
// save window in a static var, so it is accessible for creating window surface
802-
glob.android_window = window;
803-
804802
glob.cur_window_dims.x() = float(ANativeWindow_getWidth(window));
805803
glob.cur_window_dims.y() = float(ANativeWindow_getHeight(window));
806804

807-
// If we have no application instance yet, create it now.
808-
// Otherwise the window was re-created after moving the app from background to foreground
809-
// and we just need to create EGL surface for the new window.
810-
if (!glob.app) {
811-
try {
812-
// TODO: create application in globals_wrapper, do not create surface there, create surface here
813-
application* app = ruisapp::application_factory::make_application(
814-
0, // argc
815-
nullptr // argv
816-
)
817-
.release();
818-
819-
// android application should always have GUI
820-
utki::assert_always(app, SL);
821-
822-
activity->instance = app;
823-
824-
// Set the fd_flag to call the update() for the first time if there
825-
// were any updateables started during creating application
826-
// object.
827-
glob.fd_flag.set();
828-
} catch (std::exception& e) {
829-
utki::log_debug([&](auto& o) {
830-
o << "std::exception uncaught while creating application instance: " << e.what() << std::endl;
831-
});
832-
throw;
833-
} catch (...) {
834-
utki::log_debug([](auto& o) {
835-
o << "unknown exception uncaught while creating application instance!" << std::endl;
836-
});
837-
throw;
838-
}
839-
} else {
840-
get_impl(get_app(activity)).create_surface();
841-
}
805+
auto& glue = get_glue();
806+
807+
// The android window was just initially created or was re-created after moving the app
808+
// from background to foreground. In any case we just need to create EGL surface for the new android window.
809+
glue.create_window_surface(*window);
842810
}
843811

844812
void on_native_window_resized(ANativeActivity* activity,//
@@ -848,6 +816,10 @@ void on_native_window_resized(ANativeActivity* activity,//
848816
o << "on_native_window_resized(): invoked" << std::endl;
849817
});
850818

819+
utki::assert(window, SL);
820+
821+
auto& glob = get_glob();
822+
851823
// save window dimensions
852824
glob.cur_window_dims.x() = float(ANativeWindow_getWidth(window));
853825
glob.cur_window_dims.y() = float(ANativeWindow_getHeight(window));
@@ -864,9 +836,8 @@ void on_native_window_redraw_needed(ANativeActivity* activity, //
864836
o << "on_native_window_redraw_needed(): invoked" << std::endl;
865837
});
866838

867-
auto& app = get_app(activity);
868-
869-
get_impl(app).render(app);
839+
auto& glue = get_glue();
840+
glue.render();
870841
}
871842

872843
// This function is called right before destroying Window object, according to
@@ -879,12 +850,12 @@ void on_native_window_destroyed(ANativeActivity* activity, //
879850
o << "on_native_window_destroyed(): invoked" << std::endl;
880851
});
881852

853+
auto& glue = get_glue();
854+
882855
// destroy EGL drawing surface associated with the window.
883856
// the EGL context remains existing and should preserve all resources like
884857
// textures, vertex buffers, etc.
885-
get_impl(get_app(activity)).destroy_surface();
886-
887-
android_window = nullptr;
858+
glue.destroy_window_surface();
888859
}
889860

890861
int on_input_events_ready_for_reading_from_queue(
@@ -999,6 +970,7 @@ void on_content_rect_changed(
999970
o << "on_content_rect_changed(): cur_window_dims = " << glob.cur_window_dims << std::endl;
1000971
});
1001972

973+
// TODO: this check is not needed anymore, since app object outlives window now
1002974
// Sometimes Android calls on_content_rect_changed() even after native window
1003975
// was destroyed, i.e. on_native_window_destroyed() was called and, thus,
1004976
// application object was destroyed. So need to check if our application is
@@ -1017,15 +989,15 @@ void on_content_rect_changed(
1017989
update_window_rect(
1018990
app,
1019991
ruis::rect(
1020-
float(rect->left),
992+
float(rect->left), //
1021993
glob.cur_window_dims.y() - float(rect->bottom),
1022994
float(rect->right - rect->left),
1023995
float(rect->bottom - rect->top)
1024996
)
1025997
);
1026998

1027999
// redraw, since WindowRedrawNeeded not always comes
1028-
get_impl(app).render(app);
1000+
glob.render();
10291001
}
10301002

10311003
void on_destroy(ANativeActivity* activity)

src/ruisapp/glue/android/window.cxx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void native_window::swap_frame_buffers(){
2323
}
2424
}
2525

26-
void native_window::create_surface(){
26+
void native_window::create_surface(ANativeWindow& android_window){
2727
utki::assert(!this->egl_surface.has_value(), SL);
2828

2929
EGLint format;
@@ -42,21 +42,18 @@ void native_window::create_surface(){
4242
throw std::runtime_error("eglGetConfigAttrib() failed");
4343
}
4444

45-
utki::assert(android_window, SL);
46-
47-
// TODO: get android_window from somewhere
48-
utki::assert(android_window, SL);
45+
// if both buffer width and height are 0 then it will be sized to the window dimensions
4946
ANativeWindow_setBuffersGeometry(
50-
android_window, //
51-
0,
52-
0,
47+
&android_window, //
48+
0, // buffer width in pixels
49+
0, // buffer height in pixels
5350
format
5451
);
5552

5653
this->egl_surface.emplace(
5754
this->egl_display,//
5855
this->egl_config,
59-
EGLNativeWindowType(android_window)
56+
EGLNativeWindowType(&android_window)
6057
);
6158
}
6259

src/ruisapp/glue/android/window.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public:
2323

2424
void swap_frame_buffers()override;
2525

26-
void create_surface();
26+
void create_surface(ANativeWindow& android_window);
2727
void destroy_surface();
2828

2929
r4::vector2<unsigned> get_dims();

0 commit comments

Comments
 (0)