OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "SkImageRef.h" | 8 #include "SkImageRef.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkFlattenableBuffers.h" | 10 #include "SkFlattenableBuffers.h" |
11 #include "SkImageDecoder.h" | 11 #include "SkImageDecoder.h" |
12 #include "SkStream.h" | 12 #include "SkStream.h" |
13 #include "SkTemplates.h" | 13 #include "SkTemplates.h" |
14 #include "SkThread.h" | 14 #include "SkThread.h" |
15 | 15 |
16 //#define DUMP_IMAGEREF_LIFECYCLE | 16 //#define DUMP_IMAGEREF_LIFECYCLE |
17 | 17 |
| 18 |
18 /////////////////////////////////////////////////////////////////////////////// | 19 /////////////////////////////////////////////////////////////////////////////// |
19 | 20 |
20 SkImageRef::SkImageRef(const SkImageInfo& info, SkStreamRewindable* stream, | 21 SkImageRef::SkImageRef(SkStreamRewindable* stream, SkBitmap::Config config, |
21 int sampleSize, SkBaseMutex* mutex) | 22 int sampleSize, SkBaseMutex* mutex) |
22 : INHERITED(info, mutex), fErrorInDecoding(false) { | 23 : SkPixelRef(mutex), fErrorInDecoding(false) { |
23 SkASSERT(stream); | 24 SkASSERT(stream); |
24 stream->ref(); | 25 stream->ref(); |
25 fStream = stream; | 26 fStream = stream; |
| 27 fConfig = config; |
26 fSampleSize = sampleSize; | 28 fSampleSize = sampleSize; |
27 fDoDither = true; | 29 fDoDither = true; |
28 fPrev = fNext = NULL; | 30 fPrev = fNext = NULL; |
29 fFactory = NULL; | 31 fFactory = NULL; |
30 | 32 |
31 #ifdef DUMP_IMAGEREF_LIFECYCLE | 33 #ifdef DUMP_IMAGEREF_LIFECYCLE |
32 SkDebugf("add ImageRef %p [%d] data=%d\n", | 34 SkDebugf("add ImageRef %p [%d] data=%d\n", |
33 this, config, (int)stream->getLength()); | 35 this, config, (int)stream->getLength()); |
34 #endif | 36 #endif |
35 } | 37 } |
36 | 38 |
37 SkImageRef::~SkImageRef() { | 39 SkImageRef::~SkImageRef() { |
38 | 40 |
39 #ifdef DUMP_IMAGEREF_LIFECYCLE | 41 #ifdef DUMP_IMAGEREF_LIFECYCLE |
40 SkDebugf("delete ImageRef %p [%d] data=%d\n", | 42 SkDebugf("delete ImageRef %p [%d] data=%d\n", |
41 this, this->info().fColorType, (int)fStream->getLength()); | 43 this, fConfig, (int)fStream->getLength()); |
42 #endif | 44 #endif |
43 | 45 |
44 fStream->unref(); | 46 fStream->unref(); |
45 SkSafeUnref(fFactory); | 47 SkSafeUnref(fFactory); |
46 } | 48 } |
47 | 49 |
48 bool SkImageRef::getInfo(SkBitmap* bitmap) { | 50 bool SkImageRef::getInfo(SkBitmap* bitmap) { |
49 SkAutoMutexAcquire ac(this->mutex()); | 51 SkAutoMutexAcquire ac(this->mutex()); |
50 | 52 |
51 if (!this->prepareBitmap(SkImageDecoder::kDecodeBounds_Mode)) { | 53 if (!this->prepareBitmap(SkImageDecoder::kDecodeBounds_Mode)) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 SkImageDecoder::Mode mode) { | 85 SkImageDecoder::Mode mode) { |
84 return codec->decode(stream, bitmap, config, mode); | 86 return codec->decode(stream, bitmap, config, mode); |
85 } | 87 } |
86 | 88 |
87 bool SkImageRef::prepareBitmap(SkImageDecoder::Mode mode) { | 89 bool SkImageRef::prepareBitmap(SkImageDecoder::Mode mode) { |
88 | 90 |
89 if (fErrorInDecoding) { | 91 if (fErrorInDecoding) { |
90 return false; | 92 return false; |
91 } | 93 } |
92 | 94 |
| 95 /* As soon as we really know our config, we record it, so that on |
| 96 subsequent calls to the codec, we are sure we will always get the same |
| 97 result. |
| 98 */ |
| 99 if (SkBitmap::kNo_Config != fBitmap.config()) { |
| 100 fConfig = fBitmap.config(); |
| 101 } |
| 102 |
93 if (NULL != fBitmap.getPixels() || | 103 if (NULL != fBitmap.getPixels() || |
94 (SkBitmap::kNo_Config != fBitmap.config() && | 104 (SkBitmap::kNo_Config != fBitmap.config() && |
95 SkImageDecoder::kDecodeBounds_Mode == mode)) { | 105 SkImageDecoder::kDecodeBounds_Mode == mode)) { |
96 return true; | 106 return true; |
97 } | 107 } |
98 | 108 |
99 SkASSERT(fBitmap.getPixels() == NULL); | 109 SkASSERT(fBitmap.getPixels() == NULL); |
100 | 110 |
101 if (!fStream->rewind()) { | 111 if (!fStream->rewind()) { |
102 SkDEBUGF(("Failed to rewind SkImageRef stream!")); | 112 SkDEBUGF(("Failed to rewind SkImageRef stream!")); |
103 return false; | 113 return false; |
104 } | 114 } |
105 | 115 |
106 SkImageDecoder* codec; | 116 SkImageDecoder* codec; |
107 if (fFactory) { | 117 if (fFactory) { |
108 codec = fFactory->newDecoder(fStream); | 118 codec = fFactory->newDecoder(fStream); |
109 } else { | 119 } else { |
110 codec = SkImageDecoder::Factory(fStream); | 120 codec = SkImageDecoder::Factory(fStream); |
111 } | 121 } |
112 | 122 |
113 if (codec) { | 123 if (codec) { |
114 SkAutoTDelete<SkImageDecoder> ad(codec); | 124 SkAutoTDelete<SkImageDecoder> ad(codec); |
115 | 125 |
116 codec->setSampleSize(fSampleSize); | 126 codec->setSampleSize(fSampleSize); |
117 codec->setDitherImage(fDoDither); | 127 codec->setDitherImage(fDoDither); |
118 if (this->onDecode(codec, fStream, &fBitmap, fBitmap.config(), mode)) { | 128 if (this->onDecode(codec, fStream, &fBitmap, fConfig, mode)) { |
119 return true; | 129 return true; |
120 } | 130 } |
121 } | 131 } |
122 | 132 |
123 #ifdef DUMP_IMAGEREF_LIFECYCLE | 133 #ifdef DUMP_IMAGEREF_LIFECYCLE |
124 if (NULL == codec) { | 134 if (NULL == codec) { |
125 SkDebugf("--- ImageRef: <%s> failed to find codec\n", this->getURI()); | 135 SkDebugf("--- ImageRef: <%s> failed to find codec\n", this->getURI()); |
126 } else { | 136 } else { |
127 SkDebugf("--- ImageRef: <%s> failed in codec for %d mode\n", | 137 SkDebugf("--- ImageRef: <%s> failed in codec for %d mode\n", |
128 this->getURI(), mode); | 138 this->getURI(), mode); |
129 } | 139 } |
130 #endif | 140 #endif |
131 fErrorInDecoding = true; | 141 fErrorInDecoding = true; |
132 fBitmap.reset(); | 142 fBitmap.reset(); |
133 return false; | 143 return false; |
134 } | 144 } |
135 | 145 |
136 bool SkImageRef::onNewLockPixels(LockRec* rec) { | 146 void* SkImageRef::onLockPixels(SkColorTable** ct) { |
137 if (NULL == fBitmap.getPixels()) { | 147 if (NULL == fBitmap.getPixels()) { |
138 (void)this->prepareBitmap(SkImageDecoder::kDecodePixels_Mode); | 148 (void)this->prepareBitmap(SkImageDecoder::kDecodePixels_Mode); |
139 } | 149 } |
140 | 150 |
141 if (NULL == fBitmap.getPixels()) { | 151 if (ct) { |
142 return false; | 152 *ct = fBitmap.getColorTable(); |
143 } | 153 } |
144 rec->fPixels = fBitmap.getPixels(); | 154 return fBitmap.getPixels(); |
145 rec->fColorTable = NULL; | |
146 rec->fRowBytes = fBitmap.rowBytes(); | |
147 return true; | |
148 } | 155 } |
149 | 156 |
150 size_t SkImageRef::ramUsed() const { | 157 size_t SkImageRef::ramUsed() const { |
151 size_t size = 0; | 158 size_t size = 0; |
152 | 159 |
153 if (fBitmap.getPixels()) { | 160 if (fBitmap.getPixels()) { |
154 size = fBitmap.getSize(); | 161 size = fBitmap.getSize(); |
155 if (fBitmap.getColorTable()) { | 162 if (fBitmap.getColorTable()) { |
156 size += fBitmap.getColorTable()->count() * sizeof(SkPMColor); | 163 size += fBitmap.getColorTable()->count() * sizeof(SkPMColor); |
157 } | 164 } |
158 } | 165 } |
159 return size; | 166 return size; |
160 } | 167 } |
161 | 168 |
162 /////////////////////////////////////////////////////////////////////////////// | 169 /////////////////////////////////////////////////////////////////////////////// |
163 | 170 |
164 SkImageRef::SkImageRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) | 171 SkImageRef::SkImageRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) |
165 : INHERITED(buffer, mutex), fErrorInDecoding(false) { | 172 : INHERITED(buffer, mutex), fErrorInDecoding(false) { |
| 173 fConfig = (SkBitmap::Config)buffer.readUInt(); |
166 fSampleSize = buffer.readInt(); | 174 fSampleSize = buffer.readInt(); |
167 fDoDither = buffer.readBool(); | 175 fDoDither = buffer.readBool(); |
168 | 176 |
169 size_t length = buffer.getArrayCount(); | 177 size_t length = buffer.getArrayCount(); |
170 fStream = SkNEW_ARGS(SkMemoryStream, (length)); | 178 fStream = SkNEW_ARGS(SkMemoryStream, (length)); |
171 buffer.readByteArray((void*)fStream->getMemoryBase(), length); | 179 buffer.readByteArray((void*)fStream->getMemoryBase(), length); |
172 | 180 |
173 fPrev = fNext = NULL; | 181 fPrev = fNext = NULL; |
174 fFactory = NULL; | 182 fFactory = NULL; |
175 } | 183 } |
176 | 184 |
177 void SkImageRef::flatten(SkFlattenableWriteBuffer& buffer) const { | 185 void SkImageRef::flatten(SkFlattenableWriteBuffer& buffer) const { |
178 this->INHERITED::flatten(buffer); | 186 this->INHERITED::flatten(buffer); |
179 | 187 |
| 188 buffer.writeUInt(fConfig); |
180 buffer.writeInt(fSampleSize); | 189 buffer.writeInt(fSampleSize); |
181 buffer.writeBool(fDoDither); | 190 buffer.writeBool(fDoDither); |
182 // FIXME: Consider moving this logic should go into writeStream itself. | 191 // FIXME: Consider moving this logic should go into writeStream itself. |
183 // writeStream currently has no other callers, so this may be fine for | 192 // writeStream currently has no other callers, so this may be fine for |
184 // now. | 193 // now. |
185 if (!fStream->rewind()) { | 194 if (!fStream->rewind()) { |
186 SkDEBUGF(("Failed to rewind SkImageRef stream!")); | 195 SkDEBUGF(("Failed to rewind SkImageRef stream!")); |
187 buffer.write32(0); | 196 buffer.write32(0); |
188 } else { | 197 } else { |
189 // FIXME: Handle getLength properly here. Perhaps this class should | 198 // FIXME: Handle getLength properly here. Perhaps this class should |
190 // take an SkStreamAsset. | 199 // take an SkStreamAsset. |
191 buffer.writeStream(fStream, fStream->getLength()); | 200 buffer.writeStream(fStream, fStream->getLength()); |
192 } | 201 } |
193 } | 202 } |
OLD | NEW |