epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2008 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkImageRef_DEFINED |
| 11 | #define SkImageRef_DEFINED |
| 12 | |
| 13 | #include "SkPixelRef.h" |
| 14 | #include "SkBitmap.h" |
| 15 | #include "SkImageDecoder.h" |
| 16 | #include "SkString.h" |
| 17 | |
| 18 | class SkImageRefPool; |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 19 | class SkStreamRewindable; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | |
| 21 | // define this to enable dumping whenever we add/remove/purge an imageref |
| 22 | //#define DUMP_IMAGEREF_LIFECYCLE |
| 23 | |
| 24 | class SkImageRef : public SkPixelRef { |
| 25 | public: |
| 26 | /** Create a new imageref from a stream. NOTE: the stream is not copied, but |
| 27 | since it may be accessed from another thread, the caller must ensure |
| 28 | that this imageref is the only owner of the stream. i.e. - sole |
| 29 | ownership of the stream object is transferred to this imageref object. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 30 | |
reed@android.com | 1337a7b | 2009-03-16 13:56:10 +0000 | [diff] [blame] | 31 | @param stream The stream containing the encoded image data. This may be |
| 32 | retained (by calling ref()), so the caller should not |
| 33 | explicitly delete it. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | @param config The preferred config of the decoded bitmap. |
| 35 | @param sampleSize Requested sampleSize for decoding. Defaults to 1. |
| 36 | */ |
reed@google.com | bf79023 | 2013-12-13 19:45:58 +0000 | [diff] [blame] | 37 | SkImageRef(const SkImageInfo&, SkStreamRewindable*, int sampleSize = 1, |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 38 | SkBaseMutex* mutex = NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | virtual ~SkImageRef(); |
reed@android.com | bb9aea9 | 2009-09-24 17:21:05 +0000 | [diff] [blame] | 40 | |
| 41 | /** this value is passed onto the decoder. Default is true |
| 42 | */ |
| 43 | void setDitherImage(bool dither) { fDoDither = dither; } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 44 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | /** Return true if the image can be decoded. If so, and bitmap is non-null, |
| 46 | call its setConfig() with the corresponding values, but explicitly will |
| 47 | not set its pixels or colortable. Use SkPixelRef::lockPixels() for that. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 48 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | If there has been an error decoding the bitmap, this will return false |
| 50 | and ignore the bitmap parameter. |
| 51 | */ |
| 52 | bool getInfo(SkBitmap* bm); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 53 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 54 | /** Return true if the image can be decoded and is opaque. Calling this |
| 55 | method will decode and set the pixels in the specified bitmap and |
| 56 | sets the isOpaque flag. |
| 57 | */ |
| 58 | bool isOpaque(SkBitmap* bm); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 59 | |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 60 | SkImageDecoderFactory* getDecoderFactory() const { return fFactory; } |
| 61 | // returns the factory parameter |
| 62 | SkImageDecoderFactory* setDecoderFactory(SkImageDecoderFactory*); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | protected: |
| 65 | /** Override if you want to install a custom allocator. |
| 66 | When this is called we will have already acquired the mutex! |
| 67 | */ |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 68 | virtual bool onDecode(SkImageDecoder* codec, SkStreamRewindable*, SkBitmap*, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | SkBitmap::Config, SkImageDecoder::Mode); |
| 70 | |
| 71 | /* Overrides from SkPixelRef |
| 72 | When these are called, we will have already acquired the mutex! |
| 73 | */ |
| 74 | |
reed@google.com | f1ce052 | 2014-01-03 16:22:48 +0000 | [diff] [blame^] | 75 | virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | // override this in your subclass to clean up when we're unlocking pixels |
reed@google.com | f1ce052 | 2014-01-03 16:22:48 +0000 | [diff] [blame^] | 77 | virtual void onUnlockPixels() SK_OVERRIDE {} |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 78 | |
djsollen@google.com | 528a556 | 2013-02-01 15:57:13 +0000 | [diff] [blame] | 79 | SkImageRef(SkFlattenableReadBuffer&, SkBaseMutex* mutex = NULL); |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 80 | virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | |
| 82 | SkBitmap fBitmap; |
| 83 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 84 | private: |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 85 | SkStreamRewindable* setStream(SkStreamRewindable*); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | // called with mutex already held. returns true if the bitmap is in the |
| 87 | // requested state (or further, i.e. has pixels) |
| 88 | bool prepareBitmap(SkImageDecoder::Mode); |
| 89 | |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 90 | SkImageDecoderFactory* fFactory; // may be null |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 91 | SkStreamRewindable* fStream; |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 92 | int fSampleSize; |
reed@android.com | bb9aea9 | 2009-09-24 17:21:05 +0000 | [diff] [blame] | 93 | bool fDoDither; |
reed@android.com | a14ea0e | 2009-03-17 17:59:53 +0000 | [diff] [blame] | 94 | bool fErrorInDecoding; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 95 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | friend class SkImageRefPool; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 97 | |
| 98 | SkImageRef* fPrev, *fNext; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | size_t ramUsed() const; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 100 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | typedef SkPixelRef INHERITED; |
| 102 | }; |
| 103 | |
| 104 | #endif |