OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkCodec.h" | 8 #include "SkCodec.h" |
9 #include "SkCodecPriv.h" | 9 #include "SkCodecPriv.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 virtual bool read(void* data, size_t offset, size_t length) = 0; | 192 virtual bool read(void* data, size_t offset, size_t length) = 0; |
193 | 193 |
194 /* | 194 /* |
195 * Creates an SkMemoryStream from the offset with size. | 195 * Creates an SkMemoryStream from the offset with size. |
196 * Note: for performance reason, this function is destructive to the SkRawSt
ream. One should | 196 * Note: for performance reason, this function is destructive to the SkRawSt
ream. One should |
197 * abandon current object after the function call. | 197 * abandon current object after the function call. |
198 */ | 198 */ |
199 virtual SkMemoryStream* transferBuffer(size_t offset, size_t size) = 0; | 199 virtual SkMemoryStream* transferBuffer(size_t offset, size_t size) = 0; |
200 }; | 200 }; |
201 | 201 |
| 202 class SkRawLimitedDynamicMemoryWStream : public SkDynamicMemoryWStream { |
| 203 public: |
| 204 virtual ~SkRawLimitedDynamicMemoryWStream() {} |
| 205 |
| 206 bool write(const void* buffer, size_t size) override { |
| 207 size_t newSize; |
| 208 if (!safe_add_to_size_t(this->bytesWritten(), size, &newSize) || |
| 209 newSize > kMaxStreamSize) |
| 210 { |
| 211 SkCodecPrintf("Error: Stream size exceeds the limit.\n"); |
| 212 return false; |
| 213 } |
| 214 return this->INHERITED::write(buffer, size); |
| 215 } |
| 216 |
| 217 private: |
| 218 const size_t kMaxStreamSize = 100 * 1024 * 1024; // 100MB |
| 219 |
| 220 typedef SkDynamicMemoryWStream INHERITED; |
| 221 }; |
| 222 |
| 223 // Note: the maximum buffer size is 100MB (limited by SkRawLimitedDynamicMemoryW
Stream). |
202 class SkRawBufferedStream : public SkRawStream { | 224 class SkRawBufferedStream : public SkRawStream { |
203 public: | 225 public: |
204 // Will take the ownership of the stream. | 226 // Will take the ownership of the stream. |
205 explicit SkRawBufferedStream(SkStream* stream) | 227 explicit SkRawBufferedStream(SkStream* stream) |
206 : fStream(stream) | 228 : fStream(stream) |
207 , fWholeStreamRead(false) | 229 , fWholeStreamRead(false) |
208 { | 230 { |
209 // Only use SkRawBufferedStream when the stream is not an asset stream. | 231 // Only use SkRawBufferedStream when the stream is not an asset stream. |
210 SkASSERT(!is_asset_stream(*stream)); | 232 SkASSERT(!is_asset_stream(*stream)); |
211 } | 233 } |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 const size_t bytesRead = fStream->read(tempBuffer.get(), sizeToRead); | 316 const size_t bytesRead = fStream->read(tempBuffer.get(), sizeToRead); |
295 if (bytesRead < sizeRequested) { | 317 if (bytesRead < sizeRequested) { |
296 return false; | 318 return false; |
297 } | 319 } |
298 return fStreamBuffer.write(tempBuffer.get(), bytesRead); | 320 return fStreamBuffer.write(tempBuffer.get(), bytesRead); |
299 } | 321 } |
300 | 322 |
301 SkAutoTDelete<SkStream> fStream; | 323 SkAutoTDelete<SkStream> fStream; |
302 bool fWholeStreamRead; | 324 bool fWholeStreamRead; |
303 | 325 |
304 SkDynamicMemoryWStream fStreamBuffer; | 326 // Use a size-limited stream to avoid holding too huge buffer. |
| 327 SkRawLimitedDynamicMemoryWStream fStreamBuffer; |
305 | 328 |
306 const size_t kReadToEnd = 0; | 329 const size_t kReadToEnd = 0; |
307 }; | 330 }; |
308 | 331 |
309 class SkRawAssetStream : public SkRawStream { | 332 class SkRawAssetStream : public SkRawStream { |
310 public: | 333 public: |
311 // Will take the ownership of the stream. | 334 // Will take the ownership of the stream. |
312 explicit SkRawAssetStream(SkStream* stream) | 335 explicit SkRawAssetStream(SkStream* stream) |
313 : fStream(stream) | 336 : fStream(stream) |
314 { | 337 { |
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
709 SkISize sizeFloor = this->onGetScaledDimensions(1.f / std::floor(fullShortEd
ge / shortEdge)); | 732 SkISize sizeFloor = this->onGetScaledDimensions(1.f / std::floor(fullShortEd
ge / shortEdge)); |
710 SkISize sizeCeil = this->onGetScaledDimensions(1.f / std::ceil(fullShortEdge
/ shortEdge)); | 733 SkISize sizeCeil = this->onGetScaledDimensions(1.f / std::ceil(fullShortEdge
/ shortEdge)); |
711 return sizeFloor == dim || sizeCeil == dim; | 734 return sizeFloor == dim || sizeCeil == dim; |
712 } | 735 } |
713 | 736 |
714 SkRawCodec::~SkRawCodec() {} | 737 SkRawCodec::~SkRawCodec() {} |
715 | 738 |
716 SkRawCodec::SkRawCodec(SkDngImage* dngImage) | 739 SkRawCodec::SkRawCodec(SkDngImage* dngImage) |
717 : INHERITED(dngImage->getImageInfo(), nullptr) | 740 : INHERITED(dngImage->getImageInfo(), nullptr) |
718 , fDngImage(dngImage) {} | 741 , fDngImage(dngImage) {} |
OLD | NEW |