gfx/layers/ipc/ISurfaceAllocator.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/layers/ipc/ISurfaceAllocator.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,235 @@
     1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef GFX_LAYERS_ISURFACEDEALLOCATOR
    1.10 +#define GFX_LAYERS_ISURFACEDEALLOCATOR
    1.11 +
    1.12 +#include <stddef.h>                     // for size_t
    1.13 +#include <stdint.h>                     // for uint32_t
    1.14 +#include "gfxTypes.h"
    1.15 +#include "mozilla/gfx/Point.h"          // for IntSize
    1.16 +#include "mozilla/ipc/SharedMemory.h"   // for SharedMemory, etc
    1.17 +#include "mozilla/RefPtr.h"
    1.18 +#include "nsIMemoryReporter.h"          // for nsIMemoryReporter
    1.19 +#include "mozilla/Atomics.h"            // for Atomic
    1.20 +#include "mozilla/layers/LayersMessages.h" // for ShmemSection
    1.21 +#include "LayersTypes.h"
    1.22 +#include <vector>
    1.23 +#include "mozilla/layers/AtomicRefCountedWithFinalize.h"
    1.24 +
    1.25 +/*
    1.26 + * FIXME [bjacob] *** PURE CRAZYNESS WARNING ***
    1.27 + *
    1.28 + * This #define is actually needed here, because subclasses of ISurfaceAllocator,
    1.29 + * namely ShadowLayerForwarder, will or will not override AllocGrallocBuffer
    1.30 + * depending on whether MOZ_HAVE_SURFACEDESCRIPTORGRALLOC is defined.
    1.31 + */
    1.32 +#ifdef MOZ_WIDGET_GONK
    1.33 +#define MOZ_HAVE_SURFACEDESCRIPTORGRALLOC
    1.34 +#endif
    1.35 +
    1.36 +class gfxSharedImageSurface;
    1.37 +
    1.38 +namespace base {
    1.39 +class Thread;
    1.40 +}
    1.41 +
    1.42 +namespace mozilla {
    1.43 +namespace ipc {
    1.44 +class Shmem;
    1.45 +}
    1.46 +namespace gfx {
    1.47 +class DataSourceSurface;
    1.48 +}
    1.49 +
    1.50 +namespace layers {
    1.51 +
    1.52 +class PGrallocBufferChild;
    1.53 +class MaybeMagicGrallocBufferHandle;
    1.54 +class MemoryTextureClient;
    1.55 +class MemoryTextureHost;
    1.56 +
    1.57 +enum BufferCapabilities {
    1.58 +  DEFAULT_BUFFER_CAPS = 0,
    1.59 +  /**
    1.60 +   * The allocated buffer must be efficiently mappable as a
    1.61 +   * gfxImageSurface.
    1.62 +   */
    1.63 +  MAP_AS_IMAGE_SURFACE = 1 << 0,
    1.64 +  /**
    1.65 +   * The allocated buffer will be used for GL rendering only
    1.66 +   */
    1.67 +  USING_GL_RENDERING_ONLY = 1 << 1
    1.68 +};
    1.69 +
    1.70 +class SurfaceDescriptor;
    1.71 +
    1.72 +
    1.73 +mozilla::ipc::SharedMemory::SharedMemoryType OptimalShmemType();
    1.74 +bool IsSurfaceDescriptorValid(const SurfaceDescriptor& aSurface);
    1.75 +bool IsSurfaceDescriptorOwned(const SurfaceDescriptor& aDescriptor);
    1.76 +bool ReleaseOwnedSurfaceDescriptor(const SurfaceDescriptor& aDescriptor);
    1.77 +
    1.78 +TemporaryRef<gfx::DrawTarget> GetDrawTargetForDescriptor(const SurfaceDescriptor& aDescriptor, gfx::BackendType aBackend);
    1.79 +TemporaryRef<gfx::DataSourceSurface> GetSurfaceForDescriptor(const SurfaceDescriptor& aDescriptor);
    1.80 +/**
    1.81 + * An interface used to create and destroy surfaces that are shared with the
    1.82 + * Compositor process (using shmem, or gralloc, or other platform specific memory)
    1.83 + *
    1.84 + * Most of the methods here correspond to methods that are implemented by IPDL
    1.85 + * actors without a common polymorphic interface.
    1.86 + * These methods should be only called in the ipdl implementor's thread, unless
    1.87 + * specified otherwise in the implementing class.
    1.88 + */
    1.89 +class ISurfaceAllocator : public AtomicRefCountedWithFinalize<ISurfaceAllocator>
    1.90 +{
    1.91 +public:
    1.92 +  MOZ_DECLARE_REFCOUNTED_TYPENAME(ISurfaceAllocator)
    1.93 +  ISurfaceAllocator() {}
    1.94 +
    1.95 +  void Finalize();
    1.96 +
    1.97 +  /**
    1.98 +   * Returns the type of backend that is used off the main thread.
    1.99 +   * We only don't allow changing the backend type at runtime so this value can
   1.100 +   * be queried once and will not change until Gecko is restarted.
   1.101 +   *
   1.102 +   * XXX - With e10s this may not be true anymore. we can have accelerated widgets
   1.103 +   * and non-accelerated widgets (small popups, etc.)
   1.104 +   */
   1.105 +  virtual LayersBackend GetCompositorBackendType() const = 0;
   1.106 +
   1.107 +  /**
   1.108 +   * Allocate shared memory that can be accessed by only one process at a time.
   1.109 +   * Ownership of this memory is passed when the memory is sent in an IPDL
   1.110 +   * message.
   1.111 +   */
   1.112 +  virtual bool AllocShmem(size_t aSize,
   1.113 +                          mozilla::ipc::SharedMemory::SharedMemoryType aType,
   1.114 +                          mozilla::ipc::Shmem* aShmem) = 0;
   1.115 +
   1.116 +  /**
   1.117 +   * Allocate shared memory that can be accessed by both processes at the
   1.118 +   * same time. Safety is left for the user of the memory to care about.
   1.119 +   */
   1.120 +  virtual bool AllocUnsafeShmem(size_t aSize,
   1.121 +                                mozilla::ipc::SharedMemory::SharedMemoryType aType,
   1.122 +                                mozilla::ipc::Shmem* aShmem) = 0;
   1.123 +
   1.124 +  /**
   1.125 +   * Allocate memory in shared memory that can always be accessed by both
   1.126 +   * processes at a time. Safety is left for the user of the memory to care
   1.127 +   * about.
   1.128 +   */
   1.129 +  bool AllocShmemSection(size_t aSize,
   1.130 +                         mozilla::layers::ShmemSection* aShmemSection);
   1.131 +
   1.132 +  /**
   1.133 +   * Deallocates a shmem section.
   1.134 +   */
   1.135 +  void FreeShmemSection(mozilla::layers::ShmemSection& aShmemSection);
   1.136 +
   1.137 +  /**
   1.138 +   * Deallocate memory allocated by either AllocShmem or AllocUnsafeShmem.
   1.139 +   */
   1.140 +  virtual void DeallocShmem(mozilla::ipc::Shmem& aShmem) = 0;
   1.141 +
   1.142 +  // was AllocBuffer
   1.143 +  virtual bool AllocSurfaceDescriptor(const gfx::IntSize& aSize,
   1.144 +                                      gfxContentType aContent,
   1.145 +                                      SurfaceDescriptor* aBuffer);
   1.146 +
   1.147 +  // was AllocBufferWithCaps
   1.148 +  virtual bool AllocSurfaceDescriptorWithCaps(const gfx::IntSize& aSize,
   1.149 +                                              gfxContentType aContent,
   1.150 +                                              uint32_t aCaps,
   1.151 +                                              SurfaceDescriptor* aBuffer);
   1.152 +
   1.153 +  /**
   1.154 +   * Returns the maximum texture size supported by the compositor.
   1.155 +   */
   1.156 +  virtual int32_t GetMaxTextureSize() const { return INT32_MAX; }
   1.157 +
   1.158 +  virtual void DestroySharedSurface(SurfaceDescriptor* aSurface);
   1.159 +
   1.160 +  // method that does the actual allocation work
   1.161 +  virtual PGrallocBufferChild* AllocGrallocBuffer(const gfx::IntSize& aSize,
   1.162 +                                                  uint32_t aFormat,
   1.163 +                                                  uint32_t aUsage,
   1.164 +                                                  MaybeMagicGrallocBufferHandle* aHandle)
   1.165 +  {
   1.166 +    return nullptr;
   1.167 +  }
   1.168 +
   1.169 +  virtual void DeallocGrallocBuffer(PGrallocBufferChild* aChild)
   1.170 +  {
   1.171 +    NS_RUNTIMEABORT("should not be called");
   1.172 +  }
   1.173 +
   1.174 +  virtual bool IPCOpen() const { return true; }
   1.175 +  virtual bool IsSameProcess() const = 0;
   1.176 +
   1.177 +  // Returns true if aSurface wraps a Shmem.
   1.178 +  static bool IsShmem(SurfaceDescriptor* aSurface);
   1.179 +
   1.180 +protected:
   1.181 +
   1.182 +  virtual bool IsOnCompositorSide() const = 0;
   1.183 +
   1.184 +  virtual ~ISurfaceAllocator();
   1.185 +
   1.186 +  void ShrinkShmemSectionHeap();
   1.187 +
   1.188 +  // This is used to implement an extremely simple & naive heap allocator.
   1.189 +  std::vector<mozilla::ipc::Shmem> mUsedShmems;
   1.190 +
   1.191 +  friend class AtomicRefCountedWithFinalize<ISurfaceAllocator>;
   1.192 +};
   1.193 +
   1.194 +class GfxMemoryImageReporter MOZ_FINAL : public nsIMemoryReporter
   1.195 +{
   1.196 +public:
   1.197 +  NS_DECL_ISUPPORTS
   1.198 +
   1.199 +  GfxMemoryImageReporter()
   1.200 +  {
   1.201 +#ifdef DEBUG
   1.202 +    // There must be only one instance of this class, due to |sAmount|
   1.203 +    // being static.
   1.204 +    static bool hasRun = false;
   1.205 +    MOZ_ASSERT(!hasRun);
   1.206 +    hasRun = true;
   1.207 +#endif
   1.208 +  }
   1.209 +
   1.210 +  MOZ_DEFINE_MALLOC_SIZE_OF_ON_ALLOC(MallocSizeOfOnAlloc)
   1.211 +  MOZ_DEFINE_MALLOC_SIZE_OF_ON_FREE(MallocSizeOfOnFree)
   1.212 +
   1.213 +  static void DidAlloc(void* aPointer)
   1.214 +  {
   1.215 +    sAmount += MallocSizeOfOnAlloc(aPointer);
   1.216 +  }
   1.217 +
   1.218 +  static void WillFree(void* aPointer)
   1.219 +  {
   1.220 +    sAmount -= MallocSizeOfOnFree(aPointer);
   1.221 +  }
   1.222 +
   1.223 +  NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
   1.224 +                            nsISupports* aData)
   1.225 +  {
   1.226 +    return MOZ_COLLECT_REPORT(
   1.227 +      "explicit/gfx/heap-textures", KIND_HEAP, UNITS_BYTES, sAmount,
   1.228 +      "Heap memory shared between threads by texture clients and hosts.");
   1.229 +  }
   1.230 +
   1.231 +private:
   1.232 +  static mozilla::Atomic<size_t> sAmount;
   1.233 +};
   1.234 +
   1.235 +} // namespace
   1.236 +} // namespace
   1.237 +
   1.238 +#endif

mercurial