• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7 
8 #ifndef surface_glue_android_DEFINED
9 #define surface_glue_android_DEFINED
10 
11 #include <pthread.h>
12 
13 #include <android/native_window_jni.h>
14 
15 #include "SkString.h"
16 
17 #include "../Application.h"
18 #include "../Window.h"
19 
20 namespace sk_app {
21 
22 enum MessageType {
23     kUndefined,
24     kSurfaceCreated,
25     kSurfaceChanged,
26     kSurfaceDestroyed,
27     kDestroyApp,
28     kContentInvalidated,
29     kKeyPressed,
30     kTouched,
31     kUIStateChanged,
32 };
33 
34 struct Message {
35     MessageType fType = kUndefined;
36     ANativeWindow* fNativeWindow = nullptr;
37     int fKeycode = 0;
38     int fTouchOwner, fTouchState;
39     float fTouchX, fTouchY;
40 
41     SkString* stateName;
42     SkString* stateValue;
43 
MessageMessage44     Message() {}
MessageMessage45     Message(MessageType t) : fType(t) {}
46 };
47 
48 struct SkiaAndroidApp {
49     Application* fApp;
50     Window* fWindow;
51     jobject fAndroidApp;
52 
53     SkiaAndroidApp(JNIEnv* env, jobject androidApp);
54 
55     void postMessage(const Message& message) const;
56     void readMessage(Message* message) const;
57 
58     // These must be called in SkiaAndroidApp's own pthread because the JNIEnv is thread sensitive
59     void setTitle(const char* title) const;
60     void setUIState(const char* state) const;
61 
62 private:
63     pthread_t fThread;
64     ANativeWindow* fNativeWindow;
65     int fPipes[2];  // 0 is the read message pipe, 1 is the write message pipe
66     JavaVM* fJavaVM;
67     JNIEnv* fPThreadEnv;
68     jmethodID fSetTitleMethodID, fSetStateMethodID;
69 
70     // This must be called in SkiaAndroidApp's own pthread because the JNIEnv is thread sensitive
71     ~SkiaAndroidApp();
72 
73     static int message_callback(int fd, int events, void* data);
74     static void* pthread_main(void*);
75 };
76 
77 }  // namespace sk_app
78 
79 #endif
80