Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SurfaceThread.cpp
Go to the documentation of this file.
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 */
8
12
16
18 pipe(fPipe);
19 fRunning = true;
20 pthread_create(&fThread, nullptr, pthread_main, this);
21}
22
24 write(fPipe[1], &message, sizeof(message));
25}
26
28 read(fPipe[0], message, sizeof(Message));
29}
30
32 pthread_join(fThread, nullptr);
33}
34
35int SurfaceThread::message_callback(int /* fd */, int /* events */, void* data) {
36 auto surfaceThread = (SurfaceThread*)data;
38 surfaceThread->readMessage(&message);
39 // get target surface from Message
40
41 switch (message.fType) {
42 case kInitialize: {
44 auto winctx = skwindow::MakeGLForAndroid(message.fNativeWindow, params);
45 if (!winctx) {
46 break;
47 }
48 *message.fWindowSurface = new WindowSurface(message.fNativeWindow, std::move(winctx));
49 break;
50 }
51 case kDestroy: {
52 SkDebugf("surface destroyed, shutting down thread");
53 surfaceThread->fRunning = false;
54 if(auto* windowSurface = reinterpret_cast<Surface*>(*message.fWindowSurface)){
55 windowSurface->release(nullptr);
56 delete windowSurface;
57 }
58 return 0;
59 }
60 case kRenderPicture: {
62 if(auto* windowSurface = reinterpret_cast<Surface*>(*message.fWindowSurface)){
63 windowSurface->getCanvas()->drawPicture(picture);
64 windowSurface->flushAndSubmit();
65 }
66 break;
67 }
68 default: {
69 // do nothing
70 }
71 }
72
73 return 1; // continue receiving callbacks
74}
75
76void* SurfaceThread::pthread_main(void* arg) {
77 auto surfaceThread = (SurfaceThread*)arg;
78 // Looper setup
79 ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
80 ALooper_addFd(looper, surfaceThread->fPipe[0], 1, ALOOPER_EVENT_INPUT,
81 surfaceThread->message_callback, surfaceThread);
82
83 while (surfaceThread->fRunning) {
84 const int ident = ALooper_pollAll(0, nullptr, nullptr, nullptr);
85
86 if (ident >= 0) {
87 SkDebugf("Unhandled ALooper_pollAll ident=%d !", ident);
88 }
89 }
90 return nullptr;
91}
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
static bool read(SkStream *stream, void *buffer, size_t amount)
@ kRenderPicture
@ kInitialize
@ kDestroy
void postMessage(const Message &message) const
void readMessage(Message *message) const
const EmbeddedViewParams * params
Win32Message message
sk_sp< const SkPicture > picture
Definition SkRecords.h:299
void write(SkWStream *wStream, const T &text)
Definition skqp.cpp:188