Skip to content

Commit

Permalink
wip: video
Browse files Browse the repository at this point in the history
  • Loading branch information
stergiotis committed Jul 21, 2024
1 parent 68d0a8d commit c3ce6d2
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 4 deletions.
1 change: 1 addition & 0 deletions skia/build_cpp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ here=$(dirname "$(readlink -f "$BASH_SOURCE")")
cd "$here"
flatc="../../contrib/flatbuffers/flatc"
"$flatc" -o imgui --cpp imgui/vectorCmd.fbs
"$flatc" -o skia/video --cpp ../video_player/spec/userInteraction.fbs

./cmakelists.dhall

Expand Down
2 changes: 2 additions & 0 deletions skia/skia/cliOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ void CliOptions::usage(const char *name, FILE *file) const {
fprintf(file, "string flags:\n");
fprintf(file, " -videoRawFramesFile [path:%s]\n", videoRawFramesFile);
fprintf(file, " -videoRawOutputFormat [format:%s]\n", videoRawOutputFormat);
fprintf(file, " -videoUserInteractionEventsInFile [path:%s]\n", videoUserInteractionEventsInFile);
fprintf(file, "integer flags:\n");
fprintf(file, " -videoResolutionWidth [int:%u]\n", videoResolutionWidth);
fprintf(file, " -videoResolutionHeight [int:%u]\n", videoResolutionHeight);
Expand Down Expand Up @@ -125,6 +126,7 @@ void CliOptions::parse(int argc,char **argv,FILE *logChannel) {
videoResolutionHeight = static_cast<uint32_t>(findFlagValueDefaultInt(logChannel, u, argc, argv, "-videoResolutionHeight", "1080"));
videoExitAfterNFrames = static_cast<uint32_t>(findFlagValueDefaultInt(logChannel, u, argc, argv, "-videoExitAfterNFrames", "0"));
videoRawOutputFormat = findFlagValueDefault(logChannel, u, argc, argv, "-videoRawOutputFormat", "qoi");
videoUserInteractionEventsInFile = findFlagValueDefault(logChannel, u, argc, argv, "-videoUserInteractionEventsInFile", videoUserInteractionEventsInFile);

if(std::popcount(u) != (argc-1)) {
for(int i=1;i<argc;i++) {
Expand Down
1 change: 1 addition & 0 deletions skia/skia/cliOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct CliOptions {
uint32_t videoResolutionHeight = 0;
const char *videoRawOutputFormat = nullptr;
uint32_t videoExitAfterNFrames = 0;
const char *videoUserInteractionEventsInFile = nullptr;

CliOptions() = default;
~CliOptions() = default;
Expand Down
110 changes: 107 additions & 3 deletions skia/skia/video/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cstdio>
#include <cstring>
#include <fcntl.h>

#include "imgui.h"

Expand All @@ -27,8 +28,8 @@
#define QOI_IMPLEMENTATION
#define QOI_NO_STDIO
#define QOI_FREE static_assert(false && "free should never be called")
#define QOI_MALLOC(sz) qoi_malloc(sz)
static void *qoi_malloc(size_t sz) {
#define QOI_MALLOC(sz) qoiMalloc(sz)
static void *qoiMalloc(size_t sz) {
static void *qoiBuffer = nullptr;
static size_t lastSz = 0;
if(qoiBuffer == nullptr) {
Expand Down Expand Up @@ -152,7 +153,6 @@ int App::run(CliOptions &opts) {
sk_sp<SkFontMgr> fontMgr = nullptr;
sk_sp<SkTypeface> typeface = nullptr;
sk_sp<SkData> ttfData = nullptr;
SkMemoryStream *ttfStream = nullptr;
{ // setup skia/imgui shared objects
if (opts.fffiInterpreter) {
if (opts.fffiInFile != nullptr) {
Expand Down Expand Up @@ -306,6 +306,28 @@ int App::run(CliOptions &opts) {
loopSkp(opts);
break;
}
if(opts.videoUserInteractionEventsInFile != nullptr && opts.videoUserInteractionEventsInFile[0] != '\0') {
fUserInteractionFH = fopen(opts.videoUserInteractionEventsInFile, "rb");
if(fUserInteractionFH == nullptr) {
fprintf(stderr, "unable to open user interaction events in file %s: %s\n", opts.videoUserInteractionEventsInFile, strerror(errno));
return 1;
}

auto const fd = fileno(fUserInteractionFH);
auto const flags = fcntl(fd, F_GETFL);
if(flags < 0) {
fprintf(stderr, "unable to get file status flags for file %s: %s\n", opts.videoUserInteractionEventsInFile, strerror(errno));
return 1;
}
if(fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) {
fprintf(stderr, "unable to set file status flag O_NONBLOCK file %s: %s\n", opts.videoUserInteractionEventsInFile, strerror(errno));
return 1;
}
if(setvbuf(fUserInteractionFH,nullptr,_IONBF,0) != 0) {
fprintf(stderr, "unable to set buffering for file %s: %s\n", opts.videoUserInteractionEventsInFile, strerror(errno));
return 1;
}
}

if(opts.fffiInterpreter) {
render_cleanup();
Expand Down Expand Up @@ -619,3 +641,85 @@ void App::loopSkp(const CliOptions &opts) {
postPaint();
}
}
App::~App() {
if(fUserInteractionFH != nullptr) {
fclose(fUserInteractionFH);
fUserInteractionFH = nullptr;
}
}

void App::dispatchUserInteractionEvents() {
size_t memorySize = 1024 * 1024;
static uint8_t state = 0;
static uint8_t* mem = nullptr;
static uint8_t* p = nullptr;
static uint32_t bytesToRead = 0;

while(true) {
switch(state) {
case 0: // init
mem = static_cast<uint8_t *>(malloc(memorySize));
if(mem == nullptr) {
fprintf(stderr,"unable to allocate memory\n");
exit(2);
}
state = 1;
bytesToRead = 4;
p = mem;
break;
case 1: // read flatbuffers message length
{
auto r = fread(p,1,bytesToRead,fUserInteractionFH);
bytesToRead -= r;
p += r;
if(bytesToRead == 0) {
// read length of message
bytesToRead = flatbuffers::ReadScalar<uint32_t>(mem);
if(bytesToRead > memorySize) {
memorySize = (bytesToRead/4096+1)*4096;
mem = static_cast<uint8_t *>(realloc(mem, memorySize));
p = mem+4;
}
state = 2;
}
}
break;
case 2: // read flatbuffers message
{
auto r = fread(p,1,bytesToRead,fUserInteractionFH);
bytesToRead -= r;
p += r;
if(bytesToRead == 0) {
auto const e = UserInteractionFB::GetSizePrefixedEvent(mem);
handleUserInteractionEvent(*e);
state = 1;
p = mem;
bytesToRead = 4;
}
}
break;
}
}
}

void App::handleUserInteractionEvent(UserInteractionFB::Event const &ev) {
switch(ev.event_type()) {
case UserInteractionFB::UserInteraction_NONE:
break;
case UserInteractionFB::UserInteraction_EventMouseMotion:
{
auto const e = ev.event_as_EventMouseMotion();
}
break;
case UserInteractionFB::UserInteraction_EventMouseWheel:
{
auto const e = ev.event_as_EventMouseMotion();
}
break;
case UserInteractionFB::UserInteraction_EventMouseButton:
{
auto const e = ev.event_as_EventMouseButton();
}
break;
}
}
6 changes: 5 additions & 1 deletion skia/skia/video/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "SkPaint.h"
#include "vectorCmdSkiaRenderer.h"
#include "setupUI.h"
#include "userInteraction_generated.h"

enum rawFrameOutputFormat {
kRawFrameOutputFormat_None = 0,
Expand All @@ -23,7 +24,7 @@ enum rawFrameOutputFormat {
class App {
public:
App();
~App() = default;
~App();
int run(CliOptions &opts);
private:
void paint(SkCanvas* canvas, int width, int height);
Expand All @@ -39,6 +40,8 @@ class App {
void loopFlatbuffers(CliOptions const &opts);
void loopSvg(CliOptions const &opts);
void loopSkp(CliOptions const &opts);
void dispatchUserInteractionEvents();
void handleUserInteractionEvent(UserInteractionFB::Event const &ev);

SkPaint fFontPaint;
VectorCmdSkiaRenderer fVectorCmdSkiaRenderer;
Expand All @@ -53,4 +56,5 @@ class App {
uint64_t fFrame;
double fPreviousTime;
rawFrameOutputFormat fOutputFormat;
FILE *fUserInteractionFH = nullptr;
};

0 comments on commit c3ce6d2

Please sign in to comment.