1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Corporation code.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2011
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Robert O'Callahan <robert@ocallahan.org>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "CanvasImageCache.h"
39 : #include "nsIImageLoadingContent.h"
40 : #include "nsExpirationTracker.h"
41 : #include "imgIRequest.h"
42 : #include "gfxASurface.h"
43 : #include "gfxPoint.h"
44 : #include "nsIDOMElement.h"
45 : #include "nsTHashtable.h"
46 : #include "nsHTMLCanvasElement.h"
47 : #include "nsContentUtils.h"
48 :
49 : namespace mozilla {
50 :
51 : struct ImageCacheKey {
52 0 : ImageCacheKey(nsIDOMElement* aImage, nsHTMLCanvasElement* aCanvas)
53 0 : : mImage(aImage), mCanvas(aCanvas) {}
54 : nsIDOMElement* mImage;
55 : nsHTMLCanvasElement* mCanvas;
56 : };
57 :
58 0 : struct ImageCacheEntryData {
59 : ImageCacheEntryData(const ImageCacheEntryData& aOther)
60 : : mImage(aOther.mImage)
61 : , mILC(aOther.mILC)
62 : , mCanvas(aOther.mCanvas)
63 : , mRequest(aOther.mRequest)
64 : , mSurface(aOther.mSurface)
65 : , mSize(aOther.mSize)
66 : {}
67 0 : ImageCacheEntryData(const ImageCacheKey& aKey)
68 : : mImage(aKey.mImage)
69 : , mILC(nsnull)
70 0 : , mCanvas(aKey.mCanvas)
71 0 : {}
72 :
73 0 : nsExpirationState* GetExpirationState() { return &mState; }
74 :
75 : // Key
76 : nsCOMPtr<nsIDOMElement> mImage;
77 : nsIImageLoadingContent* mILC;
78 : nsRefPtr<nsHTMLCanvasElement> mCanvas;
79 : // Value
80 : nsCOMPtr<imgIRequest> mRequest;
81 : nsRefPtr<gfxASurface> mSurface;
82 : gfxIntSize mSize;
83 : nsExpirationState mState;
84 : };
85 :
86 : class ImageCacheEntry : public PLDHashEntryHdr {
87 : public:
88 : typedef ImageCacheKey KeyType;
89 : typedef const ImageCacheKey* KeyTypePointer;
90 :
91 0 : ImageCacheEntry(const KeyType *key) :
92 0 : mData(new ImageCacheEntryData(*key)) {}
93 : ImageCacheEntry(const ImageCacheEntry &toCopy) :
94 : mData(new ImageCacheEntryData(*toCopy.mData)) {}
95 0 : ~ImageCacheEntry() {}
96 :
97 0 : bool KeyEquals(KeyTypePointer key) const
98 : {
99 0 : return mData->mImage == key->mImage && mData->mCanvas == key->mCanvas;
100 : }
101 :
102 0 : static KeyTypePointer KeyToPointer(KeyType& key) { return &key; }
103 0 : static PLDHashNumber HashKey(KeyTypePointer key)
104 : {
105 0 : return HashGeneric(key->mImage, key->mCanvas);
106 : }
107 : enum { ALLOW_MEMMOVE = true };
108 :
109 : nsAutoPtr<ImageCacheEntryData> mData;
110 : };
111 :
112 : class ImageCache MOZ_FINAL : public nsExpirationTracker<ImageCacheEntryData,4> {
113 : public:
114 : // We use 3 generations of 1 second each to get a 2-3 seconds timeout.
115 : enum { GENERATION_MS = 1000 };
116 0 : ImageCache()
117 0 : : nsExpirationTracker<ImageCacheEntryData,4>(GENERATION_MS)
118 : {
119 0 : mCache.Init();
120 0 : }
121 0 : ~ImageCache() {
122 0 : AgeAllGenerations();
123 0 : }
124 :
125 0 : virtual void NotifyExpired(ImageCacheEntryData* aObject)
126 : {
127 0 : RemoveObject(aObject);
128 : // Deleting the entry will delete aObject since the entry owns aObject
129 0 : mCache.RemoveEntry(ImageCacheKey(aObject->mImage, aObject->mCanvas));
130 0 : }
131 :
132 : nsTHashtable<ImageCacheEntry> mCache;
133 : };
134 :
135 : static ImageCache* gImageCache = nsnull;
136 :
137 : class CanvasImageCacheShutdownObserver MOZ_FINAL : public nsIObserver
138 0 : {
139 : public:
140 : NS_DECL_ISUPPORTS
141 : NS_DECL_NSIOBSERVER
142 : };
143 :
144 : void
145 0 : CanvasImageCache::NotifyDrawImage(nsIDOMElement* aImage,
146 : nsHTMLCanvasElement* aCanvas,
147 : imgIRequest* aRequest,
148 : gfxASurface* aSurface,
149 : const gfxIntSize& aSize)
150 : {
151 0 : if (!gImageCache) {
152 0 : gImageCache = new ImageCache();
153 0 : nsContentUtils::RegisterShutdownObserver(new CanvasImageCacheShutdownObserver());
154 : }
155 :
156 0 : ImageCacheEntry* entry = gImageCache->mCache.PutEntry(ImageCacheKey(aImage, aCanvas));
157 0 : if (entry) {
158 0 : if (entry->mData->mSurface) {
159 : // We are overwriting an existing entry.
160 0 : gImageCache->RemoveObject(entry->mData);
161 : }
162 0 : gImageCache->AddObject(entry->mData);
163 :
164 0 : nsCOMPtr<nsIImageLoadingContent> ilc = do_QueryInterface(aImage);
165 0 : if (ilc) {
166 0 : ilc->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
167 0 : getter_AddRefs(entry->mData->mRequest));
168 : }
169 0 : entry->mData->mILC = ilc;
170 0 : entry->mData->mSurface = aSurface;
171 0 : entry->mData->mSize = aSize;
172 : }
173 0 : }
174 :
175 : gfxASurface*
176 0 : CanvasImageCache::Lookup(nsIDOMElement* aImage,
177 : nsHTMLCanvasElement* aCanvas,
178 : gfxIntSize* aSize)
179 : {
180 0 : if (!gImageCache)
181 0 : return nsnull;
182 :
183 0 : ImageCacheEntry* entry = gImageCache->mCache.GetEntry(ImageCacheKey(aImage, aCanvas));
184 0 : if (!entry || !entry->mData->mILC)
185 0 : return nsnull;
186 :
187 0 : nsCOMPtr<imgIRequest> request;
188 0 : entry->mData->mILC->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST, getter_AddRefs(request));
189 0 : if (request != entry->mData->mRequest)
190 0 : return nsnull;
191 :
192 0 : gImageCache->MarkUsed(entry->mData);
193 :
194 0 : *aSize = entry->mData->mSize;
195 0 : return entry->mData->mSurface;
196 : }
197 :
198 0 : NS_IMPL_ISUPPORTS1(CanvasImageCacheShutdownObserver, nsIObserver)
199 :
200 : NS_IMETHODIMP
201 0 : CanvasImageCacheShutdownObserver::Observe(nsISupports *aSubject,
202 : const char *aTopic,
203 : const PRUnichar *aData)
204 : {
205 0 : if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
206 0 : delete gImageCache;
207 0 : gImageCache = nsnull;
208 :
209 0 : nsContentUtils::UnregisterShutdownObserver(this);
210 : }
211 :
212 0 : return NS_OK;
213 : }
214 :
215 : } // namespace mozilla
|