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 #include <pthread.h> 8 #include <unistd.h> 9 #include <android/looper.h> 10 #include <android/native_window.h> 11 12 #include "include/core/SkPictureRecorder.h" 13 14 enum MessageType { 15 kUndefined, 16 kInitialize, 17 kDestroy, 18 kRenderPicture, 19 }; 20 21 struct Message { 22 MessageType fType = kUndefined; 23 ANativeWindow* fNativeWindow = nullptr; 24 SkPicture* fPicture; 25 MessageMessage26 Message() {} MessageMessage27 Message(MessageType t) : fType(t) {} 28 }; 29 30 class SurfaceThread { 31 public: 32 SurfaceThread(); 33 34 void postMessage(const Message& message) const; 35 void readMessage(Message* message) const; 36 37 private: 38 static void* pthread_main(void* arg); 39 static int message_callback(int fd, int events, void* data); 40 41 pthread_t fThread; 42 int fPipe[2]; // acts as a Message queue, read from [0] write to [1] 43 }; 44