blob: 8022092c9a27100e823f791e2f224f77726377ee [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.org3f045172014-05-15 15:10:48 +000023 void CopyTilesRenderer::init(SkPicture* pict, const SkString* writePath,
24 const SkString* mismatchPath, const SkString* inputFilename,
25 bool useChecksumBasedFilenames) {
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000026 // Do not call INHERITED::init(), which would create a (potentially large) canvas which is
27 // not used by bench_pictures.
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000028 SkASSERT(pict != NULL);
29 // Only work with absolute widths (as opposed to percentages).
30 SkASSERT(this->getTileWidth() != 0 && this->getTileHeight() != 0);
robertphillips@google.com84b18c72014-04-13 19:09:42 +000031 fPicture.reset(pict)->ref();
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000032 this->CopyString(&fWritePath, writePath);
33 this->CopyString(&fMismatchPath, mismatchPath);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000034 this->CopyString(&fInputFilename, inputFilename);
35 fUseChecksumBasedFilenames = useChecksumBasedFilenames;
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000036 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));
42 }
43
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000044 bool CopyTilesRenderer::render(SkBitmap** out) {
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000045 int i = 0;
46 bool success = true;
47 SkBitmap dst;
scroggo@google.comc0d5e542012-12-13 21:40:48 +000048 for (int x = 0; x < this->getViewWidth(); x += fLargeTileWidth) {
49 for (int y = 0; y < this->getViewHeight(); y += fLargeTileHeight) {
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000050 SkAutoCanvasRestore autoRestore(fCanvas, true);
scroggo@google.com82ec0b02012-12-17 19:25:54 +000051 // Translate so that we draw the correct portion of the picture.
52 // Perform a postTranslate so that the scaleFactor does not interfere with the
53 // positioning.
54 SkMatrix mat(fCanvas->getTotalMatrix());
55 mat.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
56 fCanvas->setMatrix(mat);
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000057 // Draw the picture
58 fCanvas->drawPicture(*fPicture);
59 // Now extract the picture into tiles
60 const SkBitmap& baseBitmap = fCanvas->getDevice()->accessBitmap(false);
61 SkIRect subset;
62 for (int tileY = 0; tileY < fLargeTileHeight; tileY += this->getTileHeight()) {
63 for (int tileX = 0; tileX < fLargeTileWidth; tileX += this->getTileWidth()) {
64 subset.set(tileX, tileY, tileX + this->getTileWidth(),
65 tileY + this->getTileHeight());
66 SkDEBUGCODE(bool extracted =)
67 baseBitmap.extractSubset(&dst, subset);
68 SkASSERT(extracted);
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000069 if (!fWritePath.isEmpty()) {
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000070 // Similar to write() in PictureRenderer.cpp, but just encodes
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000071 // a bitmap directly.
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000072 // TODO: Share more common code with write() to do this, to properly
73 // write out the JSON summary, etc.
74 SkString pathWithNumber;
commit-bot@chromium.org3f045172014-05-15 15:10:48 +000075 make_filepath(&pathWithNumber, fWritePath, fInputFilename);
commit-bot@chromium.orgf5e315c2014-03-19 17:26:07 +000076 pathWithNumber.remove(pathWithNumber.size() - 4, 4);
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000077 pathWithNumber.appendf("%i.png", i++);
78 SkBitmap copy;
79#if SK_SUPPORT_GPU
80 if (isUsingGpuDevice()) {
81 dst.pixelRef()->readPixels(&copy, &subset);
82 } else {
83#endif
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000084 dst.copyTo(&copy);
scroggo@google.com4a26d9d2012-11-07 18:01:46 +000085#if SK_SUPPORT_GPU
86 }
87#endif
88 success &= SkImageEncoder::EncodeFile(pathWithNumber.c_str(), copy,
89 SkImageEncoder::kPNG_Type, 100);
90 }
91 }
92 }
93 }
94 }
95 return success;
96 }
97
98 SkString CopyTilesRenderer::getConfigNameInternal() {
99 return SkString("copy_tiles");
100 }
101}