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

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

Issue 1260673002: SkScaledCodec class (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: add helper functions to calculate sampleSize and scaledDimensions Created 5 years, 4 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
OLDNEW
1 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 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 "SkJpegCodec.h" 9 #include "SkJpegCodec.h"
10 #include "SkJpegDecoderMgr.h" 10 #include "SkJpegDecoderMgr.h"
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 dinfo.global_state = DSTATE_READY; 182 dinfo.global_state = DSTATE_READY;
183 dinfo.num_components = 0; 183 dinfo.num_components = 0;
184 dinfo.scale_num = num; 184 dinfo.scale_num = num;
185 dinfo.scale_denom = denom; 185 dinfo.scale_denom = denom;
186 turbo_jpeg_calc_output_dimensions(&dinfo); 186 turbo_jpeg_calc_output_dimensions(&dinfo);
187 187
188 // Return the calculated output dimensions for the given scale 188 // Return the calculated output dimensions for the given scale
189 return SkISize::Make(dinfo.output_width, dinfo.output_height); 189 return SkISize::Make(dinfo.output_width, dinfo.output_height);
190 } 190 }
191 191
192 SkCodec::Result SkJpegCodec::initializeSwizzler(const SkImageInfo& requestedInfo ,
193 void* dst, size_t rowBytes,
194 const Options& options,
195 SkPMColor ctable[],
196 int* ctableCount) {
197
198 const SkColorType srcColorType = requestedInfo.colorType();
199 SkSwizzler::SrcConfig srcConfig;
200 switch (srcColorType) {
201 case kGray_8_SkColorType:
202 srcConfig = SkSwizzler::kGray;
203 break;
204 case kRGBA_8888_SkColorType:
205 srcConfig = SkSwizzler::kRGBX;
206 break;
207 case kBGRA_8888_SkColorType:
208 srcConfig = SkSwizzler::kBGRX;
209 break;
210 case kRGB_565_SkColorType:
211 srcConfig = SkSwizzler::kRGB_565;
212 break;
213 default:
214 //would have exited before now if the colorType was supported by jpe g
215 SkASSERT(false);
216 }
217
218 fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, NULL, requestedInfo, d st, rowBytes,
219 options.fZeroInitialized, this->g etInfo()));
220 if (!fSwizzler) {
221 // FIXME: CreateSwizzler could fail for another reason.
222 return kUnimplemented;
223 }
224 return kSuccess;
225 }
226
192 /* 227 /*
193 * Handles rewinding the input stream if it is necessary 228 * Handles rewinding the input stream if it is necessary
194 */ 229 */
195 bool SkJpegCodec::handleRewind() { 230 bool SkJpegCodec::handleRewind() {
196 switch(this->rewindIfNeeded()) { 231 switch(this->rewindIfNeeded()) {
197 case kCouldNotRewind_RewindState: 232 case kCouldNotRewind_RewindState:
198 return fDecoderMgr->returnFalse("could not rewind"); 233 return fDecoderMgr->returnFalse("could not rewind");
199 case kRewound_RewindState: { 234 case kRewound_RewindState: {
200 JpegDecoderMgr* decoderMgr = NULL; 235 JpegDecoderMgr* decoderMgr = NULL;
201 if (!ReadHeader(this->stream(), NULL, &decoderMgr)) { 236 if (!ReadHeader(this->stream(), NULL, &decoderMgr)) {
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 fDecoderMgr->dinfo()->scale_denom = 8; 315 fDecoderMgr->dinfo()->scale_denom = 8;
281 fDecoderMgr->dinfo()->scale_num = 8; 316 fDecoderMgr->dinfo()->scale_num = 8;
282 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); 317 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
283 while (fDecoderMgr->dinfo()->output_width != dstWidth || 318 while (fDecoderMgr->dinfo()->output_width != dstWidth ||
284 fDecoderMgr->dinfo()->output_height != dstHeight) { 319 fDecoderMgr->dinfo()->output_height != dstHeight) {
285 320
286 // Return a failure if we have tried all of the possible scales 321 // Return a failure if we have tried all of the possible scales
287 if (1 == fDecoderMgr->dinfo()->scale_num || 322 if (1 == fDecoderMgr->dinfo()->scale_num ||
288 dstWidth > fDecoderMgr->dinfo()->output_width || 323 dstWidth > fDecoderMgr->dinfo()->output_width ||
289 dstHeight > fDecoderMgr->dinfo()->output_height) { 324 dstHeight > fDecoderMgr->dinfo()->output_height) {
325 this->fDecoderMgr->dinfo()->scale_num = 8;
326 turbo_jpeg_calc_output_dimensions(this->fDecoderMgr->dinfo());
290 return fDecoderMgr->returnFalse("could not scale to requested dimens ions"); 327 return fDecoderMgr->returnFalse("could not scale to requested dimens ions");
291 } 328 }
292 329
293 // Try the next scale 330 // Try the next scale
294 fDecoderMgr->dinfo()->scale_num -= 1; 331 fDecoderMgr->dinfo()->scale_num -= 1;
295 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo()); 332 turbo_jpeg_calc_output_dimensions(fDecoderMgr->dinfo());
296 } 333 }
297 return true; 334 return true;
298 } 335 }
299 336
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 } 415 }
379 416
380 /* 417 /*
381 * Enable scanline decoding for jpegs 418 * Enable scanline decoding for jpegs
382 */ 419 */
383 class SkJpegScanlineDecoder : public SkScanlineDecoder { 420 class SkJpegScanlineDecoder : public SkScanlineDecoder {
384 public: 421 public:
385 SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec) 422 SkJpegScanlineDecoder(const SkImageInfo& dstInfo, SkJpegCodec* codec)
386 : INHERITED(dstInfo) 423 : INHERITED(dstInfo)
387 , fCodec(codec) 424 , fCodec(codec)
388 {} 425 {
426 fStorage.reset(fCodec->getInfo().width() * dstInfo.minRowBytes());
scroggo 2015/07/30 18:28:23 We only need to do this if there is a swizzler, ri
emmaleer 2015/07/30 22:27:56 Yes, I've added an if statement to only do this if
427 fSrcRow = static_cast<uint8_t*>(fStorage.get());
428 }
389 429
390 virtual ~SkJpegScanlineDecoder() { 430 virtual ~SkJpegScanlineDecoder() {
391 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { 431 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
392 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n"); 432 SkCodecPrintf("setjmp: Error in libjpeg finish_decompress\n");
393 return; 433 return;
394 } 434 }
395 435
396 // We may not have decoded the entire image. Prevent libjpeg-turbo from failing on a 436 // We may not have decoded the entire image. Prevent libjpeg-turbo from failing on a
397 // partial decode. 437 // partial decode.
398 fCodec->fDecoderMgr->dinfo()->output_scanline = fCodec->getInfo().height (); 438 fCodec->fDecoderMgr->dinfo()->output_scanline = fCodec->getInfo().height ();
399 turbo_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo()); 439 turbo_jpeg_finish_decompress(fCodec->fDecoderMgr->dinfo());
400 } 440 }
401 441
402 SkCodec::Result onGetScanlines(void* dst, int count, size_t rowBytes) overri de { 442 SkCodec::Result onGetScanlines(void* dst, int count, size_t rowBytes) overri de {
403 // Set the jump location for libjpeg errors 443 // Set the jump location for libjpeg errors
404 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { 444 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
405 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali dInput); 445 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali dInput);
406 } 446 }
447 // Read rows one at a time
448 JSAMPLE* dstRow;
449 if (fCodec->fSwizzler) {
scroggo 2015/07/30 18:28:23 So it looks like these introduce a slight performa
emmaleer 2015/07/30 22:27:56 I think it's negligible. Also, I don't think this
msarett 2015/07/31 12:17:59 I think Leon's suggestion is a good one. We did r
emmaleer 2015/07/31 18:41:56 So I re-ran the performance tests, after changing
scroggo 2015/07/31 19:31:58 That's great! I'm a little surprised that it is fa
emmaleer 2015/07/31 20:43:09 Yes, it is comparing running nanobench on JPEG, wi
450 // write data to storage row, then sample using swizzler
451 dstRow = fSrcRow;
452 } else {
453 // write data directly to dst
454 dstRow = (JSAMPLE*) dst;
455 }
407 456
408 // Read rows one at a time
409 JSAMPLE* dstRow = (JSAMPLE*) dst;
410 for (int y = 0; y < count; y++) { 457 for (int y = 0; y < count; y++) {
411 // Read row of the image 458 // Read row of the image
412 uint32_t rowsDecoded = 459 uint32_t rowsDecoded =
413 turbo_jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dst Row, 1); 460 turbo_jpeg_read_scanlines(fCodec->fDecoderMgr->dinfo(), &dst Row, 1);
414 if (rowsDecoded != 1) { 461 if (rowsDecoded != 1) {
415 SkSwizzler::Fill( 462 SkSwizzler::Fill(
416 dstRow, this->dstInfo(), rowBytes, count - y, SK_ColorBL ACK, NULL); 463 dstRow, this->dstInfo(), rowBytes, count - y, SK_ColorBL ACK, NULL);
417 fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo(). height(); 464 fCodec->fDecoderMgr->dinfo()->output_scanline = this->dstInfo(). height();
418 return SkCodec::kIncompleteInput; 465 return SkCodec::kIncompleteInput;
419 } 466 }
420 467
421 // Convert to RGBA if necessary 468 // Convert to RGBA if necessary
422 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) { 469 if (JCS_CMYK == fCodec->fDecoderMgr->dinfo()->out_color_space) {
423 convert_CMYK_to_RGBA(dstRow, this->dstInfo().width()); 470 convert_CMYK_to_RGBA(dstRow, this->dstInfo().width());
424 } 471 }
425 472
426 // Move to the next row 473 if(fCodec->fSwizzler) {
427 dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes); 474 // use swizzler to sample row
475 fCodec->fSwizzler->setDstRow(dst);
476 fCodec->fSwizzler->next(dstRow);
477 dst += rowBytes;
scroggo 2015/07/30 18:28:23 Why not use SkTAddOffset?
emmaleer 2015/07/30 22:27:56 Changed to to SkTAddOffset.
478 } else {
479 dstRow = SkTAddOffset<JSAMPLE>(dstRow, rowBytes);
480 }
428 } 481 }
429
430 return SkCodec::kSuccess; 482 return SkCodec::kSuccess;
431 } 483 }
432 484
433 #ifndef TURBO_HAS_SKIP 485 #ifndef TURBO_HAS_SKIP
434 #define turbo_jpeg_skip_scanlines(dinfo, count) \ 486 #define turbo_jpeg_skip_scanlines(dinfo, count) \
435 SkAutoMalloc storage(dinfo->output_width * dinfo->out_color_components); \ 487 SkAutoMalloc storage(dinfo->output_width * dinfo->out_color_components); \
436 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); \ 488 uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); \
437 for (int y = 0; y < count; y++) { \ 489 for (int y = 0; y < count; y++) { \
438 turbo_jpeg_read_scanlines(dinfo, &storagePtr, 1); \ 490 turbo_jpeg_read_scanlines(dinfo, &storagePtr, 1); \
439 } 491 }
440 #endif 492 #endif
441 493
442 SkCodec::Result onSkipScanlines(int count) override { 494 SkCodec::Result onSkipScanlines(int count) override {
443 // Set the jump location for libjpeg errors 495 // Set the jump location for libjpeg errors
444 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) { 496 if (setjmp(fCodec->fDecoderMgr->getJmpBuf())) {
445 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali dInput); 497 return fCodec->fDecoderMgr->returnFailure("setjmp", SkCodec::kInvali dInput);
446 } 498 }
447 499
448 turbo_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count); 500 turbo_jpeg_skip_scanlines(fCodec->fDecoderMgr->dinfo(), count);
449 501
450 return SkCodec::kSuccess; 502 return SkCodec::kSuccess;
451 } 503 }
452 504
453 private: 505 private:
454 SkAutoTDelete<SkJpegCodec> fCodec; 506 SkAutoTDelete<SkJpegCodec> fCodec;
507 SkAutoMalloc fStorage;
scroggo 2015/07/30 18:28:23 // Only used if sampling is needed.
emmaleer 2015/07/30 22:27:56 Acknowledged.
508 uint8_t* fSrcRow;
455 509
456 typedef SkScanlineDecoder INHERITED; 510 typedef SkScanlineDecoder INHERITED;
457 }; 511 };
458 512
459 SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo, 513 SkScanlineDecoder* SkJpegCodec::onGetScanlineDecoder(const SkImageInfo& dstInfo,
460 const Options& options, SkPMColor ctable[], int* ctableCount) { 514 const Options& options, SkPMColor ctable[], int* ctableCount) {
461 515
462 // Rewind the stream if needed 516 // Rewind the stream if needed
463 if (!this->handleRewind()) { 517 if (!this->handleRewind()) {
464 SkCodecPrintf("Could not rewind\n"); 518 SkCodecPrintf("Could not rewind\n");
(...skipping 16 matching lines...) Expand all
481 } 535 }
482 536
483 // Check if we can decode to the requested destination and set the output co lor space 537 // Check if we can decode to the requested destination and set the output co lor space
484 if (!codec->setOutputColorSpace(dstInfo)) { 538 if (!codec->setOutputColorSpace(dstInfo)) {
485 SkCodecPrintf("Cannot convert to output type\n"); 539 SkCodecPrintf("Cannot convert to output type\n");
486 return NULL; 540 return NULL;
487 } 541 }
488 542
489 // Perform the necessary scaling 543 // Perform the necessary scaling
490 if (!codec->scaleToDimensions(dstInfo.width(), dstInfo.height())) { 544 if (!codec->scaleToDimensions(dstInfo.width(), dstInfo.height())) {
491 SkCodecPrintf("Cannot scale to output dimensions\n"); 545 // native scaling to dstInfo dimensions not supported
492 return NULL; 546
547 if (dstInfo.height() != this->getInfo().height()) {
548 // no non-native height sampling supported
549 return NULL;
550 }
551 // create swizzler for sampling
552 if (codec->initializeSwizzler(dstInfo, NULL, dstInfo.minRowBytes(), opti ons, ctable,
553 ctableCount) != kSuccess) {
554 SkCodecPrintf("failed to initialize the swizzler.\n");
555 return NULL;
556 }
493 } 557 }
494 558
495 // Now, given valid output dimensions, we can start the decompress 559 // Now, given valid output dimensions, we can start the decompress
496 if (!turbo_jpeg_start_decompress(codec->fDecoderMgr->dinfo())) { 560 if (!turbo_jpeg_start_decompress(codec->fDecoderMgr->dinfo())) {
497 SkCodecPrintf("start decompress failed\n"); 561 SkCodecPrintf("start decompress failed\n");
498 return NULL; 562 return NULL;
499 } 563 }
500 564
501 // Return the new scanline decoder 565 // Return the new scanline decoder
502 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, codec.detach())); 566 return SkNEW_ARGS(SkJpegScanlineDecoder, (dstInfo, codec.detach()));
503 } 567 }
OLDNEW
« no previous file with comments | « src/codec/SkJpegCodec.h ('k') | src/codec/SkScaledCodec.cpp » ('j') | src/codec/SkSwizzler.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698