Skip to content

Commit

Permalink
WIP: Add a combined graphics sample.
Browse files Browse the repository at this point in the history
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
DanAlbert committed May 16, 2024
1 parent 3930e39 commit bdb6868
Show file tree
Hide file tree
Showing 30 changed files with 506 additions and 0 deletions.
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ curl = "7.79.1-beta-1"
googletest = "1.11.0-beta-1"
jsoncpp = "1.9.5-beta-1"
openssl = "1.1.1q-beta-1"
coreKtx = "1.13.1"

[libraries]
junit = { group = "junit", name = "junit", version.ref = "junit" }
Expand All @@ -40,6 +41,7 @@ openssl = { group = "com.android.ndk.thirdparty", name = "openssl", version.ref

# build-logic dependencies
android-gradlePlugin = { group = "com.android.tools.build", name = "gradle", version.ref = "agp" }
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
Expand Down
1 change: 1 addition & 0 deletions graphics/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
4 changes: 4 additions & 0 deletions graphics/README.md
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.
25 changes: 25 additions & 0 deletions graphics/build.gradle.kts
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)
}
30 changes: 30 additions & 0 deletions graphics/src/main/AndroidManifest.xml
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>
31 changes: 31 additions & 0 deletions graphics/src/main/cpp/CMakeLists.txt
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
)
72 changes: 72 additions & 0 deletions graphics/src/main/cpp/android_main.cpp
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);
}
}
6 changes: 6 additions & 0 deletions graphics/src/main/cpp/libapp.map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBAPP {
global:
Java_com_google_androidgamesdk_GameActivity_initializeNativeCode;
local:
*;
};
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)
}
}
Loading

0 comments on commit bdb6868

Please sign in to comment.