first pass at making animated gifs work better in cairo builds. bug 324707. r=vlad

This commit is contained in:
pavlov%pavlov.net 2006-04-01 00:35:58 +00:00
Родитель 2443505faa
Коммит cddbd98657
3 изменённых файлов: 31 добавлений и 284 удалений

Просмотреть файл

@ -986,16 +986,13 @@ void imgContainerGIF::BlackenFrame(gfxIImageFrame *aFrame)
if (!aFrame)
return;
#ifdef MOZ_CAIRO_GFX
nsCOMPtr<nsIImage> img(do_GetInterface(aFrame));
if (!img)
return;
PRInt32 widthFrame;
PRInt32 heightFrame;
aFrame->GetWidth(&widthFrame);
aFrame->GetHeight(&heightFrame);
BlackenFrame(aFrame, 0, 0, widthFrame, heightFrame);
nsRefPtr<gfxASurface> surf;
img->GetSurface(getter_AddRefs(surf));
nsRefPtr<gfxContext> ctx = new gfxContext(surf);
ctx->SetColor(gfxRGBA(0, 0, 0));
ctx->SetOperator(gfxContext::OPERATOR_SOURCE);
ctx->Paint();
#else
PRUint32 aDataLength;
@ -1012,6 +1009,24 @@ void imgContainerGIF::BlackenFrame(gfxIImageFrame *aFrame,
if (!aFrame)
return;
#ifdef MOZ_CAIRO_GFX
nsCOMPtr<nsIImage> img(do_GetInterface(aFrame));
if (!img)
return;
nsRefPtr<gfxASurface> surf;
img->GetSurface(getter_AddRefs(surf));
nsRefPtr<gfxContext> ctx = new gfxContext(surf);
ctx->SetColor(gfxRGBA(0, 0, 0));
ctx->Rectangle(gfxRect(aX, aY, aWidth, aHeight));
ctx->Fill();
nsIntRect r(aX, aY, aWidth, aHeight);
img->ImageUpdated(nsnull, nsImageUpdateFlags_kBitsChanged, &r);
#else // MOZ_CAIRO_GFX
PRInt32 widthFrame;
PRInt32 heightFrame;
aFrame->GetWidth(&widthFrame);
@ -1027,7 +1042,7 @@ void imgContainerGIF::BlackenFrame(gfxIImageFrame *aFrame,
PRUint32 bpr; // Bytes Per Row
aFrame->GetImageBytesPerRow(&bpr);
#if defined(MOZ_CAIRO_GFX) && (defined(XP_MAC) || defined(XP_MACOSX))
#if defined(XP_MAC) || defined(XP_MACOSX)
const PRUint8 bpp = 4;
#else
const PRUint8 bpp = 3;
@ -1038,6 +1053,7 @@ void imgContainerGIF::BlackenFrame(gfxIImageFrame *aFrame,
for (PRInt32 y = 0; y < height; y++) {
aFrame->SetImageData(nsnull, bprToWrite, ((y + aY) * bpr) + xOffset);
}
#endif // MOZ_CAIRO_GFX
}

Просмотреть файл

@ -1,273 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2001
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/** @file
* This file declares the imgContainerGIF class, which
* handles animation of GIF frames.
*
* @author Stuart Parmenter <pavlov@netscape.com>
* @author Chris Saari <saari@netscape.com>
* @author Arron Mogge <paper@animecity.nu>
*/
#ifndef _imgContainerGIF_h_
#define _imgContainerGIF_h_
#include "imgIContainerObserver.h"
#include "imgIContainer.h"
#include "nsCOMArray.h"
#include "nsCOMPtr.h"
#include "nsITimer.h"
#include "imgIDecoderObserver.h"
#include "gfxIImageFrame.h"
#include "nsWeakReference.h"
#define NS_GIFCONTAINER_CID \
{ /* da72e7ee-4821-4452-802d-5eb2d865dd3c */ \
0xda72e7ee, \
0x4821, \
0x4452, \
{0x80, 0x2d, 0x5e, 0xb2, 0xd8, 0x65, 0xdd, 0x3c} \
}
/**
* Handles animation of GIF frames.
*
*
* @par A Quick Walk Through
* nsGIFDecoder initializes this class and calls AppendFrame() to add a frame.
* Once imgContainerGIF detects more than one frame, it starts the animation
* with StartAnimation().
*
* @par
* StartAnimation() checks if animating is allowed, and creates a timer. The
* timer calls Notify when the specified frame delay time is up.
*
* @par
* Notify() moves on to the next frame, sets up the new timer delay, destroys
* the old frame, and forces a redraw via observer->FrameChanged().
*
* @par
* Each GIF frame can have a different method of removing itself. These are
* listed as an enum prefixed with DISPOSE_. Notify() calls DoComposite() to
* handle any special frame destruction.
*
* @par
* The basic path through DoComposite() is:
* 1) Calculate Area that needs updating, which is at least the area of
* aNextFrame.
* 2) Dispose of previous frame.
* 3) Draw new image onto mCompositingFrame.
* See comments in DoComposite() for more information and optimizations.
*
* @par
* The rest of the imgContainerGIF specific functions are used by DoComposite to
* destroy the old frame and build the new one.
*
* @note
* <li> "Mask", "Alpha", and "Alpha Level" are interchangable phrases in
* respects to imgContainerGIF.
*
* @par
* <li> GIFs never have more than a 1 bit alpha.
*
* @par
* <li> GIFs are stored in a 24bit buffer. Although one GIF frame can never
* have more than 256 colors, due to frame disposal methods, one composited
* frame could end up with far more than 256 colors. (In the future each
* frame in mFrames[..] may be 8bit, and the compositing frames 24)
*
* @par
* <li> Background color specified in GIF is ignored by web browsers.
*
* @par
* <li> If Frame 3 wants to dispose by restoring previous, what it wants is to
* restore the composition up to and including Frame 2, as well as Frame 2s
* disposal. So, in the middle of DoComposite when composing Frame 3, right
* after destroying Frame 2's area, we copy mCompositingFrame to
* mPrevCompositingFrame. When DoComposite get's called to do Frame 4, we
* copy mPrevCompositingFrame back, and then draw Frame 4 on top.
*/
class imgContainerGIF : public imgIContainer,
public nsITimerCallback
{
public:
NS_DECL_ISUPPORTS
NS_DECL_IMGICONTAINER
NS_DECL_NSITIMERCALLBACK
imgContainerGIF();
virtual ~imgContainerGIF();
private:
/** "Disposal" method indicates how the image should be handled before the
* subsequent image is displayed.
*/
enum {
DISPOSE_CLEAR_ALL = -1, //!< Clear the whole image, revealing
//! what was there before the gif displayed
DISPOSE_NOT_SPECIFIED = 0, //!< Leave frame, let new frame draw on top
DISPOSE_KEEP = 1, //!< Leave frame, let new frame draw on top
DISPOSE_CLEAR = 2, //!< Clear the frame's area, revealing bg
DISPOSE_RESTORE_PREVIOUS = 3 //!< Restore the previous (composited) frame
};
inline gfxIImageFrame* inlinedGetCurrentFrame() {
if (mLastCompositedFrameIndex == mCurrentAnimationFrameIndex)
return mCompositingFrame;
return mFrames.SafeObjectAt(mCurrentAnimationFrameIndex);
}
/** Function for doing the frame compositing of animations
*
* @param aFrameToUse Set by DoComposite
* (aNextFrame, mCompositingFrame, or mCompositingPrevFrame)
* @param aDirtyRect Area that the display will need to update
* @param aPrevFrame Last Frame seen/processed
* @param aNextFrame Frame we need to incorperate/display
* @param aNextFrameIndex Position of aNextFrame in mFrames list
*/
nsresult DoComposite(gfxIImageFrame** aFrameToUse, nsIntRect* aDirtyRect,
gfxIImageFrame* aPrevFrame,
gfxIImageFrame* aNextFrame,
PRInt32 aNextFrameIndex);
/**
* Combine aOverlayFrame's mask into aCompositingFrame's mask.
*
* This takes the mask information from the passed in aOverlayFrame and
* inserts that information into the aCompositingFrame's mask at the proper
* offsets. It does *not* rebuild the entire mask.
*
* @param aCompositingFrame Target frame
* @param aOverlayFrame This frame's mask is being copied
*/
void BuildCompositeMask(gfxIImageFrame* aCompositingFrame,
gfxIImageFrame* aOverlayFrame);
/** Sets an area of the frame's mask.
*
* @param aFrame Target Frame
* @param aVisible Turn on (PR_TRUE) or off (PR_FALSE) visibility
*
* @note Invisible area of frame's image will need to be set to 0
*/
void SetMaskVisibility(gfxIImageFrame *aFrame, PRBool aVisible);
//! @overload
void SetMaskVisibility(gfxIImageFrame *aFrame,
PRInt32 aX, PRInt32 aY,
PRInt32 aWidth, PRInt32 aHeight,
PRBool aVisible);
//! @overload
void SetMaskVisibility(gfxIImageFrame *aFrame,
nsIntRect &aRect, PRBool aVisible) {
SetMaskVisibility(aFrame, aRect.x, aRect.y,
aRect.width, aRect.height, aVisible);
}
/** Fills an area of <aFrame> with black.
*
* @param aFrame Target Frame
*
* @note Does not set the mask
*/
void BlackenFrame(gfxIImageFrame* aFrame);
//! @overload
void BlackenFrame(gfxIImageFrame* aFrame,
PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight);
//! @overload
inline void BlackenFrame(gfxIImageFrame* aFrame, nsIntRect &aRect) {
BlackenFrame(aFrame, aRect.x, aRect.y, aRect.width, aRect.height);
}
//! Copy one gfxIImageFrame's image and mask into another
static PRBool CopyFrameImage(gfxIImageFrame *aSrcFrame,
gfxIImageFrame *aDstFrame);
//! imgIContainerObserver; used for telling observers that the frame changed
nsWeakPtr mObserver;
//! All the <gfxIImageFrame>s of the GIF
nsCOMArray<gfxIImageFrame> mFrames;
//! Size of GIF (not necessarily the frame)
nsIntSize mSize;
//! Area of the first frame that needs to be redrawn on subsequent loops
nsIntRect mFirstFrameRefreshArea;
PRInt32 mCurrentDecodingFrameIndex; // 0 to numFrames-1
PRInt32 mCurrentAnimationFrameIndex; // 0 to numFrames-1
//! Track the last composited frame for Optimizations (See DoComposite code)
PRInt32 mLastCompositedFrameIndex;
//! Whether we can assume there will be no more frames
//! (and thus loop the animation)
PRBool mDoneDecoding;
//! Are we currently animating the GIF?
PRBool mAnimating;
//! See imgIContainer for mode constants
PRUint16 mAnimationMode;
//! # loops remaining before animation stops (-1 no stop)
PRInt32 mLoopCount;
//! Timer to animate multiframed images
nsCOMPtr<nsITimer> mTimer;
/** For managing blending of frames
*
* Some GIF animations will use the mCompositingFrame to composite images
* and just hand this back to the caller when it is time to draw the frame.
* NOTE: When clearing mCompositingFrame, remember to set
* mLastCompositedFrameIndex to -1. Code assume that if
* mLastCompositedFrameIndex >= 0 then mCompositingFrame exists.
*/
nsCOMPtr<gfxIImageFrame> mCompositingFrame;
/** the previous composited frame, for DISPOSE_RESTORE_PREVIOUS
*
* The Previous Frame (all frames composited up to the current) needs to be
* stored in cases where the GIF specifies it wants the last frame back
* when it's done with the current frame.
*/
nsCOMPtr<gfxIImageFrame> mCompositingPrevFrame;
};
#endif /* __imgContainerGIF_h__ */

Просмотреть файл

@ -492,6 +492,9 @@ int nsGIFDecoder2::HaveDecodedRow(
if (!cmap) { // cmap could have null value if the global color table flag is 0
for (int i = 0; i < aDuplicateCount; ++i) {
#ifdef MOZ_CAIRO_GFX
imgContainerGIF::BlackenFrame(decoder->mImageFrame, 0, aRowNumber+i, width, 1);
#else
if (format == gfxIFormats::RGB_A1 ||
format == gfxIFormats::BGR_A1) {
decoder->mImageFrame->SetAlphaData(nsnull,
@ -499,6 +502,7 @@ int nsGIFDecoder2::HaveDecodedRow(
}
decoder->mImageFrame->SetImageData(nsnull,
bpr, (aRowNumber+i)*bpr);
#endif
}
} else {
PRUint8* rowBufIndex = aRowBufPtr;
@ -602,7 +606,7 @@ int nsGIFDecoder2::HaveDecodedRow(
break;
}
}
#endif
#endif // else !MOZ_CAIRO_GFX
}
decoder->mCurrentRow = aRowNumber + aDuplicateCount - 1;