ImageSource.h revision 058ccc7ba0a4d59b9f6e92808332aa9895425fc7
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/Noncopyable.h> 31#include <wtf/Vector.h> 32 33#if PLATFORM(WX) 34class wxBitmap; 35class wxGraphicsBitmap; 36#elif PLATFORM(CG) 37typedef struct CGImageSource* CGImageSourceRef; 38typedef struct CGImage* CGImageRef; 39typedef const struct __CFData* CFDataRef; 40#elif PLATFORM(QT) 41#include <qglobal.h> 42QT_BEGIN_NAMESPACE 43class QPixmap; 44QT_END_NAMESPACE 45#elif PLATFORM(CAIRO) 46struct _cairo_surface; 47typedef struct _cairo_surface cairo_surface_t; 48#elif PLATFORM(ANDROID) && PLATFORM(SGL) 49#include "SkString.h" 50class SkBitmapRef; 51class PrivateAndroidImageSourceRec; 52#elif PLATFORM(SKIA) 53class NativeImageSkia; 54#elif PLATFORM(WINCE) 55#include "SharedBitmap.h" 56#endif 57 58namespace WebCore { 59 60class IntSize; 61class SharedBuffer; 62class String; 63 64#if PLATFORM(WX) 65class ImageDecoder; 66typedef ImageDecoder* NativeImageSourcePtr; 67typedef const Vector<char>* NativeBytePtr; 68#if USE(WXGC) 69typedef wxGraphicsBitmap* NativeImagePtr; 70#else 71typedef wxBitmap* NativeImagePtr; 72#endif 73#elif PLATFORM(CG) 74typedef CGImageSourceRef NativeImageSourcePtr; 75typedef CGImageRef NativeImagePtr; 76#elif PLATFORM(QT) 77class ImageDecoderQt; 78typedef ImageDecoderQt* NativeImageSourcePtr; 79typedef QPixmap* NativeImagePtr; 80#elif PLATFORM(ANDROID) 81#if PLATFORM(SGL) 82class String; 83#ifdef ANDROID_ANIMATED_GIF 84class ImageDecoder; 85#endif 86struct NativeImageSourcePtr { 87 SkString m_url; 88 PrivateAndroidImageSourceRec* m_image; 89#ifdef ANDROID_ANIMATED_GIF 90 ImageDecoder* m_gifDecoder; 91#endif 92}; 93typedef const Vector<char>* NativeBytePtr; 94typedef SkBitmapRef* NativeImagePtr; 95#elif PLATFORM(SKIA) // ANDROID 96class ImageDecoder; 97typedef ImageDecoder* NativeImageSourcePtr; 98typedef NativeImageSkia* NativeImagePtr; 99#endif 100#elif PLATFORM(CAIRO) 101class ImageDecoder; 102typedef ImageDecoder* NativeImageSourcePtr; 103typedef cairo_surface_t* NativeImagePtr; 104#elif PLATFORM(SKIA) 105class ImageDecoder; 106typedef ImageDecoder* NativeImageSourcePtr; 107typedef NativeImageSkia* NativeImagePtr; 108#elif PLATFORM(WINCE) 109class ImageDecoder; 110typedef ImageDecoder* NativeImageSourcePtr; 111typedef RefPtr<SharedBitmap> NativeImagePtr; 112#endif 113 114const int cAnimationLoopOnce = -1; 115const int cAnimationNone = -2; 116 117class ImageSource : public Noncopyable { 118public: 119 ImageSource(); 120 ~ImageSource(); 121 122 // Tells the ImageSource that the Image no longer cares about decoded frame 123 // data -- at all (if |destroyAll| is true), or before frame 124 // |clearBeforeFrame| (if |destroyAll| is false). The ImageSource should 125 // delete cached decoded data for these frames where possible to keep memory 126 // usage low. When |destroyAll| is true, the ImageSource should also reset 127 // any local state so that decoding can begin again. 128 // 129 // Implementations that delete less than what's specified above waste 130 // memory. Implementations that delete more may burn CPU re-decoding frames 131 // that could otherwise have been cached, or encounter errors if they're 132 // asked to decode frames they can't decode due to the loss of previous 133 // decoded frames. 134 // 135 // Callers should not call clear(false, n) and subsequently call 136 // createFrameAtIndex(m) with m < n, unless they first call clear(true). 137 // This ensures that stateful ImageSources/decoders will work properly. 138 // 139 // The |data| and |allDataReceived| parameters should be supplied by callers 140 // who set |destroyAll| to true if they wish to be able to continue using 141 // the ImageSource. This way implementations which choose to destroy their 142 // decoders in some cases can reconstruct them correctly. 143 void clear(bool destroyAll, 144 size_t clearBeforeFrame = 0, 145 SharedBuffer* data = NULL, 146 bool allDataReceived = false); 147 148 bool initialized() const; 149 150 void setData(SharedBuffer* data, bool allDataReceived); 151 String filenameExtension() const; 152 153 bool isSizeAvailable(); 154 IntSize size() const; 155 IntSize frameSizeAtIndex(size_t) const; 156 157 int repetitionCount(); 158 159 size_t frameCount() const; 160 161 // Callers should not call this after calling clear() with a higher index; 162 // see comments on clear() above. 163 NativeImagePtr createFrameAtIndex(size_t); 164 165 float frameDurationAtIndex(size_t); 166 bool frameHasAlphaAtIndex(size_t); // Whether or not the frame actually used any alpha. 167 bool frameIsCompleteAtIndex(size_t); // Whether or not the frame is completely decoded. 168 169#if PLATFORM(ANDROID) 170#if PLATFORM(SGL) 171 void clearURL(); 172 void setURL(const String& url); 173#endif 174#endif 175private: 176#if PLATFORM(ANDROID) 177 // FIXME: This is protected only to allow ImageSourceSkia to set ICO decoder 178 // with a preferred size. See ImageSourceSkia.h for discussion. 179protected: 180#endif 181 NativeImageSourcePtr m_decoder; 182}; 183 184} 185 186#endif 187