-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathnative_engine.cpp
More file actions
710 lines (615 loc) · 21.1 KB
/
native_engine.cpp
File metadata and controls
710 lines (615 loc) · 21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed 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
*
* https://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.
*/
#include "native_engine.h"
#include <android/window.h>
#include "common.h"
#include "demo_scene.h"
#include "imgui_manager.h"
#include "input_util.h"
#include "scene_manager.h"
// verbose debug logs on?
#define VERBOSE_LOGGING 1
#if VERBOSE_LOGGING
#define VLOGD ALOGI
#else
#define VLOGD
#endif
// max # of GL errors to print before giving up
#define MAX_GL_ERRORS 200
static bool all_motion_filter(const GameActivityMotionEvent *event) {
// Process all motion events
return true;
}
static NativeEngine *_singleton = NULL;
// workaround for internal bug b/149866792
static NativeEngineSavedState appState = {false};
NativeEngine::NativeEngine(struct android_app *app) {
ALOGI("NativeEngine: initializing.");
mApp = app;
mHasFocus = mIsVisible = mHasWindow = false;
mHasGLObjects = false;
mEglDisplay = EGL_NO_DISPLAY;
mEglSurface = EGL_NO_SURFACE;
mEglContext = EGL_NO_CONTEXT;
mEglConfig = 0;
mSurfWidth = mSurfHeight = 0;
mApiVersion = 0;
mScreenDensity = AConfiguration_getDensity(app->config);
mActiveAxisIds = 0;
mJniEnv = NULL;
mImGuiManager = NULL;
memset(&mState, 0, sizeof(mState));
mIsFirstFrame = true;
app->motionEventFilter = all_motion_filter;
if (app->savedState != NULL) {
// we are starting with previously saved state -- restore it
mState = *(struct NativeEngineSavedState *)app->savedState;
}
// only one instance of NativeEngine may exist!
MY_ASSERT(_singleton == NULL);
_singleton = this;
// Initialize Swappy to adjust swap timing properly.
ALOGI("Calling SwappyGL_init");
SwappyGL_init(GetJniEnv(), mApp->activity->javaGameActivity);
SwappyGL_setSwapIntervalNS(SWAPPY_SWAP_60FPS);
VLOGD("NativeEngine: querying API level.");
ALOGI("NativeEngine: API version %d.", mApiVersion);
ALOGI("NativeEngine: Density %d", mScreenDensity);
}
NativeEngine *NativeEngine::GetInstance() {
MY_ASSERT(_singleton != NULL);
return _singleton;
}
NativeEngine::~NativeEngine() {
// Destroy Swappy instance.
SwappyGL_destroy();
VLOGD("NativeEngine: destructor running");
KillContext();
if (mImGuiManager != NULL) {
delete mImGuiManager;
}
if (mJniEnv) {
ALOGI("Detaching current thread from JNI.");
mApp->activity->vm->DetachCurrentThread();
ALOGI("Current thread detached from JNI.");
mJniEnv = NULL;
}
_singleton = NULL;
}
static void _handle_cmd_proxy(struct android_app *app, int32_t cmd) {
NativeEngine *engine = (NativeEngine *)app->userData;
engine->HandleCommand(cmd);
}
bool NativeEngine::IsAnimating() {
return mHasFocus && mIsVisible && mHasWindow;
}
static bool _cooked_event_callback(struct CookedEvent *event) {
SceneManager *mgr = SceneManager::GetInstance();
PointerCoords coords;
memset(&coords, 0, sizeof(coords));
coords.x_ = event->motion_x_;
coords.y_ = event->motion_y_;
coords.min_x_ = event->motion_min_x_;
coords.max_x_ = event->motion_max_x_;
coords.min_y_ = event->motion_min_y_;
coords.max_y_ = event->motion_max_y_;
coords.is_screen_ = event->motion_is_on_screen_;
switch (event->type_) {
case COOKED_EVENT_TYPE_POINTER_DOWN:
mgr->OnPointerDown(event->motion_pointer_id_, &coords);
return true;
case COOKED_EVENT_TYPE_POINTER_UP:
mgr->OnPointerUp(event->motion_pointer_id_, &coords);
return true;
case COOKED_EVENT_TYPE_POINTER_MOVE:
mgr->OnPointerMove(event->motion_pointer_id_, &coords);
return true;
default:
return false;
}
}
// This is here and not in input_util.cpp due to being specific to the
// GameActivity version
static bool _cook_game_activity_motion_event(
GameActivityMotionEvent *motionEvent, CookedEventCallback callback) {
if (motionEvent->pointerCount > 0) {
int action = motionEvent->action;
int actionMasked = action & AMOTION_EVENT_ACTION_MASK;
int ptrIndex = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
if (ptrIndex < motionEvent->pointerCount) {
struct CookedEvent ev;
memset(&ev, 0, sizeof(ev));
if (actionMasked == AMOTION_EVENT_ACTION_DOWN ||
actionMasked == AMOTION_EVENT_ACTION_POINTER_DOWN) {
ev.type_ = COOKED_EVENT_TYPE_POINTER_DOWN;
} else if (actionMasked == AMOTION_EVENT_ACTION_UP ||
actionMasked == AMOTION_EVENT_ACTION_POINTER_UP) {
ev.type_ = COOKED_EVENT_TYPE_POINTER_UP;
} else {
ev.type_ = COOKED_EVENT_TYPE_POINTER_MOVE;
}
ev.motion_pointer_id_ = motionEvent->pointers[ptrIndex].id;
ev.motion_is_on_screen_ =
(motionEvent->source & AINPUT_SOURCE_TOUCHSCREEN) ==
AINPUT_SOURCE_TOUCHSCREEN;
ev.motion_x_ =
GameActivityPointerAxes_getX(&motionEvent->pointers[ptrIndex]);
ev.motion_y_ =
GameActivityPointerAxes_getY(&motionEvent->pointers[ptrIndex]);
if (ev.motion_is_on_screen_) {
// use screen size as the motion range
ev.motion_min_x_ = 0.0f;
ev.motion_max_x_ = SceneManager::GetInstance()->GetScreenWidth();
ev.motion_min_y_ = 0.0f;
ev.motion_max_y_ = SceneManager::GetInstance()->GetScreenHeight();
}
return callback(&ev);
}
}
return false;
}
void NativeEngine::GameLoop() {
mApp->userData = this;
mApp->onAppCmd = _handle_cmd_proxy;
// mApp->onInputEvent = _handle_input_proxy;
auto activity = NativeEngine::GetInstance()->GetAndroidApp()->activity;
GameActivity_setWindowFlags(
activity,
AWINDOW_FLAG_KEEP_SCREEN_ON | AWINDOW_FLAG_TURN_SCREEN_ON |
AWINDOW_FLAG_FULLSCREEN | AWINDOW_FLAG_SHOW_WHEN_LOCKED,
0);
UpdateSystemBarOffset();
while (1) {
int events;
struct android_poll_source *source;
// 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;
}
}
HandleGameActivityInput();
if (IsAnimating()) {
DoFrame();
}
}
}
JNIEnv *NativeEngine::GetJniEnv() {
if (!mJniEnv) {
ALOGI("Attaching current thread to JNI.");
if (0 != mApp->activity->vm->AttachCurrentThread(&mJniEnv, NULL)) {
ALOGE("*** FATAL ERROR: Failed to attach thread to JNI.");
ABORT_GAME;
}
MY_ASSERT(mJniEnv != NULL);
ALOGI("Attached current thread to JNI, %p", mJniEnv);
}
return mJniEnv;
}
JNIEnv *NativeEngine::GetAppJniEnv() {
if (!mAppJniEnv) {
ALOGI("Attaching current thread to JNI.");
if (0 != mApp->activity->vm->AttachCurrentThread(&mAppJniEnv, NULL)) {
ALOGE("*** FATAL ERROR: Failed to attach thread to JNI.");
ABORT_GAME;
}
MY_ASSERT(mAppJniEnv != NULL);
ALOGI("Attached current thread to JNI, %p", mAppJniEnv);
}
return mAppJniEnv;
}
void NativeEngine::HandleCommand(int32_t cmd) {
SceneManager *mgr = SceneManager::GetInstance();
VLOGD("NativeEngine: handling command %d.", cmd);
switch (cmd) {
case APP_CMD_SAVE_STATE:
// The system has asked us to save our current state.
VLOGD("NativeEngine: APP_CMD_SAVE_STATE");
mState.mHasFocus = mHasFocus;
mApp->savedState = malloc(sizeof(mState));
*((NativeEngineSavedState *)mApp->savedState) = mState;
mApp->savedStateSize = sizeof(mState);
break;
case APP_CMD_INIT_WINDOW:
// We have a window!
VLOGD("NativeEngine: APP_CMD_INIT_WINDOW");
if (mApp->window != NULL) {
mHasWindow = true;
// Set the window to Swappy instance.
SwappyGL_setWindow(mApp->window);
if (mApp->savedStateSize == sizeof(mState) &&
mApp->savedState != nullptr) {
mState = *((NativeEngineSavedState *)mApp->savedState);
mHasFocus = mState.mHasFocus;
} else {
// Workaround APP_CMD_GAINED_FOCUS issue where the focus state is not
// passed down from NativeActivity when restarting Activity
mHasFocus = appState.mHasFocus;
}
}
VLOGD("HandleCommand(%d): hasWindow = %d, hasFocus = %d", cmd,
mHasWindow ? 1 : 0, mHasFocus ? 1 : 0);
break;
case APP_CMD_TERM_WINDOW:
// The window is going away -- kill the surface
VLOGD("NativeEngine: APP_CMD_TERM_WINDOW");
KillSurface();
mHasWindow = false;
break;
case APP_CMD_GAINED_FOCUS:
VLOGD("NativeEngine: APP_CMD_GAINED_FOCUS");
mHasFocus = true;
mState.mHasFocus = appState.mHasFocus = mHasFocus;
break;
case APP_CMD_LOST_FOCUS:
VLOGD("NativeEngine: APP_CMD_LOST_FOCUS");
mHasFocus = false;
mState.mHasFocus = appState.mHasFocus = mHasFocus;
break;
case APP_CMD_PAUSE:
VLOGD("NativeEngine: APP_CMD_PAUSE");
mgr->OnPause();
VLOGD("NativeEngine: APP_CMD_PAUSE2");
break;
case APP_CMD_RESUME:
VLOGD("NativeEngine: APP_CMD_RESUME");
mgr->OnResume();
break;
case APP_CMD_STOP:
VLOGD("NativeEngine: APP_CMD_STOP");
mIsVisible = false;
break;
case APP_CMD_START:
VLOGD("NativeEngine: APP_CMD_START");
mIsVisible = true;
break;
case APP_CMD_WINDOW_RESIZED:
case APP_CMD_CONFIG_CHANGED:
VLOGD("NativeEngine: %s", cmd == APP_CMD_WINDOW_RESIZED
? "APP_CMD_WINDOW_RESIZED"
: "APP_CMD_CONFIG_CHANGED");
// Window was resized or some other configuration changed.
// Note: we don't handle this event because we check the surface
// dimensions every frame, so that's how we know it was resized. If you
// are NOT doing that, then you need to handle this event!
break;
case APP_CMD_LOW_MEMORY:
VLOGD("NativeEngine: APP_CMD_LOW_MEMORY");
// system told us we have low memory. So if we are not visible, let's
// cooperate by deallocating all of our graphic resources.
if (!mHasWindow) {
VLOGD("NativeEngine: trimming memory footprint (deleting GL objects).");
KillGLObjects();
}
break;
case APP_CMD_WINDOW_INSETS_CHANGED:
VLOGD("NativeEngine: APP_CMD_WINDOW_INSETS_CHANGED");
UpdateSystemBarOffset();
break;
default:
VLOGD("NativeEngine: (unknown command).");
break;
}
VLOGD("NativeEngine: STATUS: F%d, V%d, W%d, EGL: D %p, S %p, CTX %p, CFG %p",
mHasFocus, mIsVisible, mHasWindow, mEglDisplay, mEglSurface,
mEglContext, mEglConfig);
}
void NativeEngine::UpdateSystemBarOffset() {
ARect insets;
// Log all the insets types
GameActivity_getWindowInsets(mApp->activity,
GAMECOMMON_INSETS_TYPE_SYSTEM_BARS, &insets);
mSystemBarOffset = insets.top;
}
bool NativeEngine::HandleInput(AInputEvent *event) { return false; }
void NativeEngine::HandleGameActivityInput() {
// Swap input buffers so we don't miss any events while processing
// inputBuffer.
android_input_buffer *inputBuffer = android_app_swap_input_buffers(mApp);
// Early exit if no events.
if (inputBuffer == nullptr) return;
if (inputBuffer->keyEventsCount != 0) {
android_app_clear_key_events(inputBuffer);
}
if (inputBuffer->motionEventsCount != 0) {
for (uint64_t i = 0; i < inputBuffer->motionEventsCount; ++i) {
GameActivityMotionEvent *motionEvent = &inputBuffer->motionEvents[i];
// Didn't belong to a game controller, process it ourselves if it is a
// touch event
_cook_game_activity_motion_event(motionEvent, _cooked_event_callback);
}
android_app_clear_motion_events(inputBuffer);
}
}
bool NativeEngine::InitDisplay() {
if (mEglDisplay != EGL_NO_DISPLAY) {
// nothing to do
ALOGI("NativeEngine: no need to init display (already had one).");
return true;
}
ALOGI("NativeEngine: initializing display.");
mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (EGL_FALSE == eglInitialize(mEglDisplay, 0, 0)) {
ALOGE("NativeEngine: failed to init display, error %d", eglGetError());
return false;
}
return true;
}
bool NativeEngine::InitSurface() {
// need a display
MY_ASSERT(mEglDisplay != EGL_NO_DISPLAY);
if (mEglSurface != EGL_NO_SURFACE) {
// nothing to do
ALOGI("NativeEngine: no need to init surface (already had one).");
return true;
}
ALOGI("NativeEngine: initializing surface.");
EGLint numConfigs;
const EGLint attribs[] = {EGL_RENDERABLE_TYPE,
EGL_OPENGL_ES2_BIT, // request OpenGL ES 2.0
EGL_SURFACE_TYPE,
EGL_WINDOW_BIT,
EGL_BLUE_SIZE,
8,
EGL_GREEN_SIZE,
8,
EGL_RED_SIZE,
8,
EGL_DEPTH_SIZE,
16,
EGL_NONE};
// since this is a simple sample, we have a trivial selection process. We pick
// the first EGLConfig that matches:
eglChooseConfig(mEglDisplay, attribs, &mEglConfig, 1, &numConfigs);
// create EGL surface
mEglSurface =
eglCreateWindowSurface(mEglDisplay, mEglConfig, mApp->window, NULL);
if (mEglSurface == EGL_NO_SURFACE) {
ALOGE("Failed to create EGL surface, EGL error %d", eglGetError());
return false;
}
ALOGI("NativeEngine: successfully initialized surface.");
return true;
}
bool NativeEngine::InitContext() {
// need a display
MY_ASSERT(mEglDisplay != EGL_NO_DISPLAY);
EGLint attribList[] = {EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE}; // OpenGL ES 2.0
if (mEglContext != EGL_NO_CONTEXT) {
// nothing to do
ALOGI("NativeEngine: no need to init context (already had one).");
return true;
}
ALOGI("NativeEngine: initializing context.");
// create EGL context
mEglContext = eglCreateContext(mEglDisplay, mEglConfig, NULL, attribList);
if (mEglContext == EGL_NO_CONTEXT) {
ALOGE("Failed to create EGL context, EGL error %d", eglGetError());
return false;
}
ALOGI("NativeEngine: successfully initialized context.");
return true;
}
void NativeEngine::ConfigureOpenGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
}
bool NativeEngine::PrepareToRender() {
if (mEglDisplay == EGL_NO_DISPLAY || mEglSurface == EGL_NO_SURFACE ||
mEglContext == EGL_NO_CONTEXT) {
// create display if needed
if (!InitDisplay()) {
ALOGE("NativeEngine: failed to create display.");
return false;
}
// create surface if needed
if (!InitSurface()) {
ALOGE("NativeEngine: failed to create surface.");
return false;
}
// create context if needed
if (!InitContext()) {
ALOGE("NativeEngine: failed to create context.");
return false;
}
ALOGI(
"NativeEngine: binding surface and context (display %p, surface %p, "
"context %p)",
mEglDisplay, mEglSurface, mEglContext);
// bind them
if (EGL_FALSE ==
eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) {
ALOGE("NativeEngine: eglMakeCurrent failed, EGL error %d", eglGetError());
HandleEglError(eglGetError());
}
// configure our global OpenGL settings
ConfigureOpenGL();
if (mImGuiManager == NULL) {
mImGuiManager = new ImGuiManager();
}
}
if (!mHasGLObjects) {
ALOGI("NativeEngine: creating OpenGL objects.");
if (!InitGLObjects()) {
ALOGE("NativeEngine: unable to initialize OpenGL objects.");
return false;
}
}
// Keep the ImGui display size up to date
mImGuiManager->SetDisplaySize(mSurfWidth, mSurfHeight, mScreenDensity);
// ready to render
return true;
}
void NativeEngine::KillGLObjects() {
if (mHasGLObjects) {
SceneManager *mgr = SceneManager::GetInstance();
mgr->KillGraphics();
mHasGLObjects = false;
}
}
void NativeEngine::KillSurface() {
ALOGI("NativeEngine: killing surface.");
eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (mEglSurface != EGL_NO_SURFACE) {
eglDestroySurface(mEglDisplay, mEglSurface);
mEglSurface = EGL_NO_SURFACE;
}
ALOGI("NativeEngine: Surface killed successfully.");
}
void NativeEngine::KillContext() {
ALOGI("NativeEngine: killing context.");
// since the context is going away, we have to kill the GL objects
KillGLObjects();
eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (mEglContext != EGL_NO_CONTEXT) {
eglDestroyContext(mEglDisplay, mEglContext);
mEglContext = EGL_NO_CONTEXT;
}
ALOGI("NativeEngine: Context killed successfully.");
}
void NativeEngine::KillDisplay() {
// causes context and surface to go away too, if they are there
ALOGI("NativeEngine: killing display.");
KillContext();
KillSurface();
if (mEglDisplay != EGL_NO_DISPLAY) {
ALOGI("NativeEngine: terminating display now.");
eglTerminate(mEglDisplay);
mEglDisplay = EGL_NO_DISPLAY;
}
ALOGI("NativeEngine: display killed successfully.");
}
bool NativeEngine::HandleEglError(EGLint error) {
switch (error) {
case EGL_SUCCESS:
// nothing to do
return true;
case EGL_CONTEXT_LOST:
ALOGW("NativeEngine: egl error: EGL_CONTEXT_LOST. Recreating context.");
KillContext();
return true;
case EGL_BAD_CONTEXT:
ALOGW("NativeEngine: egl error: EGL_BAD_CONTEXT. Recreating context.");
KillContext();
return true;
case EGL_BAD_DISPLAY:
ALOGW("NativeEngine: egl error: EGL_BAD_DISPLAY. Recreating display.");
KillDisplay();
return true;
case EGL_BAD_SURFACE:
ALOGW("NativeEngine: egl error: EGL_BAD_SURFACE. Recreating display.");
KillSurface();
return true;
default:
ALOGW("NativeEngine: unknown egl error: %d", error);
return false;
}
}
static void _log_opengl_error(GLenum err) {
switch (err) {
case GL_NO_ERROR:
ALOGE("*** OpenGL error: GL_NO_ERROR");
break;
case GL_INVALID_ENUM:
ALOGE("*** OpenGL error: GL_INVALID_ENUM");
break;
case GL_INVALID_VALUE:
ALOGE("*** OpenGL error: GL_INVALID_VALUE");
break;
case GL_INVALID_OPERATION:
ALOGE("*** OpenGL error: GL_INVALID_OPERATION");
break;
case GL_INVALID_FRAMEBUFFER_OPERATION:
ALOGE("*** OpenGL error: GL_INVALID_FRAMEBUFFER_OPERATION");
break;
case GL_OUT_OF_MEMORY:
ALOGE("*** OpenGL error: GL_OUT_OF_MEMORY");
break;
default:
ALOGE("*** OpenGL error: error %d", err);
break;
}
}
void NativeEngine::DoFrame() {
// prepare to render (create context, surfaces, etc, if needed)
if (!PrepareToRender()) {
// not ready
VLOGD("NativeEngine: preparation to render failed.");
return;
}
SceneManager *mgr = SceneManager::GetInstance();
// how big is the surface? We query every frame because it's cheap, and some
// strange devices out there change the surface size without calling any
// callbacks...
int width, height;
eglQuerySurface(mEglDisplay, mEglSurface, EGL_WIDTH, &width);
eglQuerySurface(mEglDisplay, mEglSurface, EGL_HEIGHT, &height);
if (width != mSurfWidth || height != mSurfHeight) {
// notify scene manager that the surface has changed size
ALOGI("NativeEngine: surface changed size %dx%d --> %dx%d", mSurfWidth,
mSurfHeight, width, height);
mSurfWidth = width;
mSurfHeight = height;
mgr->SetScreenSize(mSurfWidth, mSurfHeight);
glViewport(0, 0, mSurfWidth, mSurfHeight);
}
// if this is the first frame, install the demo scene
if (mIsFirstFrame) {
mIsFirstFrame = false;
mgr->RequestNewScene(new DemoScene());
}
// render!
mgr->DoFrame();
if (mImGuiManager != NULL) {
mImGuiManager->EndImGuiFrame();
}
// swap buffers
if (!SwappyGL_swap(mEglDisplay, mEglSurface)) { // failed to swap buffers...
ALOGW("NativeEngine: SwappyGL_swap failed, EGL error %d", eglGetError());
HandleEglError(eglGetError());
}
// print out GL errors, if any
GLenum e;
static int errorsPrinted = 0;
while ((e = glGetError()) != GL_NO_ERROR) {
if (errorsPrinted < MAX_GL_ERRORS) {
_log_opengl_error(e);
++errorsPrinted;
if (errorsPrinted >= MAX_GL_ERRORS) {
ALOGE("*** NativeEngine: TOO MANY OPENGL ERRORS. NO LONGER PRINTING.");
}
}
}
}
android_app *NativeEngine::GetAndroidApp() { return mApp; }
bool NativeEngine::InitGLObjects() {
if (!mHasGLObjects) {
SceneManager *mgr = SceneManager::GetInstance();
mgr->StartGraphics();
_log_opengl_error(glGetError());
mHasGLObjects = true;
}
return true;
}