Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multi-window replay on Android #1912

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion android/framework/application/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
add_library(gfxrecon_application STATIC "")

target_sources(gfxrecon_application
PRIVATE
PUBLIC
${GFXRECON_SOURCE_DIR}/framework/application/android_context.h
${GFXRECON_SOURCE_DIR}/framework/application/android_context.cpp
${GFXRECON_SOURCE_DIR}/framework/application/android_window.h
Expand All @@ -10,6 +10,7 @@ target_sources(gfxrecon_application
${GFXRECON_SOURCE_DIR}/framework/application/application.cpp
${GFXRECON_SOURCE_DIR}/framework/application/wsi_context.h
${GFXRECON_SOURCE_DIR}/framework/application/wsi_context.cpp
${GFXRECON_SOURCE_DIR}/framework/application/android_jni.cpp
)

target_include_directories(gfxrecon_application
Expand Down
2 changes: 1 addition & 1 deletion android/scripts/gfxrecon.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

# Application info
app_name = 'com.lunarg.gfxreconstruct.replay'
app_activity = '"com.lunarg.gfxreconstruct.replay/android.app.NativeActivity"'
app_activity = '"com.lunarg.gfxreconstruct.replay/.ReplayActivity"'
app_action = 'android.intent.action.MAIN'
app_category = 'android.intent.category.LAUNCHER'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
android:theme="@style/AppTheme"
android:supportsRtl="true"
android:extractNativeLibs="true"
android:hasCode="false">
<activity android:name="android.app.NativeActivity"
android:hasCode="true">
<activity android:name=".ReplayActivity"
android:exported="true"
android:configChanges="orientation|screenSize|keyboard|keyboardHidden|screenLayout"
android:screenOrientation="unspecified">
Expand Down
7 changes: 7 additions & 0 deletions android/tools/replay/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
Expand Down
12 changes: 12 additions & 0 deletions android/tools/replay/res/values-v27/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="@android:style/Theme.NoTitleBar.Fullscreen">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>

<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
** Copyright (c) 2025 LunarG, Inc.
** Copyright (c) 2025 Arm Limited and/or its affiliates <[email protected]>
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and associated documentation files (the "Software"),
** to deal in the Software without restriction, including without limitation
** the rights to use, copy, modify, merge, publish, distribute, sublicense,
** and/or sell copies of the Software, and to permit persons to whom the
** Software is furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in
** all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
** DEALINGS IN THE SOFTWARE.
*/

package com.lunarg.gfxreconstruct.replay;
import java.util.List;
import java.util.ArrayList;
import android.app.NativeActivity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.view.ViewGroup.LayoutParams;
import android.view.Surface;
import android.util.Log;
import android.content.Context;
import android.view.View;

public class ReplayActivity extends NativeActivity
{
private FrameLayout mFrameLayout;
private static final String TAG = "gfxrecon";
private Surface mSurface;
public native void setSurface(Surface surface);
private List<VKSurfaceView> mSurfaceviewList = new ArrayList<VKSurfaceView>();

@Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

// Create a FrameLayout to hold SurfaceView instances
mFrameLayout = new FrameLayout(this);
setContentView(mFrameLayout);

System.loadLibrary("gfxrecon-replay");
}

private class VKSurfaceView extends SurfaceView implements SurfaceHolder.Callback
{
private int width;
private int height;

public VKSurfaceView(Context context, int w, int h)
{
super(context);

width = w;
height = h;

SurfaceHolder holder = getHolder();
holder.addCallback(this);
}

@Override public void surfaceCreated(SurfaceHolder holder)
{
mSurface = holder.getSurface();
Log.i(TAG, "SurfaceHolder.Callback: surfaceCreated:" + mSurface);
}

@Override public void surfaceDestroyed(SurfaceHolder holder)
{
mSurface = holder.getSurface();
Log.i(TAG, "SurfaceHolder.Callback: surfaceDestroyed:" + mSurface);
}

@Override public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
{
mSurface = holder.getSurface();
Log.i(TAG, "SurfaceHolder.Callback: surfaceChanged:" + mSurface + " " + w + " " + h);
}
}

public void addNewView(int width, int height)
{
// Create a new SurfaceView
final Context context = this;
final int wid = width;
final int hei = height;
mSurface = null;
runOnUiThread(new Runnable() {
@Override public void run()
{
System.loadLibrary("gfxrecon-replay");
VKSurfaceView newSurfaceView = new VKSurfaceView(context, wid, hei);
mFrameLayout.addView(newSurfaceView,
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Log.i(TAG,
"Create a new surface view:"
+ " width:" + wid + " height:" + hei);
mSurfaceviewList.add(newSurfaceView);
}
});
while (mSurface == null)
{
try
{
Thread.sleep(100);
}
catch (Exception e)
{
Log.w(TAG, "Create new surface failed");
e.printStackTrace();
}
}
setSurface(mSurface);
}

private void removeOneView(int surface_idx)
{
final int sur_idx = surface_idx;
runOnUiThread(new Runnable() {
@Override public void run()
{
if (mFrameLayout != null)
{
Log.i(TAG, "Remove one view");
mFrameLayout.removeView(mSurfaceviewList.get(sur_idx));
}
else
{
Log.w(TAG, "View container has been destroyed!");
}
}
});
}
}
38 changes: 38 additions & 0 deletions framework/application/android_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,43 @@ void AndroidContext::SetOrientation(ScreenOrientation orientation)
}
}

