DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (1aeaa33a64f9)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef MOZILLA_LAYERS_PAINTTHREAD_H
#define MOZILLA_LAYERS_PAINTTHREAD_H

#include "base/platform_thread.h"
#include "mozilla/RefPtr.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/layers/TextureClient.h"
#include "RotatedBuffer.h"
#include "nsThreadUtils.h"

class nsIThreadPool;

namespace mozilla {
namespace gfx {
class DrawTarget;
class DrawTargetCapture;
};  // namespace gfx

namespace layers {

// Holds the key parts from a RotatedBuffer::PaintState
// required to draw the captured paint state
class CapturedPaintState {
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CapturedPaintState)
 public:
  CapturedPaintState(nsIntRegion& aRegionToDraw, gfx::DrawTarget* aTargetDual,
                     gfx::DrawTarget* aTarget, gfx::DrawTarget* aTargetOnWhite,
                     const gfx::Matrix& aTargetTransform,
                     SurfaceMode aSurfaceMode, gfxContentType aContentType)
      : mRegionToDraw(aRegionToDraw),
        mTargetDual(aTargetDual),
        mTarget(aTarget),
        mTargetOnWhite(aTargetOnWhite),
        mTargetTransform(aTargetTransform),
        mSurfaceMode(aSurfaceMode),
        mContentType(aContentType) {}

  template <typename F>
  void ForEachTextureClient(F aClosure) const {
    aClosure(mTextureClient);
    if (mTextureClientOnWhite) {
      aClosure(mTextureClientOnWhite);
    }
  }

  void DropTextureClients() {
    mTextureClient = nullptr;
    mTextureClientOnWhite = nullptr;
  }

  nsIntRegion mRegionToDraw;
  RefPtr<TextureClient> mTextureClient;
  RefPtr<TextureClient> mTextureClientOnWhite;
  RefPtr<gfx::DrawTargetCapture> mCapture;
  RefPtr<gfx::DrawTarget> mTargetDual;
  RefPtr<gfx::DrawTarget> mTarget;
  RefPtr<gfx::DrawTarget> mTargetOnWhite;
  gfx::Matrix mTargetTransform;
  SurfaceMode mSurfaceMode;
  gfxContentType mContentType;

 protected:
  virtual ~CapturedPaintState() {}
};

// Holds the key operations for a ContentClient to prepare
// its buffers for painting
class CapturedBufferState final {
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CapturedBufferState)
 public:
  struct Copy {
    Copy(RefPtr<RotatedBuffer> aSource, RefPtr<RotatedBuffer> aDestination,
         gfx::IntRect aBounds)
        : mSource(aSource), mDestination(aDestination), mBounds(aBounds) {}

    bool CopyBuffer();

    RefPtr<RotatedBuffer> mSource;
    RefPtr<RotatedBuffer> mDestination;
    gfx::IntRect mBounds;
  };

  struct Unrotate {
    Unrotate(RotatedBuffer::Parameters aParameters,
             RefPtr<RotatedBuffer> aBuffer)
        : mParameters(aParameters), mBuffer(aBuffer) {}

    bool UnrotateBuffer();

    RotatedBuffer::Parameters mParameters;
    RefPtr<RotatedBuffer> mBuffer;
  };

  /**
   * Prepares the rotated buffers for painting by copying a previous frame
   * into the buffer and/or unrotating the pixels and returns whether the
   * operations were successful. If this fails a new buffer should be created
   * for the frame.
   */
  bool PrepareBuffer();

  bool HasOperations() const {
    return mBufferFinalize || mBufferUnrotate || mBufferInitialize;
  }

  template <typename F>
  void ForEachTextureClient(F aClosure) const {
    if (mBufferFinalize) {
      if (TextureClient* source = mBufferFinalize->mSource->GetClient()) {
        aClosure(source);
      }
      if (TextureClient* sourceOnWhite =
              mBufferFinalize->mSource->GetClientOnWhite()) {
        aClosure(sourceOnWhite);
      }
      if (TextureClient* destination =
              mBufferFinalize->mDestination->GetClient()) {
        aClosure(destination);
      }
      if (TextureClient* destinationOnWhite =
              mBufferFinalize->mDestination->GetClientOnWhite()) {
        aClosure(destinationOnWhite);
      }
    }

    if (mBufferUnrotate) {
      if (TextureClient* client = mBufferUnrotate->mBuffer->GetClient()) {
        aClosure(client);
      }
      if (TextureClient* clientOnWhite =
              mBufferUnrotate->mBuffer->GetClientOnWhite()) {
        aClosure(clientOnWhite);
      }
    }

    if (mBufferInitialize) {
      if (TextureClient* source = mBufferInitialize->mSource->GetClient()) {
        aClosure(source);
      }
      if (TextureClient* sourceOnWhite =
              mBufferInitialize->mSource->GetClientOnWhite()) {
        aClosure(sourceOnWhite);
      }
      if (TextureClient* destination =
              mBufferInitialize->mDestination->GetClient()) {
        aClosure(destination);
      }
      if (TextureClient* destinationOnWhite =
              mBufferInitialize->mDestination->GetClientOnWhite()) {
        aClosure(destinationOnWhite);
      }
    }
  }

  void DropTextureClients() {
    mBufferFinalize = Nothing();
    mBufferUnrotate = Nothing();
    mBufferInitialize = Nothing();
  }

