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