gfx/layers/ipc/CompositorParent.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

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.

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim: set sw=4 ts=8 et tw=80 : */
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #ifndef mozilla_layers_CompositorParent_h
     8 #define mozilla_layers_CompositorParent_h
    10 // Enable this pref to turn on compositor performance warning.
    11 // This will print warnings if the compositor isn't meeting
    12 // its responsiveness objectives:
    13 //    1) Compose a frame within 15ms of receiving a ScheduleCompositeCall
    14 //    2) Unless a frame was composited within the throttle threshold in
    15 //       which the deadline will be 15ms + throttle threshold
    16 //#define COMPOSITOR_PERFORMANCE_WARNING
    18 #include <stdint.h>                     // for uint64_t
    19 #include "Layers.h"                     // for Layer
    20 #include "ShadowLayersManager.h"        // for ShadowLayersManager
    21 #include "base/basictypes.h"            // for DISALLOW_EVIL_CONSTRUCTORS
    22 #include "base/platform_thread.h"       // for PlatformThreadId
    23 #include "mozilla/Assertions.h"         // for MOZ_ASSERT_HELPER2
    24 #include "mozilla/Attributes.h"         // for MOZ_OVERRIDE
    25 #include "mozilla/Monitor.h"            // for Monitor
    26 #include "mozilla/RefPtr.h"             // for RefPtr
    27 #include "mozilla/TimeStamp.h"          // for TimeStamp
    28 #include "mozilla/ipc/ProtocolUtils.h"
    29 #include "mozilla/layers/GeckoContentController.h"
    30 #include "mozilla/layers/LayersMessages.h"  // for TargetConfig
    31 #include "mozilla/layers/PCompositorParent.h"
    32 #include "nsAutoPtr.h"                  // for nsRefPtr
    33 #include "nsISupportsImpl.h"
    34 #include "nsSize.h"                     // for nsIntSize
    36 class CancelableTask;
    37 class MessageLoop;
    38 class gfxContext;
    39 class nsIWidget;
    41 namespace mozilla {
    42 namespace gfx {
    43 class DrawTarget;
    44 }
    46 namespace layers {
    48 class APZCTreeManager;
    49 class AsyncCompositionManager;
    50 class Compositor;
    51 class LayerManagerComposite;
    52 class LayerTransactionParent;
    54 struct ScopedLayerTreeRegistration
    55 {
    56   ScopedLayerTreeRegistration(uint64_t aLayersId,
    57                               Layer* aRoot,
    58                               GeckoContentController* aController);
    59   ~ScopedLayerTreeRegistration();
    61 private:
    62   uint64_t mLayersId;
    63 };
    65 class CompositorParent MOZ_FINAL : public PCompositorParent,
    66                                    public ShadowLayersManager
    67 {
    68   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CompositorParent)
    70 public:
    71   CompositorParent(nsIWidget* aWidget,
    72                    bool aUseExternalSurfaceSize = false,
    73                    int aSurfaceWidth = -1, int aSurfaceHeight = -1);
    75   // IToplevelProtocol::CloneToplevel()
    76   virtual IToplevelProtocol*
    77   CloneToplevel(const InfallibleTArray<mozilla::ipc::ProtocolFdMapping>& aFds,
    78                 base::ProcessHandle aPeerProcess,
    79                 mozilla::ipc::ProtocolCloneContext* aCtx) MOZ_OVERRIDE;
    81   virtual bool RecvWillStop() MOZ_OVERRIDE;
    82   virtual bool RecvStop() MOZ_OVERRIDE;
    83   virtual bool RecvPause() MOZ_OVERRIDE;
    84   virtual bool RecvResume() MOZ_OVERRIDE;
    85   virtual bool RecvNotifyChildCreated(const uint64_t& child) MOZ_OVERRIDE;
    86   virtual bool RecvMakeSnapshot(const SurfaceDescriptor& aInSnapshot,
    87                                 SurfaceDescriptor* aOutSnapshot) MOZ_OVERRIDE;
    88   virtual bool RecvFlushRendering() MOZ_OVERRIDE;
    90   virtual bool RecvNotifyRegionInvalidated(const nsIntRegion& aRegion) MOZ_OVERRIDE;
    91   virtual bool RecvStartFrameTimeRecording(const int32_t& aBufferSize, uint32_t* aOutStartIndex) MOZ_OVERRIDE;
    92   virtual bool RecvStopFrameTimeRecording(const uint32_t& aStartIndex, InfallibleTArray<float>* intervals) MOZ_OVERRIDE;
    94   virtual void ActorDestroy(ActorDestroyReason why) MOZ_OVERRIDE;
    96   virtual void ShadowLayersUpdated(LayerTransactionParent* aLayerTree,
    97                                    const TargetConfig& aTargetConfig,
    98                                    bool aIsFirstPaint,
    99                                    bool aScheduleComposite) MOZ_OVERRIDE;
   100   virtual void ForceComposite(LayerTransactionParent* aLayerTree) MOZ_OVERRIDE;
   101   virtual bool SetTestSampleTime(LayerTransactionParent* aLayerTree,
   102                                  const TimeStamp& aTime) MOZ_OVERRIDE;
   103   virtual void LeaveTestMode(LayerTransactionParent* aLayerTree) MOZ_OVERRIDE;
   104   virtual AsyncCompositionManager* GetCompositionManager(LayerTransactionParent* aLayerTree) MOZ_OVERRIDE { return mCompositionManager; }
   106   /**
   107    * This forces the is-first-paint flag to true. This is intended to
   108    * be called by the widget code when it loses its viewport information
   109    * (or for whatever reason wants to refresh the viewport information).
   110    * The information refresh happens because the compositor will call
   111    * SetFirstPaintViewport on the next frame of composition.
   112    */
   113   void ForceIsFirstPaint();
   114   void Destroy();
   116   void NotifyChildCreated(uint64_t aChild);
   118   void AsyncRender();
   120   // Can be called from any thread
   121   void ScheduleRenderOnCompositorThread();
   122   void SchedulePauseOnCompositorThread();
   123   /**
   124    * Returns true if a surface was obtained and the resume succeeded; false
   125    * otherwise.
   126    */
   127   bool ScheduleResumeOnCompositorThread(int width, int height);
   129   virtual void ScheduleComposition();
   130   void NotifyShadowTreeTransaction(uint64_t aId, bool aIsFirstPaint, bool aScheduleComposite);
   132   /**
   133    * Returns the unique layer tree identifier that corresponds to the root
   134    * tree of this compositor.
   135    */
   136   uint64_t RootLayerTreeId();
   138   /**
   139    * Returns a pointer to the compositor corresponding to the given ID.
   140    */
   141   static CompositorParent* GetCompositor(uint64_t id);
   143   /**
   144    * Returns the compositor thread's message loop.
   145    *
   146    * This message loop is used by CompositorParent and ImageBridgeParent.
   147    */
   148   static MessageLoop* CompositorLoop();
   150   /**
   151    * Creates the compositor thread and the global compositor map.
   152    */
   153   static void StartUp();
   155   /**
   156    * Destroys the compositor thread and the global compositor map.
   157    */
   158   static void ShutDown();
   160   /**
   161    * Allocate an ID that can be used to refer to a layer tree and
   162    * associated resources that live only on the compositor thread.
   163    *
   164    * Must run on the content main thread.
   165    */
   166   static uint64_t AllocateLayerTreeId();
   167   /**
   168    * Release compositor-thread resources referred to by |aID|.
   169    *
   170    * Must run on the content main thread.
   171    */
   172   static void DeallocateLayerTreeId(uint64_t aId);
   174   /**
   175    * Set aController as the pan/zoom callback for the subtree referred
   176    * to by aLayersId.
   177    *
   178    * Must run on content main thread.
   179    */
   180   static void SetControllerForLayerTree(uint64_t aLayersId,
   181                                         GeckoContentController* aController);
   183   /**
   184    * This returns a reference to the APZCTreeManager to which
   185    * pan/zoom-related events can be sent.
   186    */
   187   static APZCTreeManager* GetAPZCTreeManager(uint64_t aLayersId);
   189   /**
   190    * A new child process has been configured to push transactions
   191    * directly to us.  Transport is to its thread context.
   192    */
   193   static PCompositorParent*
   194   Create(Transport* aTransport, ProcessId aOtherProcess);
   196   /**
   197    * Setup external message loop and thread ID for Compositor.
   198    * Should be used when CompositorParent should work in existing thread/MessageLoop,
   199    * for example moving Compositor into native toolkit main thread will allow to avoid
   200    * extra synchronization and call ::Composite() right from toolkit::Paint event
   201    */
   202   static void StartUpWithExistingThread(MessageLoop* aMsgLoop,
   203                                         PlatformThreadId aThreadID);
   205   struct LayerTreeState {
   206     LayerTreeState();
   207     nsRefPtr<Layer> mRoot;
   208     nsRefPtr<GeckoContentController> mController;
   209     CompositorParent* mParent;
   210     LayerManagerComposite* mLayerManager;
   211     // Pointer to the CrossProcessCompositorParent. Used by APZCs to share
   212     // their FrameMetrics with the corresponding child process that holds
   213     // the PCompositorChild
   214     PCompositorParent* mCrossProcessParent;
   215     TargetConfig mTargetConfig;
   216   };
   218   /**
   219    * Lookup the indirect shadow tree for |aId| and return it if it
   220    * exists.  Otherwise null is returned.  This must only be called on
   221    * the compositor thread.
   222    */
   223   static const LayerTreeState* GetIndirectShadowTree(uint64_t aId);
   225   float ComputeRenderIntegrity();
   227   /**
   228    * Returns true if the calling thread is the compositor thread.
   229    */
   230   static bool IsInCompositorThread();
   232 private:
   233   // Private destructor, to discourage deletion outside of Release():
   234   virtual ~CompositorParent();
   236   virtual PLayerTransactionParent*
   237     AllocPLayerTransactionParent(const nsTArray<LayersBackend>& aBackendHints,
   238                                  const uint64_t& aId,
   239                                  TextureFactoryIdentifier* aTextureFactoryIdentifier,
   240                                  bool* aSuccess) MOZ_OVERRIDE;
   241   virtual bool DeallocPLayerTransactionParent(PLayerTransactionParent* aLayers) MOZ_OVERRIDE;
   242   virtual void ScheduleTask(CancelableTask*, int);
   243   void Composite();
   244   void CompositeToTarget(gfx::DrawTarget* aTarget);
   245   void ForceComposeToTarget(gfx::DrawTarget* aTarget);
   247   void SetEGLSurfaceSize(int width, int height);
   249   void InitializeLayerManager(const nsTArray<LayersBackend>& aBackendHints);
   250   void PauseComposition();
   251   void ResumeComposition();
   252   void ResumeCompositionAndResize(int width, int height);
   253   void ForceComposition();
   254   void CancelCurrentCompositeTask();
   256   inline static PlatformThreadId CompositorThreadID();
   258   /**
   259    * Creates a global map referencing each compositor by ID.
   260    *
   261    * This map is used by the ImageBridge protocol to trigger
   262    * compositions without having to keep references to the
   263    * compositor
   264    */
   265   static void CreateCompositorMap();
   266   static void DestroyCompositorMap();
   268   /**
   269    * Creates the compositor thread.
   270    *
   271    * All compositors live on the same thread.
   272    * The thread is not lazily created on first access to avoid dealing with
   273    * thread safety. Therefore it's best to create and destroy the thread when
   274    * we know we areb't using it (So creating/destroying along with gfxPlatform
   275    * looks like a good place).
   276    */
   277   static bool CreateThread();
   279   /**
   280    * Destroys the compositor thread.
   281    *
   282    * It is safe to call this fucntion more than once, although the second call
   283    * will have no effect.
   284    * This function is not thread-safe.
   285    */
   286   static void DestroyThread();
   288   /**
   289    * Add a compositor to the global compositor map.
   290    */
   291   static void AddCompositor(CompositorParent* compositor, uint64_t* id);
   292   /**
   293    * Remove a compositor from the global compositor map.
   294    */
   295   static CompositorParent* RemoveCompositor(uint64_t id);
   297    /**
   298    * Return true if current state allows compositing, that is
   299    * finishing a layers transaction.
   300    */
   301   bool CanComposite();
   303   void DidComposite();
   305   nsRefPtr<LayerManagerComposite> mLayerManager;
   306   nsRefPtr<Compositor> mCompositor;
   307   RefPtr<AsyncCompositionManager> mCompositionManager;
   308   nsIWidget* mWidget;
   309   CancelableTask *mCurrentCompositeTask;
   310   TimeStamp mLastCompose;
   311   TimeStamp mTestTime;
   312   bool mIsTesting;
   313 #ifdef COMPOSITOR_PERFORMANCE_WARNING
   314   TimeStamp mExpectedComposeStartTime;
   315 #endif
   317   bool mPaused;
   319   bool mUseExternalSurfaceSize;
   320   nsIntSize mEGLSurfaceSize;
   322   mozilla::Monitor mPauseCompositionMonitor;
   323   mozilla::Monitor mResumeCompositionMonitor;
   325   uint64_t mCompositorID;
   326   uint64_t mRootLayerTreeID;
   328   bool mOverrideComposeReadiness;
   329   CancelableTask* mForceCompositionTask;
   331   nsRefPtr<APZCTreeManager> mApzcTreeManager;
   333   bool mWantDidCompositeEvent;
   335   DISALLOW_EVIL_CONSTRUCTORS(CompositorParent);
   336 };
   338 } // layers
   339 } // mozilla
   341 #endif // mozilla_layers_CompositorParent_h

mercurial