LayerTreeOwnerTracker.cpp (2333B)
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 #include "LayerTreeOwnerTracker.h" 8 9 #include "mozilla/StaticPtr.h" // for StaticAutoPtr 10 #include "mozilla/gfx/GPUChild.h" // for GPUChild 11 #include "mozilla/gfx/GPUProcessManager.h" // for GPUProcessManager 12 13 #include <functional> 14 15 namespace mozilla { 16 namespace layers { 17 18 static StaticAutoPtr<LayerTreeOwnerTracker> sSingleton; 19 20 LayerTreeOwnerTracker::LayerTreeOwnerTracker() 21 : mLayerIdsLock("LayerTreeOwnerTrackerLock") {} 22 23 void LayerTreeOwnerTracker::Initialize() { 24 MOZ_ASSERT(!sSingleton); 25 sSingleton = new LayerTreeOwnerTracker(); 26 } 27 28 void LayerTreeOwnerTracker::Shutdown() { sSingleton = nullptr; } 29 30 LayerTreeOwnerTracker* LayerTreeOwnerTracker::Get() { return sSingleton; } 31 32 void LayerTreeOwnerTracker::Map(LayersId aLayersId, 33 base::ProcessId aProcessId) { 34 MutexAutoLock lock(mLayerIdsLock); 35 36 // Add the mapping to the list 37 const auto i = mLayerIds.insert({aLayersId, aProcessId}); 38 MOZ_ASSERT(i.second, "Mapping already used layers ID!"); 39 (void)i; 40 } 41 42 void LayerTreeOwnerTracker::Unmap(LayersId aLayersId, 43 base::ProcessId aProcessId) { 44 MutexAutoLock lock(mLayerIdsLock); 45 46 auto i = mLayerIds.find(aLayersId); 47 if (i == mLayerIds.end()) { 48 return; 49 } 50 51 if (NS_WARN_IF(i->second != aProcessId)) { 52 MOZ_ASSERT_UNREACHABLE("Unmapping layers ID for another process!"); 53 return; 54 } 55 56 mLayerIds.erase(i); 57 } 58 59 bool LayerTreeOwnerTracker::IsMapped(LayersId aLayersId, 60 base::ProcessId aProcessId) { 61 MutexAutoLock lock(mLayerIdsLock); 62 63 auto iter = mLayerIds.find(aLayersId); 64 return iter != mLayerIds.end() && iter->second == aProcessId; 65 } 66 67 void LayerTreeOwnerTracker::Iterate( 68 const std::function<void(LayersId aLayersId, base::ProcessId aProcessId)>& 69 aCallback) { 70 MutexAutoLock lock(mLayerIdsLock); 71 72 for (const auto& iter : mLayerIds) { 73 aCallback(iter.first, iter.second); 74 } 75 } 76 77 } // namespace layers 78 } // namespace mozilla