blob: 6ab6e525ffdf43f3776a3b771db25d7b3f88d588 [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();
46
47 /** Return true if the image can be decoded. If so, and bitmap is non-null,
48 call its setConfig() with the corresponding values, but explicitly will
49 not set its pixels or colortable. Use SkPixelRef::lockPixels() for that.
50
51 If there has been an error decoding the bitmap, this will return false
52 and ignore the bitmap parameter.
53 */
54 bool getInfo(SkBitmap* bm);
reed@android.coma14ea0e2009-03-17 17:59:53 +000055
56 SkImageDecoderFactory* getDecoderFactory() const { return fFactory; }
57 // returns the factory parameter
58 SkImageDecoderFactory* setDecoderFactory(SkImageDecoderFactory*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000059
60 // overrides
61 virtual void flatten(SkFlattenableWriteBuffer&) const;
62
63protected:
64 /** Override if you want to install a custom allocator.
65 When this is called we will have already acquired the mutex!
66 */
67 virtual bool onDecode(SkImageDecoder* codec, SkStream*, SkBitmap*,
68 SkBitmap::Config, SkImageDecoder::Mode);
69
70 /* Overrides from SkPixelRef
71 When these are called, we will have already acquired the mutex!
72 */
73
74 virtual void* onLockPixels(SkColorTable**);
75 // override this in your subclass to clean up when we're unlocking pixels
76 virtual void onUnlockPixels();
77
78 SkImageRef(SkFlattenableReadBuffer&);
79
80 SkBitmap fBitmap;
81
82private:
83 SkStream* setStream(SkStream*);
84 // called with mutex already held. returns true if the bitmap is in the
85 // requested state (or further, i.e. has pixels)
86 bool prepareBitmap(SkImageDecoder::Mode);
87
reed@android.coma14ea0e2009-03-17 17:59:53 +000088 SkImageDecoderFactory* fFactory; // may be null
89 SkStream* fStream;
90 SkBitmap::Config fConfig;
91 int fSampleSize;
92 bool fErrorInDecoding;
reed@android.com8a1c16f2008-12-17 15:59:43 +000093
94 friend class SkImageRefPool;
95
96 SkImageRef* fPrev, *fNext;
97 size_t ramUsed() const;
98
99 typedef SkPixelRef INHERITED;
100};
101
102#endif