blob: 9c9896f6fcff3fa39cba7f6a6085a6c3ce98e87e [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
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
25class SkImageRefPool;
26class SkStream;
27
28// define this to enable dumping whenever we add/remove/purge an imageref
29//#define DUMP_IMAGEREF_LIFECYCLE
30
31class SkImageRef : public SkPixelRef {
32public:
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
reed@android.com1337a7b2009-03-16 13:56:10 +000038 @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.
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 @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();
reed@android.combb9aea92009-09-24 17:21:05 +000046
47 /** this value is passed onto the decoder. Default is true
48 */
49 void setDitherImage(bool dither) { fDoDither = dither; }
reed@android.com8a1c16f2008-12-17 15:59:43 +000050
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);
reed@android.coma14ea0e2009-03-17 17:59:53 +000059
60 SkImageDecoderFactory* getDecoderFactory() const { return fFactory; }
61 // returns the factory parameter
62 SkImageDecoderFactory* setDecoderFactory(SkImageDecoderFactory*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000063
64 // overrides
65 virtual void flatten(SkFlattenableWriteBuffer&) const;
66
67protected:
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
86private:
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
reed@android.coma14ea0e2009-03-17 17:59:53 +000092 SkImageDecoderFactory* fFactory; // may be null
93 SkStream* fStream;
94 SkBitmap::Config fConfig;
95 int fSampleSize;
reed@android.combb9aea92009-09-24 17:21:05 +000096 bool fDoDither;
reed@android.coma14ea0e2009-03-17 17:59:53 +000097 bool fErrorInDecoding;
reed@android.com8a1c16f2008-12-17 15:59:43 +000098
99 friend class SkImageRefPool;
100
101 SkImageRef* fPrev, *fNext;
102 size_t ramUsed() const;
103
104 typedef SkPixelRef INHERITED;
105};
106
107#endif