Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* vim: set ts=2 sw=2 et tw=80: */ |
michael@0 | 2 | /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "ImageBridgeParent.h" |
michael@0 | 8 | #include <stdint.h> // for uint64_t, uint32_t |
michael@0 | 9 | #include "CompositableHost.h" // for CompositableParent, Create |
michael@0 | 10 | #include "base/message_loop.h" // for MessageLoop |
michael@0 | 11 | #include "base/process.h" // for ProcessHandle |
michael@0 | 12 | #include "base/process_util.h" // for OpenProcessHandle |
michael@0 | 13 | #include "base/task.h" // for CancelableTask, DeleteTask, etc |
michael@0 | 14 | #include "base/tracked.h" // for FROM_HERE |
michael@0 | 15 | #include "mozilla/gfx/Point.h" // for IntSize |
michael@0 | 16 | #include "mozilla/ipc/MessageChannel.h" // for MessageChannel, etc |
michael@0 | 17 | #include "mozilla/ipc/ProtocolUtils.h" |
michael@0 | 18 | #include "mozilla/ipc/Transport.h" // for Transport |
michael@0 | 19 | #include "mozilla/layers/CompositableTransactionParent.h" |
michael@0 | 20 | #include "mozilla/layers/CompositorParent.h" // for CompositorParent |
michael@0 | 21 | #include "mozilla/layers/LayerManagerComposite.h" |
michael@0 | 22 | #include "mozilla/layers/LayersMessages.h" // for EditReply |
michael@0 | 23 | #include "mozilla/layers/LayersSurfaces.h" // for PGrallocBufferParent |
michael@0 | 24 | #include "mozilla/layers/PCompositableParent.h" |
michael@0 | 25 | #include "mozilla/layers/PImageBridgeParent.h" |
michael@0 | 26 | #include "mozilla/layers/Compositor.h" |
michael@0 | 27 | #include "mozilla/mozalloc.h" // for operator new, etc |
michael@0 | 28 | #include "nsAutoPtr.h" // for nsRefPtr |
michael@0 | 29 | #include "nsDebug.h" // for NS_RUNTIMEABORT, etc |
michael@0 | 30 | #include "nsISupportsImpl.h" // for ImageBridgeParent::Release, etc |
michael@0 | 31 | #include "nsTArray.h" // for nsTArray, nsTArray_Impl |
michael@0 | 32 | #include "nsTArrayForwardDeclare.h" // for InfallibleTArray |
michael@0 | 33 | #include "nsXULAppAPI.h" // for XRE_GetIOMessageLoop |
michael@0 | 34 | #include "mozilla/layers/TextureHost.h" |
michael@0 | 35 | #include "nsThreadUtils.h" |
michael@0 | 36 | |
michael@0 | 37 | using namespace mozilla::ipc; |
michael@0 | 38 | using namespace mozilla::gfx; |
michael@0 | 39 | |
michael@0 | 40 | namespace mozilla { |
michael@0 | 41 | namespace layers { |
michael@0 | 42 | |
michael@0 | 43 | class PGrallocBufferParent; |
michael@0 | 44 | |
michael@0 | 45 | ImageBridgeParent::ImageBridgeParent(MessageLoop* aLoop, Transport* aTransport) |
michael@0 | 46 | : mMessageLoop(aLoop) |
michael@0 | 47 | , mTransport(aTransport) |
michael@0 | 48 | { |
michael@0 | 49 | // creates the map only if it has not been created already, so it is safe |
michael@0 | 50 | // with several bridges |
michael@0 | 51 | CompositableMap::Create(); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | ImageBridgeParent::~ImageBridgeParent() |
michael@0 | 55 | { |
michael@0 | 56 | if (mTransport) { |
michael@0 | 57 | XRE_GetIOMessageLoop()->PostTask(FROM_HERE, |
michael@0 | 58 | new DeleteTask<Transport>(mTransport)); |
michael@0 | 59 | } |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | LayersBackend |
michael@0 | 63 | ImageBridgeParent::GetCompositorBackendType() const |
michael@0 | 64 | { |
michael@0 | 65 | return Compositor::GetBackend(); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | void |
michael@0 | 69 | ImageBridgeParent::ActorDestroy(ActorDestroyReason aWhy) |
michael@0 | 70 | { |
michael@0 | 71 | MessageLoop::current()->PostTask( |
michael@0 | 72 | FROM_HERE, |
michael@0 | 73 | NewRunnableMethod(this, &ImageBridgeParent::DeferredDestroy)); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | bool |
michael@0 | 77 | ImageBridgeParent::RecvUpdate(const EditArray& aEdits, EditReplyArray* aReply) |
michael@0 | 78 | { |
michael@0 | 79 | // If we don't actually have a compositor, then don't bother |
michael@0 | 80 | // creating any textures. |
michael@0 | 81 | if (Compositor::GetBackend() == LayersBackend::LAYERS_NONE) { |
michael@0 | 82 | return true; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | // Clear fence handles used in previsou transaction. |
michael@0 | 86 | ClearPrevFenceHandles(); |
michael@0 | 87 | |
michael@0 | 88 | EditReplyVector replyv; |
michael@0 | 89 | for (EditArray::index_type i = 0; i < aEdits.Length(); ++i) { |
michael@0 | 90 | if (!ReceiveCompositableUpdate(aEdits[i], replyv)) { |
michael@0 | 91 | return false; |
michael@0 | 92 | } |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | aReply->SetCapacity(replyv.size()); |
michael@0 | 96 | if (replyv.size() > 0) { |
michael@0 | 97 | aReply->AppendElements(&replyv.front(), replyv.size()); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | // Ensure that any pending operations involving back and front |
michael@0 | 101 | // buffers have completed, so that neither process stomps on the |
michael@0 | 102 | // other's buffer contents. |
michael@0 | 103 | LayerManagerComposite::PlatformSyncBeforeReplyUpdate(); |
michael@0 | 104 | |
michael@0 | 105 | return true; |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | bool |
michael@0 | 109 | ImageBridgeParent::RecvUpdateNoSwap(const EditArray& aEdits) |
michael@0 | 110 | { |
michael@0 | 111 | InfallibleTArray<EditReply> noReplies; |
michael@0 | 112 | bool success = RecvUpdate(aEdits, &noReplies); |
michael@0 | 113 | NS_ABORT_IF_FALSE(noReplies.Length() == 0, "RecvUpdateNoSwap requires a sync Update to carry Edits"); |
michael@0 | 114 | return success; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | static void |
michael@0 | 118 | ConnectImageBridgeInParentProcess(ImageBridgeParent* aBridge, |
michael@0 | 119 | Transport* aTransport, |
michael@0 | 120 | base::ProcessHandle aOtherProcess) |
michael@0 | 121 | { |
michael@0 | 122 | aBridge->Open(aTransport, aOtherProcess, XRE_GetIOMessageLoop(), ipc::ParentSide); |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | /*static*/ PImageBridgeParent* |
michael@0 | 126 | ImageBridgeParent::Create(Transport* aTransport, ProcessId aOtherProcess) |
michael@0 | 127 | { |
michael@0 | 128 | base::ProcessHandle processHandle; |
michael@0 | 129 | if (!base::OpenProcessHandle(aOtherProcess, &processHandle)) { |
michael@0 | 130 | return nullptr; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | MessageLoop* loop = CompositorParent::CompositorLoop(); |
michael@0 | 134 | nsRefPtr<ImageBridgeParent> bridge = new ImageBridgeParent(loop, aTransport); |
michael@0 | 135 | bridge->mSelfRef = bridge; |
michael@0 | 136 | loop->PostTask(FROM_HERE, |
michael@0 | 137 | NewRunnableFunction(ConnectImageBridgeInParentProcess, |
michael@0 | 138 | bridge.get(), aTransport, processHandle)); |
michael@0 | 139 | return bridge.get(); |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | bool ImageBridgeParent::RecvWillStop() |
michael@0 | 143 | { |
michael@0 | 144 | // If there is any texture still alive we have to force it to deallocate the |
michael@0 | 145 | // device data (GL textures, etc.) now because shortly after SenStop() returns |
michael@0 | 146 | // on the child side the widget will be destroyed along with it's associated |
michael@0 | 147 | // GL context. |
michael@0 | 148 | InfallibleTArray<PTextureParent*> textures; |
michael@0 | 149 | ManagedPTextureParent(textures); |
michael@0 | 150 | for (unsigned int i = 0; i < textures.Length(); ++i) { |
michael@0 | 151 | RefPtr<TextureHost> tex = TextureHost::AsTextureHost(textures[i]); |
michael@0 | 152 | tex->DeallocateDeviceData(); |
michael@0 | 153 | } |
michael@0 | 154 | return true; |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | bool ImageBridgeParent::RecvStop() |
michael@0 | 158 | { |
michael@0 | 159 | // Nothing to do. This message just serves as synchronization between the |
michael@0 | 160 | // child and parent threads during shutdown. |
michael@0 | 161 | return true; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | static uint64_t GenImageContainerID() { |
michael@0 | 165 | static uint64_t sNextImageID = 1; |
michael@0 | 166 | |
michael@0 | 167 | ++sNextImageID; |
michael@0 | 168 | return sNextImageID; |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | PGrallocBufferParent* |
michael@0 | 172 | ImageBridgeParent::AllocPGrallocBufferParent(const IntSize& aSize, |
michael@0 | 173 | const uint32_t& aFormat, |
michael@0 | 174 | const uint32_t& aUsage, |
michael@0 | 175 | MaybeMagicGrallocBufferHandle* aOutHandle) |
michael@0 | 176 | { |
michael@0 | 177 | #ifdef MOZ_HAVE_SURFACEDESCRIPTORGRALLOC |
michael@0 | 178 | return GrallocBufferActor::Create(aSize, aFormat, aUsage, aOutHandle); |
michael@0 | 179 | #else |
michael@0 | 180 | NS_RUNTIMEABORT("No gralloc buffers for you"); |
michael@0 | 181 | return nullptr; |
michael@0 | 182 | #endif |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | bool |
michael@0 | 186 | ImageBridgeParent::DeallocPGrallocBufferParent(PGrallocBufferParent* actor) |
michael@0 | 187 | { |
michael@0 | 188 | #ifdef MOZ_HAVE_SURFACEDESCRIPTORGRALLOC |
michael@0 | 189 | delete actor; |
michael@0 | 190 | return true; |
michael@0 | 191 | #else |
michael@0 | 192 | NS_RUNTIMEABORT("Um, how did we get here?"); |
michael@0 | 193 | return false; |
michael@0 | 194 | #endif |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | PCompositableParent* |
michael@0 | 198 | ImageBridgeParent::AllocPCompositableParent(const TextureInfo& aInfo, |
michael@0 | 199 | uint64_t* aID) |
michael@0 | 200 | { |
michael@0 | 201 | uint64_t id = GenImageContainerID(); |
michael@0 | 202 | *aID = id; |
michael@0 | 203 | return CompositableHost::CreateIPDLActor(this, aInfo, id); |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | bool ImageBridgeParent::DeallocPCompositableParent(PCompositableParent* aActor) |
michael@0 | 207 | { |
michael@0 | 208 | return CompositableHost::DestroyIPDLActor(aActor); |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | PTextureParent* |
michael@0 | 212 | ImageBridgeParent::AllocPTextureParent(const SurfaceDescriptor& aSharedData, |
michael@0 | 213 | const TextureFlags& aFlags) |
michael@0 | 214 | { |
michael@0 | 215 | return TextureHost::CreateIPDLActor(this, aSharedData, aFlags); |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | bool |
michael@0 | 219 | ImageBridgeParent::DeallocPTextureParent(PTextureParent* actor) |
michael@0 | 220 | { |
michael@0 | 221 | return TextureHost::DestroyIPDLActor(actor); |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | MessageLoop * ImageBridgeParent::GetMessageLoop() { |
michael@0 | 225 | return mMessageLoop; |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | class ReleaseRunnable : public nsRunnable |
michael@0 | 229 | { |
michael@0 | 230 | public: |
michael@0 | 231 | ReleaseRunnable(ImageBridgeParent* aRef) |
michael@0 | 232 | : mRef(aRef) |
michael@0 | 233 | { |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | NS_IMETHOD Run() |
michael@0 | 237 | { |
michael@0 | 238 | mRef->Release(); |
michael@0 | 239 | return NS_OK; |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | private: |
michael@0 | 243 | ImageBridgeParent* mRef; |
michael@0 | 244 | }; |
michael@0 | 245 | |
michael@0 | 246 | void |
michael@0 | 247 | ImageBridgeParent::DeferredDestroy() |
michael@0 | 248 | { |
michael@0 | 249 | ImageBridgeParent* self; |
michael@0 | 250 | mSelfRef.forget(&self); |
michael@0 | 251 | |
michael@0 | 252 | nsCOMPtr<nsIRunnable> runnable = new ReleaseRunnable(self); |
michael@0 | 253 | MOZ_ALWAYS_TRUE(NS_SUCCEEDED(NS_DispatchToMainThread(runnable))); |
michael@0 | 254 | } |
michael@0 | 255 | |
michael@0 | 256 | IToplevelProtocol* |
michael@0 | 257 | ImageBridgeParent::CloneToplevel(const InfallibleTArray<ProtocolFdMapping>& aFds, |
michael@0 | 258 | base::ProcessHandle aPeerProcess, |
michael@0 | 259 | mozilla::ipc::ProtocolCloneContext* aCtx) |
michael@0 | 260 | { |
michael@0 | 261 | for (unsigned int i = 0; i < aFds.Length(); i++) { |
michael@0 | 262 | if (aFds[i].protocolId() == unsigned(GetProtocolId())) { |
michael@0 | 263 | Transport* transport = OpenDescriptor(aFds[i].fd(), |
michael@0 | 264 | Transport::MODE_SERVER); |
michael@0 | 265 | PImageBridgeParent* bridge = Create(transport, base::GetProcId(aPeerProcess)); |
michael@0 | 266 | bridge->CloneManagees(this, aCtx); |
michael@0 | 267 | bridge->IToplevelProtocol::SetTransport(transport); |
michael@0 | 268 | return bridge; |
michael@0 | 269 | } |
michael@0 | 270 | } |
michael@0 | 271 | return nullptr; |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | bool ImageBridgeParent::IsSameProcess() const |
michael@0 | 275 | { |
michael@0 | 276 | return OtherProcess() == ipc::kInvalidProcessHandle; |
michael@0 | 277 | } |
michael@0 | 278 | |
michael@0 | 279 | } // layers |
michael@0 | 280 | } // mozilla |