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 gfx_layers_ipc_ImageBridgeParent_h_
8 #define gfx_layers_ipc_ImageBridgeParent_h_
9 
10 #include <stddef.h>  // for size_t
11 #include <stdint.h>  // for uint32_t, uint64_t
12 #include "CompositableTransactionParent.h"
13 #include "mozilla/Assertions.h"  // for MOZ_ASSERT_HELPER2
14 #include "mozilla/Attributes.h"  // for override
15 #include "mozilla/ipc/ProtocolUtils.h"
16 #include "mozilla/ipc/SharedMemory.h"  // for SharedMemory, etc
17 #include "mozilla/layers/CompositorThread.h"
18 #include "mozilla/layers/PImageBridgeParent.h"
19 #include "nsISupportsImpl.h"
20 #include "nsTArrayForwardDeclare.h"  // for nsTArray
21 
22 namespace mozilla {
23 namespace ipc {
24 class Shmem;
25 }  // namespace ipc
26 
27 namespace layers {
28 
29 struct ImageCompositeNotificationInfo;
30 
31 /**
32  * ImageBridgeParent is the manager Protocol of async Compositables.
33  */
34 class ImageBridgeParent final : public PImageBridgeParent,
35                                 public CompositableParentManager,
36                                 public mozilla::ipc::IShmemAllocator {
37  public:
38   typedef nsTArray<CompositableOperation> EditArray;
39   typedef nsTArray<OpDestroy> OpDestroyArray;
40 
41  protected:
42   ImageBridgeParent(nsISerialEventTarget* aThread, ProcessId aChildProcessId);
43 
44  public:
45   virtual ~ImageBridgeParent();
46 
47   /**
48    * Creates the globals of ImageBridgeParent.
49    */
50   static void Setup();
51 
52   static ImageBridgeParent* CreateSameProcess();
53   static bool CreateForGPUProcess(Endpoint<PImageBridgeParent>&& aEndpoint);
54   static bool CreateForContent(Endpoint<PImageBridgeParent>&& aEndpoint);
55   static void Shutdown();
56 
AsShmemAllocator()57   IShmemAllocator* AsShmemAllocator() override { return this; }
58 
59   void ActorDestroy(ActorDestroyReason aWhy) override;
60 
61   // CompositableParentManager
62   void SendAsyncMessage(
63       const nsTArray<AsyncParentMessageData>& aMessage) override;
64 
65   void NotifyNotUsed(PTextureParent* aTexture,
66                      uint64_t aTransactionId) override;
67 
GetChildProcessId()68   base::ProcessId GetChildProcessId() override { return OtherPid(); }
69 
70   // PImageBridge
71   mozilla::ipc::IPCResult RecvUpdate(EditArray&& aEdits,
72                                      OpDestroyArray&& aToDestroy,
73                                      const uint64_t& aFwdTransactionId);
74 
75   PTextureParent* AllocPTextureParent(
76       const SurfaceDescriptor& aSharedData, const ReadLockDescriptor& aReadLock,
77       const LayersBackend& aLayersBackend, const TextureFlags& aFlags,
78       const uint64_t& aSerial,
79       const wr::MaybeExternalImageId& aExternalImageId);
80   bool DeallocPTextureParent(PTextureParent* actor);
81 
82   mozilla::ipc::IPCResult RecvNewCompositable(
83       const CompositableHandle& aHandle, const TextureInfo& aInfo,
84       const LayersBackend& aLayersBackend);
85   mozilla::ipc::IPCResult RecvReleaseCompositable(
86       const CompositableHandle& aHandle);
87 
88   PMediaSystemResourceManagerParent* AllocPMediaSystemResourceManagerParent();
89   bool DeallocPMediaSystemResourceManagerParent(
90       PMediaSystemResourceManagerParent* aActor);
91 
92   // Shutdown step 1
93   mozilla::ipc::IPCResult RecvWillClose();
94 
GetThread()95   nsISerialEventTarget* GetThread() const { return mThread; }
96 
97   // IShmemAllocator
98 
99   bool AllocShmem(size_t aSize, ipc::SharedMemory::SharedMemoryType aType,
100                   ipc::Shmem* aShmem) override;
101 
102   bool AllocUnsafeShmem(size_t aSize, ipc::SharedMemory::SharedMemoryType aType,
103                         ipc::Shmem* aShmem) override;
104 
105   bool DeallocShmem(ipc::Shmem& aShmem) override;
106 
107   bool IsSameProcess() const override;
108 
109   static already_AddRefed<ImageBridgeParent> GetInstance(ProcessId aId);
110 
111   static bool NotifyImageComposites(
112       nsTArray<ImageCompositeNotificationInfo>& aNotifications);
113 
UsesImageBridge()114   bool UsesImageBridge() const override { return true; }
115 
IPCOpen()116   bool IPCOpen() const override { return !mClosed; }
117 
118   // See PluginInstanceParent for details on the Windows async plugin
119   // rendering protocol.
120   mozilla::ipc::IPCResult RecvMakeAsyncPluginSurfaces(
121       SurfaceFormat aFormat, IntSize aSize, SurfaceDescriptorPlugin* aSD);
122   mozilla::ipc::IPCResult RecvUpdateAsyncPluginSurface(
123       const SurfaceDescriptorPlugin& aSD);
124   mozilla::ipc::IPCResult RecvReadbackAsyncPluginSurface(
125       const SurfaceDescriptorPlugin& aSD, SurfaceDescriptor* aResult);
126   mozilla::ipc::IPCResult RecvRemoveAsyncPluginSurface(
127       const SurfaceDescriptorPlugin& aSD, bool aIsFrontSurface);
128 
129   RefPtr<TextureHost> LookupTextureHost(
130       const SurfaceDescriptorPlugin& aDescriptor);
131 
132  protected:
133   void Bind(Endpoint<PImageBridgeParent>&& aEndpoint);
134 
135  private:
136   static void ShutdownInternal();
137 
138   void DeferredDestroy();
139   nsCOMPtr<nsISerialEventTarget> mThread;
140   // This keeps us alive until ActorDestroy(), at which point we do a
141   // deferred destruction of ourselves.
142   RefPtr<ImageBridgeParent> mSelfRef;
143 
144   bool mClosed;
145 
146   /**
147    * Map of all living ImageBridgeParent instances
148    */
149   typedef std::map<base::ProcessId, ImageBridgeParent*> ImageBridgeMap;
150   static ImageBridgeMap sImageBridges;
151 
152   RefPtr<CompositorThreadHolder> mCompositorThreadHolder;
153 
154 #if defined(OS_WIN)
155   // Owns a pair of textures used to double-buffer a plugin async rendering
156   // instance.
157   struct PluginTextureDatas {
158     UniquePtr<D3D11TextureData> mPluginTextureData;
159     UniquePtr<D3D11TextureData> mDisplayTextureData;
160 
161     PluginTextureDatas(UniquePtr<D3D11TextureData>&& aPluginTextureData,
162                        UniquePtr<D3D11TextureData>&& aDisplayTextureData);
163 
164     ~PluginTextureDatas();
165 
166     PluginTextureDatas(const PluginTextureDatas& o) = delete;
167     PluginTextureDatas& operator=(const PluginTextureDatas& o) = delete;
168 
IsValidPluginTextureDatas169     bool IsValid() { return mPluginTextureData && mDisplayTextureData; }
170   };
171 
172   HashMap<WindowsHandle, RefPtr<TextureHost>> mGPUVideoTextureHosts;
173   HashMap<WindowsHandle, UniquePtr<PluginTextureDatas>> mPluginTextureDatas;
174 #endif  // defined(OS_WIN)
175 };
176 
177 }  // namespace layers
178 }  // namespace mozilla
179 
180 #endif  // gfx_layers_ipc_ImageBridgeParent_h_
181