ImageComposite.h (4676B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef MOZILLA_GFX_IMAGECOMPOSITE_H 8 #define MOZILLA_GFX_IMAGECOMPOSITE_H 9 10 #include "CompositableHost.h" // for CompositableTextureHostRef 11 #include "mozilla/gfx/2D.h" 12 #include "mozilla/TimeStamp.h" // for TimeStamp 13 #include "nsTArray.h" 14 15 namespace mozilla { 16 namespace layers { 17 18 /** 19 * Implements Image selection logic. 20 */ 21 class ImageComposite { 22 public: 23 static const float BIAS_TIME_MS; 24 25 explicit ImageComposite(); 26 virtual ~ImageComposite(); 27 28 int32_t GetFrameID() { 29 const TimedImage* img = ChooseImage(); 30 return img ? img->mFrameID : -1; 31 } 32 33 int32_t GetProducerID() { 34 const TimedImage* img = ChooseImage(); 35 return img ? img->mProducerID : -1; 36 } 37 38 int32_t GetLastFrameID() const { return mLastFrameID; } 39 int32_t GetLastProducerID() const { return mLastProducerID; } 40 uint32_t GetDroppedFramesAndReset() { 41 uint32_t dropped = mDroppedFrames; 42 mDroppedFrames = 0; 43 return dropped; 44 } 45 46 enum Bias { 47 // Don't apply bias to frame times 48 BIAS_NONE, 49 // Apply a negative bias to frame times to keep them before the vsync time 50 BIAS_NEGATIVE, 51 // Apply a positive bias to frame times to keep them after the vsync time 52 BIAS_POSITIVE, 53 }; 54 55 protected: 56 virtual TimeStamp GetCompositionTime() const = 0; 57 virtual CompositionOpportunityId GetCompositionOpportunityId() const = 0; 58 virtual void AppendImageCompositeNotification( 59 const ImageCompositeNotificationInfo& aInfo) const = 0; 60 61 struct TimedImage { 62 CompositableTextureHostRef mTextureHost; 63 TimeStamp mTimeStamp; 64 gfx::IntRect mPictureRect; 65 int32_t mFrameID; 66 int32_t mProducerID; 67 }; 68 69 /** 70 * ChooseImage is guaranteed to return the same TimedImage every time it's 71 * called during the same composition, up to the end of Composite() --- 72 * it depends only on mImages, mCompositor->GetCompositionTime(), and mBias. 73 * mBias is updated at the end of Composite(). 74 */ 75 const TimedImage* ChooseImage(); 76 int ChooseImageIndex(); 77 const TimedImage* GetImage(size_t aIndex) const; 78 size_t ImagesCount() const { return mImages.Length(); } 79 const nsTArray<TimedImage>& Images() const { return mImages; } 80 81 void RemoveImagesWithTextureHost(TextureHost* aTexture); 82 void ClearImages(); 83 void SetImages(nsTArray<TimedImage>&& aNewImages); 84 85 protected: 86 // Send ImageComposite notifications and update the ChooseImage bias. 87 void OnFinishRendering(int aImageIndex, const TimedImage* aImage, 88 base::ProcessId aProcessId, 89 const CompositableHandle& aHandle); 90 91 int32_t mLastFrameID = -1; 92 int32_t mLastProducerID = -1; 93 94 private: 95 nsTArray<TimedImage> mImages; 96 TimeStamp GetBiasedTime(const TimeStamp& aInput) const; 97 98 // Called when we know that frames older than aImage will never get another 99 // chance to be displayed. 100 // Counts frames in mImages that were skipped, and adds the skipped count to 101 // mSkippedFramesSinceLastComposite. 102 void CountSkippedFrames(const TimedImage* aImage); 103 104 // Update mLastFrameID and mLastProducerID, and report dropped frames. 105 // Returns whether the frame changed. 106 bool UpdateCompositedFrame(int aImageIndex, 107 bool aWasVisibleAtPreviousComposition); 108 109 void UpdateBias(size_t aImageIndex, bool aFrameUpdated); 110 111 // Emit a profiler marker about video frame timestamp jitter. 112 void DetectTimeStampJitter(const TimedImage* aNewImage); 113 114 /** 115 * Bias to apply to the next frame. 116 */ 117 Bias mBias = BIAS_NONE; 118 119 // Video frames that were in mImages but that will never reach the screen. 120 // This count is reset in UpdateCompositedFrame. 121 int32_t mSkippedFramesSinceLastComposite = 0; 122 123 // The number of dropped frames since the last call to 124 // GetDroppedFramesAndReset(). Not all skipped frames are considered "dropped" 125 // - for example, videos that are scrolled out of view or videos in background 126 // tabs are expected to skip frames, and those skipped frames are not counted 127 // as dropped frames. 128 uint32_t mDroppedFrames = 0; 129 130 // The composition opportunity IDs of the last calls to ChooseImageIndex and 131 // UpdateCompositedFrame, respectively. 132 CompositionOpportunityId mLastChooseImageIndexComposition; 133 CompositionOpportunityId mLastFrameUpdateComposition; 134 }; 135 136 } // namespace layers 137 } // namespace mozilla 138 139 #endif // MOZILLA_GFX_IMAGECOMPOSITE_H