xref: /aosp_15_r20/external/skia/tools/sk_app/android/surface_glue_android.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "include/core/SkString.h"
16 
17 #include "tools/sk_app/Application.h"
18 #include "tools/sk_app/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     jobjectArray fArgs;
53 
54     SkiaAndroidApp(JNIEnv* env, jobject androidApp, jobjectArray args);
55 
56     void postMessage(const Message& message) const;
57     void readMessage(Message* message) const;
58 
59     // These must be called in SkiaAndroidApp's own pthread because the JNIEnv is thread sensitive
60     void setTitle(const char* title) const;
61     void setUIState(const char* state) const;
62 
63 private:
64     pthread_t fThread;
65     ANativeWindow* fNativeWindow;
66     int fPipes[2];  // 0 is the read message pipe, 1 is the write message pipe
67     JavaVM* fJavaVM;
68     JNIEnv* fPThreadEnv;
69     jmethodID fSetTitleMethodID, fSetStateMethodID;
70 
71     // This must be called in SkiaAndroidApp's own pthread because the JNIEnv is thread sensitive
72     ~SkiaAndroidApp();
73 
74     static int message_callback(int fd, int events, void* data);
75     static void* pthread_main(void*);
76 };
77 
78 }  // namespace sk_app
79 
80 #endif
81