2012-11-07 18:01:46 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "picture_utils.h"
|
|
|
|
#include "CopyTilesRenderer.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkDevice.h"
|
|
|
|
#include "SkImageEncoder.h"
|
2014-10-09 11:59:19 +00:00
|
|
|
#include "SkMultiPictureDraw.h"
|
2012-11-07 18:01:46 +00:00
|
|
|
#include "SkPicture.h"
|
|
|
|
#include "SkPixelRef.h"
|
|
|
|
#include "SkRect.h"
|
|
|
|
#include "SkString.h"
|
|
|
|
|
|
|
|
namespace sk_tools {
|
2014-08-18 14:52:17 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2015-05-22 21:01:46 +00:00
|
|
|
CopyTilesRenderer::CopyTilesRenderer(const GrContextOptions& opts, int x, int y)
|
2014-08-18 14:52:17 +00:00
|
|
|
: INHERITED(opts)
|
|
|
|
, fXTilesPerLargeTile(x)
|
|
|
|
, fYTilesPerLargeTile(y) { }
|
|
|
|
#else
|
2012-11-07 18:01:46 +00:00
|
|
|
CopyTilesRenderer::CopyTilesRenderer(int x, int y)
|
|
|
|
: fXTilesPerLargeTile(x)
|
2014-08-18 14:52:17 +00:00
|
|
|
, fYTilesPerLargeTile(y) { }
|
|
|
|
#endif
|
2014-07-07 20:46:35 +00:00
|
|
|
void CopyTilesRenderer::init(const SkPicture* pict, const SkString* writePath,
|
2014-05-15 15:10:48 +00:00
|
|
|
const SkString* mismatchPath, const SkString* inputFilename,
|
2014-10-09 11:59:19 +00:00
|
|
|
bool useChecksumBasedFilenames, bool useMultiPictureDraw) {
|
2014-03-19 17:26:07 +00:00
|
|
|
// Do not call INHERITED::init(), which would create a (potentially large) canvas which is
|
|
|
|
// not used by bench_pictures.
|
2012-11-07 18:01:46 +00:00
|
|
|
SkASSERT(pict != NULL);
|
|
|
|
// Only work with absolute widths (as opposed to percentages).
|
|
|
|
SkASSERT(this->getTileWidth() != 0 && this->getTileHeight() != 0);
|
2014-04-13 19:09:42 +00:00
|
|
|
fPicture.reset(pict)->ref();
|
2014-05-15 15:10:48 +00:00
|
|
|
this->CopyString(&fWritePath, writePath);
|
|
|
|
this->CopyString(&fMismatchPath, mismatchPath);
|
2014-03-19 17:26:07 +00:00
|
|
|
this->CopyString(&fInputFilename, inputFilename);
|
|
|
|
fUseChecksumBasedFilenames = useChecksumBasedFilenames;
|
2014-10-09 11:59:19 +00:00
|
|
|
fUseMultiPictureDraw = useMultiPictureDraw;
|
2012-11-07 18:01:46 +00:00
|
|
|
this->buildBBoxHierarchy();
|
|
|
|
// In order to avoid allocating a large canvas (particularly important for GPU), create one
|
|
|
|
// canvas that is a multiple of the tile size, and draw portions of the picture.
|
|
|
|
fLargeTileWidth = fXTilesPerLargeTile * this->getTileWidth();
|
|
|
|
fLargeTileHeight = fYTilesPerLargeTile * this->getTileHeight();
|
|
|
|
fCanvas.reset(this->INHERITED::setupCanvas(fLargeTileWidth, fLargeTileHeight));
|
|
|
|
}
|
|
|
|
|
2014-03-19 17:26:07 +00:00
|
|
|
bool CopyTilesRenderer::render(SkBitmap** out) {
|
2012-11-07 18:01:46 +00:00
|
|
|
int i = 0;
|
|
|
|
bool success = true;
|
|
|
|
SkBitmap dst;
|
2012-12-13 21:40:48 +00:00
|
|
|
for (int x = 0; x < this->getViewWidth(); x += fLargeTileWidth) {
|
|
|
|
for (int y = 0; y < this->getViewHeight(); y += fLargeTileHeight) {
|
2012-11-07 18:01:46 +00:00
|
|
|
SkAutoCanvasRestore autoRestore(fCanvas, true);
|
2012-12-17 19:25:54 +00:00
|
|
|
// Translate so that we draw the correct portion of the picture.
|
|
|
|
// Perform a postTranslate so that the scaleFactor does not interfere with the
|
|
|
|
// positioning.
|
|
|
|
SkMatrix mat(fCanvas->getTotalMatrix());
|
|
|
|
mat.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
|
|
|
|
fCanvas->setMatrix(mat);
|
2012-11-07 18:01:46 +00:00
|
|
|
// Draw the picture
|
2014-10-09 11:59:19 +00:00
|
|
|
if (fUseMultiPictureDraw) {
|
|
|
|
SkMultiPictureDraw mpd;
|
|
|
|
|
|
|
|
mpd.add(fCanvas, fPicture);
|
|
|
|
|
|
|
|
mpd.draw();
|
|
|
|
} else {
|
|
|
|
fCanvas->drawPicture(fPicture);
|
|
|
|
}
|
2012-11-07 18:01:46 +00:00
|
|
|
// Now extract the picture into tiles
|
2014-06-30 16:05:34 +00:00
|
|
|
SkBitmap baseBitmap;
|
|
|
|
fCanvas->readPixels(SkIRect::MakeSize(fCanvas->getBaseLayerSize()), &baseBitmap);
|
2012-11-07 18:01:46 +00:00
|
|
|
SkIRect subset;
|
|
|
|
for (int tileY = 0; tileY < fLargeTileHeight; tileY += this->getTileHeight()) {
|
|
|
|
for (int tileX = 0; tileX < fLargeTileWidth; tileX += this->getTileWidth()) {
|
|
|
|
subset.set(tileX, tileY, tileX + this->getTileWidth(),
|
|
|
|
tileY + this->getTileHeight());
|
|
|
|
SkDEBUGCODE(bool extracted =)
|
|
|
|
baseBitmap.extractSubset(&dst, subset);
|
|
|
|
SkASSERT(extracted);
|
2014-05-15 15:10:48 +00:00
|
|
|
if (!fWritePath.isEmpty()) {
|
2014-03-19 17:26:07 +00:00
|
|
|
// Similar to write() in PictureRenderer.cpp, but just encodes
|
2012-11-07 18:01:46 +00:00
|
|
|
// a bitmap directly.
|
2014-03-19 17:26:07 +00:00
|
|
|
// TODO: Share more common code with write() to do this, to properly
|
|
|
|
// write out the JSON summary, etc.
|
2014-07-29 02:26:58 +00:00
|
|
|
SkString pathWithNumber = SkOSPath::Join(fWritePath.c_str(),
|
|
|
|
fInputFilename.c_str());
|
2014-03-19 17:26:07 +00:00
|
|
|
pathWithNumber.remove(pathWithNumber.size() - 4, 4);
|
2012-11-07 18:01:46 +00:00
|
|
|
pathWithNumber.appendf("%i.png", i++);
|
|
|
|
SkBitmap copy;
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
if (isUsingGpuDevice()) {
|
|
|
|
dst.pixelRef()->readPixels(©, &subset);
|
|
|
|
} else {
|
|
|
|
#endif
|
2014-02-23 03:59:35 +00:00
|
|
|
dst.copyTo(©);
|
2012-11-07 18:01:46 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
success &= SkImageEncoder::EncodeFile(pathWithNumber.c_str(), copy,
|
|
|
|
SkImageEncoder::kPNG_Type, 100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkString CopyTilesRenderer::getConfigNameInternal() {
|
|
|
|
return SkString("copy_tiles");
|
|
|
|
}
|
|
|
|
}
|