1 /*
2  * Copyright (C) 2015 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 package com.android.compatibility.common.deviceinfo;
17 
18 import android.os.Bundle;
19 
20 import com.android.compatibility.common.util.DeviceInfoStore;
21 
22 import java.io.IOException;
23 
24 import java.util.List;
25 import java.util.Set;
26 
27 /**
28  * Graphics device info collector.
29  */
30 public final class GraphicsDeviceInfo extends DeviceInfo {
31 
32     private static final String LOG_TAG = "GraphicsDeviceInfo";
33 
34     // Java generics won't handle basic types, can't simplify
storeValue(DeviceInfoStore store, String name, float[] valueArray, boolean dynamicArray)35     private static void storeValue(DeviceInfoStore store, String name, float[] valueArray,
36                                    boolean dynamicArray) throws IOException {
37         if (valueArray.length == 1 && !dynamicArray) {
38             store.addResult(name, valueArray[0]);
39         } else {
40             store.addArrayResult(name, valueArray);
41         }
42     }
43 
storeValue(DeviceInfoStore store, String name, int[] valueArray, boolean dynamicArray)44     private static void storeValue(DeviceInfoStore store, String name, int[] valueArray,
45                                    boolean dynamicArray) throws IOException {
46         if (valueArray.length == 1 && !dynamicArray) {
47             store.addResult(name, valueArray[0]);
48         } else {
49             store.addArrayResult(name, valueArray);
50         }
51     }
52 
storeValue(DeviceInfoStore store, String name, long[] valueArray, boolean dynamicArray)53     private static void storeValue(DeviceInfoStore store, String name, long[] valueArray,
54                                    boolean dynamicArray) throws IOException {
55         if (valueArray.length == 1 && !dynamicArray) {
56             store.addResult(name, valueArray[0]);
57         } else {
58             store.addArrayResult(name, valueArray);
59         }
60     }
61 
62     @Override
collectDeviceInfo(DeviceInfoStore store)63     protected void collectDeviceInfo(DeviceInfoStore store) throws Exception {
64         GlesStubActivity stubActivity = GraphicsDeviceInfo.this.launchActivity(
65                 "com.android.compatibility.common.deviceinfo",
66                 GlesStubActivity.class,
67                 new Bundle());
68         stubActivity.waitForActivityToFinish();
69 
70         store.addResult("gl_version", stubActivity.getGlVersion());
71         store.addResult("vendor", stubActivity.getVendor());
72         store.addResult("renderer", stubActivity.getRenderer());
73 
74         store.addListResult("gl_texture", stubActivity.getCompressedTextureFormats());
75         store.addListResult("gl_extension", stubActivity.getOpenGlExtensions());
76 
77         Set<String> variables = stubActivity.getImplementationVariableNames();
78         for (String name : variables) {
79             Object value = stubActivity.getImplementationVariable(name);
80             String lowerCaseName = name.toLowerCase();
81             if (lowerCaseName.equals("gl_version")) {
82                 lowerCaseName = "gl_version_real";
83             }
84 
85             if (value != null) {
86                 boolean dynamicArray = stubActivity.isDynamicArrayVariable(name);
87                 if (value instanceof String) {
88                     store.addResult(lowerCaseName, (String)value);
89                 } else if (value instanceof float[]) {
90                     storeValue(store, lowerCaseName, (float[])value, dynamicArray);
91                 } else if (value instanceof long[]) {
92                     storeValue(store, lowerCaseName, (long[])value, dynamicArray);
93                 } else {
94                     storeValue(store, lowerCaseName, (int[])value, dynamicArray);
95                 }
96             }
97         }
98 
99         store.addListResult("egl_extension", stubActivity.getEglExtensions());
100     }
101 }
102