Skip to content
This repository was archived by the owner on Oct 23, 2024. It is now read-only.

Commit c1de268

Browse files
authored
Merge pull request #28 from DAGalpin/main
Added GLSLImageProcessor that (mostly) uses Kotlin to implement GL/ES Compute
2 parents 08d5130 + 494e1aa commit c1de268

File tree

14 files changed

+899
-16
lines changed

14 files changed

+899
-16
lines changed

RenderScriptMigrationSample/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55
[RenderScript is being deprecated](https://android-developers.googleblog.com/2021/04/android-gpu-compute-going-forward.html) since Android 12. We recommend computationally intensive applications to use [Vulkan](https://www.khronos.org/vulkan), the cross platform industry standard API. Please refer to the [RenderScript Migration Guide](https://developer.android.com/guide/topics/renderscript/migrate) for more details.
66

7-
To help developers for the migration, this sample is created to demonstrate how to apply the image filtering to a bitmap with the Vulkan compute pipeline. The sample creates a common ImageProcessor interface, on the top of Vulkan Compute and RenderScript, that performs two compute tasks:
7+
To help developers migrate, this sample is created to demonstrate how to apply image filtering to a bitmap with Renderscript Intrinsics, Renderscript Scripts, Vulkan compute pipeline, GLSL compute (in Kotlin). Since the two effects demonstrated can also be implemented using the platform RenderEffect, this is also demonstrated. The sample creates a common ImageProcessor interface that performs two compute tasks:
88
- HUE rotation: A simple compute task with a single compute kernel.
99
- Blur: A more complex compute task which executes two compute kernels sequentially.
1010

11-
Both tasks are implemented with RenderScript (intrinsics & custom scripts) and Vulkan to demonstrate the migration from RenderScript to Vulkan Compute pipeline.
11+
Both tasks are implemented with RenderScript (intrinsics & custom scripts), GLSL, and Vulkan to demonstrate the migration from RenderScript to Vulkan or GLSL Compute pipelines.
1212

1313
## Pre-requisites
1414

15-
- Android Studio Arctic Fox 2020.3.1+
16-
- SDK Build Tools 31.0.0+
17-
- NDK r20+
18-
- Android API 29+
15+
- Android Studio Flamingo 2022.2.1+
16+
- SDK Build Tools 34.0.0+
17+
- NDK 22.0.7026061
18+
- Android API 33+
1919

2020
## Getting Started
2121

@@ -32,7 +32,7 @@ Both tasks are implemented with RenderScript (intrinsics & custom scripts) and V
3232

3333
## Support
3434

35-
We highly recommend to use [Stack Overflow](http://stackoverflow.com/questions/tagged/android) to get help from the Andorid community.
35+
We highly recommend to use [Stack Overflow](http://stackoverflow.com/questions/tagged/android) to get help from the Android community.
3636

3737
If you've found an error in this sample, please file an issue:
3838
https://github.com/android/renderscript-samples

RenderScriptMigrationSample/app/build.gradle

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
22
apply plugin: 'kotlin-android'
33

44
android {
5-
compileSdkVersion 31
5+
compileSdk 33
66

77
// When building this project with with SDK build tools of version earlier than 31.0.0, and
88
// minSdkVersion 29+, the RenderScript compiler will fail with the following error message:
@@ -44,7 +44,7 @@ android {
4444
sourceSets {
4545
main {
4646
jniLibs {
47-
srcDirs "$android.ndkDirectory/sources/third_party/vulkan/src/build-android/jniLibs"
47+
srcDirs 'src/main/jniLibs/android-binaries-sdk-1.3.261.0'
4848
}
4949
}
5050
}
@@ -57,6 +57,9 @@ android {
5757
dependencies {
5858
implementation fileTree(dir: "libs", include: ["*.jar"])
5959
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
60+
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
61+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9")
62+
implementation("androidx.graphics:graphics-core:1.0.0-alpha04")
6063
implementation 'androidx.core:core-ktx:1.6.0'
6164
implementation 'androidx.appcompat:appcompat:1.3.1'
6265
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'

RenderScriptMigrationSample/app/src/main/cpp/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ add_library(rs_migration_jni
2323
ComputePipeline.cpp
2424
ImageProcessor.cpp
2525
VulkanContext.cpp
26-
VulkanResources.cpp)
26+
VulkanResources.cpp
27+
GLDebug.cpp)
2728

2829
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1z")
2930
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Weverything -Werror")
@@ -38,8 +39,12 @@ find_library(libandroid android)
3839
find_library(liblog log)
3940
find_library(libjnigraphics jnigraphics)
4041
find_library(libvulkan vulkan)
42+
find_library(libgl GLESv3)
43+
find_library(libegl EGL)
4144
target_link_libraries(rs_migration_jni
4245
${libandroid}
4346
${liblog}
4447
${libjnigraphics}
45-
${libvulkan})
48+
${libvulkan}
49+
${libgl}
50+
${libegl})
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (C) 2023 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <android/log.h>
18+
#include <jni.h>
19+
#include <GLES3/gl32.h>
20+
#include <EGL/egl.h>
21+
22+
// our JNICallback struct.
23+
static thread_local struct JNICallback {
24+
JNIEnv *env;
25+
jmethodID mid;
26+
jobject obj;
27+
} jniCallback;
28+
29+
static void openGLMessageCallback(GLenum source, GLenum type, GLuint id,
30+
GLenum severity, GLsizei, const GLchar* message,
31+
const void* userParam)
32+
{
33+
if ( nullptr != userParam ) {
34+
const JNICallback* callback = reinterpret_cast<const JNICallback*>(userParam);
35+
36+
jstring jniMessageString = callback->env->NewStringUTF(message);
37+
callback->env->CallVoidMethod(callback->obj, callback->mid,
38+
static_cast<jint>(source),
39+
static_cast<jint>(type),
40+
static_cast<jint>(id),
41+
static_cast<jint>(severity),
42+
jniMessageString );
43+
}
44+
}
45+
46+
// There's no way to do this in managed code, so here's something to help out those devs that
47+
// want some more debug info.
48+
extern "C"
49+
JNIEXPORT jboolean JNICALL
50+
Java_com_android_example_rsmigration_GLSLImageProcessorKt_EnableDebugLogging(JNIEnv *env,
51+
jclass,
52+
jobject callback) {
53+
if ( env ) {
54+
auto debugCallback = reinterpret_cast<void (*)(void *, void *)>(eglGetProcAddress("glDebugMessageCallback"));
55+
if (debugCallback) {
56+
// enable debug output
57+
glEnable(GL_DEBUG_OUTPUT);
58+
// call back on the same thread
59+
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
60+
61+
jniCallback.env = env;
62+
jclass cls = env->GetObjectClass(callback);
63+
jniCallback.mid = env->GetMethodID(cls, "onMessage","(IIIILjava/lang/String;)V");
64+
jniCallback.obj = env->NewWeakGlobalRef(callback);
65+
debugCallback(reinterpret_cast<void*>(openGLMessageCallback), &jniCallback);
66+
}
67+
}
68+
return false;
69+
}

0 commit comments

Comments
 (0)