image/src/SurfaceCache.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /**
michael@0 7 * SurfaceCache is a service for caching temporary surfaces in imagelib.
michael@0 8 */
michael@0 9
michael@0 10 #ifndef MOZILLA_IMAGELIB_SURFACECACHE_H_
michael@0 11 #define MOZILLA_IMAGELIB_SURFACECACHE_H_
michael@0 12
michael@0 13 #include "mozilla/HashFunctions.h" // for HashGeneric and AddToHash
michael@0 14 #include "gfxPoint.h" // for gfxSize
michael@0 15 #include "nsCOMPtr.h" // for already_AddRefed
michael@0 16 #include "mozilla/gfx/Point.h" // for mozilla::gfx::IntSize
michael@0 17 #include "SVGImageContext.h" // for SVGImageContext
michael@0 18
michael@0 19 class gfxDrawable;
michael@0 20
michael@0 21 namespace mozilla {
michael@0 22
michael@0 23 namespace gfx {
michael@0 24 class DrawTarget;
michael@0 25 } // namespace gfx
michael@0 26
michael@0 27 namespace image {
michael@0 28
michael@0 29 class Image;
michael@0 30
michael@0 31 /*
michael@0 32 * ImageKey contains the information we need to look up all cached surfaces for
michael@0 33 * a particular image.
michael@0 34 */
michael@0 35 typedef Image* ImageKey;
michael@0 36
michael@0 37 /*
michael@0 38 * SurfaceKey contains the information we need to look up a specific cached
michael@0 39 * surface. Together with an ImageKey, this uniquely identifies the surface.
michael@0 40 *
michael@0 41 * XXX(seth): Right now this is specialized to the needs of VectorImage. We'll
michael@0 42 * generalize it in bug 919071.
michael@0 43 */
michael@0 44 class SurfaceKey
michael@0 45 {
michael@0 46 typedef gfx::IntSize IntSize;
michael@0 47 public:
michael@0 48 SurfaceKey(const IntSize& aSize,
michael@0 49 const gfxSize aScale,
michael@0 50 const SVGImageContext* aSVGContext,
michael@0 51 const float aAnimationTime,
michael@0 52 const uint32_t aFlags)
michael@0 53 : mSize(aSize)
michael@0 54 , mScale(aScale)
michael@0 55 , mSVGContextIsValid(aSVGContext != nullptr)
michael@0 56 , mAnimationTime(aAnimationTime)
michael@0 57 , mFlags(aFlags)
michael@0 58 {
michael@0 59 // XXX(seth): Would love to use Maybe<T> here, but see bug 913586.
michael@0 60 if (mSVGContextIsValid)
michael@0 61 mSVGContext = *aSVGContext;
michael@0 62 }
michael@0 63
michael@0 64 bool operator==(const SurfaceKey& aOther) const
michael@0 65 {
michael@0 66 bool matchesSVGContext = aOther.mSVGContextIsValid == mSVGContextIsValid &&
michael@0 67 (!mSVGContextIsValid || aOther.mSVGContext == mSVGContext);
michael@0 68 return aOther.mSize == mSize &&
michael@0 69 aOther.mScale == mScale &&
michael@0 70 matchesSVGContext &&
michael@0 71 aOther.mAnimationTime == mAnimationTime &&
michael@0 72 aOther.mFlags == mFlags;
michael@0 73 }
michael@0 74
michael@0 75 uint32_t Hash() const
michael@0 76 {
michael@0 77 uint32_t hash = HashGeneric(mSize.width, mSize.height);
michael@0 78 hash = AddToHash(hash, mScale.width, mScale.height);
michael@0 79 hash = AddToHash(hash, mSVGContextIsValid, mSVGContext.Hash());
michael@0 80 hash = AddToHash(hash, mAnimationTime, mFlags);
michael@0 81 return hash;
michael@0 82 }
michael@0 83
michael@0 84 IntSize Size() const { return mSize; }
michael@0 85
michael@0 86 private:
michael@0 87 IntSize mSize;
michael@0 88 gfxSize mScale;
michael@0 89 SVGImageContext mSVGContext;
michael@0 90 bool mSVGContextIsValid;
michael@0 91 float mAnimationTime;
michael@0 92 uint32_t mFlags;
michael@0 93 };
michael@0 94
michael@0 95 /**
michael@0 96 * SurfaceCache is an imagelib-global service that allows caching of temporary
michael@0 97 * surfaces. Surfaces expire from the cache automatically if they go too long
michael@0 98 * without being accessed.
michael@0 99 *
michael@0 100 * SurfaceCache is not thread-safe; it should only be accessed from the main
michael@0 101 * thread.
michael@0 102 */
michael@0 103 struct SurfaceCache
michael@0 104 {
michael@0 105 typedef gfx::IntSize IntSize;
michael@0 106
michael@0 107 /*
michael@0 108 * Initialize static data. Called during imagelib module initialization.
michael@0 109 */
michael@0 110 static void Initialize();
michael@0 111
michael@0 112 /*
michael@0 113 * Release static data. Called during imagelib module shutdown.
michael@0 114 */
michael@0 115 static void Shutdown();
michael@0 116
michael@0 117 /*
michael@0 118 * Look up a surface in the cache.
michael@0 119 *
michael@0 120 * @param aImageKey Key data identifying which image the surface belongs to.
michael@0 121 * @param aSurfaceKey Key data which uniquely identifies the requested surface.
michael@0 122 *
michael@0 123 * @return the requested surface, or nullptr if not found.
michael@0 124 */
michael@0 125 static already_AddRefed<gfxDrawable> Lookup(const ImageKey aImageKey,
michael@0 126 const SurfaceKey& aSurfaceKey);
michael@0 127
michael@0 128 /*
michael@0 129 * Insert a surface into the cache. It is an error to call this function
michael@0 130 * without first calling Lookup to verify that the surface is not already in
michael@0 131 * the cache.
michael@0 132 *
michael@0 133 * @param aTarget The new surface (in the form of a DrawTarget) to insert
michael@0 134 * into the cache.
michael@0 135 * @param aImageKey Key data identifying which image the surface belongs to.
michael@0 136 * @param aSurfaceKey Key data which uniquely identifies the requested surface.
michael@0 137 */
michael@0 138 static void Insert(mozilla::gfx::DrawTarget* aTarget,
michael@0 139 const ImageKey aImageKey,
michael@0 140 const SurfaceKey& aSurfaceKey);
michael@0 141
michael@0 142 /*
michael@0 143 * Checks if a surface of a given size could possibly be stored in the cache.
michael@0 144 * If CanHold() returns false, Insert() will always fail to insert the
michael@0 145 * surface, but the inverse is not true: Insert() may take more information
michael@0 146 * into account than just image size when deciding whether to cache the
michael@0 147 * surface, so Insert() may still fail even if CanHold() returns true.
michael@0 148 *
michael@0 149 * Use CanHold() to avoid the need to create a temporary surface when we know
michael@0 150 * for sure the cache can't hold it.
michael@0 151 *
michael@0 152 * @param aSize The dimensions of a surface in pixels.
michael@0 153 *
michael@0 154 * @return false if the surface cache can't hold a surface of that size.
michael@0 155 */
michael@0 156 static bool CanHold(const IntSize& aSize);
michael@0 157
michael@0 158 /*
michael@0 159 * Evicts any cached surfaces associated with the given image from the cache.
michael@0 160 * This MUST be called, at a minimum, when the image is destroyed. If
michael@0 161 * another image were allocated at the same address it could result in
michael@0 162 * subtle, difficult-to-reproduce bugs.
michael@0 163 *
michael@0 164 * @param aImageKey The image which should be removed from the cache.
michael@0 165 */
michael@0 166 static void Discard(const ImageKey aImageKey);
michael@0 167
michael@0 168 /*
michael@0 169 * Evicts all caches surfaces from ths cache.
michael@0 170 */
michael@0 171 static void DiscardAll();
michael@0 172
michael@0 173 private:
michael@0 174 virtual ~SurfaceCache() = 0; // Forbid instantiation.
michael@0 175 };
michael@0 176
michael@0 177 } // namespace image
michael@0 178 } // namespace mozilla
michael@0 179
michael@0 180 #endif // MOZILLA_IMAGELIB_SURFACECACHE_H_

mercurial