-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(🤖): Remove internal draw loop (#2763)
On Android it removes the need to use JNI back and forth for each frame.
- Loading branch information
1 parent
1352eca
commit ba1db84
Showing
41 changed files
with
256 additions
and
766 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/skia/android/cpp/rnskia-android/MainThreadDispatcher.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#pragma once | ||
|
||
#include <android/looper.h> | ||
#include <unistd.h> | ||
|
||
class MainThreadDispatcher { | ||
private: | ||
ALooper *mainLooper; | ||
int messagePipe[2]; | ||
std::queue<std::function<void()>> taskQueue; | ||
std::mutex queueMutex; | ||
|
||
static constexpr int LOOPER_ID_MAIN = 1; | ||
|
||
void processMessages() { | ||
std::lock_guard<std::mutex> lock(queueMutex); | ||
while (!taskQueue.empty()) { | ||
auto task = taskQueue.front(); | ||
taskQueue.pop(); | ||
task(); | ||
} | ||
} | ||
|
||
public: | ||
static MainThreadDispatcher &getInstance() { | ||
static MainThreadDispatcher instance; | ||
return instance; | ||
} | ||
|
||
void post(std::function<void()> task) { | ||
// TODO: this is disabled for now but we can clean this up | ||
// if (ALooper_forThread() == mainLooper) { | ||
// task(); | ||
// } else { | ||
{ | ||
std::lock_guard<std::mutex> lock(queueMutex); | ||
taskQueue.push(std::move(task)); | ||
} | ||
char wake = 1; | ||
write(messagePipe[1], &wake, 1); | ||
// } | ||
} | ||
|
||
~MainThreadDispatcher() { | ||
close(messagePipe[0]); | ||
close(messagePipe[1]); | ||
} | ||
|
||
private: | ||
MainThreadDispatcher() { | ||
mainLooper = ALooper_forThread(); | ||
if (!mainLooper) { | ||
mainLooper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); | ||
} | ||
|
||
pipe(messagePipe); | ||
|
||
ALooper_addFd( | ||
mainLooper, messagePipe[0], LOOPER_ID_MAIN, ALOOPER_EVENT_INPUT, | ||
[](int fd, int events, void *data) -> int { | ||
char buf[1]; | ||
read(fd, buf, 1); | ||
auto dispatcher = static_cast<MainThreadDispatcher *>(data); | ||
dispatcher->processMessages(); | ||
return 1; | ||
}, | ||
this); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.