SharedSurfacesParent.h (5604B)
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_SHAREDSURFACESPARENT_H 8 #define MOZILLA_GFX_SHAREDSURFACESPARENT_H 9 10 #include <stdint.h> // for uint32_t 11 #include "mozilla/Attributes.h" // for override 12 #include "mozilla/StaticMutex.h" // for StaticMutex 13 #include "mozilla/StaticPtr.h" // for StaticAutoPtr 14 #include "mozilla/RefPtr.h" // for already_AddRefed 15 #include "mozilla/gfx/2D.h" // for SurfaceFormat 16 #include "mozilla/gfx/Point.h" // for IntSize 17 #include "mozilla/layers/LayersSurfaces.h" // for SurfaceDescriptorShared 18 #include "mozilla/layers/SourceSurfaceSharedData.h" 19 #include "mozilla/webrender/WebRenderTypes.h" // for wr::ExternalImageId 20 #include "nsExpirationTracker.h" 21 #include "nsRefPtrHashtable.h" 22 23 namespace mozilla { 24 namespace gfx { 25 class DataSourceSurface; 26 } // namespace gfx 27 28 namespace layers { 29 30 class CompositorManagerParent; 31 class SharedSurfacesMemoryReport; 32 33 class SharedSurfacesParent final { 34 public: 35 static void Initialize(); 36 static void ShutdownRenderThread(); 37 static void Shutdown(); 38 39 // Get without increasing the consumer count. 40 static already_AddRefed<gfx::DataSourceSurface> Get( 41 const wr::ExternalImageId& aId); 42 43 // Get but also increase the consumer count. Must call Release after finished. 44 static already_AddRefed<gfx::DataSourceSurface> Acquire( 45 const wr::ExternalImageId& aId); 46 47 static bool Release(const wr::ExternalImageId& aId, bool aForCreator = false); 48 49 static void Add(const wr::ExternalImageId& aId, 50 SurfaceDescriptorShared&& aDesc, base::ProcessId aPid); 51 52 static void Remove(const wr::ExternalImageId& aId); 53 54 static void RemoveAll(uint32_t aNamespace); 55 56 static void AccumulateMemoryReport(uint32_t aNamespace, 57 SharedSurfacesMemoryReport& aReport); 58 59 static bool AccumulateMemoryReport(SharedSurfacesMemoryReport& aReport); 60 61 static void AddTracking(gfx::SourceSurfaceSharedDataWrapper* aSurface); 62 63 static void RemoveTracking(gfx::SourceSurfaceSharedDataWrapper* aSurface); 64 65 static bool AgeOneGeneration( 66 nsTArray<RefPtr<gfx::SourceSurfaceSharedDataWrapper>>& aExpired); 67 68 static bool AgeAndExpireOneGeneration(); 69 70 private: 71 friend class CompositorManagerParent; 72 friend class gfx::SourceSurfaceSharedDataWrapper; 73 74 SharedSurfacesParent(); 75 76 static void AddSameProcess(const wr::ExternalImageId& aId, 77 gfx::SourceSurfaceSharedData* aSurface); 78 79 static void AddTrackingLocked(gfx::SourceSurfaceSharedDataWrapper* aSurface, 80 const StaticMutexAutoLock& aAutoLock); 81 82 static void RemoveTrackingLocked( 83 gfx::SourceSurfaceSharedDataWrapper* aSurface, 84 const StaticMutexAutoLock& aAutoLock); 85 86 static bool AgeOneGenerationLocked( 87 nsTArray<RefPtr<gfx::SourceSurfaceSharedDataWrapper>>& aExpired, 88 const StaticMutexAutoLock& aAutoLock); 89 90 static void ExpireMap( 91 nsTArray<RefPtr<gfx::SourceSurfaceSharedDataWrapper>>& aExpired); 92 93 static StaticMutex sMutex MOZ_UNANNOTATED; 94 95 static StaticAutoPtr<SharedSurfacesParent> sInstance; 96 97 nsRefPtrHashtable<nsUint64HashKey, gfx::SourceSurfaceSharedDataWrapper> 98 mSurfaces; 99 100 class MappingTracker final 101 : public ExpirationTrackerImpl<gfx::SourceSurfaceSharedDataWrapper, 4, 102 StaticMutex, StaticMutexAutoLock> { 103 public: 104 explicit MappingTracker(uint32_t aExpirationTimeoutMS, 105 nsIEventTarget* aEventTarget) 106 : ExpirationTrackerImpl<gfx::SourceSurfaceSharedDataWrapper, 4, 107 StaticMutex, StaticMutexAutoLock>( 108 aExpirationTimeoutMS, "SharedMappingTracker"_ns, aEventTarget) {} 109 110 void TakeExpired( 111 nsTArray<RefPtr<gfx::SourceSurfaceSharedDataWrapper>>& aExpired, 112 const StaticMutexAutoLock& aAutoLock); 113 114 protected: 115 void NotifyExpiredLocked(gfx::SourceSurfaceSharedDataWrapper* aSurface, 116 const StaticMutexAutoLock& aAutoLock) override; 117 118 void NotifyHandlerEndLocked(const StaticMutexAutoLock& aAutoLock) override { 119 } 120 121 void NotifyHandlerEnd() override; 122 123 StaticMutex& GetMutex() override { return sMutex; } 124 125 nsTArray<RefPtr<gfx::SourceSurfaceSharedDataWrapper>> mExpired; 126 }; 127 128 MappingTracker mTracker; 129 }; 130 131 /** 132 * Helper class that is used to keep SourceSurfaceSharedDataWrapper objects 133 * around as long as one of the dependent IPDL actors is still alive and may 134 * reference them for a given PCompositorManager namespace. 135 */ 136 class SharedSurfacesHolder final { 137 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SharedSurfacesHolder) 138 139 public: 140 explicit SharedSurfacesHolder(uint32_t aNamespace) : mNamespace(aNamespace) {} 141 142 already_AddRefed<gfx::DataSourceSurface> Get(const wr::ExternalImageId& aId) { 143 uint32_t extNamespace = static_cast<uint32_t>(wr::AsUint64(aId) >> 32); 144 if (NS_WARN_IF(extNamespace != mNamespace)) { 145 MOZ_ASSERT_UNREACHABLE("Wrong namespace?"); 146 return nullptr; 147 } 148 149 return SharedSurfacesParent::Get(aId); 150 } 151 152 private: 153 ~SharedSurfacesHolder() { SharedSurfacesParent::RemoveAll(mNamespace); } 154 155 uint32_t mNamespace; 156 }; 157 158 } // namespace layers 159 } // namespace mozilla 160 161 #endif