Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.  All rights reserved.
      3  * Copyright (C) 2007-2008 Torch Mobile, Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #ifndef ImageSource_h
     28 #define ImageSource_h
     29 
     30 #include <wtf/Forward.h>
     31 #include <wtf/Noncopyable.h>
     32 #include <wtf/Vector.h>
     33 
     34 #if PLATFORM(WX)
     35 class wxBitmap;
     36 class wxGraphicsBitmap;
     37 #elif USE(CG)
     38 typedef struct CGImageSource* CGImageSourceRef;
     39 typedef struct CGImage* CGImageRef;
     40 typedef const struct __CFData* CFDataRef;
     41 #elif PLATFORM(QT)
     42 #include <qglobal.h>
     43 QT_BEGIN_NAMESPACE
     44 class QPixmap;
     45 QT_END_NAMESPACE
     46 #elif USE(CAIRO)
     47 struct _cairo_surface;
     48 typedef struct _cairo_surface cairo_surface_t;
     49 #elif USE(SKIA)
     50 #if PLATFORM(ANDROID)
     51 #include "SkString.h"
     52 class SkBitmapRef;
     53 class PrivateAndroidImageSourceRec;
     54 #else
     55 namespace WebCore {
     56 class NativeImageSkia;
     57 }
     58 #endif
     59 #elif PLATFORM(HAIKU)
     60 class BBitmap;
     61 #elif OS(WINCE)
     62 #include "SharedBitmap.h"
     63 #endif
     64 
     65 namespace WebCore {
     66 
     67 class IntPoint;
     68 class IntSize;
     69 class SharedBuffer;
     70 
     71 #if USE(CG)
     72 #if USE(WEBKIT_IMAGE_DECODERS)
     73 class ImageDecoder;
     74 typedef ImageDecoder* NativeImageSourcePtr;
     75 #else
     76 typedef CGImageSourceRef NativeImageSourcePtr;
     77 #endif
     78 typedef CGImageRef NativeImagePtr;
     79 #elif PLATFORM(OPENVG)
     80 class ImageDecoder;
     81 class TiledImageOpenVG;
     82 typedef ImageDecoder* NativeImageSourcePtr;
     83 typedef TiledImageOpenVG* NativeImagePtr;
     84 #elif PLATFORM(QT)
     85 class ImageDecoderQt;
     86 typedef ImageDecoderQt* NativeImageSourcePtr;
     87 typedef QPixmap* NativeImagePtr;
     88 #elif USE(SKIA) && PLATFORM(ANDROID)
     89 #ifdef ANDROID_ANIMATED_GIF
     90 class GIFImageDecoder;
     91 #endif
     92 struct NativeImageSourcePtr {
     93     SkString m_url;
     94     PrivateAndroidImageSourceRec* m_image;
     95 #ifdef ANDROID_ANIMATED_GIF
     96     GIFImageDecoder* m_gifDecoder;
     97 #endif
     98 };
     99 typedef const Vector<char>* NativeBytePtr;
    100 typedef SkBitmapRef* NativeImagePtr;
    101 #else
    102 class ImageDecoder;
    103 typedef ImageDecoder* NativeImageSourcePtr;
    104 #if PLATFORM(WX)
    105 #if USE(WXGC)
    106 typedef wxGraphicsBitmap* NativeImagePtr;
    107 #else
    108 typedef wxBitmap* NativeImagePtr;
    109 #endif
    110 #elif USE(CAIRO)
    111 typedef cairo_surface_t* NativeImagePtr;
    112 #elif USE(SKIA)
    113 typedef WebCore::NativeImageSkia* NativeImagePtr;
    114 #elif PLATFORM(HAIKU)
    115 typedef BBitmap* NativeImagePtr;
    116 #elif OS(WINCE)
    117 typedef RefPtr<SharedBitmap> NativeImagePtr;
    118 #endif
    119 #endif
    120 
    121 // Right now GIFs are the only recognized image format that supports animation.
    122 // The animation system and the constants below are designed with this in mind.
    123 // GIFs have an optional 16-bit unsigned loop count that describes how an
    124 // animated GIF should be cycled.  If the loop count is absent, the animation
    125 // cycles once; if it is 0, the animation cycles infinitely; otherwise the
    126 // animation plays n + 1 cycles (where n is the specified loop count).  If the
    127 // GIF decoder defaults to cAnimationLoopOnce in the absence of any loop count
    128 // and translates an explicit "0" loop count to cAnimationLoopInfinite, then we
    129 // get a couple of nice side effects:
    130 //   * By making cAnimationLoopOnce be 0, we allow the animation cycling code in
    131 //     BitmapImage.cpp to avoid special-casing it, and simply treat all
    132 //     non-negative loop counts identically.
    133 //   * By making the other two constants negative, we avoid conflicts with any
    134 //     real loop count values.
    135 const int cAnimationLoopOnce = 0;
    136 const int cAnimationLoopInfinite = -1;
    137 const int cAnimationNone = -2;
    138 
    139 class ImageSource {
    140     WTF_MAKE_NONCOPYABLE(ImageSource);
    141 public:
    142     enum AlphaOption {
    143         AlphaPremultiplied,
    144         AlphaNotPremultiplied
    145     };
    146 
    147     enum GammaAndColorProfileOption {
    148         GammaAndColorProfileApplied,
    149         GammaAndColorProfileIgnored
    150     };
    151 
    152     ImageSource(AlphaOption alphaOption = AlphaPremultiplied, GammaAndColorProfileOption gammaAndColorProfileOption = GammaAndColorProfileApplied);
    153     ~ImageSource();
    154 
    155     // Tells the ImageSource that the Image no longer cares about decoded frame
    156     // data -- at all (if |destroyAll| is true), or before frame
    157     // |clearBeforeFrame| (if |destroyAll| is false).  The ImageSource should
    158     // delete cached decoded data for these frames where possible to keep memory
    159     // usage low.  When |destroyAll| is true, the ImageSource should also reset
    160     // any local state so that decoding can begin again.
    161     //
    162     // Implementations that delete less than what's specified above waste
    163     // memory.  Implementations that delete more may burn CPU re-decoding frames
    164     // that could otherwise have been cached, or encounter errors if they're
    165     // asked to decode frames they can't decode due to the loss of previous
    166     // decoded frames.
    167     //
    168     // Callers should not call clear(false, n) and subsequently call
    169     // createFrameAtIndex(m) with m < n, unless they first call clear(true).
    170     // This ensures that stateful ImageSources/decoders will work properly.
    171     //
    172     // The |data| and |allDataReceived| parameters should be supplied by callers
    173     // who set |destroyAll| to true if they wish to be able to continue using
    174     // the ImageSource.  This way implementations which choose to destroy their
    175     // decoders in some cases can reconstruct them correctly.
    176     void clear(bool destroyAll,
    177                size_t clearBeforeFrame = 0,
    178                SharedBuffer* data = NULL,
    179                bool allDataReceived = false);
    180 
    181     bool initialized() const;
    182 
    183     void setData(SharedBuffer* data, bool allDataReceived);
    184     String filenameExtension() const;
    185 
    186     bool isSizeAvailable();
    187     IntSize size() const;
    188     IntSize frameSizeAtIndex(size_t) const;
    189     bool getHotSpot(IntPoint&) const;
    190 
    191     size_t bytesDecodedToDetermineProperties() const;
    192 
    193     int repetitionCount();
    194 
    195     size_t frameCount() const;
    196 
    197     // Callers should not call this after calling clear() with a higher index;
    198     // see comments on clear() above.
    199     NativeImagePtr createFrameAtIndex(size_t);
    200 
    201     float frameDurationAtIndex(size_t);
    202     bool frameHasAlphaAtIndex(size_t); // Whether or not the frame actually used any alpha.
    203     bool frameIsCompleteAtIndex(size_t); // Whether or not the frame is completely decoded.
    204 
    205 #if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
    206     static unsigned maxPixelsPerDecodedImage() { return s_maxPixelsPerDecodedImage; }
    207     static void setMaxPixelsPerDecodedImage(unsigned maxPixels) { s_maxPixelsPerDecodedImage = maxPixels; }
    208 #endif
    209 
    210 #if PLATFORM(ANDROID)
    211     void clearURL();
    212     void setURL(const String& url);
    213 #endif
    214 
    215 private:
    216 #if PLATFORM(ANDROID)
    217     // FIXME: This is protected only to allow ImageSourceSkia to set ICO decoder
    218     // with a preferred size. See ImageSourceSkia.h for discussion.
    219 protected:
    220 #endif
    221     NativeImageSourcePtr m_decoder;
    222     AlphaOption m_alphaOption;
    223     GammaAndColorProfileOption m_gammaAndColorProfileOption;
    224 #if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
    225     static unsigned s_maxPixelsPerDecodedImage;
    226 #endif
    227 };
    228 
    229 }
    230 
    231 #endif
    232