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