surface_glue_android.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #ifndef surface_glue_android_DEFINED
  8. #define surface_glue_android_DEFINED
  9. #include <pthread.h>
  10. #include <android/native_window_jni.h>
  11. #include "include/core/SkString.h"
  12. #include "tools/sk_app/Application.h"
  13. #include "tools/sk_app/Window.h"
  14. namespace sk_app {
  15. enum MessageType {
  16. kUndefined,
  17. kSurfaceCreated,
  18. kSurfaceChanged,
  19. kSurfaceDestroyed,
  20. kDestroyApp,
  21. kContentInvalidated,
  22. kKeyPressed,
  23. kTouched,
  24. kUIStateChanged,
  25. };
  26. struct Message {
  27. MessageType fType = kUndefined;
  28. ANativeWindow* fNativeWindow = nullptr;
  29. int fKeycode = 0;
  30. int fTouchOwner, fTouchState;
  31. float fTouchX, fTouchY;
  32. SkString* stateName;
  33. SkString* stateValue;
  34. Message() {}
  35. Message(MessageType t) : fType(t) {}
  36. };
  37. struct SkiaAndroidApp {
  38. Application* fApp;
  39. Window* fWindow;
  40. jobject fAndroidApp;
  41. SkiaAndroidApp(JNIEnv* env, jobject androidApp);
  42. void postMessage(const Message& message) const;
  43. void readMessage(Message* message) const;
  44. // These must be called in SkiaAndroidApp's own pthread because the JNIEnv is thread sensitive
  45. void setTitle(const char* title) const;
  46. void setUIState(const char* state) const;
  47. private:
  48. pthread_t fThread;
  49. ANativeWindow* fNativeWindow;
  50. int fPipes[2]; // 0 is the read message pipe, 1 is the write message pipe
  51. JavaVM* fJavaVM;
  52. JNIEnv* fPThreadEnv;
  53. jmethodID fSetTitleMethodID, fSetStateMethodID;
  54. // This must be called in SkiaAndroidApp's own pthread because the JNIEnv is thread sensitive
  55. ~SkiaAndroidApp();
  56. static int message_callback(int fd, int events, void* data);
  57. static void* pthread_main(void*);
  58. };
  59. } // namespace sk_app
  60. #endif