2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "picture_utils.h"
9 #include "CopyTilesRenderer.h"
12 #include "SkImageEncoder.h"
13 #include "SkPicture.h"
14 #include "SkPixelRef.h"
19 CopyTilesRenderer::CopyTilesRenderer(int x, int y)
20 : fXTilesPerLargeTile(x)
21 , fYTilesPerLargeTile(y) {
23 void CopyTilesRenderer::init(const SkPicture* pict, const SkString* writePath,
24 const SkString* mismatchPath, const SkString* inputFilename,
25 bool useChecksumBasedFilenames) {
26 // Do not call INHERITED::init(), which would create a (potentially large) canvas which is
27 // not used by bench_pictures.
28 SkASSERT(pict != NULL);
29 // Only work with absolute widths (as opposed to percentages).
30 SkASSERT(this->getTileWidth() != 0 && this->getTileHeight() != 0);
31 fPicture.reset(pict)->ref();
32 this->CopyString(&fWritePath, writePath);
33 this->CopyString(&fMismatchPath, mismatchPath);
34 this->CopyString(&fInputFilename, inputFilename);
35 fUseChecksumBasedFilenames = useChecksumBasedFilenames;
36 this->buildBBoxHierarchy();
37 // In order to avoid allocating a large canvas (particularly important for GPU), create one
38 // canvas that is a multiple of the tile size, and draw portions of the picture.
39 fLargeTileWidth = fXTilesPerLargeTile * this->getTileWidth();
40 fLargeTileHeight = fYTilesPerLargeTile * this->getTileHeight();
41 fCanvas.reset(this->INHERITED::setupCanvas(fLargeTileWidth, fLargeTileHeight));
44 bool CopyTilesRenderer::render(SkBitmap** out) {
48 for (int x = 0; x < this->getViewWidth(); x += fLargeTileWidth) {
49 for (int y = 0; y < this->getViewHeight(); y += fLargeTileHeight) {
50 SkAutoCanvasRestore autoRestore(fCanvas, true);
51 // Translate so that we draw the correct portion of the picture.
52 // Perform a postTranslate so that the scaleFactor does not interfere with the
54 SkMatrix mat(fCanvas->getTotalMatrix());
55 mat.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
56 fCanvas->setMatrix(mat);
58 fCanvas->drawPicture(fPicture);
59 // Now extract the picture into tiles
61 fCanvas->readPixels(SkIRect::MakeSize(fCanvas->getBaseLayerSize()), &baseBitmap);
63 for (int tileY = 0; tileY < fLargeTileHeight; tileY += this->getTileHeight()) {
64 for (int tileX = 0; tileX < fLargeTileWidth; tileX += this->getTileWidth()) {
65 subset.set(tileX, tileY, tileX + this->getTileWidth(),
66 tileY + this->getTileHeight());
67 SkDEBUGCODE(bool extracted =)
68 baseBitmap.extractSubset(&dst, subset);
70 if (!fWritePath.isEmpty()) {
71 // Similar to write() in PictureRenderer.cpp, but just encodes
73 // TODO: Share more common code with write() to do this, to properly
74 // write out the JSON summary, etc.
75 SkString pathWithNumber = SkOSPath::Join(fWritePath.c_str(),
76 fInputFilename.c_str());
77 pathWithNumber.remove(pathWithNumber.size() - 4, 4);
78 pathWithNumber.appendf("%i.png", i++);
81 if (isUsingGpuDevice()) {
82 dst.pixelRef()->readPixels(©, &subset);
89 success &= SkImageEncoder::EncodeFile(pathWithNumber.c_str(), copy,
90 SkImageEncoder::kPNG_Type, 100);
99 SkString CopyTilesRenderer::getConfigNameInternal() {
100 return SkString("copy_tiles");