  Maybe<Copy> mBufferFinalize;
  Maybe<Unrotate> mBufferUnrotate;
  Maybe<Copy> mBufferInitialize;

 protected:
  ~CapturedBufferState() {}
};

typedef bool (*PrepDrawTargetForPaintingCallback)(
    CapturedPaintState* aPaintState);

// Holds the key operations needed to update a tiled content client on the
// paint thread.
class CapturedTiledPaintState {
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CapturedPaintState)
 public:
  struct Copy {
    Copy(RefPtr<gfx::DrawTarget> aSource, RefPtr<gfx::DrawTarget> aDestination,
         gfx::IntRect aSourceBounds, gfx::IntPoint aDestinationPoint)
        : mSource(aSource),
          mDestination(aDestination),
          mSourceBounds(aSourceBounds),
          mDestinationPoint(aDestinationPoint) {}

    bool CopyBuffer();

    RefPtr<gfx::DrawTarget> mSource;
    RefPtr<gfx::DrawTarget> mDestination;
    gfx::IntRect mSourceBounds;
    gfx::IntPoint mDestinationPoint;
  };

  struct Clear {
    Clear(RefPtr<gfx::DrawTarget> aTarget,
          RefPtr<gfx::DrawTarget> aTargetOnWhite, nsIntRegion aDirtyRegion)
        : mTarget(aTarget),
          mTargetOnWhite(aTargetOnWhite),
          mDirtyRegion(aDirtyRegion) {}

    void ClearBuffer();

    RefPtr<gfx::DrawTarget> mTarget;
    RefPtr<gfx::DrawTarget> mTargetOnWhite;
    nsIntRegion mDirtyRegion;
  };

  CapturedTiledPaintState() {}
  CapturedTiledPaintState(gfx::DrawTarget* aTarget,
                          gfx::DrawTargetCapture* aCapture)
      : mTarget(aTarget), mCapture(aCapture) {}

  template <typename F>
  void ForEachTextureClient(F aClosure) const {
    for (auto client : mClients) {
      aClosure(client);
    }
  }

  void DropTextureClients() { mClients.clear(); }

  RefPtr<gfx::DrawTarget> mTarget;
  RefPtr<gfx::DrawTargetCapture> mCapture;
  std::vector<Copy> mCopies;
  std::vector<Clear> mClears;

  std::vector<RefPtr<TextureClient>> mClients;

 protected:
  virtual ~CapturedTiledPaintState() {}
};

class CompositorBridgeChild;

class PaintThread final {
  friend void DestroyPaintThread(UniquePtr<PaintThread>&& aPaintThread);

 public:
  static void Start();
  static void Shutdown();
  static PaintThread* Get();

  // Helper for asserts.
  static bool IsOnPaintThread();
  bool IsOnPaintWorkerThread();

  void PrepareBuffer(CapturedBufferState* aState);

  void PaintContents(CapturedPaintState* aState,
                     PrepDrawTargetForPaintingCallback aCallback);

  void PaintTiledContents(CapturedTiledPaintState* aState);

  // Must be called on the main thread. Signifies that the current
  // batch of CapturedPaintStates* for PaintContents have been recorded
  // and the main thread is finished recording this layer.
  void EndLayer();

  // This allows external users to run code on the paint thread.
  void Dispatch(RefPtr<Runnable>& aRunnable);

  // Must be called on the main thread. Signifies that the current
  // layer tree transaction has been finished and any async paints
  // for it have been queued on the paint thread. This MUST be called
  // at the end of a layer transaction as it will be used to do an optional
  // texture sync and then unblock the main thread if it is waiting to paint
  // a new frame.
  void EndLayerTransaction(SyncObjectClient* aSyncObject);

  // Sync Runnables need threads to be ref counted,
  // But this thread lives through the whole process.
  // We're only temporarily using sync runnables so
  // Override release/addref but don't do anything.
  void Release();
  void AddRef();

  static int32_t CalculatePaintWorkerCount();

 private:
  PaintThread();

  bool Init();
  void ShutdownOnPaintThread();
  void InitOnPaintThread();

  void AsyncPrepareBuffer(CompositorBridgeChild* aBridge,
                          CapturedBufferState* aState);
  void AsyncPaintContents(CompositorBridgeChild* aBridge,
                          CapturedPaintState* aState,
                          PrepDrawTargetForPaintingCallback aCallback);
  void AsyncPaintTiledContents(CompositorBridgeChild* aBridge,
                               CapturedTiledPaintState* aState);
  void AsyncPaintTiledContentsFinished(CompositorBridgeChild* aBridge,
                                       CapturedTiledPaintState* aState);
  void AsyncEndLayer();
  void AsyncEndLayerTransaction(CompositorBridgeChild* aBridge);

  void DispatchEndLayerTransaction(CompositorBridgeChild* aBridge);

  static StaticAutoPtr<PaintThread> sSingleton;
  static StaticRefPtr<nsIThread> sThread;
  static PlatformThreadId sThreadId;

  RefPtr<nsIThreadPool> mPaintWorkers;

  // This shouldn't be very many elements, so a list should be fine.
  // Should only be accessed on the paint thread.
  nsTArray<RefPtr<gfx::DrawTarget>> mDrawTargetsToFlush;
};

}  // namespace layers
}  // namespace mozilla

#endif