michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- 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: #ifndef GFX_LAYERS_ISURFACEDEALLOCATOR michael@0: #define GFX_LAYERS_ISURFACEDEALLOCATOR michael@0: michael@0: #include // for size_t michael@0: #include // for uint32_t michael@0: #include "gfxTypes.h" michael@0: #include "mozilla/gfx/Point.h" // for IntSize michael@0: #include "mozilla/ipc/SharedMemory.h" // for SharedMemory, etc michael@0: #include "mozilla/RefPtr.h" michael@0: #include "nsIMemoryReporter.h" // for nsIMemoryReporter michael@0: #include "mozilla/Atomics.h" // for Atomic michael@0: #include "mozilla/layers/LayersMessages.h" // for ShmemSection michael@0: #include "LayersTypes.h" michael@0: #include michael@0: #include "mozilla/layers/AtomicRefCountedWithFinalize.h" michael@0: michael@0: /* michael@0: * FIXME [bjacob] *** PURE CRAZYNESS WARNING *** michael@0: * michael@0: * This #define is actually needed here, because subclasses of ISurfaceAllocator, michael@0: * namely ShadowLayerForwarder, will or will not override AllocGrallocBuffer michael@0: * depending on whether MOZ_HAVE_SURFACEDESCRIPTORGRALLOC is defined. michael@0: */ michael@0: #ifdef MOZ_WIDGET_GONK michael@0: #define MOZ_HAVE_SURFACEDESCRIPTORGRALLOC michael@0: #endif michael@0: michael@0: class gfxSharedImageSurface; michael@0: michael@0: namespace base { michael@0: class Thread; michael@0: } michael@0: michael@0: namespace mozilla { michael@0: namespace ipc { michael@0: class Shmem; michael@0: } michael@0: namespace gfx { michael@0: class DataSourceSurface; michael@0: } michael@0: michael@0: namespace layers { michael@0: michael@0: class PGrallocBufferChild; michael@0: class MaybeMagicGrallocBufferHandle; michael@0: class MemoryTextureClient; michael@0: class MemoryTextureHost; michael@0: michael@0: enum BufferCapabilities { michael@0: DEFAULT_BUFFER_CAPS = 0, michael@0: /** michael@0: * The allocated buffer must be efficiently mappable as a michael@0: * gfxImageSurface. michael@0: */ michael@0: MAP_AS_IMAGE_SURFACE = 1 << 0, michael@0: /** michael@0: * The allocated buffer will be used for GL rendering only michael@0: */ michael@0: USING_GL_RENDERING_ONLY = 1 << 1 michael@0: }; michael@0: michael@0: class SurfaceDescriptor; michael@0: michael@0: michael@0: mozilla::ipc::SharedMemory::SharedMemoryType OptimalShmemType(); michael@0: bool IsSurfaceDescriptorValid(const SurfaceDescriptor& aSurface); michael@0: bool IsSurfaceDescriptorOwned(const SurfaceDescriptor& aDescriptor); michael@0: bool ReleaseOwnedSurfaceDescriptor(const SurfaceDescriptor& aDescriptor); michael@0: michael@0: TemporaryRef GetDrawTargetForDescriptor(const SurfaceDescriptor& aDescriptor, gfx::BackendType aBackend); michael@0: TemporaryRef GetSurfaceForDescriptor(const SurfaceDescriptor& aDescriptor); michael@0: /** michael@0: * An interface used to create and destroy surfaces that are shared with the michael@0: * Compositor process (using shmem, or gralloc, or other platform specific memory) michael@0: * michael@0: * Most of the methods here correspond to methods that are implemented by IPDL michael@0: * actors without a common polymorphic interface. michael@0: * These methods should be only called in the ipdl implementor's thread, unless michael@0: * specified otherwise in the implementing class. michael@0: */ michael@0: class ISurfaceAllocator : public AtomicRefCountedWithFinalize michael@0: { michael@0: public: michael@0: MOZ_DECLARE_REFCOUNTED_TYPENAME(ISurfaceAllocator) michael@0: ISurfaceAllocator() {} michael@0: michael@0: void Finalize(); michael@0: michael@0: /** michael@0: * Returns the type of backend that is used off the main thread. michael@0: * We only don't allow changing the backend type at runtime so this value can michael@0: * be queried once and will not change until Gecko is restarted. michael@0: * michael@0: * XXX - With e10s this may not be true anymore. we can have accelerated widgets michael@0: * and non-accelerated widgets (small popups, etc.) michael@0: */ michael@0: virtual LayersBackend GetCompositorBackendType() const = 0; michael@0: michael@0: /** michael@0: * Allocate shared memory that can be accessed by only one process at a time. michael@0: * Ownership of this memory is passed when the memory is sent in an IPDL michael@0: * message. michael@0: */ michael@0: virtual bool AllocShmem(size_t aSize, michael@0: mozilla::ipc::SharedMemory::SharedMemoryType aType, michael@0: mozilla::ipc::Shmem* aShmem) = 0; michael@0: michael@0: /** michael@0: * Allocate shared memory that can be accessed by both processes at the michael@0: * same time. Safety is left for the user of the memory to care about. michael@0: */ michael@0: virtual bool AllocUnsafeShmem(size_t aSize, michael@0: mozilla::ipc::SharedMemory::SharedMemoryType aType, michael@0: mozilla::ipc::Shmem* aShmem) = 0; michael@0: michael@0: /** michael@0: * Allocate memory in shared memory that can always be accessed by both michael@0: * processes at a time. Safety is left for the user of the memory to care michael@0: * about. michael@0: */ michael@0: bool AllocShmemSection(size_t aSize, michael@0: mozilla::layers::ShmemSection* aShmemSection); michael@0: michael@0: /** michael@0: * Deallocates a shmem section. michael@0: */ michael@0: void FreeShmemSection(mozilla::layers::ShmemSection& aShmemSection); michael@0: michael@0: /** michael@0: * Deallocate memory allocated by either AllocShmem or AllocUnsafeShmem. michael@0: */ michael@0: virtual void DeallocShmem(mozilla::ipc::Shmem& aShmem) = 0; michael@0: michael@0: // was AllocBuffer michael@0: virtual bool AllocSurfaceDescriptor(const gfx::IntSize& aSize, michael@0: gfxContentType aContent, michael@0: SurfaceDescriptor* aBuffer); michael@0: michael@0: // was AllocBufferWithCaps michael@0: virtual bool AllocSurfaceDescriptorWithCaps(const gfx::IntSize& aSize, michael@0: gfxContentType aContent, michael@0: uint32_t aCaps, michael@0: SurfaceDescriptor* aBuffer); michael@0: michael@0: /** michael@0: * Returns the maximum texture size supported by the compositor. michael@0: */ michael@0: virtual int32_t GetMaxTextureSize() const { return INT32_MAX; } michael@0: michael@0: virtual void DestroySharedSurface(SurfaceDescriptor* aSurface); michael@0: michael@0: // method that does the actual allocation work michael@0: virtual PGrallocBufferChild* AllocGrallocBuffer(const gfx::IntSize& aSize, michael@0: uint32_t aFormat, michael@0: uint32_t aUsage, michael@0: MaybeMagicGrallocBufferHandle* aHandle) michael@0: { michael@0: return nullptr; michael@0: } michael@0: michael@0: virtual void DeallocGrallocBuffer(PGrallocBufferChild* aChild) michael@0: { michael@0: NS_RUNTIMEABORT("should not be called"); michael@0: } michael@0: michael@0: virtual bool IPCOpen() const { return true; } michael@0: virtual bool IsSameProcess() const = 0; michael@0: michael@0: // Returns true if aSurface wraps a Shmem. michael@0: static bool IsShmem(SurfaceDescriptor* aSurface); michael@0: michael@0: protected: michael@0: michael@0: virtual bool IsOnCompositorSide() const = 0; michael@0: michael@0: virtual ~ISurfaceAllocator(); michael@0: michael@0: void ShrinkShmemSectionHeap(); michael@0: michael@0: // This is used to implement an extremely simple & naive heap allocator. michael@0: std::vector mUsedShmems; michael@0: michael@0: friend class AtomicRefCountedWithFinalize; michael@0: }; michael@0: michael@0: class GfxMemoryImageReporter MOZ_FINAL : public nsIMemoryReporter michael@0: { michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: michael@0: GfxMemoryImageReporter() michael@0: { michael@0: #ifdef DEBUG michael@0: // There must be only one instance of this class, due to |sAmount| michael@0: // being static. michael@0: static bool hasRun = false; michael@0: MOZ_ASSERT(!hasRun); michael@0: hasRun = true; michael@0: #endif michael@0: } michael@0: michael@0: MOZ_DEFINE_MALLOC_SIZE_OF_ON_ALLOC(MallocSizeOfOnAlloc) michael@0: MOZ_DEFINE_MALLOC_SIZE_OF_ON_FREE(MallocSizeOfOnFree) michael@0: michael@0: static void DidAlloc(void* aPointer) michael@0: { michael@0: sAmount += MallocSizeOfOnAlloc(aPointer); michael@0: } michael@0: michael@0: static void WillFree(void* aPointer) michael@0: { michael@0: sAmount -= MallocSizeOfOnFree(aPointer); 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: "explicit/gfx/heap-textures", KIND_HEAP, UNITS_BYTES, sAmount, michael@0: "Heap memory shared between threads by texture clients and hosts."); michael@0: } michael@0: michael@0: private: michael@0: static mozilla::Atomic sAmount; michael@0: }; michael@0: michael@0: } // namespace michael@0: } // namespace michael@0: michael@0: #endif