rive-animation-view: Use adaptation layer. 81/280281/1
authorMichal Szczecinski <m.szczecinsk@partner.samsung.com>
Wed, 17 Aug 2022 20:17:58 +0000 (22:17 +0200)
committerTaehyub Kim <taehyub.kim@samsung.com>
Fri, 26 Aug 2022 06:42:12 +0000 (15:42 +0900)
Using the adaptation layer allows to update rive-cpp to 2.0 version
without changes in this codebase.

Change-Id: I737c76cc91d102b1918e56c2b1c7da22dd43029d

dali-extension/internal/rive-animation-view/animation-renderer/rive-animation-renderer.cpp
dali-extension/internal/rive-animation-view/animation-renderer/rive-animation-renderer.h

index 382336e..44910ce 100644 (file)
@@ -33,7 +33,6 @@
 #include <rive/shapes/paint/fill.hpp>
 #include <rive/shapes/paint/solid_color.hpp>
 #include <rive/shapes/paint/stroke.hpp>
-#include <rive/tvg_renderer.hpp>
 
 // INTERNAL INCLUDES
 #include <dali-extension/internal/rive-animation-view/animation-renderer/rive-animation-renderer-manager.h>
@@ -65,8 +64,6 @@ RiveAnimationRenderer::RiveAnimationRenderer()
   mTargetSurface(),
   mUploadCompletedSignal(),
   mTbmQueue(NULL),
-  mSwCanvas(nullptr),
-  mFile(nullptr),
   mArtboard(nullptr),
   mAnimation(nullptr),
   mStartFrameNumber(0),
@@ -78,24 +75,23 @@ RiveAnimationRenderer::RiveAnimationRenderer()
   mFrameRate(60.0f),
   mResourceReady(false),
   mShaderChanged(false),
-  mResourceReadyTriggered(false)
+  mResourceReadyTriggered(false),
+  mRiveTizenAdapter(nullptr)
 {
-  tvg::Initializer::init(tvg::CanvasEngine::Sw, 0);
+  mRiveTizenAdapter = new RiveTizen();
 }
 
 RiveAnimationRenderer::~RiveAnimationRenderer()
 {
   Dali::Mutex::ScopedLock lock(mMutex);
-
   ClearRiveAnimations();
 
-  if(mFile)
+  if (mRiveTizenAdapter)
   {
-    delete mFile;
+    delete mRiveTizenAdapter;
   }
 
-  mSwCanvas->clear();
-  tvg::Initializer::term(tvg::CanvasEngine::Sw);
+  mRiveTizenAdapter = nullptr;
 
   DALI_LOG_INFO(gRiveAnimationLogFilter, Debug::Verbose, "RiveAnimationRenderer::~RiveAnimationRenderer: this = %p\n", this);
 }
@@ -116,30 +112,15 @@ void RiveAnimationRenderer::LoadRiveFile(const std::string& filename)
     return;
   }
 
-  auto reader = rive::BinaryReader(&bytes[0], bytes.Size());
-
-  if(mFile)
-  {
-    delete mFile;
-  }
-
-  auto result = rive::File::import(reader, &mFile);
-  if(result != rive::ImportResult::success)
-  {
-    DALI_LOG_ERROR("Failed to import %s", filename.c_str());
-    return;
-  }
-
-  mArtboard = mFile->artboard();
-  mArtboard->advance(0.0f);
-
   ClearRiveAnimations();
+  mRiveTizenAdapter->loadRiveResource(&bytes[0], bytes.Size());
+  mArtboard = mRiveTizenAdapter->getArtboard();
 
   for(unsigned int i = 0; i < mArtboard->animationCount(); i++)
   {
     auto               animation = mArtboard->animation(i);
     const std::string& name      = animation->name();
-    mAnimations.emplace_back(Animation(new rive::LinearAnimationInstance(animation), name, false));
+    mAnimations.emplace_back(Animation(mRiveTizenAdapter->createLinearAnimationInstance(i), name, false));
   }
 
   mAnimation = mArtboard->firstAnimation();
