blob: e94b7d4e998ba55f9ef42b3a617917c8ada0682b [file] [log] [blame]
The Android Open Source Project09109162009-03-03 19:30:35 -08001/*
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
38 @param stream The stream containing the encoded image data. Ownership
39 of this stream is transferred to the imageref, and
40 therefore the stream's ownercount must be 1.
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 /** 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);
55
56 // overrides
57 virtual void flatten(SkFlattenableWriteBuffer&) const;
58
59protected:
60 /** Override if you want to install a custom allocator.
61 When this is called we will have already acquired the mutex!
62 */
63 virtual bool onDecode(SkImageDecoder* codec, SkStream*, SkBitmap*,
64 SkBitmap::Config, SkImageDecoder::Mode);
65
66 /* Overrides from SkPixelRef
67 When these are called, we will have already acquired the mutex!
68 */
69
70 virtual void* onLockPixels(SkColorTable**);
71 // override this in your subclass to clean up when we're unlocking pixels
72 virtual void onUnlockPixels();
73
74 SkImageRef(SkFlattenableReadBuffer&);
75
76 SkBitmap fBitmap;
77
78private:
79 SkStream* setStream(SkStream*);
80 // called with mutex already held. returns true if the bitmap is in the
81 // requested state (or further, i.e. has pixels)
82 bool prepareBitmap(SkImageDecoder::Mode);
83
84 SkStream* fStream;
85 SkBitmap::Config fConfig;
86 int fSampleSize;
87 bool fErrorInDecoding;
88
89 friend class SkImageRefPool;
90
91 SkImageRef* fPrev, *fNext;
92 size_t ramUsed() const;
93
94 typedef SkPixelRef INHERITED;
95};
96
97#endif