Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: src/codec/SkRawCodec.cpp

Issue 1644893003: Avoid too small reads to bufferMoreData() (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Update commit message Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 return SkStreamCopy(&fStreamBuffer, fStream.get()); 260 return SkStreamCopy(&fStreamBuffer, fStream.get());
261 } 261 }
262 262
263 if (newSize <= fStreamBuffer.bytesWritten()) { // already buffered to n ewSize 263 if (newSize <= fStreamBuffer.bytesWritten()) { // already buffered to n ewSize
264 return true; 264 return true;
265 } 265 }
266 if (fWholeStreamRead) { // newSize is larger than the whole stream. 266 if (fWholeStreamRead) { // newSize is larger than the whole stream.
267 return false; 267 return false;
268 } 268 }
269 269
270 const size_t sizeToRead = newSize - fStreamBuffer.bytesWritten(); 270 // Try to read at least 8192 bytes to avoid to many small reads.
271 SkAutoTMalloc<uint8> tempBuffer(sizeToRead); 271 const size_t kMinSizeToRead = 8192;
272 const size_t sizeRequested = newSize - fStreamBuffer.bytesWritten();
273 const size_t sizeToRead = SkTMax(kMinSizeToRead, sizeRequested);
274 SkAutoSTMalloc<kMinSizeToRead, uint8> tempBuffer(sizeToRead);
272 const size_t bytesRead = fStream->read(tempBuffer.get(), sizeToRead); 275 const size_t bytesRead = fStream->read(tempBuffer.get(), sizeToRead);
273 if (bytesRead != sizeToRead) { 276 if (bytesRead < sizeRequested) {
274 return false; 277 return false;
275 } 278 }
276 return fStreamBuffer.write(tempBuffer.get(), bytesRead); 279 return fStreamBuffer.write(tempBuffer.get(), bytesRead);
277 } 280 }
278 281
279 SkAutoTDelete<SkStream> fStream; 282 SkAutoTDelete<SkStream> fStream;
280 bool fWholeStreamRead; 283 bool fWholeStreamRead;
281 284
282 SkDynamicMemoryWStream fStreamBuffer; 285 SkDynamicMemoryWStream fStreamBuffer;
283 286
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 }; 434 };
432 435
433 /* 436 /*
434 * Tries to handle the image with PIEX. If PIEX returns kOk and finds the previe w image, create a 437 * Tries to handle the image with PIEX. If PIEX returns kOk and finds the previe w image, create a
435 * SkJpegCodec. If PIEX returns kFail, then the file is invalid, return nullptr. In other cases, 438 * SkJpegCodec. If PIEX returns kFail, then the file is invalid, return nullptr. In other cases,
436 * fallback to create SkRawCodec for DNG images. 439 * fallback to create SkRawCodec for DNG images.
437 */ 440 */
438 SkCodec* SkRawCodec::NewFromStream(SkStream* stream) { 441 SkCodec* SkRawCodec::NewFromStream(SkStream* stream) {
439 SkAutoTDelete<SkRawStream> rawStream(new SkRawStream(stream)); 442 SkAutoTDelete<SkRawStream> rawStream(new SkRawStream(stream));
440 ::piex::PreviewImageData imageData; 443 ::piex::PreviewImageData imageData;
441 // FIXME: ::piex::GetPreviewImageData() calls GetData() frequently with smal l amounts,
442 // resulting in many calls to bufferMoreData(). Could we make this more effi cient by grouping
443 // smaller requests together?
444 if (::piex::IsRaw(rawStream.get())) { 444 if (::piex::IsRaw(rawStream.get())) {
445 ::piex::Error error = ::piex::GetPreviewImageData(rawStream.get(), &imag eData); 445 ::piex::Error error = ::piex::GetPreviewImageData(rawStream.get(), &imag eData);
446 446
447 if (error == ::piex::Error::kOk && imageData.preview_length > 0) { 447 if (error == ::piex::Error::kOk && imageData.preview_length > 0) {
448 #if !defined(GOOGLE3) 448 #if !defined(GOOGLE3)
449 // transferBuffer() is destructive to the rawStream. Abandon the raw Stream after this 449 // transferBuffer() is destructive to the rawStream. Abandon the raw Stream after this
450 // function call. 450 // function call.
451 // FIXME: one may avoid the copy of memoryStream and use the buffere d rawStream. 451 // FIXME: one may avoid the copy of memoryStream and use the buffere d rawStream.
452 SkMemoryStream* memoryStream = 452 SkMemoryStream* memoryStream =
453 rawStream->transferBuffer(imageData.preview_offset, imageDat a.preview_length); 453 rawStream->transferBuffer(imageData.preview_offset, imageDat a.preview_length);
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
559 SkISize sizeFloor = this->onGetScaledDimensions(1.f / std::floor(fullShortEd ge / shortEdge)); 559 SkISize sizeFloor = this->onGetScaledDimensions(1.f / std::floor(fullShortEd ge / shortEdge));
560 SkISize sizeCeil = this->onGetScaledDimensions(1.f / std::ceil(fullShortEdge / shortEdge)); 560 SkISize sizeCeil = this->onGetScaledDimensions(1.f / std::ceil(fullShortEdge / shortEdge));
561 return sizeFloor == dim || sizeCeil == dim; 561 return sizeFloor == dim || sizeCeil == dim;
562 } 562 }
563 563
564 SkRawCodec::~SkRawCodec() {} 564 SkRawCodec::~SkRawCodec() {}
565 565
566 SkRawCodec::SkRawCodec(SkDngImage* dngImage) 566 SkRawCodec::SkRawCodec(SkDngImage* dngImage)
567 : INHERITED(dngImage->getImageInfo(), nullptr) 567 : INHERITED(dngImage->getImageInfo(), nullptr)
568 , fDngImage(dngImage) {} 568 , fDngImage(dngImage) {}
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698