2014-07-01 19:26:35 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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 "GMPSharedMemManager.h"
|
|
|
|
#include "GMPMessageUtils.h"
|
|
|
|
#include "mozilla/ipc/SharedMemory.h"
|
2014-07-10 20:35:28 -07:00
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2014-07-01 19:26:35 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gmp {
|
|
|
|
|
|
|
|
// Really one set of pools on each side of the plugin API.
|
|
|
|
|
|
|
|
// YUV buffers go from Encoder parent to child; pool there, and then return
|
|
|
|
// with Decoded() frames to the Decoder parent and goes into the parent pool.
|
|
|
|
// Compressed (encoded) data goes from the Decoder parent to the child;
|
|
|
|
// pool there, and then return with Encoded() frames and goes into the parent
|
|
|
|
// pool.
|
2014-07-10 20:35:28 -07:00
|
|
|
static StaticAutoPtr<nsTArray<ipc::Shmem>> sGmpFreelist[GMPSharedMemManager::kGMPNumTypes];
|
|
|
|
static uint32_t sGMPShmemManagerCount = 0;
|
|
|
|
|
|
|
|
GMPSharedMemManager::GMPSharedMemManager()
|
|
|
|
{
|
|
|
|
if (!sGMPShmemManagerCount) {
|
|
|
|
for (uint32_t i = 0; i < GMPSharedMemManager::kGMPNumTypes; i++) {
|
|
|
|
sGmpFreelist[i] = new nsTArray<ipc::Shmem>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sGMPShmemManagerCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
GMPSharedMemManager::~GMPSharedMemManager()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(sGMPShmemManagerCount > 0);
|
|
|
|
sGMPShmemManagerCount--;
|
|
|
|
if (!sGMPShmemManagerCount) {
|
|
|
|
for (uint32_t i = 0; i < GMPSharedMemManager::kGMPNumTypes; i++) {
|
|
|
|
sGmpFreelist[i] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsTArray<ipc::Shmem>&
|
|
|
|
GetGmpFreelist(GMPSharedMemManager::GMPMemoryClasses aTypes)
|
|
|
|
{
|
|
|
|
return *(sGmpFreelist[aTypes]);
|
|
|
|
}
|
|
|
|
|
2014-07-01 19:26:35 -07:00
|
|
|
static uint32_t sGmpAllocated[GMPSharedMemManager::kGMPNumTypes]; // 0's
|
|
|
|
|
|
|
|
bool
|
|
|
|
GMPSharedMemManager::MgrAllocShmem(GMPMemoryClasses aClass, size_t aSize,
|
|
|
|
ipc::Shmem::SharedMemory::SharedMemoryType aType,
|
|
|
|
ipc::Shmem* aMem)
|
|
|
|
{
|
|
|
|
CheckThread();
|
|
|
|
|
|
|
|
// first look to see if we have a free buffer large enough
|
2014-07-10 20:35:28 -07:00
|
|
|
for (uint32_t i = 0; i < GetGmpFreelist(aClass).Length(); i++) {
|
|
|
|
MOZ_ASSERT(GetGmpFreelist(aClass)[i].IsWritable());
|
|
|
|
if (aSize <= GetGmpFreelist(aClass)[i].Size<uint8_t>()) {
|
|
|
|
*aMem = GetGmpFreelist(aClass)[i];
|
|
|
|
GetGmpFreelist(aClass).RemoveElementAt(i);
|
2014-07-01 19:26:35 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Didn't find a buffer free with enough space; allocate one
|
|
|
|
size_t pagesize = ipc::SharedMemory::SystemPageSize();
|
|
|
|
aSize = (aSize + (pagesize-1)) & ~(pagesize-1); // round up to page size
|
|
|
|
bool retval = Alloc(aSize, aType, aMem);
|
|
|
|
if (retval) {
|
|
|
|
sGmpAllocated[aClass]++;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GMPSharedMemManager::MgrDeallocShmem(GMPMemoryClasses aClass, ipc::Shmem& aMem)
|
|
|
|
{
|
|
|
|
CheckThread();
|
|
|
|
|
|
|
|
size_t size = aMem.Size<uint8_t>();
|
|
|
|
size_t total = 0;
|
|
|
|
// XXX This works; there are better pool algorithms. We need to avoid
|
|
|
|
// "falling off a cliff" with too low a number
|
2014-07-10 20:35:28 -07:00
|
|
|
if (GetGmpFreelist(aClass).Length() > 10) {
|
|
|
|
Dealloc(GetGmpFreelist(aClass)[0]);
|
|
|
|
GetGmpFreelist(aClass).RemoveElementAt(0);
|
2014-07-01 19:26:35 -07:00
|
|
|
// The allocation numbers will be fubar on the Child!
|
|
|
|
sGmpAllocated[aClass]--;
|
|
|
|
}
|
2014-07-10 20:35:28 -07:00
|
|
|
for (uint32_t i = 0; i < GetGmpFreelist(aClass).Length(); i++) {
|
|
|
|
MOZ_ASSERT(GetGmpFreelist(aClass)[i].IsWritable());
|
|
|
|
total += GetGmpFreelist(aClass)[i].Size<uint8_t>();
|
|
|
|
if (size < GetGmpFreelist(aClass)[i].Size<uint8_t>()) {
|
|
|
|
GetGmpFreelist(aClass).InsertElementAt(i, aMem);
|
2014-07-01 19:26:35 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-07-10 20:35:28 -07:00
|
|
|
GetGmpFreelist(aClass).AppendElement(aMem);
|
2014-07-01 19:26:35 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
GMPSharedMemManager::NumInUse(GMPMemoryClasses aClass)
|
|
|
|
{
|
2014-07-10 20:35:28 -07:00
|
|
|
return sGmpAllocated[aClass] - GetGmpFreelist(aClass).Length();
|
2014-07-01 19:26:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|