• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SkImageRef_DEFINED
18 #define SkImageRef_DEFINED
19 
20 #include "SkPixelRef.h"
21 #include "SkBitmap.h"
22 #include "SkImageDecoder.h"
23 #include "SkString.h"
24 
25 class SkImageRefPool;
26 class SkStream;
27 
28 // define this to enable dumping whenever we add/remove/purge an imageref
29 //#define DUMP_IMAGEREF_LIFECYCLE
30 
31 class SkImageRef : public SkPixelRef {
32 public:
33     /** Create a new imageref from a stream. NOTE: the stream is not copied, but
34         since it may be accessed from another thread, the caller must ensure
35         that this imageref is the only owner of the stream. i.e. - sole
36         ownership of the stream object is transferred to this imageref object.
37 
38         @param stream The stream containing the encoded image data. This may be
39                       retained (by calling ref()), so the caller should not
40                       explicitly delete it.
41         @param config The preferred config of the decoded bitmap.
42         @param sampleSize Requested sampleSize for decoding. Defaults to 1.
43     */
44     SkImageRef(SkStream*, SkBitmap::Config config, int sampleSize = 1);
45     virtual ~SkImageRef();
46 
47     /** this value is passed onto the decoder. Default is true
48      */
setDitherImage(bool dither)49     void setDitherImage(bool dither) { fDoDither = dither; }
50 
51     /** Return true if the image can be decoded. If so, and bitmap is non-null,
52         call its setConfig() with the corresponding values, but explicitly will
53         not set its pixels or colortable. Use SkPixelRef::lockPixels() for that.
54 
55         If there has been an error decoding the bitmap, this will return false
56         and ignore the bitmap parameter.
57     */
58     bool getInfo(SkBitmap* bm);
59 
getDecoderFactory()60     SkImageDecoderFactory* getDecoderFactory() const { return fFactory; }
61     // returns the factory parameter
62     SkImageDecoderFactory* setDecoderFactory(SkImageDecoderFactory*);
63 
64     // overrides
65     virtual void flatten(SkFlattenableWriteBuffer&) const;
66 
67 protected:
68     /** Override if you want to install a custom allocator.
69         When this is called we will have already acquired the mutex!
70     */
71     virtual bool onDecode(SkImageDecoder* codec, SkStream*, SkBitmap*,
72                           SkBitmap::Config, SkImageDecoder::Mode);
73 
74     /*  Overrides from SkPixelRef
75         When these are called, we will have already acquired the mutex!
76      */
77 
78     virtual void* onLockPixels(SkColorTable**);
79     // override this in your subclass to clean up when we're unlocking pixels
80     virtual void onUnlockPixels();
81 
82     SkImageRef(SkFlattenableReadBuffer&);
83 
84     SkBitmap fBitmap;
85 
86 private:
87     SkStream* setStream(SkStream*);
88     // called with mutex already held. returns true if the bitmap is in the
89     // requested state (or further, i.e. has pixels)
90     bool prepareBitmap(SkImageDecoder::Mode);
91 
92     SkImageDecoderFactory*  fFactory;    // may be null
93     SkStream*               fStream;
94     SkBitmap::Config        fConfig;
95     int                     fSampleSize;
96     bool                    fDoDither;
97     bool                    fErrorInDecoding;
98 
99     friend class SkImageRefPool;
100 
101     SkImageRef*  fPrev, *fNext;
102     size_t ramUsed() const;
103 
104     typedef SkPixelRef INHERITED;
105 };
106 
107 #endif
108