michael@0: michael@0: /* michael@0: * Copyright 2008 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #ifndef SkImageRef_DEFINED michael@0: #define SkImageRef_DEFINED michael@0: michael@0: #include "SkPixelRef.h" michael@0: #include "SkBitmap.h" michael@0: #include "SkImageDecoder.h" michael@0: #include "SkString.h" michael@0: michael@0: class SkImageRefPool; michael@0: class SkStreamRewindable; michael@0: michael@0: // define this to enable dumping whenever we add/remove/purge an imageref michael@0: //#define DUMP_IMAGEREF_LIFECYCLE michael@0: michael@0: class SkImageRef : public SkPixelRef { michael@0: public: michael@0: /** Create a new imageref from a stream. NOTE: the stream is not copied, but michael@0: since it may be accessed from another thread, the caller must ensure michael@0: that this imageref is the only owner of the stream. i.e. - sole michael@0: ownership of the stream object is transferred to this imageref object. michael@0: michael@0: @param stream The stream containing the encoded image data. This may be michael@0: retained (by calling ref()), so the caller should not michael@0: explicitly delete it. michael@0: @param config The preferred config of the decoded bitmap. michael@0: @param sampleSize Requested sampleSize for decoding. Defaults to 1. michael@0: */ michael@0: SkImageRef(const SkImageInfo&, SkStreamRewindable*, int sampleSize = 1, michael@0: SkBaseMutex* mutex = NULL); michael@0: virtual ~SkImageRef(); michael@0: michael@0: /** this value is passed onto the decoder. Default is true michael@0: */ michael@0: void setDitherImage(bool dither) { fDoDither = dither; } michael@0: michael@0: /** Return true if the image can be decoded. If so, and bitmap is non-null, michael@0: call its setConfig() with the corresponding values, but explicitly will michael@0: not set its pixels or colortable. Use SkPixelRef::lockPixels() for that. michael@0: michael@0: If there has been an error decoding the bitmap, this will return false michael@0: and ignore the bitmap parameter. michael@0: */ michael@0: bool getInfo(SkBitmap* bm); michael@0: michael@0: /** Return true if the image can be decoded and is opaque. Calling this michael@0: method will decode and set the pixels in the specified bitmap and michael@0: sets the isOpaque flag. michael@0: */ michael@0: bool isOpaque(SkBitmap* bm); michael@0: michael@0: SkImageDecoderFactory* getDecoderFactory() const { return fFactory; } michael@0: // returns the factory parameter michael@0: SkImageDecoderFactory* setDecoderFactory(SkImageDecoderFactory*); michael@0: michael@0: protected: michael@0: /** Override if you want to install a custom allocator. michael@0: When this is called we will have already acquired the mutex! michael@0: */ michael@0: virtual bool onDecode(SkImageDecoder* codec, SkStreamRewindable*, SkBitmap*, michael@0: SkBitmap::Config, SkImageDecoder::Mode); michael@0: michael@0: /* Overrides from SkPixelRef michael@0: When these are called, we will have already acquired the mutex! michael@0: */ michael@0: michael@0: virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE; michael@0: // override this in your subclass to clean up when we're unlocking pixels michael@0: virtual void onUnlockPixels() SK_OVERRIDE {} michael@0: michael@0: SkImageRef(SkReadBuffer&, SkBaseMutex* mutex = NULL); michael@0: virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; michael@0: michael@0: SkBitmap fBitmap; michael@0: michael@0: private: michael@0: SkStreamRewindable* setStream(SkStreamRewindable*); michael@0: // called with mutex already held. returns true if the bitmap is in the michael@0: // requested state (or further, i.e. has pixels) michael@0: bool prepareBitmap(SkImageDecoder::Mode); michael@0: michael@0: SkImageDecoderFactory* fFactory; // may be null michael@0: SkStreamRewindable* fStream; michael@0: int fSampleSize; michael@0: bool fDoDither; michael@0: bool fErrorInDecoding; michael@0: michael@0: friend class SkImageRefPool; michael@0: michael@0: SkImageRef* fPrev, *fNext; michael@0: size_t ramUsed() const; michael@0: michael@0: typedef SkPixelRef INHERITED; michael@0: }; michael@0: michael@0: #endif