michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=8 et : michael@0: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "GLContext.h" michael@0: #include "mozilla/unused.h" michael@0: #include "nsXULAppAPI.h" michael@0: michael@0: #include "FenceUtilsGonk.h" michael@0: michael@0: using namespace android; michael@0: using namespace base; michael@0: using namespace mozilla::layers; michael@0: michael@0: namespace IPC { michael@0: michael@0: void michael@0: ParamTraits::Write(Message* aMsg, michael@0: const paramType& aParam) michael@0: { michael@0: #if ANDROID_VERSION >= 19 michael@0: sp flattenable = aParam.mFence; michael@0: #else michael@0: Flattenable *flattenable = aParam.mFence.get(); michael@0: #endif michael@0: size_t nbytes = flattenable->getFlattenedSize(); michael@0: size_t nfds = flattenable->getFdCount(); michael@0: michael@0: char data[nbytes]; michael@0: int fds[nfds]; michael@0: michael@0: #if ANDROID_VERSION >= 19 michael@0: // Make a copy of "data" and "fds" for flatten() to avoid casting problem michael@0: void *pdata = (void *)data; michael@0: int *pfds = fds; michael@0: michael@0: flattenable->flatten(pdata, nbytes, pfds, nfds); michael@0: michael@0: // In Kitkat, flatten() will change the value of nbytes and nfds, which dues michael@0: // to multiple parcelable object consumption. The actual size and fd count michael@0: // which returned by getFlattenedSize() and getFdCount() are not changed. michael@0: // So we change nbytes and nfds back by call corresponding calls. michael@0: nbytes = flattenable->getFlattenedSize(); michael@0: nfds = flattenable->getFdCount(); michael@0: #else michael@0: flattenable->flatten(data, nbytes, fds, nfds); michael@0: #endif michael@0: aMsg->WriteSize(nbytes); michael@0: aMsg->WriteSize(nfds); michael@0: michael@0: aMsg->WriteBytes(data, nbytes); michael@0: for (size_t n = 0; n < nfds; ++n) { michael@0: // These buffers can't die in transit because they're created michael@0: // synchonously and the parent-side buffer can only be dropped if michael@0: // there's a crash. michael@0: aMsg->WriteFileDescriptor(FileDescriptor(fds[n], false)); michael@0: } michael@0: } michael@0: michael@0: bool michael@0: ParamTraits::Read(const Message* aMsg, michael@0: void** aIter, paramType* aResult) michael@0: { michael@0: size_t nbytes; michael@0: size_t nfds; michael@0: const char* data; michael@0: michael@0: if (!aMsg->ReadSize(aIter, &nbytes) || michael@0: !aMsg->ReadSize(aIter, &nfds) || michael@0: !aMsg->ReadBytes(aIter, &data, nbytes)) { michael@0: return false; michael@0: } michael@0: michael@0: int fds[nfds]; michael@0: michael@0: for (size_t n = 0; n < nfds; ++n) { michael@0: FileDescriptor fd; michael@0: if (!aMsg->ReadFileDescriptor(aIter, &fd)) { michael@0: return false; michael@0: } michael@0: // If the GraphicBuffer was shared cross-process, SCM_RIGHTS does michael@0: // the right thing and dup's the fd. If it's shared cross-thread, michael@0: // SCM_RIGHTS doesn't dup the fd. That's surprising, but we just michael@0: // deal with it here. NB: only the "default" (master) process can michael@0: // alloc gralloc buffers. michael@0: bool sameProcess = (XRE_GetProcessType() == GeckoProcessType_Default); michael@0: int dupFd = sameProcess ? dup(fd.fd) : fd.fd; michael@0: fds[n] = dupFd; michael@0: } michael@0: michael@0: sp buffer(new Fence()); michael@0: #if ANDROID_VERSION >= 19 michael@0: // Make a copy of "data" and "fds" for unflatten() to avoid casting problem michael@0: void const *pdata = (void const *)data; michael@0: int const *pfds = fds; michael@0: michael@0: if (NO_ERROR == buffer->unflatten(pdata, nbytes, pfds, nfds)) { michael@0: #else michael@0: Flattenable *flattenable = buffer.get(); michael@0: michael@0: if (NO_ERROR == flattenable->unflatten(data, nbytes, fds, nfds)) { michael@0: #endif michael@0: aResult->mFence = buffer; michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: } // namespace IPC michael@0: michael@0: namespace mozilla { michael@0: namespace layers { michael@0: michael@0: FenceHandle::FenceHandle(const sp& aFence) michael@0: : mFence(aFence) michael@0: { michael@0: } michael@0: michael@0: } // namespace layers michael@0: } // namespace mozilla