1
2/*
3 * Copyright 2008 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
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
18class SkImageRefPool;
19class SkStream;
20
21// define this to enable dumping whenever we add/remove/purge an imageref
22//#define DUMP_IMAGEREF_LIFECYCLE
23
24class SkImageRef : public SkPixelRef {
25public:
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.
30
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.
34        @param config The preferred config of the decoded bitmap.
35        @param sampleSize Requested sampleSize for decoding. Defaults to 1.
36    */
37    SkImageRef(SkStream*, SkBitmap::Config config, int sampleSize = 1);
38    virtual ~SkImageRef();
39
40    /** this value is passed onto the decoder. Default is true
41     */
42    void setDitherImage(bool dither) { fDoDither = dither; }
43
44    /** Return true if the image can be decoded. If so, and bitmap is non-null,
45        call its setConfig() with the corresponding values, but explicitly will
46        not set its pixels or colortable. Use SkPixelRef::lockPixels() for that.
47
48        If there has been an error decoding the bitmap, this will return false
49        and ignore the bitmap parameter.
50    */
51    bool getInfo(SkBitmap* bm);
52
53    /** Return true if the image can be decoded and is opaque. Calling this
54        method will decode and set the pixels in the specified bitmap and
55        sets the isOpaque flag.
56     */
57    bool isOpaque(SkBitmap* bm);
58
59    SkImageDecoderFactory* getDecoderFactory() const { return fFactory; }
60    // returns the factory parameter
61    SkImageDecoderFactory* setDecoderFactory(SkImageDecoderFactory*);
62
63    // overrides
64    virtual void flatten(SkFlattenableWriteBuffer&) const;
65
66protected:
67    /** Override if you want to install a custom allocator.
68        When this is called we will have already acquired the mutex!
69    */
70    virtual bool onDecode(SkImageDecoder* codec, SkStream*, SkBitmap*,
71                          SkBitmap::Config, SkImageDecoder::Mode);
72
73    /*  Overrides from SkPixelRef
74        When these are called, we will have already acquired the mutex!
75     */
76
77    virtual void* onLockPixels(SkColorTable**);
78    // override this in your subclass to clean up when we're unlocking pixels
79    virtual void onUnlockPixels();
80
81    SkImageRef(SkFlattenableReadBuffer&);
82
83    SkBitmap fBitmap;
84
85private:
86    SkStream* setStream(SkStream*);
87    // called with mutex already held. returns true if the bitmap is in the
88    // requested state (or further, i.e. has pixels)
89    bool prepareBitmap(SkImageDecoder::Mode);
90
91    SkImageDecoderFactory*  fFactory;    // may be null
92    SkStream*               fStream;
93    SkBitmap::Config        fConfig;
94    int                     fSampleSize;
95    bool                    fDoDither;
96    bool                    fErrorInDecoding;
97
98    friend class SkImageRefPool;
99
100    SkImageRef*  fPrev, *fNext;
101    size_t ramUsed() const;
102
103    typedef SkPixelRef INHERITED;
104};
105
106#endif
107