CompositorThread.h (2307B)
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 #ifndef mozilla_layers_CompositorThread_h 7 #define mozilla_layers_CompositorThread_h 8 9 #include "nsISupportsImpl.h" 10 #include "nsIThread.h" 11 12 namespace mozilla::baseprofiler { 13 class BaseProfilerThreadId; 14 } 15 using ProfilerThreadId = mozilla::baseprofiler::BaseProfilerThreadId; 16 class nsIThread; 17 18 namespace mozilla { 19 namespace layers { 20 21 class CompositorThreadHolder final { 22 NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DELETE_ON_MAIN_THREAD( 23 CompositorThreadHolder) 24 25 public: 26 CompositorThreadHolder(); 27 28 nsIThread* GetCompositorThread() const { return mCompositorThread; } 29 30 /** 31 * Returns true if the calling thread is the compositor thread. This works 32 * even if the CompositorThread has begun to shutdown. 33 */ 34 bool IsInThread() { 35 bool rv = false; 36 MOZ_ALWAYS_TRUE(NS_SUCCEEDED(mCompositorThread->IsOnCurrentThread(&rv))); 37 return rv; 38 } 39 40 nsresult Dispatch( 41 already_AddRefed<nsIRunnable> event, 42 nsIEventTarget::DispatchFlags flags = nsIEventTarget::DISPATCH_NORMAL) { 43 return mCompositorThread->Dispatch(std::move(event), flags); 44 } 45 46 static CompositorThreadHolder* GetSingleton(); 47 48 static bool IsActive() { return !!GetSingleton(); } 49 50 /** 51 * Creates the compositor thread and the global compositor map. 52 */ 53 static void Start(); 54 55 /* 56 * Waits for all [CrossProcess]CompositorBridgeParents to shutdown and 57 * releases compositor-thread owned resources. 58 */ 59 static void Shutdown(); 60 61 // Returns true if the calling thread is the compositor thread. 62 static bool IsInCompositorThread(); 63 64 // Thread id to use as a MarkerThreadId option for profiler markers. 65 static ProfilerThreadId GetThreadId(); 66 67 private: 68 ~CompositorThreadHolder(); 69 70 const nsCOMPtr<nsIThread> mCompositorThread; 71 72 static already_AddRefed<nsIThread> CreateCompositorThread(); 73 74 friend class CompositorBridgeParent; 75 }; 76 77 nsIThread* CompositorThread(); 78 79 } // namespace layers 80 } // namespace mozilla 81 82 #endif // mozilla_layers_CompositorThread_h