tor-browser

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

InlineTranslator.h (6222B)


      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_layout_InlineTranslator_h
      8 #define mozilla_layout_InlineTranslator_h
      9 
     10 #include <string>
     11 
     12 #include "mozilla/gfx/2D.h"
     13 #include "mozilla/gfx/Filters.h"
     14 #include "mozilla/gfx/RecordedEvent.h"
     15 
     16 namespace mozilla {
     17 namespace gfx {
     18 
     19 using gfx::DrawTarget;
     20 using gfx::FilterNode;
     21 using gfx::GradientStops;
     22 using gfx::NativeFontResource;
     23 using gfx::Path;
     24 using gfx::ReferencePtr;
     25 using gfx::ScaledFont;
     26 using gfx::SourceSurface;
     27 using gfx::Translator;
     28 
     29 class InlineTranslator : public Translator {
     30 public:
     31  InlineTranslator();
     32 
     33  explicit InlineTranslator(DrawTarget* aDT, void* aFontContext = nullptr);
     34 
     35  bool TranslateRecording(char*, size_t len);
     36 
     37  void SetExternalSurfaces(
     38      nsRefPtrHashtable<nsUint64HashKey, SourceSurface>* aExternalSurfaces) {
     39    mExternalSurfaces = aExternalSurfaces;
     40  }
     41  void SetReferenceDrawTargetTransform(const Matrix& aTransform) {
     42    mBaseDTTransform = aTransform;
     43  }
     44 
     45  DrawTarget* LookupDrawTarget(ReferencePtr aRefPtr) final {
     46    DrawTarget* result = mDrawTargets.GetWeak(aRefPtr);
     47    MOZ_ASSERT(result);
     48    return result;
     49  }
     50 
     51  Path* LookupPath(ReferencePtr aRefPtr) final {
     52    Path* result = mPaths.GetWeak(aRefPtr);
     53    MOZ_ASSERT(result);
     54    return result;
     55  }
     56 
     57  SourceSurface* LookupSourceSurface(ReferencePtr aRefPtr) final {
     58    SourceSurface* result = mSourceSurfaces.GetWeak(aRefPtr);
     59    MOZ_ASSERT(result);
     60    return result;
     61  }
     62 
     63  FilterNode* LookupFilterNode(ReferencePtr aRefPtr) final {
     64    FilterNode* result = mFilterNodes.GetWeak(aRefPtr);
     65    MOZ_ASSERT(result);
     66    return result;
     67  }
     68 
     69  already_AddRefed<GradientStops> LookupGradientStops(
     70      ReferencePtr aRefPtr) final {
     71    return mGradientStops.Get(aRefPtr);
     72  }
     73 
     74  ScaledFont* LookupScaledFont(ReferencePtr aRefPtr) final {
     75    ScaledFont* result = mScaledFonts.GetWeak(aRefPtr);
     76    MOZ_ASSERT(result);
     77    return result;
     78  }
     79 
     80  UnscaledFont* LookupUnscaledFont(ReferencePtr aRefPtr) final {
     81    UnscaledFont* result = mUnscaledFonts.GetWeak(aRefPtr);
     82    MOZ_ASSERT(result);
     83    return result;
     84  }
     85 
     86  NativeFontResource* LookupNativeFontResource(uint64_t aKey) final {
     87    NativeFontResource* result = mNativeFontResources.GetWeak(aKey);
     88    MOZ_ASSERT(result);
     89    return result;
     90  }
     91 
     92  already_AddRefed<SourceSurface> LookupExternalSurface(uint64_t aKey) override;
     93 
     94  void AddDrawTarget(ReferencePtr aRefPtr, DrawTarget* aDT) final {
     95    RefPtr<DrawTarget>& value = mDrawTargets.LookupOrInsert(aRefPtr);
     96    if (mCurrentDT && mCurrentDT == value) {
     97      mCurrentDT = nullptr;
     98    }
     99    value = aDT;
    100  }
    101 
    102  void AddPath(ReferencePtr aRefPtr, Path* aPath) final {
    103    mPaths.InsertOrUpdate(aRefPtr, RefPtr{aPath});
    104  }
    105 
    106  void AddSourceSurface(ReferencePtr aRefPtr,
    107                        SourceSurface* aSurface) override {
    108    mSourceSurfaces.InsertOrUpdate(aRefPtr, RefPtr{aSurface});
    109  }
    110 
    111  void AddFilterNode(ReferencePtr aRefPtr, FilterNode* aFilter) final {
    112    mFilterNodes.InsertOrUpdate(aRefPtr, RefPtr{aFilter});
    113  }
    114 
    115  void AddGradientStops(ReferencePtr aRefPtr, GradientStops* aStops) final {
    116    mGradientStops.InsertOrUpdate(aRefPtr, RefPtr{aStops});
    117  }
    118 
    119  void AddScaledFont(ReferencePtr aRefPtr, ScaledFont* aScaledFont) final {
    120    mScaledFonts.InsertOrUpdate(aRefPtr, RefPtr{aScaledFont});
    121  }
    122 
    123  void AddUnscaledFont(ReferencePtr aRefPtr,
    124                       UnscaledFont* aUnscaledFont) final {
    125    mUnscaledFonts.InsertOrUpdate(aRefPtr, RefPtr{aUnscaledFont});
    126  }
    127 
    128  void AddNativeFontResource(uint64_t aKey,
    129                             NativeFontResource* aScaledFontResouce) final {
    130    mNativeFontResources.InsertOrUpdate(aKey, RefPtr{aScaledFontResouce});
    131  }
    132 
    133  void RemoveDrawTarget(ReferencePtr aRefPtr) override {
    134    RefPtr<DrawTarget> removedDT;
    135    if (mDrawTargets.Remove(aRefPtr, getter_AddRefs(removedDT)) &&
    136        mCurrentDT == removedDT) {
    137      mCurrentDT = nullptr;
    138    }
    139  }
    140 
    141  bool SetCurrentDrawTarget(ReferencePtr aRefPtr) override {
    142    mCurrentDT = mDrawTargets.GetWeak(aRefPtr);
    143    return !!mCurrentDT;
    144  }
    145 
    146  void RemovePath(ReferencePtr aRefPtr) final { mPaths.Remove(aRefPtr); }
    147 
    148  void RemoveSourceSurface(ReferencePtr aRefPtr) override {
    149    mSourceSurfaces.Remove(aRefPtr);
    150  }
    151 
    152  void RemoveFilterNode(ReferencePtr aRefPtr) final {
    153    mFilterNodes.Remove(aRefPtr);
    154  }
    155 
    156  void RemoveGradientStops(ReferencePtr aRefPtr) final {
    157    mGradientStops.Remove(aRefPtr);
    158  }
    159 
    160  void RemoveScaledFont(ReferencePtr aRefPtr) final {
    161    mScaledFonts.Remove(aRefPtr);
    162  }
    163 
    164  void RemoveUnscaledFont(ReferencePtr aRefPtr) final {
    165    mUnscaledFonts.Remove(aRefPtr);
    166  }
    167 
    168  already_AddRefed<DrawTarget> CreateDrawTarget(
    169      ReferencePtr aRefPtr, const gfx::IntSize& aSize,
    170      gfx::SurfaceFormat aFormat) override;
    171 
    172  mozilla::gfx::DrawTarget* GetReferenceDrawTarget() final {
    173    MOZ_ASSERT(mBaseDT, "mBaseDT has not been initialized.");
    174    return mBaseDT;
    175  }
    176  Matrix GetReferenceDrawTargetTransform() final { return mBaseDTTransform; }
    177 
    178  void* GetFontContext() final { return mFontContext; }
    179  std::string GetError() { return mError; }
    180 
    181 protected:
    182  RefPtr<DrawTarget> mBaseDT;
    183  Matrix mBaseDTTransform;
    184  nsRefPtrHashtable<nsPtrHashKey<void>, DrawTarget> mDrawTargets;
    185 
    186 private:
    187  void* mFontContext;
    188  std::string mError;
    189 
    190  nsRefPtrHashtable<nsPtrHashKey<void>, Path> mPaths;
    191  nsRefPtrHashtable<nsPtrHashKey<void>, SourceSurface> mSourceSurfaces;
    192  nsRefPtrHashtable<nsPtrHashKey<void>, FilterNode> mFilterNodes;
    193  nsRefPtrHashtable<nsPtrHashKey<void>, GradientStops> mGradientStops;
    194  nsRefPtrHashtable<nsPtrHashKey<void>, ScaledFont> mScaledFonts;
    195  nsRefPtrHashtable<nsPtrHashKey<void>, UnscaledFont> mUnscaledFonts;
    196  nsRefPtrHashtable<nsUint64HashKey, NativeFontResource> mNativeFontResources;
    197  nsRefPtrHashtable<nsUint64HashKey, SourceSurface>* mExternalSurfaces =
    198      nullptr;
    199 };
    200 
    201 }  // namespace gfx
    202 }  // namespace mozilla
    203 
    204 #endif  // mozilla_layout_InlineTranslator_h