Line data Source code
1 : /** 2 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev> 3 : 4 : Permission is hereby granted, free of charge, to any person obtaining a copy 5 : of this software and associated documentation files (the "Software"), to deal 6 : in the Software without restriction, including without limitation the rights 7 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 : copies of the Software, and to permit persons to whom the Software is 9 : furnished to do so, subject to the following conditions: 10 : 11 : The above copyright notice and this permission notice shall be included in 12 : all copies or substantial portions of the Software. 13 : 14 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 : THE SOFTWARE. 21 : **/ 22 : 23 : #ifndef XENOLITH_CORE_XLCOREFRAMEEMITTER_H_ 24 : #define XENOLITH_CORE_XLCOREFRAMEEMITTER_H_ 25 : 26 : #include "XLCoreAttachment.h" 27 : #include "XLCoreFrameHandle.h" 28 : #include "SPMovingAverage.h" 29 : 30 : namespace STAPPLER_VERSIONIZED stappler::xenolith::core { 31 : 32 : // Frame emitter is an interface, that continuously spawns frames, and can control validity of a frame 33 : class FrameEmitter : public Ref { 34 : public: 35 : virtual ~FrameEmitter(); 36 : 37 : virtual bool init(const Rc<Loop> &, uint64_t frameInterval); 38 : virtual void invalidate(); 39 : 40 : virtual void setFrameSubmitted(FrameHandle &); 41 : virtual bool isFrameValid(const FrameHandle &handle); 42 : 43 : virtual void acquireNextFrame(); 44 : 45 : virtual void dropFrameTimeout(); 46 : 47 : void dropFrames(); 48 : 49 : bool isValid() const { return _valid; } 50 : 51 : void setFrameTime(uint64_t v) { _frame = v; } 52 : uint64_t getFrameTime() const { return _frame; } 53 : 54 21 : void setFrameInterval(uint64_t v) { _frameInterval = v; } 55 : uint64_t getFrameInterval() const { return _frameInterval; } 56 : 57 : const Rc<Loop> &getLoop() const { return _loop; } 58 : 59 : uint64_t getLastFrameTime() const; 60 : uint64_t getAvgFrameTime() const; 61 : uint64_t getAvgFenceTime() const; 62 : 63 : bool isReadyForSubmit() const; 64 : 65 : void setEnableBarrier(bool value); 66 : 67 : Rc<FrameRequest> makeRequest(const FrameContraints &); 68 : Rc<FrameHandle> submitNextFrame(Rc<FrameRequest> &&); 69 : 70 : protected: 71 : virtual void onFrameEmitted(FrameHandle &); 72 : virtual void onFrameSubmitted(FrameHandle &); 73 : virtual void onFrameComplete(FrameHandle &); 74 : virtual void onFrameTimeout(uint64_t order); 75 : virtual void onFrameRequest(bool timeout); 76 : 77 : virtual Rc<FrameHandle> makeFrame(Rc<FrameRequest> &&, bool readyForSubmit); 78 : virtual bool canStartFrame() const; 79 : virtual void scheduleNextFrame(Rc<FrameRequest> &&); 80 : virtual void scheduleFrameTimeout(); 81 : 82 : uint64_t _submitted = 0; 83 : uint64_t _order = 0; 84 : uint64_t _gen = 0; 85 : 86 : bool _valid = true; 87 : std::atomic<uint64_t> _frame = 0; 88 : uint64_t _frameInterval = 1'000'000 / 60; 89 : uint64_t _suboptimal = 0; 90 : 91 : bool _frameTimeoutPassed = true; 92 : bool _nextFrameAcquired = false; 93 : bool _onDemand = true; 94 : bool _enableBarrier = true; 95 : Rc<FrameRequest> _nextFrameRequest; 96 : std::deque<Rc<FrameHandle>> _frames; 97 : std::deque<Rc<FrameHandle>> _framesPending; 98 : 99 : Rc<Loop> _loop; 100 : 101 : uint64_t _lastSubmit = 0; 102 : 103 : std::atomic<uint64_t> _lastFrameTime = 0; 104 : math::MovingAverage<20, uint64_t> _avgFrameTime; 105 : std::atomic<uint64_t> _avgFrameTimeValue = 0; 106 : 107 : math::MovingAverage<20, uint64_t> _avgFenceInterval; 108 : std::atomic<uint64_t> _avgFenceIntervalValue = 0; 109 : 110 : uint64_t _lastTotalFrameTime = 0; 111 : }; 112 : 113 : } 114 : 115 : #endif /* XENOLITH_CORE_XLCOREFRAMEEMITTER_H_ */