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#if SK_SUPPORT_GPU
20    CopyTilesRenderer::CopyTilesRenderer(const GrContext::Options& opts, int x, int y)
21    : INHERITED(opts)
22    , fXTilesPerLargeTile(x)
23    , fYTilesPerLargeTile(y) { }
24#else
25    CopyTilesRenderer::CopyTilesRenderer(int x, int y)
26    : fXTilesPerLargeTile(x)
27    , fYTilesPerLargeTile(y) { }
28#endif
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));
48    }
49
50    bool CopyTilesRenderer::render(SkBitmap** out) {
51        int i = 0;
52        bool success = true;
53        SkBitmap dst;
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
59                // positioning.
60                SkMatrix mat(fCanvas->getTotalMatrix());
61                mat.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
62                fCanvas->setMatrix(mat);
63                // Draw the picture
64                fCanvas->drawPicture(fPicture);
65                // Now extract the picture into tiles
66                SkBitmap baseBitmap;
67                fCanvas->readPixels(SkIRect::MakeSize(fCanvas->getBaseLayerSize()), &baseBitmap);
68                SkIRect subset;
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);
75                        SkASSERT(extracted);
76                        if (!fWritePath.isEmpty()) {
77                            // Similar to write() in PictureRenderer.cpp, but just encodes
78                            // a bitmap directly.
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++);
85                            SkBitmap copy;
86#if SK_SUPPORT_GPU
87                            if (isUsingGpuDevice()) {
88                                dst.pixelRef()->readPixels(&copy, &subset);
89                            } else {
90#endif
91                                dst.copyTo(&copy);
92#if SK_SUPPORT_GPU
93                            }
94#endif
95                            success &= SkImageEncoder::EncodeFile(pathWithNumber.c_str(), copy,
96                                                                  SkImageEncoder::kPNG_Type, 100);
97                        }
98                    }
99                }
100            }
101        }
102        return success;
103    }
104
105    SkString CopyTilesRenderer::getConfigNameInternal() {
106        return SkString("copy_tiles");
107    }
108}
109