gfx/layers/ipc/CompositorChild.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set sw=2 ts=2 et tw=80 : */
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 "CompositorChild.h"
michael@0 8 #include <stddef.h> // for size_t
michael@0 9 #include "ClientLayerManager.h" // for ClientLayerManager
michael@0 10 #include "base/message_loop.h" // for MessageLoop
michael@0 11 #include "base/process_util.h" // for OpenProcessHandle
michael@0 12 #include "base/task.h" // for NewRunnableMethod, etc
michael@0 13 #include "base/tracked.h" // for FROM_HERE
michael@0 14 #include "mozilla/layers/LayerTransactionChild.h"
michael@0 15 #include "mozilla/layers/PLayerTransactionChild.h"
michael@0 16 #include "mozilla/mozalloc.h" // for operator new, etc
michael@0 17 #include "nsDebug.h" // for NS_RUNTIMEABORT
michael@0 18 #include "nsIObserver.h" // for nsIObserver
michael@0 19 #include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc
michael@0 20 #include "nsTArray.h" // for nsTArray, nsTArray_Impl
michael@0 21 #include "nsXULAppAPI.h" // for XRE_GetIOMessageLoop, etc
michael@0 22 #include "FrameLayerBuilder.h"
michael@0 23 #include "mozilla/dom/TabChild.h"
michael@0 24
michael@0 25 using mozilla::layers::LayerTransactionChild;
michael@0 26
michael@0 27 namespace mozilla {
michael@0 28 namespace layers {
michael@0 29
michael@0 30 /*static*/ CompositorChild* CompositorChild::sCompositor;
michael@0 31
michael@0 32 Atomic<int32_t> CompositableForwarder::sSerialCounter(0);
michael@0 33
michael@0 34 CompositorChild::CompositorChild(ClientLayerManager *aLayerManager)
michael@0 35 : mLayerManager(aLayerManager)
michael@0 36 {
michael@0 37 MOZ_COUNT_CTOR(CompositorChild);
michael@0 38 }
michael@0 39
michael@0 40 CompositorChild::~CompositorChild()
michael@0 41 {
michael@0 42 MOZ_COUNT_DTOR(CompositorChild);
michael@0 43 }
michael@0 44
michael@0 45 void
michael@0 46 CompositorChild::Destroy()
michael@0 47 {
michael@0 48 mLayerManager->Destroy();
michael@0 49 mLayerManager = nullptr;
michael@0 50 while (size_t len = ManagedPLayerTransactionChild().Length()) {
michael@0 51 LayerTransactionChild* layers =
michael@0 52 static_cast<LayerTransactionChild*>(ManagedPLayerTransactionChild()[len - 1]);
michael@0 53 layers->Destroy();
michael@0 54 }
michael@0 55 SendStop();
michael@0 56 }
michael@0 57
michael@0 58 bool
michael@0 59 CompositorChild::LookupCompositorFrameMetrics(const FrameMetrics::ViewID aId,
michael@0 60 FrameMetrics& aFrame)
michael@0 61 {
michael@0 62 SharedFrameMetricsData* data = mFrameMetricsTable.Get(aId);
michael@0 63 if (data) {
michael@0 64 data->CopyFrameMetrics(&aFrame);
michael@0 65 return true;
michael@0 66 }
michael@0 67 return false;
michael@0 68 }
michael@0 69
michael@0 70 /*static*/ PCompositorChild*
michael@0 71 CompositorChild::Create(Transport* aTransport, ProcessId aOtherProcess)
michael@0 72 {
michael@0 73 // There's only one compositor per child process.
michael@0 74 MOZ_ASSERT(!sCompositor);
michael@0 75
michael@0 76 nsRefPtr<CompositorChild> child(new CompositorChild(nullptr));
michael@0 77 ProcessHandle handle;
michael@0 78 if (!base::OpenProcessHandle(aOtherProcess, &handle)) {
michael@0 79 // We can't go on without a compositor.
michael@0 80 NS_RUNTIMEABORT("Couldn't OpenProcessHandle() to parent process.");
michael@0 81 return nullptr;
michael@0 82 }
michael@0 83 if (!child->Open(aTransport, handle, XRE_GetIOMessageLoop(), ipc::ChildSide)) {
michael@0 84 NS_RUNTIMEABORT("Couldn't Open() Compositor channel.");
michael@0 85 return nullptr;
michael@0 86 }
michael@0 87 // We release this ref in ActorDestroy().
michael@0 88 return sCompositor = child.forget().take();
michael@0 89 }
michael@0 90
michael@0 91 /*static*/ CompositorChild*
michael@0 92 CompositorChild::Get()
michael@0 93 {
michael@0 94 // This is only expected to be used in child processes.
michael@0 95 MOZ_ASSERT(XRE_GetProcessType() != GeckoProcessType_Default);
michael@0 96 return sCompositor;
michael@0 97 }
michael@0 98
michael@0 99 PLayerTransactionChild*
michael@0 100 CompositorChild::AllocPLayerTransactionChild(const nsTArray<LayersBackend>& aBackendHints,
michael@0 101 const uint64_t& aId,
michael@0 102 TextureFactoryIdentifier*,
michael@0 103 bool*)
michael@0 104 {
michael@0 105 LayerTransactionChild* c = new LayerTransactionChild();
michael@0 106 c->AddIPDLReference();
michael@0 107 return c;
michael@0 108 }
michael@0 109
michael@0 110 bool
michael@0 111 CompositorChild::DeallocPLayerTransactionChild(PLayerTransactionChild* actor)
michael@0 112 {
michael@0 113 static_cast<LayerTransactionChild*>(actor)->ReleaseIPDLReference();
michael@0 114 return true;
michael@0 115 }
michael@0 116
michael@0 117 bool
michael@0 118 CompositorChild::RecvInvalidateAll()
michael@0 119 {
michael@0 120 FrameLayerBuilder::InvalidateAllLayers(mLayerManager);
michael@0 121 return true;
michael@0 122 }
michael@0 123
michael@0 124 bool
michael@0 125 CompositorChild::RecvDidComposite(const uint64_t& aId)
michael@0 126 {
michael@0 127 if (mLayerManager) {
michael@0 128 MOZ_ASSERT(aId == 0);
michael@0 129 mLayerManager->DidComposite();
michael@0 130 } else if (aId != 0) {
michael@0 131 dom::TabChild *child = dom::TabChild::GetFrom(aId);
michael@0 132 if (child) {
michael@0 133 child->DidComposite();
michael@0 134 }
michael@0 135 }
michael@0 136 return true;
michael@0 137 }
michael@0 138
michael@0 139 void
michael@0 140 CompositorChild::ActorDestroy(ActorDestroyReason aWhy)
michael@0 141 {
michael@0 142 MOZ_ASSERT(sCompositor == this);
michael@0 143
michael@0 144 #ifdef MOZ_B2G
michael@0 145 // Due to poor lifetime management of gralloc (and possibly shmems) we will
michael@0 146 // crash at some point in the future when we get destroyed due to abnormal
michael@0 147 // shutdown. Its better just to crash here. On desktop though, we have a chance
michael@0 148 // of recovering.
michael@0 149 if (aWhy == AbnormalShutdown) {
michael@0 150 NS_RUNTIMEABORT("ActorDestroy by IPC channel failure at CompositorChild");
michael@0 151 }
michael@0 152 #endif
michael@0 153 if (sCompositor) {
michael@0 154 sCompositor->Release();
michael@0 155 sCompositor = nullptr;
michael@0 156 }
michael@0 157 // We don't want to release the ref to sCompositor here, during
michael@0 158 // cleanup, because that will cause it to be deleted while it's
michael@0 159 // still being used. So defer the deletion to after it's not in
michael@0 160 // use.
michael@0 161 MessageLoop::current()->PostTask(
michael@0 162 FROM_HERE,
michael@0 163 NewRunnableMethod(this, &CompositorChild::Release));
michael@0 164 }
michael@0 165
michael@0 166 bool
michael@0 167 CompositorChild::RecvSharedCompositorFrameMetrics(
michael@0 168 const mozilla::ipc::SharedMemoryBasic::Handle& metrics,
michael@0 169 const CrossProcessMutexHandle& handle,
michael@0 170 const uint32_t& aAPZCId)
michael@0 171 {
michael@0 172 SharedFrameMetricsData* data = new SharedFrameMetricsData(metrics, handle, aAPZCId);
michael@0 173 mFrameMetricsTable.Put(data->GetViewID(), data);
michael@0 174 return true;
michael@0 175 }
michael@0 176
michael@0 177 bool
michael@0 178 CompositorChild::RecvReleaseSharedCompositorFrameMetrics(
michael@0 179 const ViewID& aId,
michael@0 180 const uint32_t& aAPZCId)
michael@0 181 {
michael@0 182 SharedFrameMetricsData* data = mFrameMetricsTable.Get(aId);
michael@0 183 // The SharedFrameMetricsData may have been removed previously if
michael@0 184 // a SharedFrameMetricsData with the same ViewID but later APZCId had
michael@0 185 // been store and over wrote it.
michael@0 186 if (data && (data->GetAPZCId() == aAPZCId)) {
michael@0 187 mFrameMetricsTable.Remove(aId);
michael@0 188 }
michael@0 189 return true;
michael@0 190 }
michael@0 191
michael@0 192 CompositorChild::SharedFrameMetricsData::SharedFrameMetricsData(
michael@0 193 const ipc::SharedMemoryBasic::Handle& metrics,
michael@0 194 const CrossProcessMutexHandle& handle,
michael@0 195 const uint32_t& aAPZCId) :
michael@0 196 mBuffer(nullptr),
michael@0 197 mMutex(nullptr),
michael@0 198 mAPZCId(aAPZCId)
michael@0 199 {
michael@0 200 mBuffer = new ipc::SharedMemoryBasic(metrics);
michael@0 201 mBuffer->Map(sizeof(FrameMetrics));
michael@0 202 mMutex = new CrossProcessMutex(handle);
michael@0 203 MOZ_COUNT_CTOR(SharedFrameMetricsData);
michael@0 204 }
michael@0 205
michael@0 206 CompositorChild::SharedFrameMetricsData::~SharedFrameMetricsData()
michael@0 207 {
michael@0 208 // When the hash table deletes the class, delete
michael@0 209 // the shared memory and mutex.
michael@0 210 delete mMutex;
michael@0 211 delete mBuffer;
michael@0 212 MOZ_COUNT_DTOR(SharedFrameMetricsData);
michael@0 213 }
michael@0 214
michael@0 215 void
michael@0 216 CompositorChild::SharedFrameMetricsData::CopyFrameMetrics(FrameMetrics* aFrame)
michael@0 217 {
michael@0 218 FrameMetrics* frame = static_cast<FrameMetrics*>(mBuffer->memory());
michael@0 219 MOZ_ASSERT(frame);
michael@0 220 mMutex->Lock();
michael@0 221 *aFrame = *frame;
michael@0 222 mMutex->Unlock();
michael@0 223 }
michael@0 224
michael@0 225 FrameMetrics::ViewID
michael@0 226 CompositorChild::SharedFrameMetricsData::GetViewID()
michael@0 227 {
michael@0 228 FrameMetrics* frame = static_cast<FrameMetrics*>(mBuffer->memory());
michael@0 229 MOZ_ASSERT(frame);
michael@0 230 // Not locking to read of mScrollId since it should not change after being
michael@0 231 // initially set.
michael@0 232 return frame->GetScrollId();
michael@0 233 }
michael@0 234
michael@0 235 uint32_t
michael@0 236 CompositorChild::SharedFrameMetricsData::GetAPZCId()
michael@0 237 {
michael@0 238 return mAPZCId;
michael@0 239 }
michael@0 240
michael@0 241 } // namespace layers
michael@0 242 } // namespace mozilla
michael@0 243

mercurial