Bug 1039883 - release Tiled layer's gralloc when an application is background r=nical
[gecko.git] / gfx / layers / ipc / ISurfaceAllocator.h
blobd0a070847b4131161b755e764081afc31fcf6351
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
11 #include "gfxTypes.h"
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"
19 #include <vector>
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
31 #endif
33 class gfxSharedImageSurface;
35 namespace base {
36 class Thread;
39 namespace mozilla {
40 namespace ipc {
41 class Shmem;
43 namespace gfx {
44 class DataSourceSurface;
47 namespace layers {
49 class MaybeMagicGrallocBufferHandle;
50 class MemoryTextureClient;
51 class MemoryTextureHost;
53 enum BufferCapabilities {
54 DEFAULT_BUFFER_CAPS = 0,
55 /**
56 * The allocated buffer must be efficiently mappable as a DataSourceSurface.
58 MAP_AS_IMAGE_SURFACE = 1 << 0,
59 /**
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);
75 /**
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>
86 public:
87 MOZ_DECLARE_REFCOUNTED_TYPENAME(ISurfaceAllocator)
88 ISurfaceAllocator()
89 : mDefaultMessageLoop(MessageLoop::current())
92 void Finalize();
94 /**
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
107 * message.
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
124 * about.
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;
139 // was AllocBuffer
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,
147 uint32_t aCaps,
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,
159 uint32_t aFormat,
160 uint32_t aUsage,
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);
180 protected:
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() {}
200 public:
201 NS_DECL_ISUPPORTS
203 GfxMemoryImageReporter()
205 #ifdef DEBUG
206 // There must be only one instance of this class, due to |sAmount|
207 // being static.
208 static bool hasRun = false;
209 MOZ_ASSERT(!hasRun);
210 hasRun = true;
211 #endif
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.");
235 private:
236 static mozilla::Atomic<size_t> sAmount;
239 } // namespace
240 } // namespace
242 #endif