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 "SkMultiPictureDraw.h"
14 #include "SkPicture.h"
15 #include "SkPixelRef.h"
16 #include "SkRect.h"
17 #include "SkString.h"
18 
19 namespace sk_tools {
20 #if SK_SUPPORT_GPU
CopyTilesRenderer(const GrContext::Options & opts,int x,int y)21     CopyTilesRenderer::CopyTilesRenderer(const GrContext::Options& opts, int x, int y)
22     : INHERITED(opts)
23     , fXTilesPerLargeTile(x)
24     , fYTilesPerLargeTile(y) { }
25 #else
26     CopyTilesRenderer::CopyTilesRenderer(int x, int y)
27     : fXTilesPerLargeTile(x)
28     , fYTilesPerLargeTile(y) { }
29 #endif
init(const SkPicture * pict,const SkString * writePath,const SkString * mismatchPath,const SkString * inputFilename,bool useChecksumBasedFilenames,bool useMultiPictureDraw)30     void CopyTilesRenderer::init(const SkPicture* pict, const SkString* writePath,
31                                  const SkString* mismatchPath, const SkString* inputFilename,
32                                  bool useChecksumBasedFilenames, bool useMultiPictureDraw) {
33         // Do not call INHERITED::init(), which would create a (potentially large) canvas which is
34         // not used by bench_pictures.
35         SkASSERT(pict != NULL);
36         // Only work with absolute widths (as opposed to percentages).
37         SkASSERT(this->getTileWidth() != 0 && this->getTileHeight() != 0);
38         fPicture.reset(pict)->ref();
39         this->CopyString(&fWritePath, writePath);
40         this->CopyString(&fMismatchPath, mismatchPath);
41         this->CopyString(&fInputFilename, inputFilename);
42         fUseChecksumBasedFilenames = useChecksumBasedFilenames;
43         fUseMultiPictureDraw = useMultiPictureDraw;
44         this->buildBBoxHierarchy();
45         // In order to avoid allocating a large canvas (particularly important for GPU), create one
46         // canvas that is a multiple of the tile size, and draw portions of the picture.
47         fLargeTileWidth = fXTilesPerLargeTile * this->getTileWidth();
48         fLargeTileHeight = fYTilesPerLargeTile * this->getTileHeight();
49         fCanvas.reset(this->INHERITED::setupCanvas(fLargeTileWidth, fLargeTileHeight));
50     }
51 
render(SkBitmap ** out)52     bool CopyTilesRenderer::render(SkBitmap** out) {
53         int i = 0;
54         bool success = true;
55         SkBitmap dst;
56         for (int x = 0; x < this->getViewWidth(); x += fLargeTileWidth) {
57             for (int y = 0; y < this->getViewHeight(); y += fLargeTileHeight) {
58                 SkAutoCanvasRestore autoRestore(fCanvas, true);
59                 // Translate so that we draw the correct portion of the picture.
60                 // Perform a postTranslate so that the scaleFactor does not interfere with the
61                 // positioning.
62                 SkMatrix mat(fCanvas->getTotalMatrix());
63                 mat.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
64                 fCanvas->setMatrix(mat);
65                 // Draw the picture
66                 if (fUseMultiPictureDraw) {
67                     SkMultiPictureDraw mpd;
68 
69                     mpd.add(fCanvas, fPicture);
70 
71                     mpd.draw();
72                 } else {
73                     fCanvas->drawPicture(fPicture);
74                 }
75                 // Now extract the picture into tiles
76                 SkBitmap baseBitmap;
77                 fCanvas->readPixels(SkIRect::MakeSize(fCanvas->getBaseLayerSize()), &baseBitmap);
78                 SkIRect subset;
79                 for (int tileY = 0; tileY < fLargeTileHeight; tileY += this->getTileHeight()) {
80                     for (int tileX = 0; tileX < fLargeTileWidth; tileX += this->getTileWidth()) {
81                         subset.set(tileX, tileY, tileX + this->getTileWidth(),
82                                    tileY + this->getTileHeight());
83                         SkDEBUGCODE(bool extracted =)
84                         baseBitmap.extractSubset(&dst, subset);
85                         SkASSERT(extracted);
86                         if (!fWritePath.isEmpty()) {
87                             // Similar to write() in PictureRenderer.cpp, but just encodes
88                             // a bitmap directly.
89                             // TODO: Share more common code with write() to do this, to properly
90                             // write out the JSON summary, etc.
91                             SkString pathWithNumber = SkOSPath::Join(fWritePath.c_str(),
92                                                                      fInputFilename.c_str());
93                             pathWithNumber.remove(pathWithNumber.size() - 4, 4);
94                             pathWithNumber.appendf("%i.png", i++);
95                             SkBitmap copy;
96 #if SK_SUPPORT_GPU
97                             if (isUsingGpuDevice()) {
98                                 dst.pixelRef()->readPixels(&copy, &subset);
99                             } else {
100 #endif
101                                 dst.copyTo(&copy);
102 #if SK_SUPPORT_GPU
103                             }
104 #endif
105                             success &= SkImageEncoder::EncodeFile(pathWithNumber.c_str(), copy,
106                                                                   SkImageEncoder::kPNG_Type, 100);
107                         }
108                     }
109                 }
110             }
111         }
112         return success;
113     }
114 
getConfigNameInternal()115     SkString CopyTilesRenderer::getConfigNameInternal() {
116         return SkString("copy_tiles");
117     }
118 }
119