blob: 9e919e0a4ff3c57ea5d27fd317cb172f3372f6af [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);
robertphillips@google.com84b18c72014-04-13 19:09:42 +000030 fPicture.reset(pict)->ref();
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 this->buildBBoxHierarchy();
35 // In order to avoid allocating a large canvas (particularly important for GPU), create one
36 // canvas that is a multiple of the tile size, and draw portions of the picture.
37 fLargeTileWidth = fXTilesPerLargeTile * this->getTileWidth();
38 fLargeTileHeight = fYTilesPerLargeTile * this->getTileHeight();
39 fCanvas.reset(this->INHERITED::setupCanvas(fLargeTileWidth, fLargeTileHeight));
40 }
41
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000042 bool CopyTilesRenderer::render(SkBitmap** out) {
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000043 int i = 0;
44 bool success = true;
45 SkBitmap dst;
scroggo@google.comc0d5e542012-12-13 21:40:48 +000046 for (int x = 0; x < this->getViewWidth(); x += fLargeTileWidth) {
47 for (int y = 0; y < this->getViewHeight(); y += fLargeTileHeight) {
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000048 SkAutoCanvasRestore autoRestore(fCanvas, true);
scroggo@google.com82ec0b02012-12-17 19:25:54 +000049 // Translate so that we draw the correct portion of the picture.
50 // Perform a postTranslate so that the scaleFactor does not interfere with the
51 // positioning.
52 SkMatrix mat(fCanvas->getTotalMatrix());
53 mat.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
54 fCanvas->setMatrix(mat);
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000055 // Draw the picture
56 fCanvas->drawPicture(*fPicture);
57 // Now extract the picture into tiles
58 const SkBitmap& baseBitmap = fCanvas->getDevice()->accessBitmap(false);
59 SkIRect subset;
60 for (int tileY = 0; tileY < fLargeTileHeight; tileY += this->getTileHeight()) {
61 for (int tileX = 0; tileX < fLargeTileWidth; tileX += this->getTileWidth()) {
62 subset.set(tileX, tileY, tileX + this->getTileWidth(),
63 tileY + this->getTileHeight());
64 SkDEBUGCODE(bool extracted =)
65 baseBitmap.extractSubset(&dst, subset);
66 SkASSERT(extracted);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000067 if (!fOutputDir.isEmpty()) {
68 // Similar to write() in PictureRenderer.cpp, but just encodes
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000069 // a bitmap directly.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000070 // TODO: Share more common code with write() to do this, to properly
71 // write out the JSON summary, etc.
72 SkString pathWithNumber;
73 make_filepath(&pathWithNumber, fOutputDir, fInputFilename);
74 pathWithNumber.remove(pathWithNumber.size() - 4, 4);
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000075 pathWithNumber.appendf("%i.png", i++);
76 SkBitmap copy;
77#if SK_SUPPORT_GPU
78 if (isUsingGpuDevice()) {
79 dst.pixelRef()->readPixels(&copy, &subset);
80 } else {
81#endif
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000082 dst.copyTo(&copy);
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000083#if SK_SUPPORT_GPU
84 }
85#endif
86 success &= SkImageEncoder::EncodeFile(pathWithNumber.c_str(), copy,
87 SkImageEncoder::kPNG_Type, 100);
88 }
89 }
90 }
91 }
92 }
93 return success;
94 }
95
96 SkString CopyTilesRenderer::getConfigNameInternal() {
97 return SkString("copy_tiles");
98 }
99}