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