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.app.Activity;
19 import android.app.ActivityManager;
20 import android.app.Instrumentation;
21 import android.content.Context;
22 import android.content.pm.ConfigurationInfo;
23 import android.content.res.Configuration;
24 import android.os.Bundle;
25 import android.view.Window;
26 import android.view.WindowManager;
27 import android.opengl.GLES20;
28 import android.opengl.GLES30;
29 import android.opengl.GLSurfaceView;
30 import android.util.Log;
31 
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Locale;
35 import java.util.HashSet;
36 import java.util.Scanner;
37 import java.util.Set;
38 import java.util.concurrent.CountDownLatch;
39 
40 import javax.microedition.khronos.egl.EGLConfig;
41 import javax.microedition.khronos.opengles.GL10;
42 
43 /** Stub activity to collect data from the GlesView */
44 public final class GlesStubActivity extends Activity {
45 
46     private static final String LOG_TAG = "GlesStubActivity";
47     private int mVersion = -1;
48     private GraphicsDeviceInfo mGraphicsDeviceInfo;
49     private CountDownLatch mDone = new CountDownLatch(1);
50     private HashSet<String> mOpenGlExtensions = new HashSet<String>();
51     private HashSet<String> mFormats = new HashSet<String>();
52     private String mGraphicsVendor;
53     private String mGraphicsRenderer;
54 
55     @Override
onCreate(Bundle bundle)56     public void onCreate(Bundle bundle) {
57         // Dismiss keyguard and keep screen on while this test is on.
58         Window window = getWindow();
59         window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
60                 WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
61                 WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
62 
63         super.onCreate(bundle);
64 
65         window.setFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
66                 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
67 
68         ActivityManager activityManager =
69                 (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
70         ConfigurationInfo info = activityManager.getDeviceConfigurationInfo();
71 
72         mVersion = (info.reqGlEsVersion & 0xffff0000) >> 16;
73 
74         new Thread() {
75             @Override
76             public void run() {
77                 runIterations(mVersion);
78             }
79         }.start();
80     }
81 
82      /**
83      * Wait for this activity to finish gathering information
84      */
waitForActivityToFinish()85     public void waitForActivityToFinish() {
86         try {
87             mDone.await();
88         } catch (InterruptedException e) {
89             // just move on
90         }
91     }
92 
runIterations(int glVersion)93     private void runIterations(int glVersion) {
94         for (int i = 1; i <= glVersion; i++) {
95             final CountDownLatch done = new CountDownLatch(1);
96             final int version = i;
97             GlesStubActivity.this.runOnUiThread(new Runnable() {
98                 @Override
99                 public void run() {
100                     setContentView(new GlesSurfaceView(GlesStubActivity.this, version, done));
101                 }
102             });
103             try {
104                 done.await();
105             } catch (InterruptedException e) {
106                 // just move on
107             }
108         }
109         mDone.countDown();
110     }
111 
getGlVersion()112     int getGlVersion() {
113         return mVersion;
114     }
115 
getOpenGlExtensions()116     List<String> getOpenGlExtensions() {
117         return new ArrayList<>(mOpenGlExtensions);
118     }
119 
addOpenGlExtension(String openGlExtension)120     void addOpenGlExtension(String openGlExtension) {
121         mOpenGlExtensions.add(openGlExtension);
122     }
123 
getCompressedTextureFormats()124     List<String> getCompressedTextureFormats() {
125         return new ArrayList<>(mFormats);
126     }
127 
addCompressedTextureFormat(String format)128     void addCompressedTextureFormat(String format) {
129         mFormats.add(format);
130     }
131 
getVendor()132     String getVendor() {
133         return mGraphicsVendor;
134     }
135 
setVendor(String vendor)136     void setVendor(String vendor) {
137         mGraphicsVendor = vendor;
138     }
139 
getRenderer()140     String getRenderer() {
141         return mGraphicsRenderer;
142     }
143 
setRenderer(String renderer)144     void setRenderer(String renderer) {
145         mGraphicsRenderer = renderer;
146     }
147 
148     static class GlesSurfaceView extends GLSurfaceView {
149 
GlesSurfaceView(GlesStubActivity parent, int glVersion, CountDownLatch done)150         public GlesSurfaceView(GlesStubActivity parent, int glVersion, CountDownLatch done) {
151             super(parent);
152 
153             if (glVersion > 1) {
154                 // Default is 1 so only set if bigger than 1
155                 setEGLContextClientVersion(glVersion);
156             }
157             setRenderer(new OpenGlesRenderer(parent, glVersion, done));
158         }
159     }
160 
161     static class OpenGlesRenderer implements GLSurfaceView.Renderer {
162 
163         private final GlesStubActivity mParent;
164         private final int mGlVersion;
165         private final CountDownLatch mDone;
166 
OpenGlesRenderer(GlesStubActivity parent, int glVersion, CountDownLatch done)167         OpenGlesRenderer(GlesStubActivity parent, int glVersion, CountDownLatch done) {
168             mParent = parent;
169             mGlVersion = glVersion;
170             mDone = done;
171         }
172 
173         @Override
onSurfaceCreated(GL10 gl, EGLConfig config)174         public void onSurfaceCreated(GL10 gl, EGLConfig config) {
175             String extensions;
176             String vendor;
177             String renderer;
178             if (mGlVersion == 2) {
179                 extensions = GLES20.glGetString(GLES20.GL_EXTENSIONS);
180                 vendor = GLES20.glGetString(GLES20.GL_VENDOR);
181                 renderer = GLES20.glGetString(GLES20.GL_RENDERER);
182             } else if (mGlVersion == 3) {
183                 extensions = GLES30.glGetString(GLES30.GL_EXTENSIONS);
184                 vendor = GLES30.glGetString(GLES30.GL_VENDOR);
185                 renderer = GLES30.glGetString(GLES30.GL_RENDERER);
186             } else {
187                 extensions = gl.glGetString(GL10.GL_EXTENSIONS);
188                 vendor = gl.glGetString(GL10.GL_VENDOR);
189                 renderer = gl.glGetString(GL10.GL_RENDERER);
190             }
191             mParent.setVendor(vendor);
192             mParent.setRenderer(renderer);
193             Scanner scanner = new Scanner(extensions);
194             scanner.useDelimiter(" ");
195             while (scanner.hasNext()) {
196                 String ext = scanner.next();
197                 mParent.addOpenGlExtension(ext);
198                 if (ext.contains("texture")) {
199                     if (ext.contains("compression") || ext.contains("compressed")) {
200                         mParent.addCompressedTextureFormat(ext);
201                     }
202                 }
203             }
204             scanner.close();
205             mDone.countDown();
206         }
207 
208         @Override
onSurfaceChanged(GL10 gl, int width, int height)209         public void onSurfaceChanged(GL10 gl, int width, int height) {}
210 
211         @Override
onDrawFrame(GL10 gl)212         public void onDrawFrame(GL10 gl) {}
213     }
214 }
215