зеркало из https://github.com/mozilla/pjs.git
bug 296818. discard uncompressed image data after a period of time. original patch from Federico Mena-Quintero <federico@ximian.com>. Changes from me. r=vlad
This commit is contained in:
Родитель
137c086687
Коммит
d070896f7a
|
@ -22,6 +22,7 @@
|
|||
*
|
||||
* Contributor(s):
|
||||
* Stuart Parmenter <stuart@mozilla.com>
|
||||
* Federico Mena-Quintero <federico@novell.com>
|
||||
*
|
||||
* 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
|
||||
|
@ -63,8 +64,10 @@ NS_IMPL_ISUPPORTS1(nsJPEGDecoder, imgIDecoder)
|
|||
|
||||
#if defined(PR_LOGGING)
|
||||
PRLogModuleInfo *gJPEGlog = PR_NewLogModule("JPEGDecoder");
|
||||
static PRLogModuleInfo *gJPEGDecoderAccountingLog = PR_NewLogModule("JPEGDecoderAccounting");
|
||||
#else
|
||||
#define gJPEGlog
|
||||
#define gJPEGDecoderAccountingLog
|
||||
#endif
|
||||
|
||||
|
||||
|
@ -96,6 +99,10 @@ nsJPEGDecoder::nsJPEGDecoder()
|
|||
|
||||
mInProfile = nsnull;
|
||||
mTransform = nsnull;
|
||||
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("nsJPEGDecoder::nsJPEGDecoder: Creating JPEG decoder %p",
|
||||
this));
|
||||
}
|
||||
|
||||
nsJPEGDecoder::~nsJPEGDecoder()
|
||||
|
@ -106,6 +113,10 @@ nsJPEGDecoder::~nsJPEGDecoder()
|
|||
cmsDeleteTransform(mTransform);
|
||||
if (mInProfile)
|
||||
cmsCloseProfile(mInProfile);
|
||||
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("nsJPEGDecoder::~nsJPEGDecoder: Destroying JPEG decoder %p",
|
||||
this));
|
||||
}
|
||||
|
||||
|
||||
|
@ -147,6 +158,34 @@ NS_IMETHODIMP nsJPEGDecoder::Init(imgILoad *aLoad)
|
|||
for (PRUint32 m = 0; m < 16; m++)
|
||||
jpeg_save_markers(&mInfo, JPEG_APP0 + m, 0xFFFF);
|
||||
|
||||
|
||||
|
||||
/* Check if the request already has an image container.
|
||||
* this is the case when multipart/x-mixed-replace is being downloaded
|
||||
* if we already have one and it has the same width and height, reuse it.
|
||||
* This is also the case when an existing container is reloading itself from
|
||||
* us.
|
||||
*
|
||||
* If we have a mismatch in width/height for the container later on we will
|
||||
* generate an error.
|
||||
*/
|
||||
mImageLoad->GetImage(getter_AddRefs(mImage));
|
||||
|
||||
if (!mImage) {
|
||||
mImage = do_CreateInstance("@mozilla.org/image/container;1");
|
||||
if (!mImage)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
mImageLoad->SetImage(mImage);
|
||||
nsresult result = mImage->SetDiscardable("image/jpeg");
|
||||
if (NS_FAILED(result)) {
|
||||
mState = JPEG_ERROR;
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
(" (could not set image container to discardable)"));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
@ -185,11 +224,20 @@ NS_IMETHODIMP nsJPEGDecoder::WriteFrom(nsIInputStream *inStr, PRUint32 count, PR
|
|||
{
|
||||
LOG_SCOPE_WITH_PARAM(gJPEGlog, "nsJPEGDecoder::WriteFrom", "count", count);
|
||||
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("nsJPEGDecoder::WriteFrom(decoder = %p) {\n"
|
||||
" image container %s; %u bytes to be added",
|
||||
this,
|
||||
mImage ? "exists" : "does not exist",
|
||||
count));
|
||||
|
||||
if (inStr) {
|
||||
if (!mBuffer) {
|
||||
mBuffer = (JOCTET *)PR_Malloc(count);
|
||||
if (!mBuffer) {
|
||||
mState = JPEG_ERROR;
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("} (out of memory allocating buffer)"));
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
mBufferSize = count;
|
||||
|
@ -197,6 +245,8 @@ NS_IMETHODIMP nsJPEGDecoder::WriteFrom(nsIInputStream *inStr, PRUint32 count, PR
|
|||
JOCTET *buf = (JOCTET *)PR_Realloc(mBuffer, count);
|
||||
if (!buf) {
|
||||
mState = JPEG_ERROR;
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("} (out of memory resizing buffer)"));
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
mBuffer = buf;
|
||||
|
@ -204,9 +254,29 @@ NS_IMETHODIMP nsJPEGDecoder::WriteFrom(nsIInputStream *inStr, PRUint32 count, PR
|
|||
}
|
||||
|
||||
nsresult rv = inStr->Read((char*)mBuffer, count, &mBufferLen);
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv), "nsJPEGDecoder::WriteFrom -- inStr->Read failed");
|
||||
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("nsJPEGDecoder::WriteFrom(): decoder %p got %u bytes, read %u from the stream (buffer size %u)",
|
||||
this,
|
||||
count,
|
||||
mBufferLen,
|
||||
mBufferSize));
|
||||
|
||||
*_retval = mBufferLen;
|
||||
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv), "nsJPEGDecoder::WriteFrom -- inStr->Read failed");
|
||||
nsresult result = mImage->AddRestoreData((char *) mBuffer, count);
|
||||
|
||||
if (NS_FAILED(result)) {
|
||||
mState = JPEG_ERROR;
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("} (could not add restore data)"));
|
||||
return result;
|
||||
}
|
||||
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
(" added %u bytes to restore data",
|
||||
count));
|
||||
}
|
||||
// else no input stream.. Flush() ?
|
||||
|
||||
|
@ -217,11 +287,15 @@ NS_IMETHODIMP nsJPEGDecoder::WriteFrom(nsIInputStream *inStr, PRUint32 count, PR
|
|||
if (error_code == NS_ERROR_FAILURE) {
|
||||
/* Error due to corrupt stream - return NS_OK so that libpr0n
|
||||
doesn't throw away a partial image load */
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("} (setjmp returned NS_ERROR_FAILURE)"));
|
||||
return NS_OK;
|
||||
} else {
|
||||
/* Error due to reasons external to the stream (probably out of
|
||||
memory) - let libpr0n attempt to clean up, even though
|
||||
mozilla is seconds away from falling flat on its face. */
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("} (setjmp returned an error)"));
|
||||
return error_code;
|
||||
}
|
||||
}
|
||||
|
@ -235,8 +309,11 @@ NS_IMETHODIMP nsJPEGDecoder::WriteFrom(nsIInputStream *inStr, PRUint32 count, PR
|
|||
LOG_SCOPE(gJPEGlog, "nsJPEGDecoder::WriteFrom -- entering JPEG_HEADER case");
|
||||
|
||||
/* Step 3: read file parameters with jpeg_read_header() */
|
||||
if (jpeg_read_header(&mInfo, TRUE) == JPEG_SUSPENDED)
|
||||
if (jpeg_read_header(&mInfo, TRUE) == JPEG_SUSPENDED) {
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("} (JPEG_SUSPENDED)"));
|
||||
return NS_OK; /* I/O suspension */
|
||||
}
|
||||
|
||||
JOCTET *profile;
|
||||
PRUint32 profileLength;
|
||||
|
@ -278,6 +355,8 @@ NS_IMETHODIMP nsJPEGDecoder::WriteFrom(nsIInputStream *inStr, PRUint32 count, PR
|
|||
break;
|
||||
default:
|
||||
mState = JPEG_ERROR;
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("} (unknown colorpsace (1))"));
|
||||
return NS_ERROR_UNEXPECTED;
|
||||
}
|
||||
|
||||
|
@ -301,6 +380,8 @@ NS_IMETHODIMP nsJPEGDecoder::WriteFrom(nsIInputStream *inStr, PRUint32 count, PR
|
|||
break;
|
||||
default:
|
||||
mState = JPEG_ERROR;
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("} (unknown colorpsace (2))"));
|
||||
return NS_ERROR_UNEXPECTED;
|
||||
}
|
||||
|
||||
|
@ -336,6 +417,8 @@ NS_IMETHODIMP nsJPEGDecoder::WriteFrom(nsIInputStream *inStr, PRUint32 count, PR
|
|||
break;
|
||||
default:
|
||||
mState = JPEG_ERROR;
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("} (unknown colorpsace (3))"));
|
||||
return NS_ERROR_UNEXPECTED;
|
||||
break;
|
||||
}
|
||||
|
@ -352,30 +435,21 @@ NS_IMETHODIMP nsJPEGDecoder::WriteFrom(nsIInputStream *inStr, PRUint32 count, PR
|
|||
|
||||
mObserver->OnStartDecode(nsnull);
|
||||
|
||||
/* Check if the request already has an image container.
|
||||
this is the case when multipart/x-mixed-replace is being downloaded
|
||||
if we already have one and it has the same width and height, reuse it.
|
||||
/* verify that the width and height of the image are the same as
|
||||
* the container we're about to put things in to.
|
||||
* XXX it might not matter maybe we should just resize the image.
|
||||
*/
|
||||
mImageLoad->GetImage(getter_AddRefs(mImage));
|
||||
if (mImage) {
|
||||
PRInt32 width, height;
|
||||
mImage->GetWidth(&width);
|
||||
mImage->GetHeight(&height);
|
||||
if ((width != (PRInt32)mInfo.image_width) ||
|
||||
(height != (PRInt32)mInfo.image_height)) {
|
||||
mImage = nsnull;
|
||||
}
|
||||
PRInt32 width, height;
|
||||
mImage->GetWidth(&width);
|
||||
mImage->GetHeight(&height);
|
||||
if (width == 0 && height == 0) {
|
||||
mImage->Init(mInfo.image_width, mInfo.image_height, mObserver);
|
||||
} else if ((width != (PRInt32)mInfo.image_width) || (height != (PRInt32)mInfo.image_height)) {
|
||||
mState = JPEG_ERROR;
|
||||
return NS_ERROR_UNEXPECTED;
|
||||
}
|
||||
|
||||
if (!mImage) {
|
||||
mImage = do_CreateInstance("@mozilla.org/image/container;1");
|
||||
if (!mImage) {
|
||||
mState = JPEG_ERROR;
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
mImageLoad->SetImage(mImage);
|
||||
mImage->Init(mInfo.image_width, mInfo.image_height, mObserver);
|
||||
}
|
||||
mImage->Init(mInfo.image_width, mInfo.image_height, mObserver);
|
||||
|
||||
mObserver->OnStartContainer(nsnull, mImage);
|
||||
|
||||
|
@ -400,6 +474,8 @@ NS_IMETHODIMP nsJPEGDecoder::WriteFrom(nsIInputStream *inStr, PRUint32 count, PR
|
|||
mFrame = do_CreateInstance("@mozilla.org/gfx/image/frame;2");
|
||||
if (!mFrame) {
|
||||
mState = JPEG_ERROR;
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("} (could not create image frame)"));
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
|
@ -410,11 +486,17 @@ NS_IMETHODIMP nsJPEGDecoder::WriteFrom(nsIInputStream *inStr, PRUint32 count, PR
|
|||
|
||||
if (NS_FAILED(mFrame->Init(0, 0, mInfo.image_width, mInfo.image_height, format, 24))) {
|
||||
mState = JPEG_ERROR;
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("} (could not initialize image frame)"));
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
mImage->AppendFrame(mFrame);
|
||||
}
|
||||
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
(" JPEGDecoderAccounting: nsJPEGDecoder::WriteFrom -- created image frame with %ux%u pixels",
|
||||
mInfo.image_width, mInfo.image_height));
|
||||
}
|
||||
|
||||
mObserver->OnStartFrame(nsnull, mFrame);
|
||||
mState = JPEG_START_DECOMPRESS;
|
||||
|
@ -435,8 +517,11 @@ NS_IMETHODIMP nsJPEGDecoder::WriteFrom(nsIInputStream *inStr, PRUint32 count, PR
|
|||
mInfo.do_block_smoothing = TRUE;
|
||||
|
||||
/* Step 5: Start decompressor */
|
||||
if (jpeg_start_decompress(&mInfo) == FALSE)
|
||||
if (jpeg_start_decompress(&mInfo) == FALSE) {
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("} (I/O suspension after jpeg_start_decompress())"));
|
||||
return NS_OK; /* I/O suspension */
|
||||
}
|
||||
|
||||
/* If this is a progressive JPEG ... */
|
||||
if (mInfo.buffered_image) {
|
||||
|
@ -452,8 +537,11 @@ NS_IMETHODIMP nsJPEGDecoder::WriteFrom(nsIInputStream *inStr, PRUint32 count, PR
|
|||
{
|
||||
LOG_SCOPE(gJPEGlog, "nsJPEGDecoder::WriteFrom -- JPEG_DECOMPRESS_SEQUENTIAL case");
|
||||
|
||||
if (!OutputScanlines())
|
||||
if (!OutputScanlines()) {
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("} (I/O suspension after OutputScanlines() - SEQUENTIAL)"));
|
||||
return NS_OK; /* I/O suspension */
|
||||
}
|
||||
|
||||
/* If we've completed image output ... */
|
||||
NS_ASSERTION(mInfo.output_scanline == mInfo.output_height, "We didn't process all of the data!");
|
||||
|
@ -485,8 +573,11 @@ NS_IMETHODIMP nsJPEGDecoder::WriteFrom(nsIInputStream *inStr, PRUint32 count, PR
|
|||
(status != JPEG_REACHED_EOI))
|
||||
scan--;
|
||||
|
||||
if (!jpeg_start_output(&mInfo, scan))
|
||||
if (!jpeg_start_output(&mInfo, scan)) {
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("} (I/O suspension after jpeg_start_output() - PROGRESSIVE)"));
|
||||
return NS_OK; /* I/O suspension */
|
||||
}
|
||||
}
|
||||
|
||||
if (mInfo.output_scanline == 0xffffff)
|
||||
|
@ -498,13 +589,18 @@ NS_IMETHODIMP nsJPEGDecoder::WriteFrom(nsIInputStream *inStr, PRUint32 count, PR
|
|||
jpeg_start_output() multiple times for the same scan */
|
||||
mInfo.output_scanline = 0xffffff;
|
||||
}
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("} (I/O suspension after OutputScanlines() - PROGRESSIVE)"));
|
||||
return NS_OK; /* I/O suspension */
|
||||
}
|
||||
|
||||
if (mInfo.output_scanline == mInfo.output_height)
|
||||
{
|
||||
if (!jpeg_finish_output(&mInfo))
|
||||
if (!jpeg_finish_output(&mInfo)) {
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("} (I/O suspension after jpeg_finish_output() - PROGRESSIVE)"));
|
||||
return NS_OK; /* I/O suspension */
|
||||
}
|
||||
|
||||
if (jpeg_input_complete(&mInfo) &&
|
||||
(mInfo.input_scan_number == mInfo.output_scan_number))
|
||||
|
@ -520,15 +616,28 @@ NS_IMETHODIMP nsJPEGDecoder::WriteFrom(nsIInputStream *inStr, PRUint32 count, PR
|
|||
|
||||
case JPEG_DONE:
|
||||
{
|
||||
nsresult result;
|
||||
|
||||
LOG_SCOPE(gJPEGlog, "nsJPEGDecoder::WriteFrom -- entering JPEG_DONE case");
|
||||
|
||||
/* Step 7: Finish decompression */
|
||||
|
||||
if (jpeg_finish_decompress(&mInfo) == FALSE)
|
||||
if (jpeg_finish_decompress(&mInfo) == FALSE) {
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("} (I/O suspension after jpeg_finish_decompress() - DONE)"));
|
||||
return NS_OK; /* I/O suspension */
|
||||
}
|
||||
|
||||
mState = JPEG_SINK_NON_JPEG_TRAILER;
|
||||
|
||||
result = mImage->RestoreDataDone();
|
||||
if (NS_FAILED (result)) {
|
||||
mState = JPEG_ERROR;
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("} (could not mark image container with RestoreDataDone)"));
|
||||
return result;
|
||||
}
|
||||
|
||||
/* we're done dude */
|
||||
break;
|
||||
}
|
||||
|
@ -545,6 +654,8 @@ NS_IMETHODIMP nsJPEGDecoder::WriteFrom(nsIInputStream *inStr, PRUint32 count, PR
|
|||
break;
|
||||
}
|
||||
|
||||
PR_LOG(gJPEGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("} (end of function)"));
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
|
|
@ -124,6 +124,10 @@ public:
|
|||
cmsHTRANSFORM mTransform;
|
||||
|
||||
PRPackedBool mReading;
|
||||
|
||||
private:
|
||||
|
||||
nsresult AddToTmpAccumulateBuffer(JOCTET *src, PRUint32 len);
|
||||
};
|
||||
|
||||
#endif // nsJPEGDecoder_h__
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
* Contributor(s):
|
||||
* Stuart Parmenter <stuart@mozilla.com>
|
||||
* Andrew Smith
|
||||
* Federico Mena-Quintero <federico@novell.com>
|
||||
*
|
||||
* 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
|
||||
|
@ -72,7 +73,8 @@ static void PNGAPI error_callback(png_structp png_ptr, png_const_charp error_msg
|
|||
static void PNGAPI warning_callback(png_structp png_ptr, png_const_charp warning_msg);
|
||||
|
||||
#ifdef PR_LOGGING
|
||||
PRLogModuleInfo *gPNGLog = PR_NewLogModule("PNGDecoder");
|
||||
static PRLogModuleInfo *gPNGLog = PR_NewLogModule("PNGDecoder");
|
||||
static PRLogModuleInfo *gPNGDecoderAccountingLog = PR_NewLogModule("PNGDecoderAccounting");
|
||||
#endif
|
||||
|
||||
NS_IMPL_ISUPPORTS1(nsPNGDecoder, imgIDecoder)
|
||||
|
@ -120,6 +122,12 @@ void nsPNGDecoder::CreateFrame(png_uint_32 x_offset, png_uint_32 y_offset,
|
|||
if (mObserver)
|
||||
mObserver->OnStartFrame(nsnull, mFrame);
|
||||
|
||||
|
||||
PR_LOG(gPNGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("PNGDecoderAccounting: nsPNGDecoder::CreateFrame -- created image frame with %dx%d pixels in container %p",
|
||||
width, height,
|
||||
mImage.get ()));
|
||||
|
||||
mFrameHasNoAlpha = PR_TRUE;
|
||||
}
|
||||
|
||||
|
@ -209,6 +217,25 @@ NS_IMETHODIMP nsPNGDecoder::Init(imgILoad *aLoad)
|
|||
png_set_progressive_read_fn(mPNG, static_cast<png_voidp>(this),
|
||||
info_callback, row_callback, end_callback);
|
||||
|
||||
|
||||
/* The image container may already exist if it is reloading itself from us.
|
||||
* Check that it has the same width/height; otherwise create a new container.
|
||||
*/
|
||||
mImageLoad->GetImage(getter_AddRefs(mImage));
|
||||
if (!mImage) {
|
||||
mImage = do_CreateInstance("@mozilla.org/image/container;1");
|
||||
if (!mImage)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
mImageLoad->SetImage(mImage);
|
||||
if (NS_FAILED(mImage->SetDiscardable("image/png"))) {
|
||||
PR_LOG(gPNGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("PNGDecoderAccounting: info_callback(): failed to set image container %p as discardable",
|
||||
mImage.get()));
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
@ -218,13 +245,28 @@ NS_IMETHODIMP nsPNGDecoder::Close()
|
|||
if (mPNG)
|
||||
png_destroy_read_struct(&mPNG, mInfo ? &mInfo : NULL, NULL);
|
||||
|
||||
if (mImage) { // mImage could be null in the case of an error
|
||||
nsresult result = mImage->RestoreDataDone();
|
||||
if (NS_FAILED(result)) {
|
||||
PR_LOG(gPNGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("PNGDecoderAccounting: nsPNGDecoder::Close(): failure in RestoreDataDone() for image container %p",
|
||||
mImage.get()));
|
||||
|
||||
mError = PR_TRUE;
|
||||
return result;
|
||||
}
|
||||
|
||||
PR_LOG(gPNGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("PNGDecoderAccounting: nsPNGDecoder::Close(): image container %p is now with RestoreDataDone",
|
||||
mImage.get()));
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* void flush (); */
|
||||
NS_IMETHODIMP nsPNGDecoder::Flush()
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
|
@ -250,10 +292,24 @@ static NS_METHOD ReadDataOut(nsIInputStream* in,
|
|||
*writeCount = 0;
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
png_process_data(decoder->mPNG, decoder->mInfo,
|
||||
reinterpret_cast<unsigned char *>(const_cast<char *>(fromRawSegment)), count);
|
||||
|
||||
nsresult result = decoder->mImage->AddRestoreData((char *) fromRawSegment, count);
|
||||
if (NS_FAILED (result)) {
|
||||
PR_LOG(gPNGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("PNGDecoderAccounting: ReadDataOut(): failed to add restore data to image container %p",
|
||||
decoder->mImage.get()));
|
||||
|
||||
decoder->mError = PR_TRUE;
|
||||
*writeCount = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
PR_LOG(gPNGDecoderAccountingLog, PR_LOG_DEBUG,
|
||||
("PNGDecoderAccounting: ReadDataOut(): Added restore data to image container %p",
|
||||
decoder->mImage.get()));
|
||||
|
||||
*writeCount = count;
|
||||
return NS_OK;
|
||||
}
|
||||
|
@ -513,13 +569,18 @@ info_callback(png_structp png_ptr, png_infop info_ptr)
|
|||
if (decoder->mObserver)
|
||||
decoder->mObserver->OnStartDecode(nsnull);
|
||||
|
||||
decoder->mImage = do_CreateInstance("@mozilla.org/image/container;1");
|
||||
if (!decoder->mImage)
|
||||
longjmp(decoder->mPNG->jmpbuf, 5); // NS_ERROR_OUT_OF_MEMORY
|
||||
|
||||
decoder->mImageLoad->SetImage(decoder->mImage);
|
||||
|
||||
decoder->mImage->Init(width, height, decoder->mObserver);
|
||||
/* The image container may already exist if it is reloading itself from us.
|
||||
* Check that it has the same width/height; otherwise create a new container.
|
||||
*/
|
||||
PRInt32 containerWidth, containerHeight;
|
||||
decoder->mImage->GetWidth(&containerWidth);
|
||||
decoder->mImage->GetHeight(&containerHeight);
|
||||
if (containerWidth == 0 && containerHeight == 0) {
|
||||
// the image hasn't been inited yet
|
||||
decoder->mImage->Init(width, height, decoder->mObserver);
|
||||
} else if (containerWidth != width || containerHeight != height) {
|
||||
longjmp(decoder->mPNG->jmpbuf, 5); // NS_ERROR_UNEXPECTED
|
||||
}
|
||||
|
||||
if (decoder->mObserver)
|
||||
decoder->mObserver->OnStartContainer(nsnull, decoder->mImage);
|
||||
|
@ -761,7 +822,7 @@ end_callback(png_structp png_ptr, png_infop info_ptr)
|
|||
}
|
||||
|
||||
decoder->mImage->DecodingComplete();
|
||||
|
||||
|
||||
if (decoder->mObserver) {
|
||||
if (!(decoder->apngFlags & FRAME_HIDDEN))
|
||||
decoder->mObserver->OnStopFrame(nsnull, decoder->mFrame);
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
*
|
||||
* Contributor(s):
|
||||
* Stuart Parmenter <pavlov@netscape.com>
|
||||
* Federico Mena-Quintero <federico@novell.com>
|
||||
*
|
||||
* 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
|
||||
|
@ -144,4 +145,10 @@ interface imgIContainer : nsISupports
|
|||
* @note -1 means forever.
|
||||
*/
|
||||
attribute long loopCount;
|
||||
|
||||
/* Methods to discard uncompressed images and restore them again */
|
||||
[noscript] void setDiscardable(in string aMimeType);
|
||||
[noscript] void addRestoreData([array, size_is(aCount), const] in char data,
|
||||
in unsigned long aCount);
|
||||
[noscript] void restoreDataDone();
|
||||
};
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
* Asko Tontti <atontti@cc.hut.fi>
|
||||
* Arron Mogge <paper@animecity.nu>
|
||||
* Andrew Smith
|
||||
* Federico Mena-Quintero <federico@novell.com>
|
||||
*
|
||||
* 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
|
||||
|
@ -42,23 +43,47 @@
|
|||
|
||||
#include "nsComponentManagerUtils.h"
|
||||
#include "imgIContainerObserver.h"
|
||||
#include "ImageErrors.h"
|
||||
#include "nsIImage.h"
|
||||
#include "imgILoad.h"
|
||||
#include "imgIDecoder.h"
|
||||
#include "imgIDecoderObserver.h"
|
||||
#include "imgContainer.h"
|
||||
#include "nsIInterfaceRequestor.h"
|
||||
#include "nsIInterfaceRequestorUtils.h"
|
||||
#include "nsAutoPtr.h"
|
||||
#include "nsStringStream.h"
|
||||
#include "prmem.h"
|
||||
#include "prlog.h"
|
||||
#include "prenv.h"
|
||||
|
||||
#include "gfxContext.h"
|
||||
|
||||
/* Accounting for compressed data */
|
||||
#if defined(PR_LOGGING)
|
||||
static PRLogModuleInfo *gCompressedImageAccountingLog = PR_NewLogModule ("CompressedImageAccounting");
|
||||
#else
|
||||
#define gCompressedImageAccountingLog
|
||||
#endif
|
||||
|
||||
static int num_containers_with_discardable_data;
|
||||
static PRInt64 num_compressed_image_bytes;
|
||||
|
||||
|
||||
NS_IMPL_ISUPPORTS3(imgContainer, imgIContainer, nsITimerCallback, nsIProperties)
|
||||
|
||||
//******************************************************************************
|
||||
imgContainer::imgContainer() :
|
||||
mSize(0,0),
|
||||
mNumFrames(0),
|
||||
mAnim(nsnull),
|
||||
mAnimationMode(kNormalAnimMode),
|
||||
mLoopCount(-1),
|
||||
mObserver(nsnull)
|
||||
mObserver(nsnull),
|
||||
mDiscardable(PR_FALSE),
|
||||
mDiscarded(PR_FALSE),
|
||||
mRestoreDataDone(PR_FALSE),
|
||||
mDiscardTimer(nsnull)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -67,6 +92,23 @@ imgContainer::~imgContainer()
|
|||
{
|
||||
if (mAnim)
|
||||
delete mAnim;
|
||||
|
||||
if (!mRestoreData.IsEmpty()) {
|
||||
num_containers_with_discardable_data--;
|
||||
num_compressed_image_bytes -= mRestoreData.Length();
|
||||
|
||||
PR_LOG (gCompressedImageAccountingLog, PR_LOG_DEBUG,
|
||||
("CompressedImageAccounting: destroying imgContainer %p. "
|
||||
"Compressed containers: %d, Compressed data bytes: %lld",
|
||||
this,
|
||||
num_containers_with_discardable_data,
|
||||
num_compressed_image_bytes));
|
||||
}
|
||||
|
||||
if (mDiscardTimer) {
|
||||
mDiscardTimer->Cancel ();
|
||||
mDiscardTimer = nsnull;
|
||||
}
|
||||
}
|
||||
|
||||
//******************************************************************************
|
||||
|
@ -124,15 +166,53 @@ NS_IMETHODIMP imgContainer::GetHeight(PRInt32 *aHeight)
|
|||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult imgContainer::GetCurrentFrameNoRef(gfxIImageFrame **aFrame)
|
||||
{
|
||||
nsresult result;
|
||||
|
||||
result = RestoreDiscardedData();
|
||||
if (NS_FAILED (result)) {
|
||||
PR_LOG (gCompressedImageAccountingLog, PR_LOG_DEBUG,
|
||||
("CompressedImageAccounting: imgContainer::GetCurrentFrameNoRef(): error %d in RestoreDiscardedData(); "
|
||||
"returning a null frame from imgContainer %p",
|
||||
result,
|
||||
this));
|
||||
|
||||
*aFrame = nsnull;
|
||||
return result;
|
||||
}
|
||||
|
||||
if (!mAnim)
|
||||
*aFrame = mFrames.SafeObjectAt(0);
|
||||
else if (mAnim->lastCompositedFrameIndex == mAnim->currentAnimationFrameIndex)
|
||||
*aFrame = mAnim->compositingFrame;
|
||||
else
|
||||
*aFrame = mFrames.SafeObjectAt(mAnim->currentAnimationFrameIndex);
|
||||
|
||||
if (!*aFrame)
|
||||
PR_LOG (gCompressedImageAccountingLog, PR_LOG_DEBUG,
|
||||
("CompressedImageAccounting: imgContainer::GetCurrentFrameNoRef(): returning null frame from imgContainer %p "
|
||||
"(no errors when restoring data)",
|
||||
this));
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//******************************************************************************
|
||||
/* readonly attribute gfxIImageFrame currentFrame; */
|
||||
NS_IMETHODIMP imgContainer::GetCurrentFrame(gfxIImageFrame **aCurrentFrame)
|
||||
{
|
||||
nsresult result;
|
||||
|
||||
NS_ASSERTION(aCurrentFrame, "imgContainer::GetCurrentFrame; Invalid Arg");
|
||||
if (!aCurrentFrame)
|
||||
return NS_ERROR_INVALID_POINTER;
|
||||
|
||||
if (!(*aCurrentFrame = inlinedGetCurrentFrame()))
|
||||
result = GetCurrentFrameNoRef (aCurrentFrame);
|
||||
if (NS_FAILED (result))
|
||||
return result;
|
||||
|
||||
if (!*aCurrentFrame)
|
||||
return NS_ERROR_FAILURE;
|
||||
|
||||
NS_ADDREF(*aCurrentFrame);
|
||||
|
@ -148,7 +228,7 @@ NS_IMETHODIMP imgContainer::GetNumFrames(PRUint32 *aNumFrames)
|
|||
if (!aNumFrames)
|
||||
return NS_ERROR_INVALID_ARG;
|
||||
|
||||
*aNumFrames = mFrames.Count();
|
||||
*aNumFrames = mNumFrames;
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
@ -157,16 +237,24 @@ NS_IMETHODIMP imgContainer::GetNumFrames(PRUint32 *aNumFrames)
|
|||
/* gfxIImageFrame getFrameAt (in unsigned long index); */
|
||||
NS_IMETHODIMP imgContainer::GetFrameAt(PRUint32 index, gfxIImageFrame **_retval)
|
||||
{
|
||||
nsresult result;
|
||||
|
||||
NS_ASSERTION(_retval, "imgContainer::GetFrameAt; Invalid Arg");
|
||||
if (!_retval)
|
||||
return NS_ERROR_INVALID_POINTER;
|
||||
|
||||
if (!mFrames.Count()) {
|
||||
if (mNumFrames == 0) {
|
||||
*_retval = nsnull;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_ENSURE_ARG(index < static_cast<PRUint32>(mFrames.Count()));
|
||||
NS_ENSURE_ARG((int) index < mNumFrames);
|
||||
|
||||
result = RestoreDiscardedData ();
|
||||
if (NS_FAILED (result)) {
|
||||
*_retval = nsnull;
|
||||
return result;
|
||||
}
|
||||
|
||||
if (!(*_retval = mFrames[index]))
|
||||
return NS_ERROR_FAILURE;
|
||||
|
@ -183,16 +271,17 @@ NS_IMETHODIMP imgContainer::AppendFrame(gfxIImageFrame *item)
|
|||
NS_ASSERTION(item, "imgContainer::AppendFrame; Invalid Arg");
|
||||
if (!item)
|
||||
return NS_ERROR_INVALID_ARG;
|
||||
|
||||
PRInt32 numFrames = mFrames.Count();
|
||||
|
||||
if (numFrames == 0) {
|
||||
|
||||
if (mFrames.Count () == 0) {
|
||||
// This may not be an animated image, don't do all the animation stuff.
|
||||
mFrames.AppendObject(item);
|
||||
|
||||
mNumFrames++;
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
if (numFrames == 1) {
|
||||
if (mFrames.Count () == 1) {
|
||||
// Now that we got a second frame, initialize animation stuff.
|
||||
if (!ensureAnimExists())
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
@ -216,11 +305,13 @@ NS_IMETHODIMP imgContainer::AppendFrame(gfxIImageFrame *item)
|
|||
itemRect);
|
||||
|
||||
mFrames.AppendObject(item);
|
||||
|
||||
mNumFrames++;
|
||||
|
||||
// If this is our second frame, start the animation.
|
||||
// Must be called after AppendObject because StartAnimation checks for > 1
|
||||
// frame
|
||||
if (numFrames == 1)
|
||||
if (mFrames.Count () == 1)
|
||||
StartAnimation();
|
||||
|
||||
return NS_OK;
|
||||
|
@ -230,6 +321,7 @@ NS_IMETHODIMP imgContainer::AppendFrame(gfxIImageFrame *item)
|
|||
/* void removeFrame (in gfxIImageFrame item); */
|
||||
NS_IMETHODIMP imgContainer::RemoveFrame(gfxIImageFrame *item)
|
||||
{
|
||||
/* Remember to decrement mNumFrames if you implement this */
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
|
@ -253,7 +345,7 @@ NS_IMETHODIMP imgContainer::DecodingComplete(void)
|
|||
mAnim->doneDecoding = PR_TRUE;
|
||||
// If there's only 1 frame, optimize it.
|
||||
// Optimizing animated images is not supported
|
||||
if (mFrames.Count() == 1)
|
||||
if (mNumFrames == 1)
|
||||
mFrames[0]->SetMutable(PR_FALSE);
|
||||
return NS_OK;
|
||||
}
|
||||
|
@ -292,11 +384,11 @@ NS_IMETHODIMP imgContainer::SetAnimationMode(PRUint16 aAnimationMode)
|
|||
break;
|
||||
case kNormalAnimMode:
|
||||
if (mLoopCount != 0 ||
|
||||
(mAnim && (mAnim->currentAnimationFrameIndex + 1 < mFrames.Count())))
|
||||
(mAnim && (mAnim->currentAnimationFrameIndex + 1 < mNumFrames)))
|
||||
StartAnimation();
|
||||
break;
|
||||
case kLoopOnceAnimMode:
|
||||
if (mAnim && (mAnim->currentAnimationFrameIndex + 1 < mFrames.Count()))
|
||||
if (mAnim && (mAnim->currentAnimationFrameIndex + 1 < mNumFrames))
|
||||
StartAnimation();
|
||||
break;
|
||||
}
|
||||
|
@ -312,12 +404,18 @@ NS_IMETHODIMP imgContainer::StartAnimation()
|
|||
(mAnim && (mAnim->timer || mAnim->animating)))
|
||||
return NS_OK;
|
||||
|
||||
if (mFrames.Count() > 1) {
|
||||
if (mNumFrames > 1) {
|
||||
if (!ensureAnimExists())
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
PRInt32 timeout;
|
||||
gfxIImageFrame *currentFrame = inlinedGetCurrentFrame();
|
||||
nsresult result;
|
||||
gfxIImageFrame *currentFrame;
|
||||
|
||||
result = GetCurrentFrameNoRef (¤tFrame);
|
||||
if (NS_FAILED (result))
|
||||
return result;
|
||||
|
||||
if (currentFrame) {
|
||||
currentFrame->GetTimeout(&timeout);
|
||||
if (timeout <= 0) // -1 means display this frame forever
|
||||
|
@ -376,8 +474,15 @@ NS_IMETHODIMP imgContainer::ResetAnimation()
|
|||
mAnim->currentAnimationFrameIndex = 0;
|
||||
// Update display
|
||||
nsCOMPtr<imgIContainerObserver> observer(do_QueryReferent(mObserver));
|
||||
if (observer)
|
||||
if (observer) {
|
||||
nsresult result;
|
||||
|
||||
result = RestoreDiscardedData ();
|
||||
if (NS_FAILED (result))
|
||||
return result;
|
||||
|
||||
observer->FrameChanged(this, mFrames[0], &(mAnim->firstFrameRefreshArea));
|
||||
}
|
||||
|
||||
if (oldAnimating)
|
||||
return StartAnimation();
|
||||
|
@ -411,10 +516,150 @@ NS_IMETHODIMP imgContainer::SetLoopCount(PRInt32 aLoopCount)
|
|||
return NS_OK;
|
||||
}
|
||||
|
||||
static PRBool
|
||||
DiscardingEnabled(void)
|
||||
{
|
||||
static PRBool inited;
|
||||
static PRBool enabled;
|
||||
|
||||
if (!inited) {
|
||||
inited = PR_TRUE;
|
||||
|
||||
enabled = (PR_GetEnv("MOZ_DISABLE_IMAGE_DISCARD") == nsnull);
|
||||
}
|
||||
|
||||
return enabled;
|
||||
}
|
||||
|
||||
//******************************************************************************
|
||||
/* void setDiscardable(in string mime_type); */
|
||||
NS_IMETHODIMP imgContainer::SetDiscardable(const char* aMimeType)
|
||||
{
|
||||
NS_ASSERTION(aMimeType, "imgContainer::SetDiscardable() called with null aMimeType");
|
||||
|
||||
if (!DiscardingEnabled())
|
||||
return NS_OK;
|
||||
|
||||
if (mDiscardable) {
|
||||
NS_WARNING ("imgContainer::SetDiscardable(): cannot change an imgContainer which is already discardable");
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
mDiscardableMimeType.Assign(aMimeType);
|
||||
mDiscardable = PR_TRUE;
|
||||
|
||||
num_containers_with_discardable_data++;
|
||||
PR_LOG (gCompressedImageAccountingLog, PR_LOG_DEBUG,
|
||||
("CompressedImageAccounting: Making imgContainer %p (%s) discardable. "
|
||||
"Compressed containers: %d, Compressed data bytes: %lld",
|
||||
this,
|
||||
aMimeType,
|
||||
num_containers_with_discardable_data,
|
||||
num_compressed_image_bytes));
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//******************************************************************************
|
||||
/* void addRestoreData(in nsIInputStream aInputStream, in unsigned long aCount); */
|
||||
NS_IMETHODIMP imgContainer::AddRestoreData(const char *aBuffer, PRUint32 aCount)
|
||||
{
|
||||
NS_ASSERTION(aBuffer, "imgContainer::AddRestoreData() called with null aBuffer");
|
||||
|
||||
if (!DiscardingEnabled ())
|
||||
return NS_OK;
|
||||
|
||||
if (!mDiscardable) {
|
||||
NS_WARNING ("imgContainer::AddRestoreData() can only be called if SetDiscardable is called first");
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
if (mRestoreDataDone) {
|
||||
/* We are being called from the decoder while the data is being restored
|
||||
* (i.e. we were fully loaded once, then we discarded the image data, then
|
||||
* we are being restored). We don't want to save the compressed data again,
|
||||
* since we already have it.
|
||||
*/
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
if (!mRestoreData.AppendElements(aBuffer, aCount))
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
num_compressed_image_bytes += aCount;
|
||||
|
||||
PR_LOG (gCompressedImageAccountingLog, PR_LOG_DEBUG,
|
||||
("CompressedImageAccounting: Added compressed data to imgContainer %p (%s). "
|
||||
"Compressed containers: %d, Compressed data bytes: %lld",
|
||||
this,
|
||||
mDiscardableMimeType.get(),
|
||||
num_containers_with_discardable_data,
|
||||
num_compressed_image_bytes));
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* Note! buf must be declared as char buf[9]; */
|
||||
// just used for logging and hashing the header
|
||||
static void
|
||||
get_header_str (char *buf, char *data, PRSize data_len)
|
||||
{
|
||||
int i;
|
||||
int n;
|
||||
static char hex[] = "0123456789abcdef";
|
||||
|
||||
n = data_len < 4 ? data_len : 4;
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
buf[i * 2] = hex[(data[i] >> 4) & 0x0f];
|
||||
buf[i * 2 + 1] = hex[data[i] & 0x0f];
|
||||
}
|
||||
|
||||
buf[i * 2] = 0;
|
||||
}
|
||||
|
||||
//******************************************************************************
|
||||
/* void restoreDataDone(); */
|
||||
NS_IMETHODIMP imgContainer::RestoreDataDone (void)
|
||||
{
|
||||
|
||||
if (!DiscardingEnabled ())
|
||||
return NS_OK;
|
||||
|
||||
if (mRestoreDataDone)
|
||||
return NS_OK;
|
||||
|
||||
mRestoreData.Compact();
|
||||
|
||||
mRestoreDataDone = PR_TRUE;
|
||||
|
||||
if (PR_LOG_TEST(gCompressedImageAccountingLog, PR_LOG_DEBUG)) {
|
||||
char buf[9];
|
||||
get_header_str(buf, mRestoreData.Elements(), mRestoreData.Length());
|
||||
PR_LOG (gCompressedImageAccountingLog, PR_LOG_DEBUG,
|
||||
("CompressedImageAccounting: imgContainer::RestoreDataDone() - data is done for container %p (%s), %d real frames (cached as %d frames) - header %p is 0x%s (length %d)",
|
||||
this,
|
||||
mDiscardableMimeType.get(),
|
||||
mFrames.Count (),
|
||||
mNumFrames,
|
||||
mRestoreData.Elements(),
|
||||
buf,
|
||||
mRestoreData.Length()));
|
||||
}
|
||||
|
||||
return ResetDiscardTimer();
|
||||
}
|
||||
|
||||
//******************************************************************************
|
||||
/* void notify(in nsITimer timer); */
|
||||
NS_IMETHODIMP imgContainer::Notify(nsITimer *timer)
|
||||
{
|
||||
nsresult result;
|
||||
|
||||
result = RestoreDiscardedData();
|
||||
if (NS_FAILED (result))
|
||||
return result;
|
||||
|
||||
// This should never happen since the timer is only set up in StartAnimation()
|
||||
// after mAnim is checked to exist.
|
||||
NS_ASSERTION(mAnim, "imgContainer::Notify() called but mAnim is null");
|
||||
|
@ -433,8 +678,7 @@ NS_IMETHODIMP imgContainer::Notify(nsITimer *timer)
|
|||
return NS_OK;
|
||||
}
|
||||
|
||||
PRInt32 numFrames = mFrames.Count();
|
||||
if (!numFrames)
|
||||
if (mNumFrames == 0)
|
||||
return NS_OK;
|
||||
|
||||
gfxIImageFrame *nextFrame = nsnull;
|
||||
|
@ -448,7 +692,7 @@ NS_IMETHODIMP imgContainer::Notify(nsITimer *timer)
|
|||
// finished decoding (see EndFrameDecode)
|
||||
if (mAnim->doneDecoding ||
|
||||
(nextFrameIndex < mAnim->currentDecodingFrameIndex)) {
|
||||
if (numFrames == nextFrameIndex) {
|
||||
if (mNumFrames == nextFrameIndex) {
|
||||
// End of Animation
|
||||
|
||||
// If animation mode is "loop once", it's time to stop animating
|
||||
|
@ -906,3 +1150,308 @@ NS_IMETHODIMP imgContainer::GetKeys(PRUint32 *count, char ***keys)
|
|||
}
|
||||
return mProperties->GetKeys(count, keys);
|
||||
}
|
||||
|
||||
static int
|
||||
get_discard_timer_ms (void)
|
||||
{
|
||||
/* FIXME: don't hardcode this */
|
||||
return 45000; /* 45 seconds */
|
||||
}
|
||||
|
||||
void
|
||||
imgContainer::sDiscardTimerCallback(nsITimer *aTimer, void *aClosure)
|
||||
{
|
||||
imgContainer *self = (imgContainer *) aClosure;
|
||||
int old_frame_count;
|
||||
|
||||
NS_ASSERTION(aTimer == self->mDiscardTimer,
|
||||
"imgContainer::DiscardTimerCallback() got a callback for an unknown timer");
|
||||
|
||||
self->mDiscardTimer = nsnull;
|
||||
|
||||
old_frame_count = self->mFrames.Count();
|
||||
|
||||
if (self->mAnim) {
|
||||
delete self->mAnim;
|
||||
self->mAnim = nsnull;
|
||||
}
|
||||
|
||||
self->mFrames.Clear();
|
||||
|
||||
self->mDiscarded = PR_TRUE;
|
||||
|
||||
PR_LOG(gCompressedImageAccountingLog, PR_LOG_DEBUG,
|
||||
("CompressedImageAccounting: discarded uncompressed image data from imgContainer %p (%s) - %d frames (cached count: %d); "
|
||||
"Compressed containers: %d, Compressed data bytes: %lld",
|
||||
self,
|
||||
self->mDiscardableMimeType.get(),
|
||||
old_frame_count,
|
||||
self->mNumFrames,
|
||||
num_containers_with_discardable_data,
|
||||
num_compressed_image_bytes));
|
||||
}
|
||||
|
||||
nsresult
|
||||
imgContainer::ResetDiscardTimer (void)
|
||||
{
|
||||
if (!DiscardingEnabled())
|
||||
return NS_OK;
|
||||
|
||||
if (!mDiscardTimer) {
|
||||
mDiscardTimer = do_CreateInstance("@mozilla.org/timer;1");
|
||||
|
||||
if (!mDiscardTimer)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
} else {
|
||||
if (NS_FAILED(mDiscardTimer->Cancel()))
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
return mDiscardTimer->InitWithFuncCallback(sDiscardTimerCallback,
|
||||
(void *) this,
|
||||
get_discard_timer_ms (),
|
||||
nsITimer::TYPE_ONE_SHOT);
|
||||
}
|
||||
|
||||
nsresult
|
||||
imgContainer::RestoreDiscardedData(void)
|
||||
{
|
||||
nsresult result;
|
||||
int num_expected_frames;
|
||||
|
||||
if (!mDiscardable)
|
||||
return NS_OK;
|
||||
|
||||
result = ResetDiscardTimer();
|
||||
if (NS_FAILED (result))
|
||||
return result;
|
||||
|
||||
if (!mDiscarded)
|
||||
return NS_OK;
|
||||
|
||||
num_expected_frames = mNumFrames;
|
||||
|
||||
result = ReloadImages ();
|
||||
if (NS_FAILED (result)) {
|
||||
PR_LOG (gCompressedImageAccountingLog, PR_LOG_DEBUG,
|
||||
("CompressedImageAccounting: imgContainer::RestoreDiscardedData() for container %p failed to ReloadImages()",
|
||||
this));
|
||||
return result;
|
||||
}
|
||||
|
||||
mDiscarded = PR_FALSE;
|
||||
|
||||
NS_ASSERTION (mNumFrames == mFrames.Count(),
|
||||
"number of restored image frames doesn't match");
|
||||
NS_ASSERTION (num_expected_frames == mNumFrames,
|
||||
"number of restored image frames doesn't match the original number of frames!");
|
||||
|
||||
PR_LOG (gCompressedImageAccountingLog, PR_LOG_DEBUG,
|
||||
("CompressedImageAccounting: imgContainer::RestoreDiscardedData() restored discarded data "
|
||||
"for imgContainer %p (%s) - %d image frames. "
|
||||
"Compressed containers: %d, Compressed data bytes: %lld",
|
||||
this,
|
||||
mDiscardableMimeType.get(),
|
||||
mNumFrames,
|
||||
num_containers_with_discardable_data,
|
||||
num_compressed_image_bytes));
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
class ContainerLoader : public imgILoad,
|
||||
public imgIDecoderObserver,
|
||||
public nsSupportsWeakReference
|
||||
{
|
||||
public:
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
NS_DECL_IMGILOAD
|
||||
NS_DECL_IMGIDECODEROBSERVER
|
||||
NS_DECL_IMGICONTAINEROBSERVER
|
||||
|
||||
ContainerLoader(void);
|
||||
|
||||
private:
|
||||
|
||||
imgIContainer *mContainer;
|
||||
};
|
||||
|
||||
NS_IMPL_ISUPPORTS4 (ContainerLoader, imgILoad, imgIDecoderObserver, imgIContainerObserver, nsISupportsWeakReference)
|
||||
|
||||
ContainerLoader::ContainerLoader (void)
|
||||
{
|
||||
}
|
||||
|
||||
/* Implement imgILoad::image getter */
|
||||
NS_IMETHODIMP
|
||||
ContainerLoader::GetImage(imgIContainer **aImage)
|
||||
{
|
||||
*aImage = mContainer;
|
||||
NS_IF_ADDREF (*aImage);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* Implement imgILoad::image setter */
|
||||
NS_IMETHODIMP
|
||||
ContainerLoader::SetImage(imgIContainer *aImage)
|
||||
{
|
||||
mContainer = aImage;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* Implement imgILoad::isMultiPartChannel getter */
|
||||
NS_IMETHODIMP
|
||||
ContainerLoader::GetIsMultiPartChannel(PRBool *aIsMultiPartChannel)
|
||||
{
|
||||
*aIsMultiPartChannel = PR_FALSE; /* FIXME: is this always right? */
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* Implement imgIDecoderObserver::onStartRequest() */
|
||||
NS_IMETHODIMP
|
||||
ContainerLoader::OnStartRequest(imgIRequest *aRequest)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* Implement imgIDecoderObserver::onStartDecode() */
|
||||
NS_IMETHODIMP
|
||||
ContainerLoader::OnStartDecode(imgIRequest *aRequest)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* Implement imgIDecoderObserver::onStartContainer() */
|
||||
NS_IMETHODIMP
|
||||
ContainerLoader::OnStartContainer(imgIRequest *aRequest, imgIContainer *aContainer)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* Implement imgIDecoderObserver::onStartFrame() */
|
||||
NS_IMETHODIMP
|
||||
ContainerLoader::OnStartFrame(imgIRequest *aRequest, gfxIImageFrame *aFrame)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* Implement imgIDecoderObserver::onDataAvailable() */
|
||||
NS_IMETHODIMP
|
||||
ContainerLoader::OnDataAvailable(imgIRequest *aRequest, gfxIImageFrame *aFrame, const nsIntRect * aRect)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* Implement imgIDecoderObserver::onStopFrame() */
|
||||
NS_IMETHODIMP
|
||||
ContainerLoader::OnStopFrame(imgIRequest *aRequest, gfxIImageFrame *aFrame)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* Implement imgIDecoderObserver::onStopContainer() */
|
||||
NS_IMETHODIMP
|
||||
ContainerLoader::OnStopContainer(imgIRequest *aRequest, imgIContainer *aContainer)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* Implement imgIDecoderObserver::onStopDecode() */
|
||||
NS_IMETHODIMP
|
||||
ContainerLoader::OnStopDecode(imgIRequest *aRequest, nsresult status, const PRUnichar *statusArg)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* Implement imgIDecoderObserver::onStopRequest() */
|
||||
NS_IMETHODIMP
|
||||
ContainerLoader::OnStopRequest(imgIRequest *aRequest, PRBool aIsLastPart)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* implement imgIContainerObserver::frameChanged() */
|
||||
NS_IMETHODIMP
|
||||
ContainerLoader::FrameChanged(imgIContainer *aContainer, gfxIImageFrame *aFrame, nsIntRect * aDirtyRect)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
imgContainer::ReloadImages(void)
|
||||
{
|
||||
nsresult result = NS_ERROR_FAILURE;
|
||||
nsCOMPtr<nsIInputStream> stream;
|
||||
|
||||
NS_ASSERTION(!mRestoreData.IsEmpty(),
|
||||
"imgContainer::ReloadImages(): mRestoreData should not be empty");
|
||||
NS_ASSERTION(mRestoreDataDone,
|
||||
"imgContainer::ReloadImages(): mRestoreDataDone shoudl be true!");
|
||||
|
||||
mNumFrames = 0;
|
||||
NS_ASSERTION(mFrames.Count() == 0,
|
||||
"imgContainer::ReloadImages(): mFrames should be empty");
|
||||
|
||||
nsCAutoString decoderCID(NS_LITERAL_CSTRING("@mozilla.org/image/decoder;2?type=") + mDiscardableMimeType);
|
||||
|
||||
nsCOMPtr<imgIDecoder> decoder = do_CreateInstance(decoderCID.get());
|
||||
if (!decoder) {
|
||||
PR_LOG(gCompressedImageAccountingLog, PR_LOG_WARNING,
|
||||
("CompressedImageAccounting: imgContainer::ReloadImages() could not create decoder for %s",
|
||||
mDiscardableMimeType.get()));
|
||||
return NS_IMAGELIB_ERROR_NO_DECODER;
|
||||
}
|
||||
|
||||
nsCOMPtr<imgILoad> loader = new ContainerLoader();
|
||||
if (!loader) {
|
||||
PR_LOG(gCompressedImageAccountingLog, PR_LOG_WARNING,
|
||||
("CompressedImageAccounting: imgContainer::ReloadImages() could not allocate ContainerLoader "
|
||||
"when reloading the images for container %p",
|
||||
this));
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
loader->SetImage(this);
|
||||
|
||||
result = decoder->Init(loader);
|
||||
if (NS_FAILED(result)) {
|
||||
PR_LOG(gCompressedImageAccountingLog, PR_LOG_WARNING,
|
||||
("CompressedImageAccounting: imgContainer::ReloadImages() image container %p "
|
||||
"failed to initialize the decoder (%s)",
|
||||
this,
|
||||
mDiscardableMimeType.get()));
|
||||
return result;
|
||||
}
|
||||
|
||||
result = NS_NewByteInputStream(getter_AddRefs(stream), mRestoreData.Elements(), mRestoreData.Length(), NS_ASSIGNMENT_DEPEND);
|
||||
NS_ENSURE_SUCCESS(result, result);
|
||||
|
||||
if (PR_LOG_TEST(gCompressedImageAccountingLog, PR_LOG_DEBUG)) {
|
||||
char buf[9];
|
||||
get_header_str(buf, mRestoreData.Elements(), mRestoreData.Length());
|
||||
PR_LOG(gCompressedImageAccountingLog, PR_LOG_WARNING,
|
||||
("CompressedImageAccounting: imgContainer::ReloadImages() starting to restore images for container %p (%s) - "
|
||||
"header %p is 0x%s (length %d)",
|
||||
this,
|
||||
mDiscardableMimeType.get(),
|
||||
mRestoreData.Elements(),
|
||||
buf,
|
||||
mRestoreData.Length()));
|
||||
}
|
||||
|
||||
PRUint32 written;
|
||||
result = decoder->WriteFrom(stream, mRestoreData.Length(), &written);
|
||||
NS_ENSURE_SUCCESS(result, result);
|
||||
|
||||
if (NS_FAILED(decoder->Flush()))
|
||||
return result;
|
||||
|
||||
result = decoder->Close();
|
||||
NS_ENSURE_SUCCESS(result, result);
|
||||
|
||||
NS_ASSERTION(mFrames.Count() == mNumFrames,
|
||||
"imgContainer::ReloadImages(): the restored mFrames.Count() doesn't match mNumFrames!");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
* Contributor(s):
|
||||
* Stuart Parmenter <pavlov@netscape.com>
|
||||
* Chris Saari <saari@netscape.com>
|
||||
* Federico Mena-Quintero <federico@novell.com>
|
||||
*
|
||||
* 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
|
||||
|
@ -58,6 +59,7 @@
|
|||
#include "nsIProperties.h"
|
||||
#include "nsITimer.h"
|
||||
#include "nsWeakReference.h"
|
||||
#include "nsTArray.h"
|
||||
|
||||
#define NS_IMGCONTAINER_CID \
|
||||
{ /* 27f0682c-ff64-4dd2-ae7a-668e59f2fd38 */ \
|
||||
|
@ -192,14 +194,8 @@ private:
|
|||
timer->Cancel();
|
||||
}
|
||||
};
|
||||
|
||||
inline gfxIImageFrame* inlinedGetCurrentFrame() {
|
||||
if (!mAnim)
|
||||
return mFrames.SafeObjectAt(0);
|
||||
if (mAnim->lastCompositedFrameIndex == mAnim->currentAnimationFrameIndex)
|
||||
return mAnim->compositingFrame;
|
||||
return mFrames.SafeObjectAt(mAnim->currentAnimationFrameIndex);
|
||||
}
|
||||
|
||||
nsresult GetCurrentFrameNoRef(gfxIImageFrame** aFrame);
|
||||
|
||||
inline Anim* ensureAnimExists() {
|
||||
if (!mAnim)
|
||||
|
@ -283,10 +279,15 @@ private:
|
|||
nsIntSize mSize;
|
||||
|
||||
//! All the <gfxIImageFrame>s of the PNG
|
||||
// *** IMPORTANT: if you use mFrames in a method, call RestoreDiscardedData() first to ensure
|
||||
// that the frames actually exist (they may have been discarded to save memory).
|
||||
nsCOMArray<gfxIImageFrame> mFrames;
|
||||
int mNumFrames; /* stored separately from mFrames.Count() to support discarded images */
|
||||
|
||||
nsCOMPtr<nsIProperties> mProperties;
|
||||
|
||||
|
||||
// *** IMPORTANT: if you use mAnim in a method, call RestoreDiscardedData() first to ensure
|
||||
// that the frames actually exist (they may have been discarded to save memory).
|
||||
imgContainer::Anim* mAnim;
|
||||
|
||||
//! See imgIContainer for mode constants
|
||||
|
@ -297,6 +298,19 @@ private:
|
|||
|
||||
//! imgIContainerObserver
|
||||
nsWeakPtr mObserver;
|
||||
|
||||
PRBool mDiscardable;
|
||||
PRBool mDiscarded;
|
||||
nsCString mDiscardableMimeType;
|
||||
|
||||
nsTArray<char> mRestoreData;
|
||||
PRBool mRestoreDataDone;
|
||||
nsCOMPtr<nsITimer> mDiscardTimer;
|
||||
|
||||
nsresult ResetDiscardTimer (void);
|
||||
nsresult RestoreDiscardedData (void);
|
||||
nsresult ReloadImages (void);
|
||||
static void sDiscardTimerCallback (nsITimer *aTimer, void *aClosure);
|
||||
};
|
||||
|
||||
#endif /* __imgContainer_h__ */
|
||||
|
|
Загрузка…
Ссылка в новой задаче