Skip to content

Commit

Permalink
Add android demo. (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
lvpengwei authored Nov 2, 2023
1 parent 56c87dc commit f2d9b54
Show file tree
Hide file tree
Showing 39 changed files with 1,086 additions and 0 deletions.
15 changes: 15 additions & 0 deletions android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
14 changes: 14 additions & 0 deletions android/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions android/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
84 changes: 84 additions & 0 deletions android/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}

android {
namespace = "io.pag.tgfxdemo"
compileSdk = 33

defaultConfig {
applicationId = "io.pag.tgfxdemo"
minSdk = 24
targetSdk = 33
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
externalNativeBuild {
cmake {
cppFlags("-std=c++17")
arguments("-DANDROID_STL=c++_static")
}
}
ndk {
abiFilters.add("armeabi-v7a")
abiFilters.add("arm64-v8a")
}
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
externalNativeBuild {
cmake {
path("src/main/cpp/CMakeLists.txt")
version = "3.22.1"
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.4.3"
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}

dependencies {
implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
implementation("androidx.activity:activity-compose:1.7.2")
implementation(platform("androidx.compose:compose-bom:2023.03.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
}
21 changes: 21 additions & 0 deletions android/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.pag.tgfxdemo

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("io.pag.tgfxdemo", appContext.packageName)
}
}
28 changes: 28 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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.TGFXDemo"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.TGFXDemo">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
15 changes: 15 additions & 0 deletions android/app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.10)
project(TGFXDemo)

set(TGFX_DEMO_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../include)

find_library(LOG_LIB log)
list(APPEND TGFX_DEMO_SHARED_LIBS ${LOG_LIB})
find_library(ANDROID_LIB android)
list(APPEND TGFX_DEMO_SHARED_LIBS ${ANDROID_LIB})

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../../../../../ ./tgfx)

add_library(tgfxdemo SHARED JTGFXView.cpp)
target_include_directories(tgfxdemo PRIVATE ${TGFX_DEMO_INCLUDES})
target_link_libraries(tgfxdemo tgfx ${TGFX_DEMO_SHARED_LIBS})
144 changes: 144 additions & 0 deletions android/app/src/main/cpp/JTGFXView.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#include <jni.h>
#include <string>
#include <jni.h>
#include <android/native_window_jni.h>
#include "tgfx/gpu/Window.h"
#include "tgfx/opengl/egl/EGLWindow.h"

namespace tgfxdemo {
static jfieldID TGFXView_nativePtr;

class JTGFXView {
public:
explicit JTGFXView(ANativeWindow* nativeWindow, std::shared_ptr<tgfx::Window> window)
: nativeWindow(nativeWindow), window(std::move(window)) {
updateSize();
}

~JTGFXView() {
ANativeWindow_release(nativeWindow);
}

void updateSize() {
_width = ANativeWindow_getWidth(nativeWindow);
_height = ANativeWindow_getHeight(nativeWindow);
surface = nullptr;
}

void draw() {
if (surface == nullptr) {
createSurface();
}
if (surface == nullptr) {
return;
}
auto device = window->getDevice();
auto context = device->lockContext();
if (context == nullptr) {
return;
}
auto canvas = surface->getCanvas();
canvas->clear();
tgfx::Paint paint;
paint.setColor(tgfx::Color{0.8f, 0.8f, 0.8f, 1.f});
if (drawCount % 2 == 0) {
auto rect = tgfx::Rect::MakeXYWH(20, 20, 100, 100);
canvas->drawRect(rect, paint);
} else {
int tileSize = 8;
for (int y = 0; y < _height; y += tileSize) {
bool draw = (y / tileSize) % 2 == 1;
for (int x = 0; x < _width; x += tileSize) {
if (draw) {
auto rect = tgfx::Rect::MakeXYWH(static_cast<float>(x), static_cast<float>(y),
static_cast<float>(tileSize), static_cast<float>(tileSize));
canvas->drawRect(rect, paint);
}
draw = !draw;
}
}
}
surface->flush();
context->submit();
window->present(context);
device->unlock();
drawCount++;
}

private:
void createSurface() {
if (_width <= 0 || _height <= 0) {
return;
}
auto device = window->getDevice();
auto context = device->lockContext();
if (context == nullptr) {
return;
}
surface = window->createSurface(context);
device->unlock();
}

ANativeWindow* nativeWindow = nullptr;
std::shared_ptr<tgfx::Window> window;
std::shared_ptr<tgfx::Surface> surface;
int _width = 0;
int _height = 0;
int drawCount = 0;
};
}

tgfxdemo::JTGFXView* getJTGFXView(JNIEnv* env, jobject thiz) {
return reinterpret_cast<tgfxdemo::JTGFXView*>(env->GetLongField(thiz,
tgfxdemo::TGFXView_nativePtr));
}

extern "C" {
JNIEXPORT void JNICALL
Java_io_pag_tgfxdemo_TGFXView_nativeRelease(JNIEnv* env, jobject thiz) {
auto jTGFXView =
reinterpret_cast<tgfxdemo::JTGFXView*>(env->GetLongField(thiz, tgfxdemo::TGFXView_nativePtr));
delete jTGFXView;
}

JNIEXPORT void JNICALL
Java_io_pag_tgfxdemo_TGFXView_00024Companion_nativeInit(JNIEnv* env, jobject thiz) {
auto clazz = env->FindClass("io/pag/tgfxdemo/TGFXView");
tgfxdemo::TGFXView_nativePtr = env->GetFieldID(clazz, "nativePtr", "J");
}

JNIEXPORT jlong JNICALL
Java_io_pag_tgfxdemo_TGFXView_00024Companion_setupFromSurface(JNIEnv* env, jobject,
jobject surface) {

if (surface == nullptr) {
printf("SetupFromSurface() Invalid surface specified.\n");
return 0;
}
auto nativeWindow = ANativeWindow_fromSurface(env, surface);
auto window = tgfx::EGLWindow::MakeFrom(nativeWindow);
if (window == nullptr) {
printf("SetupFromSurface() Invalid surface specified.\n");
return 0;
}
return reinterpret_cast<jlong>(new tgfxdemo::JTGFXView(nativeWindow, std::move(window)));
}

JNIEXPORT void JNICALL
Java_io_pag_tgfxdemo_TGFXView_nativeDraw(JNIEnv* env, jobject thiz) {
auto* view = getJTGFXView(env, thiz);
if (view == nullptr) {
return;
}
view->draw();
}

JNIEXPORT void JNICALL
Java_io_pag_tgfxdemo_TGFXView_updateSize(JNIEnv* env, jobject thiz) {
auto* view = getJTGFXView(env, thiz);
if (view == nullptr) {
return;
}
view->updateSize();
}
}
15 changes: 15 additions & 0 deletions android/app/src/main/java/io/pag/tgfxdemo/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.pag.tgfxdemo

import android.os.Bundle
import androidx.activity.ComponentActivity

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val view = findViewById<TGFXView>(R.id.tgfx_demo_view)
view.setOnClickListener {
view.draw()
}
}
}
Loading

0 comments on commit f2d9b54

Please sign in to comment.