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"
|
|
|
|
#include "SkPicture.h"
|
|
|
|
#include "SkPixelRef.h"
|
|
|
|
#include "SkRect.h"
|
|
|
|
#include "SkString.h"
|
|
|
|
|
|
|
|
namespace sk_tools {
|
|
|
|
CopyTilesRenderer::CopyTilesRenderer(int x, int y)
|
|
|
|
: fXTilesPerLargeTile(x)
|
|
|
|
, fYTilesPerLargeTile(y) {
|
|
|
|
}
|
|
|
|
void CopyTilesRenderer::init(SkPicture* pict) {
|
|
|
|
SkASSERT(pict != NULL);
|
|
|
|
// Only work with absolute widths (as opposed to percentages).
|
|
|
|
SkASSERT(this->getTileWidth() != 0 && this->getTileHeight() != 0);
|
|
|
|
fPicture = pict;
|
|
|
|
fPicture->ref();
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2012-12-18 22:24:03 +00:00
|
|
|
bool CopyTilesRenderer::render(const SkString* path, 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
|
|
|
|
fCanvas->drawPicture(*fPicture);
|
|
|
|
// Now extract the picture into tiles
|
|
|
|
const SkBitmap& baseBitmap = fCanvas->getDevice()->accessBitmap(false);
|
|
|
|
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);
|
|
|
|
if (path != NULL) {
|
|
|
|
// Similar to writeAppendNumber in PictureRenderer.cpp, but just encodes
|
|
|
|
// a bitmap directly.
|
|
|
|
SkString pathWithNumber(*path);
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|