1 /* 2 * Copyright 2021 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 JetSki_SurfaceThread_DEFINED 8 #define JetSki_SurfaceThread_DEFINED 9 10 #include <pthread.h> 11 #include <unistd.h> 12 #include <android/looper.h> 13 #include <android/native_window.h> 14 15 #include "include/core/SkPictureRecorder.h" 16 17 class WindowSurface; 18 19 #include "modules/jetski/src/Surface.h" 20 21 enum MessageType { 22 kUndefined, 23 kInitialize, 24 kDestroy, 25 kRenderPicture, 26 }; 27 28 struct Message { 29 MessageType fType = kUndefined; 30 ANativeWindow* fNativeWindow = nullptr; 31 SkPicture* fPicture = nullptr; 32 WindowSurface** fWindowSurface = nullptr; 33 MessageMessage34 Message() {} MessageMessage35 Message(MessageType t) : fType(t) {} 36 }; 37 38 class SurfaceThread { 39 public: 40 SurfaceThread(); 41 42 void postMessage(const Message& message) const; 43 void readMessage(Message* message) const; 44 void release(); 45 private: 46 static void* pthread_main(void* arg); 47 static int message_callback(int fd, int events, void* data); 48 // TODO: This has to be static, which is weird now, but fine in a singleton 49 // Switch to singleton design or find other way to break out of thread loop 50 bool fRunning; 51 52 pthread_t fThread; 53 int fPipe[2]; // acts as a Message queue, read from [0] write to [1] 54 }; 55 56 #endif 57