forked from android/ndk-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Add a combined graphics sample.
This will replace most of the existing GL and Vulkan samples. Right now this is just a trivial GameActivity app. More to come later. android#1060
- Loading branch information
Showing
30 changed files
with
506 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
## Graphics | ||
|
||
This sample demonstrates how to use Android graphics APIs like OpenGL and | ||
Vulkan. It renders a spinning textured cube. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
plugins { | ||
id("ndksamples.android.application") | ||
id("ndksamples.android.kotlin") | ||
} | ||
|
||
android { | ||
namespace = "com.android.ndk.samples.graphics" | ||
|
||
externalNativeBuild { | ||
cmake { | ||
path("src/main/cpp/CMakeLists.txt") | ||
} | ||
} | ||
|
||
buildFeatures { | ||
prefab = true | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation(libs.androidx.core.ktx) | ||
implementation(libs.androidx.games.gameactivity) | ||
implementation(libs.appcompat) | ||
implementation(libs.material) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:dataExtractionRules="@xml/data_extraction_rules" | ||
android:fullBackupContent="@xml/backup_rules" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/Theme.GraphicsSample" | ||
tools:targetApi="31"> | ||
<activity | ||
android:name=".MainActivity" | ||
android:exported="true"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
|
||
<meta-data | ||
android:name="android.app.lib_name" | ||
android:value="app" /> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
cmake_minimum_required(VERSION 3.22.1) | ||
project(GraphicsSample LANGUAGES CXX) | ||
|
||
find_package(game-activity REQUIRED CONFIG) | ||
|
||
add_compile_options(-Wall -Werror -Wextra) | ||
|
||
add_library(app | ||
SHARED | ||
android_main.cpp | ||
) | ||
|
||
target_link_libraries(app | ||
android | ||
log | ||
# Required to force the linker to include this definition in the app, since | ||
# there's otherwise no reference to it. | ||
-uJava_com_google_androidgamesdk_GameActivity_initializeNativeCode | ||
game-activity::game-activity_static | ||
) | ||
|
||
target_link_options(app | ||
PRIVATE | ||
-flto | ||
-Wl,--version-script,${CMAKE_SOURCE_DIR}/libapp.map.txt | ||
) | ||
|
||
set_target_properties(app | ||
PROPERTIES | ||
LINK_DEPENDS ${CMAKE_SOURCE_DIR}/libapp.map.txt | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright (C) 2024 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 | ||
* | ||
* 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. | ||
*/ | ||
|
||
#include <cstdlib> | ||
|
||
#include <android/looper.h> | ||
|
||
#include "game-activity/native_app_glue/android_native_app_glue.h" | ||
|
||
#define LOGI(...) | ||
|
||
static void HandleCmd(android_app *, int32_t) { | ||
// Nothing for now. | ||
} | ||
|
||
|
||
static bool KeyEventFilter(const GameActivityKeyEvent *) { | ||
return false; | ||
} | ||
|
||
static bool MotionEventFilter(const GameActivityMotionEvent *) { | ||
return false; | ||
} | ||
|
||
static void HandleInputEvents(android_app *app) { | ||
auto inputBuf = android_app_swap_input_buffers(app); | ||
if (inputBuf == nullptr) { | ||
return; | ||
} | ||
|
||
// For the minimum, apps need to process the exit event (for example, | ||
// listening to AKEYCODE_BACK). This sample has done that in the Kotlin side | ||
// and not processing other input events, we just reset the event counter | ||
// inside the android_input_buffer to keep app glue code in a working state. | ||
android_app_clear_motion_events(inputBuf); | ||
android_app_clear_motion_events(inputBuf); | ||
} | ||
|
||
|
||
void android_main(android_app* app) { | ||
app->onAppCmd = HandleCmd; | ||
|
||
android_app_set_key_event_filter(app, KeyEventFilter); | ||
android_app_set_motion_event_filter(app, MotionEventFilter); | ||
|
||
while (!app->destroyRequested) { | ||
android_poll_source* poll_source = nullptr; | ||
auto result = ALooper_pollOnce(-1, nullptr, nullptr, reinterpret_cast<void**>(&poll_source)); | ||
if (result == ALOOPER_POLL_ERROR) { | ||
std::abort(); | ||
} | ||
|
||
if (poll_source != nullptr) { | ||
poll_source->process(app, poll_source); | ||
} | ||
|
||
HandleInputEvents(app); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
LIBAPP { | ||
global: | ||
Java_com_google_androidgamesdk_GameActivity_initializeNativeCode; | ||
local: | ||
*; | ||
}; |
45 changes: 45 additions & 0 deletions
45
graphics/src/main/java/com/android/ndk/samples/graphics/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (C) 2024 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 | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.android.ndk.samples.graphics | ||
|
||
import android.view.View | ||
import com.google.androidgamesdk.GameActivity | ||
|
||
class MainActivity : GameActivity() { | ||
companion object { | ||
init { | ||
System.loadLibrary("app") | ||
} | ||
} | ||
|
||
override fun onWindowFocusChanged(hasFocus: Boolean) { | ||
super.onWindowFocusChanged(hasFocus) | ||
if (hasFocus) { | ||
hideSystemUi() | ||
} | ||
} | ||
|
||
private fun hideSystemUi() { | ||
val decorView = window.decorView | ||
decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | ||
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE | ||
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | ||
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | ||
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | ||
or View.SYSTEM_UI_FLAG_FULLSCREEN) | ||
} | ||
} |
Oops, something went wrong.