Tests: GPU-based viewer 52/281152/4
authorMichal Maciola <m.maciola@samsung.com>
Tue, 13 Sep 2022 13:23:27 +0000 (15:23 +0200)
committerMichal Maciola <m.maciola@samsung.com>
Mon, 17 Oct 2022 12:05:45 +0000 (14:05 +0200)
This commits add a GPU-based viewer.
Both cpu and gpu viewer are build, but only one will be supported
accordingly to the use_gl flag of rive-tizen.

Change-Id: Ib542acae13e7dfc3039e95e2478d4cd363d7aba2

viewer/meson.build
viewer/viewer_gpu.cpp [new file with mode: 0644]

index 7e958e235c4e38306232bb0adb8e43a7cbaeb4a5..0476d66b2cda1d7a9f1e41f7cd59f698e8f3bb95 100644 (file)
@@ -9,7 +9,16 @@ rive_tizen_dep = [dependency('rive_tizen', required: true)]
 
 message('Building CPU Viewer')
 elementary_dep = dependency('elementary', required : true)
-viewer_dep = [elementary_dep, rive_tizen_dep]
+cpu_viewer_dep = [elementary_dep, rive_tizen_dep]
 
 executable('viewer_cpu', 'viewer_cpu.cpp',
-    dependencies : viewer_dep)
\ No newline at end of file
+    dependencies : cpu_viewer_dep)
+
+
+message('Building GPU Viewer')
+glfw3_dep = dependency('glfw3', required : true)
+threads_dep = dependency('threads', required: true)
+gpu_viewer_dep = [glfw3_dep, threads_dep, rive_tizen_dep]
+
+executable('viewer_gpu', 'viewer_gpu.cpp',
+    dependencies : gpu_viewer_dep)
\ No newline at end of file
diff --git a/viewer/viewer_gpu.cpp b/viewer/viewer_gpu.cpp
new file mode 100644 (file)
index 0000000..d04150f
--- /dev/null
@@ -0,0 +1,132 @@
+#include <cstdio>
+#include <cmath>
+
+#define SK_GL
+#include <GLFW/glfw3.h>
+
+#include <rive_tizen.hpp>
+
+#define WIDTH 1280
+#define HEIGHT 720
+
+RiveTizen *mRiveTizenAdapter;
+rive::Artboard *mArtboard;
+std::vector<rive::LinearAnimationInstance*> mAnimations;
+
+static void loadRivFile(const char* filename) {
+    FILE* fp = fopen(filename, "rb");
+    if (!fp) {
+        fprintf(stderr, "[%s:%d][%s]:ERROR file %s opening failure\n", __FILE__, __LINE__, __func__, filename);
+        return;
+    }
+    fseek(fp, 0, SEEK_END);
+    size_t size = ftell(fp);
+    fseek(fp, 0, SEEK_SET);
+
+    std::vector<uint8_t> bytes;
+    bytes.resize(size);
+    if (fread(bytes.data(), 1, size, fp) != size) {
+        bytes.clear();
+        fprintf(stderr, "[%s:%d][%s]:ERROR failed to read all of %s\n", __FILE__, __LINE__, __func__, filename);
+        return;
+    }
+
+    mRiveTizenAdapter->loadRiveResource(&bytes[0], bytes.size());
+
+    mArtboard = mRiveTizenAdapter->getArtboard();
+
+    for (unsigned int i = 0; i < mArtboard->animationCount(); i++) {
+        mAnimations.emplace_back(mRiveTizenAdapter->createLinearAnimationInstance(i));
+    }
+}
+
+static void glfwCursorPosCallback(GLFWwindow* window, double x, double y) {
+
+}
+static void glfwMouseButtonCallback(GLFWwindow* window, int button, int action, int mods) {
+
+}
+
+static void glfwErrorCallback(int error, const char* description) {
+    puts(description);
+}
+
+static void glfwDropCallback(GLFWwindow* window, int count, const char** paths) {
+    loadRivFile(paths[0]);
+}
+
+int main() {
+    if (!glfwInit()) {
+        fprintf(stderr, "Failed to initialize glfw.\n");
+        return 1;
+    }
+    glfwSetErrorCallback(glfwErrorCallback);
+
+    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
+    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
+    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Rive-skia GPU Viewer", NULL, NULL);
+    if (window == nullptr) {
+        fprintf(stderr, "Failed to make window or GL.\n");
+        glfwTerminate();
+        return 1;
+    }
+
+    glfwSetDropCallback(window, glfwDropCallback);
+    glfwSetCursorPosCallback(window, glfwCursorPosCallback);
+    glfwSetMouseButtonCallback(window, glfwMouseButtonCallback);
+    glfwMakeContextCurrent(window);
+
+    // Enable VSYNC.
+    glfwSwapInterval(1);
+
+    // Init rive-tizen and skia backend
+    mRiveTizenAdapter = new RiveTizen();
+    mRiveTizenAdapter->createCanvas(nullptr, WIDTH, HEIGHT, WIDTH);
+
+    printf("Viewer loaded. Drag and drop .riv file to load.\n");
+    loadRivFile("buggy.riv");
+
+    int width = 0, height = 0;
+    int lastScreenWidth = 0, lastScreenHeight = 0;
+    double lastTime = glfwGetTime();
+
+    while (!glfwWindowShouldClose(window)) {
+        glfwGetFramebufferSize(window, &width, &height);
+
+        if (width != lastScreenWidth || height != lastScreenHeight) {
+            lastScreenWidth = width;
+            lastScreenHeight = height;
+            mRiveTizenAdapter->createCanvas(nullptr, width, height, width);
+        }
+
+        double time = glfwGetTime();
+        float elapsed = (float)(time - lastTime);
+        lastTime = time;
+
+        int i = 0;
+        for(auto& animation : mAnimations) {
+            //const std::string& name = animation->name();
+            if (i == 1 || i == 3) {
+                mRiveTizenAdapter->animationAdvanceApply(animation, elapsed);
+            }
+            ++i;
+        }
+
+        clock_t begin = clock();
+
+        if (!mRiveTizenAdapter->render(elapsed, WIDTH, HEIGHT)) {
+            printf("[%s:%d][%s] ERROR\n", __FILE__, __LINE__, __func__);
+        }
+
+        clock_t end = clock();
+        double time_spent = (double)(end - begin) / CLOCKS_PER_SEC * 1e3;
+        printf("Rendering time %0.3f ms\n", time_spent);
+
+        glfwSwapBuffers(window);
+        glfwPollEvents();
+    }
+
+    glfwDestroyWindow(window);
+    glfwTerminate();
+    exit(EXIT_SUCCESS);
+}