@@ -236,7 +217,7 @@ void RiveAnimationRenderer::SetSize(uint32_t width, uint32_t height)
 bool RiveAnimationRenderer::Render(double elapsed)
 {
   Dali::Mutex::ScopedLock lock(mMutex);
-  if(!mTbmQueue || !mTargetSurface || !mArtboard || mAnimations.empty())
+  if(!mTbmQueue || !mTargetSurface || mAnimations.empty())
   {
     return false;
   }
@@ -283,13 +264,7 @@ bool RiveAnimationRenderer::Render(double elapsed)
 
   tbm_surface_internal_ref(tbmSurface);
 
-  if(!mSwCanvas)
-  {
-    mSwCanvas = tvg::SwCanvas::gen();
-    mSwCanvas->mempool(tvg::SwCanvas::MempoolPolicy::Individual);
-  }
-  mSwCanvas->clear();
-  mSwCanvas->target((uint32_t*)buffer, info.planes[0].stride / 4, info.width, info.height, tvg::SwCanvas::ARGB8888);
+  mRiveTizenAdapter->createCanvas(buffer, info.width, info.height, info.planes[0].stride / 4);
 
   // Render Rive animation by elapsed time
   for(auto& animation : mAnimations)
@@ -298,32 +273,16 @@ bool RiveAnimationRenderer::Render(double elapsed)
     {
       if(animation.enable)
       {
-        animation.instance->advance(elapsed);
-        animation.instance->apply(mArtboard);
+        mRiveTizenAdapter->animationAdvanceApply(animation.instance.get(), elapsed);
       }
       else if(animation.elapsed >= 0.0f)
       {
-        animation.instance->time(animation.elapsed);
-        animation.instance->apply(mArtboard);
+        mRiveTizenAdapter->animationApply(animation.instance.get(), elapsed);
       }
     }
   }
-  mArtboard->advance(elapsed);
-
-  rive::TvgRenderer renderer(mSwCanvas.get());
-  renderer.save();
-  renderer.align(rive::Fit::contain,
-                 rive::Alignment::center,
-                 rive::AABB(0, 0, info.width, info.height),
-                 mArtboard->bounds());
-  mArtboard->draw(&renderer);
-  renderer.restore();
-
-  if(mSwCanvas->draw() == tvg::Result::Success)
-  {
-    mSwCanvas->sync();
-  }
-  else
+
+  if (!mRiveTizenAdapter->render(elapsed, info.width, info.height))
   {
     tbm_surface_unmap(tbmSurface);
     tbm_surface_queue_cancel_dequeue(mTbmQueue, tbmSurface);
@@ -375,8 +334,7 @@ void RiveAnimationRenderer::EnableAnimation(const std::string& animationName, bo
     {
       if(mAnimations[i].instance)
       {
-        auto animation = mArtboard->animation(i);
-        mAnimations[i].instance.reset(new rive::LinearAnimationInstance(animation));
+        mAnimations[i].instance.reset(mRiveTizenAdapter->createLinearAnimationInstance(i));
       }
       mAnimations[i].enable = enable;
       return;
index 64f4214..edfa5f1 100755 (executable)
 #include <tbm_surface.h>
 #include <tbm_surface_queue.h>
 
-// RIVE - THORVG INCLUDES
-#include <thorvg.h>
+// RIVE - INCLUDES
+#include <rive_tizen.hpp>
 #include <rive/animation/linear_animation_instance.hpp>
 #include <rive/artboard.hpp>
-//
 
 // INTERNAL INCLUDES
 #include <dali-extension/internal/rive-animation-view/animation-renderer/rive-animation-renderer-event-handler.h>
@@ -259,8 +258,6 @@ private:
   NativeImageSourceQueuePtr              mTargetSurface;         ///< The target surface
   UploadCompletedSignalType              mUploadCompletedSignal; ///< Upload completed signal
   tbm_surface_queue_h                    mTbmQueue;              ///< Tbm surface queue handle
-  std::unique_ptr<tvg::SwCanvas>         mSwCanvas;              ///< ThorVG SW canvas handle
-  rive::File                             *mFile;                 ///< Rive file handle
   rive::Artboard                         *mArtboard;             ///< Rive artboard handle
   std::vector<Animation>                 mAnimations;            ///< Rive animations
   rive::LinearAnimation                  *mAnimation;            ///< Rive animation handle
@@ -274,6 +271,7 @@ private:
   bool                                   mResourceReady;         ///< Whether the resource is ready
   bool                                   mShaderChanged;         ///< Whether the shader is changed to support native image
   bool                                   mResourceReadyTriggered;///< Whether the resource ready is triggered
+  RiveTizen                              *mRiveTizenAdapter;     ///< Rive Tizen Adapter
 };
 
 } // namespace Internal