Back to index

lightning-sunbird  0.9+nobinonly
nsImageLoader.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 2001
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Stuart Parmenter <pavlov@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsImageLoader.h"
00041 
00042 #include "imgILoader.h"
00043 
00044 #include "nsIURI.h"
00045 #include "nsILoadGroup.h"
00046 #include "nsNetUtil.h"
00047 
00048 #include "nsPresContext.h"
00049 #include "nsIPresShell.h"
00050 #include "nsIFrame.h"
00051 #include "nsIContent.h"
00052 #include "nsIDocument.h"
00053 
00054 #include "imgIContainer.h"
00055 
00056 #include "nsIViewManager.h"
00057 
00058 #include "nsStyleContext.h"
00059 
00060 // Paint forcing
00061 #include "prenv.h"
00062 
00063 NS_IMPL_ISUPPORTS2(nsImageLoader, imgIDecoderObserver, imgIContainerObserver)
00064 
00065 nsImageLoader::nsImageLoader() :
00066   mFrame(nsnull), mPresContext(nsnull)
00067 {
00068 }
00069 
00070 nsImageLoader::~nsImageLoader()
00071 {
00072   mFrame = nsnull;
00073   mPresContext = nsnull;
00074 
00075   if (mRequest) {
00076     mRequest->Cancel(NS_ERROR_FAILURE);
00077   }
00078 }
00079 
00080 
00081 void
00082 nsImageLoader::Init(nsIFrame *aFrame, nsPresContext *aPresContext)
00083 {
00084   mFrame = aFrame;
00085   mPresContext = aPresContext;
00086 }
00087 
00088 void
00089 nsImageLoader::Destroy()
00090 {
00091   mFrame = nsnull;
00092   mPresContext = nsnull;
00093 
00094   if (mRequest) {
00095     mRequest->Cancel(NS_ERROR_FAILURE);
00096   }
00097 
00098   mRequest = nsnull;
00099 }
00100 
00101 nsresult
00102 nsImageLoader::Load(imgIRequest *aImage)
00103 {
00104   if (!mFrame)
00105     return NS_ERROR_NOT_INITIALIZED;
00106 
00107   if (!aImage)
00108     return NS_ERROR_FAILURE;
00109 
00110   if (mRequest) {
00111     nsCOMPtr<nsIURI> oldURI;
00112     mRequest->GetURI(getter_AddRefs(oldURI));
00113     nsCOMPtr<nsIURI> newURI;
00114     aImage->GetURI(getter_AddRefs(newURI));
00115     PRBool eq = PR_FALSE;
00116     nsresult rv = newURI->Equals(oldURI, &eq);
00117     if (NS_SUCCEEDED(rv) && eq) {
00118       return NS_OK;
00119     }
00120 
00121     // Now cancel the old request so it won't hold a stale ref to us.
00122     mRequest->Cancel(NS_ERROR_FAILURE);
00123     mRequest = nsnull;
00124   }
00125 
00126   // Make sure to clone into a temporary, then set mRequest, since
00127   // cloning may notify and we don't want to trigger paints from this
00128   // code.
00129   nsCOMPtr<imgIRequest> newRequest;
00130   nsresult rv = aImage->Clone(this, getter_AddRefs(newRequest));
00131   mRequest.swap(newRequest);
00132   return rv;
00133 }
00134 
00135                     
00136 
00137 NS_IMETHODIMP nsImageLoader::OnStartDecode(imgIRequest *aRequest)
00138 {
00139   return NS_OK;
00140 }
00141 
00142 NS_IMETHODIMP nsImageLoader::OnStartContainer(imgIRequest *aRequest,
00143                                               imgIContainer *aImage)
00144 {
00145   if (aImage)
00146   {
00147     /* Get requested animation policy from the pres context:
00148      *   normal = 0
00149      *   one frame = 1
00150      *   one loop = 2
00151      */
00152     aImage->SetAnimationMode(mPresContext->ImageAnimationMode());
00153     // Ensure the animation (if any) is started.
00154     aImage->StartAnimation();
00155   }
00156   return NS_OK;
00157 }
00158 
00159 NS_IMETHODIMP nsImageLoader::OnStartFrame(imgIRequest *aRequest,
00160                                           gfxIImageFrame *aFrame)
00161 {
00162   return NS_OK;
00163 }
00164 
00165 NS_IMETHODIMP nsImageLoader::OnDataAvailable(imgIRequest *aRequest,
00166                                              gfxIImageFrame *aFrame,
00167                                              const nsRect *aRect)
00168 {
00169   // Background images are not displayed incrementally, they are displayed after the entire 
00170   // image has been loaded.
00171   // Note: Images referenced by the <img> element are displayed incrementally in nsImageFrame.cpp
00172   return NS_OK;
00173 }
00174 
00175 NS_IMETHODIMP nsImageLoader::OnStopFrame(imgIRequest *aRequest,
00176                                          gfxIImageFrame *aFrame)
00177 {
00178   if (!mFrame)
00179     return NS_ERROR_FAILURE;
00180   
00181 #ifdef NS_DEBUG
00182 // Make sure the image request status's STATUS_FRAME_COMPLETE flag has been set to ensure
00183 // the image will be painted when invalidated
00184   if (aRequest) {
00185    PRUint32 status = imgIRequest::STATUS_ERROR;
00186    nsresult rv = aRequest->GetImageStatus(&status);
00187    if (NS_SUCCEEDED(rv)) {
00188      NS_ASSERTION((status & imgIRequest::STATUS_FRAME_COMPLETE), "imgIRequest::STATUS_FRAME_COMPLETE not set");
00189    }
00190   }
00191 #endif
00192 
00193   if (!mRequest) {
00194     // We're in the middle of a paint anyway
00195     return NS_OK;
00196   }
00197   
00198   // Draw the background image
00199   RedrawDirtyFrame(nsnull);
00200   return NS_OK;
00201 }
00202 
00203 NS_IMETHODIMP nsImageLoader::OnStopContainer(imgIRequest *aRequest,
00204                                              imgIContainer *aImage)
00205 {
00206   return NS_OK;
00207 }
00208 
00209 NS_IMETHODIMP nsImageLoader::OnStopDecode(imgIRequest *aRequest,
00210                                           nsresult status,
00211                                           const PRUnichar *statusArg)
00212 {
00213   return NS_OK;
00214 }
00215 
00216 NS_IMETHODIMP nsImageLoader::FrameChanged(imgIContainer *aContainer,
00217                                           gfxIImageFrame *newframe,
00218                                           nsRect * dirtyRect)
00219 {
00220   if (!mFrame)
00221     return NS_ERROR_FAILURE;
00222 
00223   if (!mRequest) {
00224     // We're in the middle of a paint anyway
00225     return NS_OK;
00226   }
00227   
00228   nsRect r(*dirtyRect);
00229 
00230   float p2t;
00231   p2t = mPresContext->PixelsToTwips();
00232   r.x = NSIntPixelsToTwips(r.x, p2t);
00233   r.y = NSIntPixelsToTwips(r.y, p2t);
00234   r.width = NSIntPixelsToTwips(r.width, p2t);
00235   r.height = NSIntPixelsToTwips(r.height, p2t);
00236 
00237   RedrawDirtyFrame(&r);
00238 
00239   return NS_OK;
00240 }
00241 
00242 
00243 void
00244 nsImageLoader::RedrawDirtyFrame(const nsRect* aDamageRect)
00245 {
00246   // NOTE: It is not sufficient to invalidate only the size of the image:
00247   //       the image may be tiled! 
00248   //       The best option is to call into the frame, however lacking this
00249   //       we have to at least invalidate the frame's bounds, hence
00250   //       as long as we have a frame we'll use its size.
00251   //
00252 
00253   // Invalidate the entire frame
00254   // XXX We really only need to invalidate the client area of the frame...    
00255 
00256   nsRect bounds(nsPoint(0, 0), mFrame->GetSize());
00257 
00258   // XXX this should be ok, but there is some crappy ass bug causing it not to work
00259   // XXX seems related to the "body fixup rule" dealing with the canvas and body frames...
00260 #if 0
00261   // Invalidate the entire frame only if the frame has a tiled background
00262   // image, otherwise just invalidate the intersection of the frame's bounds
00263   // with the damaged rect.
00264   nsStyleContext* styleContext;
00265   mFrame->GetStyleContext(&styleContext);
00266   const nsStyleBackground* bg = styleContext->GetStyleBackground();
00267 
00268   if ((bg->mBackgroundFlags & NS_STYLE_BG_IMAGE_NONE) ||
00269       (bg->mBackgroundRepeat == NS_STYLE_BG_REPEAT_OFF)) {
00270     // The frame does not have a background image so we are free
00271     // to invalidate only the intersection of the damage rect and
00272     // the frame's bounds.
00273 
00274     if (aDamageRect) {
00275       bounds.IntersectRect(*aDamageRect, bounds);
00276     }
00277   }
00278 
00279 #endif
00280 
00281   mFrame->Invalidate(bounds, PR_FALSE);
00282 }