Skip to content

Commit c3ce6d2

Browse files
committed
wip: video
1 parent 68d0a8d commit c3ce6d2

File tree

5 files changed

+116
-4
lines changed

5 files changed

+116
-4
lines changed

skia/build_cpp.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ here=$(dirname "$(readlink -f "$BASH_SOURCE")")
44
cd "$here"
55
flatc="../../contrib/flatbuffers/flatc"
66
"$flatc" -o imgui --cpp imgui/vectorCmd.fbs
7+
"$flatc" -o skia/video --cpp ../video_player/spec/userInteraction.fbs
78

89
./cmakelists.dhall
910

skia/skia/cliOptions.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ void CliOptions::usage(const char *name, FILE *file) const {
8080
fprintf(file, "string flags:\n");
8181
fprintf(file, " -videoRawFramesFile [path:%s]\n", videoRawFramesFile);
8282
fprintf(file, " -videoRawOutputFormat [format:%s]\n", videoRawOutputFormat);
83+
fprintf(file, " -videoUserInteractionEventsInFile [path:%s]\n", videoUserInteractionEventsInFile);
8384
fprintf(file, "integer flags:\n");
8485
fprintf(file, " -videoResolutionWidth [int:%u]\n", videoResolutionWidth);
8586
fprintf(file, " -videoResolutionHeight [int:%u]\n", videoResolutionHeight);
@@ -125,6 +126,7 @@ void CliOptions::parse(int argc,char **argv,FILE *logChannel) {
125126
videoResolutionHeight = static_cast<uint32_t>(findFlagValueDefaultInt(logChannel, u, argc, argv, "-videoResolutionHeight", "1080"));
126127
videoExitAfterNFrames = static_cast<uint32_t>(findFlagValueDefaultInt(logChannel, u, argc, argv, "-videoExitAfterNFrames", "0"));
127128
videoRawOutputFormat = findFlagValueDefault(logChannel, u, argc, argv, "-videoRawOutputFormat", "qoi");
129+
videoUserInteractionEventsInFile = findFlagValueDefault(logChannel, u, argc, argv, "-videoUserInteractionEventsInFile", videoUserInteractionEventsInFile);
128130

129131
if(std::popcount(u) != (argc-1)) {
130132
for(int i=1;i<argc;i++) {

skia/skia/cliOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct CliOptions {
2525
uint32_t videoResolutionHeight = 0;
2626
const char *videoRawOutputFormat = nullptr;
2727
uint32_t videoExitAfterNFrames = 0;
28+
const char *videoUserInteractionEventsInFile = nullptr;
2829

2930
CliOptions() = default;
3031
~CliOptions() = default;

skia/skia/video/app.cpp

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <cstdio>
44
#include <cstring>
5+
#include <fcntl.h>
56

67
#include "imgui.h"
78

@@ -27,8 +28,8 @@
2728
#define QOI_IMPLEMENTATION
2829
#define QOI_NO_STDIO
2930
#define QOI_FREE static_assert(false && "free should never be called")
30-
#define QOI_MALLOC(sz) qoi_malloc(sz)
31-
static void *qoi_malloc(size_t sz) {
31+
#define QOI_MALLOC(sz) qoiMalloc(sz)
32+
static void *qoiMalloc(size_t sz) {
3233
static void *qoiBuffer = nullptr;
3334
static size_t lastSz = 0;
3435
if(qoiBuffer == nullptr) {
@@ -152,7 +153,6 @@ int App::run(CliOptions &opts) {
152153
sk_sp<SkFontMgr> fontMgr = nullptr;
153154
sk_sp<SkTypeface> typeface = nullptr;
154155
sk_sp<SkData> ttfData = nullptr;
155-
SkMemoryStream *ttfStream = nullptr;
156156
{ // setup skia/imgui shared objects
157157
if (opts.fffiInterpreter) {
158158
if (opts.fffiInFile != nullptr) {
@@ -306,6 +306,28 @@ int App::run(CliOptions &opts) {
306306
loopSkp(opts);
307307
break;
308308
}
309+
if(opts.videoUserInteractionEventsInFile != nullptr && opts.videoUserInteractionEventsInFile[0] != '\0') {
310+
fUserInteractionFH = fopen(opts.videoUserInteractionEventsInFile, "rb");
311+
if(fUserInteractionFH == nullptr) {
312+
fprintf(stderr, "unable to open user interaction events in file %s: %s\n", opts.videoUserInteractionEventsInFile, strerror(errno));
313+
return 1;
314+
}
315+
316+
auto const fd = fileno(fUserInteractionFH);
317+
auto const flags = fcntl(fd, F_GETFL);
318+
if(flags < 0) {
319+
fprintf(stderr, "unable to get file status flags for file %s: %s\n", opts.videoUserInteractionEventsInFile, strerror(errno));
320+
return 1;
321+
}
322+
if(fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
323+
fprintf(stderr, "unable to set file status flag O_NONBLOCK file %s: %s\n", opts.videoUserInteractionEventsInFile, strerror(errno));
324+
return 1;
325+
}
326+
if(setvbuf(fUserInteractionFH,nullptr,_IONBF,0) != 0) {
327+
fprintf(stderr, "unable to set buffering for file %s: %s\n", opts.videoUserInteractionEventsInFile, strerror(errno));
328+
return 1;
329+
}
330+
}
309331

310332
if(opts.fffiInterpreter) {
311333
render_cleanup();
@@ -619,3 +641,85 @@ void App::loopSkp(const CliOptions &opts) {
619641
postPaint();
620642
}
621643
}
644+
App::~App() {
645+
if(fUserInteractionFH != nullptr) {
646+
fclose(fUserInteractionFH);
647+
fUserInteractionFH = nullptr;
648+
}
649+
}
650+
651+
void App::dispatchUserInteractionEvents() {
652+
size_t memorySize = 1024 * 1024;
653+
static uint8_t state = 0;
654+
static uint8_t* mem = nullptr;
655+
static uint8_t* p = nullptr;
656+
static uint32_t bytesToRead = 0;
657+
658+
while(true) {
659+
switch(state) {
660+
case 0: // init
661+
mem = static_cast<uint8_t *>(malloc(memorySize));
662+
if(mem == nullptr) {
663+
fprintf(stderr,"unable to allocate memory\n");
664+
exit(2);
665+
}
666+
state = 1;
667+
bytesToRead = 4;
668+
p = mem;
669+
break;
670+
case 1: // read flatbuffers message length
671+
{
672+
auto r = fread(p,1,bytesToRead,fUserInteractionFH);
673+
bytesToRead -= r;
674+
p += r;
675+
if(bytesToRead == 0) {
676+
// read length of message
677+
bytesToRead = flatbuffers::ReadScalar<uint32_t>(mem);
678+
if(bytesToRead > memorySize) {
679+
memorySize = (bytesToRead/4096+1)*4096;
680+
mem = static_cast<uint8_t *>(realloc(mem, memorySize));
681+
p = mem+4;
682+
}
683+
state = 2;
684+
}
685+
}
686+
break;
687+
case 2: // read flatbuffers message
688+
{
689+
auto r = fread(p,1,bytesToRead,fUserInteractionFH);
690+
bytesToRead -= r;
691+
p += r;
692+
if(bytesToRead == 0) {
693+
auto const e = UserInteractionFB::GetSizePrefixedEvent(mem);
694+
handleUserInteractionEvent(*e);
695+
state = 1;
696+
p = mem;
697+
bytesToRead = 4;
698+
}
699+
}
700+
break;
701+
}
702+
}
703+
}
704+
705+
void App::handleUserInteractionEvent(UserInteractionFB::Event const &ev) {
706+
switch(ev.event_type()) {
707+
case UserInteractionFB::UserInteraction_NONE:
708+
break;
709+
case UserInteractionFB::UserInteraction_EventMouseMotion:
710+
{
711+
auto const e = ev.event_as_EventMouseMotion();
712+
}
713+
break;
714+
case UserInteractionFB::UserInteraction_EventMouseWheel:
715+
{
716+
auto const e = ev.event_as_EventMouseMotion();
717+
}
718+
break;
719+
case UserInteractionFB::UserInteraction_EventMouseButton:
720+
{
721+
auto const e = ev.event_as_EventMouseButton();
722+
}
723+
break;
724+
}
725+
}

skia/skia/video/app.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "SkPaint.h"
66
#include "vectorCmdSkiaRenderer.h"
77
#include "setupUI.h"
8+
#include "userInteraction_generated.h"
89

910
enum rawFrameOutputFormat {
1011
kRawFrameOutputFormat_None = 0,
@@ -23,7 +24,7 @@ enum rawFrameOutputFormat {
2324
class App {
2425
public:
2526
App();
26-
~App() = default;
27+
~App();
2728
int run(CliOptions &opts);
2829
private:
2930
void paint(SkCanvas* canvas, int width, int height);
@@ -39,6 +40,8 @@ class App {
3940
void loopFlatbuffers(CliOptions const &opts);
4041
void loopSvg(CliOptions const &opts);
4142
void loopSkp(CliOptions const &opts);
43+
void dispatchUserInteractionEvents();
44+
void handleUserInteractionEvent(UserInteractionFB::Event const &ev);
4245

4346
SkPaint fFontPaint;
4447
VectorCmdSkiaRenderer fVectorCmdSkiaRenderer;
@@ -53,4 +56,5 @@ class App {
5356
uint64_t fFrame;
5457
double fPreviousTime;
5558
rawFrameOutputFormat fOutputFormat;
59+
FILE *fUserInteractionFH = nullptr;
5660
};

0 commit comments

Comments
 (0)