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 "mozilla/DebugOnly.h" michael@0: michael@0: #include "mozilla/gfx/Point.h" michael@0: #include "mozilla/layers/PGrallocBufferChild.h" michael@0: #include "mozilla/layers/PGrallocBufferParent.h" michael@0: #include "mozilla/layers/LayerTransactionChild.h" michael@0: #include "mozilla/layers/ShadowLayers.h" michael@0: #include "mozilla/layers/LayerManagerComposite.h" michael@0: #include "mozilla/layers/CompositorTypes.h" michael@0: #include "mozilla/layers/TextureHost.h" michael@0: #include "mozilla/unused.h" michael@0: #include "nsXULAppAPI.h" michael@0: michael@0: #include "ShadowLayerUtilsGralloc.h" michael@0: michael@0: #include "nsIMemoryReporter.h" michael@0: michael@0: #include "gfxPlatform.h" michael@0: #include "gfx2DGlue.h" michael@0: #include "GLContext.h" michael@0: michael@0: #include "GeckoProfiler.h" michael@0: michael@0: #include "cutils/properties.h" michael@0: michael@0: #include "MainThreadUtils.h" michael@0: michael@0: using namespace android; michael@0: using namespace base; michael@0: using namespace mozilla::layers; michael@0: using namespace mozilla::gl; 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.mGraphicBuffer; michael@0: #else michael@0: Flattenable *flattenable = aParam.mGraphicBuffer.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: 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 GraphicBuffer()); 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->mGraphicBuffer = 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: MagicGrallocBufferHandle::MagicGrallocBufferHandle(const sp& aGraphicBuffer) michael@0: : mGraphicBuffer(aGraphicBuffer) michael@0: { michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // Parent process michael@0: michael@0: static gfxImageFormat michael@0: ImageFormatForPixelFormat(android::PixelFormat aFormat) michael@0: { michael@0: switch (aFormat) { michael@0: case PIXEL_FORMAT_RGBA_8888: michael@0: return gfxImageFormat::ARGB32; michael@0: case PIXEL_FORMAT_RGBX_8888: michael@0: return gfxImageFormat::RGB24; michael@0: case PIXEL_FORMAT_RGB_565: michael@0: return gfxImageFormat::RGB16_565; michael@0: default: michael@0: MOZ_CRASH("Unknown gralloc pixel format"); michael@0: } michael@0: return gfxImageFormat::ARGB32; michael@0: } michael@0: michael@0: static android::PixelFormat michael@0: PixelFormatForImageFormat(gfxImageFormat aFormat) michael@0: { michael@0: switch (aFormat) { michael@0: case gfxImageFormat::ARGB32: michael@0: return android::PIXEL_FORMAT_RGBA_8888; michael@0: case gfxImageFormat::RGB24: michael@0: return android::PIXEL_FORMAT_RGBX_8888; michael@0: case gfxImageFormat::RGB16_565: michael@0: return android::PIXEL_FORMAT_RGB_565; michael@0: case gfxImageFormat::A8: michael@0: NS_WARNING("gralloc does not support gfxImageFormat::A8"); michael@0: return android::PIXEL_FORMAT_UNKNOWN; michael@0: default: michael@0: MOZ_CRASH("Unknown gralloc pixel format"); michael@0: } michael@0: return android::PIXEL_FORMAT_RGBA_8888; michael@0: } michael@0: michael@0: static size_t michael@0: BytesPerPixelForPixelFormat(android::PixelFormat aFormat) michael@0: { michael@0: switch (aFormat) { michael@0: case PIXEL_FORMAT_RGBA_8888: michael@0: case PIXEL_FORMAT_RGBX_8888: michael@0: case PIXEL_FORMAT_BGRA_8888: michael@0: return 4; michael@0: case PIXEL_FORMAT_RGB_888: michael@0: return 3; michael@0: case PIXEL_FORMAT_RGB_565: michael@0: case PIXEL_FORMAT_RGBA_5551: michael@0: case PIXEL_FORMAT_RGBA_4444: michael@0: return 2; michael@0: default: michael@0: return 0; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: static android::PixelFormat michael@0: PixelFormatForContentType(gfxContentType aContentType) michael@0: { michael@0: return PixelFormatForImageFormat( michael@0: gfxPlatform::GetPlatform()->OptimalFormatForContent(aContentType)); michael@0: } michael@0: michael@0: static gfxContentType michael@0: ContentTypeFromPixelFormat(android::PixelFormat aFormat) michael@0: { michael@0: return gfxASurface::ContentFromFormat(ImageFormatForPixelFormat(aFormat)); michael@0: } michael@0: michael@0: class GrallocReporter MOZ_FINAL : public nsIMemoryReporter michael@0: { michael@0: friend class GrallocBufferActor; michael@0: michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: michael@0: GrallocReporter() michael@0: { michael@0: #ifdef DEBUG michael@0: // There must be only one instance of this class, due to |sAmount| michael@0: // being static. Assert this. michael@0: static bool hasRun = false; michael@0: MOZ_ASSERT(!hasRun); michael@0: hasRun = true; michael@0: #endif michael@0: } michael@0: michael@0: NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport, michael@0: nsISupports* aData) michael@0: { michael@0: return MOZ_COLLECT_REPORT( michael@0: "gralloc", KIND_OTHER, UNITS_BYTES, sAmount, michael@0: "Special RAM that can be shared between processes and directly accessed by " michael@0: "both the CPU and GPU. Gralloc memory is usually a relatively precious " michael@0: "resource, with much less available than generic RAM. When it's exhausted, " michael@0: "graphics performance can suffer. This value can be incorrect because of race " michael@0: "conditions."); michael@0: } michael@0: michael@0: private: michael@0: static int64_t sAmount; michael@0: }; michael@0: michael@0: NS_IMPL_ISUPPORTS(GrallocReporter, nsIMemoryReporter) michael@0: michael@0: int64_t GrallocReporter::sAmount = 0; michael@0: michael@0: void InitGralloc() { michael@0: NS_ASSERTION(NS_IsMainThread(), "Should be on main thread."); michael@0: RegisterStrongMemoryReporter(new GrallocReporter()); michael@0: } michael@0: michael@0: GrallocBufferActor::GrallocBufferActor() michael@0: : mAllocBytes(0) michael@0: , mTextureHost(nullptr) michael@0: { michael@0: } michael@0: michael@0: GrallocBufferActor::~GrallocBufferActor() michael@0: { michael@0: if (mAllocBytes > 0) { michael@0: GrallocReporter::sAmount -= mAllocBytes; michael@0: } michael@0: } michael@0: michael@0: /*static*/ PGrallocBufferParent* michael@0: GrallocBufferActor::Create(const gfx::IntSize& aSize, michael@0: const uint32_t& aFormat, michael@0: const uint32_t& aUsage, michael@0: MaybeMagicGrallocBufferHandle* aOutHandle) michael@0: { michael@0: PROFILER_LABEL("GrallocBufferActor", "Create"); michael@0: GrallocBufferActor* actor = new GrallocBufferActor(); michael@0: *aOutHandle = null_t(); michael@0: uint32_t format = aFormat; michael@0: uint32_t usage = aUsage; michael@0: michael@0: if (format == 0 || usage == 0) { michael@0: printf_stderr("GrallocBufferActor::Create -- format and usage must be non-zero"); michael@0: return actor; michael@0: } michael@0: michael@0: // If the requested size is too big (i.e. exceeds the commonly used max GL texture size) michael@0: // then we risk OOMing the parent process. It's better to just deny the allocation and michael@0: // kill the child process, which is what the following code does. michael@0: // TODO: actually use GL_MAX_TEXTURE_SIZE instead of hardcoding 4096 michael@0: if (aSize.width > 4096 || aSize.height > 4096) { michael@0: printf_stderr("GrallocBufferActor::Create -- requested gralloc buffer is too big. Killing child instead."); michael@0: delete actor; michael@0: return nullptr; michael@0: } michael@0: michael@0: sp buffer(new GraphicBuffer(aSize.width, aSize.height, format, usage)); michael@0: if (buffer->initCheck() != OK) michael@0: return actor; michael@0: michael@0: size_t bpp = BytesPerPixelForPixelFormat(format); michael@0: actor->mAllocBytes = aSize.width * aSize.height * bpp; michael@0: GrallocReporter::sAmount += actor->mAllocBytes; michael@0: michael@0: actor->mGraphicBuffer = buffer; michael@0: *aOutHandle = MagicGrallocBufferHandle(buffer); michael@0: michael@0: return actor; michael@0: } michael@0: michael@0: void GrallocBufferActor::ActorDestroy(ActorDestroyReason) michael@0: { michael@0: // Used only for hacky fix for bug 966446. michael@0: if (mTextureHost) { michael@0: mTextureHost->ForgetBufferActor(); michael@0: mTextureHost = nullptr; michael@0: } michael@0: } michael@0: michael@0: void GrallocBufferActor::AddTextureHost(TextureHost* aTextureHost) michael@0: { michael@0: mTextureHost = aTextureHost; michael@0: } michael@0: michael@0: void GrallocBufferActor::RemoveTextureHost() michael@0: { michael@0: mTextureHost = nullptr; michael@0: } michael@0: michael@0: /*static*/ bool michael@0: LayerManagerComposite::SupportsDirectTexturing() michael@0: { michael@0: return true; michael@0: } michael@0: michael@0: /*static*/ void michael@0: LayerManagerComposite::PlatformSyncBeforeReplyUpdate() michael@0: { michael@0: // Nothing to be done for gralloc. michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // Child process michael@0: michael@0: /*static*/ PGrallocBufferChild* michael@0: GrallocBufferActor::Create() michael@0: { michael@0: return new GrallocBufferActor(); michael@0: } michael@0: michael@0: void michael@0: GrallocBufferActor::InitFromHandle(const MagicGrallocBufferHandle& aHandle) michael@0: { michael@0: MOZ_ASSERT(!mGraphicBuffer.get()); michael@0: MOZ_ASSERT(aHandle.mGraphicBuffer.get()); michael@0: michael@0: mGraphicBuffer = aHandle.mGraphicBuffer; michael@0: } michael@0: michael@0: PGrallocBufferChild* michael@0: ShadowLayerForwarder::AllocGrallocBuffer(const gfx::IntSize& aSize, michael@0: uint32_t aFormat, michael@0: uint32_t aUsage, michael@0: MaybeMagicGrallocBufferHandle* aHandle) michael@0: { michael@0: if (!mShadowManager->IPCOpen()) { michael@0: return nullptr; michael@0: } michael@0: return mShadowManager->SendPGrallocBufferConstructor(aSize, aFormat, aUsage, aHandle); michael@0: } michael@0: michael@0: void michael@0: ShadowLayerForwarder::DeallocGrallocBuffer(PGrallocBufferChild* aChild) michael@0: { michael@0: MOZ_ASSERT(aChild); michael@0: PGrallocBufferChild::Send__delete__(aChild); michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // Both processes michael@0: michael@0: android::GraphicBuffer* michael@0: GrallocBufferActor::GetGraphicBuffer() michael@0: { michael@0: return mGraphicBuffer.get(); michael@0: } michael@0: michael@0: /*static*/ void michael@0: ShadowLayerForwarder::PlatformSyncBeforeUpdate() michael@0: { michael@0: // Nothing to be done for gralloc. michael@0: } michael@0: michael@0: } // namespace layers michael@0: } // namespace mozilla