Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 20; 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 | #ifndef MASKLAYERIMAGECACHE_H_ |
michael@0 | 7 | #define MASKLAYERIMAGECACHE_H_ |
michael@0 | 8 | |
michael@0 | 9 | #include "DisplayItemClip.h" |
michael@0 | 10 | #include "nsPresContext.h" |
michael@0 | 11 | #include "mozilla/gfx/Matrix.h" |
michael@0 | 12 | |
michael@0 | 13 | namespace mozilla { |
michael@0 | 14 | |
michael@0 | 15 | namespace layers { |
michael@0 | 16 | class ImageContainer; |
michael@0 | 17 | } |
michael@0 | 18 | |
michael@0 | 19 | /** |
michael@0 | 20 | * Keeps a record of image containers for mask layers, containers are mapped |
michael@0 | 21 | * from the rounded rects used to create them. |
michael@0 | 22 | * The cache stores MaskLayerImageEntries indexed by MaskLayerImageKeys. |
michael@0 | 23 | * Each MaskLayerImageEntry owns a heap-allocated MaskLayerImageKey |
michael@0 | 24 | * (heap-allocated so that a mask layer's userdata can keep a pointer to the |
michael@0 | 25 | * key for its image, in spite of the hashtable moving its entries around). |
michael@0 | 26 | * The key consists of the rounded rects used to create the mask, |
michael@0 | 27 | * an nsRefPtr to the ImageContainer containing the image, and a count |
michael@0 | 28 | * of the number of layers currently using this ImageContainer. |
michael@0 | 29 | * When the key's layer count is zero, the cache |
michael@0 | 30 | * may remove the entry, which deletes the key object. |
michael@0 | 31 | */ |
michael@0 | 32 | class MaskLayerImageCache |
michael@0 | 33 | { |
michael@0 | 34 | typedef mozilla::layers::ImageContainer ImageContainer; |
michael@0 | 35 | public: |
michael@0 | 36 | MaskLayerImageCache(); |
michael@0 | 37 | ~MaskLayerImageCache(); |
michael@0 | 38 | |
michael@0 | 39 | /** |
michael@0 | 40 | * Representation of a rounded rectangle in device pixel coordinates, in |
michael@0 | 41 | * contrast to DisplayItemClip::RoundedRect, which uses app units. |
michael@0 | 42 | * In particular, our internal representation uses a gfxRect, rather than |
michael@0 | 43 | * an nsRect, so this class is easier to use with transforms. |
michael@0 | 44 | */ |
michael@0 | 45 | struct PixelRoundedRect |
michael@0 | 46 | { |
michael@0 | 47 | PixelRoundedRect(const DisplayItemClip::RoundedRect& aRRect, |
michael@0 | 48 | nsPresContext* aPresContext) |
michael@0 | 49 | : mRect(aPresContext->AppUnitsToGfxUnits(aRRect.mRect.x), |
michael@0 | 50 | aPresContext->AppUnitsToGfxUnits(aRRect.mRect.y), |
michael@0 | 51 | aPresContext->AppUnitsToGfxUnits(aRRect.mRect.width), |
michael@0 | 52 | aPresContext->AppUnitsToGfxUnits(aRRect.mRect.height)) |
michael@0 | 53 | { |
michael@0 | 54 | MOZ_COUNT_CTOR(PixelRoundedRect); |
michael@0 | 55 | NS_FOR_CSS_HALF_CORNERS(corner) { |
michael@0 | 56 | mRadii[corner] = aPresContext->AppUnitsToGfxUnits(aRRect.mRadii[corner]); |
michael@0 | 57 | } |
michael@0 | 58 | } |
michael@0 | 59 | PixelRoundedRect(const PixelRoundedRect& aPRR) |
michael@0 | 60 | : mRect(aPRR.mRect) |
michael@0 | 61 | { |
michael@0 | 62 | MOZ_COUNT_CTOR(PixelRoundedRect); |
michael@0 | 63 | NS_FOR_CSS_HALF_CORNERS(corner) { |
michael@0 | 64 | mRadii[corner] = aPRR.mRadii[corner]; |
michael@0 | 65 | } |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | ~PixelRoundedRect() |
michael@0 | 69 | { |
michael@0 | 70 | MOZ_COUNT_DTOR(PixelRoundedRect); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | // Applies the scale and translate components of aTransform. |
michael@0 | 74 | // It is an error to pass a matrix which does more than just scale |
michael@0 | 75 | // and translate. |
michael@0 | 76 | void ScaleAndTranslate(const gfx::Matrix& aTransform) |
michael@0 | 77 | { |
michael@0 | 78 | NS_ASSERTION(aTransform._12 == 0 && aTransform._21 == 0, |
michael@0 | 79 | "Transform has a component other than scale and translate"); |
michael@0 | 80 | |
michael@0 | 81 | mRect = aTransform.TransformBounds(mRect); |
michael@0 | 82 | |
michael@0 | 83 | for (size_t i = 0; i < ArrayLength(mRadii); i += 2) { |
michael@0 | 84 | mRadii[i] *= aTransform._11; |
michael@0 | 85 | mRadii[i + 1] *= aTransform._22; |
michael@0 | 86 | } |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | bool operator==(const PixelRoundedRect& aOther) const { |
michael@0 | 90 | if (!mRect.IsEqualInterior(aOther.mRect)) { |
michael@0 | 91 | return false; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | NS_FOR_CSS_HALF_CORNERS(corner) { |
michael@0 | 95 | if (mRadii[corner] != aOther.mRadii[corner]) { |
michael@0 | 96 | return false; |
michael@0 | 97 | } |
michael@0 | 98 | } |
michael@0 | 99 | return true; |
michael@0 | 100 | } |
michael@0 | 101 | bool operator!=(const PixelRoundedRect& aOther) const { |
michael@0 | 102 | return !(*this == aOther); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | // Create a hash for this object. |
michael@0 | 106 | PLDHashNumber Hash() const |
michael@0 | 107 | { |
michael@0 | 108 | PLDHashNumber hash = HashBytes(&mRect.x, 4*sizeof(gfxFloat)); |
michael@0 | 109 | hash = AddToHash(hash, HashBytes(mRadii, 8*sizeof(gfxFloat))); |
michael@0 | 110 | |
michael@0 | 111 | return hash; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | gfx::Rect mRect; |
michael@0 | 115 | // Indices into mRadii are the NS_CORNER_* constants in nsStyleConsts.h |
michael@0 | 116 | gfxFloat mRadii[8]; |
michael@0 | 117 | |
michael@0 | 118 | private: |
michael@0 | 119 | PixelRoundedRect() MOZ_DELETE; |
michael@0 | 120 | }; |
michael@0 | 121 | |
michael@0 | 122 | /** |
michael@0 | 123 | * A key to identify cached image containers. |
michael@0 | 124 | * The const-ness of this class is with respect to its use as a key into a |
michael@0 | 125 | * hashtable, so anything not used to create the hash is mutable. |
michael@0 | 126 | * mLayerCount counts the number of mask layers which have a reference to |
michael@0 | 127 | * MaskLayerImageEntry::mContainer; it is maintained by MaskLayerUserData, |
michael@0 | 128 | * which keeps a reference to the key. There will usually be mLayerCount + 1 |
michael@0 | 129 | * pointers to a key object (the +1 being from the hashtable entry), but this |
michael@0 | 130 | * invariant may be temporarily broken. |
michael@0 | 131 | */ |
michael@0 | 132 | struct MaskLayerImageKey |
michael@0 | 133 | { |
michael@0 | 134 | MaskLayerImageKey() |
michael@0 | 135 | : mLayerCount(0) |
michael@0 | 136 | , mRoundedClipRects() |
michael@0 | 137 | { |
michael@0 | 138 | MOZ_COUNT_CTOR(MaskLayerImageKey); |
michael@0 | 139 | } |
michael@0 | 140 | MaskLayerImageKey(const MaskLayerImageKey& aKey) |
michael@0 | 141 | : mLayerCount(aKey.mLayerCount) |
michael@0 | 142 | , mRoundedClipRects(aKey.mRoundedClipRects) |
michael@0 | 143 | { |
michael@0 | 144 | MOZ_COUNT_CTOR(MaskLayerImageKey); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | ~MaskLayerImageKey() |
michael@0 | 148 | { |
michael@0 | 149 | MOZ_COUNT_DTOR(MaskLayerImageKey); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | void AddRef() const { ++mLayerCount; } |
michael@0 | 153 | void Release() const |
michael@0 | 154 | { |
michael@0 | 155 | NS_ASSERTION(mLayerCount > 0, "Inconsistent layer count"); |
michael@0 | 156 | --mLayerCount; |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | PLDHashNumber Hash() const |
michael@0 | 160 | { |
michael@0 | 161 | PLDHashNumber hash = 0; |
michael@0 | 162 | |
michael@0 | 163 | for (uint32_t i = 0; i < mRoundedClipRects.Length(); ++i) { |
michael@0 | 164 | hash = AddToHash(hash, mRoundedClipRects[i].Hash()); |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | return hash; |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | bool operator==(const MaskLayerImageKey& aOther) const |
michael@0 | 171 | { |
michael@0 | 172 | return mRoundedClipRects == aOther.mRoundedClipRects; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | mutable uint32_t mLayerCount; |
michael@0 | 176 | nsTArray<PixelRoundedRect> mRoundedClipRects; |
michael@0 | 177 | }; |
michael@0 | 178 | |
michael@0 | 179 | |
michael@0 | 180 | // Find an image container for aKey, returns nullptr if there is no suitable |
michael@0 | 181 | // cached image. If there is an image, then aKey is set to point at the stored |
michael@0 | 182 | // key for the image. |
michael@0 | 183 | ImageContainer* FindImageFor(const MaskLayerImageKey** aKey); |
michael@0 | 184 | |
michael@0 | 185 | // Add an image container with a key to the cache |
michael@0 | 186 | // The image container used will be set as the container in aKey and aKey |
michael@0 | 187 | // itself will be linked from this cache |
michael@0 | 188 | void PutImage(const MaskLayerImageKey* aKey, |
michael@0 | 189 | ImageContainer* aContainer); |
michael@0 | 190 | |
michael@0 | 191 | // Sweep the cache for old image containers that can be deleted |
michael@0 | 192 | void Sweep(); |
michael@0 | 193 | |
michael@0 | 194 | protected: |
michael@0 | 195 | |
michael@0 | 196 | class MaskLayerImageEntry : public PLDHashEntryHdr |
michael@0 | 197 | { |
michael@0 | 198 | public: |
michael@0 | 199 | typedef const MaskLayerImageKey& KeyType; |
michael@0 | 200 | typedef const MaskLayerImageKey* KeyTypePointer; |
michael@0 | 201 | |
michael@0 | 202 | MaskLayerImageEntry(KeyTypePointer aKey) : mKey(aKey) |
michael@0 | 203 | { |
michael@0 | 204 | MOZ_COUNT_CTOR(MaskLayerImageEntry); |
michael@0 | 205 | } |
michael@0 | 206 | MaskLayerImageEntry(const MaskLayerImageEntry& aOther) |
michael@0 | 207 | : mKey(aOther.mKey.get()) |
michael@0 | 208 | { |
michael@0 | 209 | NS_ERROR("ALLOW_MEMMOVE == true, should never be called"); |
michael@0 | 210 | } |
michael@0 | 211 | ~MaskLayerImageEntry() |
michael@0 | 212 | { |
michael@0 | 213 | MOZ_COUNT_DTOR(MaskLayerImageEntry); |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | // KeyEquals(): does this entry match this key? |
michael@0 | 217 | bool KeyEquals(KeyTypePointer aKey) const |
michael@0 | 218 | { |
michael@0 | 219 | return *mKey == *aKey; |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | // KeyToPointer(): Convert KeyType to KeyTypePointer |
michael@0 | 223 | static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; } |
michael@0 | 224 | |
michael@0 | 225 | // HashKey(): calculate the hash number |
michael@0 | 226 | static PLDHashNumber HashKey(KeyTypePointer aKey) |
michael@0 | 227 | { |
michael@0 | 228 | return aKey->Hash(); |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | // ALLOW_MEMMOVE can we move this class with memmove(), or do we have |
michael@0 | 232 | // to use the copy constructor? |
michael@0 | 233 | enum { ALLOW_MEMMOVE = true }; |
michael@0 | 234 | |
michael@0 | 235 | bool operator==(const MaskLayerImageEntry& aOther) const |
michael@0 | 236 | { |
michael@0 | 237 | return KeyEquals(aOther.mKey); |
michael@0 | 238 | } |
michael@0 | 239 | |
michael@0 | 240 | nsAutoPtr<const MaskLayerImageKey> mKey; |
michael@0 | 241 | nsRefPtr<ImageContainer> mContainer; |
michael@0 | 242 | }; |
michael@0 | 243 | |
michael@0 | 244 | nsTHashtable<MaskLayerImageEntry> mMaskImageContainers; |
michael@0 | 245 | |
michael@0 | 246 | // helper funtion for Sweep(), called for each entry in the hashtable |
michael@0 | 247 | static PLDHashOperator SweepFunc(MaskLayerImageEntry* aEntry, void* aUserArg); |
michael@0 | 248 | }; |
michael@0 | 249 | |
michael@0 | 250 | } |
michael@0 | 251 | |
michael@0 | 252 | |
michael@0 | 253 | #endif |