DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (dcc6d7a0dc00)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: sw=2 ts=8 et :
 */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "base/task.h"                  // for NewRunnableFunction, etc
#include "base/thread.h"                // for Thread
#include "base/tracked.h"               // for FROM_HERE
#include "mozilla/gfx/Logging.h"        // for gfxDebug
#include "mozilla/layers/SharedBufferManagerChild.h"
#include "mozilla/layers/SharedBufferManagerParent.h"
#include "mozilla/StaticPtr.h"          // for StaticRefPtr
#include "mozilla/ReentrantMonitor.h"   // for ReentrantMonitor, etc
#include "nsThreadUtils.h"              // fo NS_IsMainThread

#ifdef MOZ_NUWA_PROCESS
#include "ipc/Nuwa.h"
#endif

#ifdef MOZ_WIDGET_GONK
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "SBMChild", ## args)
#endif

namespace mozilla {
namespace layers {

using namespace mozilla::gfx;

// Singleton
SharedBufferManagerChild* SharedBufferManagerChild::sSharedBufferManagerChildSingleton = nullptr;
SharedBufferManagerParent* SharedBufferManagerChild::sSharedBufferManagerParentSingleton = nullptr;
base::Thread* SharedBufferManagerChild::sSharedBufferManagerChildThread = nullptr;

SharedBufferManagerChild::SharedBufferManagerChild()
#ifdef MOZ_HAVE_SURFACEDESCRIPTORGRALLOC
  : mBufferMutex("BufferMonitor")
#endif
{
}

static bool
InSharedBufferManagerChildThread()
{
  return SharedBufferManagerChild::sSharedBufferManagerChildThread->thread_id() == PlatformThread::CurrentId();
}

// dispatched function
static void
DeleteSharedBufferManagerSync(ReentrantMonitor *aBarrier, bool *aDone)
{
  ReentrantMonitorAutoEnter autoMon(*aBarrier);

  MOZ_ASSERT(InSharedBufferManagerChildThread(),
             "Should be in SharedBufferManagerChild thread.");
  SharedBufferManagerChild::sSharedBufferManagerChildSingleton = nullptr;
  SharedBufferManagerChild::sSharedBufferManagerParentSingleton = nullptr;
  *aDone = true;
  aBarrier->NotifyAll();
}

// dispatched function
static void
ConnectSharedBufferManager(SharedBufferManagerChild *child, SharedBufferManagerParent *parent)
{
  MessageLoop *parentMsgLoop = parent->GetMessageLoop();
  ipc::MessageChannel *parentChannel = parent->GetIPCChannel();
  child->Open(parentChannel, parentMsgLoop, mozilla::ipc::ChildSide);
}

base::Thread*
SharedBufferManagerChild::GetThread() const
{
  return sSharedBufferManagerChildThread;
}

SharedBufferManagerChild*
SharedBufferManagerChild::GetSingleton()
{
  return sSharedBufferManagerChildSingleton;
}

bool
SharedBufferManagerChild::IsCreated()
{
  return GetSingleton() != nullptr;
}

void
SharedBufferManagerChild::StartUp()
{
  NS_ASSERTION(NS_IsMainThread(), "Should be on the main Thread!");
  SharedBufferManagerChild::StartUpOnThread(new base::Thread("BufferMgrChild"));
}

static void
ConnectSharedBufferManagerInChildProcess(mozilla::ipc::Transport* aTransport,
                                         base::ProcessId aOtherPid)
{
  // Bind the IPC channel to the shared buffer manager thread.
  SharedBufferManagerChild::sSharedBufferManagerChildSingleton->Open(aTransport,
                                                                     aOtherPid,
                                                                     XRE_GetIOMessageLoop(),
                                                                     ipc::ChildSide);

#ifdef MOZ_NUWA_PROCESS
  if (IsNuwaProcess()) {
    SharedBufferManagerChild::sSharedBufferManagerChildThread
      ->message_loop()->PostTask(FROM_HERE,
                                 NewRunnableFunction(NuwaMarkCurrentThread,
                                                     (void (*)(void *))nullptr,
                                                     (void *)nullptr));
  }
#endif
}

PSharedBufferManagerChild*
SharedBufferManagerChild::StartUpInChildProcess(Transport* aTransport,
                                                base::ProcessId aOtherPid)
{
  NS_ASSERTION(NS_IsMainThread(), "Should be on the main Thread!");

  sSharedBufferManagerChildThread = new base::Thread("BufferMgrChild");
  if (!sSharedBufferManagerChildThread->Start()) {
    return nullptr;
  }

  sSharedBufferManagerChildSingleton = new SharedBufferManagerChild();
  sSharedBufferManagerChildSingleton->GetMessageLoop()->PostTask(
    FROM_HERE,
    NewRunnableFunction(ConnectSharedBufferManagerInChildProcess,
                        aTransport, aOtherPid));

  return sSharedBufferManagerChildSingleton;
}

void
SharedBufferManagerChild::ShutDown()
{
  NS_ASSERTION(NS_IsMainThread(), "Should be on the main Thread!");
  if (IsCreated()) {
    SharedBufferManagerChild::DestroyManager();
    delete sSharedBufferManagerChildThread;
    sSharedBufferManagerChildThread = nullptr;
  }
}

bool
SharedBufferManagerChild::StartUpOnThread(base::Thread* aThread)
{
  MOZ_ASSERT(aThread, "SharedBufferManager needs a thread.");
  if (sSharedBufferManagerChildSingleton != nullptr) {
    return false;
  }

  sSharedBufferManagerChildThread = aThread;
  if (!aThread->IsRunning()) {
    aThread->Start();
  }
  sSharedBufferManagerChildSingleton = new SharedBufferManagerChild();
  char thrname[128];
  base::snprintf(thrname, 128, "BufMgrParent#%d", base::Process::Current().pid());
  sSharedBufferManagerParentSingleton = new SharedBufferManagerParent(
    nullptr, base::Process::Current().pid(), new base::Thread(thrname));
  sSharedBufferManagerChildSingleton->ConnectAsync(sSharedBufferManagerParentSingleton);
  return true;
}

void
SharedBufferManagerChild::DestroyManager()
{
  MOZ_ASSERT(!InSharedBufferManagerChildThread(),
             "This method must not be called in this thread.");
  // ...because we are about to dispatch synchronous messages to the
  // BufferManagerChild thread.

  if (!IsCreated()) {
    return;
  }

  ReentrantMonitor barrier("BufferManagerDestroyTask lock");
  ReentrantMonitorAutoEnter autoMon(barrier);

  bool done = false;
  sSharedBufferManagerChildSingleton->GetMessageLoop()->PostTask(FROM_HERE,
    NewRunnableFunction(&DeleteSharedBufferManagerSync, &barrier, &done));
  while (!done) {
    barrier.Wait();
  }

}

MessageLoop *
SharedBufferManagerChild::GetMessageLoop() const
{
  return sSharedBufferManagerChildThread != nullptr ?
      sSharedBufferManagerChildThread->message_loop() :
      nullptr;
}

void
SharedBufferManagerChild::ConnectAsync(SharedBufferManagerParent* aParent)
{
  GetMessageLoop()->PostTask(FROM_HERE, NewRunnableFunction(&ConnectSharedBufferManager,
                                                            this, aParent));
}

// dispatched function
void
SharedBufferManagerChild::AllocGrallocBufferSync(const GrallocParam& aParam,
                                                 Monitor* aBarrier,
                                                 bool* aDone)
{
  MonitorAutoLock autoMon(*aBarrier);

  sSharedBufferManagerChildSingleton->AllocGrallocBufferNow(aParam.size,
                                                            aParam.format,
                                                            aParam.usage,
                                                            aParam.buffer);
  *aDone = true;
  aBarrier->NotifyAll();
}

// dispatched function
void
SharedBufferManagerChild::DeallocGrallocBufferSync(const mozilla::layers::MaybeMagicGrallocBufferHandle& aBuffer)
{
  SharedBufferManagerChild::sSharedBufferManagerChildSingleton->
    DeallocGrallocBufferNow(aBuffer);
}

bool
SharedBufferManagerChild::AllocGrallocBuffer(const gfx::IntSize& aSize,
                                             const uint32_t& aFormat,
                                             const uint32_t& aUsage,
                                             mozilla::layers::MaybeMagicGrallocBufferHandle* aBuffer)
{
  if (aSize.width <= 0 || aSize.height <= 0) {
    gfxDebug() << "Asking for gralloc of invalid size " << aSize.width << "x" << aSize.height;
    return false;
  }

  if (InSharedBufferManagerChildThread()) {
    return SharedBufferManagerChild::AllocGrallocBufferNow(aSize, aFormat, aUsage, aBuffer);
  }

  Monitor barrier("AllocSurfaceDescriptorGralloc Lock");
  MonitorAutoLock autoMon(barrier);
  bool done = false;

  GetMessageLoop()->PostTask(
    FROM_HERE,
    NewRunnableFunction(&AllocGrallocBufferSync,
                        GrallocParam(aSize, aFormat, aUsage, aBuffer), &barrier, &done));

  while (!done) {
    barrier.Wait();
  }
  return true;
}

bool
SharedBufferManagerChild::AllocGrallocBufferNow(const IntSize& aSize,
                                                const uint32_t& aFormat,
                                                const uint32_t& aUsage,
                                                mozilla::layers::MaybeMagicGrallocBufferHandle* aHandle)
{
  // These are protected functions, we can just assert and ask the caller to test
  MOZ_ASSERT(aSize.width >= 0 && aSize.height >= 0);

#ifdef MOZ_HAVE_SURFACEDESCRIPTORGRALLOC
  mozilla::layers::MaybeMagicGrallocBufferHandle handle;
  if (!SendAllocateGrallocBuffer(aSize, aFormat, aUsage, &handle)) {
    return false;
  }
  if (handle.type() != mozilla::layers::MaybeMagicGrallocBufferHandle::TMagicGrallocBufferHandle) {
    return false;
  }
  *aHandle = handle.get_MagicGrallocBufferHandle().mRef;

  {
    MutexAutoLock lock(mBufferMutex);
    MOZ_ASSERT(mBuffers.count(handle.get_MagicGrallocBufferHandle().mRef.mKey)==0);
    mBuffers[handle.get_MagicGrallocBufferHandle().mRef.mKey] = handle.get_MagicGrallocBufferHandle().mGraphicBuffer;
  }
  return true;
#else
  NS_RUNTIMEABORT("No GrallocBuffer for you");
  return true;
#endif
}

void
SharedBufferManagerChild::DeallocGrallocBuffer(const mozilla::layers::MaybeMagicGrallocBufferHandle& aBuffer)
{
#ifdef MOZ_HAVE_SURFACEDESCRIPTORGRALLOC
  NS_ASSERTION(aBuffer.type() != mozilla::layers::MaybeMagicGrallocBufferHandle::TMagicGrallocBufferHandle, "We shouldn't try to do IPC with real buffer");
  if (aBuffer.type() != mozilla::layers::MaybeMagicGrallocBufferHandle::TGrallocBufferRef) {
    return;
  }
#endif

  if (InSharedBufferManagerChildThread()) {
    return SharedBufferManagerChild::DeallocGrallocBufferNow(aBuffer);
  }

  GetMessageLoop()->PostTask(FROM_HERE, NewRunnableFunction(&DeallocGrallocBufferSync,
                                                            aBuffer));
}

void
SharedBufferManagerChild::DeallocGrallocBufferNow(const mozilla::layers::MaybeMagicGrallocBufferHandle& aBuffer)
{
#ifdef MOZ_HAVE_SURFACEDESCRIPTORGRALLOC
  NS_ASSERTION(aBuffer.type() != mozilla::layers::MaybeMagicGrallocBufferHandle::TMagicGrallocBufferHandle, "We shouldn't try to do IPC with real buffer");

  {
    MutexAutoLock lock(mBufferMutex);
    mBuffers.erase(aBuffer.get_GrallocBufferRef().mKey);
  }
  SendDropGrallocBuffer(aBuffer);
#else
  NS_RUNTIMEABORT("No GrallocBuffer for you");
#endif
}

void
SharedBufferManagerChild::DropGrallocBuffer(const mozilla::layers::MaybeMagicGrallocBufferHandle& aHandle)
{
#ifdef MOZ_HAVE_SURFACEDESCRIPTORGRALLOC
  int64_t bufferKey = -1;
  if (aHandle.type() == mozilla::layers::MaybeMagicGrallocBufferHandle::TMagicGrallocBufferHandle) {
    bufferKey = aHandle.get_MagicGrallocBufferHandle().mRef.mKey;
  } else if (aHandle.type() == mozilla::layers::MaybeMagicGrallocBufferHandle::TGrallocBufferRef) {
    bufferKey = aHandle.get_GrallocBufferRef().mKey;
  } else {
    return;
  }

  {
    MutexAutoLock lock(mBufferMutex);
    mBuffers.erase(bufferKey);
  }
#endif
}

bool SharedBufferManagerChild::RecvDropGrallocBuffer(const mozilla::layers::MaybeMagicGrallocBufferHandle& aHandle)
{
  DropGrallocBuffer(aHandle);
  return true;
}

#ifdef MOZ_HAVE_SURFACEDESCRIPTORGRALLOC
android::sp<android::GraphicBuffer>
SharedBufferManagerChild::GetGraphicBuffer(int64_t key)
{
  MutexAutoLock lock(mBufferMutex);
  if (mBuffers.count(key) == 0) {
    printf_stderr("SharedBufferManagerChild::GetGraphicBuffer -- invalid key");
    return nullptr;
  }
  return mBuffers[key];
}
#endif

} /* namespace layers */
} /* namespace mozilla */