void AndroidContext::requestNativeWindow(int width, int height)
{
JavaVM* jni_vm = nullptr;
jobject jni_activity = nullptr;
JNIEnv* env = nullptr;
if ((android_app_ != nullptr) && (android_app_->activity != nullptr))
{
jni_vm = android_app_->activity->vm;
jni_activity = android_app_->activity->clazz;
}
if ((jni_vm != nullptr) && (jni_activity != 0) && (jni_vm->AttachCurrentThread(&env, nullptr) == JNI_OK))
{
jclass object_class = env->GetObjectClass(jni_activity);
jmethodID createsufaceview = env->GetMethodID(object_class, "addNewView", "(II)V");
env->CallVoidMethod(jni_activity, createsufaceview, width, height);
jni_vm->DetachCurrentThread();
}
}

void AndroidContext::destroyNativeWindow(int window_index)
{
JavaVM* jni_vm = nullptr;
jobject jni_activity = nullptr;
JNIEnv* env = nullptr;
if ((android_app_ != nullptr) && (android_app_->activity != nullptr))
{
jni_vm = android_app_->activity->vm;
jni_activity = android_app_->activity->clazz;
}
if ((jni_vm != nullptr) && (jni_activity != 0) && (jni_vm->AttachCurrentThread(&env, nullptr) == JNI_OK))
{
jclass object_class = env->GetObjectClass(jni_activity);
jmethodID removesurfaceview = env->GetMethodID(object_class, "removeOneView", "(I)V");
env->CallVoidMethod(jni_activity, removesurfaceview, window_index);
jni_vm->DetachCurrentThread();
}
}

GFXRECON_END_NAMESPACE(application)
GFXRECON_END_NAMESPACE(gfxrecon)
4 changes: 4 additions & 0 deletions framework/application/android_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class AndroidContext : public WsiContext

void SetOrientation(ScreenOrientation orientation);

void requestNativeWindow(int width, int height);

void destroyNativeWindow(int window_index);

private:
std::unique_ptr<AndroidWindow> window_{};
struct android_app* android_app_{};
Expand Down
36 changes: 36 additions & 0 deletions framework/application/android_jni.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
** Copyright (c) 2025 LunarG, Inc.
** Copyright (c) 2025 Arm Limited and/or its affiliates <[email protected]>
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and associated documentation files (the "Software"),
** to deal in the Software without restriction, including without limitation
** the rights to use, copy, modify, merge, publish, distribute, sublicense,
** and/or sell copies of the Software, and to permit persons to whom the
** Software is furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in
** all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
** DEALINGS IN THE SOFTWARE.
*/

