llpuppetmotion.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /**
  2. * @file llpuppetmotion.h
  3. * @brief Declaration of the LLPuppetMotion class.
  4. *
  5. * $LicenseInfo:firstyear=2021&license=viewergpl$
  6. *
  7. * Copyright (c) 2021-2022, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #if LL_PUPPETRY
  33. #ifndef LL_LLPUPPETMOTION_H
  34. #define LL_LLPUPPETMOTION_H
  35. #include <deque>
  36. #include <map>
  37. #include <vector>
  38. #include "lldatapacker.h"
  39. #include "llframetimer.h"
  40. #include "llik.h"
  41. #include "llmotion.h"
  42. #include "llpointer.h"
  43. #include "llquaternion.h"
  44. class LLMessageSystem;
  45. class LLViewerRegion;
  46. ///////////////////////////////////////////////////////////////////////////////
  47. // LLPuppet*Event classes (reside in llpuppetevent.h in LL's viewer sources).
  48. // While they are only used internally by LLPuppetMotion, their declarations
  49. // need to be placed here (instead of just in llpuppetmotion.cpp) to prevent
  50. // forward declaration compilation errors in LLPuppetMotion with gcc... HB
  51. ///////////////////////////////////////////////////////////////////////////////
  52. // Information about an expression event that we want to broadcast
  53. class LLPuppetJointEvent
  54. {
  55. protected:
  56. LOG_CLASS(LLPuppetJointEvent);
  57. public:
  58. enum E_REFERENCE_FRAME
  59. {
  60. ROOT_FRAME = 0,
  61. PARENT_FRAME = 1
  62. };
  63. LL_INLINE LLPuppetJointEvent()
  64. : mJointID(-1),
  65. mRefFrame(ROOT_FRAME),
  66. mMask(0x0),
  67. mRequestID(-1)
  68. {
  69. }
  70. LL_INLINE void setReferenceFrame(S32 frame) { mRefFrame = frame; }
  71. LL_INLINE void setRotation(const LLQuaternion& rotation)
  72. {
  73. mRotation = rotation;
  74. mRotation.normalize();
  75. mMask |= mRefFrame == PARENT_FRAME ? LLIK::CONFIG_FLAG_LOCAL_ROT
  76. : LLIK::CONFIG_FLAG_TARGET_ROT;
  77. }
  78. LL_INLINE void setPosition(const LLVector3& position)
  79. {
  80. mPosition = position;
  81. mMask |= mRefFrame == PARENT_FRAME ? LLIK::CONFIG_FLAG_LOCAL_POS
  82. : LLIK::CONFIG_FLAG_TARGET_POS;
  83. }
  84. LL_INLINE void setScale(const LLVector3& scale)
  85. {
  86. mScale = scale;
  87. mMask |= LLIK::CONFIG_FLAG_LOCAL_SCALE;
  88. }
  89. LL_INLINE void disableConstraint()
  90. {
  91. mMask |= LLIK::CONFIG_FLAG_DISABLE_CONSTRAINT;
  92. }
  93. LL_INLINE void enableReporting(S32 reqid)
  94. {
  95. mMask |= LLIK::CONFIG_FLAG_ENABLE_REPORTING;
  96. mRequestID = reqid;
  97. }
  98. LL_INLINE S32 getRequestID() const { return mRequestID; }
  99. LL_INLINE void setJointID(S32 id) { mJointID = S16(id); }
  100. LL_INLINE S16 getJointID() const { return mJointID; }
  101. LL_INLINE LLQuaternion getRotation() const { return mRotation; }
  102. LL_INLINE LLVector3 getPosition() const { return mPosition; }
  103. LL_INLINE LLVector3 getScale() const { return mScale; }
  104. size_t getSize() const;
  105. size_t pack(U8* wptr) const;
  106. size_t unpack(U8* wptr);
  107. void interpolate(F32 del, const LLPuppetJointEvent& a,
  108. const LLPuppetJointEvent& b);
  109. LL_INLINE bool isEmpty() const { return mMask == 0; }
  110. LL_INLINE U8 getMask() const { return mMask; }
  111. private:
  112. LLQuaternion mRotation;
  113. LLVector3 mPosition;
  114. LLVector3 mScale;
  115. S32 mRefFrame;
  116. S32 mRequestID; // Used for reporting.
  117. U16 mJointID;
  118. U8 mMask;
  119. };
  120. // An event is snapshot at mTimestamp (msec from start) with 1 or more joints
  121. // that have moved or rotated. These snapshots along with the time delta are
  122. // used to reconstruct the animation on the receiving clients.
  123. class LLPuppetEvent
  124. {
  125. protected:
  126. LOG_CLASS(LLPuppetEvent);
  127. public:
  128. typedef std::deque<LLPuppetJointEvent> joint_deq_t;
  129. LL_INLINE LLPuppetEvent()
  130. : mTimestamp(0)
  131. {
  132. }
  133. LL_INLINE void addJointEvent(const LLPuppetJointEvent& joint_event)
  134. {
  135. mJointEvents.emplace_back(joint_event);
  136. }
  137. bool pack(LLDataPackerBinaryBuffer& buffer, S32& out_num_joints);
  138. bool unpack(LLDataPackerBinaryBuffer& mesgsys);
  139. // For outbound LLPuppetEvents
  140. LL_INLINE void updateTimestamp()
  141. {
  142. mTimestamp = (S32)(LLFrameTimer::getElapsedSeconds() * 1000.0);
  143. }
  144. // For inbound LLPuppetEvents we compute a localized timestamp and slam it
  145. LL_INLINE void setTimestamp(S32 timestamp) { mTimestamp = timestamp; }
  146. LL_INLINE S32 getTimestamp() const { return mTimestamp; }
  147. LL_INLINE U32 getNumJoints() const { return mJointEvents.size(); }
  148. S32 getMinEventSize() const;
  149. public:
  150. joint_deq_t mJointEvents;
  151. private:
  152. S32 mTimestamp; // In milliseconds
  153. };
  154. ///////////////////////////////////////////////////////////////////////////////
  155. // LLPuppetMotion class
  156. ///////////////////////////////////////////////////////////////////////////////
  157. class LLPuppetMotion final : public LLMotion
  158. {
  159. protected:
  160. LOG_CLASS(LLPuppetMotion);
  161. public:
  162. typedef std::map<S16, LLPointer<LLJointState> > state_map_t;
  163. typedef std::vector<S16> jointid_vec_t;
  164. typedef std::deque<LLPuppetEvent> update_deq_t;
  165. typedef std::vector<LLPuppetJointEvent> joint_events_t;
  166. using ik_map_t = LLIK::Solver::joint_config_map_t;
  167. LLPuppetMotion(const LLUUID& id);
  168. virtual ~LLPuppetMotion() = default;
  169. class JointStateExpiry
  170. {
  171. public:
  172. LL_INLINE JointStateExpiry()
  173. : mExpiry(S32_MAX)
  174. {
  175. }
  176. LL_INLINE JointStateExpiry(LLPointer<LLJointState> state, S32 expiry)
  177. : mState(state),
  178. mExpiry(expiry)
  179. {
  180. }
  181. public:
  182. LLPointer<LLJointState> mState;
  183. S32 mExpiry;
  184. };
  185. typedef std::deque<std::pair<S32, LLPuppetJointEvent> > event_queue_t;
  186. class DelayedEventQueue
  187. {
  188. public:
  189. LL_INLINE DelayedEventQueue()
  190. : mLastRemoteTimestamp(-1),
  191. mEventPeriod(100.f),
  192. mEventJitter(50.f)
  193. {
  194. }
  195. void addEvent(S32 remote_timestamp, S32 local_timestamp,
  196. const LLPuppetJointEvent& event);
  197. LL_INLINE event_queue_t& getEventQueue() { return mQueue; }
  198. private:
  199. event_queue_t mQueue;
  200. S32 mLastRemoteTimestamp; // In milliseconds
  201. // EventPeriod and Jitter are dynamically updated but we start with
  202. // these optimistic guesses
  203. F32 mEventPeriod; // In milliseconds
  204. F32 mEventJitter; // In milliseconds
  205. };
  206. typedef std::map<S16, DelayedEventQueue> evqueues_map_t;
  207. void collectJoints(LLJoint* jointp);
  208. void addExpressionEvent(const LLPuppetJointEvent& event);
  209. void queueOutgoingEvent(const LLPuppetEvent& event);
  210. void unpackEvents(LLMessageSystem* mesgsys, int blocknum);
  211. void clearAll();
  212. void reportRootRelativePosition(S16 joint_id, S32 request_id);
  213. // Methods to support MotionController and MotionRegistry
  214. static void requestPuppetryStatus(LLViewerRegion* regionp);
  215. static void requestPuppetryStatusCoro(const std::string& capurl);
  216. // Static constructor: all subclasses must implement such a function and
  217. // register it.
  218. LL_INLINE static LLMotion* create(const LLUUID& id)
  219. {
  220. return new LLPuppetMotion(id);
  221. }
  222. // LLMotion overrides.
  223. // Motions must specify whether or not they loop
  224. LL_INLINE bool getLoop() override { return false; }
  225. // Motions must report their total duration
  226. LL_INLINE F32 getDuration() override { return 0.f; }
  227. // Motions must report their "ease in" duration
  228. LL_INLINE F32 getEaseInDuration() override { return 1.f; }
  229. // Motions must report their "ease out" duration.
  230. LL_INLINE F32 getEaseOutDuration() override { return 1.f; }
  231. // Motions must report their priority
  232. // Note: LLMotion::getPriority() is only used to delegate motion-wide
  233. // priority to LLJointStates added to mPose in LLMotion::addJointState()...
  234. // when they have LLJoint::USE_MOTION_PRIORITY.
  235. LL_INLINE LLJoint::JointPriority getPriority() override
  236. {
  237. return mMotionPriority;
  238. }
  239. LL_INLINE void setPriority(LLJoint::JointPriority priority)
  240. {
  241. mMotionPriority = priority;
  242. }
  243. virtual LLMotionBlendType getBlendType() override { return NORMAL_BLEND; }
  244. // Called to determine when a motion should be activated/deactivated based
  245. // on avatar pixel coverage.
  246. LL_INLINE F32 getMinPixelArea() override
  247. {
  248. return 500.f;
  249. }
  250. // Run-time (post constructor) initialization, called after parameters have
  251. // been set. Must return success status and be available for activation.
  252. LLMotionInitStatus onInitialize(LLCharacter* charp) override;
  253. // LLMotionController calls this when it adds this motion to its active
  254. // list. As of 2022.04.21 the return value is never checked.
  255. bool onActivate() override;
  256. // LLMotionController calls this when it removes this motion from its
  257. // active list.
  258. void onDeactivate() override;
  259. // Called per time step. Must return true while it is active, and must
  260. // return false when the motion is completed.
  261. bool onUpdate(F32 time, U8* joint_mask) override;
  262. LL_INLINE bool canDeprecate() override { return false; }
  263. bool needsUpdate() const override;
  264. // End of overrides
  265. void addJointToSkeletonData(LLSD& skeleton_sd, LLJoint* joint,
  266. const LLVector3& parent_rel_pos,
  267. const LLVector3& tip_rel_end_pos);
  268. LLSD getSkeletonData();
  269. void updateSkeletonGeometry();
  270. LL_INLINE static bool enabled()
  271. {
  272. return sIsPuppetryEnabled;
  273. }
  274. // Called from llviewercontrol.cpp, on updates to the "PuppetryAllowed"
  275. // debug setting. HB
  276. static void updatePuppetryEnabling();
  277. private:
  278. static void setPuppetryEnabled(bool enabled, size_t event_size);
  279. void measureArmSpan();
  280. void queueEvent(const LLPuppetEvent& event);
  281. void applyEvent(const LLPuppetJointEvent& event, U64 now,
  282. ik_map_t& configs);
  283. void applyBroadcastEvent(const LLPuppetJointEvent& event, S32 now);
  284. void packEvents();
  285. void pumpOutgoingEvents();
  286. void solveIKAndHarvestResults(ik_map_t& configs, S32 now);
  287. void updateFromExpression(S32 now);
  288. void updateFromBroadcast(S32 now);
  289. void rememberPosedJoint(S16 joint_id, LLPointer<LLJointState> joint_state,
  290. S32 now);
  291. private:
  292. LLFrameTimer mBroadcastTimer; // When to broadcast events.
  293. LLFrameTimer mPlaybackTimer; // Playback what was broadcast
  294. state_map_t mJointStates; // Joints known to IK
  295. evqueues_map_t mEventQueues;
  296. // Recently animated joints and their expiries
  297. typedef std::map<S16, JointStateExpiry> expiries_map_t;
  298. expiries_map_t mJointStateExpiries;
  299. update_deq_t mOutgoingEvents; // LLPuppetEvents to broadcast.
  300. typedef std::vector<LLPointer<LLJointState> > jointstate_vec_t;
  301. jointstate_vec_t mJointsToRemoveFromPose;
  302. joint_events_t mExpressionEvents;
  303. LLIK::Solver mIKSolver;
  304. LLJoint::JointPriority mMotionPriority;
  305. S32 mNextJointStateExpiry;
  306. F32 mRemoteToLocalClockOffset; // In milliseconds
  307. F32 mArmSpan;
  308. bool mIsSelf;
  309. // Simulator reported maximum size for a puppetry event.
  310. static size_t sPuppeteerEventMaxSize;
  311. // true when puppetry is enabled on the simulator and not disabled by the
  312. // user.
  313. static bool sIsPuppetryEnabled;
  314. };
  315. #endif // LL_LLPUPPETMOTION_H
  316. #endif // LL_PUPPETRY