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 include_dom_media_ipc_RemoteDecoderManagerParent_h
7 #define include_dom_media_ipc_RemoteDecoderManagerParent_h
8 
9 #include "GPUVideoImage.h"
10 #include "mozilla/PRemoteDecoderManagerParent.h"
11 #include "mozilla/layers/VideoBridgeChild.h"
12 
13 namespace mozilla {
14 
15 class PDMFactory;
16 
17 class RemoteDecoderManagerParent final
18     : public PRemoteDecoderManagerParent,
19       public layers::IGPUVideoSurfaceManager {
20   friend class PRemoteDecoderManagerParent;
21 
22  public:
23   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RemoteDecoderManagerParent, override)
24 
25   static bool CreateForContent(
26       Endpoint<PRemoteDecoderManagerParent>&& aEndpoint);
27 
28   static bool CreateVideoBridgeToOtherProcess(
29       Endpoint<layers::PVideoBridgeChild>&& aEndpoint);
30 
31   // Must be called on manager thread.
32   // Store the image so that it can be used out of process. Will be released
33   // when DeallocateSurfaceDescriptor is called.
34   void StoreImage(const SurfaceDescriptorGPUVideo& aSD, layers::Image* aImage,
35                   layers::TextureClient* aTexture);
36 
37   // IGPUVideoSurfaceManager methods
Readback(const SurfaceDescriptorGPUVideo & aSD)38   already_AddRefed<gfx::SourceSurface> Readback(
39       const SurfaceDescriptorGPUVideo& aSD) override {
40     MOZ_ASSERT_UNREACHABLE("Not usable from the parent");
41     return nullptr;
42   }
43   void DeallocateSurfaceDescriptor(
44       const SurfaceDescriptorGPUVideo& aSD) override;
45 
46   static bool StartupThreads();
47   static void ShutdownThreads();
48 
49   static void ShutdownVideoBridge();
50 
51   bool OnManagerThread();
52 
53   // Can be called from manager thread only
54   PDMFactory& EnsurePDMFactory();
55 
56  protected:
57   PRemoteDecoderParent* AllocPRemoteDecoderParent(
58       const RemoteDecoderInfoIPDL& aRemoteDecoderInfo,
59       const CreateDecoderParams::OptionSet& aOptions,
60       const Maybe<layers::TextureFactoryIdentifier>& aIdentifier);
61   bool DeallocPRemoteDecoderParent(PRemoteDecoderParent* actor);
62 
63   mozilla::ipc::IPCResult RecvReadback(const SurfaceDescriptorGPUVideo& aSD,
64                                        SurfaceDescriptor* aResult);
65   mozilla::ipc::IPCResult RecvDeallocateSurfaceDescriptorGPUVideo(
66       const SurfaceDescriptorGPUVideo& aSD);
67 
68   void ActorDestroy(mozilla::ipc::IProtocol::ActorDestroyReason) override;
69   void ActorDealloc() override;
70 
71  private:
72   explicit RemoteDecoderManagerParent(nsISerialEventTarget* aThread);
73   ~RemoteDecoderManagerParent();
74 
75   void Open(Endpoint<PRemoteDecoderManagerParent>&& aEndpoint);
76 
77   std::map<uint64_t, RefPtr<layers::Image>> mImageMap;
78   std::map<uint64_t, RefPtr<layers::TextureClient>> mTextureMap;
79 
80   nsCOMPtr<nsISerialEventTarget> mThread;
81   RefPtr<PDMFactory> mPDMFactory;
82 };
83 
84 }  // namespace mozilla
85 
86 #endif  // include_dom_media_ipc_RemoteDecoderManagerParent_h
87