1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef GFX_LAYERS_ISURFACEDEALLOCATOR
7 #define GFX_LAYERS_ISURFACEDEALLOCATOR
9 #include <stddef.h> // for size_t
10 #include <stdint.h> // for uint32_t
12 #include "mozilla/gfx/Point.h" // for IntSize
13 #include "mozilla/ipc/SharedMemory.h" // for SharedMemory, etc
14 #include "mozilla/RefPtr.h"
15 #include "nsIMemoryReporter.h" // for nsIMemoryReporter
16 #include "mozilla/Atomics.h" // for Atomic
17 #include "mozilla/layers/LayersMessages.h" // for ShmemSection
18 #include "LayersTypes.h"
20 #include "mozilla/layers/AtomicRefCountedWithFinalize.h"
23 * FIXME [bjacob] *** PURE CRAZYNESS WARNING ***
25 * This #define is actually needed here, because subclasses of ISurfaceAllocator,
26 * namely ShadowLayerForwarder, will or will not override AllocGrallocBuffer
27 * depending on whether MOZ_HAVE_SURFACEDESCRIPTORGRALLOC is defined.
29 #ifdef MOZ_WIDGET_GONK
30 #define MOZ_HAVE_SURFACEDESCRIPTORGRALLOC
33 class gfxSharedImageSurface
;
44 class DataSourceSurface
;
49 class MaybeMagicGrallocBufferHandle
;
50 class MemoryTextureClient
;
51 class MemoryTextureHost
;
53 enum BufferCapabilities
{
54 DEFAULT_BUFFER_CAPS
= 0,
56 * The allocated buffer must be efficiently mappable as a DataSourceSurface.
58 MAP_AS_IMAGE_SURFACE
= 1 << 0,
60 * The allocated buffer will be used for GL rendering only
62 USING_GL_RENDERING_ONLY
= 1 << 1
65 class SurfaceDescriptor
;
68 mozilla::ipc::SharedMemory::SharedMemoryType
OptimalShmemType();
69 bool IsSurfaceDescriptorValid(const SurfaceDescriptor
& aSurface
);
70 bool IsSurfaceDescriptorOwned(const SurfaceDescriptor
& aDescriptor
);
71 bool ReleaseOwnedSurfaceDescriptor(const SurfaceDescriptor
& aDescriptor
);
73 TemporaryRef
<gfx::DrawTarget
> GetDrawTargetForDescriptor(const SurfaceDescriptor
& aDescriptor
, gfx::BackendType aBackend
);
74 TemporaryRef
<gfx::DataSourceSurface
> GetSurfaceForDescriptor(const SurfaceDescriptor
& aDescriptor
);
76 * An interface used to create and destroy surfaces that are shared with the
77 * Compositor process (using shmem, or gralloc, or other platform specific memory)
79 * Most of the methods here correspond to methods that are implemented by IPDL
80 * actors without a common polymorphic interface.
81 * These methods should be only called in the ipdl implementor's thread, unless
82 * specified otherwise in the implementing class.
84 class ISurfaceAllocator
: public AtomicRefCountedWithFinalize
<ISurfaceAllocator
>
87 MOZ_DECLARE_REFCOUNTED_TYPENAME(ISurfaceAllocator
)
89 : mDefaultMessageLoop(MessageLoop::current())
95 * Returns the type of backend that is used off the main thread.
96 * We only don't allow changing the backend type at runtime so this value can
97 * be queried once and will not change until Gecko is restarted.
99 * XXX - With e10s this may not be true anymore. we can have accelerated widgets
100 * and non-accelerated widgets (small popups, etc.)
102 virtual LayersBackend
GetCompositorBackendType() const = 0;
105 * Allocate shared memory that can be accessed by only one process at a time.
106 * Ownership of this memory is passed when the memory is sent in an IPDL
109 virtual bool AllocShmem(size_t aSize
,
110 mozilla::ipc::SharedMemory::SharedMemoryType aType
,
111 mozilla::ipc::Shmem
* aShmem
) = 0;
114 * Allocate shared memory that can be accessed by both processes at the
115 * same time. Safety is left for the user of the memory to care about.
117 virtual bool AllocUnsafeShmem(size_t aSize
,
118 mozilla::ipc::SharedMemory::SharedMemoryType aType
,
119 mozilla::ipc::Shmem
* aShmem
) = 0;
122 * Allocate memory in shared memory that can always be accessed by both
123 * processes at a time. Safety is left for the user of the memory to care
126 bool AllocShmemSection(size_t aSize
,
127 mozilla::layers::ShmemSection
* aShmemSection
);
130 * Deallocates a shmem section.
132 void FreeShmemSection(mozilla::layers::ShmemSection
& aShmemSection
);
135 * Deallocate memory allocated by either AllocShmem or AllocUnsafeShmem.
137 virtual void DeallocShmem(mozilla::ipc::Shmem
& aShmem
) = 0;
140 virtual bool AllocSurfaceDescriptor(const gfx::IntSize
& aSize
,
141 gfxContentType aContent
,
142 SurfaceDescriptor
* aBuffer
);
144 // was AllocBufferWithCaps
145 virtual bool AllocSurfaceDescriptorWithCaps(const gfx::IntSize
& aSize
,
146 gfxContentType aContent
,
148 SurfaceDescriptor
* aBuffer
);
151 * Returns the maximum texture size supported by the compositor.
153 virtual int32_t GetMaxTextureSize() const { return INT32_MAX
; }
155 virtual void DestroySharedSurface(SurfaceDescriptor
* aSurface
);
157 // method that does the actual allocation work
158 bool AllocGrallocBuffer(const gfx::IntSize
& aSize
,
161 MaybeMagicGrallocBufferHandle
* aHandle
);
163 void DeallocGrallocBuffer(MaybeMagicGrallocBufferHandle
* aHandle
);
165 void DropGrallocBuffer(MaybeMagicGrallocBufferHandle
* aHandle
);
167 virtual bool IPCOpen() const { return true; }
168 virtual bool IsSameProcess() const = 0;
170 virtual bool IsImageBridgeChild() const { return false; }
172 virtual MessageLoop
* GetMessageLoop() const
174 return mDefaultMessageLoop
;
177 // Returns true if aSurface wraps a Shmem.
178 static bool IsShmem(SurfaceDescriptor
* aSurface
);
182 virtual bool IsOnCompositorSide() const = 0;
184 virtual ~ISurfaceAllocator();
186 void ShrinkShmemSectionHeap();
188 // This is used to implement an extremely simple & naive heap allocator.
189 std::vector
<mozilla::ipc::Shmem
> mUsedShmems
;
191 MessageLoop
* mDefaultMessageLoop
;
193 friend class AtomicRefCountedWithFinalize
<ISurfaceAllocator
>;
196 class GfxMemoryImageReporter MOZ_FINAL
: public nsIMemoryReporter
198 ~GfxMemoryImageReporter() {}
203 GfxMemoryImageReporter()
206 // There must be only one instance of this class, due to |sAmount|
208 static bool hasRun
= false;
214 MOZ_DEFINE_MALLOC_SIZE_OF_ON_ALLOC(MallocSizeOfOnAlloc
)
215 MOZ_DEFINE_MALLOC_SIZE_OF_ON_FREE(MallocSizeOfOnFree
)
217 static void DidAlloc(void* aPointer
)
219 sAmount
+= MallocSizeOfOnAlloc(aPointer
);
222 static void WillFree(void* aPointer
)
224 sAmount
-= MallocSizeOfOnFree(aPointer
);
227 NS_IMETHOD
CollectReports(nsIHandleReportCallback
* aHandleReport
,
228 nsISupports
* aData
, bool aAnonymize
)
230 return MOZ_COLLECT_REPORT(
231 "explicit/gfx/heap-textures", KIND_HEAP
, UNITS_BYTES
, sAmount
,
232 "Heap memory shared between threads by texture clients and hosts.");
236 static mozilla::Atomic
<size_t> sAmount
;