gfx/layers/ipc/CompositorParent.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/layers/ipc/CompositorParent.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,341 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set sw=4 ts=8 et tw=80 : */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef mozilla_layers_CompositorParent_h
    1.11 +#define mozilla_layers_CompositorParent_h
    1.12 +
    1.13 +// Enable this pref to turn on compositor performance warning.
    1.14 +// This will print warnings if the compositor isn't meeting
    1.15 +// its responsiveness objectives:
    1.16 +//    1) Compose a frame within 15ms of receiving a ScheduleCompositeCall
    1.17 +//    2) Unless a frame was composited within the throttle threshold in
    1.18 +//       which the deadline will be 15ms + throttle threshold
    1.19 +//#define COMPOSITOR_PERFORMANCE_WARNING
    1.20 +
    1.21 +#include <stdint.h>                     // for uint64_t
    1.22 +#include "Layers.h"                     // for Layer
    1.23 +#include "ShadowLayersManager.h"        // for ShadowLayersManager
    1.24 +#include "base/basictypes.h"            // for DISALLOW_EVIL_CONSTRUCTORS
    1.25 +#include "base/platform_thread.h"       // for PlatformThreadId
    1.26 +#include "mozilla/Assertions.h"         // for MOZ_ASSERT_HELPER2
    1.27 +#include "mozilla/Attributes.h"         // for MOZ_OVERRIDE
    1.28 +#include "mozilla/Monitor.h"            // for Monitor
    1.29 +#include "mozilla/RefPtr.h"             // for RefPtr
    1.30 +#include "mozilla/TimeStamp.h"          // for TimeStamp
    1.31 +#include "mozilla/ipc/ProtocolUtils.h"
    1.32 +#include "mozilla/layers/GeckoContentController.h"
    1.33 +#include "mozilla/layers/LayersMessages.h"  // for TargetConfig
    1.34 +#include "mozilla/layers/PCompositorParent.h"
    1.35 +#include "nsAutoPtr.h"                  // for nsRefPtr
    1.36 +#include "nsISupportsImpl.h"
    1.37 +#include "nsSize.h"                     // for nsIntSize
    1.38 +
    1.39 +class CancelableTask;
    1.40 +class MessageLoop;
    1.41 +class gfxContext;
    1.42 +class nsIWidget;
    1.43 +
    1.44 +namespace mozilla {
    1.45 +namespace gfx {
    1.46 +class DrawTarget;
    1.47 +}
    1.48 +
    1.49 +namespace layers {
    1.50 +
    1.51 +class APZCTreeManager;
    1.52 +class AsyncCompositionManager;
    1.53 +class Compositor;
    1.54 +class LayerManagerComposite;
    1.55 +class LayerTransactionParent;
    1.56 +
    1.57 +struct ScopedLayerTreeRegistration
    1.58 +{
    1.59 +  ScopedLayerTreeRegistration(uint64_t aLayersId,
    1.60 +                              Layer* aRoot,
    1.61 +                              GeckoContentController* aController);
    1.62 +  ~ScopedLayerTreeRegistration();
    1.63 +
    1.64 +private:
    1.65 +  uint64_t mLayersId;
    1.66 +};
    1.67 +
    1.68 +class CompositorParent MOZ_FINAL : public PCompositorParent,
    1.69 +                                   public ShadowLayersManager
    1.70 +{
    1.71 +  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CompositorParent)
    1.72 +
    1.73 +public:
    1.74 +  CompositorParent(nsIWidget* aWidget,
    1.75 +                   bool aUseExternalSurfaceSize = false,
    1.76 +                   int aSurfaceWidth = -1, int aSurfaceHeight = -1);
    1.77 +
    1.78 +  // IToplevelProtocol::CloneToplevel()
    1.79 +  virtual IToplevelProtocol*
    1.80 +  CloneToplevel(const InfallibleTArray<mozilla::ipc::ProtocolFdMapping>& aFds,
    1.81 +                base::ProcessHandle aPeerProcess,
    1.82 +                mozilla::ipc::ProtocolCloneContext* aCtx) MOZ_OVERRIDE;
    1.83 +
    1.84 +  virtual bool RecvWillStop() MOZ_OVERRIDE;
    1.85 +  virtual bool RecvStop() MOZ_OVERRIDE;
    1.86 +  virtual bool RecvPause() MOZ_OVERRIDE;
    1.87 +  virtual bool RecvResume() MOZ_OVERRIDE;
    1.88 +  virtual bool RecvNotifyChildCreated(const uint64_t& child) MOZ_OVERRIDE;
    1.89 +  virtual bool RecvMakeSnapshot(const SurfaceDescriptor& aInSnapshot,
    1.90 +                                SurfaceDescriptor* aOutSnapshot) MOZ_OVERRIDE;
    1.91 +  virtual bool RecvFlushRendering() MOZ_OVERRIDE;
    1.92 +
    1.93 +  virtual bool RecvNotifyRegionInvalidated(const nsIntRegion& aRegion) MOZ_OVERRIDE;
    1.94 +  virtual bool RecvStartFrameTimeRecording(const int32_t& aBufferSize, uint32_t* aOutStartIndex) MOZ_OVERRIDE;
    1.95 +  virtual bool RecvStopFrameTimeRecording(const uint32_t& aStartIndex, InfallibleTArray<float>* intervals) MOZ_OVERRIDE;
    1.96 +
    1.97 +  virtual void ActorDestroy(ActorDestroyReason why) MOZ_OVERRIDE;
    1.98 +
    1.99 +  virtual void ShadowLayersUpdated(LayerTransactionParent* aLayerTree,
   1.100 +                                   const TargetConfig& aTargetConfig,
   1.101 +                                   bool aIsFirstPaint,
   1.102 +                                   bool aScheduleComposite) MOZ_OVERRIDE;
   1.103 +  virtual void ForceComposite(LayerTransactionParent* aLayerTree) MOZ_OVERRIDE;
   1.104 +  virtual bool SetTestSampleTime(LayerTransactionParent* aLayerTree,
   1.105 +                                 const TimeStamp& aTime) MOZ_OVERRIDE;
   1.106 +  virtual void LeaveTestMode(LayerTransactionParent* aLayerTree) MOZ_OVERRIDE;
   1.107 +  virtual AsyncCompositionManager* GetCompositionManager(LayerTransactionParent* aLayerTree) MOZ_OVERRIDE { return mCompositionManager; }
   1.108 +
   1.109 +  /**
   1.110 +   * This forces the is-first-paint flag to true. This is intended to
   1.111 +   * be called by the widget code when it loses its viewport information
   1.112 +   * (or for whatever reason wants to refresh the viewport information).
   1.113 +   * The information refresh happens because the compositor will call
   1.114 +   * SetFirstPaintViewport on the next frame of composition.
   1.115 +   */
   1.116 +  void ForceIsFirstPaint();
   1.117 +  void Destroy();
   1.118 +
   1.119 +  void NotifyChildCreated(uint64_t aChild);
   1.120 +
   1.121 +  void AsyncRender();
   1.122 +
   1.123 +  // Can be called from any thread
   1.124 +  void ScheduleRenderOnCompositorThread();
   1.125 +  void SchedulePauseOnCompositorThread();
   1.126 +  /**
   1.127 +   * Returns true if a surface was obtained and the resume succeeded; false
   1.128 +   * otherwise.
   1.129 +   */
   1.130 +  bool ScheduleResumeOnCompositorThread(int width, int height);
   1.131 +
   1.132 +  virtual void ScheduleComposition();
   1.133 +  void NotifyShadowTreeTransaction(uint64_t aId, bool aIsFirstPaint, bool aScheduleComposite);
   1.134 +
   1.135 +  /**
   1.136 +   * Returns the unique layer tree identifier that corresponds to the root
   1.137 +   * tree of this compositor.
   1.138 +   */
   1.139 +  uint64_t RootLayerTreeId();
   1.140 +
   1.141 +  /**
   1.142 +   * Returns a pointer to the compositor corresponding to the given ID.
   1.143 +   */
   1.144 +  static CompositorParent* GetCompositor(uint64_t id);
   1.145 +
   1.146 +  /**
   1.147 +   * Returns the compositor thread's message loop.
   1.148 +   *
   1.149 +   * This message loop is used by CompositorParent and ImageBridgeParent.
   1.150 +   */
   1.151 +  static MessageLoop* CompositorLoop();
   1.152 +
   1.153 +  /**
   1.154 +   * Creates the compositor thread and the global compositor map.
   1.155 +   */
   1.156 +  static void StartUp();
   1.157 +
   1.158 +  /**
   1.159 +   * Destroys the compositor thread and the global compositor map.
   1.160 +   */
   1.161 +  static void ShutDown();
   1.162 +
   1.163 +  /**
   1.164 +   * Allocate an ID that can be used to refer to a layer tree and
   1.165 +   * associated resources that live only on the compositor thread.
   1.166 +   *
   1.167 +   * Must run on the content main thread.
   1.168 +   */
   1.169 +  static uint64_t AllocateLayerTreeId();
   1.170 +  /**
   1.171 +   * Release compositor-thread resources referred to by |aID|.
   1.172 +   *
   1.173 +   * Must run on the content main thread.
   1.174 +   */
   1.175 +  static void DeallocateLayerTreeId(uint64_t aId);
   1.176 +
   1.177 +  /**
   1.178 +   * Set aController as the pan/zoom callback for the subtree referred
   1.179 +   * to by aLayersId.
   1.180 +   *
   1.181 +   * Must run on content main thread.
   1.182 +   */
   1.183 +  static void SetControllerForLayerTree(uint64_t aLayersId,
   1.184 +                                        GeckoContentController* aController);
   1.185 +
   1.186 +  /**
   1.187 +   * This returns a reference to the APZCTreeManager to which
   1.188 +   * pan/zoom-related events can be sent.
   1.189 +   */
   1.190 +  static APZCTreeManager* GetAPZCTreeManager(uint64_t aLayersId);
   1.191 +
   1.192 +  /**
   1.193 +   * A new child process has been configured to push transactions
   1.194 +   * directly to us.  Transport is to its thread context.
   1.195 +   */
   1.196 +  static PCompositorParent*
   1.197 +  Create(Transport* aTransport, ProcessId aOtherProcess);
   1.198 +
   1.199 +  /**
   1.200 +   * Setup external message loop and thread ID for Compositor.
   1.201 +   * Should be used when CompositorParent should work in existing thread/MessageLoop,
   1.202 +   * for example moving Compositor into native toolkit main thread will allow to avoid
   1.203 +   * extra synchronization and call ::Composite() right from toolkit::Paint event
   1.204 +   */
   1.205 +  static void StartUpWithExistingThread(MessageLoop* aMsgLoop,
   1.206 +                                        PlatformThreadId aThreadID);
   1.207 +
   1.208 +  struct LayerTreeState {
   1.209 +    LayerTreeState();
   1.210 +    nsRefPtr<Layer> mRoot;
   1.211 +    nsRefPtr<GeckoContentController> mController;
   1.212 +    CompositorParent* mParent;
   1.213 +    LayerManagerComposite* mLayerManager;
   1.214 +    // Pointer to the CrossProcessCompositorParent. Used by APZCs to share
   1.215 +    // their FrameMetrics with the corresponding child process that holds
   1.216 +    // the PCompositorChild
   1.217 +    PCompositorParent* mCrossProcessParent;
   1.218 +    TargetConfig mTargetConfig;
   1.219 +  };
   1.220 +
   1.221 +  /**
   1.222 +   * Lookup the indirect shadow tree for |aId| and return it if it
   1.223 +   * exists.  Otherwise null is returned.  This must only be called on
   1.224 +   * the compositor thread.
   1.225 +   */
   1.226 +  static const LayerTreeState* GetIndirectShadowTree(uint64_t aId);
   1.227 +
   1.228 +  float ComputeRenderIntegrity();
   1.229 +
   1.230 +  /**
   1.231 +   * Returns true if the calling thread is the compositor thread.
   1.232 +   */
   1.233 +  static bool IsInCompositorThread();
   1.234 +
   1.235 +private:
   1.236 +  // Private destructor, to discourage deletion outside of Release():
   1.237 +  virtual ~CompositorParent();
   1.238 +
   1.239 +  virtual PLayerTransactionParent*
   1.240 +    AllocPLayerTransactionParent(const nsTArray<LayersBackend>& aBackendHints,
   1.241 +                                 const uint64_t& aId,
   1.242 +                                 TextureFactoryIdentifier* aTextureFactoryIdentifier,
   1.243 +                                 bool* aSuccess) MOZ_OVERRIDE;
   1.244 +  virtual bool DeallocPLayerTransactionParent(PLayerTransactionParent* aLayers) MOZ_OVERRIDE;
   1.245 +  virtual void ScheduleTask(CancelableTask*, int);
   1.246 +  void Composite();
   1.247 +  void CompositeToTarget(gfx::DrawTarget* aTarget);
   1.248 +  void ForceComposeToTarget(gfx::DrawTarget* aTarget);
   1.249 +
   1.250 +  void SetEGLSurfaceSize(int width, int height);
   1.251 +
   1.252 +  void InitializeLayerManager(const nsTArray<LayersBackend>& aBackendHints);
   1.253 +  void PauseComposition();
   1.254 +  void ResumeComposition();
   1.255 +  void ResumeCompositionAndResize(int width, int height);
   1.256 +  void ForceComposition();
   1.257 +  void CancelCurrentCompositeTask();
   1.258 +
   1.259 +  inline static PlatformThreadId CompositorThreadID();
   1.260 +
   1.261 +  /**
   1.262 +   * Creates a global map referencing each compositor by ID.
   1.263 +   *
   1.264 +   * This map is used by the ImageBridge protocol to trigger
   1.265 +   * compositions without having to keep references to the
   1.266 +   * compositor
   1.267 +   */
   1.268 +  static void CreateCompositorMap();
   1.269 +  static void DestroyCompositorMap();
   1.270 +
   1.271 +  /**
   1.272 +   * Creates the compositor thread.
   1.273 +   *
   1.274 +   * All compositors live on the same thread.
   1.275 +   * The thread is not lazily created on first access to avoid dealing with
   1.276 +   * thread safety. Therefore it's best to create and destroy the thread when
   1.277 +   * we know we areb't using it (So creating/destroying along with gfxPlatform
   1.278 +   * looks like a good place).
   1.279 +   */
   1.280 +  static bool CreateThread();
   1.281 +
   1.282 +  /**
   1.283 +   * Destroys the compositor thread.
   1.284 +   *
   1.285 +   * It is safe to call this fucntion more than once, although the second call
   1.286 +   * will have no effect.
   1.287 +   * This function is not thread-safe.
   1.288 +   */
   1.289 +  static void DestroyThread();
   1.290 +
   1.291 +  /**
   1.292 +   * Add a compositor to the global compositor map.
   1.293 +   */
   1.294 +  static void AddCompositor(CompositorParent* compositor, uint64_t* id);
   1.295 +  /**
   1.296 +   * Remove a compositor from the global compositor map.
   1.297 +   */
   1.298 +  static CompositorParent* RemoveCompositor(uint64_t id);
   1.299 +
   1.300 +   /**
   1.301 +   * Return true if current state allows compositing, that is
   1.302 +   * finishing a layers transaction.
   1.303 +   */
   1.304 +  bool CanComposite();
   1.305 +
   1.306 +  void DidComposite();
   1.307 +
   1.308 +  nsRefPtr<LayerManagerComposite> mLayerManager;
   1.309 +  nsRefPtr<Compositor> mCompositor;
   1.310 +  RefPtr<AsyncCompositionManager> mCompositionManager;
   1.311 +  nsIWidget* mWidget;
   1.312 +  CancelableTask *mCurrentCompositeTask;
   1.313 +  TimeStamp mLastCompose;
   1.314 +  TimeStamp mTestTime;
   1.315 +  bool mIsTesting;
   1.316 +#ifdef COMPOSITOR_PERFORMANCE_WARNING
   1.317 +  TimeStamp mExpectedComposeStartTime;
   1.318 +#endif
   1.319 +
   1.320 +  bool mPaused;
   1.321 +
   1.322 +  bool mUseExternalSurfaceSize;
   1.323 +  nsIntSize mEGLSurfaceSize;
   1.324 +
   1.325 +  mozilla::Monitor mPauseCompositionMonitor;
   1.326 +  mozilla::Monitor mResumeCompositionMonitor;
   1.327 +
   1.328 +  uint64_t mCompositorID;
   1.329 +  uint64_t mRootLayerTreeID;
   1.330 +
   1.331 +  bool mOverrideComposeReadiness;
   1.332 +  CancelableTask* mForceCompositionTask;
   1.333 +
   1.334 +  nsRefPtr<APZCTreeManager> mApzcTreeManager;
   1.335 +
   1.336 +  bool mWantDidCompositeEvent;
   1.337 +
   1.338 +  DISALLOW_EVIL_CONSTRUCTORS(CompositorParent);
   1.339 +};
   1.340 +
   1.341 +} // layers
   1.342 +} // mozilla
   1.343 +
   1.344 +#endif // mozilla_layers_CompositorParent_h

mercurial