Tracer build fixes. (b=588021, r=dvander)
[mozilla-central.git] / layout / base / nsImageLoader.cpp
blobde3956edac229eb7165a9db3d24068aa7187712f
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2001
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Stuart Parmenter <pavlov@netscape.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 /* class to notify frames of background image loads */
42 #include "nsImageLoader.h"
44 #include "imgILoader.h"
46 #include "nsIURI.h"
47 #include "nsILoadGroup.h"
48 #include "nsNetUtil.h"
50 #include "nsPresContext.h"
51 #include "nsIPresShell.h"
52 #include "nsIFrame.h"
53 #include "nsIContent.h"
54 #include "nsIDocument.h"
56 #include "imgIContainer.h"
58 #include "nsStyleContext.h"
59 #include "nsGkAtoms.h"
61 // Paint forcing
62 #include "prenv.h"
64 NS_IMPL_ISUPPORTS2(nsImageLoader, imgIDecoderObserver, imgIContainerObserver)
66 nsImageLoader::nsImageLoader(nsIFrame *aFrame, PRUint32 aActions,
67 nsImageLoader *aNextLoader)
68 : mFrame(aFrame),
69 mActions(aActions),
70 mNextLoader(aNextLoader)
74 nsImageLoader::~nsImageLoader()
76 mFrame = nsnull;
78 if (mRequest) {
79 mRequest->CancelAndForgetObserver(NS_ERROR_FAILURE);
83 /* static */ already_AddRefed<nsImageLoader>
84 nsImageLoader::Create(nsIFrame *aFrame, imgIRequest *aRequest,
85 PRUint32 aActions, nsImageLoader *aNextLoader)
87 nsRefPtr<nsImageLoader> loader =
88 new nsImageLoader(aFrame, aActions, aNextLoader);
90 loader->Load(aRequest);
92 return loader.forget();
95 void
96 nsImageLoader::Destroy()
98 // Destroy the chain with only one level of recursion.
99 nsRefPtr<nsImageLoader> list = mNextLoader;
100 mNextLoader = nsnull;
101 while (list) {
102 nsRefPtr<nsImageLoader> todestroy = list;
103 list = todestroy->mNextLoader;
104 todestroy->mNextLoader = nsnull;
105 todestroy->Destroy();
108 mFrame = nsnull;
110 if (mRequest) {
111 mRequest->CancelAndForgetObserver(NS_ERROR_FAILURE);
114 mRequest = nsnull;
117 nsresult
118 nsImageLoader::Load(imgIRequest *aImage)
120 NS_ASSERTION(!mRequest, "can't reuse image loaders");
122 if (!mFrame)
123 return NS_ERROR_NOT_INITIALIZED;
125 if (!aImage)
126 return NS_ERROR_FAILURE;
128 // Make sure to clone into a temporary, then set mRequest, since
129 // cloning may notify and we don't want to trigger paints from this
130 // code.
131 nsCOMPtr<imgIRequest> newRequest;
132 nsresult rv = aImage->Clone(this, getter_AddRefs(newRequest));
133 mRequest.swap(newRequest);
134 return rv;
139 NS_IMETHODIMP nsImageLoader::OnStartContainer(imgIRequest *aRequest,
140 imgIContainer *aImage)
142 NS_ABORT_IF_FALSE(aImage, "Who's calling us then?");
144 /* Get requested animation policy from the pres context:
145 * normal = 0
146 * one frame = 1
147 * one loop = 2
149 aImage->SetAnimationMode(mFrame->PresContext()->ImageAnimationMode());
151 return NS_OK;
154 NS_IMETHODIMP nsImageLoader::OnStopFrame(imgIRequest *aRequest,
155 PRUint32 aFrame)
157 if (!mFrame)
158 return NS_ERROR_FAILURE;
160 if (!mRequest) {
161 // We're in the middle of a paint anyway
162 return NS_OK;
165 // Take requested actions
166 if (mActions & ACTION_REFLOW_ON_DECODE) {
167 DoReflow();
169 if (mActions & ACTION_REDRAW_ON_DECODE) {
170 DoRedraw(nsnull);
172 return NS_OK;
175 NS_IMETHODIMP nsImageLoader::OnStopRequest(imgIRequest *aRequest,
176 PRBool aLastPart)
178 if (!mFrame)
179 return NS_ERROR_FAILURE;
181 if (!mRequest) {
182 // We're in the middle of a paint anyway
183 return NS_OK;
186 // Take requested actions
187 if (mActions & ACTION_REFLOW_ON_LOAD) {
188 DoReflow();
190 if (mActions & ACTION_REDRAW_ON_LOAD) {
191 DoRedraw(nsnull);
193 return NS_OK;
196 NS_IMETHODIMP nsImageLoader::FrameChanged(imgIContainer *aContainer,
197 const nsIntRect *aDirtyRect)
199 if (!mFrame)
200 return NS_ERROR_FAILURE;
202 if (!mRequest) {
203 // We're in the middle of a paint anyway
204 return NS_OK;
207 nsRect r = (*aDirtyRect == nsIntRect::GetMaxSizedIntRect()) ?
208 nsRect(nsPoint(0, 0), mFrame->GetSize()) :
209 aDirtyRect->ToAppUnits(nsPresContext::AppUnitsPerCSSPixel());
211 DoRedraw(&r);
213 return NS_OK;
216 void
217 nsImageLoader::DoReflow()
219 nsIPresShell *shell = mFrame->PresContext()->GetPresShell();
220 shell->FrameNeedsReflow(mFrame, nsIPresShell::eStyleChange, NS_FRAME_IS_DIRTY);
223 void
224 nsImageLoader::DoRedraw(const nsRect* aDamageRect)
226 // NOTE: It is not sufficient to invalidate only the size of the image:
227 // the image may be tiled!
228 // The best option is to call into the frame, however lacking this
229 // we have to at least invalidate the frame's bounds, hence
230 // as long as we have a frame we'll use its size.
233 // Invalidate the entire frame
234 // XXX We really only need to invalidate the client area of the frame...
236 nsRect bounds(nsPoint(0, 0), mFrame->GetSize());
238 if (mFrame->GetType() == nsGkAtoms::canvasFrame) {
239 // The canvas's background covers the whole viewport.
240 bounds = mFrame->GetVisualOverflowRect();
243 // XXX this should be ok, but there is some crappy ass bug causing it not to work
244 // XXX seems related to the "body fixup rule" dealing with the canvas and body frames...
245 #if 0
246 // Invalidate the entire frame only if the frame has a tiled background
247 // image, otherwise just invalidate the intersection of the frame's bounds
248 // with the damaged rect.
249 nsStyleContext* styleContext;
250 mFrame->GetStyleContext(&styleContext);
251 const nsStyleBackground* bg = styleContext->GetStyleBackground();
253 if ((bg->mBackgroundFlags & NS_STYLE_BG_IMAGE_NONE) ||
254 (bg->mBackgroundRepeat == NS_STYLE_BG_REPEAT_OFF)) {
255 // The frame does not have a background image so we are free
256 // to invalidate only the intersection of the damage rect and
257 // the frame's bounds.
259 if (aDamageRect) {
260 bounds.IntersectRect(*aDamageRect, bounds);
264 #endif
266 if (mFrame->GetStyleVisibility()->IsVisible()) {
267 mFrame->Invalidate(bounds);