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 |
21 SkImageRef::SkImageRef(SkStreamRewindable* stream, SkBitmap::Config config, | 21 SkImageRef::SkImageRef(const SkImageInfo& info, SkStreamRewindable* stream, |
22 int sampleSize, SkBaseMutex* mutex) | 22 int sampleSize, SkBaseMutex* mutex) |
23 : SkPixelRef(mutex), fErrorInDecoding(false) { | 23 : SkPixelRef(info, mutex), fErrorInDecoding(false) { |
24 SkASSERT(stream); | 24 SkASSERT(stream); |
25 stream->ref(); | 25 stream->ref(); |
26 fStream = stream; | 26 fStream = stream; |
27 fConfig = config; | |
28 fSampleSize = sampleSize; | 27 fSampleSize = sampleSize; |
29 fDoDither = true; | 28 fDoDither = true; |
30 fPrev = fNext = NULL; | 29 fPrev = fNext = NULL; |
31 fFactory = NULL; | 30 fFactory = NULL; |
32 | 31 |
33 #ifdef DUMP_IMAGEREF_LIFECYCLE | 32 #ifdef DUMP_IMAGEREF_LIFECYCLE |
34 SkDebugf("add ImageRef %p [%d] data=%d\n", | 33 SkDebugf("add ImageRef %p [%d] data=%d\n", |
35 this, config, (int)stream->getLength()); | 34 this, this->info().fColorType, (int)stream->getLength()); |
36 #endif | 35 #endif |
37 } | 36 } |
38 | 37 |
39 SkImageRef::~SkImageRef() { | 38 SkImageRef::~SkImageRef() { |
40 | 39 |
41 #ifdef DUMP_IMAGEREF_LIFECYCLE | 40 #ifdef DUMP_IMAGEREF_LIFECYCLE |
42 SkDebugf("delete ImageRef %p [%d] data=%d\n", | 41 SkDebugf("delete ImageRef %p [%d] data=%d\n", |
43 this, fConfig, (int)fStream->getLength()); | 42 this, fConfig, (int)fStream->getLength()); |
44 #endif | 43 #endif |
45 | 44 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 SkImageDecoder::Mode mode) { | 84 SkImageDecoder::Mode mode) { |
86 return codec->decode(stream, bitmap, config, mode); | 85 return codec->decode(stream, bitmap, config, mode); |
87 } | 86 } |
88 | 87 |
89 bool SkImageRef::prepareBitmap(SkImageDecoder::Mode mode) { | 88 bool SkImageRef::prepareBitmap(SkImageDecoder::Mode mode) { |
90 | 89 |
91 if (fErrorInDecoding) { | 90 if (fErrorInDecoding) { |
92 return false; | 91 return false; |
93 } | 92 } |
94 | 93 |
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 | |
103 if (NULL != fBitmap.getPixels() || | 94 if (NULL != fBitmap.getPixels() || |
104 (SkBitmap::kNo_Config != fBitmap.config() && | 95 (SkBitmap::kNo_Config != fBitmap.config() && |
105 SkImageDecoder::kDecodeBounds_Mode == mode)) { | 96 SkImageDecoder::kDecodeBounds_Mode == mode)) { |
106 return true; | 97 return true; |
107 } | 98 } |
108 | 99 |
109 SkASSERT(fBitmap.getPixels() == NULL); | 100 SkASSERT(fBitmap.getPixels() == NULL); |
110 | 101 |
111 if (!fStream->rewind()) { | 102 if (!fStream->rewind()) { |
112 SkDEBUGF(("Failed to rewind SkImageRef stream!")); | 103 SkDEBUGF(("Failed to rewind SkImageRef stream!")); |
113 return false; | 104 return false; |
114 } | 105 } |
115 | 106 |
116 SkImageDecoder* codec; | 107 SkImageDecoder* codec; |
117 if (fFactory) { | 108 if (fFactory) { |
118 codec = fFactory->newDecoder(fStream); | 109 codec = fFactory->newDecoder(fStream); |
119 } else { | 110 } else { |
120 codec = SkImageDecoder::Factory(fStream); | 111 codec = SkImageDecoder::Factory(fStream); |
121 } | 112 } |
122 | 113 |
123 if (codec) { | 114 if (codec) { |
124 SkAutoTDelete<SkImageDecoder> ad(codec); | 115 SkAutoTDelete<SkImageDecoder> ad(codec); |
125 | 116 |
126 codec->setSampleSize(fSampleSize); | 117 codec->setSampleSize(fSampleSize); |
127 codec->setDitherImage(fDoDither); | 118 codec->setDitherImage(fDoDither); |
128 if (this->onDecode(codec, fStream, &fBitmap, fConfig, mode)) { | 119 if (this->onDecode(codec, fStream, &fBitmap, fBitmap.config(), mode)) { |
129 return true; | 120 return true; |
130 } | 121 } |
131 } | 122 } |
132 | 123 |
133 #ifdef DUMP_IMAGEREF_LIFECYCLE | 124 #ifdef DUMP_IMAGEREF_LIFECYCLE |
134 if (NULL == codec) { | 125 if (NULL == codec) { |
135 SkDebugf("--- ImageRef: <%s> failed to find codec\n", this->getURI()); | 126 SkDebugf("--- ImageRef: <%s> failed to find codec\n", this->getURI()); |
136 } else { | 127 } else { |
137 SkDebugf("--- ImageRef: <%s> failed in codec for %d mode\n", | 128 SkDebugf("--- ImageRef: <%s> failed in codec for %d mode\n", |
138 this->getURI(), mode); | 129 this->getURI(), mode); |
(...skipping 24 matching lines...) Expand all Loading... |
163 size += fBitmap.getColorTable()->count() * sizeof(SkPMColor); | 154 size += fBitmap.getColorTable()->count() * sizeof(SkPMColor); |
164 } | 155 } |
165 } | 156 } |
166 return size; | 157 return size; |
167 } | 158 } |
168 | 159 |
169 /////////////////////////////////////////////////////////////////////////////// | 160 /////////////////////////////////////////////////////////////////////////////// |
170 | 161 |
171 SkImageRef::SkImageRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) | 162 SkImageRef::SkImageRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) |
172 : INHERITED(buffer, mutex), fErrorInDecoding(false) { | 163 : INHERITED(buffer, mutex), fErrorInDecoding(false) { |
173 fConfig = (SkBitmap::Config)buffer.readUInt(); | |
174 fSampleSize = buffer.readInt(); | 164 fSampleSize = buffer.readInt(); |
175 fDoDither = buffer.readBool(); | 165 fDoDither = buffer.readBool(); |
176 | 166 |
177 size_t length = buffer.getArrayCount(); | 167 size_t length = buffer.getArrayCount(); |
178 fStream = SkNEW_ARGS(SkMemoryStream, (length)); | 168 fStream = SkNEW_ARGS(SkMemoryStream, (length)); |
179 buffer.readByteArray((void*)fStream->getMemoryBase(), length); | 169 buffer.readByteArray((void*)fStream->getMemoryBase(), length); |
180 | 170 |
181 fPrev = fNext = NULL; | 171 fPrev = fNext = NULL; |
182 fFactory = NULL; | 172 fFactory = NULL; |
183 } | 173 } |
184 | 174 |
185 void SkImageRef::flatten(SkFlattenableWriteBuffer& buffer) const { | 175 void SkImageRef::flatten(SkFlattenableWriteBuffer& buffer) const { |
186 this->INHERITED::flatten(buffer); | 176 this->INHERITED::flatten(buffer); |
187 | 177 |
188 buffer.writeUInt(fConfig); | |
189 buffer.writeInt(fSampleSize); | 178 buffer.writeInt(fSampleSize); |
190 buffer.writeBool(fDoDither); | 179 buffer.writeBool(fDoDither); |
191 // FIXME: Consider moving this logic should go into writeStream itself. | 180 // FIXME: Consider moving this logic should go into writeStream itself. |
192 // writeStream currently has no other callers, so this may be fine for | 181 // writeStream currently has no other callers, so this may be fine for |
193 // now. | 182 // now. |
194 if (!fStream->rewind()) { | 183 if (!fStream->rewind()) { |
195 SkDEBUGF(("Failed to rewind SkImageRef stream!")); | 184 SkDEBUGF(("Failed to rewind SkImageRef stream!")); |
196 buffer.write32(0); | 185 buffer.write32(0); |
197 } else { | 186 } else { |
198 // FIXME: Handle getLength properly here. Perhaps this class should | 187 // FIXME: Handle getLength properly here. Perhaps this class should |
199 // take an SkStreamAsset. | 188 // take an SkStreamAsset. |
200 buffer.writeStream(fStream, fStream->getLength()); | 189 buffer.writeStream(fStream, fStream->getLength()); |
201 } | 190 } |
202 } | 191 } |
OLD | NEW |