2015-03-25 14:11:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2015-01-15 18:56:12 +00:00
|
|
|
#include "DMSrcSink.h"
|
|
|
|
#include "SamplePipeControllers.h"
|
2015-03-03 16:59:20 +00:00
|
|
|
#include "SkCodec.h"
|
2015-05-05 18:38:45 +00:00
|
|
|
#include "SkCommonFlags.h"
|
2015-03-25 20:13:43 +00:00
|
|
|
#include "SkData.h"
|
2015-05-05 15:11:33 +00:00
|
|
|
#include "SkDeferredCanvas.h"
|
2015-01-15 18:56:12 +00:00
|
|
|
#include "SkDocument.h"
|
2015-02-25 22:09:45 +00:00
|
|
|
#include "SkError.h"
|
2015-05-07 17:53:34 +00:00
|
|
|
#include "SkFunction.h"
|
2015-03-25 20:13:43 +00:00
|
|
|
#include "SkImageGenerator.h"
|
2015-01-15 18:56:12 +00:00
|
|
|
#include "SkMultiPictureDraw.h"
|
2015-02-13 23:11:10 +00:00
|
|
|
#include "SkNullCanvas.h"
|
2015-01-15 18:56:12 +00:00
|
|
|
#include "SkOSFile.h"
|
2015-03-16 17:38:07 +00:00
|
|
|
#include "SkPictureData.h"
|
2015-01-15 18:56:12 +00:00
|
|
|
#include "SkPictureRecorder.h"
|
|
|
|
#include "SkRandom.h"
|
2015-05-05 19:59:56 +00:00
|
|
|
#include "SkRecordDraw.h"
|
|
|
|
#include "SkRecorder.h"
|
2015-02-06 20:51:10 +00:00
|
|
|
#include "SkSVGCanvas.h"
|
2015-05-05 18:38:45 +00:00
|
|
|
#include "SkScanlineDecoder.h"
|
2015-01-21 20:09:53 +00:00
|
|
|
#include "SkStream.h"
|
2015-02-06 20:51:10 +00:00
|
|
|
#include "SkXMLWriter.h"
|
2015-01-15 18:56:12 +00:00
|
|
|
|
2015-04-14 21:06:18 +00:00
|
|
|
DEFINE_bool(multiPage, false, "For document-type backends, render the source"
|
|
|
|
" into multiple pages");
|
|
|
|
|
2015-03-25 20:13:43 +00:00
|
|
|
static bool lazy_decode_bitmap(const void* src, size_t size, SkBitmap* dst) {
|
|
|
|
SkAutoTUnref<SkData> encoded(SkData::NewWithCopy(src, size));
|
|
|
|
return encoded && SkInstallDiscardablePixelRef(encoded, dst);
|
|
|
|
}
|
|
|
|
|
2015-01-15 18:56:12 +00:00
|
|
|
namespace DM {
|
|
|
|
|
|
|
|
GMSrc::GMSrc(skiagm::GMRegistry::Factory factory) : fFactory(factory) {}
|
|
|
|
|
|
|
|
Error GMSrc::draw(SkCanvas* canvas) const {
|
|
|
|
SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
|
|
|
|
canvas->concat(gm->getInitialTransform());
|
|
|
|
gm->draw(canvas);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize GMSrc::size() const {
|
|
|
|
SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
|
|
|
|
return gm->getISize();
|
|
|
|
}
|
|
|
|
|
|
|
|
Name GMSrc::name() const {
|
|
|
|
SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
|
|
|
|
return gm->getName();
|
|
|
|
}
|
|
|
|
|
2015-05-27 20:23:23 +00:00
|
|
|
void GMSrc::modifyGrContextOptions(GrContextOptions* options) const {
|
|
|
|
SkAutoTDelete<skiagm::GM> gm(fFactory(NULL));
|
|
|
|
gm->modifyGrContextOptions(options);
|
|
|
|
}
|
|
|
|
|
2015-01-15 18:56:12 +00:00
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
2015-06-11 21:27:27 +00:00
|
|
|
CodecSrc::CodecSrc(Path path, Mode mode, DstColorType dstColorType, float scale)
|
2015-04-09 19:43:10 +00:00
|
|
|
: fPath(path)
|
|
|
|
, fMode(mode)
|
|
|
|
, fDstColorType(dstColorType)
|
2015-06-11 21:27:27 +00:00
|
|
|
, fScale(scale)
|
2015-04-09 19:43:10 +00:00
|
|
|
{}
|
2015-03-19 13:03:39 +00:00
|
|
|
|
|
|
|
Error CodecSrc::draw(SkCanvas* canvas) const {
|
|
|
|
SkImageInfo canvasInfo;
|
|
|
|
if (NULL == canvas->peekPixels(&canvasInfo, NULL)) {
|
|
|
|
// TODO: Once we implement GPU paths (e.g. JPEG YUV), we should use a deferred decode to
|
|
|
|
// let the GPU handle it.
|
|
|
|
return Error::Nonfatal("No need to test decoding to non-raster backend.");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
|
|
|
|
if (!encoded) {
|
|
|
|
return SkStringPrintf("Couldn't read %s.", fPath.c_str());
|
|
|
|
}
|
|
|
|
SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
|
2015-04-09 19:43:10 +00:00
|
|
|
if (NULL == codec.get()) {
|
|
|
|
return SkStringPrintf("Couldn't create codec for %s.", fPath.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Choose the color type to decode to
|
|
|
|
SkImageInfo decodeInfo = codec->getInfo();
|
|
|
|
SkColorType canvasColorType = canvasInfo.colorType();
|
|
|
|
switch (fDstColorType) {
|
|
|
|
case kIndex8_Always_DstColorType:
|
2015-04-15 14:32:19 +00:00
|
|
|
decodeInfo = codec->getInfo().makeColorType(kIndex_8_SkColorType);
|
|
|
|
if (kRGB_565_SkColorType == canvasColorType) {
|
|
|
|
return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
|
|
|
|
}
|
|
|
|
break;
|
2015-04-09 19:43:10 +00:00
|
|
|
case kGrayscale_Always_DstColorType:
|
2015-04-15 14:32:19 +00:00
|
|
|
decodeInfo = codec->getInfo().makeColorType(kGray_8_SkColorType);
|
2015-04-09 19:43:10 +00:00
|
|
|
if (kRGB_565_SkColorType == canvasColorType) {
|
|
|
|
return Error::Nonfatal("Testing non-565 to 565 is uninteresting.");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
decodeInfo = decodeInfo.makeColorType(canvasColorType);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-06-11 21:27:27 +00:00
|
|
|
// Try to scale the image if it is desired
|
|
|
|
SkISize size = codec->getScaledDimensions(fScale);
|
|
|
|
if (size == decodeInfo.dimensions() && 1.0f != fScale) {
|
|
|
|
return Error::Nonfatal("Test without scaling is uninteresting.");
|
|
|
|
}
|
|
|
|
decodeInfo = decodeInfo.makeWH(size.width(), size.height());
|
|
|
|
|
2015-04-09 19:43:10 +00:00
|
|
|
// Construct a color table for the decode if necessary
|
|
|
|
SkAutoTUnref<SkColorTable> colorTable(NULL);
|
|
|
|
SkPMColor* colorPtr = NULL;
|
|
|
|
int* colorCountPtr = NULL;
|
|
|
|
int maxColors = 256;
|
|
|
|
if (kIndex_8_SkColorType == decodeInfo.colorType()) {
|
|
|
|
SkPMColor colors[256];
|
|
|
|
colorTable.reset(SkNEW_ARGS(SkColorTable, (colors, maxColors)));
|
|
|
|
colorPtr = const_cast<SkPMColor*>(colorTable->readColors());
|
|
|
|
colorCountPtr = &maxColors;
|
2015-03-19 13:03:39 +00:00
|
|
|
}
|
|
|
|
|
2015-04-09 19:43:10 +00:00
|
|
|
// FIXME: Currently we cannot draw unpremultiplied sources.
|
2015-03-19 13:03:39 +00:00
|
|
|
if (decodeInfo.alphaType() == kUnpremul_SkAlphaType) {
|
|
|
|
decodeInfo = decodeInfo.makeAlphaType(kPremul_SkAlphaType);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkBitmap bitmap;
|
2015-04-09 19:43:10 +00:00
|
|
|
if (!bitmap.tryAllocPixels(decodeInfo, NULL, colorTable.get())) {
|
2015-03-19 13:03:39 +00:00
|
|
|
return SkStringPrintf("Image(%s) is too large (%d x %d)\n", fPath.c_str(),
|
|
|
|
decodeInfo.width(), decodeInfo.height());
|
|
|
|
}
|
|
|
|
|
2015-03-25 20:48:49 +00:00
|
|
|
switch (fMode) {
|
2015-06-22 17:40:21 +00:00
|
|
|
case kNormal_Mode: {
|
2015-04-09 19:43:10 +00:00
|
|
|
switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes(), NULL,
|
|
|
|
colorPtr, colorCountPtr)) {
|
2015-03-25 20:48:49 +00:00
|
|
|
case SkImageGenerator::kSuccess:
|
|
|
|
// We consider incomplete to be valid, since we should still decode what is
|
|
|
|
// available.
|
|
|
|
case SkImageGenerator::kIncompleteInput:
|
|
|
|
break;
|
|
|
|
case SkImageGenerator::kInvalidConversion:
|
|
|
|
return Error::Nonfatal("Incompatible colortype conversion");
|
|
|
|
default:
|
|
|
|
// Everything else is considered a failure.
|
|
|
|
return SkStringPrintf("Couldn't getPixels %s.", fPath.c_str());
|
|
|
|
}
|
2015-05-27 19:36:10 +00:00
|
|
|
canvas->drawBitmap(bitmap, 0, 0);
|
2015-03-25 20:48:49 +00:00
|
|
|
break;
|
2015-06-22 17:40:21 +00:00
|
|
|
}
|
2015-03-25 20:48:49 +00:00
|
|
|
case kScanline_Mode: {
|
2015-04-29 14:38:43 +00:00
|
|
|
SkScanlineDecoder* scanlineDecoder = codec->getScanlineDecoder(decodeInfo, NULL,
|
|
|
|
colorPtr, colorCountPtr);
|
2015-03-25 20:48:49 +00:00
|
|
|
if (NULL == scanlineDecoder) {
|
|
|
|
return Error::Nonfatal("Cannot use scanline decoder for all images");
|
|
|
|
}
|
2015-06-22 17:40:21 +00:00
|
|
|
const SkImageGenerator::Result result = scanlineDecoder->getScanlines(
|
|
|
|
bitmap.getAddr(0, 0), decodeInfo.height(), bitmap.rowBytes());
|
|
|
|
switch (result) {
|
|
|
|
case SkImageGenerator::kSuccess:
|
|
|
|
case SkImageGenerator::kIncompleteInput:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return SkStringPrintf("%s failed with error message %d",
|
|
|
|
fPath.c_str(), (int) result);
|
2015-03-25 20:48:49 +00:00
|
|
|
}
|
2015-05-27 19:36:10 +00:00
|
|
|
canvas->drawBitmap(bitmap, 0, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kScanline_Subset_Mode: {
|
|
|
|
//this mode decodes the image in divisor*divisor subsets, using a scanline decoder
|
|
|
|
const int divisor = 2;
|
|
|
|
const int w = decodeInfo.width();
|
|
|
|
const int h = decodeInfo.height();
|
|
|
|
if (divisor > w || divisor > h) {
|
2015-06-19 14:44:05 +00:00
|
|
|
return Error::Nonfatal(SkStringPrintf("Cannot decode subset: divisor %d is too big"
|
|
|
|
"for %s with dimensions (%d x %d)", divisor, fPath.c_str(), w, h));
|
2015-05-27 19:36:10 +00:00
|
|
|
}
|
|
|
|
const int subsetWidth = w/divisor;
|
|
|
|
const int subsetHeight = h/divisor;
|
|
|
|
// One of our subsets will be larger to contain any pixels that do not divide evenly.
|
|
|
|
const int extraX = w % divisor;
|
|
|
|
const int extraY = h % divisor;
|
|
|
|
/*
|
|
|
|
* if w or h are not evenly divided by divisor need to adjust width and height of end
|
|
|
|
* subsets to cover entire image.
|
|
|
|
* Add extraX and extraY to largestSubsetBm's width and height to adjust width
|
|
|
|
* and height of end subsets.
|
|
|
|
* subsetBm is extracted from largestSubsetBm.
|
|
|
|
* subsetBm's size is determined based on the current subset and may be larger for end
|
|
|
|
* subsets.
|
|
|
|
*/
|
2015-06-11 21:27:27 +00:00
|
|
|
SkImageInfo largestSubsetDecodeInfo =
|
2015-05-27 19:36:10 +00:00
|
|
|
decodeInfo.makeWH(subsetWidth + extraX, subsetHeight + extraY);
|
|
|
|
SkBitmap largestSubsetBm;
|
|
|
|
if (!largestSubsetBm.tryAllocPixels(largestSubsetDecodeInfo, NULL, colorTable.get())) {
|
|
|
|
return SkStringPrintf("Image(%s) is too large (%d x %d)\n", fPath.c_str(),
|
|
|
|
largestSubsetDecodeInfo.width(), largestSubsetDecodeInfo.height());
|
|
|
|
}
|
2015-06-22 17:40:21 +00:00
|
|
|
const size_t rowBytes = decodeInfo.minRowBytes();
|
|
|
|
char* buffer = SkNEW_ARRAY(char, largestSubsetDecodeInfo.height() * rowBytes);
|
|
|
|
SkAutoTDeleteArray<char> lineDeleter(buffer);
|
2015-05-27 19:36:10 +00:00
|
|
|
for (int col = 0; col < divisor; col++) {
|
|
|
|
//currentSubsetWidth may be larger than subsetWidth for rightmost subsets
|
|
|
|
const int currentSubsetWidth = (col + 1 == divisor) ?
|
|
|
|
subsetWidth + extraX : subsetWidth;
|
|
|
|
const int x = col * subsetWidth;
|
|
|
|
for (int row = 0; row < divisor; row++) {
|
|
|
|
//currentSubsetHeight may be larger than subsetHeight for bottom subsets
|
|
|
|
const int currentSubsetHeight = (row + 1 == divisor) ?
|
|
|
|
subsetHeight + extraY : subsetHeight;
|
|
|
|
const int y = row * subsetHeight;
|
|
|
|
//create scanline decoder for each subset
|
|
|
|
SkScanlineDecoder* subsetScanlineDecoder = codec->getScanlineDecoder(decodeInfo,
|
|
|
|
NULL, colorPtr, colorCountPtr);
|
|
|
|
if (NULL == subsetScanlineDecoder) {
|
|
|
|
if (x == 0 && y == 0) {
|
|
|
|
//first try, image may not be compatible
|
|
|
|
return Error::Nonfatal("Cannot use scanline decoder for all images");
|
|
|
|
} else {
|
|
|
|
return "Error scanline decoder is NULL";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//skip to first line of subset
|
2015-06-11 21:27:27 +00:00
|
|
|
const SkImageGenerator::Result skipResult =
|
2015-05-27 19:36:10 +00:00
|
|
|
subsetScanlineDecoder->skipScanlines(y);
|
|
|
|
switch (skipResult) {
|
|
|
|
case SkImageGenerator::kSuccess:
|
|
|
|
case SkImageGenerator::kIncompleteInput:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return SkStringPrintf("%s failed after attempting to skip %d scanlines"
|
|
|
|
"with error message %d", fPath.c_str(), y, (int) skipResult);
|
|
|
|
}
|
|
|
|
//create and set size of subsetBm
|
|
|
|
SkBitmap subsetBm;
|
|
|
|
SkIRect bounds = SkIRect::MakeWH(subsetWidth, subsetHeight);
|
|
|
|
bounds.setXYWH(0, 0, currentSubsetWidth, currentSubsetHeight);
|
|
|
|
SkAssertResult(largestSubsetBm.extractSubset(&subsetBm, bounds));
|
|
|
|
SkAutoLockPixels autlockSubsetBm(subsetBm, true);
|
2015-06-22 17:40:21 +00:00
|
|
|
const SkImageGenerator::Result subsetResult =
|
|
|
|
subsetScanlineDecoder->getScanlines(buffer, currentSubsetHeight, rowBytes);
|
|
|
|
switch (subsetResult) {
|
|
|
|
case SkImageGenerator::kSuccess:
|
|
|
|
case SkImageGenerator::kIncompleteInput:
|
|
|
|
break;
|
|
|
|
default:
|
2015-07-07 16:43:28 +00:00
|
|
|
return SkStringPrintf("%s failed with error message %d",
|
2015-06-22 17:40:21 +00:00
|
|
|
fPath.c_str(), (int) subsetResult);
|
|
|
|
}
|
|
|
|
const size_t bpp = decodeInfo.bytesPerPixel();
|
2015-07-07 16:43:28 +00:00
|
|
|
/*
|
|
|
|
* we copy all the lines at once becuase when calling getScanlines for
|
|
|
|
* interlaced pngs the entire image must be read regardless of the number
|
2015-06-22 17:40:21 +00:00
|
|
|
* of lines requested. Reading an interlaced png in a loop, line-by-line, would
|
|
|
|
* decode the entire image height times, which is very slow
|
|
|
|
* it is aknowledged that copying each line as you read it in a loop
|
|
|
|
* may be faster for other types of images. Since this is a correctness test
|
|
|
|
* that's okay.
|
|
|
|
*/
|
2015-07-07 16:43:28 +00:00
|
|
|
char* bufferRow = buffer;
|
2015-05-27 19:36:10 +00:00
|
|
|
for (int subsetY = 0; subsetY < currentSubsetHeight; ++subsetY) {
|
2015-07-07 16:43:28 +00:00
|
|
|
memcpy(subsetBm.getAddr(0, subsetY), bufferRow + x*bpp,
|
2015-06-22 17:40:21 +00:00
|
|
|
currentSubsetWidth*bpp);
|
|
|
|
bufferRow += rowBytes;
|
2015-05-27 19:36:10 +00:00
|
|
|
}
|
2015-07-07 16:43:28 +00:00
|
|
|
|
2015-05-27 19:36:10 +00:00
|
|
|
canvas->drawBitmap(subsetBm, SkIntToScalar(x), SkIntToScalar(y));
|
|
|
|
}
|
|
|
|
}
|
2015-03-25 20:48:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-06-11 21:27:27 +00:00
|
|
|
case kStripe_Mode: {
|
|
|
|
const int height = decodeInfo.height();
|
|
|
|
// This value is chosen arbitrarily. We exercise more cases by choosing a value that
|
|
|
|
// does not align with image blocks.
|
|
|
|
const int stripeHeight = 37;
|
|
|
|
const int numStripes = (height + stripeHeight - 1) / stripeHeight;
|
|
|
|
|
|
|
|
// Decode odd stripes
|
|
|
|
SkScanlineDecoder* decoder = codec->getScanlineDecoder(decodeInfo, NULL, colorPtr,
|
|
|
|
colorCountPtr);
|
|
|
|
if (NULL == decoder) {
|
|
|
|
return Error::Nonfatal("Cannot use scanline decoder for all images");
|
|
|
|
}
|
|
|
|
for (int i = 0; i < numStripes; i += 2) {
|
|
|
|
// Skip a stripe
|
|
|
|
const int linesToSkip = SkTMin(stripeHeight, height - i * stripeHeight);
|
|
|
|
SkImageGenerator::Result result = decoder->skipScanlines(linesToSkip);
|
|
|
|
switch (result) {
|
|
|
|
case SkImageGenerator::kSuccess:
|
|
|
|
case SkImageGenerator::kIncompleteInput:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return SkStringPrintf("Cannot skip scanlines for %s.", fPath.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read a stripe
|
|
|
|
const int startY = (i + 1) * stripeHeight;
|
|
|
|
const int linesToRead = SkTMin(stripeHeight, height - startY);
|
|
|
|
if (linesToRead > 0) {
|
|
|
|
result = decoder->getScanlines(bitmap.getAddr(0, startY),
|
|
|
|
linesToRead, bitmap.rowBytes());
|
|
|
|
switch (result) {
|
|
|
|
case SkImageGenerator::kSuccess:
|
|
|
|
case SkImageGenerator::kIncompleteInput:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return SkStringPrintf("Cannot get scanlines for %s.", fPath.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode even stripes
|
|
|
|
decoder = codec->getScanlineDecoder(decodeInfo, NULL, colorPtr, colorCountPtr);
|
|
|
|
if (NULL == decoder) {
|
|
|
|
return "Failed to create second scanline decoder.";
|
|
|
|
}
|
|
|
|
for (int i = 0; i < numStripes; i += 2) {
|
|
|
|
// Read a stripe
|
|
|
|
const int startY = i * stripeHeight;
|
|
|
|
const int linesToRead = SkTMin(stripeHeight, height - startY);
|
|
|
|
SkImageGenerator::Result result = decoder->getScanlines(bitmap.getAddr(0, startY),
|
|
|
|
linesToRead, bitmap.rowBytes());
|
|
|
|
switch (result) {
|
|
|
|
case SkImageGenerator::kSuccess:
|
|
|
|
case SkImageGenerator::kIncompleteInput:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return SkStringPrintf("Cannot get scanlines for %s.", fPath.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip a stripe
|
2015-06-12 16:34:04 +00:00
|
|
|
const int linesToSkip = SkTMin(stripeHeight, height - (i + 1) * stripeHeight);
|
|
|
|
if (linesToSkip > 0) {
|
|
|
|
result = decoder->skipScanlines(linesToSkip);
|
|
|
|
switch (result) {
|
|
|
|
case SkImageGenerator::kSuccess:
|
|
|
|
case SkImageGenerator::kIncompleteInput:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return SkStringPrintf("Cannot skip scanlines for %s.", fPath.c_str());
|
|
|
|
}
|
2015-06-11 21:27:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas->drawBitmap(bitmap, 0, 0);
|
2015-06-22 17:40:21 +00:00
|
|
|
break;
|
2015-06-11 21:27:27 +00:00
|
|
|
}
|
2015-03-19 13:03:39 +00:00
|
|
|
}
|
2015-03-25 20:48:49 +00:00
|
|
|
return "";
|
2015-03-19 13:03:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkISize CodecSrc::size() const {
|
|
|
|
SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
|
|
|
|
SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(encoded));
|
2015-03-25 12:27:48 +00:00
|
|
|
if (NULL != codec) {
|
2015-06-11 21:27:27 +00:00
|
|
|
SkISize size = codec->getScaledDimensions(fScale);
|
|
|
|
return size;
|
2015-03-25 12:27:48 +00:00
|
|
|
} else {
|
|
|
|
return SkISize::Make(0, 0);
|
|
|
|
}
|
2015-03-19 13:03:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Name CodecSrc::name() const {
|
2015-06-11 21:27:27 +00:00
|
|
|
if (1.0f == fScale) {
|
|
|
|
return SkOSPath::Basename(fPath.c_str());
|
|
|
|
} else {
|
|
|
|
return SkStringPrintf("%s_%.3f", SkOSPath::Basename(fPath.c_str()).c_str(), fScale);
|
|
|
|
}
|
2015-03-19 13:03:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
2015-01-30 21:22:23 +00:00
|
|
|
ImageSrc::ImageSrc(Path path, int divisor) : fPath(path), fDivisor(divisor) {}
|
2015-01-15 18:56:12 +00:00
|
|
|
|
|
|
|
Error ImageSrc::draw(SkCanvas* canvas) const {
|
2015-03-19 13:03:39 +00:00
|
|
|
SkImageInfo canvasInfo;
|
|
|
|
if (NULL == canvas->peekPixels(&canvasInfo, NULL)) {
|
|
|
|
// TODO: Instead, use lazy decoding to allow the GPU to handle cases like YUV.
|
|
|
|
return Error::Nonfatal("No need to test decoding to non-raster backend.");
|
|
|
|
}
|
|
|
|
|
2015-01-18 15:05:01 +00:00
|
|
|
SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
|
2015-01-15 18:56:12 +00:00
|
|
|
if (!encoded) {
|
|
|
|
return SkStringPrintf("Couldn't read %s.", fPath.c_str());
|
|
|
|
}
|
2015-03-19 13:03:39 +00:00
|
|
|
const SkColorType dstColorType = canvasInfo.colorType();
|
2015-01-30 21:22:23 +00:00
|
|
|
if (fDivisor == 0) {
|
2015-01-15 18:56:12 +00:00
|
|
|
// Decode the full image.
|
|
|
|
SkBitmap bitmap;
|
2015-03-19 13:03:39 +00:00
|
|
|
if (!SkImageDecoder::DecodeMemory(encoded->data(), encoded->size(), &bitmap,
|
|
|
|
dstColorType, SkImageDecoder::kDecodePixels_Mode)) {
|
|
|
|
return SkStringPrintf("Couldn't decode %s.", fPath.c_str());
|
|
|
|
}
|
|
|
|
if (kRGB_565_SkColorType == dstColorType && !bitmap.isOpaque()) {
|
|
|
|
// Do not draw a bitmap with alpha to a destination without alpha.
|
|
|
|
return Error::Nonfatal("Uninteresting to decode image with alpha into 565.");
|
2015-01-15 18:56:12 +00:00
|
|
|
}
|
2015-01-18 15:05:01 +00:00
|
|
|
encoded.reset((SkData*)NULL); // Might as well drop this when we're done with it.
|
2015-01-15 18:56:12 +00:00
|
|
|
canvas->drawBitmap(bitmap, 0,0);
|
|
|
|
return "";
|
|
|
|
}
|
2015-01-30 21:22:23 +00:00
|
|
|
// Decode subsets. This is a little involved.
|
2015-01-21 20:09:53 +00:00
|
|
|
SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(encoded));
|
|
|
|
SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream.get()));
|
2015-01-15 18:56:12 +00:00
|
|
|
if (!decoder) {
|
|
|
|
return SkStringPrintf("Can't find a good decoder for %s.", fPath.c_str());
|
|
|
|
}
|
2015-01-21 20:09:53 +00:00
|
|
|
stream->rewind();
|
2015-01-15 18:56:12 +00:00
|
|
|
int w,h;
|
2015-06-19 14:44:05 +00:00
|
|
|
if (!decoder->buildTileIndex(stream.detach(), &w, &h)) {
|
2015-03-05 16:40:28 +00:00
|
|
|
return Error::Nonfatal("Subset decoding not supported.");
|
2015-01-15 18:56:12 +00:00
|
|
|
}
|
2015-01-30 21:22:23 +00:00
|
|
|
|
|
|
|
// Divide the image into subsets that cover the entire image.
|
|
|
|
if (fDivisor > w || fDivisor > h) {
|
2015-06-19 14:44:05 +00:00
|
|
|
return Error::Nonfatal(SkStringPrintf("Cannot decode subset: divisor %d is too big"
|
|
|
|
"for %s with dimensions (%d x %d)", fDivisor, fPath.c_str(), w, h));
|
2015-01-30 21:22:23 +00:00
|
|
|
}
|
|
|
|
const int subsetWidth = w / fDivisor,
|
|
|
|
subsetHeight = h / fDivisor;
|
|
|
|
for (int y = 0; y < h; y += subsetHeight) {
|
|
|
|
for (int x = 0; x < w; x += subsetWidth) {
|
|
|
|
SkBitmap subset;
|
|
|
|
SkIRect rect = SkIRect::MakeXYWH(x, y, subsetWidth, subsetHeight);
|
|
|
|
if (!decoder->decodeSubset(&subset, rect, dstColorType)) {
|
|
|
|
return SkStringPrintf("Could not decode subset (%d, %d, %d, %d).",
|
|
|
|
x, y, x+subsetWidth, y+subsetHeight);
|
|
|
|
}
|
2015-03-05 19:46:40 +00:00
|
|
|
if (kRGB_565_SkColorType == dstColorType && !subset.isOpaque()) {
|
|
|
|
// Do not draw a bitmap with alpha to a destination without alpha.
|
|
|
|
// This is not an error, but there is nothing interesting to show.
|
|
|
|
|
|
|
|
// This should only happen on the first iteration through the loop.
|
|
|
|
SkASSERT(0 == x && 0 == y);
|
|
|
|
|
|
|
|
return Error::Nonfatal("Uninteresting to decode image with alpha into 565.");
|
|
|
|
}
|
2015-01-30 21:22:23 +00:00
|
|
|
canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y));
|
2015-01-15 18:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize ImageSrc::size() const {
|
2015-01-18 15:05:01 +00:00
|
|
|
SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(fPath.c_str()));
|
2015-03-19 13:03:39 +00:00
|
|
|
SkBitmap bitmap;
|
|
|
|
if (!encoded || !SkImageDecoder::DecodeMemory(encoded->data(),
|
|
|
|
encoded->size(),
|
|
|
|
&bitmap,
|
|
|
|
kUnknown_SkColorType,
|
|
|
|
SkImageDecoder::kDecodeBounds_Mode)) {
|
|
|
|
return SkISize::Make(0,0);
|
2015-01-15 18:56:12 +00:00
|
|
|
}
|
2015-03-19 13:03:39 +00:00
|
|
|
return bitmap.dimensions();
|
2015-01-15 18:56:12 +00:00
|
|
|
}
|
|
|
|
|
2015-01-20 18:11:53 +00:00
|
|
|
Name ImageSrc::name() const {
|
2015-01-30 21:22:23 +00:00
|
|
|
return SkOSPath::Basename(fPath.c_str());
|
2015-01-20 18:11:53 +00:00
|
|
|
}
|
2015-01-15 18:56:12 +00:00
|
|
|
|
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
2015-01-28 23:32:24 +00:00
|
|
|
static const SkRect kSKPViewport = {0,0, 1000,1000};
|
|
|
|
|
2015-01-30 19:42:31 +00:00
|
|
|
SKPSrc::SKPSrc(Path path) : fPath(path) {}
|
2015-01-15 18:56:12 +00:00
|
|
|
|
|
|
|
Error SKPSrc::draw(SkCanvas* canvas) const {
|
2015-01-21 20:09:53 +00:00
|
|
|
SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
|
2015-01-18 15:05:01 +00:00
|
|
|
if (!stream) {
|
2015-01-15 18:56:12 +00:00
|
|
|
return SkStringPrintf("Couldn't read %s.", fPath.c_str());
|
|
|
|
}
|
2015-03-25 20:13:43 +00:00
|
|
|
SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(stream, &lazy_decode_bitmap));
|
2015-01-18 15:05:01 +00:00
|
|
|
if (!pic) {
|
|
|
|
return SkStringPrintf("Couldn't decode %s as a picture.", fPath.c_str());
|
|
|
|
}
|
|
|
|
stream.reset((SkStream*)NULL); // Might as well drop this when we're done with it.
|
2015-03-31 20:32:05 +00:00
|
|
|
|
2015-01-28 23:32:24 +00:00
|
|
|
canvas->clipRect(kSKPViewport);
|
2015-01-15 18:56:12 +00:00
|
|
|
canvas->drawPicture(pic);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
SkISize SKPSrc::size() const {
|
2015-03-16 17:38:07 +00:00
|
|
|
SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(fPath.c_str()));
|
|
|
|
if (!stream) {
|
|
|
|
return SkISize::Make(0,0);
|
|
|
|
}
|
|
|
|
SkPictInfo info;
|
|
|
|
if (!SkPicture::InternalOnly_StreamIsSKP(stream, &info)) {
|
|
|
|
return SkISize::Make(0,0);
|
|
|
|
}
|
|
|
|
SkRect viewport = kSKPViewport;
|
|
|
|
if (!viewport.intersect(info.fCullRect)) {
|
|
|
|
return SkISize::Make(0,0);
|
|
|
|
}
|
|
|
|
return viewport.roundOut().size();
|
2015-01-15 18:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
|
|
|
|
|
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
2015-02-13 23:11:10 +00:00
|
|
|
Error NullSink::draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const {
|
|
|
|
SkAutoTDelete<SkCanvas> canvas(SkCreateNullCanvas());
|
|
|
|
return src.draw(canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
2015-02-03 02:26:03 +00:00
|
|
|
DEFINE_bool(gpuStats, false, "Append GPU stats to the log for each GPU task?");
|
|
|
|
|
2015-01-15 20:46:02 +00:00
|
|
|
GPUSink::GPUSink(GrContextFactory::GLContextType ct,
|
|
|
|
GrGLStandard api,
|
|
|
|
int samples,
|
|
|
|
bool dfText,
|
|
|
|
bool threaded)
|
2015-01-15 18:56:12 +00:00
|
|
|
: fContextType(ct)
|
|
|
|
, fGpuAPI(api)
|
|
|
|
, fSampleCount(samples)
|
2015-01-15 20:46:02 +00:00
|
|
|
, fUseDFText(dfText)
|
|
|
|
, fThreaded(threaded) {}
|
2015-01-15 18:56:12 +00:00
|
|
|
|
|
|
|
int GPUSink::enclave() const {
|
2015-01-21 23:50:13 +00:00
|
|
|
return fThreaded ? kAnyThread_Enclave : kGPU_Enclave;
|
2015-01-15 18:56:12 +00:00
|
|
|
}
|
|
|
|
|
2015-02-25 22:09:45 +00:00
|
|
|
void PreAbandonGpuContextErrorHandler(SkError, void*) {}
|
|
|
|
|
2015-02-03 02:26:03 +00:00
|
|
|
Error GPUSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString* log) const {
|
2015-05-27 20:23:23 +00:00
|
|
|
GrContextOptions options;
|
|
|
|
src.modifyGrContextOptions(&options);
|
|
|
|
|
|
|
|
GrContextFactory factory(options);
|
2015-01-28 23:32:24 +00:00
|
|
|
const SkISize size = src.size();
|
2015-01-15 18:56:12 +00:00
|
|
|
const SkImageInfo info =
|
|
|
|
SkImageInfo::Make(size.width(), size.height(), kN32_SkColorType, kPremul_SkAlphaType);
|
|
|
|
SkAutoTUnref<SkSurface> surface(
|
2015-01-21 23:50:13 +00:00
|
|
|
NewGpuSurface(&factory, fContextType, fGpuAPI, info, fSampleCount, fUseDFText));
|
2015-01-15 18:56:12 +00:00
|
|
|
if (!surface) {
|
|
|
|
return "Could not create a surface.";
|
|
|
|
}
|
2015-02-25 22:09:45 +00:00
|
|
|
if (FLAGS_preAbandonGpuContext) {
|
|
|
|
SkSetErrorCallback(&PreAbandonGpuContextErrorHandler, NULL);
|
|
|
|
factory.abandonContexts();
|
|
|
|
}
|
2015-01-15 18:56:12 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
Error err = src.draw(canvas);
|
|
|
|
if (!err.isEmpty()) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
canvas->flush();
|
2015-02-03 02:26:03 +00:00
|
|
|
if (FLAGS_gpuStats) {
|
|
|
|
canvas->getGrContext()->dumpCacheStats(log);
|
|
|
|
canvas->getGrContext()->dumpGpuStats(log);
|
|
|
|
}
|
2015-01-15 18:56:12 +00:00
|
|
|
dst->allocPixels(info);
|
2015-02-25 22:09:45 +00:00
|
|
|
canvas->readPixels(dst, 0, 0);
|
2015-01-21 23:50:13 +00:00
|
|
|
if (FLAGS_abandonGpuContext) {
|
|
|
|
factory.abandonContexts();
|
|
|
|
}
|
2015-01-15 18:56:12 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
2015-03-03 17:13:09 +00:00
|
|
|
static Error draw_skdocument(const Src& src, SkDocument* doc, SkWStream* dst) {
|
|
|
|
// Print the given DM:Src to a document, breaking on 8.5x11 pages.
|
|
|
|
SkASSERT(doc);
|
2015-01-28 19:45:58 +00:00
|
|
|
int width = src.size().width(),
|
|
|
|
height = src.size().height();
|
|
|
|
|
2015-04-14 21:06:18 +00:00
|
|
|
if (FLAGS_multiPage) {
|
|
|
|
const int kLetterWidth = 612, // 8.5 * 72
|
|
|
|
kLetterHeight = 792; // 11 * 72
|
|
|
|
const SkRect letter = SkRect::MakeWH(SkIntToScalar(kLetterWidth),
|
|
|
|
SkIntToScalar(kLetterHeight));
|
|
|
|
|
|
|
|
int xPages = ((width - 1) / kLetterWidth) + 1;
|
|
|
|
int yPages = ((height - 1) / kLetterHeight) + 1;
|
|
|
|
|
|
|
|
for (int y = 0; y < yPages; ++y) {
|
|
|
|
for (int x = 0; x < xPages; ++x) {
|
|
|
|
int w = SkTMin(kLetterWidth, width - (x * kLetterWidth));
|
|
|
|
int h = SkTMin(kLetterHeight, height - (y * kLetterHeight));
|
|
|
|
SkCanvas* canvas =
|
|
|
|
doc->beginPage(SkIntToScalar(w), SkIntToScalar(h));
|
|
|
|
if (!canvas) {
|
|
|
|
return "SkDocument::beginPage(w,h) returned NULL";
|
|
|
|
}
|
|
|
|
canvas->clipRect(letter);
|
|
|
|
canvas->translate(-letter.width() * x, -letter.height() * y);
|
|
|
|
Error err = src.draw(canvas);
|
|
|
|
if (!err.isEmpty()) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
doc->endPage();
|
2015-01-28 19:45:58 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-14 21:06:18 +00:00
|
|
|
} else {
|
|
|
|
SkCanvas* canvas =
|
|
|
|
doc->beginPage(SkIntToScalar(width), SkIntToScalar(height));
|
|
|
|
if (!canvas) {
|
|
|
|
return "SkDocument::beginPage(w,h) returned NULL";
|
|
|
|
}
|
|
|
|
Error err = src.draw(canvas);
|
|
|
|
if (!err.isEmpty()) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
doc->endPage();
|
|
|
|
}
|
|
|
|
if (!doc->close()) {
|
|
|
|
return "SkDocument::close() returned false";
|
2015-01-15 18:56:12 +00:00
|
|
|
}
|
2015-01-28 19:45:58 +00:00
|
|
|
dst->flush();
|
2015-01-15 18:56:12 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2015-03-03 17:13:09 +00:00
|
|
|
PDFSink::PDFSink() {}
|
|
|
|
|
|
|
|
Error PDFSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
|
|
|
|
SkAutoTUnref<SkDocument> doc(SkDocument::CreatePDF(dst));
|
|
|
|
if (!doc) {
|
|
|
|
return "SkDocument::CreatePDF() returned NULL";
|
|
|
|
}
|
|
|
|
return draw_skdocument(src, doc.get(), dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
|
|
|
XPSSink::XPSSink() {}
|
|
|
|
|
|
|
|
Error XPSSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
|
|
|
|
SkAutoTUnref<SkDocument> doc(SkDocument::CreateXPS(dst));
|
|
|
|
if (!doc) {
|
|
|
|
return "SkDocument::CreateXPS() returned NULL";
|
|
|
|
}
|
|
|
|
return draw_skdocument(src, doc.get(), dst);
|
|
|
|
}
|
2015-01-15 18:56:12 +00:00
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
2015-01-28 19:35:18 +00:00
|
|
|
SKPSink::SKPSink() {}
|
|
|
|
|
2015-02-03 02:26:03 +00:00
|
|
|
Error SKPSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
|
2015-01-28 19:35:18 +00:00
|
|
|
SkSize size;
|
|
|
|
size = src.size();
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
Error err = src.draw(recorder.beginRecording(size.width(), size.height()));
|
|
|
|
if (!err.isEmpty()) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
SkAutoTUnref<SkPicture> pic(recorder.endRecording());
|
|
|
|
pic->serialize(dst);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
2015-02-01 04:00:58 +00:00
|
|
|
SVGSink::SVGSink() {}
|
|
|
|
|
2015-02-03 02:26:03 +00:00
|
|
|
Error SVGSink::draw(const Src& src, SkBitmap*, SkWStream* dst, SkString*) const {
|
2015-02-06 20:51:10 +00:00
|
|
|
SkAutoTDelete<SkXMLWriter> xmlWriter(SkNEW_ARGS(SkXMLStreamWriter, (dst)));
|
|
|
|
SkAutoTUnref<SkCanvas> canvas(SkSVGCanvas::Create(
|
|
|
|
SkRect::MakeWH(SkIntToScalar(src.size().width()), SkIntToScalar(src.size().height())),
|
|
|
|
xmlWriter));
|
|
|
|
return src.draw(canvas);
|
2015-02-01 04:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
2015-01-15 18:56:12 +00:00
|
|
|
RasterSink::RasterSink(SkColorType colorType) : fColorType(colorType) {}
|
|
|
|
|
2015-02-03 02:26:03 +00:00
|
|
|
Error RasterSink::draw(const Src& src, SkBitmap* dst, SkWStream*, SkString*) const {
|
2015-01-28 23:32:24 +00:00
|
|
|
const SkISize size = src.size();
|
2015-01-15 18:56:12 +00:00
|
|
|
// If there's an appropriate alpha type for this color type, use it, otherwise use premul.
|
|
|
|
SkAlphaType alphaType = kPremul_SkAlphaType;
|
|
|
|
(void)SkColorTypeValidateAlphaType(fColorType, alphaType, &alphaType);
|
|
|
|
|
|
|
|
dst->allocPixels(SkImageInfo::Make(size.width(), size.height(), fColorType, alphaType));
|
|
|
|
dst->eraseColor(SK_ColorTRANSPARENT);
|
|
|
|
SkCanvas canvas(*dst);
|
|
|
|
return src.draw(&canvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
2015-05-05 18:38:45 +00:00
|
|
|
// Handy for front-patching a Src. Do whatever up-front work you need, then call draw_to_canvas(),
|
2015-05-07 17:53:34 +00:00
|
|
|
// passing the Sink draw() arguments, a size, and a function draws into an SkCanvas.
|
2015-05-05 18:38:45 +00:00
|
|
|
// Several examples below.
|
|
|
|
|
|
|
|
static Error draw_to_canvas(Sink* sink, SkBitmap* bitmap, SkWStream* stream, SkString* log,
|
2015-05-07 17:53:34 +00:00
|
|
|
SkISize size, SkFunction<Error(SkCanvas*)> draw) {
|
2015-05-05 18:38:45 +00:00
|
|
|
class ProxySrc : public Src {
|
|
|
|
public:
|
2015-05-07 17:53:34 +00:00
|
|
|
ProxySrc(SkISize size, SkFunction<Error(SkCanvas*)> draw) : fSize(size), fDraw(draw) {}
|
2015-05-05 18:38:45 +00:00
|
|
|
Error draw(SkCanvas* canvas) const override { return fDraw(canvas); }
|
|
|
|
Name name() const override { sk_throw(); return ""; } // Won't be called.
|
|
|
|
SkISize size() const override { return fSize; }
|
|
|
|
private:
|
2015-05-07 17:53:34 +00:00
|
|
|
SkISize fSize;
|
|
|
|
SkFunction<Error(SkCanvas*)> fDraw;
|
2015-05-05 18:38:45 +00:00
|
|
|
};
|
|
|
|
return sink->draw(ProxySrc(size, draw), bitmap, stream, log);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
2015-02-17 19:13:33 +00:00
|
|
|
static SkISize auto_compute_translate(SkMatrix* matrix, int srcW, int srcH) {
|
|
|
|
SkRect bounds = SkRect::MakeIWH(srcW, srcH);
|
|
|
|
matrix->mapRect(&bounds);
|
|
|
|
matrix->postTranslate(-bounds.x(), -bounds.y());
|
|
|
|
return SkISize::Make(SkScalarRoundToInt(bounds.width()), SkScalarRoundToInt(bounds.height()));
|
|
|
|
}
|
|
|
|
|
2015-05-06 14:54:07 +00:00
|
|
|
ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
|
2015-01-15 18:56:12 +00:00
|
|
|
|
2015-02-03 02:26:03 +00:00
|
|
|
Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
|
2015-05-05 18:38:45 +00:00
|
|
|
SkMatrix matrix = fMatrix;
|
|
|
|
SkISize size = auto_compute_translate(&matrix, src.size().width(), src.size().height());
|
|
|
|
return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) {
|
|
|
|
canvas->concat(matrix);
|
|
|
|
return src.draw(canvas);
|
|
|
|
});
|
2015-01-15 18:56:12 +00:00
|
|
|
}
|
|
|
|
|
2015-02-17 19:13:33 +00:00
|
|
|
// Undoes any flip or 90 degree rotate without changing the scale of the bitmap.
|
|
|
|
// This should be pixel-preserving.
|
2015-05-06 14:54:07 +00:00
|
|
|
ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : Via(sink), fMatrix(matrix) {}
|
2015-02-17 19:13:33 +00:00
|
|
|
|
|
|
|
Error ViaUpright::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
|
|
|
|
Error err = fSink->draw(src, bitmap, stream, log);
|
|
|
|
if (!err.isEmpty()) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkMatrix inverse;
|
|
|
|
if (!fMatrix.rectStaysRect() || !fMatrix.invert(&inverse)) {
|
|
|
|
return "Cannot upright --matrix.";
|
|
|
|
}
|
|
|
|
SkMatrix upright = SkMatrix::I();
|
|
|
|
upright.setScaleX(SkScalarSignAsScalar(inverse.getScaleX()));
|
|
|
|
upright.setScaleY(SkScalarSignAsScalar(inverse.getScaleY()));
|
|
|
|
upright.setSkewX(SkScalarSignAsScalar(inverse.getSkewX()));
|
|
|
|
upright.setSkewY(SkScalarSignAsScalar(inverse.getSkewY()));
|
|
|
|
|
|
|
|
SkBitmap uprighted;
|
|
|
|
SkISize size = auto_compute_translate(&upright, bitmap->width(), bitmap->height());
|
|
|
|
uprighted.allocPixels(bitmap->info().makeWH(size.width(), size.height()));
|
|
|
|
|
|
|
|
SkCanvas canvas(uprighted);
|
|
|
|
canvas.concat(upright);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setXfermodeMode(SkXfermode::kSrc_Mode);
|
|
|
|
canvas.drawBitmap(*bitmap, 0, 0, &paint);
|
|
|
|
|
|
|
|
*bitmap = uprighted;
|
|
|
|
bitmap->lockPixels();
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2015-01-15 18:56:12 +00:00
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
2015-02-03 02:26:03 +00:00
|
|
|
Error ViaPipe::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
|
2015-05-05 18:38:45 +00:00
|
|
|
auto size = src.size();
|
|
|
|
return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) {
|
|
|
|
PipeController controller(canvas, &SkImageDecoder::DecodeMemory);
|
|
|
|
SkGPipeWriter pipe;
|
|
|
|
const uint32_t kFlags = 0; // We mirror SkDeferredCanvas, which doesn't use any flags.
|
|
|
|
return src.draw(pipe.startRecording(&controller, kFlags, size.width(), size.height()));
|
|
|
|
});
|
2015-01-15 18:56:12 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 15:11:33 +00:00
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
2015-05-05 18:38:45 +00:00
|
|
|
|
2015-05-05 15:11:33 +00:00
|
|
|
Error ViaDeferred::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
|
2015-05-05 18:38:45 +00:00
|
|
|
// We draw via a deferred canvas into a surface that's compatible with the original canvas,
|
|
|
|
// then snap that surface as an image and draw it into the original canvas.
|
|
|
|
return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) -> Error {
|
|
|
|
SkAutoTUnref<SkSurface> surface(canvas->newSurface(canvas->imageInfo()));
|
|
|
|
if (!surface.get()) {
|
|
|
|
return "can't make surface for deferred canvas";
|
|
|
|
}
|
|
|
|
SkAutoTDelete<SkDeferredCanvas> defcan(SkDeferredCanvas::Create(surface));
|
|
|
|
Error err = src.draw(defcan);
|
|
|
|
if (!err.isEmpty()) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
SkAutoTUnref<SkImage> image(defcan->newImageSnapshot());
|
|
|
|
if (!image) {
|
|
|
|
return "failed to create deferred image snapshot";
|
2015-05-05 15:11:33 +00:00
|
|
|
}
|
2015-05-05 18:38:45 +00:00
|
|
|
canvas->drawImage(image, 0, 0, NULL);
|
|
|
|
return "";
|
|
|
|
});
|
2015-05-05 15:11:33 +00:00
|
|
|
}
|
2015-05-05 18:38:45 +00:00
|
|
|
|
2015-01-15 18:56:12 +00:00
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
2015-05-05 18:38:45 +00:00
|
|
|
Error ViaSerialization::draw(
|
|
|
|
const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
|
2015-01-15 18:56:12 +00:00
|
|
|
// Record our Src into a picture.
|
2015-05-05 18:38:45 +00:00
|
|
|
auto size = src.size();
|
2015-01-15 18:56:12 +00:00
|
|
|
SkPictureRecorder recorder;
|
2015-05-05 18:38:45 +00:00
|
|
|
Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
|
|
|
|
SkIntToScalar(size.height())));
|
2015-01-15 18:56:12 +00:00
|
|
|
if (!err.isEmpty()) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
SkAutoTUnref<SkPicture> pic(recorder.endRecording());
|
|
|
|
|
|
|
|
// Serialize it and then deserialize it.
|
|
|
|
SkDynamicMemoryWStream wStream;
|
|
|
|
pic->serialize(&wStream);
|
2015-01-21 20:09:53 +00:00
|
|
|
SkAutoTDelete<SkStream> rStream(wStream.detachAsStream());
|
2015-03-25 20:13:43 +00:00
|
|
|
SkAutoTUnref<SkPicture> deserialized(SkPicture::CreateFromStream(rStream, &lazy_decode_bitmap));
|
2015-01-15 18:56:12 +00:00
|
|
|
|
2015-05-05 18:38:45 +00:00
|
|
|
return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) {
|
|
|
|
canvas->drawPicture(deserialized);
|
|
|
|
return "";
|
|
|
|
});
|
2015-01-15 18:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
|
|
|
ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
|
2015-05-06 14:54:07 +00:00
|
|
|
: Via(sink)
|
|
|
|
, fW(w)
|
2015-01-15 18:56:12 +00:00
|
|
|
, fH(h)
|
2015-05-06 14:54:07 +00:00
|
|
|
, fFactory(factory) {}
|
2015-01-15 18:56:12 +00:00
|
|
|
|
2015-02-03 02:26:03 +00:00
|
|
|
Error ViaTiles::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
|
2015-05-05 18:38:45 +00:00
|
|
|
auto size = src.size();
|
2015-01-15 18:56:12 +00:00
|
|
|
SkPictureRecorder recorder;
|
2015-05-05 18:38:45 +00:00
|
|
|
Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
|
|
|
|
SkIntToScalar(size.height()),
|
|
|
|
fFactory.get()));
|
2015-01-15 18:56:12 +00:00
|
|
|
if (!err.isEmpty()) {
|
|
|
|
return err;
|
|
|
|
}
|
2015-04-07 15:30:32 +00:00
|
|
|
SkAutoTUnref<SkPicture> pic(recorder.endRecordingAsPicture());
|
2015-01-15 18:56:12 +00:00
|
|
|
|
2015-05-05 18:38:45 +00:00
|
|
|
return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) {
|
|
|
|
const int xTiles = (size.width() + fW - 1) / fW,
|
|
|
|
yTiles = (size.height() + fH - 1) / fH;
|
|
|
|
SkMultiPictureDraw mpd(xTiles*yTiles);
|
|
|
|
SkTDArray<SkSurface*> surfaces;
|
|
|
|
surfaces.setReserve(xTiles*yTiles);
|
|
|
|
|
|
|
|
SkImageInfo info = canvas->imageInfo().makeWH(fW, fH);
|
|
|
|
for (int j = 0; j < yTiles; j++) {
|
|
|
|
for (int i = 0; i < xTiles; i++) {
|
|
|
|
// This lets our ultimate Sink determine the best kind of surface.
|
|
|
|
// E.g., if it's a GpuSink, the surfaces and images are textures.
|
|
|
|
SkSurface* s = canvas->newSurface(info);
|
|
|
|
if (!s) {
|
|
|
|
s = SkSurface::NewRaster(info); // Some canvases can't create surfaces.
|
2015-01-15 18:56:12 +00:00
|
|
|
}
|
2015-05-05 18:38:45 +00:00
|
|
|
surfaces.push(s);
|
|
|
|
SkCanvas* c = s->getCanvas();
|
|
|
|
c->translate(SkIntToScalar(-i * fW),
|
|
|
|
SkIntToScalar(-j * fH)); // Line up the canvas with this tile.
|
|
|
|
mpd.add(c, pic);
|
2015-01-15 18:56:12 +00:00
|
|
|
}
|
2015-05-05 18:38:45 +00:00
|
|
|
}
|
|
|
|
mpd.draw();
|
|
|
|
for (int j = 0; j < yTiles; j++) {
|
|
|
|
for (int i = 0; i < xTiles; i++) {
|
|
|
|
SkAutoTUnref<SkImage> image(surfaces[i+xTiles*j]->newImageSnapshot());
|
|
|
|
canvas->drawImage(image, SkIntToScalar(i*fW), SkIntToScalar(j*fH));
|
2015-01-15 18:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-05 18:38:45 +00:00
|
|
|
surfaces.unrefAll();
|
|
|
|
return "";
|
|
|
|
});
|
2015-01-15 18:56:12 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 15:30:32 +00:00
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
|
|
|
// Draw the Src into two pictures, then draw the second picture into the wrapped Sink.
|
|
|
|
// This tests that any shortcuts we may take while recording that second picture are legal.
|
|
|
|
Error ViaSecondPicture::draw(
|
|
|
|
const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
|
2015-05-05 18:38:45 +00:00
|
|
|
auto size = src.size();
|
|
|
|
return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
SkAutoTUnref<SkPicture> pic;
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
Error err = src.draw(recorder.beginRecording(SkIntToScalar(size.width()),
|
|
|
|
SkIntToScalar(size.height())));
|
|
|
|
if (!err.isEmpty()) {
|
|
|
|
return err;
|
2015-04-07 15:30:32 +00:00
|
|
|
}
|
2015-05-05 18:38:45 +00:00
|
|
|
pic.reset(recorder.endRecordingAsPicture());
|
2015-04-07 15:30:32 +00:00
|
|
|
}
|
2015-05-05 18:38:45 +00:00
|
|
|
canvas->drawPicture(pic);
|
|
|
|
return "";
|
|
|
|
});
|
2015-04-07 15:30:32 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 19:59:56 +00:00
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
2015-05-06 18:35:40 +00:00
|
|
|
// Draw the Src twice. This can help exercise caching.
|
|
|
|
Error ViaTwice::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
|
|
|
|
return draw_to_canvas(fSink, bitmap, stream, log, src.size(), [&](SkCanvas* canvas) -> Error {
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true/*save now*/);
|
|
|
|
canvas->clear(SK_ColorTRANSPARENT);
|
|
|
|
Error err = src.draw(canvas);
|
|
|
|
if (err.isEmpty()) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
2015-05-05 19:59:56 +00:00
|
|
|
// This is like SkRecords::Draw, in that it plays back SkRecords ops into a Canvas.
|
|
|
|
// Unlike SkRecords::Draw, it builds a single-op sub-picture out of each Draw-type op.
|
|
|
|
// This is an only-slightly-exaggerated simluation of Blink's Slimming Paint pictures.
|
|
|
|
struct DrawsAsSingletonPictures {
|
|
|
|
SkCanvas* fCanvas;
|
2015-07-07 16:43:28 +00:00
|
|
|
const SkDrawableList& fDrawables;
|
2015-05-05 19:59:56 +00:00
|
|
|
|
|
|
|
SK_CREATE_MEMBER_DETECTOR(paint);
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void draw(const T& op, SkCanvas* canvas) {
|
|
|
|
// We must pass SkMatrix::I() as our initial matrix.
|
|
|
|
// By default SkRecords::Draw() uses the canvas' matrix as its initial matrix,
|
|
|
|
// which would have the funky effect of applying transforms over and over.
|
2015-07-07 16:43:28 +00:00
|
|
|
SkRecords::Draw d(canvas, nullptr, fDrawables.begin(), fDrawables.count(), &SkMatrix::I());
|
|
|
|
d(op);
|
2015-05-05 19:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Most things that have paints are Draw-type ops. Create sub-pictures for each.
|
|
|
|
template <typename T>
|
|
|
|
SK_WHEN(HasMember_paint<T>, void) operator()(const T& op) {
|
|
|
|
SkPictureRecorder rec;
|
|
|
|
this->draw(op, rec.beginRecording(SkRect::MakeLargest()));
|
|
|
|
SkAutoTUnref<SkPicture> pic(rec.endRecordingAsPicture());
|
|
|
|
fCanvas->drawPicture(pic);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If you don't have a paint or are a SaveLayer, you're not a Draw-type op.
|
|
|
|
// We cannot make subpictures out of these because they affect state. Draw them directly.
|
|
|
|
template <typename T>
|
|
|
|
SK_WHEN(!HasMember_paint<T>, void) operator()(const T& op) { this->draw(op, fCanvas); }
|
|
|
|
void operator()(const SkRecords::SaveLayer& op) { this->draw(op, fCanvas); }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Record Src into a picture, then record it into a macro picture with a sub-picture for each draw.
|
|
|
|
// Then play back that macro picture into our wrapped sink.
|
|
|
|
Error ViaSingletonPictures::draw(
|
|
|
|
const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
|
|
|
|
auto size = src.size();
|
|
|
|
return draw_to_canvas(fSink, bitmap, stream, log, size, [&](SkCanvas* canvas) -> Error {
|
|
|
|
// Use low-level (Skia-private) recording APIs so we can read the SkRecord.
|
|
|
|
SkRecord skr;
|
|
|
|
SkRecorder recorder(&skr, size.width(), size.height());
|
|
|
|
Error err = src.draw(&recorder);
|
|
|
|
if (!err.isEmpty()) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record our macro-picture, with each draw op as its own sub-picture.
|
|
|
|
SkPictureRecorder macroRec;
|
|
|
|
SkCanvas* macroCanvas = macroRec.beginRecording(SkIntToScalar(size.width()),
|
|
|
|
SkIntToScalar(size.height()));
|
2015-07-07 16:43:28 +00:00
|
|
|
|
|
|
|
SkAutoTDelete<SkDrawableList> drawables(recorder.detachDrawableList());
|
|
|
|
const SkDrawableList empty;
|
|
|
|
|
|
|
|
DrawsAsSingletonPictures drawsAsSingletonPictures = {
|
|
|
|
macroCanvas,
|
|
|
|
drawables ? *drawables : empty,
|
|
|
|
};
|
2015-05-05 19:59:56 +00:00
|
|
|
for (unsigned i = 0; i < skr.count(); i++) {
|
|
|
|
skr.visit<void>(i, drawsAsSingletonPictures);
|
|
|
|
}
|
|
|
|
SkAutoTUnref<SkPicture> macroPic(macroRec.endRecordingAsPicture());
|
|
|
|
|
|
|
|
canvas->drawPicture(macroPic);
|
|
|
|
return "";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-01-15 18:56:12 +00:00
|
|
|
} // namespace DM
|