michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsMimeTypes.h" michael@0: michael@0: #include "Image.h" michael@0: michael@0: namespace mozilla { michael@0: namespace image { michael@0: michael@0: // Constructor michael@0: ImageResource::ImageResource(ImageURL* aURI) : michael@0: mURI(aURI), michael@0: mInnerWindowId(0), michael@0: mAnimationConsumers(0), michael@0: mAnimationMode(kNormalAnimMode), michael@0: mInitialized(false), michael@0: mAnimating(false), michael@0: mError(false) michael@0: { michael@0: } michael@0: michael@0: uint32_t michael@0: ImageResource::SizeOfData() michael@0: { michael@0: if (mError) michael@0: return 0; michael@0: michael@0: // This is not used by memory reporters, but for sizing the cache, which is michael@0: // why it uses |moz_malloc_size_of| rather than a michael@0: // |MOZ_DEFINE_MALLOC_SIZE_OF|. michael@0: return uint32_t(HeapSizeOfSourceWithComputedFallback(moz_malloc_size_of) + michael@0: HeapSizeOfDecodedWithComputedFallback(moz_malloc_size_of) + michael@0: NonHeapSizeOfDecoded() + michael@0: OutOfProcessSizeOfDecoded()); michael@0: } michael@0: michael@0: // Translates a mimetype into a concrete decoder michael@0: Image::eDecoderType michael@0: Image::GetDecoderType(const char *aMimeType) michael@0: { michael@0: // By default we don't know michael@0: eDecoderType rv = eDecoderType_unknown; michael@0: michael@0: // PNG michael@0: if (!strcmp(aMimeType, IMAGE_PNG)) michael@0: rv = eDecoderType_png; michael@0: else if (!strcmp(aMimeType, IMAGE_X_PNG)) michael@0: rv = eDecoderType_png; michael@0: michael@0: // GIF michael@0: else if (!strcmp(aMimeType, IMAGE_GIF)) michael@0: rv = eDecoderType_gif; michael@0: michael@0: michael@0: // JPEG michael@0: else if (!strcmp(aMimeType, IMAGE_JPEG)) michael@0: rv = eDecoderType_jpeg; michael@0: else if (!strcmp(aMimeType, IMAGE_PJPEG)) michael@0: rv = eDecoderType_jpeg; michael@0: else if (!strcmp(aMimeType, IMAGE_JPG)) michael@0: rv = eDecoderType_jpeg; michael@0: michael@0: // BMP michael@0: else if (!strcmp(aMimeType, IMAGE_BMP)) michael@0: rv = eDecoderType_bmp; michael@0: else if (!strcmp(aMimeType, IMAGE_BMP_MS)) michael@0: rv = eDecoderType_bmp; michael@0: michael@0: michael@0: // ICO michael@0: else if (!strcmp(aMimeType, IMAGE_ICO)) michael@0: rv = eDecoderType_ico; michael@0: else if (!strcmp(aMimeType, IMAGE_ICO_MS)) michael@0: rv = eDecoderType_ico; michael@0: michael@0: // Icon michael@0: else if (!strcmp(aMimeType, IMAGE_ICON_MS)) michael@0: rv = eDecoderType_icon; michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: void michael@0: ImageResource::IncrementAnimationConsumers() michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread(), "Main thread only to encourage serialization " michael@0: "with DecrementAnimationConsumers"); michael@0: mAnimationConsumers++; michael@0: } michael@0: michael@0: void michael@0: ImageResource::DecrementAnimationConsumers() michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread(), "Main thread only to encourage serialization " michael@0: "with IncrementAnimationConsumers"); michael@0: NS_ABORT_IF_FALSE(mAnimationConsumers >= 1, "Invalid no. of animation consumers!"); michael@0: mAnimationConsumers--; michael@0: } michael@0: michael@0: nsresult michael@0: ImageResource::GetAnimationModeInternal(uint16_t* aAnimationMode) michael@0: { michael@0: if (mError) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: NS_ENSURE_ARG_POINTER(aAnimationMode); michael@0: michael@0: *aAnimationMode = mAnimationMode; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: ImageResource::SetAnimationModeInternal(uint16_t aAnimationMode) michael@0: { michael@0: if (mError) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: NS_ASSERTION(aAnimationMode == kNormalAnimMode || michael@0: aAnimationMode == kDontAnimMode || michael@0: aAnimationMode == kLoopOnceAnimMode, michael@0: "Wrong Animation Mode is being set!"); michael@0: michael@0: mAnimationMode = aAnimationMode; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: ImageResource::EvaluateAnimation() michael@0: { michael@0: if (!mAnimating && ShouldAnimate()) { michael@0: nsresult rv = StartAnimation(); michael@0: mAnimating = NS_SUCCEEDED(rv); michael@0: } else if (mAnimating && !ShouldAnimate()) { michael@0: StopAnimation(); michael@0: } michael@0: } michael@0: michael@0: } // namespace image michael@0: } // namespace mozilla