Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef GFX_LAYERS_ISURFACEDEALLOCATOR |
michael@0 | 7 | #define GFX_LAYERS_ISURFACEDEALLOCATOR |
michael@0 | 8 | |
michael@0 | 9 | #include <stddef.h> // for size_t |
michael@0 | 10 | #include <stdint.h> // for uint32_t |
michael@0 | 11 | #include "gfxTypes.h" |
michael@0 | 12 | #include "mozilla/gfx/Point.h" // for IntSize |
michael@0 | 13 | #include "mozilla/ipc/SharedMemory.h" // for SharedMemory, etc |
michael@0 | 14 | #include "mozilla/RefPtr.h" |
michael@0 | 15 | #include "nsIMemoryReporter.h" // for nsIMemoryReporter |
michael@0 | 16 | #include "mozilla/Atomics.h" // for Atomic |
michael@0 | 17 | #include "mozilla/layers/LayersMessages.h" // for ShmemSection |
michael@0 | 18 | #include "LayersTypes.h" |
michael@0 | 19 | #include <vector> |
michael@0 | 20 | #include "mozilla/layers/AtomicRefCountedWithFinalize.h" |
michael@0 | 21 | |
michael@0 | 22 | /* |
michael@0 | 23 | * FIXME [bjacob] *** PURE CRAZYNESS WARNING *** |
michael@0 | 24 | * |
michael@0 | 25 | * This #define is actually needed here, because subclasses of ISurfaceAllocator, |
michael@0 | 26 | * namely ShadowLayerForwarder, will or will not override AllocGrallocBuffer |
michael@0 | 27 | * depending on whether MOZ_HAVE_SURFACEDESCRIPTORGRALLOC is defined. |
michael@0 | 28 | */ |
michael@0 | 29 | #ifdef MOZ_WIDGET_GONK |
michael@0 | 30 | #define MOZ_HAVE_SURFACEDESCRIPTORGRALLOC |
michael@0 | 31 | #endif |
michael@0 | 32 | |
michael@0 | 33 | class gfxSharedImageSurface; |
michael@0 | 34 | |
michael@0 | 35 | namespace base { |
michael@0 | 36 | class Thread; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | namespace mozilla { |
michael@0 | 40 | namespace ipc { |
michael@0 | 41 | class Shmem; |
michael@0 | 42 | } |
michael@0 | 43 | namespace gfx { |
michael@0 | 44 | class DataSourceSurface; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | namespace layers { |
michael@0 | 48 | |
michael@0 | 49 | class PGrallocBufferChild; |
michael@0 | 50 | class MaybeMagicGrallocBufferHandle; |
michael@0 | 51 | class MemoryTextureClient; |
michael@0 | 52 | class MemoryTextureHost; |
michael@0 | 53 | |
michael@0 | 54 | enum BufferCapabilities { |
michael@0 | 55 | DEFAULT_BUFFER_CAPS = 0, |
michael@0 | 56 | /** |
michael@0 | 57 | * The allocated buffer must be efficiently mappable as a |
michael@0 | 58 | * gfxImageSurface. |
michael@0 | 59 | */ |
michael@0 | 60 | MAP_AS_IMAGE_SURFACE = 1 << 0, |
michael@0 | 61 | /** |
michael@0 | 62 | * The allocated buffer will be used for GL rendering only |
michael@0 | 63 | */ |
michael@0 | 64 | USING_GL_RENDERING_ONLY = 1 << 1 |
michael@0 | 65 | }; |
michael@0 | 66 | |
michael@0 | 67 | class SurfaceDescriptor; |
michael@0 | 68 | |
michael@0 | 69 | |
michael@0 | 70 | mozilla::ipc::SharedMemory::SharedMemoryType OptimalShmemType(); |
michael@0 | 71 | bool IsSurfaceDescriptorValid(const SurfaceDescriptor& aSurface); |
michael@0 | 72 | bool IsSurfaceDescriptorOwned(const SurfaceDescriptor& aDescriptor); |
michael@0 | 73 | bool ReleaseOwnedSurfaceDescriptor(const SurfaceDescriptor& aDescriptor); |
michael@0 | 74 | |
michael@0 | 75 | TemporaryRef<gfx::DrawTarget> GetDrawTargetForDescriptor(const SurfaceDescriptor& aDescriptor, gfx::BackendType aBackend); |
michael@0 | 76 | TemporaryRef<gfx::DataSourceSurface> GetSurfaceForDescriptor(const SurfaceDescriptor& aDescriptor); |
michael@0 | 77 | /** |
michael@0 | 78 | * An interface used to create and destroy surfaces that are shared with the |
michael@0 | 79 | * Compositor process (using shmem, or gralloc, or other platform specific memory) |
michael@0 | 80 | * |
michael@0 | 81 | * Most of the methods here correspond to methods that are implemented by IPDL |
michael@0 | 82 | * actors without a common polymorphic interface. |
michael@0 | 83 | * These methods should be only called in the ipdl implementor's thread, unless |
michael@0 | 84 | * specified otherwise in the implementing class. |
michael@0 | 85 | */ |
michael@0 | 86 | class ISurfaceAllocator : public AtomicRefCountedWithFinalize<ISurfaceAllocator> |
michael@0 | 87 | { |
michael@0 | 88 | public: |
michael@0 | 89 | MOZ_DECLARE_REFCOUNTED_TYPENAME(ISurfaceAllocator) |
michael@0 | 90 | ISurfaceAllocator() {} |
michael@0 | 91 | |
michael@0 | 92 | void Finalize(); |
michael@0 | 93 | |
michael@0 | 94 | /** |
michael@0 | 95 | * Returns the type of backend that is used off the main thread. |
michael@0 | 96 | * We only don't allow changing the backend type at runtime so this value can |
michael@0 | 97 | * be queried once and will not change until Gecko is restarted. |
michael@0 | 98 | * |
michael@0 | 99 | * XXX - With e10s this may not be true anymore. we can have accelerated widgets |
michael@0 | 100 | * and non-accelerated widgets (small popups, etc.) |
michael@0 | 101 | */ |
michael@0 | 102 | virtual LayersBackend GetCompositorBackendType() const = 0; |
michael@0 | 103 | |
michael@0 | 104 | /** |
michael@0 | 105 | * Allocate shared memory that can be accessed by only one process at a time. |
michael@0 | 106 | * Ownership of this memory is passed when the memory is sent in an IPDL |
michael@0 | 107 | * message. |
michael@0 | 108 | */ |
michael@0 | 109 | virtual bool AllocShmem(size_t aSize, |
michael@0 | 110 | mozilla::ipc::SharedMemory::SharedMemoryType aType, |
michael@0 | 111 | mozilla::ipc::Shmem* aShmem) = 0; |
michael@0 | 112 | |
michael@0 | 113 | /** |
michael@0 | 114 | * Allocate shared memory that can be accessed by both processes at the |
michael@0 | 115 | * same time. Safety is left for the user of the memory to care about. |
michael@0 | 116 | */ |
michael@0 | 117 | virtual bool AllocUnsafeShmem(size_t aSize, |
michael@0 | 118 | mozilla::ipc::SharedMemory::SharedMemoryType aType, |
michael@0 | 119 | mozilla::ipc::Shmem* aShmem) = 0; |
michael@0 | 120 | |
michael@0 | 121 | /** |
michael@0 | 122 | * Allocate memory in shared memory that can always be accessed by both |
michael@0 | 123 | * processes at a time. Safety is left for the user of the memory to care |
michael@0 | 124 | * about. |
michael@0 | 125 | */ |
michael@0 | 126 | bool AllocShmemSection(size_t aSize, |
michael@0 | 127 | mozilla::layers::ShmemSection* aShmemSection); |
michael@0 | 128 | |
michael@0 | 129 | /** |
michael@0 | 130 | * Deallocates a shmem section. |
michael@0 | 131 | */ |
michael@0 | 132 | void FreeShmemSection(mozilla::layers::ShmemSection& aShmemSection); |
michael@0 | 133 | |
michael@0 | 134 | /** |
michael@0 | 135 | * Deallocate memory allocated by either AllocShmem or AllocUnsafeShmem. |
michael@0 | 136 | */ |
michael@0 | 137 | virtual void DeallocShmem(mozilla::ipc::Shmem& aShmem) = 0; |
michael@0 | 138 | |
michael@0 | 139 | // was AllocBuffer |
michael@0 | 140 | virtual bool AllocSurfaceDescriptor(const gfx::IntSize& aSize, |
michael@0 | 141 | gfxContentType aContent, |
michael@0 | 142 | SurfaceDescriptor* aBuffer); |
michael@0 | 143 | |
michael@0 | 144 | // was AllocBufferWithCaps |
michael@0 | 145 | virtual bool AllocSurfaceDescriptorWithCaps(const gfx::IntSize& aSize, |
michael@0 | 146 | gfxContentType aContent, |
michael@0 | 147 | uint32_t aCaps, |
michael@0 | 148 | SurfaceDescriptor* aBuffer); |
michael@0 | 149 | |
michael@0 | 150 | /** |
michael@0 | 151 | * Returns the maximum texture size supported by the compositor. |
michael@0 | 152 | */ |
michael@0 | 153 | virtual int32_t GetMaxTextureSize() const { return INT32_MAX; } |
michael@0 | 154 | |
michael@0 | 155 | virtual void DestroySharedSurface(SurfaceDescriptor* aSurface); |
michael@0 | 156 | |
michael@0 | 157 | // method that does the actual allocation work |
michael@0 | 158 | virtual PGrallocBufferChild* AllocGrallocBuffer(const gfx::IntSize& aSize, |
michael@0 | 159 | uint32_t aFormat, |
michael@0 | 160 | uint32_t aUsage, |
michael@0 | 161 | MaybeMagicGrallocBufferHandle* aHandle) |
michael@0 | 162 | { |
michael@0 | 163 | return nullptr; |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | virtual void DeallocGrallocBuffer(PGrallocBufferChild* aChild) |
michael@0 | 167 | { |
michael@0 | 168 | NS_RUNTIMEABORT("should not be called"); |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | virtual bool IPCOpen() const { return true; } |
michael@0 | 172 | virtual bool IsSameProcess() const = 0; |
michael@0 | 173 | |
michael@0 | 174 | // Returns true if aSurface wraps a Shmem. |
michael@0 | 175 | static bool IsShmem(SurfaceDescriptor* aSurface); |
michael@0 | 176 | |
michael@0 | 177 | protected: |
michael@0 | 178 | |
michael@0 | 179 | virtual bool IsOnCompositorSide() const = 0; |
michael@0 | 180 | |
michael@0 | 181 | virtual ~ISurfaceAllocator(); |
michael@0 | 182 | |
michael@0 | 183 | void ShrinkShmemSectionHeap(); |
michael@0 | 184 | |
michael@0 | 185 | // This is used to implement an extremely simple & naive heap allocator. |
michael@0 | 186 | std::vector<mozilla::ipc::Shmem> mUsedShmems; |
michael@0 | 187 | |
michael@0 | 188 | friend class AtomicRefCountedWithFinalize<ISurfaceAllocator>; |
michael@0 | 189 | }; |
michael@0 | 190 | |
michael@0 | 191 | class GfxMemoryImageReporter MOZ_FINAL : public nsIMemoryReporter |
michael@0 | 192 | { |
michael@0 | 193 | public: |
michael@0 | 194 | NS_DECL_ISUPPORTS |
michael@0 | 195 | |
michael@0 | 196 | GfxMemoryImageReporter() |
michael@0 | 197 | { |
michael@0 | 198 | #ifdef DEBUG |
michael@0 | 199 | // There must be only one instance of this class, due to |sAmount| |
michael@0 | 200 | // being static. |
michael@0 | 201 | static bool hasRun = false; |
michael@0 | 202 | MOZ_ASSERT(!hasRun); |
michael@0 | 203 | hasRun = true; |
michael@0 | 204 | #endif |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | MOZ_DEFINE_MALLOC_SIZE_OF_ON_ALLOC(MallocSizeOfOnAlloc) |
michael@0 | 208 | MOZ_DEFINE_MALLOC_SIZE_OF_ON_FREE(MallocSizeOfOnFree) |
michael@0 | 209 | |
michael@0 | 210 | static void DidAlloc(void* aPointer) |
michael@0 | 211 | { |
michael@0 | 212 | sAmount += MallocSizeOfOnAlloc(aPointer); |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | static void WillFree(void* aPointer) |
michael@0 | 216 | { |
michael@0 | 217 | sAmount -= MallocSizeOfOnFree(aPointer); |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport, |
michael@0 | 221 | nsISupports* aData) |
michael@0 | 222 | { |
michael@0 | 223 | return MOZ_COLLECT_REPORT( |
michael@0 | 224 | "explicit/gfx/heap-textures", KIND_HEAP, UNITS_BYTES, sAmount, |
michael@0 | 225 | "Heap memory shared between threads by texture clients and hosts."); |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | private: |
michael@0 | 229 | static mozilla::Atomic<size_t> sAmount; |
michael@0 | 230 | }; |
michael@0 | 231 | |
michael@0 | 232 | } // namespace |
michael@0 | 233 | } // namespace |
michael@0 | 234 | |
michael@0 | 235 | #endif |