-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.cpp
More file actions
365 lines (322 loc) · 12.5 KB
/
main.cpp
File metadata and controls
365 lines (322 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <jni.h>
#include <game-activity/GameActivity.cpp>
#include <game-text-input/gametextinput.cpp>
#include "sds/sds_fstream.h"
#include <sys/stat.h>
#define VK_NO_PROTOTYPES
#include <dlfcn.h>
#include <vulkan/vulkan.h>
#include "adrenotools/include/adrenotools/driver.h"
/** Copies a file from one folder into another. Dst folder must exist.
e.g.
/src/folder/my_file.jpg
/dst/folder/my_file.jpg
@param srcFolder
Source folder e.g. "/src/folder/"
@param dstFolder
Destination folder e.g. "/dst/folder/"
Folder must exist.
@param filename
Name of the file to copy. e.g. "my_file.jpg"
*/
void copyFile( const std::string &srcFolder, const std::string &dstFolder, const char *filename )
{
sds::fstream inputFile( srcFolder + filename, sds::fstream::InputEnd, false );
if( inputFile.is_open() )
{
const size_t sizeBytes = inputFile.getFileSize( false );
inputFile.seek( 0, sds::fstream::beg );
std::vector<char> fileData;
fileData.resize( sizeBytes );
inputFile.read( fileData.data(), sizeBytes );
sds::fstream outputFile( dstFolder + filename, sds::fstream::OutputDiscard );
if( outputFile.is_open() )
{
outputFile.write( fileData.data(), sizeBytes );
}
else
{
__android_log_print( ANDROID_LOG_ERROR, "DriverReplacer", "Could not write to file %s!\n",
( dstFolder + filename ).c_str() );
}
}
else
{
__android_log_print( ANDROID_LOG_ERROR, "DriverReplacer", "Could not open file %s!\n",
( srcFolder + filename ).c_str() );
}
}
/** Opens the handle to the vulkan library and loads a VkInstance and dumps driver information.
VkInstance is destroyed before return.
@param libVulkan
Handle to Vulkan so.
*/
void testVulkan( void *libVulkan )
{
PFN_vkCreateInstance vkCreateInstance =
reinterpret_cast<PFN_vkCreateInstance>( dlsym( libVulkan, "vkCreateInstance" ) );
PFN_vkDestroyInstance vkDestroyInstance =
reinterpret_cast<PFN_vkDestroyInstance>( dlsym( libVulkan, "vkDestroyInstance" ) );
/*PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr =
reinterpret_cast<PFN_vkGetInstanceProcAddr>( dlsym( libVulkan, "vkGetInstanceProcAddr" ) );*/
VkApplicationInfo appInfo = {};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = "AdrenoToolsExample";
appInfo.applicationVersion = 1;
appInfo.pEngineName = "AdrenoToolsExample";
appInfo.engineVersion = 1;
appInfo.apiVersion = VK_API_VERSION_1_0;
// Create Vulkan instance
VkInstanceCreateInfo instanceCreateInfo = {};
instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instanceCreateInfo.pApplicationInfo = &appInfo;
// PFN_vkEnumeratePhysicalDevices vkEnumeratePhysicalDevices =
// reinterpret_cast<PFN_vkEnumeratePhysicalDevices>( vkGetInstanceProcAddr( instance,
//"vkEnumeratePhysicalDevices" ) );
PFN_vkEnumeratePhysicalDevices vkEnumeratePhysicalDevices =
reinterpret_cast<PFN_vkEnumeratePhysicalDevices>(
dlsym( libVulkan, "vkEnumeratePhysicalDevices" ) );
PFN_vkGetPhysicalDeviceProperties vkGetPhysicalDeviceProperties =
reinterpret_cast<PFN_vkGetPhysicalDeviceProperties>(
dlsym( libVulkan, "vkGetPhysicalDeviceProperties" ) );
VkInstance instance;
vkCreateInstance( &instanceCreateInfo, nullptr, &instance );
uint32_t numDevices = 1u;
vkEnumeratePhysicalDevices( instance, &numDevices, NULL );
if( numDevices == 0 )
{
__android_log_print( ANDROID_LOG_ERROR, "DriverReplacer", "NO VK DEVICES!" );
}
else
{
__android_log_print( ANDROID_LOG_INFO, "DriverReplacer", "YES VK DEVICES!" );
std::vector<VkPhysicalDevice> pd;
pd.resize( numDevices );
vkEnumeratePhysicalDevices( instance, &numDevices, pd.data() );
for( uint32_t i = 0u; i < numDevices; ++i )
{
VkPhysicalDeviceProperties deviceProps;
vkGetPhysicalDeviceProperties( pd[i], &deviceProps );
// Generic version routine that matches SaschaWillems's VulkanCapsViewer
const uint32_t driverVersionMajor = ( deviceProps.driverVersion >> 22u ) & 0x3ff;
const uint32_t driverVersionMinor = ( deviceProps.driverVersion >> 12u ) & 0x3ff;
const uint32_t driverVersionRelease = ( deviceProps.driverVersion ) & 0xfff;
__android_log_print( ANDROID_LOG_INFO, "DriverReplacer",
"Device %i: %s.\n"
"Vulkan API %i.%i.%i\n"
"Driver Version: %i.%i.%i (%u)",
i, deviceProps.deviceName, //
VK_API_VERSION_MAJOR( deviceProps.apiVersion ),
VK_API_VERSION_MINOR( deviceProps.apiVersion ),
VK_API_VERSION_PATCH( deviceProps.apiVersion ), //
driverVersionMajor, driverVersionMinor, driverVersionRelease,
deviceProps.driverVersion );
}
}
vkDestroyInstance( instance, nullptr );
instance = 0;
}
/// Loads Vulkan "the proper way".
void loadOriginalVulkan()
{
void *module = dlopen( "libvulkan.so.1", RTLD_NOW | RTLD_LOCAL );
if( !module )
module = dlopen( "libvulkan.so", RTLD_NOW | RTLD_LOCAL );
if( !module )
{
__android_log_print( ANDROID_LOG_ERROR, "DriverReplacer",
"Could not open original Vulkan: %s!\n", dlerror() );
return;
}
__android_log_print( ANDROID_LOG_INFO, "DriverReplacer",
"====== START TESTING ORIGINAL VULKAN DRIVER ======" );
testVulkan( module );
__android_log_print( ANDROID_LOG_INFO, "DriverReplacer",
"====== END TESTING ORIGINAL VULKAN DRIVER ======" );
dlclose( module );
}
/** Loads Vulkan using driver injection.
@param path
Folder where the *.so of driverName is stored in.
This path must be internal to the app, otherwise there will be permission errors.
@param hooksDir
This folder MUST be the one returned by getNativeLibraryDir().
@param driverName
Name of the driver library, e.g. "libvulkan_freedreno.so", "vulkan.msm8937.so", etc.
*/
void replaceDriver( const std::string &path, const char *hooksDir, const char *driverName )
{
mkdir( ( path + "temp" ).c_str(), S_IRWXU | S_IRWXG );
// String nativeLibDir = getApplicationLibraryDir( appInfo );
// std::string nativeLibDir = GetJavaString( env, jNativeLibDir );
void *libVulkan = adrenotools_open_libvulkan( RTLD_NOW | RTLD_LOCAL, ADRENOTOOLS_DRIVER_CUSTOM,
( path + "temp" ).c_str(), //
hooksDir, //
path.c_str(), //
driverName, nullptr, nullptr );
if( !libVulkan )
{
if( !libVulkan )
{
__android_log_print( ANDROID_LOG_ERROR, "DriverReplacer",
"Could not load vulkan library : %s!\n", dlerror() );
}
}
else
{
__android_log_print( ANDROID_LOG_INFO, "DriverReplacer", "DRIVER REPLACEMENT LOADED" );
testVulkan( libVulkan );
}
}
extern "C" {
#include <game-activity/native_app_glue/android_native_app_glue.c>
}
std::string getNativeLibraryDir( struct android_app *app )
{
JNIEnv *env = nullptr;
app->activity->vm->AttachCurrentThread( &env, nullptr );
jclass contextClassDef = env->GetObjectClass( app->activity->javaGameActivity );
const jmethodID getApplicationContextMethod =
env->GetMethodID( contextClassDef, "getApplicationContext", "()Landroid/content/Context;" );
const jmethodID getApplicationInfoMethod = env->GetMethodID(
contextClassDef, "getApplicationInfo", "()Landroid/content/pm/ApplicationInfo;" );
jobject contextObject =
env->CallObjectMethod( app->activity->javaGameActivity, getApplicationContextMethod );
jobject applicationInfoObject = env->CallObjectMethod( contextObject, getApplicationInfoMethod );
jclass applicationInfoObjectDef = env->GetObjectClass( applicationInfoObject );
const jfieldID nativeLibraryDirField =
env->GetFieldID( applicationInfoObjectDef, "nativeLibraryDir", "Ljava/lang/String;" );
jstring nativeLibraryDirJStr =
(jstring)env->GetObjectField( applicationInfoObject, nativeLibraryDirField );
const char *textCStr = env->GetStringUTFChars( nativeLibraryDirJStr, nullptr );
const std::string libDir = textCStr;
env->ReleaseStringUTFChars( nativeLibraryDirJStr, textCStr );
env->DeleteLocalRef( nativeLibraryDirJStr );
env->DeleteLocalRef( applicationInfoObjectDef );
env->DeleteLocalRef( applicationInfoObject );
env->DeleteLocalRef( contextObject );
env->DeleteLocalRef( contextClassDef );
app->activity->vm->DetachCurrentThread();
return libDir;
}
extern "C" {
/*!
* Handles commands sent to this Android application
* @param pApp the app the commands are coming from
* @param cmd the command to handle
*/
void handle_cmd( android_app *pApp, int32_t cmd )
{
switch( cmd )
{
case APP_CMD_INIT_WINDOW:
// A new window is created, associate a renderer with it. You may replace this with a
// "game" class if that suits your needs. Remember to change all instances of userData
// if you change the class here as a reinterpret_cast is dangerous this in the
// android_main function and the APP_CMD_TERM_WINDOW handler case.
break;
case APP_CMD_TERM_WINDOW:
// The window is being destroyed. Use this to clean up your userData to avoid leaking
// resources.
//
// We have to check if userData is assigned just in case this comes in really quickly
break;
default:
break;
}
}
/*!
* Enable the motion events you want to handle; not handled events are
* passed back to OS for further processing. For this example case,
* only pointer and joystick devices are enabled.
*
* @param motionEvent the newly arrived GameActivityMotionEvent.
* @return true if the event is from a pointer or joystick device,
* false for all other input devices.
*/
bool motion_event_filter_func( const GameActivityMotionEvent *motionEvent )
{
auto sourceClass = motionEvent->source & AINPUT_SOURCE_CLASS_MASK;
return ( sourceClass == AINPUT_SOURCE_CLASS_POINTER || sourceClass == AINPUT_SOURCE_CLASS_JOYSTICK );
}
/*!
* This the main entry point for a native activity
*/
void android_main( struct android_app *pApp )
{
//#define USE_QUALCOMM_DRIVER
// std::string srcFolder = "/storage/emulated/0/Android/data/com.example.adrenotoolstest2/files/";
// std::string dstFolder = "/data/user/0/com.example.adrenotoolstest2/files/";
std::string srcFolder = pApp->activity->externalDataPath ? pApp->activity->externalDataPath : "";
std::string dstFolder = pApp->activity->internalDataPath ? pApp->activity->internalDataPath : "";
#ifndef USE_QUALCOMM_DRIVER
const char *vulkanLibName = "libvulkan_freedreno.so";
#else
const char *vulkanLibName = "vulkan.ad0667.so";
#endif
if( !srcFolder.empty() && srcFolder.back() != '/' )
srcFolder.push_back( '/' );
if( !dstFolder.empty() && dstFolder.back() != '/' )
dstFolder.push_back( '/' );
const std::string nativeLibraryDir = getNativeLibraryDir( pApp );
__android_log_print( ANDROID_LOG_INFO, "DriverReplacer",
"Folders:\n"
"SRC folder: %s\n"
"DST folder: %s\n"
"JNI libdir: %s\n",
srcFolder.c_str(), dstFolder.c_str(), nativeLibraryDir.c_str() );
#ifdef USE_QUALCOMM_DRIVER
# if 0
// Failed attempt at getting PowerVR to work.
const char *filesToCopy[] = { "libEGL_powervr.so", "libGLESv1_CM_powervr.so",
"libGLESv2_powervr.so", "libglslcompiler.so",
"libIMGegl.so", "libsrv_um.so",
"libufwriter.so", "libusc.so" };
# endif
# if 1
// Vulkan drivers from Qualcomm. i.e. v667-patched-adpkg.zip.
const char *filesToCopy[] = { "notadreno_utils.so", "notdmabufheap.so", "notgsl.so",
"notllvm-glnext.so", "notllvm-qgl.so" };
# endif
for( size_t i = 0u; i < sizeof( filesToCopy ) / sizeof( filesToCopy[0] ); ++i )
copyFile( srcFolder, dstFolder, filesToCopy[i] );
for( size_t i = 0u; i < sizeof( filesToCopy ) / sizeof( filesToCopy[0] ); ++i )
{
void *lib =
dlopen( ( std::string( dstFolder ) + filesToCopy[i] ).c_str(), RTLD_NOW | RTLD_LOCAL );
if( !lib )
{
__android_log_print( ANDROID_LOG_ERROR, "DriverReplacer", "Could not load %s!. Reason: %s",
filesToCopy[i], dlerror() );
}
else
{
__android_log_print( ANDROID_LOG_INFO, "DriverReplacer", "Loaded %s!", filesToCopy[i] );
}
}
#endif
loadOriginalVulkan();
copyFile( srcFolder, dstFolder, vulkanLibName );
replaceDriver( dstFolder, nativeLibraryDir.c_str(), vulkanLibName );
// Register an event handler for Android events
pApp->onAppCmd = handle_cmd;
// Set input event filters (set it to NULL if the app wants to process all inputs).
// Note that for key inputs, this example uses the default default_key_filter()
// implemented in android_native_app_glue.c.
android_app_set_motion_event_filter( pApp, motion_event_filter_func );
// This sets up a typical game/event loop. It will run until the app is destroyed.
int events;
android_poll_source *pSource;
do
{
// Process all pending events before running game logic.
if( ALooper_pollAll( 0, nullptr, &events, (void **)&pSource ) >= 0 )
{
if( pSource )
{
pSource->process( pApp, pSource );
}
}
} while( !pApp->destroyRequested );
}
}