• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2016 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // AndroidWindow.cpp: Implementation of OSWindow for Android
8 
9 #include "util/android/AndroidWindow.h"
10 
11 #include <pthread.h>
12 #include <iostream>
13 
14 #include "common/debug.h"
15 #include "util/android/third_party/android_native_app_glue.h"
16 
17 namespace
18 {
19 struct android_app *sApp = nullptr;
20 pthread_mutex_t sInitWindowMutex;
21 pthread_cond_t sInitWindowCond;
22 bool sInitWindowDone = false;
23 JNIEnv *gJni         = nullptr;
24 
25 // SCREEN_ORIENTATION_LANDSCAPE and SCREEN_ORIENTATION_PORTRAIT are
26 // available from Android API level 1
27 // https://developer.android.com/reference/android/app/Activity#setRequestedOrientation(int)
28 const int kScreenOrientationLandscape = 0;
29 const int kScreenOrientationPortrait  = 1;
30 
GetJniEnv()31 JNIEnv *GetJniEnv()
32 {
33     if (gJni)
34         return gJni;
35 
36     sApp->activity->vm->AttachCurrentThread(&gJni, NULL);
37     return gJni;
38 }
39 
SetScreenOrientation(struct android_app * app,int orientation)40 int SetScreenOrientation(struct android_app *app, int orientation)
41 {
42     // Use reverse JNI to call the Java entry point that rotates the
43     // display to respect width and height
44     JNIEnv *jni = GetJniEnv();
45     if (!jni)
46     {
47         std::cerr << "Failed to get JNI env for screen rotation";
48         return JNI_ERR;
49     }
50 
51     jclass clazz       = jni->GetObjectClass(app->activity->clazz);
52     jmethodID methodID = jni->GetMethodID(clazz, "setRequestedOrientation", "(I)V");
53     jni->CallVoidMethod(app->activity->clazz, methodID, orientation);
54 
55     return 0;
56 }
57 }  // namespace
58 
AndroidWindow()59 AndroidWindow::AndroidWindow() {}
60 
~AndroidWindow()61 AndroidWindow::~AndroidWindow() {}
62 
initializeImpl(const std::string & name,int width,int height)63 bool AndroidWindow::initializeImpl(const std::string &name, int width, int height)
64 {
65     return resize(width, height);
66 }
destroy()67 void AndroidWindow::destroy() {}
68 
disableErrorMessageDialog()69 void AndroidWindow::disableErrorMessageDialog() {}
70 
resetNativeWindow()71 void AndroidWindow::resetNativeWindow() {}
72 
getNativeWindow() const73 EGLNativeWindowType AndroidWindow::getNativeWindow() const
74 {
75     // Return the entire Activity Surface for now
76     // sApp->window is valid only after sInitWindowDone, which is true after initializeImpl()
77     return sApp->window;
78 }
79 
getNativeDisplay() const80 EGLNativeDisplayType AndroidWindow::getNativeDisplay() const
81 {
82     return EGL_DEFAULT_DISPLAY;
83 }
84 
messageLoop()85 void AndroidWindow::messageLoop()
86 {
87     // TODO: accumulate events in the real message loop of android_main,
88     // and process them here
89 }
90 
setMousePosition(int x,int y)91 void AndroidWindow::setMousePosition(int x, int y)
92 {
93     UNIMPLEMENTED();
94 }
95 
setOrientation(int width,int height)96 bool AndroidWindow::setOrientation(int width, int height)
97 {
98     // Set tests to run in correct orientation
99     int32_t err = SetScreenOrientation(
100         sApp, (width > height) ? kScreenOrientationLandscape : kScreenOrientationPortrait);
101 
102     return err == 0;
103 }
setPosition(int x,int y)104 bool AndroidWindow::setPosition(int x, int y)
105 {
106     UNIMPLEMENTED();
107     return false;
108 }
109 
resize(int width,int height)110 bool AndroidWindow::resize(int width, int height)
111 {
112     mWidth  = width;
113     mHeight = height;
114 
115     // sApp->window used below is valid only after Activity Surface is created
116     pthread_mutex_lock(&sInitWindowMutex);
117     while (!sInitWindowDone)
118     {
119         pthread_cond_wait(&sInitWindowCond, &sInitWindowMutex);
120     }
121     pthread_mutex_unlock(&sInitWindowMutex);
122 
123     if (sApp->window == nullptr)
124     {
125         // Note: logging isn't initalized yet but this message shows up in logcat.
126         FATAL() << "Window is NULL (is screen locked? e.g. SplashScreen in logcat)";
127     }
128 
129     // TODO: figure out a way to set the format as well,
130     // which is available only after EGLWindow initialization
131     int32_t err = ANativeWindow_setBuffersGeometry(sApp->window, mWidth, mHeight, 0);
132     return err == 0;
133 }
134 
setVisible(bool isVisible)135 void AndroidWindow::setVisible(bool isVisible) {}
136 
signalTestEvent()137 void AndroidWindow::signalTestEvent()
138 {
139     UNIMPLEMENTED();
140 }
141 
onAppCmd(struct android_app * app,int32_t cmd)142 static void onAppCmd(struct android_app *app, int32_t cmd)
143 {
144     switch (cmd)
145     {
146         case APP_CMD_INIT_WINDOW:
147             pthread_mutex_lock(&sInitWindowMutex);
148             sInitWindowDone = true;
149             pthread_cond_broadcast(&sInitWindowCond);
150             pthread_mutex_unlock(&sInitWindowMutex);
151             break;
152         case APP_CMD_DESTROY:
153             if (gJni)
154             {
155                 sApp->activity->vm->DetachCurrentThread();
156             }
157             gJni = nullptr;
158             break;
159 
160             // TODO: process other commands and pass them to AndroidWindow for handling
161             // TODO: figure out how to handle APP_CMD_PAUSE,
162             // which should immediately halt all the rendering,
163             // since Activity Surface is no longer available.
164             // Currently tests crash when paused, for example, due to device changing orientation
165     }
166 }
167 
onInputEvent(struct android_app * app,AInputEvent * event)168 static int32_t onInputEvent(struct android_app *app, AInputEvent *event)
169 {
170     // TODO: Handle input events
171     return 0;  // 0 == not handled
172 }
173 
validPollResult(int result)174 static bool validPollResult(int result)
175 {
176     return result >= 0 || result == ALOOPER_POLL_CALLBACK;
177 }
178 
android_main(struct android_app * app)179 void android_main(struct android_app *app)
180 {
181     int events;
182     struct android_poll_source *source;
183 
184     sApp = app;
185     pthread_mutex_init(&sInitWindowMutex, nullptr);
186     pthread_cond_init(&sInitWindowCond, nullptr);
187 
188     // Event handlers, invoked from source->process()
189     app->onAppCmd     = onAppCmd;
190     app->onInputEvent = onInputEvent;
191 
192     // Message loop, polling for events indefinitely (due to -1 timeout)
193     // Must be here in order to handle APP_CMD_INIT_WINDOW event,
194     // which occurs after AndroidWindow::initializeImpl(), but before AndroidWindow::messageLoop
195     while (
196         validPollResult(ALooper_pollOnce(-1, nullptr, &events, reinterpret_cast<void **>(&source))))
197     {
198         if (source != nullptr)
199         {
200             source->process(app, source);
201         }
202     }
203 }
204 
205 // static
GetExternalStorageDirectory()206 std::string AndroidWindow::GetExternalStorageDirectory()
207 {
208     // Use reverse JNI.
209     JNIEnv *jni = GetJniEnv();
210     if (!jni)
211     {
212         std::cerr << "GetExternalStorageDirectory:: Failed to get JNI env";
213         return "";
214     }
215 
216     jclass classEnvironment = jni->FindClass("android/os/Environment");
217     if (classEnvironment == 0)
218     {
219         std::cerr << "GetExternalStorageDirectory: Failed to find Environment";
220         return "";
221     }
222 
223     // public static File getExternalStorageDirectory ()
224     jmethodID methodIDgetExternalStorageDirectory =
225         jni->GetStaticMethodID(classEnvironment, "getExternalStorageDirectory", "()Ljava/io/File;");
226     if (methodIDgetExternalStorageDirectory == 0)
227     {
228         std::cerr << "GetExternalStorageDirectory: Failed to get static method";
229         return "";
230     }
231 
232     jobject objectFile =
233         jni->CallStaticObjectMethod(classEnvironment, methodIDgetExternalStorageDirectory);
234     jthrowable exception = jni->ExceptionOccurred();
235     if (exception != 0)
236     {
237         jni->ExceptionDescribe();
238         jni->ExceptionClear();
239         std::cerr << "GetExternalStorageDirectory: Failed because of exception";
240         return "";
241     }
242 
243     // Call method on File object to retrieve String object.
244     jclass classFile = jni->GetObjectClass(objectFile);
245     if (classEnvironment == 0)
246     {
247         std::cerr << "GetExternalStorageDirectory: Failed to find object class";
248         return "";
249     }
250 
251     jmethodID methodIDgetAbsolutePath =
252         jni->GetMethodID(classFile, "getAbsolutePath", "()Ljava/lang/String;");
253     if (methodIDgetAbsolutePath == 0)
254     {
255         std::cerr << "GetExternalStorageDirectory: Failed to get method ID";
256         return "";
257     }
258 
259     jstring stringPath =
260         static_cast<jstring>(jni->CallObjectMethod(objectFile, methodIDgetAbsolutePath));
261 
262     // TODO(jmadill): Find how to pass the root test directory to ANGLE. http://crbug.com/1097957
263 
264     // // https://stackoverflow.com/questions/12841240/android-pass-parameter-to-native-activity
265     // jclass clazz = jni->GetObjectClass(sApp->activity->clazz);
266     // if (clazz == 0)
267     // {
268     //     std::cerr << "GetExternalStorageDirectory: Bad activity";
269     //     return "";
270     // }
271 
272     // jmethodID giid = jni->GetMethodID(clazz, "getIntent", "()Landroid/content/Intent;");
273     // if (giid == 0)
274     // {
275     //     std::cerr << "GetExternalStorageDirectory: Could not find getIntent";
276     //     return "";
277     // }
278 
279     // jobject intent = jni->CallObjectMethod(sApp->activity->clazz, giid);
280     // if (intent == 0)
281     // {
282     //     std::cerr << "GetExternalStorageDirectory: Error calling getIntent";
283     //     return "";
284     // }
285 
286     // jclass icl = jni->GetObjectClass(intent);
287     // if (icl == 0)
288     // {
289     //     std::cerr << "GetExternalStorageDirectory: Error getting getIntent class";
290     //     return "";
291     // }
292 
293     // jmethodID gseid =
294     //     jni->GetMethodID(icl, "getStringExtra", "(Ljava/lang/String;)Ljava/lang/String;");
295     // if (gseid == 0)
296     // {
297     //     std::cerr << "GetExternalStorageDirectory: Could not find getStringExtra";
298     //     return "";
299     // }
300 
301     // jstring stringPath = static_cast<jstring>(jni->CallObjectMethod(
302     //     intent, gseid, jni->NewStringUTF("org.chromium.base.test.util.UrlUtils.RootDirectory")));
303     // if (stringPath != 0)
304     // {
305     //     const char *path = jni->GetStringUTFChars(stringPath, nullptr);
306     //     return std::string(path) + "/chromium_tests_root";
307     // }
308 
309     // jclass environment = jni->FindClass("org/chromium/base/test/util/UrlUtils");
310     // if (environment == 0)
311     // {
312     //     std::cerr << "GetExternalStorageDirectory: Failed to find Environment";
313     //     return "";
314     // }
315 
316     // jmethodID getDir =
317     //     jni->GetStaticMethodID(environment, "getIsolatedTestRoot", "()Ljava/lang/String;");
318     // if (getDir == 0)
319     // {
320     //     std::cerr << "GetExternalStorageDirectory: Failed to get static method";
321     //     return "";
322     // }
323 
324     // stringPath = static_cast<jstring>(jni->CallStaticObjectMethod(environment, getDir));
325 
326     exception = jni->ExceptionOccurred();
327     if (exception != 0)
328     {
329         jni->ExceptionDescribe();
330         jni->ExceptionClear();
331         std::cerr << "GetExternalStorageDirectory: Failed because of exception";
332         return "";
333     }
334 
335     const char *path = jni->GetStringUTFChars(stringPath, nullptr);
336     return std::string(path) + "/chromium_tests_root";
337 }
338 
339 // static
New()340 OSWindow *OSWindow::New()
341 {
342     // There should be only one live instance of AndroidWindow at a time,
343     // as there is only one Activity Surface behind it.
344     // Creating a new AndroidWindow each time works for ANGLETest,
345     // as it destroys an old window before creating a new one.
346     // TODO: use GLSurfaceView to support multiple windows
347     return new AndroidWindow();
348 }
349