1 /*
2  * Copyright (C) 2021 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 package android.rootlessgpudebug.app;
18 
19 import android.app.Service;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.os.IBinder;
24 import android.util.Log;
25 
26 import java.lang.Override;
27 
28 
29 public class RootlessGpuDebugService extends Service {
30 
31     private static final String TAG = RootlessGpuDebugService.class.getSimpleName();
32 
33     static {
34         System.loadLibrary("ctsgputools_jni");
35     }
36 
37     @Override
onBind(Intent intent)38     public IBinder onBind(Intent intent) {
39         // We don't provide binding, so return null
40         return null;
41     }
42 
43     @Override
onStartCommand(Intent intent, int flags, int startId)44     public int onStartCommand(Intent intent, int flags, int startId) {
45 
46         // The target API is provided via Intent extras
47         String API = null;
48 
49         if (intent == null) {
50             throw new AssertionError("No Intent provided to " + TAG);
51         }
52 
53         Bundle bundle = intent.getExtras();
54 
55         // Ensure the service was started with extras
56         if (bundle == null) {
57             throw new AssertionError("Failed to get Intent extras for " + TAG);
58         }
59 
60         API = bundle.getString("API");
61 
62         // Without an API to target, we're done
63         if (API == null) {
64             throw new AssertionError("No API provided for " + TAG);
65         }
66 
67         // Only three combinations are expected
68         Boolean supportedApi = API.equals("Vulkan") ||
69                                API.equals("GLES") ||
70                                API.equals("Both");
71         if (!supportedApi) {
72             throw new AssertionError("Unsupported API " + API + " in " + TAG);
73         }
74 
75         // For each choice, init the target API
76         if (API.equals("Vulkan") || API.equals("Both")) {
77             String result = nativeInitVulkan();
78             Log.i(TAG, result);
79         }
80 
81         if (API.equals("GLES") || API.equals("Both")) {
82             String result = nativeInitGLES();
83             Log.i(TAG, result);
84         }
85 
86         // Mark service completion
87         Log.i(TAG, "RootlessGpuDebugService complete");
88 
89         // Don't try to restart when this is shut down
90         return START_NOT_STICKY;
91     }
92 
nativeInitVulkan()93     private static native String nativeInitVulkan();
nativeInitGLES()94     private static native String nativeInitGLES();
95 }
96 
97