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_VIDEOBRIDGECHILD_H
8 #define MOZILLA_GFX_VIDEOBRIDGECHILD_H
9 
10 #include "mozilla/layers/PVideoBridgeChild.h"
11 #include "mozilla/layers/VideoBridgeUtils.h"
12 #include "ISurfaceAllocator.h"
13 #include "TextureForwarder.h"
14 
15 namespace mozilla {
16 namespace layers {
17 
18 class SynchronousTask;
19 
20 class VideoBridgeChild final : public PVideoBridgeChild,
21                                public TextureForwarder {
22  public:
23   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(VideoBridgeChild, override);
24 
25   static void StartupForGPUProcess();
26   static void Shutdown();
27 
28   static VideoBridgeChild* GetSingleton();
29 
30   // PVideoBridgeChild
31   PTextureChild* AllocPTextureChild(const SurfaceDescriptor& aSharedData,
32                                     const ReadLockDescriptor& aReadLock,
33                                     const LayersBackend& aLayersBackend,
34                                     const TextureFlags& aFlags,
35                                     const uint64_t& aSerial);
36   bool DeallocPTextureChild(PTextureChild* actor);
37 
38   void ActorDestroy(ActorDestroyReason aWhy) override;
39   void ActorDealloc() override;
40 
41   // ISurfaceAllocator
42   bool AllocUnsafeShmem(size_t aSize,
43                         mozilla::ipc::SharedMemory::SharedMemoryType aShmType,
44                         mozilla::ipc::Shmem* aShmem) override;
45   bool AllocShmem(size_t aSize,
46                   mozilla::ipc::SharedMemory::SharedMemoryType aShmType,
47                   mozilla::ipc::Shmem* aShmem) override;
48   bool DeallocShmem(mozilla::ipc::Shmem& aShmem) override;
49 
50   // TextureForwarder
51   PTextureChild* CreateTexture(const SurfaceDescriptor& aSharedData,
52                                const ReadLockDescriptor& aReadLock,
53                                LayersBackend aLayersBackend,
54                                TextureFlags aFlags, uint64_t aSerial,
55                                wr::MaybeExternalImageId& aExternalImageId,
56                                nsIEventTarget* aTarget = nullptr) override;
57 
58   // ClientIPCAllocator
GetParentPid()59   base::ProcessId GetParentPid() const override { return OtherPid(); }
GetThread()60   nsISerialEventTarget* GetThread() const override { return mThread; }
CancelWaitForNotifyNotUsed(uint64_t aTextureId)61   void CancelWaitForNotifyNotUsed(uint64_t aTextureId) override {
62     MOZ_ASSERT(false, "NO RECYCLING HERE");
63   }
64 
65   // ISurfaceAllocator
66   bool IsSameProcess() const override;
67 
CanSend()68   bool CanSend() { return mCanSend; }
69 
70   static void Open(Endpoint<PVideoBridgeChild>&& aEndpoint);
71 
72  protected:
73   void HandleFatalError(const char* aMsg) const override;
74   bool DispatchAllocShmemInternal(size_t aSize,
75                                   SharedMemory::SharedMemoryType aType,
76                                   mozilla::ipc::Shmem* aShmem, bool aUnsafe);
77   void ProxyAllocShmemNow(SynchronousTask* aTask, size_t aSize,
78                           SharedMemory::SharedMemoryType aType,
79                           mozilla::ipc::Shmem* aShmem, bool aUnsafe,
80                           bool* aSuccess);
81   void ProxyDeallocShmemNow(SynchronousTask* aTask, mozilla::ipc::Shmem* aShmem,
82                             bool* aResult);
83 
84  private:
85   VideoBridgeChild();
86   virtual ~VideoBridgeChild();
87 
88   RefPtr<VideoBridgeChild> mIPDLSelfRef;
89   nsCOMPtr<nsISerialEventTarget> mThread;
90   bool mCanSend;
91 };
92 
93 }  // namespace layers
94 }  // namespace mozilla
95 
96 #endif
97