• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "modules/androidkit/src/SurfaceThread.h"
8 
9 #include <pthread.h>
10 #include <android/looper.h>
11 #include "include/core/SkTypes.h"
12 
13 
SurfaceThread()14 SurfaceThread::SurfaceThread() {
15     SkDebugf("initialized");
16     pipe(fPipe);
17     pthread_create(&fThread, nullptr, pthread_main, this);
18 }
19 
postMessage(const Message & message) const20 void SurfaceThread::postMessage(const Message& message) const {
21     write(fPipe[1], &message, sizeof(message));
22 }
23 
readMessage(Message * message) const24 void SurfaceThread::readMessage(Message* message) const {
25     read(fPipe[0], message, sizeof(Message));
26 }
27 
message_callback(int,int,void * data)28 int SurfaceThread::message_callback(int /* fd */, int /* events */, void* data) {
29     auto surfaceThread = (SurfaceThread*)data;
30     Message message;
31     surfaceThread->readMessage(&message);
32     // get target surface from Message
33 
34     switch (message.fType) {
35         case kInitialize: {
36             SkDebugf("initialize WindowContext");
37             break;
38         }
39         case kDestroy: {
40             SkDebugf("surface destroyed, shut down thread");
41             return 0;
42             break;
43         }
44         case kRenderPicture: {
45             sk_sp<SkPicture> picture(message.fPicture);
46             SkDebugf("take in picture and surface from message and call surface.getCanvas().drawPicture()");
47             break;
48         }
49         default: {
50             // do nothing
51         }
52     }
53 
54     return 1;  // continue receiving callbacks
55 }
56 
pthread_main(void * arg)57 void* SurfaceThread::pthread_main(void* arg) {
58     auto surfaceThread = (SurfaceThread*)arg;
59     // Looper setup
60     ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
61     ALooper_addFd(looper, surfaceThread->fPipe[0], 1, ALOOPER_EVENT_INPUT,
62                surfaceThread->message_callback, surfaceThread);
63 
64     while (true) {
65         const int ident = ALooper_pollAll(0, nullptr, nullptr, nullptr);
66 
67         if (ident >= 0) {
68             SkDebugf("Unhandled ALooper_pollAll ident=%d !", ident);
69         }
70     }
71     return nullptr;
72 }
73