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"
20 CopyTilesRenderer::CopyTilesRenderer(const GrContext::Options& opts, int x, int y)
22 , fXTilesPerLargeTile(x)
23 , fYTilesPerLargeTile(y) { }
25 CopyTilesRenderer::CopyTilesRenderer(int x, int y)
26 : fXTilesPerLargeTile(x)
27 , fYTilesPerLargeTile(y) { }
29 void CopyTilesRenderer::init(const SkPicture* pict, const SkString* writePath,
30 const SkString* mismatchPath, const SkString* inputFilename,
31 bool useChecksumBasedFilenames) {
32 // Do not call INHERITED::init(), which would create a (potentially large) canvas which is
33 // not used by bench_pictures.
34 SkASSERT(pict != NULL);
35 // Only work with absolute widths (as opposed to percentages).
36 SkASSERT(this->getTileWidth() != 0 && this->getTileHeight() != 0);
37 fPicture.reset(pict)->ref();
38 this->CopyString(&fWritePath, writePath);
39 this->CopyString(&fMismatchPath, mismatchPath);
40 this->CopyString(&fInputFilename, inputFilename);
41 fUseChecksumBasedFilenames = useChecksumBasedFilenames;
42 this->buildBBoxHierarchy();
43 // In order to avoid allocating a large canvas (particularly important for GPU), create one
44 // canvas that is a multiple of the tile size, and draw portions of the picture.
45 fLargeTileWidth = fXTilesPerLargeTile * this->getTileWidth();
46 fLargeTileHeight = fYTilesPerLargeTile * this->getTileHeight();
47 fCanvas.reset(this->INHERITED::setupCanvas(fLargeTileWidth, fLargeTileHeight));
50 bool CopyTilesRenderer::render(SkBitmap** out) {
54 for (int x = 0; x < this->getViewWidth(); x += fLargeTileWidth) {
55 for (int y = 0; y < this->getViewHeight(); y += fLargeTileHeight) {
56 SkAutoCanvasRestore autoRestore(fCanvas, true);
57 // Translate so that we draw the correct portion of the picture.
58 // Perform a postTranslate so that the scaleFactor does not interfere with the
60 SkMatrix mat(fCanvas->getTotalMatrix());
61 mat.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
62 fCanvas->setMatrix(mat);
64 fCanvas->drawPicture(fPicture);
65 // Now extract the picture into tiles
67 fCanvas->readPixels(SkIRect::MakeSize(fCanvas->getBaseLayerSize()), &baseBitmap);
69 for (int tileY = 0; tileY < fLargeTileHeight; tileY += this->getTileHeight()) {
70 for (int tileX = 0; tileX < fLargeTileWidth; tileX += this->getTileWidth()) {
71 subset.set(tileX, tileY, tileX + this->getTileWidth(),
72 tileY + this->getTileHeight());
73 SkDEBUGCODE(bool extracted =)
74 baseBitmap.extractSubset(&dst, subset);
76 if (!fWritePath.isEmpty()) {
77 // Similar to write() in PictureRenderer.cpp, but just encodes
79 // TODO: Share more common code with write() to do this, to properly
80 // write out the JSON summary, etc.
81 SkString pathWithNumber = SkOSPath::Join(fWritePath.c_str(),
82 fInputFilename.c_str());
83 pathWithNumber.remove(pathWithNumber.size() - 4, 4);
84 pathWithNumber.appendf("%i.png", i++);
87 if (isUsingGpuDevice()) {
88 dst.pixelRef()->readPixels(©, &subset);
95 success &= SkImageEncoder::EncodeFile(pathWithNumber.c_str(), copy,
96 SkImageEncoder::kPNG_Type, 100);
105 SkString CopyTilesRenderer::getConfigNameInternal() {
106 return SkString("copy_tiles");