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"
|
|
|
|
#include "SkCommonFlags.h"
|
2015-03-03 16:59:20 +00:00
|
|
|
#include "SkCodec.h"
|
2015-03-25 20:13:43 +00:00
|
|
|
#include "SkData.h"
|
2015-01-15 18:56:12 +00:00
|
|
|
#include "SkDocument.h"
|
2015-02-25 22:09:45 +00:00
|
|
|
#include "SkError.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-03-25 20:48:49 +00:00
|
|
|
#include "SkScanlineDecoder.h"
|
2015-02-06 20:51:10 +00:00
|
|
|
#include "SkSVGCanvas.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-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-03-25 20:48:49 +00:00
|
|
|
CodecSrc::CodecSrc(Path path, Mode mode) : fPath(path), fMode(mode) {}
|
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));
|
|
|
|
if (!codec) {
|
|
|
|
return SkStringPrintf("Couldn't decode %s.", fPath.c_str());
|
|
|
|
}
|
|
|
|
|
2015-03-19 15:31:14 +00:00
|
|
|
SkImageInfo decodeInfo = codec->getInfo().makeColorType(canvasInfo.colorType());
|
2015-03-19 13:03:39 +00:00
|
|
|
if (decodeInfo.alphaType() == kUnpremul_SkAlphaType) {
|
|
|
|
// FIXME: Currently we cannot draw unpremultiplied sources.
|
|
|
|
decodeInfo = decodeInfo.makeAlphaType(kPremul_SkAlphaType);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkBitmap bitmap;
|
|
|
|
if (!bitmap.tryAllocPixels(decodeInfo)) {
|
|
|
|
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) {
|
|
|
|
case kNormal_Mode:
|
|
|
|
switch (codec->getPixels(decodeInfo, bitmap.getPixels(), bitmap.rowBytes())) {
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kScanline_Mode: {
|
|
|
|
SkScanlineDecoder* scanlineDecoder = codec->getScanlineDecoder(decodeInfo);
|
|
|
|
if (NULL == scanlineDecoder) {
|
|
|
|
return Error::Nonfatal("Cannot use scanline decoder for all images");
|
|
|
|
}
|
|
|
|
for (int y = 0; y < decodeInfo.height(); ++y) {
|
|
|
|
const SkImageGenerator::Result result = scanlineDecoder->getScanlines(
|
|
|
|
bitmap.getAddr(0, y), 1, 0);
|
|
|
|
switch (result) {
|
|
|
|
case SkImageGenerator::kSuccess:
|
|
|
|
case SkImageGenerator::kIncompleteInput:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return SkStringPrintf("%s failed after %d scanlines with error message %d",
|
|
|
|
fPath.c_str(), y-1, (int) result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-03-19 13:03:39 +00:00
|
|
|
}
|
2015-03-25 20:48:49 +00:00
|
|
|
canvas->drawBitmap(bitmap, 0, 0);
|
|
|
|
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) {
|
|
|
|
return codec->getInfo().dimensions();
|
|
|
|
} else {
|
|
|
|
return SkISize::Make(0, 0);
|
|
|
|
}
|
2015-03-19 13:03:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Name CodecSrc::name() const {
|
|
|
|
return SkOSPath::Basename(fPath.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
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-01-21 20:09:53 +00:00
|
|
|
if (!decoder->buildTileIndex(stream.detach(), &w, &h) || w*h == 1) {
|
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) {
|
|
|
|
return SkStringPrintf("divisor %d is too big for %s with dimensions (%d x %d)",
|
|
|
|
fDivisor, fPath.c_str(), w, h);
|
|
|
|
}
|
|
|
|
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-03-31 20:32:05 +00:00
|
|
|
// Testing TextBlob batching requires that we see individual text blobs more than once
|
|
|
|
// TODO remove this and add a flag to DM so we can run skps multiple times
|
|
|
|
//#define DOUBLE_LOOP
|
|
|
|
#ifdef DOUBLE_LOOP
|
|
|
|
{
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
#endif
|
|
|
|
canvas->drawPicture(pic);
|
|
|
|
#ifdef DOUBLE_LOOP
|
|
|
|
}
|
|
|
|
canvas->clear(0);
|
2015-01-15 18:56:12 +00:00
|
|
|
canvas->drawPicture(pic);
|
2015-03-31 20:32:05 +00:00
|
|
|
#endif
|
2015-01-15 18:56:12 +00:00
|
|
|
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-01-21 23:50:13 +00:00
|
|
|
GrContextFactory factory;
|
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();
|
|
|
|
|
|
|
|
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));
|
|
|
|
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-15 18:56:12 +00:00
|
|
|
}
|
|
|
|
doc->close();
|
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-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-01-15 18:56:12 +00:00
|
|
|
ViaMatrix::ViaMatrix(SkMatrix matrix, Sink* sink) : fMatrix(matrix), fSink(sink) {}
|
|
|
|
|
2015-02-03 02:26:03 +00:00
|
|
|
Error ViaMatrix::draw(const Src& src, SkBitmap* bitmap, SkWStream* stream, SkString* log) const {
|
2015-01-15 18:56:12 +00:00
|
|
|
// We turn our arguments into a Src, then draw that Src into our Sink to fill bitmap or stream.
|
|
|
|
struct ProxySrc : public Src {
|
2015-02-17 19:13:33 +00:00
|
|
|
const Src& fSrc;
|
|
|
|
SkMatrix fMatrix;
|
|
|
|
SkISize fSize;
|
|
|
|
|
|
|
|
ProxySrc(const Src& src, SkMatrix matrix) : fSrc(src), fMatrix(matrix) {
|
|
|
|
fSize = auto_compute_translate(&fMatrix, src.size().width(), src.size().height());
|
|
|
|
}
|
2015-01-15 18:56:12 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
Error draw(SkCanvas* canvas) const override {
|
2015-01-15 18:56:12 +00:00
|
|
|
canvas->concat(fMatrix);
|
|
|
|
return fSrc.draw(canvas);
|
|
|
|
}
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize size() const override { return fSize; }
|
|
|
|
Name name() const override { sk_throw(); return ""; } // No one should be calling this.
|
2015-01-15 18:56:12 +00:00
|
|
|
} proxy(src, fMatrix);
|
2015-02-03 02:26:03 +00:00
|
|
|
return fSink->draw(proxy, bitmap, stream, log);
|
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.
|
|
|
|
ViaUpright::ViaUpright(SkMatrix matrix, Sink* sink) : fMatrix(matrix), fSink(sink) {}
|
|
|
|
|
|
|
|
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-01-21 21:18:51 +00:00
|
|
|
ViaPipe::ViaPipe(Sink* sink) : fSink(sink) {}
|
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-01-21 21:18:51 +00:00
|
|
|
// We turn ourselves into another Src that draws our argument into bitmap/stream via pipe.
|
2015-01-15 18:56:12 +00:00
|
|
|
struct ProxySrc : public Src {
|
|
|
|
const Src& fSrc;
|
2015-01-21 21:18:51 +00:00
|
|
|
ProxySrc(const Src& src) : fSrc(src) {}
|
2015-01-15 18:56:12 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
Error draw(SkCanvas* canvas) const override {
|
2015-01-15 18:56:12 +00:00
|
|
|
SkISize size = this->size();
|
|
|
|
PipeController controller(canvas, &SkImageDecoder::DecodeMemory);
|
|
|
|
SkGPipeWriter pipe;
|
2015-01-21 21:18:51 +00:00
|
|
|
const uint32_t kFlags = 0; // We mirror SkDeferredCanvas, which doesn't use any flags.
|
|
|
|
return fSrc.draw(pipe.startRecording(&controller, kFlags, size.width(), size.height()));
|
2015-01-15 18:56:12 +00:00
|
|
|
}
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize size() const override { return fSrc.size(); }
|
|
|
|
Name name() const override { sk_throw(); return ""; } // No one should be calling this.
|
2015-01-21 21:18:51 +00:00
|
|
|
} proxy(src);
|
2015-02-03 02:26:03 +00:00
|
|
|
return fSink->draw(proxy, bitmap, stream, log);
|
2015-01-15 18:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
|
|
|
ViaSerialization::ViaSerialization(Sink* sink) : fSink(sink) {}
|
|
|
|
|
2015-02-03 02:26:03 +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.
|
|
|
|
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());
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// Turn that deserialized picture into a Src, draw it into our Sink to fill bitmap or stream.
|
|
|
|
struct ProxySrc : public Src {
|
|
|
|
const SkPicture* fPic;
|
|
|
|
const SkISize fSize;
|
|
|
|
ProxySrc(const SkPicture* pic, SkISize size) : fPic(pic), fSize(size) {}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
Error draw(SkCanvas* canvas) const override {
|
2015-01-15 18:56:12 +00:00
|
|
|
canvas->drawPicture(fPic);
|
|
|
|
return "";
|
|
|
|
}
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize size() const override { return fSize; }
|
|
|
|
Name name() const override { sk_throw(); return ""; } // No one should be calling this.
|
2015-01-15 18:56:12 +00:00
|
|
|
} proxy(deserialized, src.size());
|
2015-02-03 02:26:03 +00:00
|
|
|
return fSink->draw(proxy, bitmap, stream, log);
|
2015-01-15 18:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
|
|
|
ViaTiles::ViaTiles(int w, int h, SkBBHFactory* factory, Sink* sink)
|
|
|
|
: fW(w)
|
|
|
|
, fH(h)
|
|
|
|
, fFactory(factory)
|
|
|
|
, fSink(sink) {}
|
|
|
|
|
2015-02-03 02:26:03 +00:00
|
|
|
Error ViaTiles::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.
|
|
|
|
SkSize size;
|
|
|
|
size = src.size();
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
Error err = src.draw(recorder.beginRecording(size.width(), size.height(), fFactory.get()));
|
|
|
|
if (!err.isEmpty()) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
SkAutoTUnref<SkPicture> pic(recorder.endRecording());
|
|
|
|
|
|
|
|
// Turn that picture into a Src that draws into our Sink via tiles + MPD.
|
|
|
|
struct ProxySrc : public Src {
|
|
|
|
const int fW, fH;
|
|
|
|
const SkPicture* fPic;
|
|
|
|
const SkISize fSize;
|
|
|
|
ProxySrc(int w, int h, const SkPicture* pic, SkISize size)
|
|
|
|
: fW(w), fH(h), fPic(pic), fSize(size) {}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
Error draw(SkCanvas* canvas) const override {
|
2015-01-15 18:56:12 +00:00
|
|
|
const int xTiles = (fSize.width() + fW - 1) / fW,
|
|
|
|
yTiles = (fSize.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.
|
|
|
|
}
|
|
|
|
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, fPic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
surfaces.unrefAll();
|
|
|
|
return "";
|
|
|
|
}
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize size() const override { return fSize; }
|
|
|
|
Name name() const override { sk_throw(); return ""; } // No one should be calling this.
|
2015-01-15 18:56:12 +00:00
|
|
|
} proxy(fW, fH, pic, src.size());
|
2015-02-03 02:26:03 +00:00
|
|
|
return fSink->draw(proxy, bitmap, stream, log);
|
2015-01-15 18:56:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace DM
|