Skip to content

Commit

Permalink
wip: start working on vaapi glx interop
Browse files Browse the repository at this point in the history
  • Loading branch information
silenium-dev committed Jul 28, 2024
1 parent 07e8735 commit c63948b
Show file tree
Hide file tree
Showing 18 changed files with 525 additions and 46 deletions.
9 changes: 8 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,23 @@ dependencies {
// With compose.desktop.common you will also lose @Preview functionality
implementation(compose.desktop.currentOs)
implementation(libs.compose.gl)
implementation(libs.compose.gl.natives)
implementation(libs.jni.utils)
implementation(libs.bundles.kotlinx)
implementation(libs.bundles.logging)
implementation(libs.slf4j.api) // for logging
if (deployNative) {
implementation(project(":native"))
}
implementation(kotlin("reflect"))
// implementation(libs.bundles.skiko) {
// version {
// strictly(libs.skiko.awt.runtime.linux.x64.get().version!!)
// }
// }

testImplementation(libs.bundles.kotest)
testImplementation(libs.mockk)
testImplementation(libs.logback.classic)
}

configurations.all {
Expand Down
19 changes: 12 additions & 7 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ kotlinx-serialization = "1.7.1"
kotlinx-datetime = "0.6.0"

compose = "1.6.11"
compose-gl = "0.2.6"
compose-gl = "0.3.0"
jni-utils = "0.1.5"
skiko = "0.8.10-egl"

slf4j = "2.0.13"
logback = "1.5.6"
Expand All @@ -15,7 +17,6 @@ kotest = "5.9.1"
mockk = "1.13.12"

idea-ext = "1.1.8"
jni-utils = "0.1.5"

[libraries]
kotlinx-coroutines-core = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
Expand All @@ -31,9 +32,13 @@ kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-
kotlinx-datetime = { group = "org.jetbrains.kotlinx", name = "kotlinx-datetime", version.ref = "kotlinx-datetime" }

compose-gl = { group = "dev.silenium.compose.gl", name = "compose-gl", version.ref = "compose-gl" }
compose-gl-natives = { group = "dev.silenium.compose.gl", name = "compose-gl-natives-linux-x86_64", version.ref = "compose-gl" }
ffmpeg-natives = { group = "dev.silenium.libs", name = "ffmpeg-natives", version = "7.0+0.2.0" }
jni-utils = { group = "dev.silenium.libs.jni", name = "jni-utils", version.ref = "jni-utils" }

skiko-awt = { group = "org.jetbrains.skiko", name = "skiko-awt", version.ref = "skiko" }
skiko-awt-runtime-linux-x64 = { group = "org.jetbrains.skiko", name = "skiko-awt-runtime-linux-x64", version.ref = "skiko" }

slf4j-api = { group = "org.slf4j", name = "slf4j-api", version.ref = "slf4j" }
logback-classic = { group = "ch.qos.logback", name = "logback-classic", version.ref = "logback" }

Expand Down Expand Up @@ -64,14 +69,14 @@ kotlinx = [
"kotlinx-datetime",
]

logging = [
"slf4j-api",
"logback-classic",
]

kotest = [
"kotest-runner-junit5",
"kotest-property",
"kotest-assertions-core",
"kotest-assertions-json",
]

skiko = [
"skiko-awt",
"skiko-awt-runtime-linux-x64",
]
11 changes: 7 additions & 4 deletions native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
src/cpp/platform/linux/helper/EGL.cpp
src/cpp/platform/linux/helper/EGL.hpp
src/cpp/platform/linux/Errors.cpp
src/cpp/platform/linux/VAGLInteropImage.cpp
src/cpp/platform/linux/VAGLInteropImage.hpp
src/cpp/platform/linux/VAGLRenderInterop.cpp
src/cpp/platform/linux/VAEGLInteropImage.cpp
src/cpp/platform/linux/VAEGLInteropImage.hpp
src/cpp/platform/linux/VAGLXInteropImage.cpp
src/cpp/platform/linux/VAGLXInteropImage.hpp
src/cpp/platform/linux/VAGLXRenderInterop.cpp
src/cpp/platform/linux/VAEGLRenderInterop.cpp
src/cpp/platform/linux/VaapiDecoder.cpp
)
elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
Expand All @@ -81,7 +84,7 @@ endif ()
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_compile_definitions(${PROJECT_NAME} PRIVATE -D_LINUX)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GL REQUIRED IMPORTED_TARGET gl egl libva libva-drm libdrm)
pkg_check_modules(GL REQUIRED IMPORTED_TARGET gl egl libva libva-drm libdrm glx libva-glx)
target_link_libraries(${PROJECT_NAME} PUBLIC PkgConfig::GL)
target_include_directories(${PROJECT_NAME} PUBLIC "${JAVA_HOME}/include/linux")
if (USE_SYSTEM_FFMPEG)
Expand Down
18 changes: 15 additions & 3 deletions native/src/cpp/helper/errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//

