image/src/Image.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/image/src/Image.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,212 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef MOZILLA_IMAGELIB_IMAGE_H_
    1.10 +#define MOZILLA_IMAGELIB_IMAGE_H_
    1.11 +
    1.12 +#include "mozilla/MemoryReporting.h"
    1.13 +#include "imgIContainer.h"
    1.14 +#include "imgStatusTracker.h"
    1.15 +#include "ImageURL.h"
    1.16 +
    1.17 +class nsIRequest;
    1.18 +class nsIInputStream;
    1.19 +
    1.20 +namespace mozilla {
    1.21 +namespace image {
    1.22 +
    1.23 +class Image : public imgIContainer
    1.24 +{
    1.25 +public:
    1.26 +  // Mimetype translation
    1.27 +  enum eDecoderType {
    1.28 +    eDecoderType_png     = 0,
    1.29 +    eDecoderType_gif     = 1,
    1.30 +    eDecoderType_jpeg    = 2,
    1.31 +    eDecoderType_bmp     = 3,
    1.32 +    eDecoderType_ico     = 4,
    1.33 +    eDecoderType_icon    = 5,
    1.34 +    eDecoderType_unknown = 6
    1.35 +  };
    1.36 +  static eDecoderType GetDecoderType(const char *aMimeType);
    1.37 +
    1.38 +  /**
    1.39 +   * Flags for Image initialization.
    1.40 +   *
    1.41 +   * Meanings:
    1.42 +   *
    1.43 +   * INIT_FLAG_NONE: Lack of flags
    1.44 +   *
    1.45 +   * INIT_FLAG_DISCARDABLE: The container should be discardable
    1.46 +   *
    1.47 +   * INIT_FLAG_DECODE_ON_DRAW: The container should decode on draw rather than
    1.48 +   * decoding on load.
    1.49 +   *
    1.50 +   * INIT_FLAG_MULTIPART: The container will be used to display a stream of
    1.51 +   * images in a multipart channel. If this flag is set, INIT_FLAG_DISCARDABLE
    1.52 +   * and INIT_FLAG_DECODE_ON_DRAW must not be set.
    1.53 +   */
    1.54 +  static const uint32_t INIT_FLAG_NONE           = 0x0;
    1.55 +  static const uint32_t INIT_FLAG_DISCARDABLE    = 0x1;
    1.56 +  static const uint32_t INIT_FLAG_DECODE_ON_DRAW = 0x2;
    1.57 +  static const uint32_t INIT_FLAG_MULTIPART      = 0x4;
    1.58 +
    1.59 +  /**
    1.60 +   * Creates a new image container.
    1.61 +   *
    1.62 +   * @param aMimeType The mimetype of the image.
    1.63 +   * @param aFlags Initialization flags of the INIT_FLAG_* variety.
    1.64 +   */
    1.65 +  virtual nsresult Init(const char* aMimeType,
    1.66 +                        uint32_t aFlags) = 0;
    1.67 +
    1.68 +  virtual already_AddRefed<imgStatusTracker> GetStatusTracker() = 0;
    1.69 +  virtual void SetStatusTracker(imgStatusTracker* aStatusTracker) {}
    1.70 +
    1.71 +  /**
    1.72 +   * The rectangle defining the location and size of the given frame.
    1.73 +   */
    1.74 +  virtual nsIntRect FrameRect(uint32_t aWhichFrame) = 0;
    1.75 +
    1.76 +  /**
    1.77 +   * The size, in bytes, occupied by the significant data portions of the image.
    1.78 +   * This includes both compressed source data and decoded frames.
    1.79 +   */
    1.80 +  virtual uint32_t SizeOfData() = 0;
    1.81 +
    1.82 +  /**
    1.83 +   * The components that make up SizeOfData().
    1.84 +   */
    1.85 +  virtual size_t HeapSizeOfSourceWithComputedFallback(mozilla::MallocSizeOf aMallocSizeOf) const = 0;
    1.86 +  virtual size_t HeapSizeOfDecodedWithComputedFallback(mozilla::MallocSizeOf aMallocSizeOf) const = 0;
    1.87 +  virtual size_t NonHeapSizeOfDecoded() const = 0;
    1.88 +  virtual size_t OutOfProcessSizeOfDecoded() const = 0;
    1.89 +
    1.90 +  virtual void IncrementAnimationConsumers() = 0;
    1.91 +  virtual void DecrementAnimationConsumers() = 0;
    1.92 +#ifdef DEBUG
    1.93 +  virtual uint32_t GetAnimationConsumers() = 0;
    1.94 +#endif
    1.95 +
    1.96 +  /**
    1.97 +   * Called from OnDataAvailable when the stream associated with the image has
    1.98 +   * received new image data. The arguments are the same as OnDataAvailable's,
    1.99 +   * but by separating this functionality into a different method we don't
   1.100 +   * interfere with subclasses which wish to implement nsIStreamListener.
   1.101 +   *
   1.102 +   * Images should not do anything that could send out notifications until they
   1.103 +   * have received their first OnImageDataAvailable notification; in
   1.104 +   * particular, this means that instantiating decoders should be deferred
   1.105 +   * until OnImageDataAvailable is called.
   1.106 +   */
   1.107 +  virtual nsresult OnImageDataAvailable(nsIRequest* aRequest,
   1.108 +                                        nsISupports* aContext,
   1.109 +                                        nsIInputStream* aInStr,
   1.110 +                                        uint64_t aSourceOffset,
   1.111 +                                        uint32_t aCount) = 0;
   1.112 +
   1.113 +  /**
   1.114 +   * Called from OnStopRequest when the image's underlying request completes.
   1.115 +   *
   1.116 +   * @param aRequest  The completed request.
   1.117 +   * @param aContext  Context from Necko's OnStopRequest.
   1.118 +   * @param aStatus   A success or failure code.
   1.119 +   * @param aLastPart Whether this is the final part of the underlying request.
   1.120 +   */
   1.121 +  virtual nsresult OnImageDataComplete(nsIRequest* aRequest,
   1.122 +                                       nsISupports* aContext,
   1.123 +                                       nsresult aStatus,
   1.124 +                                       bool aLastPart) = 0;
   1.125 +
   1.126 +  /**
   1.127 +   * Called for multipart images to allow for any necessary reinitialization
   1.128 +   * when there's a new part to add.
   1.129 +   */
   1.130 +  virtual nsresult OnNewSourceData() = 0;
   1.131 +
   1.132 +  virtual void SetInnerWindowID(uint64_t aInnerWindowId) = 0;
   1.133 +  virtual uint64_t InnerWindowID() const = 0;
   1.134 +
   1.135 +  virtual bool HasError() = 0;
   1.136 +  virtual void SetHasError() = 0;
   1.137 +
   1.138 +  virtual ImageURL* GetURI() = 0;
   1.139 +};
   1.140 +
   1.141 +class ImageResource : public Image
   1.142 +{
   1.143 +public:
   1.144 +  already_AddRefed<imgStatusTracker> GetStatusTracker() MOZ_OVERRIDE {
   1.145 +    nsRefPtr<imgStatusTracker> statusTracker = mStatusTracker;
   1.146 +    MOZ_ASSERT(statusTracker);
   1.147 +    return statusTracker.forget();
   1.148 +  }
   1.149 +  void SetStatusTracker(imgStatusTracker* aStatusTracker) MOZ_OVERRIDE MOZ_FINAL {
   1.150 +    MOZ_ASSERT(aStatusTracker);
   1.151 +    MOZ_ASSERT(!mStatusTracker);
   1.152 +    mStatusTracker = aStatusTracker;
   1.153 +  }
   1.154 +  virtual uint32_t SizeOfData() MOZ_OVERRIDE;
   1.155 +
   1.156 +  virtual void IncrementAnimationConsumers() MOZ_OVERRIDE;
   1.157 +  virtual void DecrementAnimationConsumers() MOZ_OVERRIDE;
   1.158 +#ifdef DEBUG
   1.159 +  virtual uint32_t GetAnimationConsumers() MOZ_OVERRIDE { return mAnimationConsumers; }
   1.160 +#endif
   1.161 +
   1.162 +  virtual void SetInnerWindowID(uint64_t aInnerWindowId) MOZ_OVERRIDE {
   1.163 +    mInnerWindowId = aInnerWindowId;
   1.164 +  }
   1.165 +  virtual uint64_t InnerWindowID() const MOZ_OVERRIDE { return mInnerWindowId; }
   1.166 +
   1.167 +  virtual bool HasError() MOZ_OVERRIDE    { return mError; }
   1.168 +  virtual void SetHasError() MOZ_OVERRIDE { mError = true; }
   1.169 +
   1.170 +  /*
   1.171 +   * Returns a non-AddRefed pointer to the URI associated with this image.
   1.172 +   * Illegal to use off-main-thread.
   1.173 +   */
   1.174 +  virtual ImageURL* GetURI() MOZ_OVERRIDE { return mURI.get(); }
   1.175 +
   1.176 +protected:
   1.177 +  ImageResource(ImageURL* aURI);
   1.178 +
   1.179 +  // Shared functionality for implementors of imgIContainer. Every
   1.180 +  // implementation of attribute animationMode should forward here.
   1.181 +  nsresult GetAnimationModeInternal(uint16_t *aAnimationMode);
   1.182 +  nsresult SetAnimationModeInternal(uint16_t aAnimationMode);
   1.183 +
   1.184 +  /**
   1.185 +   * Decides whether animation should or should not be happening,
   1.186 +   * and makes sure the right thing is being done.
   1.187 +   */
   1.188 +  virtual void EvaluateAnimation();
   1.189 +
   1.190 +  /**
   1.191 +   * Extended by child classes, if they have additional
   1.192 +   * conditions for being able to animate.
   1.193 +   */
   1.194 +  virtual bool ShouldAnimate() {
   1.195 +    return mAnimationConsumers > 0 && mAnimationMode != kDontAnimMode;
   1.196 +  }
   1.197 +
   1.198 +  virtual nsresult StartAnimation() = 0;
   1.199 +  virtual nsresult StopAnimation() = 0;
   1.200 +
   1.201 +  // Member data shared by all implementations of this abstract class
   1.202 +  nsRefPtr<imgStatusTracker>    mStatusTracker;
   1.203 +  nsRefPtr<ImageURL>            mURI;
   1.204 +  uint64_t                      mInnerWindowId;
   1.205 +  uint32_t                      mAnimationConsumers;
   1.206 +  uint16_t                      mAnimationMode; // Enum values in imgIContainer
   1.207 +  bool                          mInitialized:1; // Have we been initalized?
   1.208 +  bool                          mAnimating:1;   // Are we currently animating?
   1.209 +  bool                          mError:1;       // Error handling
   1.210 +};
   1.211 +
   1.212 +} // namespace image
   1.213 +} // namespace mozilla
   1.214 +
   1.215 +#endif // MOZILLA_IMAGELIB_IMAGE_H_

mercurial