1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
#include "SkImageRef.h"
#include "SkBitmap.h"
#include "SkFlattenable.h"
#include "SkImageDecoder.h"
#include "SkStream.h"
#include "SkTemplates.h"
#include "SkThread.h"
// can't be static, as SkImageRef_Pool needs to see it
SkMutex gImageRefMutex;
///////////////////////////////////////////////////////////////////////////////
SkImageRef::SkImageRef(SkStream* stream, SkBitmap::Config config,
int sampleSize)
: SkPixelRef(&gImageRefMutex), fErrorInDecoding(false) {
SkASSERT(stream);
SkASSERT(1 == stream->getRefCnt());
fStream = stream;
fConfig = config;
fSampleSize = sampleSize;
fPrev = fNext = NULL;
#ifdef DUMP_IMAGEREF_LIFECYCLE
SkDebugf("add ImageRef %p [%d] data=%d\n",
this, config, (int)stream->getLength());
#endif
}
SkImageRef::~SkImageRef() {
SkASSERT(&gImageRefMutex == this->mutex());
#ifdef DUMP_IMAGEREF_LIFECYCLE
SkDebugf("delete ImageRef %p [%d] data=%d\n",
this, fConfig, (int)fStream->getLength());
#endif
delete fStream;
}
bool SkImageRef::getInfo(SkBitmap* bitmap) {
SkAutoMutexAcquire ac(gImageRefMutex);
if (!this->prepareBitmap(SkImageDecoder::kDecodeBounds_Mode)) {
return false;
}
SkASSERT(SkBitmap::kNo_Config != fBitmap.config());
if (bitmap) {
bitmap->setConfig(fBitmap.config(), fBitmap.width(), fBitmap.height());
}
return true;
}
///////////////////////////////////////////////////////////////////////////////
bool SkImageRef::onDecode(SkImageDecoder* codec, SkStream* stream,
SkBitmap* bitmap, SkBitmap::Config config,
SkImageDecoder::Mode mode) {
return codec->decode(stream, bitmap, config, mode);
}
bool SkImageRef::prepareBitmap(SkImageDecoder::Mode mode) {
SkASSERT(&gImageRefMutex == this->mutex());
if (fErrorInDecoding) {
return false;
}
/* As soon as we really know our config, we record it, so that on
subsequent calls to the codec, we are sure we will always get the same
result.
*/
if (SkBitmap::kNo_Config != fBitmap.config()) {
fConfig = fBitmap.config();
}
if (NULL != fBitmap.getPixels() ||
(SkBitmap::kNo_Config != fBitmap.config() &&
SkImageDecoder::kDecodeBounds_Mode == mode)) {
return true;
}
SkASSERT(fBitmap.getPixels() == NULL);
fStream->rewind();
SkImageDecoder* codec = SkImageDecoder::Factory(fStream);
if (codec) {
SkAutoTDelete<SkImageDecoder> ad(codec);
codec->setSampleSize(fSampleSize);
if (this->onDecode(codec, fStream, &fBitmap, fConfig, mode)) {
return true;
}
}
#ifdef DUMP_IMAGEREF_LIFECYCLE
if (NULL == codec) {
SkDebugf("--- ImageRef: <%s> failed to find codec\n", this->getURI());
} else {
SkDebugf("--- ImageRef: <%s> failed in codec for %d mode\n",
this->getURI(), mode);
}
#endif
fErrorInDecoding = true;
fBitmap.reset();
return false;
}
void* SkImageRef::onLockPixels(SkColorTable** ct) {
SkASSERT(&gImageRefMutex == this->mutex());
if (NULL == fBitmap.getPixels()) {
(void)this->prepareBitmap(SkImageDecoder::kDecodePixels_Mode);
}
if (ct) {
*ct = fBitmap.getColorTable();
}
return fBitmap.getPixels();
}
void SkImageRef::onUnlockPixels() {
// we're already have the mutex locked
SkASSERT(&gImageRefMutex == this->mutex());
}
size_t SkImageRef::ramUsed() const {
size_t size = 0;
if (fBitmap.getPixels()) {
size = fBitmap.getSize();
if (fBitmap.getColorTable()) {
size += fBitmap.getColorTable()->count() * sizeof(SkPMColor);
}
}
return size;
}
///////////////////////////////////////////////////////////////////////////////
SkImageRef::SkImageRef(SkFlattenableReadBuffer& buffer)
: INHERITED(buffer, &gImageRefMutex), fErrorInDecoding(false) {
fConfig = (SkBitmap::Config)buffer.readU8();
fSampleSize = buffer.readU8();
size_t length = buffer.readU32();
fStream = SkNEW_ARGS(SkMemoryStream, (length));
buffer.read((void*)fStream->getMemoryBase(), length);
fPrev = fNext = NULL;
}
void SkImageRef::flatten(SkFlattenableWriteBuffer& buffer) const {
this->INHERITED::flatten(buffer);
buffer.write8(fConfig);
buffer.write8(fSampleSize);
size_t length = fStream->getLength();
buffer.write32(length);
fStream->rewind();
buffer.readFromStream(fStream, length);
}
|