tor-browser

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

LookupResult.h (5522B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 /**
      7 * LookupResult is the return type of SurfaceCache's Lookup*() functions. It
      8 * combines a surface with relevant metadata tracked by SurfaceCache.
      9 */
     10 
     11 #ifndef mozilla_image_LookupResult_h
     12 #define mozilla_image_LookupResult_h
     13 
     14 #include <utility>
     15 
     16 #include "ISurfaceProvider.h"
     17 #include "mozilla/Attributes.h"
     18 #include "mozilla/gfx/Point.h"  // for IntSize
     19 
     20 namespace mozilla {
     21 namespace image {
     22 
     23 enum class MatchType : uint8_t {
     24  NOT_FOUND,  // No matching surface and no placeholder.
     25  PENDING,    // Found a matching placeholder, but no surface.
     26  EXACT,      // Found a surface that matches exactly.
     27  SUBSTITUTE_BECAUSE_NOT_FOUND,  // No exact match, but found a similar one.
     28  SUBSTITUTE_BECAUSE_PENDING,    // Found a similar surface and a placeholder
     29                                 // for an exact match.
     30 
     31  /* No exact match, but this should be considered an exact match for purposes
     32   * of deciding whether or not to request a new decode. This is because the
     33   * cache has determined that callers require too many size variants of this
     34   * image. It determines the set of sizes which best represent the image, and
     35   * will only suggest decoding of unavailable sizes from that set. */
     36  SUBSTITUTE_BECAUSE_BEST
     37 };
     38 
     39 /**
     40 * LookupResult is the return type of SurfaceCache's Lookup*() functions. It
     41 * combines a surface with relevant metadata tracked by SurfaceCache.
     42 */
     43 class MOZ_STACK_CLASS LookupResult {
     44 public:
     45  explicit LookupResult(MatchType aMatchType)
     46      : mMatchType(aMatchType), mFailedToRequestDecode(false) {
     47    MOZ_ASSERT(
     48        mMatchType == MatchType::NOT_FOUND || mMatchType == MatchType::PENDING,
     49        "Only NOT_FOUND or PENDING make sense with no surface");
     50  }
     51 
     52  LookupResult(LookupResult&& aOther)
     53      : mSurface(std::move(aOther.mSurface)),
     54        mMatchType(aOther.mMatchType),
     55        mSuggestedSize(aOther.mSuggestedSize),
     56        mFailedToRequestDecode(aOther.mFailedToRequestDecode) {}
     57 
     58  LookupResult(DrawableSurface&& aSurface, MatchType aMatchType)
     59      : mSurface(std::move(aSurface)),
     60        mMatchType(aMatchType),
     61        mFailedToRequestDecode(false) {
     62    MOZ_ASSERT(!mSurface || !(mMatchType == MatchType::NOT_FOUND ||
     63                              mMatchType == MatchType::PENDING),
     64               "Only NOT_FOUND or PENDING make sense with no surface");
     65    MOZ_ASSERT(mSurface || mMatchType == MatchType::NOT_FOUND ||
     66                   mMatchType == MatchType::PENDING,
     67               "NOT_FOUND or PENDING do not make sense with a surface");
     68  }
     69 
     70  LookupResult(MatchType aMatchType, const gfx::IntSize& aSuggestedSize)
     71      : mMatchType(aMatchType),
     72        mSuggestedSize(aSuggestedSize),
     73        mFailedToRequestDecode(false) {
     74    MOZ_ASSERT(
     75        mMatchType == MatchType::NOT_FOUND || mMatchType == MatchType::PENDING,
     76        "Only NOT_FOUND or PENDING make sense with no surface");
     77  }
     78 
     79  LookupResult(DrawableSurface&& aSurface, MatchType aMatchType,
     80               const gfx::IntSize& aSuggestedSize)
     81      : mSurface(std::move(aSurface)),
     82        mMatchType(aMatchType),
     83        mSuggestedSize(aSuggestedSize),
     84        mFailedToRequestDecode(false) {
     85    MOZ_ASSERT(!mSurface || !(mMatchType == MatchType::NOT_FOUND ||
     86                              mMatchType == MatchType::PENDING),
     87               "Only NOT_FOUND or PENDING make sense with no surface");
     88    MOZ_ASSERT(mSurface || mMatchType == MatchType::NOT_FOUND ||
     89                   mMatchType == MatchType::PENDING,
     90               "NOT_FOUND or PENDING do not make sense with a surface");
     91  }
     92 
     93  LookupResult& operator=(LookupResult&& aOther) {
     94    MOZ_ASSERT(&aOther != this, "Self-move-assignment is not supported");
     95    mSurface = std::move(aOther.mSurface);
     96    mMatchType = aOther.mMatchType;
     97    mSuggestedSize = aOther.mSuggestedSize;
     98    mFailedToRequestDecode = aOther.mFailedToRequestDecode;
     99    return *this;
    100  }
    101 
    102  DrawableSurface& Surface() { return mSurface; }
    103  const DrawableSurface& Surface() const { return mSurface; }
    104  const gfx::IntSize& SuggestedSize() const { return mSuggestedSize; }
    105 
    106  /// @return true if this LookupResult contains a surface.
    107  explicit operator bool() const { return bool(mSurface); }
    108 
    109  /// @return what kind of match this is (exact, substitute, etc.)
    110  MatchType Type() const { return mMatchType; }
    111 
    112  void SetFailedToRequestDecode() { mFailedToRequestDecode = true; }
    113  bool GetFailedToRequestDecode() { return mFailedToRequestDecode; }
    114 
    115 private:
    116  LookupResult(const LookupResult&) = delete;
    117  LookupResult& operator=(const LookupResult& aOther) = delete;
    118 
    119  DrawableSurface mSurface;
    120  MatchType mMatchType;
    121 
    122  /// mSuggestedSize will be the size of the returned surface if the result is
    123  /// SUBSTITUTE_BECAUSE_BEST. It will be empty for EXACT, and can contain a
    124  /// non-empty size possibly different from the returned surface (if any) for
    125  /// all other results. If non-empty, it will always be the size the caller
    126  /// should request any decodes at.
    127  gfx::IntSize mSuggestedSize;
    128 
    129  // True if we tried to start a decode but failed, likely because the image was
    130  // too big to fit into the surface cache.
    131  bool mFailedToRequestDecode;
    132 };
    133 
    134 }  // namespace image
    135 }  // namespace mozilla
    136 
    137 #endif  // mozilla_image_LookupResult_h