tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

OffscreenCanvasDisplayHelper.h (4081B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
      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/. */
      6 
      7 #ifndef MOZILLA_DOM_OFFSCREENCANVASDISPLAYHELPER_H_
      8 #define MOZILLA_DOM_OFFSCREENCANVASDISPLAYHELPER_H_
      9 
     10 #include "GLContextTypes.h"
     11 #include "ImageContainer.h"
     12 #include "mozilla/Maybe.h"
     13 #include "mozilla/Mutex.h"
     14 #include "mozilla/RefPtr.h"
     15 #include "mozilla/UniquePtr.h"
     16 #include "mozilla/dom/CanvasRenderingContextHelper.h"
     17 #include "mozilla/gfx/Point.h"
     18 #include "mozilla/ipc/ProtocolUtils.h"
     19 #include "mozilla/layers/LayersTypes.h"
     20 #include "nsISupportsImpl.h"
     21 #include "nsThreadUtils.h"
     22 
     23 namespace mozilla::dom {
     24 class HTMLCanvasElement;
     25 class OffscreenCanvas;
     26 class ThreadSafeWorkerRef;
     27 
     28 struct OffscreenCanvasDisplayData final {
     29  mozilla::gfx::IntSize mSize = {0, 0};
     30  bool mDoPaintCallbacks = false;
     31  bool mIsOpaque = true;
     32  bool mIsAlphaPremult = true;
     33  mozilla::gl::OriginPos mOriginPos = gl::OriginPos::TopLeft;
     34 };
     35 
     36 class OffscreenCanvasDisplayHelper final {
     37  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(OffscreenCanvasDisplayHelper)
     38 
     39 public:
     40  explicit OffscreenCanvasDisplayHelper(HTMLCanvasElement* aCanvasElement,
     41                                        uint32_t aWidth, uint32_t aHeight);
     42 
     43  CanvasContextType GetContextType() const;
     44 
     45  RefPtr<layers::ImageContainer> GetImageContainer() const;
     46 
     47  void UpdateContext(OffscreenCanvas* aOffscreenCanvas,
     48                     RefPtr<ThreadSafeWorkerRef>&& aWorkerRef,
     49                     CanvasContextType aType,
     50                     const Maybe<mozilla::ipc::ActorId>& aChildId);
     51 
     52  void FlushForDisplay();
     53 
     54  bool CommitFrameToCompositor(nsICanvasRenderingContextInternal* aContext,
     55                               const Maybe<OffscreenCanvasDisplayData>& aData);
     56 
     57  void DestroyCanvas();
     58  void DestroyElement();
     59 
     60  bool IsWriteOnly() const {
     61    MutexAutoLock lock(mMutex);
     62    return mIsWriteOnly;
     63  }
     64 
     65  bool HasWorkerRef() const {
     66    MutexAutoLock lock(mMutex);
     67    return !!mWorkerRef;
     68  }
     69 
     70  void SetWriteOnly(nsIPrincipal* aExpandedReader = nullptr);
     71  bool CallerCanRead(nsIPrincipal& aPrincipal) const;
     72 
     73  bool CanElementCaptureStream() const;
     74  bool UsingElementCaptureStream() const;
     75 
     76  already_AddRefed<mozilla::gfx::SourceSurface> GetSurfaceSnapshot();
     77  already_AddRefed<mozilla::layers::Image> GetAsImage();
     78  UniquePtr<uint8_t[]> GetImageBuffer(
     79      CanvasUtils::ImageExtraction aExtractionBehavior, int32_t* aOutFormat,
     80      gfx::IntSize* aOutImageSize);
     81 
     82 private:
     83  ~OffscreenCanvasDisplayHelper();
     84  void MaybeQueueInvalidateElement() MOZ_REQUIRES(mMutex);
     85  void InvalidateElement();
     86 
     87  already_AddRefed<gfx::SourceSurface> TransformSurface(
     88      gfx::SourceSurface* aSurface, bool aHasAlpha, bool aIsAlphaPremult,
     89      gl::OriginPos aOriginPos) const;
     90 
     91  mutable Mutex mMutex;
     92  HTMLCanvasElement* MOZ_NON_OWNING_REF mCanvasElement MOZ_GUARDED_BY(mMutex);
     93  OffscreenCanvas* MOZ_NON_OWNING_REF mOffscreenCanvas MOZ_GUARDED_BY(mMutex) =
     94      nullptr;
     95  RefPtr<layers::ImageContainer> mImageContainer MOZ_GUARDED_BY(mMutex);
     96  RefPtr<gfx::SourceSurface> mFrontBufferSurface MOZ_GUARDED_BY(mMutex);
     97  RefPtr<ThreadSafeWorkerRef> mWorkerRef MOZ_GUARDED_BY(mMutex);
     98 
     99  OffscreenCanvasDisplayData mData MOZ_GUARDED_BY(mMutex);
    100  CanvasContextType mType MOZ_GUARDED_BY(mMutex) = CanvasContextType::NoContext;
    101  Maybe<uint32_t> mContextManagerId MOZ_GUARDED_BY(mMutex);
    102  Maybe<mozilla::ipc::ActorId> mContextChildId MOZ_GUARDED_BY(mMutex);
    103  const mozilla::layers::ImageContainer::ProducerID mImageProducerID;
    104  mozilla::layers::ImageContainer::FrameID mLastFrameID MOZ_GUARDED_BY(mMutex) =
    105      0;
    106  bool mPendingInvalidate MOZ_GUARDED_BY(mMutex) = false;
    107  bool mIsWriteOnly MOZ_GUARDED_BY(mMutex) = false;
    108  RefPtr<nsIPrincipal> mExpandedReader MOZ_GUARDED_BY(mMutex);
    109 };
    110 
    111 }  // namespace mozilla::dom
    112 
    113 #endif  // MOZILLA_DOM_OFFSCREENCANVASDISPLAYHELPER_H_