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

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

Issue 1719073002: Use new jpeg_crop_scanlines() API to optimize jpeg subset decodes (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Response to comments from previous patch set 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 | « src/codec/SkJpegCodec.h ('k') | 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 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 "SkMSAN.h" 9 #include "SkMSAN.h"
10 #include "SkJpegCodec.h" 10 #include "SkJpegCodec.h"
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 return codec; 73 return codec;
74 } 74 }
75 return nullptr; 75 return nullptr;
76 } 76 }
77 77
78 SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream, 78 SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream,
79 JpegDecoderMgr* decoderMgr) 79 JpegDecoderMgr* decoderMgr)
80 : INHERITED(srcInfo, stream) 80 : INHERITED(srcInfo, stream)
81 , fDecoderMgr(decoderMgr) 81 , fDecoderMgr(decoderMgr)
82 , fReadyState(decoderMgr->dinfo()->global_state) 82 , fReadyState(decoderMgr->dinfo()->global_state)
83 , fSwizzlerSubset(SkIRect::MakeEmpty())
83 {} 84 {}
84 85
85 /* 86 /*
86 * Return the row bytes of a particular image type and width 87 * Return the row bytes of a particular image type and width
87 */ 88 */
88 static size_t get_row_bytes(const j_decompress_ptr dinfo) { 89 static size_t get_row_bytes(const j_decompress_ptr dinfo) {
89 #ifdef TURBO_HAS_565 90 #ifdef TURBO_HAS_565
90 const size_t colorBytes = (dinfo->out_color_space == JCS_RGB565) ? 2 : 91 const size_t colorBytes = (dinfo->out_color_space == JCS_RGB565) ? 2 :
91 dinfo->out_color_components; 92 dinfo->out_color_components;
92 #else 93 #else
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 default: 365 default:
365 // This function should only be called if the colorType is suppo rted by jpeg 366 // This function should only be called if the colorType is suppo rted by jpeg
366 SkASSERT(false); 367 SkASSERT(false);
367 } 368 }
368 } 369 }
369 370
370 if (JCS_RGB == fDecoderMgr->dinfo()->out_color_space) { 371 if (JCS_RGB == fDecoderMgr->dinfo()->out_color_space) {
371 srcConfig = SkSwizzler::kRGB; 372 srcConfig = SkSwizzler::kRGB;
372 } 373 }
373 374
374 fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, nullptr, dstInfo, opti ons)); 375 Options swizzlerOptions = options;
376 if (options.fSubset) {
377 // Use fSwizzlerSubset if this is a subset decode. This is necessary in the case
378 // where libjpeg-turbo provides a subset and then we need to subset it f urther.
379 // Also, verify that fSwizzlerSubset is non-empty, meaning that we've in itialized it.
380 SkASSERT(!fSwizzlerSubset.isEmpty());
scroggo 2016/02/22 19:20:20 Come to think of it, this only catches an error th
msarett 2016/02/22 19:32:51 Adding to the assert. We can't assert "contains"
scroggo 2016/02/22 19:37:40 sgtm
381 swizzlerOptions.fSubset = &fSwizzlerSubset;
382 }
383 fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, nullptr, dstInfo, swiz zlerOptions));
375 SkASSERT(fSwizzler); 384 SkASSERT(fSwizzler);
376 fStorage.reset(get_row_bytes(fDecoderMgr->dinfo())); 385 fStorage.reset(get_row_bytes(fDecoderMgr->dinfo()));
377 fSrcRow = fStorage.get(); 386 fSrcRow = fStorage.get();
378 } 387 }
379 388
380 SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) { 389 SkSampler* SkJpegCodec::getSampler(bool createIfNecessary) {
381 if (!createIfNecessary || fSwizzler) { 390 if (!createIfNecessary || fSwizzler) {
382 SkASSERT(!fSwizzler || (fSrcRow && fStorage.get() == fSrcRow)); 391 SkASSERT(!fSwizzler || (fSrcRow && fStorage.get() == fSrcRow));
383 return fSwizzler; 392 return fSwizzler;
384 } 393 }
(...skipping 19 matching lines...) Expand all
404 fSwizzler.reset(nullptr); 413 fSwizzler.reset(nullptr);
405 fSrcRow = nullptr; 414 fSrcRow = nullptr;
406 fStorage.free(); 415 fStorage.free();
407 416
408 // Now, given valid output dimensions, we can start the decompress 417 // Now, given valid output dimensions, we can start the decompress
409 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) { 418 if (!jpeg_start_decompress(fDecoderMgr->dinfo())) {
410 SkCodecPrintf("start decompress failed\n"); 419 SkCodecPrintf("start decompress failed\n");
411 return kInvalidInput; 420 return kInvalidInput;
412 } 421 }
413 422
423 if (options.fSubset) {
424 fSwizzlerSubset = *options.fSubset;
425 }
426
427 #ifdef TURBO_HAS_CROP
428 if (options.fSubset) {
429 uint32_t startX = options.fSubset->x();
430 uint32_t width = options.fSubset->width();
431
432 // libjpeg-turbo may need to align startX to a multiple of the IDCT
433 // block size. If this is the case, it will decrease the value of
434 // startX to the appropriate alignment and also increase the value
435 // of width so that the right edge of the requested subset remains
436 // the same.
437 jpeg_crop_scanline(fDecoderMgr->dinfo(), &startX, &width);
438
439 SkASSERT(startX <= (uint32_t) options.fSubset->x());
440 SkASSERT(width >= (uint32_t) options.fSubset->width());
441 SkASSERT(startX + width >= (uint32_t) options.fSubset->right());
442
443 // Instruct the swizzler (if it is necessary) to further subset the
444 // output provided by libjpeg-turbo.
445 //
446 // We set this here (rather than in the if statement below), so that
447 // if (1) we don't need a swizzler for the subset, and (2) we need a
448 // swizzler for CMYK, the swizzler will still use the proper subset
449 // dimensions.
450 //
451 // Note that the swizzler will ignore the y and height parameters of
452 // the subset. Since the scanline decoder (and the swizzler) handle
453 // one row at a time, only the subsetting in the x-dimension matters.
454 fSwizzlerSubset.setXYWH(options.fSubset->x() - startX, 0,
455 options.fSubset->width(), options.fSubset->height());
456
457 // We will need a swizzler if libjpeg-turbo cannot provide the exact
458 // subset that we request.
459 if (startX != (uint32_t) options.fSubset->x() ||
460 width != (uint32_t) options.fSubset->width()) {
461 this->initializeSwizzler(dstInfo, options);
462 }
463 }
464
465 // Make sure we have a swizzler if we are converting from CMYK.
466 if (!fSwizzler && JCS_CMYK == fDecoderMgr->dinfo()->out_color_space) {
467 this->initializeSwizzler(dstInfo, options);
468 }
469 #else
414 // We will need a swizzler if we are performing a subset decode or 470 // We will need a swizzler if we are performing a subset decode or
415 // converting from CMYK. 471 // converting from CMYK.
416 J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->out_color_space; 472 J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->out_color_space;
417 if (options.fSubset || JCS_CMYK == colorSpace || JCS_RGB == colorSpace) { 473 if (options.fSubset || JCS_CMYK == colorSpace || JCS_RGB == colorSpace) {
418 this->initializeSwizzler(dstInfo, options); 474 this->initializeSwizzler(dstInfo, options);
419 } 475 }
476 #endif
420 477
421 return kSuccess; 478 return kSuccess;
422 } 479 }
423 480
424 int SkJpegCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) { 481 int SkJpegCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
425 // Set the jump location for libjpeg errors 482 // Set the jump location for libjpeg errors
426 if (setjmp(fDecoderMgr->getJmpBuf())) { 483 if (setjmp(fDecoderMgr->getJmpBuf())) {
427 return fDecoderMgr->returnFailure("setjmp", kInvalidInput); 484 return fDecoderMgr->returnFailure("setjmp", kInvalidInput);
428 } 485 }
429 // Read rows one at a time 486 // Read rows one at a time
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
666 723
667 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock); 724 JDIMENSION linesRead = jpeg_read_raw_data(dinfo, yuv, numRowsPerBlock);
668 if (linesRead < remainingRows) { 725 if (linesRead < remainingRows) {
669 // FIXME: Handle incomplete YUV decodes without signalling an error. 726 // FIXME: Handle incomplete YUV decodes without signalling an error.
670 return kInvalidInput; 727 return kInvalidInput;
671 } 728 }
672 } 729 }
673 730
674 return kSuccess; 731 return kSuccess;
675 } 732 }
OLDNEW
« no previous file with comments | « src/codec/SkJpegCodec.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698