#include "errors.hpp"
#include <iostream>

extern "C" {
#include <libavutil/error.h>
Expand Down Expand Up @@ -35,8 +36,7 @@ jobject avResultFailure(JNIEnv *env, const char *operation, const int returnCode
const auto resultClass = env->FindClass("kotlin/Result$Failure");
const auto errorClass = env->FindClass("dev/silenium/compose/av/util/AVException");
const auto errorConstructor = env->GetMethodID(errorClass, "<init>", "(Ljava/lang/String;I)V");
const auto operationChars = env->NewStringUTF(operation);
const auto error = env->NewObject(errorClass, errorConstructor, operationChars, returnCode);
const auto error = env->NewObject(errorClass, errorConstructor, env->NewStringUTF(operation), returnCode);
const auto resultConstructor = env->GetMethodID(resultClass, "<init>", "(Ljava/lang/Throwable;)V");
const auto errorResult = env->NewObject(resultClass, resultConstructor, error);
return errorResult;
Expand All @@ -45,8 +45,14 @@ jobject avResultFailure(JNIEnv *env, const char *operation, const int returnCode
jobject eglResultFailure(JNIEnv *env, const char *operation, const long returnCode) {
const auto resultClass = env->FindClass("kotlin/Result$Failure");
const auto errorClass = env->FindClass("dev/silenium/compose/av/util/EGLException");
const auto errorConstructor = env->GetMethodID(errorClass, "<init>", "(Ljava/lang/String;I)V");
const auto errorConstructor = env->GetMethodID(errorClass, "<init>", "(Ljava/lang/String;J)V");
std::cout << "errorConstructor: " << errorConstructor << std::endl;
const auto error = env->NewObject(errorClass, errorConstructor, env->NewStringUTF(operation), returnCode);
std::cout << "error: " << error << std::endl;
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
env->ExceptionClear();
}
const auto resultConstructor = env->GetMethodID(resultClass, "<init>", "(Ljava/lang/Throwable;)V");
const auto errorResult = env->NewObject(resultClass, resultConstructor, error);
return errorResult;
Expand All @@ -56,7 +62,13 @@ jobject glResultFailure(JNIEnv *env, const char *operation, const int returnCode
const auto resultClass = env->FindClass("kotlin/Result$Failure");
const auto errorClass = env->FindClass("dev/silenium/compose/av/util/GLException");
const auto errorConstructor = env->GetMethodID(errorClass, "<init>", "(Ljava/lang/String;I)V");
std::cout << "errorConstructor: " << errorConstructor << std::endl;
const auto error = env->NewObject(errorClass, errorConstructor, env->NewStringUTF(operation), returnCode);
std::cout << "error: " << error << std::endl;
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
env->ExceptionClear();
}
const auto resultConstructor = env->GetMethodID(resultClass, "<init>", "(Ljava/lang/Throwable;)V");
const auto errorResult = env->NewObject(resultClass, resultConstructor, error);
return errorResult;
Expand Down
18 changes: 9 additions & 9 deletions native/src/cpp/platform/linux/Errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
//

#include "helper/EGL.hpp"
#include <va/va.h>
#include <jni.h>
#include <va/va.h>

extern "C" {
JNIEXPORT jstring JNICALL Java_dev_silenium_compose_av_util_ErrorsKt_eglErrorStringN(
JNIEnv *env,
jobject thiz,
const jlong error
) {
JNIEnv *env,
jobject thiz,
const jlong error) {
return env->NewStringUTF(eglGetErrorString(error));
}

JNIEXPORT jstring JNICALL Java_dev_silenium_compose_av_util_ErrorsKt_vaErrorStringN(
JNIEnv *env,
jobject thiz,
const jint error
) {
JNIEnv *env,
jobject thiz,
const jint error) {
return env->NewStringUTF(vaErrorStr(error));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
// Created by silenium-dev on 7/23/24.
//

#include "VAGLInteropImage.hpp"
#include "VAEGLInteropImage.hpp"

#include <GLES3/gl3.h>

VAGLInteropImage::VAGLInteropImage(const EGLDisplay display,
VAEGLInteropImage::VAEGLInteropImage(const EGLDisplay display,
const std::vector<EGLImageKHR> &images,
const std::vector<unsigned int> &textures,
const std::vector<Swizzles> &swizzles)
: eglDisplay(display), eglImages(images), textures(textures), swizzles(swizzles) {
}

VAGLInteropImage::~VAGLInteropImage() {
VAEGLInteropImage::~VAEGLInteropImage() {
const auto eglDestroyImageKHR = getFunc<PFNEGLDESTROYIMAGEKHRPROC>("eglDestroyImageKHR");
for (const auto &eglImage: eglImages) {
if (eglImage != EGL_NO_IMAGE_KHR) {
Expand All @@ -25,10 +25,10 @@ VAGLInteropImage::~VAGLInteropImage() {
}
}

const std::vector<GLuint> &VAGLInteropImage::planeTextures() const {
const std::vector<GLuint> &VAEGLInteropImage::planeTextures() const {
return textures;
}

const std::vector<Swizzles> &VAGLInteropImage::planeSwizzles() const {
const std::vector<Swizzles> &VAEGLInteropImage::planeSwizzles() const {
return swizzles;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
#include "helper/EGL.hpp"
#include <EGL/eglext.h>

class VAGLInteropImage final : public GLInteropImage {
class VAEGLInteropImage final : public GLInteropImage {
public:
VAGLInteropImage(EGLDisplay display, const std::vector<EGLImageKHR> &images,
VAEGLInteropImage(EGLDisplay display, const std::vector<EGLImageKHR> &images,
const std::vector<unsigned int> &textures,
const std::vector<Swizzles> &swizzles);

VAGLInteropImage(VAGLInteropImage &&) noexcept = default;
VAEGLInteropImage(VAEGLInteropImage &&) noexcept = default;

VAGLInteropImage &operator=(VAGLInteropImage &&) noexcept = default;
VAEGLInteropImage &operator=(VAEGLInteropImage &&) noexcept = default;

VAGLInteropImage(const VAGLInteropImage &) = delete;
VAEGLInteropImage(const VAEGLInteropImage &) = delete;

VAGLInteropImage &operator=(const VAGLInteropImage &) = delete;
VAEGLInteropImage &operator=(const VAEGLInteropImage &) = delete;

~VAGLInteropImage() override;
~VAEGLInteropImage() override;

[[nodiscard]] const std::vector<unsigned int> &planeTextures() const override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// Created by silenium-dev on 7/15/24.
//

#include "VAGLInteropImage.hpp"
#include "render/GLInteropImage.hpp"
#include "VAEGLInteropImage.hpp"
#include "helper/errors.hpp"
#include "render/GLInteropImage.hpp"

#include <jni.h>
#include <GLES3/gl3.h>
#include <GL/gl.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <va/va.h>
Expand All @@ -34,7 +34,7 @@ void closeDrm(const VADRMPRIMESurfaceDescriptor &drm) {

extern "C" {
JNIEXPORT jlong JNICALL
Java_dev_silenium_compose_av_platform_linux_VAGLRenderInteropKt_getVADisplayN(
Java_dev_silenium_compose_av_platform_linux_VAEGLRenderInteropKt_getVADisplayN(
JNIEnv *env, jobject thiz, const jlong frame) {
const auto avFrame = reinterpret_cast<AVFrame *>(frame);
const auto deviceCtx = reinterpret_cast<AVHWFramesContext *>(avFrame->hw_frames_ctx->data)->device_ctx;
Expand All @@ -54,7 +54,7 @@ std::map<AVPixelFormat, std::map<int, std::pair<int, int> > > planeFractions{
};

JNIEXPORT jobject JNICALL
Java_dev_silenium_compose_av_platform_linux_VAGLRenderInteropKt_mapN(JNIEnv *env, jobject thiz,
Java_dev_silenium_compose_av_platform_linux_VAEGLRenderInteropKt_mapN(JNIEnv *env, jobject thiz,
const jint pixelFormat_,
const jlong vaSurface_, const jlong vaDisplay_,
const jlong eglDisplay_) {
Expand Down Expand Up @@ -188,7 +188,7 @@ Java_dev_silenium_compose_av_platform_linux_VAGLRenderInteropKt_mapN(JNIEnv *env
closeDrm(drm);
glBindTexture(GL_TEXTURE_2D, prevTexture);

const auto interopImage = new VAGLInteropImage(eglDisplay, eglImages, textures, swizzles);
const auto interopImage = new VAEGLInteropImage(eglDisplay, eglImages, textures, swizzles);

return resultSuccess(env, reinterpret_cast<jlong>(interopImage));
}
Expand Down
33 changes: 33 additions & 0 deletions native/src/cpp/platform/linux/VAGLXInteropImage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Created by silenium-dev on 7/23/24.
//

#include "VAGLXInteropImage.hpp"

#include <GL/glx.h>
#include <va/va_glx.h>

VAGLXInteropImage::VAGLXInteropImage(
VADisplay display,
VASurfaceID surface,
void *glxSurface,
unsigned int texture,
Swizzles swizzles)
: display(display), surface(surface), glxSurface(glxSurface), texture({texture}), swizzles({swizzles}) {
}

VAGLXInteropImage::~VAGLXInteropImage() {
if (glxSurface != None) {
vaDestroySurfaceGLX(display, glxSurface);
}
vaDestroySurfaces(display, &surface, 1);
glDeleteTextures(1, &texture[0]);
}

const std::vector<GLuint> &VAGLXInteropImage::planeTextures() const {
return texture;
}

const std::vector<Swizzles> &VAGLXInteropImage::planeSwizzles() const {
return swizzles;
}
44 changes: 44 additions & 0 deletions native/src/cpp/platform/linux/VAGLXInteropImage.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// Created by silenium-dev on 7/23/24.
//

#ifndef VAINTEROPIMAGE_HPP
#define VAINTEROPIMAGE_HPP

#include "helper/EGL.hpp"
#include "render/GLInteropImage.hpp"
#include <va/va.h>

class VAGLXInteropImage final : public GLInteropImage {
public:
VAGLXInteropImage(
VADisplay display,
VASurfaceID surface,
void *glxSurfaces,
unsigned int textures,
Swizzles swizzles);

VAGLXInteropImage(VAGLXInteropImage &&) noexcept = default;

VAGLXInteropImage &operator=(VAGLXInteropImage &&) noexcept = default;

VAGLXInteropImage(const VAGLXInteropImage &) = delete;

VAGLXInteropImage &operator=(const VAGLXInteropImage &) = delete;

~VAGLXInteropImage() override;

[[nodiscard]] const std::vector<unsigned int> &planeTextures() const override;

[[nodiscard]] const std::vector<Swizzles> &planeSwizzles() const override;

private:
VADisplay display{nullptr};
VASurfaceID surface{0};
void *glxSurface{};
std::vector<unsigned int> texture{};
std::vector<Swizzles> swizzles{};
};


#endif//VAINTEROPIMAGE_HPP
Loading

0 comments on commit c63948b

Please sign in to comment.