CopyTilesRenderer.cpp revision f5e315ccf1ae2941f7cf53fa53e5c8c4bb665fe1
1/*
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    }
23    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.
27        SkASSERT(pict != NULL);
28        // Only work with absolute widths (as opposed to percentages).
29        SkASSERT(this->getTileWidth() != 0 && this->getTileHeight() != 0);
30        fPicture = pict;
31        this->CopyString(&fOutputDir, outputDir);
32        this->CopyString(&fInputFilename, inputFilename);
33        fUseChecksumBasedFilenames = useChecksumBasedFilenames;
34        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
43    bool CopyTilesRenderer::render(SkBitmap** out) {
44        int i = 0;
45        bool success = true;
46        SkBitmap dst;
47        for (int x = 0; x < this->getViewWidth(); x += fLargeTileWidth) {
48            for (int y = 0; y < this->getViewHeight(); y += fLargeTileHeight) {
49                SkAutoCanvasRestore autoRestore(fCanvas, true);
50                // 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);
56                // 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);
68                        if (!fOutputDir.isEmpty()) {
69                            // Similar to write() in PictureRenderer.cpp, but just encodes
70                            // a bitmap directly.
71                            // 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);
76                            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
83                                dst.copyTo(&copy);
84#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}
101