tor-browser

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

BlobSurfaceProvider.h (4767B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 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/. */
      6 
      7 #ifndef MOZILLA_IMAGE_BLOBSURFACEPROVIDER_H_
      8 #define MOZILLA_IMAGE_BLOBSURFACEPROVIDER_H_
      9 
     10 #include "mozilla/Maybe.h"
     11 #include "mozilla/SVGImageContext.h"
     12 #include "mozilla/gfx/2D.h"
     13 #include "mozilla/gfx/DrawEventRecorderTypes.h"
     14 #include "mozilla/layers/WebRenderLayerManager.h"
     15 #include "ImageRegion.h"
     16 #include "ISurfaceProvider.h"
     17 
     18 #include <vector>
     19 
     20 namespace mozilla {
     21 namespace image {
     22 
     23 class BlobImageKeyData final {
     24 public:
     25  BlobImageKeyData(
     26      layers::WebRenderLayerManager* aManager, const wr::BlobImageKey& aBlobKey,
     27      std::vector<RefPtr<gfx::ScaledFont>>&& aScaledFonts,
     28      gfx::DrawEventRecorderPrivate_ExternalSurfacesHolder&& aExternalSurfaces)
     29      : mManager(aManager),
     30        mBlobKey(aBlobKey),
     31        mScaledFonts(std::move(aScaledFonts)),
     32        mExternalSurfaces(std::move(aExternalSurfaces)),
     33        mDirty(false) {}
     34 
     35  BlobImageKeyData(BlobImageKeyData&& aOther) noexcept
     36      : mManager(std::move(aOther.mManager)),
     37        mBlobKey(aOther.mBlobKey),
     38        mScaledFonts(std::move(aOther.mScaledFonts)),
     39        mExternalSurfaces(std::move(aOther.mExternalSurfaces)),
     40        mDirty(aOther.mDirty) {}
     41 
     42  BlobImageKeyData& operator=(BlobImageKeyData&& aOther) noexcept {
     43    mManager = std::move(aOther.mManager);
     44    mBlobKey = aOther.mBlobKey;
     45    mScaledFonts = std::move(aOther.mScaledFonts);
     46    mExternalSurfaces = std::move(aOther.mExternalSurfaces);
     47    mDirty = aOther.mDirty;
     48    return *this;
     49  }
     50 
     51  BlobImageKeyData(const BlobImageKeyData&) = delete;
     52  BlobImageKeyData& operator=(const BlobImageKeyData&) = delete;
     53 
     54  RefPtr<layers::WebRenderLayerManager> mManager;
     55  wr::BlobImageKey mBlobKey;
     56  std::vector<RefPtr<gfx::ScaledFont>> mScaledFonts;
     57  gfx::DrawEventRecorderPrivate_ExternalSurfacesHolder mExternalSurfaces;
     58  bool mDirty;
     59 };
     60 
     61 }  // namespace image
     62 }  // namespace mozilla
     63 
     64 MOZ_DECLARE_RELOCATE_USING_MOVE_CONSTRUCTOR(mozilla::image::BlobImageKeyData);
     65 
     66 namespace mozilla {
     67 namespace wr {
     68 class IpcResourceUpdateQueue;
     69 }  // namespace wr
     70 
     71 namespace image {
     72 class SVGDocumentWrapper;
     73 
     74 /**
     75 * An ISurfaceProvider that manages blob recordings of SVG images. Unlike the
     76 * rasterized ISurfaceProviders, it only provides a recording which may be
     77 * replayed in the compositor process by WebRender. It may be invalidated
     78 * directly in order to reuse the resource ids and underlying buffers when the
     79 * SVG image has changed (e.g. it is animated).
     80 */
     81 class BlobSurfaceProvider final : public ISurfaceProvider {
     82 public:
     83  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(BlobSurfaceProvider, override)
     84 
     85  BlobSurfaceProvider(ImageKey aImageKey, const SurfaceKey& aSurfaceKey,
     86                      SVGDocumentWrapper* aSVGDocumentWrapper,
     87                      uint32_t aImageFlags);
     88 
     89  bool IsFinished() const override { return true; }
     90 
     91  size_t LogicalSizeInBytes() const override {
     92    const gfx::IntSize& size = GetSurfaceKey().Size();
     93    return size.width * size.height * sizeof(uint32_t);
     94  }
     95 
     96  nsresult UpdateKey(layers::RenderRootStateManager* aManager,
     97                     wr::IpcResourceUpdateQueue& aResources,
     98                     wr::ImageKey& aKey) override;
     99 
    100  void InvalidateSurface() override;
    101 
    102  void AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
    103                              const AddSizeOfCb& aCallback) override {
    104    AddSizeOfCbData metadata;
    105    metadata.mFinished = true;
    106    metadata.mHeapBytes += mKeys.ShallowSizeOfExcludingThis(aMallocSizeOf);
    107 
    108    gfx::SourceSurface::SizeOfInfo info;
    109    info.AddType(gfx::SurfaceType::BLOB_IMAGE);
    110    metadata.Accumulate(info);
    111 
    112    aCallback(metadata);
    113  }
    114 
    115 protected:
    116  DrawableFrameRef DrawableRef(size_t aFrame) override {
    117    MOZ_ASSERT_UNREACHABLE("BlobSurfaceProvider::DrawableRef not supported!");
    118    return DrawableFrameRef();
    119  }
    120  bool IsLocked() const override { return true; }
    121  void SetLocked(bool) override {}
    122 
    123 private:
    124  ~BlobSurfaceProvider() override;
    125 
    126  Maybe<BlobImageKeyData> RecordDrawing(layers::WebRenderLayerManager* aManager,
    127                                        wr::IpcResourceUpdateQueue& aResources,
    128                                        Maybe<wr::BlobImageKey> aBlobKey);
    129 
    130  static void DestroyKeys(const AutoTArray<BlobImageKeyData, 1>& aKeys);
    131 
    132  AutoTArray<BlobImageKeyData, 1> mKeys;
    133 
    134  RefPtr<image::SVGDocumentWrapper> mSVGDocumentWrapper;
    135  uint32_t mImageFlags;
    136 };
    137 
    138 }  // namespace image
    139 }  // namespace mozilla
    140 
    141 #endif /* MOZILLA_IMAGE_BLOBSURFACEPROVIDER_H_ */