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

Unified Diff: src/codec/SkSampledCodec.cpp

Issue 1417583009: Combine native sampling with sampling (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Work around missing initializer_list on mac Created 5 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/codec/SkSampledCodec.h ('k') | tests/CodexTest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/codec/SkSampledCodec.cpp
diff --git a/src/codec/SkSampledCodec.cpp b/src/codec/SkSampledCodec.cpp
index 229555b9bf549998a434b2339fac2eb5618586a6..ed4eb7f02de94cab2b411291805e06d4fdf703b4 100644
--- a/src/codec/SkSampledCodec.cpp
+++ b/src/codec/SkSampledCodec.cpp
@@ -7,6 +7,7 @@
#include "SkCodec.h"
#include "SkCodecPriv.h"
+#include "SkMath.h"
#include "SkSampledCodec.h"
SkSampledCodec::SkSampledCodec(SkCodec* codec)
@@ -14,30 +15,58 @@ SkSampledCodec::SkSampledCodec(SkCodec* codec)
, fCodec(codec)
{}
-SkISize SkSampledCodec::onGetSampledDimensions(int sampleSize) const {
- // Fast path for when we are not scaling.
- if (1 == sampleSize) {
- return fCodec->getInfo().dimensions();
+SkISize SkSampledCodec::accountForNativeScaling(int* sampleSizePtr, int* nativeSampleSize) const {
+ SkISize preSampledSize = fCodec->getInfo().dimensions();
+ int sampleSize = *sampleSizePtr;
+ SkASSERT(sampleSize > 1);
+
+ if (nativeSampleSize) {
+ *nativeSampleSize = 1;
}
- const int width = fCodec->getInfo().width();
- const int height = fCodec->getInfo().height();
-
- // Check if the codec can provide the scaling natively.
- float scale = get_scale_from_sample_size(sampleSize);
- SkSize idealSize = SkSize::Make(scale * (float) width, scale * (float) height);
- SkISize nativeSize = fCodec->getScaledDimensions(scale);
- float widthDiff = SkTAbs(((float) nativeSize.width()) - idealSize.width());
- float heightDiff = SkTAbs(((float) nativeSize.height()) - idealSize.height());
- // Native scaling is preferred to sampling. If we can scale natively to
- // within one of the ideal value, we should choose to scale natively.
- if (widthDiff < 1.0f && heightDiff < 1.0f) {
- return nativeSize;
+ // Only JPEG supports native downsampling.
+ if (fCodec->getEncodedFormat() == kJPEG_SkEncodedFormat) {
+ // See if libjpeg supports this scale directly
+ switch (sampleSize) {
+ case 2:
+ case 4:
+ case 8:
+ // This class does not need to do any sampling.
+ *sampleSizePtr = 1;
+ return fCodec->getScaledDimensions(get_scale_from_sample_size(sampleSize));
+ default:
+ break;
+ }
+
+ // Check if sampleSize is a multiple of something libjpeg can support.
+ int remainder;
+ const int sampleSizes[] = { 8, 4, 2 };
+ for (int supportedSampleSize : sampleSizes) {
+ int actualSampleSize;
+ SkTDivMod(sampleSize, supportedSampleSize, &actualSampleSize, &remainder);
+ if (0 == remainder) {
+ float scale = get_scale_from_sample_size(supportedSampleSize);
+
+ // fCodec will scale to this size.
+ preSampledSize = fCodec->getScaledDimensions(scale);
+
+ // And then this class will sample it.
+ *sampleSizePtr = actualSampleSize;
+ if (nativeSampleSize) {
+ *nativeSampleSize = supportedSampleSize;
+ }
+ break;
+ }
+ }
}
- // Provide the scaling by sampling.
- return SkISize::Make(get_scaled_dimension(width, sampleSize),
- get_scaled_dimension(height, sampleSize));
+ return preSampledSize;
+}
+
+SkISize SkSampledCodec::onGetSampledDimensions(int sampleSize) const {
+ const SkISize size = this->accountForNativeScaling(&sampleSize);
+ return SkISize::Make(get_scaled_dimension(size.width(), sampleSize),
+ get_scaled_dimension(size.height(), sampleSize));
}
SkCodec::Result SkSampledCodec::onGetAndroidPixels(const SkImageInfo& info, void* pixels,
@@ -59,7 +88,7 @@ SkCodec::Result SkSampledCodec::onGetAndroidPixels(const SkImageInfo& info, void
// We are performing a subset decode.
int sampleSize = options.fSampleSize;
- SkISize scaledSize = this->onGetSampledDimensions(sampleSize);
+ SkISize scaledSize = this->getSampledDimensions(sampleSize);
if (!fCodec->dimensionsSupported(scaledSize)) {
// If the native codec does not support the requested scale, scale by sampling.
return this->sampledDecode(info, pixels, rowBytes, options);
@@ -107,31 +136,46 @@ SkCodec::Result SkSampledCodec::onGetAndroidPixels(const SkImageInfo& info, void
SkCodec::Result SkSampledCodec::sampledDecode(const SkImageInfo& info, void* pixels,
size_t rowBytes, AndroidOptions& options) {
+ // We should only call this function when sampling.
+ SkASSERT(options.fSampleSize > 1);
+
// Create options struct for the codec.
SkCodec::Options sampledOptions;
sampledOptions.fZeroInitialized = options.fZeroInitialized;
+ // FIXME: This was already called by onGetAndroidPixels. Can we reduce that?
+ int sampleSize = options.fSampleSize;
+ int nativeSampleSize;
+ SkISize nativeSize = this->accountForNativeScaling(&sampleSize, &nativeSampleSize);
+
// Check if there is a subset.
SkIRect subset;
int subsetY = 0;
- int subsetWidth = fCodec->getInfo().width();
- int subsetHeight = fCodec->getInfo().height();
+ int subsetWidth = nativeSize.width();
+ int subsetHeight = nativeSize.height();
if (options.fSubset) {
// We will need to know about subsetting in the y-dimension in order to use the
// scanline decoder.
+ // Update the subset to account for scaling done by fCodec.
SkIRect* subsetPtr = options.fSubset;
- subsetY = subsetPtr->y();
- subsetWidth = subsetPtr->width();
- subsetHeight = subsetPtr->height();
+
+ // Do the divide ourselves, instead of calling get_scaled_dimension. If
+ // X and Y are 0, they should remain 0, rather than being upgraded to 1
+ // due to being smaller than the sampleSize.
+ const int subsetX = subsetPtr->x() / nativeSampleSize;
+ subsetY = subsetPtr->y() / nativeSampleSize;
+
+ subsetWidth = get_scaled_dimension(subsetPtr->width(), nativeSampleSize);
+ subsetHeight = get_scaled_dimension(subsetPtr->height(), nativeSampleSize);
// The scanline decoder only needs to be aware of subsetting in the x-dimension.
- subset.setXYWH(subsetPtr->x(), 0, subsetWidth, fCodec->getInfo().height());
+ subset.setXYWH(subsetX, 0, subsetWidth, nativeSize.height());
sampledOptions.fSubset = &subset;
}
// Start the scanline decode.
SkCodec::Result result = fCodec->startScanlineDecode(
- info.makeWH(fCodec->getInfo().width(), fCodec->getInfo().height()), &sampledOptions,
+ info.makeWH(nativeSize.width(), nativeSize.height()), &sampledOptions,
options.fColorPtr, options.fColorCount);
if (SkCodec::kSuccess != result) {
return result;
« no previous file with comments | « src/codec/SkSampledCodec.h ('k') | tests/CodexTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698