#include <jni.h>
#include <android/log.h>
#include "application/android_window.h"
#include <android/native_window_jni.h>
#include "util/logging.h"

extern "C" JNIEXPORT void JNICALL Java_com_lunarg_gfxreconstruct_replay_ReplayActivity_setSurface(JNIEnv* env,
jobject obj,
jobject surface)
{
gfxrecon::application::tmp_window = ANativeWindow_fromSurface(env, surface);
GFXRECON_LOG_INFO("Created new window %p", gfxrecon::application::tmp_window);
}
19 changes: 16 additions & 3 deletions framework/application/android_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

GFXRECON_BEGIN_NAMESPACE(gfxrecon)
GFXRECON_BEGIN_NAMESPACE(application)
ANativeWindow* tmp_window = nullptr;

AndroidWindow::AndroidWindow(AndroidContext* android_context, ANativeWindow* window) :
android_context_(android_context), window_(window), width_(0), height_(0), pre_transform_(0)
Expand Down Expand Up @@ -158,13 +159,25 @@ decode::Window* AndroidWindowFactory::Create(
GFXRECON_UNREFERENCED_PARAMETER(height);
GFXRECON_UNREFERENCED_PARAMETER(force_windowed);

return android_context_->GetWindow();
tmp_window = nullptr;
android_context_->requestNativeWindow(width, height);
AndroidWindow* tmpwin = nullptr;
if (tmp_window != nullptr)
{
tmpwin = new AndroidWindow(android_context_, tmp_window);
GFXRECON_LOG_INFO("Got android window %p", tmp_window);
}
else
{
GFXRECON_LOG_WARNING("Get android window failed");
}
return tmpwin;
}

void AndroidWindowFactory::Destroy(decode::Window* window)
{
// Android currently has a single window whose lifetime is managed by AndroidContext.
GFXRECON_UNREFERENCED_PARAMETER(window);
int32_t windowidx = created_window_.at(window);
android_context_->destroyNativeWindow(windowidx);
}

VkBool32 AndroidWindowFactory::GetPhysicalDevicePresentationSupport(const encode::VulkanInstanceTable* table,
Expand Down
2 changes: 2 additions & 0 deletions framework/application/android_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
#include "util/platform.h"

#include <android_native_app_glue.h>
#include <jni.h>

GFXRECON_BEGIN_NAMESPACE(gfxrecon)
GFXRECON_BEGIN_NAMESPACE(application)
extern ANativeWindow* tmp_window;

class AndroidWindow : public decode::Window
{
Expand Down
3 changes: 3 additions & 0 deletions framework/decode/vulkan_swapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ VkResult VulkanSwapchain::CreateSurface(VkResult orig
return VK_ERROR_UNKNOWN;
}

window_factory->created_window_.emplace(window, window_index_);
++window_index_;

result = window->CreateSurface(instance_table_, instance, flags, replay_surface);

if ((result == VK_SUCCESS) && (replay_surface != nullptr))
Expand Down
1 change: 1 addition & 0 deletions framework/decode/vulkan_swapchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class VulkanSwapchain
application::Application* application_{ nullptr };
ActiveWindows active_windows_;
int32_t create_surface_count_{ 0 };
int32_t window_index_{ 0 };
VulkanSwapchainOptions swapchain_options_;
};

Expand Down
3 changes: 3 additions & 0 deletions framework/decode/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "vulkan/vulkan.h"

#include <string>
#include <unordered_map>

GFXRECON_BEGIN_NAMESPACE(gfxrecon)
GFXRECON_BEGIN_NAMESPACE(decode)
Expand Down Expand Up @@ -108,6 +109,8 @@ class WindowFactory
virtual VkBool32 GetPhysicalDevicePresentationSupport(const encode::VulkanInstanceTable* table,
VkPhysicalDevice physical_device,
uint32_t queue_family_index) = 0;

std::unordered_map<Window*, int32_t> created_window_;
};

GFXRECON_END_NAMESPACE(decode)
Expand Down