SkPDF: use SkImage::isAImage

output size savings = ~0.4% with effected gms and skps.

BUG=568816
BUG=skia:5592

GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2760

Change-Id: Ifead46ea5789e18aa3ddea9ca3986717296a6391
Reviewed-on: https://skia-review.googlesource.com/2760
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Hal Canary <halcanary@google.com>
This commit is contained in:
Hal Canary 2016-09-28 11:53:33 -04:00 committed by Skia Commit-Bot
parent e3a4e993ef
commit 47bf4c0009

View File

@ -1219,59 +1219,64 @@ SkPDFShader::State::State(SkShader* shader, const SkMatrix& canvasTransform,
fInfo.fColorCount = 0;
fInfo.fColors = nullptr;
fInfo.fColorOffsets = nullptr;
fShaderTransform = shader->getLocalMatrix();
fImageTileModes[0] = fImageTileModes[1] = SkShader::kClamp_TileMode;
fType = shader->asAGradient(&fInfo);
if (fType == SkShader::kNone_GradientType) {
if (!shader->isABitmap(imageDst, nullptr, fImageTileModes)) {
// Generic fallback for unsupported shaders:
// * allocate a bbox-sized bitmap
// * shade the whole area
// * use the result as a bitmap shader
// bbox is in device space. While that's exactly what we
// want for sizing our bitmap, we need to map it into
// shader space for adjustments (to match
// MakeImageShader's behavior).
SkRect shaderRect = SkRect::Make(bbox);
if (!inverse_transform_bbox(canvasTransform, &shaderRect)) {
imageDst->reset();
return;
}
// Clamp the bitmap size to about 1M pixels
static const SkScalar kMaxBitmapArea = 1024 * 1024;
SkScalar bitmapArea = rasterScale * bbox.width() * rasterScale * bbox.height();
if (bitmapArea > kMaxBitmapArea) {
rasterScale *= SkScalarSqrt(kMaxBitmapArea / bitmapArea);
}
SkISize size = SkISize::Make(SkScalarRoundToInt(rasterScale * bbox.width()),
SkScalarRoundToInt(rasterScale * bbox.height()));
SkSize scale = SkSize::Make(SkIntToScalar(size.width()) / shaderRect.width(),
SkIntToScalar(size.height()) / shaderRect.height());
imageDst->allocN32Pixels(size.width(), size.height());
imageDst->eraseColor(SK_ColorTRANSPARENT);
SkPaint p;
p.setShader(sk_ref_sp(shader));
SkCanvas canvas(*imageDst);
canvas.scale(scale.width(), scale.height());
canvas.translate(-shaderRect.x(), -shaderRect.y());
canvas.drawPaint(p);
fShaderTransform.setTranslate(shaderRect.x(), shaderRect.y());
fShaderTransform.preScale(1 / scale.width(), 1 / scale.height());
}
fBitmapKey = SkBitmapKey(*imageDst);
} else {
if (fType != SkShader::kNone_GradientType) {
fShaderTransform = shader->getLocalMatrix();
this->allocateGradientInfoStorage();
shader->asAGradient(&fInfo);
return;
}
if (SkImage* skimg = shader->isAImage(&fShaderTransform, fImageTileModes)) {
// TODO(halcanary): delay converting to bitmap.
if (skimg->asLegacyBitmap(imageDst, SkImage::kRO_LegacyBitmapMode)) {
fBitmapKey = SkBitmapKey(*imageDst);
return;
}
}
fShaderTransform = shader->getLocalMatrix();
// Generic fallback for unsupported shaders:
// * allocate a bbox-sized bitmap
// * shade the whole area
// * use the result as a bitmap shader
// bbox is in device space. While that's exactly what we
// want for sizing our bitmap, we need to map it into
// shader space for adjustments (to match
// MakeImageShader's behavior).
SkRect shaderRect = SkRect::Make(bbox);
if (!inverse_transform_bbox(canvasTransform, &shaderRect)) {
imageDst->reset();
return;
}
// Clamp the bitmap size to about 1M pixels
static const SkScalar kMaxBitmapArea = 1024 * 1024;
SkScalar bitmapArea = rasterScale * bbox.width() * rasterScale * bbox.height();
if (bitmapArea > kMaxBitmapArea) {
rasterScale *= SkScalarSqrt(kMaxBitmapArea / bitmapArea);
}
SkISize size = SkISize::Make(SkScalarRoundToInt(rasterScale * bbox.width()),
SkScalarRoundToInt(rasterScale * bbox.height()));
SkSize scale = SkSize::Make(SkIntToScalar(size.width()) / shaderRect.width(),
SkIntToScalar(size.height()) / shaderRect.height());
imageDst->allocN32Pixels(size.width(), size.height());
imageDst->eraseColor(SK_ColorTRANSPARENT);
SkPaint p;
p.setShader(sk_ref_sp(shader));
SkCanvas canvas(*imageDst);
canvas.scale(scale.width(), scale.height());
canvas.translate(-shaderRect.x(), -shaderRect.y());
canvas.drawPaint(p);
fShaderTransform.setTranslate(shaderRect.x(), shaderRect.y());
fShaderTransform.preScale(1 / scale.width(), 1 / scale.height());
fBitmapKey = SkBitmapKey(*imageDst);
}
SkPDFShader::State::State(const SkPDFShader::State& other)