[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / model-motion / motion-data-load-task.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-scene3d/internal/model-motion/motion-data-load-task.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-scene3d/public-api/loader/bvh-loader.h>
26 #include <dali-scene3d/public-api/loader/facial-animation-loader.h>
27
28 namespace Dali
29 {
30 namespace Scene3D
31 {
32 namespace Internal
33 {
34 MotionDataLoadTask::MotionDataLoadTask(const std::string& path, bool useRootTranslationOnly, const Vector3& scale, CallbackBase* callback)
35 : AsyncTask(callback),
36   mFileUrl(path),
37   mRawBuffer(nullptr),
38   mRawBufferLength(0),
39   mScale(scale),
40   mUseRootTranslationOnly(useRootTranslationOnly),
41   mAnimationDefinition{},
42   mLoadMethod(MotionDataLoadTask::LoadMethod::BVH_FILE)
43 {
44 }
45
46 MotionDataLoadTask::MotionDataLoadTask(const uint8_t* rawBuffer, int rawBufferLength, bool useRootTranslationOnly, const Vector3& scale, CallbackBase* callback)
47 : AsyncTask(callback),
48   mFileUrl(),
49   mRawBuffer(nullptr),
50   mRawBufferLength(rawBufferLength),
51   mScale(scale),
52   mUseRootTranslationOnly(useRootTranslationOnly),
53   mAnimationDefinition{},
54   mLoadMethod(MotionDataLoadTask::LoadMethod::BVH_BUFFER)
55 {
56   // To keep life of raw buffer, let us copy data now.
57   mRawBuffer = new uint8_t[mRawBufferLength];
58   memcpy(mRawBuffer, rawBuffer, sizeof(uint8_t) * mRawBufferLength);
59 }
60
61 MotionDataLoadTask::MotionDataLoadTask(const std::string& url, CallbackBase* callback)
62 : AsyncTask(callback),
63   mFileUrl(url),
64   mRawBuffer(nullptr),
65   mRawBufferLength(0),
66   mScale(),
67   mUseRootTranslationOnly(false),
68   mAnimationDefinition{},
69   mLoadMethod(MotionDataLoadTask::LoadMethod::FACIAL_FILE)
70 {
71 }
72
73 MotionDataLoadTask::MotionDataLoadTask(const uint8_t* rawBuffer, int rawBufferLength, CallbackBase* callback)
74 : AsyncTask(callback),
75   mFileUrl(),
76   mRawBuffer(nullptr),
77   mRawBufferLength(rawBufferLength),
78   mScale(),
79   mUseRootTranslationOnly(false),
80   mAnimationDefinition{},
81   mLoadMethod(MotionDataLoadTask::LoadMethod::FACIAL_BUFFER)
82 {
83   // To keep life of raw buffer, let us copy data now.
84   mRawBuffer = new uint8_t[mRawBufferLength];
85   memcpy(mRawBuffer, rawBuffer, sizeof(uint8_t) * mRawBufferLength);
86 }
87
88 MotionDataLoadTask::~MotionDataLoadTask()
89 {
90   if(mRawBuffer)
91   {
92     delete[] mRawBuffer;
93   }
94 }
95
96 void MotionDataLoadTask::Process()
97 {
98   switch(mLoadMethod)
99   {
100     case LoadMethod::BVH_FILE:
101     {
102       mAnimationDefinition = std::move(Loader::LoadBvh(mFileUrl, "LoadedBvhMotionData", mUseRootTranslationOnly, mScale));
103       break;
104     }
105     case LoadMethod::BVH_BUFFER:
106     {
107       mAnimationDefinition = std::move(Loader::LoadBvhFromBuffer(mRawBuffer, mRawBufferLength, "LoadedBvhMotionData", mUseRootTranslationOnly, mScale));
108       break;
109     }
110     case LoadMethod::FACIAL_FILE:
111     {
112       mAnimationDefinition = std::move(Loader::LoadFacialAnimation(mFileUrl));
113       break;
114     }
115     case LoadMethod::FACIAL_BUFFER:
116     {
117       mAnimationDefinition = std::move(Loader::LoadFacialAnimationFromBuffer(mRawBuffer, mRawBufferLength));
118       break;
119     }
120   }
121 }
122
123 bool MotionDataLoadTask::IsReady()
124 {
125   return true;
126 }
127
128 const Scene3D::Loader::AnimationDefinition& MotionDataLoadTask::GetAnimationDefinition() const
129 {
130   return mAnimationDefinition;
131 }
132
133 } // namespace Internal
134
135 } // namespace Scene3D
136
137 } // namespace Dali