aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/viewer/sk_app/android/surface_glue_android.h
blob: 3bbf3af9c3cfec437fb7b016ac24343de504b5ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/

#ifndef surface_glue_android_DEFINED
#define surface_glue_android_DEFINED

#include <pthread.h>

#include <android/native_window_jni.h>

#include "SkString.h"

#include "../Application.h"
#include "../Window.h"

namespace sk_app {

enum MessageType {
    kUndefined,
    kSurfaceCreated,
    kSurfaceChanged,
    kSurfaceDestroyed,
    kDestroyApp,
    kContentInvalidated,
    kKeyPressed,
    kTouched,
    kUIStateChanged,
};

struct Message {
    MessageType fType = kUndefined;
    ANativeWindow* fNativeWindow = nullptr;
    int fKeycode = 0;
    int fTouchOwner, fTouchState;
    float fTouchX, fTouchY;

    SkString* stateName;
    SkString* stateValue;

    Message() {}
    Message(MessageType t) : fType(t) {}
};

struct SkiaAndroidApp {
    Application* fApp;
    Window* fWindow;
    jobject fAndroidApp;

    SkiaAndroidApp(JNIEnv* env, jobject androidApp);

    void postMessage(const Message& message) const;
    void readMessage(Message* message) const;

    // These must be called in SkiaAndroidApp's own pthread because the JNIEnv is thread sensitive
    void setTitle(const char* title) const;
    void setUIState(const Json::Value& state) const;

private:
    pthread_t fThread;
    ANativeWindow* fNativeWindow;
    int fPipes[2];  // 0 is the read message pipe, 1 is the write message pipe
    JavaVM* fJavaVM;
    JNIEnv* fPThreadEnv;
    jmethodID fSetTitleMethodID, fSetStateMethodID;

    // This must be called in SkiaAndroidApp's own pthread because the JNIEnv is thread sensitive
    ~SkiaAndroidApp();

    static int message_callback(int fd, int events, void* data);
    static void* pthread_main(void*);
};

}  // namespace sk_app

#endif