blob: 341d93edaec4dfff4fc17d016c4ee3a24c30581d [file] [log] [blame]
scroggo@google.com4a26d9d2012-11-07 18:01:46 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "picture_utils.h"
9#include "CopyTilesRenderer.h"
10#include "SkCanvas.h"
11#include "SkDevice.h"
12#include "SkImageEncoder.h"
13#include "SkPicture.h"
14#include "SkPixelRef.h"
15#include "SkRect.h"
16#include "SkString.h"
17
18namespace sk_tools {
19 CopyTilesRenderer::CopyTilesRenderer(int x, int y)
20 : fXTilesPerLargeTile(x)
21 , fYTilesPerLargeTile(y) {
22 }
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000023 void CopyTilesRenderer::init(SkPicture* pict, const SkString* outputDir,
24 const SkString* inputFilename, bool useChecksumBasedFilenames) {
25 // Do not call INHERITED::init(), which would create a (potentially large) canvas which is
26 // not used by bench_pictures.
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000027 SkASSERT(pict != NULL);
28 // Only work with absolute widths (as opposed to percentages).
29 SkASSERT(this->getTileWidth() != 0 && this->getTileHeight() != 0);
30 fPicture = pict;
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000031 this->CopyString(&fOutputDir, outputDir);
32 this->CopyString(&fInputFilename, inputFilename);
33 fUseChecksumBasedFilenames = useChecksumBasedFilenames;
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000034 fPicture->ref();
35 this->buildBBoxHierarchy();
36 // In order to avoid allocating a large canvas (particularly important for GPU), create one
37 // canvas that is a multiple of the tile size, and draw portions of the picture.
38 fLargeTileWidth = fXTilesPerLargeTile * this->getTileWidth();
39 fLargeTileHeight = fYTilesPerLargeTile * this->getTileHeight();
40 fCanvas.reset(this->INHERITED::setupCanvas(fLargeTileWidth, fLargeTileHeight));
41 }
42
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000043 bool CopyTilesRenderer::render(SkBitmap** out) {
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000044 int i = 0;
45 bool success = true;
46 SkBitmap dst;
scroggo@google.comc0d5e542012-12-13 21:40:48 +000047 for (int x = 0; x < this->getViewWidth(); x += fLargeTileWidth) {
48 for (int y = 0; y < this->getViewHeight(); y += fLargeTileHeight) {
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000049 SkAutoCanvasRestore autoRestore(fCanvas, true);
scroggo@google.com82ec0b02012-12-17 19:25:54 +000050 // Translate so that we draw the correct portion of the picture.
51 // Perform a postTranslate so that the scaleFactor does not interfere with the
52 // positioning.
53 SkMatrix mat(fCanvas->getTotalMatrix());
54 mat.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
55 fCanvas->setMatrix(mat);
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000056 // Draw the picture
57 fCanvas->drawPicture(*fPicture);
58 // Now extract the picture into tiles
59 const SkBitmap& baseBitmap = fCanvas->getDevice()->accessBitmap(false);
60 SkIRect subset;
61 for (int tileY = 0; tileY < fLargeTileHeight; tileY += this->getTileHeight()) {
62 for (int tileX = 0; tileX < fLargeTileWidth; tileX += this->getTileWidth()) {
63 subset.set(tileX, tileY, tileX + this->getTileWidth(),
64 tileY + this->getTileHeight());
65 SkDEBUGCODE(bool extracted =)
66 baseBitmap.extractSubset(&dst, subset);
67 SkASSERT(extracted);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000068 if (!fOutputDir.isEmpty()) {
69 // Similar to write() in PictureRenderer.cpp, but just encodes
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000070 // a bitmap directly.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000071 // TODO: Share more common code with write() to do this, to properly
72 // write out the JSON summary, etc.
73 SkString pathWithNumber;
74 make_filepath(&pathWithNumber, fOutputDir, fInputFilename);
75 pathWithNumber.remove(pathWithNumber.size() - 4, 4);
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000076 pathWithNumber.appendf("%i.png", i++);
77 SkBitmap copy;
78#if SK_SUPPORT_GPU
79 if (isUsingGpuDevice()) {
80 dst.pixelRef()->readPixels(&copy, &subset);
81 } else {
82#endif
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000083 dst.copyTo(&copy);
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000084#if SK_SUPPORT_GPU
85 }
86#endif
87 success &= SkImageEncoder::EncodeFile(pathWithNumber.c_str(), copy,
88 SkImageEncoder::kPNG_Type, 100);
89 }
90 }
91 }
92 }
93 }
94 return success;
95 }
96
97 SkString CopyTilesRenderer::getConfigNameInternal() {
98 return SkString("copy_tiles");
99 }
100}