From 4a9b008b83d69388e70a2a7bffa31da1b2c15a1a Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Fri, 13 Oct 2023 11:18:32 +0900 Subject: [PATCH] examples: allow gl test --- src/examples/Animation.cpp | 104 +++++++++++++++++++++++++------- src/examples/Lottie.cpp | 119 +++++++++++++++++++++++++++++++------ 2 files changed, 184 insertions(+), 39 deletions(-) diff --git a/src/examples/Animation.cpp b/src/examples/Animation.cpp index 7d57339d..6a6fa6e0 100644 --- a/src/examples/Animation.cpp +++ b/src/examples/Animation.cpp @@ -20,14 +20,15 @@ * SOFTWARE. */ -#define NO_GL_EXAMPLE - #include "Common.h" /************************************************************************/ /* Drawing Commands */ /************************************************************************/ +static unique_ptr animation; +static Elm_Transit *transit; + void tvgUpdateCmds(tvg::Canvas* canvas, tvg::Animation* animation, float progress) { if (!canvas) return; @@ -41,21 +42,8 @@ void tvgUpdateCmds(tvg::Canvas* canvas, tvg::Animation* animation, float progres } } - -/************************************************************************/ -/* Sw Engine Test Code */ -/************************************************************************/ - -static unique_ptr swCanvas; -static unique_ptr animation; -static Elm_Transit *transit; - -void tvgSwTest(uint32_t* buffer) +void tvgDrawCmds(tvg::Canvas* canvas) { - //Create a Canvas - swCanvas = tvg::SwCanvas::gen(); - swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888); - //Animation Controller animation = tvg::Animation::gen(); auto picture = animation->picture(); @@ -65,7 +53,7 @@ void tvgSwTest(uint32_t* buffer) shape->appendRect(0, 0, WIDTH, HEIGHT); shape->fill(50, 50, 50); - if (swCanvas->push(std::move(shape)) != tvg::Result::Success) return; + if (canvas->push(std::move(shape)) != tvg::Result::Success) return; if (picture->load(EXAMPLE_DIR"/sample.json") != tvg::Result::Success) { cout << "Lottie is not supported. Did you enable Lottie Loader?" << endl; @@ -89,7 +77,7 @@ void tvgSwTest(uint32_t* buffer) picture->scale(scale); picture->translate(shiftX, shiftY); - swCanvas->push(tvg::cast(picture)); + canvas->push(tvg::cast(picture)); //Run animation loop elm_transit_duration_set(transit, animation->duration()); @@ -97,6 +85,22 @@ void tvgSwTest(uint32_t* buffer) elm_transit_go(transit); } + +/************************************************************************/ +/* Sw Engine Test Code */ +/************************************************************************/ + +static unique_ptr swCanvas; + +void tvgSwTest(uint32_t* buffer) +{ + //Create a Canvas + swCanvas = tvg::SwCanvas::gen(); + swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888); + + tvgDrawCmds(swCanvas.get()); +} + void drawSwView(void* data, Eo* obj) { if (swCanvas->draw() == tvg::Result::Success) { @@ -115,30 +119,86 @@ void transitSwCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progre } +/************************************************************************/ +/* GL Engine Test Code */ +/************************************************************************/ + +static unique_ptr glCanvas; + +void initGLview(Evas_Object *obj) +{ + static constexpr auto BPP = 4; + + //Create a Canvas + glCanvas = tvg::GlCanvas::gen(); + glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT); + + tvgDrawCmds(glCanvas.get()); +} + +void drawGLview(Evas_Object *obj) +{ + auto gl = elm_glview_gl_api_get(obj); + gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + gl->glClear(GL_COLOR_BUFFER_BIT); + + if (glCanvas->draw() == tvg::Result::Success) { + glCanvas->sync(); + } +} + +void transitGlCb(Elm_Transit_Effect *effect, Elm_Transit* transit, double progress) +{ + tvgUpdateCmds(glCanvas.get(), animation.get(), progress); + elm_glview_changed_set((Evas_Object*)effect); +} + + /************************************************************************/ /* Main Code */ /************************************************************************/ int main(int argc, char **argv) { + tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw; + + if (argc > 1) { + if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl; + } + + //Initialize ThorVG Engine + if (tvgEngine == tvg::CanvasEngine::Sw) { + cout << "tvg engine: software" << endl; + } else { + cout << "tvg engine: opengl" << endl; + } + //Threads Count auto threads = std::thread::hardware_concurrency(); if (threads > 0) --threads; //Allow the designated main thread capacity //Initialize ThorVG Engine - if (tvg::Initializer::init(tvg::CanvasEngine::Sw, threads) == tvg::Result::Success) { + if (tvg::Initializer::init(tvgEngine, threads) == tvg::Result::Success) { elm_init(argc, argv); transit = elm_transit_add(); - auto view = createSwView(); - elm_transit_effect_add(transit, transitSwCb, view, nullptr); + + if (tvgEngine == tvg::CanvasEngine::Sw) { + auto view = createSwView(1024, 1024); + elm_transit_effect_add(transit, transitSwCb, view, nullptr); + } else { + auto view = createGlView(1024, 1024); + elm_transit_effect_add(transit, transitGlCb, view, nullptr); + } elm_run(); elm_shutdown(); //Terminate ThorVG Engine - tvg::Initializer::term(tvg::CanvasEngine::Sw); + tvg::Initializer::term(tvgEngine); + } else { + cout << "engine is not supported" << endl; } return 0; diff --git a/src/examples/Lottie.cpp b/src/examples/Lottie.cpp index 0c189600..e846d753 100644 --- a/src/examples/Lottie.cpp +++ b/src/examples/Lottie.cpp @@ -20,7 +20,6 @@ * SOFTWARE. */ -#define NO_GL_EXAMPLE #include #include "Common.h" @@ -34,10 +33,9 @@ #define SIZE (WIDTH/NUM_PER_ROW) static int counter = 0; -static Eo* view = nullptr; static std::vector> animations; static std::vector transitions; -static unique_ptr swCanvas; +static tvg::Canvas* canvas; //performance measure static double updateTime = 0; @@ -101,28 +99,24 @@ void tvgUpdateCmds(Elm_Transit_Effect *effect, Elm_Transit* transit, double prog auto before = ecore_time_get(); animation->frame(frame); - swCanvas->update(animation->picture()); + canvas->update(animation->picture()); auto after = ecore_time_get(); updateTime += after - before; } } -void tvgSwTest(uint32_t* buffer) +void tvgDrawCmds(tvg::Canvas* canvas) { - //Create a Canvas - swCanvas = tvg::SwCanvas::gen(); - swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888); - //Background auto shape = tvg::Shape::gen(); shape->appendRect(0, 0, WIDTH, HEIGHT); shape->fill(75, 75, 75); - if (swCanvas->push(std::move(shape)) != tvg::Result::Success) return; + if (canvas->push(std::move(shape)) != tvg::Result::Success) return; + + eina_file_dir_list(EXAMPLE_DIR, EINA_TRUE, lottieDirCallback, canvas); - eina_file_dir_list(EXAMPLE_DIR, EINA_TRUE, lottieDirCallback, swCanvas.get()); - //Run animation loop for (auto& animation : animations) { Elm_Transit* transit = elm_transit_add(); @@ -131,10 +125,28 @@ void tvgSwTest(uint32_t* buffer) elm_transit_repeat_times_set(transit, -1); elm_transit_go(transit); - swCanvas->push(tvg::cast(animation->picture())); + canvas->push(tvg::cast(animation->picture())); } } + +/************************************************************************/ +/* Sw Engine Test Code */ +/************************************************************************/ + +static unique_ptr swCanvas; + +void tvgSwTest(uint32_t* buffer) +{ + //Create a Canvas + swCanvas = tvg::SwCanvas::gen(); + swCanvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888); + + tvgDrawCmds(swCanvas.get()); + + canvas = swCanvas.get(); +} + void drawSwView(void* data, Eo* obj) { auto before = ecore_time_get(); @@ -158,7 +170,7 @@ void drawSwView(void* data, Eo* obj) updateTime = 0; } -Eina_Bool animatorCb(void *data) +Eina_Bool animatorSwCb(void *data) { Eo* img = (Eo*) data; evas_object_image_data_update_add(img, 0, 0, WIDTH, HEIGHT); @@ -168,23 +180,95 @@ Eina_Bool animatorCb(void *data) } +/************************************************************************/ +/* GL Engine Test Code */ +/************************************************************************/ + +static unique_ptr glCanvas; + +void initGLview(Evas_Object *obj) +{ + static constexpr auto BPP = 4; + + //Create a Canvas + glCanvas = tvg::GlCanvas::gen(); + glCanvas->target(nullptr, WIDTH * BPP, WIDTH, HEIGHT); + + tvgDrawCmds(glCanvas.get()); + + canvas = glCanvas.get(); +} + +void drawGLview(Evas_Object *obj) +{ + auto before = ecore_time_get(); + + auto gl = elm_glview_gl_api_get(obj); + gl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + gl->glClear(GL_COLOR_BUFFER_BIT); + + if (glCanvas->draw() == tvg::Result::Success) { + glCanvas->sync(); + } + + auto after = ecore_time_get(); + + auto rasterTime = after - before; + + ++cnt; + + accumUpdateTime += updateTime; + accumRasterTime += rasterTime; + accumTotalTime += (updateTime + rasterTime); + + printf("[%5d]: update = %fs, raster = %fs, total = %fs\n", cnt, accumUpdateTime / cnt, accumRasterTime / cnt, accumTotalTime / cnt); + + updateTime = 0; +} + +Eina_Bool animatorGlCb(void *data) +{ + elm_glview_changed_set((Evas_Object*)data); + + return ECORE_CALLBACK_RENEW; +} + + /************************************************************************/ /* Main Code */ /************************************************************************/ int main(int argc, char **argv) { + tvg::CanvasEngine tvgEngine = tvg::CanvasEngine::Sw; + + if (argc > 1) { + if (!strcmp(argv[1], "gl")) tvgEngine = tvg::CanvasEngine::Gl; + } + + //Initialize ThorVG Engine + if (tvgEngine == tvg::CanvasEngine::Sw) { + cout << "tvg engine: software" << endl; + } else { + cout << "tvg engine: opengl" << endl; + } + //Threads Count auto threads = std::thread::hardware_concurrency(); if (threads > 0) --threads; //Allow the designated main thread capacity //Initialize ThorVG Engine - if (tvg::Initializer::init(tvg::CanvasEngine::Sw, 3) == tvg::Result::Success) { + if (tvg::Initializer::init(tvgEngine, 3) == tvg::Result::Success) { elm_init(argc, argv); - view = createSwView(1280, 1280); - ecore_animator_add(animatorCb, view); + if (tvgEngine == tvg::CanvasEngine::Sw) { + auto view = createSwView(1280, 1280); + ecore_animator_add(animatorSwCb, view); + } else { + auto view = createGlView(1280, 1280); + ecore_animator_add(animatorGlCb, view); + } elm_run(); elm_shutdown(); @@ -195,5 +279,6 @@ int main(int argc, char **argv) } else { cout << "engine is not supported" << endl; } + return 0; }